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-15 13:04:05 +02:00
|
|
|
use ManiaControl\Utils\Formatter;
|
2014-06-27 01:06:11 +02:00
|
|
|
use ManiaControl\Utils\WebReader;
|
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>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @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
|
|
|
/**
|
2014-06-27 00:42:19 +02:00
|
|
|
* @deprecated
|
2014-06-27 01:06:11 +02:00
|
|
|
* @see \ManiaControl\Utils\WebReader::loadUrl()
|
2013-12-23 16:14:03 +01:00
|
|
|
*/
|
2014-06-27 00:42:19 +02:00
|
|
|
public static function loadFile($url) {
|
2014-08-25 15:29:45 +02:00
|
|
|
$response = WebReader::getUrl($url);
|
2014-06-27 00:42:19 +02:00
|
|
|
return $response->getContent();
|
2013-12-23 16:14:03 +01:00
|
|
|
}
|
2014-01-06 14:22:48 +01:00
|
|
|
|
2013-11-10 13:58:08 +01:00
|
|
|
/**
|
2014-06-20 15:17:52 +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-08-08 14:41:58 +02:00
|
|
|
$fileLocation = MANIACONTROL_PATH . 'configs' . DIRECTORY_SEPARATOR . $fileName;
|
2013-11-10 13:58:08 +01:00
|
|
|
if (!file_exists($fileLocation)) {
|
2014-06-20 15:17:52 +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-20 15:17:52 +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-06-23 15:51:39 +02:00
|
|
|
$fileName = Formatter::utf8($fileName);
|
2014-06-29 22:18:14 +02:00
|
|
|
$fileName = preg_replace('/[^0-9A-Za-z\-\+\.\_\ ]/', null, $fileName);
|
|
|
|
$fileName = preg_replace('/ /', '_', $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
|
|
|
/**
|
2014-06-20 18:51:25 +02:00
|
|
|
* Delete the temporary folder if it's empty
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-06-20 18:51:25 +02:00
|
|
|
public static function deleteTempFolder() {
|
|
|
|
return self::deleteFolder(self::getTempFolder());
|
2014-05-02 17:40:47 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 04:02:42 +02:00
|
|
|
/**
|
2014-06-20 18:51:25 +02:00
|
|
|
* Delete the given folder if it's empty
|
|
|
|
*
|
|
|
|
* @param string $folderPath
|
|
|
|
* @param bool $onlyIfEmpty
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function deleteFolder($folderPath, $onlyIfEmpty = true) {
|
|
|
|
if ($onlyIfEmpty && !self::isFolderEmpty($folderPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rmdir($folderPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given folder is empty
|
|
|
|
*
|
|
|
|
* @param string $folderPath
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isFolderEmpty($folderPath) {
|
|
|
|
if (!is_readable($folderPath) || !is_dir($folderPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$files = scandir($folderPath);
|
|
|
|
return (count($files) <= 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the temporary folder and create it if necessary
|
2014-05-02 17:40:47 +02:00
|
|
|
*
|
2014-06-20 15:17:52 +02:00
|
|
|
* @return string|bool
|
2014-05-02 04:02:42 +02:00
|
|
|
*/
|
2014-06-20 15:17:52 +02:00
|
|
|
public static function getTempFolder() {
|
2014-08-08 14:41:58 +02:00
|
|
|
$tempFolder = MANIACONTROL_PATH . 'temp' . DIRECTORY_SEPARATOR;
|
2014-06-20 15:17:52 +02:00
|
|
|
if (!is_dir($tempFolder) && !mkdir($tempFolder)) {
|
|
|
|
trigger_error("Couldn't create the temp folder!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_writeable($tempFolder)) {
|
|
|
|
trigger_error("ManiaControl doesn't have the necessary write rights for the temp folder!");
|
|
|
|
return false;
|
2014-05-02 04:02:42 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
foreach ($directories as $directory) {
|
2014-08-08 14:41:58 +02:00
|
|
|
$dir = new \RecursiveDirectoryIterator(MANIACONTROL_PATH . $directory);
|
2014-05-02 04:02:42 +02:00
|
|
|
foreach (new \RecursiveIteratorIterator($dir) as $fileName => $file) {
|
2017-04-16 16:41:30 +02:00
|
|
|
if (self::isHiddenFile($fileName)) {
|
2014-05-02 04:02:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_writable($fileName)) {
|
2014-06-20 15:17:52 +02:00
|
|
|
Logger::log("Write access missing for file '{$fileName}'!");
|
2014-05-02 04:02:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-03 21:47:53 +02:00
|
|
|
|
2014-06-20 14:57:02 +02:00
|
|
|
/**
|
2014-06-20 16:51:08 +02:00
|
|
|
* Clean the given directory by deleting old files
|
2014-06-20 14:57:02 +02:00
|
|
|
*
|
2014-06-20 16:51:08 +02:00
|
|
|
* @param string $directory
|
|
|
|
* @param float $maxFileAgeInDays
|
|
|
|
* @param bool $recursive
|
2014-06-20 14:57:02 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-06-20 16:51:08 +02:00
|
|
|
public static function cleanDirectory($directory, $maxFileAgeInDays = 10., $recursive = false) {
|
2014-08-05 01:59:53 +02:00
|
|
|
if (!$directory || !is_dir($directory) || !is_readable($directory)) {
|
2014-06-20 14:57:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 16:51:08 +02:00
|
|
|
$dirHandle = opendir($directory);
|
2014-06-20 14:57:02 +02:00
|
|
|
if (!is_resource($dirHandle)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 18:51:25 +02:00
|
|
|
$directory = self::appendDirectorySeparator($directory);
|
|
|
|
$time = time();
|
2014-06-20 14:57:02 +02:00
|
|
|
while ($fileName = readdir($dirHandle)) {
|
2014-06-20 16:51:08 +02:00
|
|
|
$filePath = $directory . $fileName;
|
2014-06-20 14:57:02 +02:00
|
|
|
if (!is_readable($filePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-20 16:51:08 +02:00
|
|
|
if (is_dir($filePath) && $recursive) {
|
|
|
|
// Directory
|
|
|
|
self::cleanDirectory($filePath . DIRECTORY_SEPARATOR, $maxFileAgeInDays, $recursive);
|
|
|
|
} else if (is_file($filePath)) {
|
|
|
|
// File
|
|
|
|
if (!is_writable($filePath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$fileModTime = filemtime($filePath);
|
|
|
|
$timeDeltaDays = ($time - $fileModTime) / (24. * 3600.);
|
|
|
|
if ($timeDeltaDays > $maxFileAgeInDays) {
|
|
|
|
unlink($filePath);
|
|
|
|
}
|
2014-06-20 14:57:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dirHandle);
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-20 18:51:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the directory separator to the given path if necessary
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function appendDirectorySeparator($path) {
|
|
|
|
if (substr($path, -1, 1) !== DIRECTORY_SEPARATOR) {
|
|
|
|
$path .= DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
2014-08-07 13:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given file name is a PHP file
|
|
|
|
*
|
|
|
|
* @param string $fileName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isPhpFileName($fileName) {
|
|
|
|
$extension = substr($fileName, -4);
|
|
|
|
return (strtolower($extension) === '.php');
|
|
|
|
}
|
2017-04-02 16:20:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the file name of a path to a PHP file
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getFileName($path) {
|
|
|
|
$className = '';
|
|
|
|
$splitNameSpace = explode(DIRECTORY_SEPARATOR, $path);
|
|
|
|
if (is_array($splitNameSpace)) {
|
|
|
|
$className = end($splitNameSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = str_replace('.php', '', $className);
|
|
|
|
|
|
|
|
return $className;
|
|
|
|
}
|
2017-04-16 16:41:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a File is Hidden
|
|
|
|
*
|
|
|
|
* @param $fileName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isHiddenFile($fileName) {
|
|
|
|
return (substr($fileName, 0, 1) === '.');
|
|
|
|
}
|
2019-04-27 19:22:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortens a path.
|
|
|
|
* Opposed to realpath, it follows symbolic links.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-02-25 17:48:27 +01:00
|
|
|
public static function shortenPath($path) {
|
2019-04-27 19:22:42 +02:00
|
|
|
$root = substr($path, 0, 1) === '/';
|
|
|
|
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
|
|
|
|
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
|
|
|
|
$absolutes = array();
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
if ('.' === $part)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ('..' === $part)
|
|
|
|
array_pop($absolutes);
|
|
|
|
else
|
|
|
|
array_push($absolutes, $part);
|
|
|
|
}
|
|
|
|
$path = implode(DIRECTORY_SEPARATOR, $absolutes);
|
|
|
|
if ($root)
|
|
|
|
$path = '/'.$path;
|
|
|
|
return $path;
|
|
|
|
}
|
2013-11-10 13:58:08 +01:00
|
|
|
}
|