2013-11-10 13:58:08 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-07 00:26:32 +01:00
|
|
|
namespace ManiaControl\Files;
|
|
|
|
|
2014-06-17 22:59:49 +02:00
|
|
|
use ManiaControl\Logger;
|
2014-05-02 17:40:47 +02:00
|
|
|
use ManiaControl\ManiaControl;
|
2014-05-15 13:04:05 +02:00
|
|
|
use ManiaControl\Utils\Formatter;
|
2013-11-10 13:58:08 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-12 12:14:37 +02:00
|
|
|
* Files Utility Class
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2013-11-10 13:58:08 +01:00
|
|
|
*/
|
2013-11-27 02:42:39 +01:00
|
|
|
abstract class FileUtil {
|
2014-06-20 14:57:02 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
/**
|
|
|
|
* Load a remote file
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2013-12-23 16:14:03 +01:00
|
|
|
* @param string $url
|
2013-12-27 21:10:53 +01:00
|
|
|
* @param string $contentType
|
2014-05-24 21:49:38 +02:00
|
|
|
* @return string
|
2013-12-23 16:14:03 +01:00
|
|
|
*/
|
2013-12-27 21:10:53 +01:00
|
|
|
public static function loadFile($url, $contentType = 'UTF-8') {
|
2013-12-23 16:14:03 +01:00
|
|
|
if (!$url) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
$urlData = parse_url($url);
|
2014-06-12 16:41:26 +02:00
|
|
|
$host = $urlData['host'];
|
2014-05-02 17:40:47 +02:00
|
|
|
$port = (isset($urlData['port']) ? $urlData['port'] : 80);
|
2014-05-24 21:49:38 +02:00
|
|
|
$urlQuery = (isset($urlData['query']) ? '?' . $urlData['query'] : '');
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-06-12 16:41:26 +02:00
|
|
|
$fsock = fsockopen($host, $port);
|
|
|
|
if (!is_resource($fsock)) {
|
|
|
|
trigger_error("Couldn't open socket connection to '{$host}' on port '{$port}'!");
|
|
|
|
return null;
|
|
|
|
}
|
2013-12-23 16:14:03 +01:00
|
|
|
stream_set_timeout($fsock, 3);
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
$query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.0' . PHP_EOL;
|
2014-06-12 16:41:26 +02:00
|
|
|
$query .= 'Host: ' . $host . PHP_EOL;
|
2013-12-27 21:10:53 +01:00
|
|
|
$query .= 'Content-Type: ' . $contentType . PHP_EOL;
|
2013-12-23 16:14:03 +01:00
|
|
|
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;
|
|
|
|
$query .= PHP_EOL;
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
fwrite($fsock, $query);
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
$buffer = '';
|
2014-05-02 17:40:47 +02:00
|
|
|
$info = array('timed_out' => false);
|
2014-05-02 04:02:42 +02:00
|
|
|
while (!feof($fsock) && !$info['timed_out']) {
|
2013-12-23 16:14:03 +01:00
|
|
|
$buffer .= fread($fsock, 1024);
|
|
|
|
$info = stream_get_meta_data($fsock);
|
|
|
|
}
|
|
|
|
fclose($fsock);
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
if ($info['timed_out'] || !$buffer) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-14 14:32:29 +02:00
|
|
|
if (substr($buffer, 9, 3) !== '200') {
|
2013-12-23 16:14:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
$result = explode("\r\n\r\n", $buffer, 2);
|
|
|
|
if (count($result) < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2013-12-23 16:14:03 +01:00
|
|
|
return $result[1];
|
|
|
|
}
|
2014-01-06 14:22:48 +01:00
|
|
|
|
2013-11-10 13:58:08 +01:00
|
|
|
/**
|
2014-06-13 19:53:36 +02:00
|
|
|
* Load config xml-file
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-01-06 14:22:48 +01:00
|
|
|
* @param string $fileName
|
2014-06-13 19:53:36 +02:00
|
|
|
* @return \SimpleXMLElement
|
2013-11-10 13:58:08 +01:00
|
|
|
*/
|
|
|
|
public static function loadConfig($fileName) {
|
2014-05-03 21:47:53 +02:00
|
|
|
$fileLocation = ManiaControlDir . 'configs' . DIRECTORY_SEPARATOR . $fileName;
|
2013-11-10 13:58:08 +01:00
|
|
|
if (!file_exists($fileLocation)) {
|
2014-06-17 22:59:49 +02:00
|
|
|
Logger::log("Config File doesn't exist! ({$fileName})");
|
2013-11-10 13:58:08 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-24 23:55:54 +01:00
|
|
|
if (!is_readable($fileLocation)) {
|
2014-06-17 22:59:49 +02:00
|
|
|
Logger::log("Config File isn't readable! Please check the File Permissions. ({$fileName})");
|
2013-11-24 23:55:54 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-13 22:09:25 +02:00
|
|
|
$configXml = @simplexml_load_file($fileLocation);
|
|
|
|
if (!$configXml) {
|
2014-06-17 22:59:49 +02:00
|
|
|
Logger::log("Config file isn't maintained properly! ({$fileName})");
|
2014-06-13 22:09:25 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $configXml;
|
2013-11-10 13:58:08 +01:00
|
|
|
}
|
2013-11-27 02:42:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return file name cleared from special characters
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-01-06 14:22:48 +01:00
|
|
|
* @param string $fileName
|
2013-11-27 02:42:39 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getClearedFileName($fileName) {
|
2014-03-31 21:04:15 +02:00
|
|
|
$fileName = Formatter::stripCodes($fileName);
|
2014-05-24 21:53:07 +02:00
|
|
|
$fileName = str_replace(array(DIRECTORY_SEPARATOR, '\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName);
|
2014-05-02 04:02:42 +02:00
|
|
|
$fileName = preg_replace('/[^[:print:]]/', '', $fileName);
|
2014-03-31 21:04:15 +02:00
|
|
|
return $fileName;
|
2013-11-27 02:42:39 +01:00
|
|
|
}
|
2014-05-02 04:02:42 +02:00
|
|
|
|
2014-05-02 17:40:47 +02:00
|
|
|
/**
|
|
|
|
* Delete the Temporary Folder if it's empty
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function removeTempFolder() {
|
|
|
|
$tempFolder = self::getTempFolder(false);
|
|
|
|
return @rmdir($tempFolder);
|
|
|
|
}
|
|
|
|
|
2014-05-02 04:02:42 +02:00
|
|
|
/**
|
|
|
|
* Get the Temporary Folder and create it if necessary
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-02 04:02:42 +02:00
|
|
|
* @param bool $createIfNecessary
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getTempFolder($createIfNecessary = true) {
|
2014-05-24 21:49:38 +02:00
|
|
|
$tempFolder = ManiaControlDir . 'temp' . DIRECTORY_SEPARATOR;
|
2014-05-02 04:02:42 +02:00
|
|
|
if ($createIfNecessary && !is_dir($tempFolder)) {
|
|
|
|
mkdir($tempFolder);
|
|
|
|
}
|
|
|
|
return $tempFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if ManiaControl has sufficient Access to write to Files in the given Directories
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-05-02 04:02:42 +02:00
|
|
|
* @param mixed $directories
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function checkWritePermissions($directories) {
|
|
|
|
if (!is_array($directories)) {
|
|
|
|
$directories = array($directories);
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:02:42 +02:00
|
|
|
foreach ($directories as $directory) {
|
|
|
|
$dir = new \RecursiveDirectoryIterator(ManiaControlDir . $directory);
|
|
|
|
foreach (new \RecursiveIteratorIterator($dir) as $fileName => $file) {
|
|
|
|
if (substr($fileName, 0, 1) === '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_writable($fileName)) {
|
|
|
|
$message = "Write-Access missing for File '{$fileName}'!";
|
2014-06-17 22:59:49 +02:00
|
|
|
Logger::log($message);
|
2014-05-02 04:02:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 17:40:47 +02:00
|
|
|
|
2014-05-02 04:02:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-03 21:47:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to delete the given Plugin Files
|
|
|
|
*
|
|
|
|
* @param mixed $pluginFileNames
|
|
|
|
*/
|
|
|
|
public static function cleanPluginFiles($pluginFileNames) {
|
2014-05-27 22:17:23 +02:00
|
|
|
$pluginFileNames = (array)$pluginFileNames;
|
2014-05-03 21:47:53 +02:00
|
|
|
$fileNames = array();
|
|
|
|
foreach ($pluginFileNames as $pluginFileName) {
|
|
|
|
$fileName = 'plugins' . DIRECTORY_SEPARATOR . $pluginFileName;
|
|
|
|
array_push($fileNames, $fileName);
|
|
|
|
}
|
|
|
|
self::cleanFiles($fileNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to delete the given Files
|
|
|
|
*
|
|
|
|
* @param mixed $fileNames
|
|
|
|
*/
|
|
|
|
public static function cleanFiles($fileNames) {
|
2014-05-27 22:17:23 +02:00
|
|
|
$fileNames = (array)$fileNames;
|
2014-05-03 21:47:53 +02:00
|
|
|
foreach ($fileNames as $fileName) {
|
|
|
|
$filePath = ManiaControlDir . $fileName;
|
|
|
|
if (file_exists($filePath) && is_writeable($filePath)) {
|
|
|
|
unlink($filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 14:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete old ManiaControl Log Files
|
|
|
|
*
|
|
|
|
* @param int $maxFileAgeInDays
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function deleteOldLogFiles($maxFileAgeInDays = 14) {
|
|
|
|
$logsFolderPath = Logger::getLogsFolderPath();
|
|
|
|
if (!is_readable($logsFolderPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$dirHandle = opendir($logsFolderPath);
|
|
|
|
if (!is_resource($dirHandle)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$time = time();
|
|
|
|
while ($fileName = readdir($dirHandle)) {
|
|
|
|
$filePath = $logsFolderPath . $fileName;
|
|
|
|
if (!is_readable($filePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$fileModTime = filemtime($filePath);
|
|
|
|
$timeDeltaDays = ($time - $fileModTime) / (24 * 3600);
|
|
|
|
if ($timeDeltaDays > $maxFileAgeInDays) {
|
|
|
|
unlink($filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dirHandle);
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-10 13:58:08 +01:00
|
|
|
}
|