removed 'application' folder to have everything in the root directory
This commit is contained in:
		
							
								
								
									
										196
									
								
								core/Files/AsynchronousFileReader.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										196
									
								
								core/Files/AsynchronousFileReader.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,196 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace ManiaControl\Files;
 | 
			
		||||
 | 
			
		||||
use cURL\Event;
 | 
			
		||||
use cURL\Request;
 | 
			
		||||
use ManiaControl\ManiaControl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Asynchronous File Reader
 | 
			
		||||
 *
 | 
			
		||||
 * @author    ManiaControl Team <mail@maniacontrol.com>
 | 
			
		||||
 * @copyright 2014 ManiaControl Team
 | 
			
		||||
 * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3
 | 
			
		||||
 */
 | 
			
		||||
class AsynchronousFileReader {
 | 
			
		||||
	/*
 | 
			
		||||
	 * Constants
 | 
			
		||||
	 */
 | 
			
		||||
	const CONTENT_TYPE_JSON = 'application/json';
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Private properties
 | 
			
		||||
	 */
 | 
			
		||||
	/** @var ManiaControl $maniaControl */
 | 
			
		||||
	private $maniaControl = null;
 | 
			
		||||
	/** @var Request[] $requests */
 | 
			
		||||
	private $requests = array();
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Construct a new Asynchronous File Reader Instance
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param ManiaControl $maniaControl
 | 
			
		||||
	 */
 | 
			
		||||
	public function __construct(ManiaControl $maniaControl) {
 | 
			
		||||
		$this->maniaControl = $maniaControl;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static function newRequestTest($url) {
 | 
			
		||||
		$request = new Request($url);
 | 
			
		||||
		$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false) // don't display response header
 | 
			
		||||
		        ->set(CURLOPT_CRLF, true) // linux line feed
 | 
			
		||||
		        ->set(CURLOPT_ENCODING, '') // accept encoding
 | 
			
		||||
		        ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
 | 
			
		||||
		        ->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
 | 
			
		||||
		return $request;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Append available Data of active Requests
 | 
			
		||||
	 */
 | 
			
		||||
	public function appendData() {
 | 
			
		||||
		foreach ($this->requests as $key => $request) {
 | 
			
		||||
			if ($request->socketPerform()) {
 | 
			
		||||
				$request->socketSelect();
 | 
			
		||||
			} else {
 | 
			
		||||
				unset($this->requests[$key]);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Load a Remote File
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string   $url
 | 
			
		||||
	 * @param callable $function
 | 
			
		||||
	 * @param string   $contentType
 | 
			
		||||
	 * @param int      $keepAlive
 | 
			
		||||
	 */
 | 
			
		||||
	public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0) {
 | 
			
		||||
		$headers = array();
 | 
			
		||||
		array_push($headers, 'Content-Type: ' . $contentType);
 | 
			
		||||
		if ($keepAlive) {
 | 
			
		||||
			array_push($headers, 'Keep-Alive: ' . $keepAlive);
 | 
			
		||||
			array_push($headers, 'Connection: Keep-Alive');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$request = $this->newRequest($url);
 | 
			
		||||
		$request->getOptions()->set(CURLOPT_AUTOREFERER, true) // accept link reference
 | 
			
		||||
		        ->set(CURLOPT_HTTPHEADER, $headers); // headers
 | 
			
		||||
 | 
			
		||||
		$request->addListener('complete', function (Event $event) use (&$function) {
 | 
			
		||||
			$error   = null;
 | 
			
		||||
			$content = null;
 | 
			
		||||
			if ($event->response->hasError()) {
 | 
			
		||||
				$error = $event->response->getError()->getMessage();
 | 
			
		||||
			} else {
 | 
			
		||||
				$content = $event->response->getContent();
 | 
			
		||||
			}
 | 
			
		||||
			call_user_func($function, $content, $error);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		$this->addRequest($request);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new cURL Request for the given URL
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $url
 | 
			
		||||
	 * @return Request
 | 
			
		||||
	 */
 | 
			
		||||
	protected function newRequest($url) {
 | 
			
		||||
		$request = new Request($url);
 | 
			
		||||
		$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false) // don't display response header
 | 
			
		||||
		        ->set(CURLOPT_CRLF, true) // linux line feed
 | 
			
		||||
		        ->set(CURLOPT_ENCODING, '') // accept encoding
 | 
			
		||||
		        ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // user-agent
 | 
			
		||||
		        ->set(CURLOPT_RETURNTRANSFER, true); // return instead of output content
 | 
			
		||||
		return $request;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//TODO remove, they are just for testing dedimania
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Add a Request to the queue
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param Request $request
 | 
			
		||||
	 */
 | 
			
		||||
	protected function addRequest(Request $request) {
 | 
			
		||||
		array_push($this->requests, $request);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function postDataTest(Request $request, $url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;') {
 | 
			
		||||
 | 
			
		||||
		$headers = array();
 | 
			
		||||
		array_push($headers, 'Content-Type: ' . $contentType);
 | 
			
		||||
		array_push($headers, 'Keep-Alive: timeout=600, max=2000');
 | 
			
		||||
		array_push($headers, 'Connection: Keep-Alive');
 | 
			
		||||
 | 
			
		||||
		$content = str_replace(array("\r", "\n"), '', $content);
 | 
			
		||||
		if ($compression) {
 | 
			
		||||
			$content = zlib_encode($content, 31);
 | 
			
		||||
			array_push($headers, 'Content-Encoding: gzip');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$request->getOptions()->set(CURLOPT_POST, true) // post method
 | 
			
		||||
		        ->set(CURLOPT_POSTFIELDS, $content) // post content field
 | 
			
		||||
		        ->set(CURLOPT_HTTPHEADER, $headers) // headers
 | 
			
		||||
		;
 | 
			
		||||
		$request->addListener('complete', function (Event $event) use (&$function) {
 | 
			
		||||
			$error   = null;
 | 
			
		||||
			$content = null;
 | 
			
		||||
			if ($event->response->hasError()) {
 | 
			
		||||
				$error = $event->response->getError()->getMessage();
 | 
			
		||||
			} else {
 | 
			
		||||
				$content = $event->response->getContent();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			call_user_func($function, $content, $error);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		$this->addRequest($request);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send Data via POST Method
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string   $url
 | 
			
		||||
	 * @param callable $function
 | 
			
		||||
	 * @param string   $content
 | 
			
		||||
	 * @param bool     $compression
 | 
			
		||||
	 * @param string   $contentType
 | 
			
		||||
	 */
 | 
			
		||||
	public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;') {
 | 
			
		||||
 | 
			
		||||
		$headers = array();
 | 
			
		||||
		array_push($headers, 'Content-Type: ' . $contentType);
 | 
			
		||||
		array_push($headers, 'Keep-Alive: timeout=600, max=2000');
 | 
			
		||||
		array_push($headers, 'Connection: Keep-Alive');
 | 
			
		||||
 | 
			
		||||
		$content = str_replace(array("\r", "\n"), '', $content);
 | 
			
		||||
		if ($compression) {
 | 
			
		||||
			$content = zlib_encode($content, 31);
 | 
			
		||||
			array_push($headers, 'Content-Encoding: gzip');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		$request = $this->newRequest($url);
 | 
			
		||||
		$request->getOptions()->set(CURLOPT_POST, true) // post method
 | 
			
		||||
		        ->set(CURLOPT_POSTFIELDS, $content) // post content field
 | 
			
		||||
		        ->set(CURLOPT_HTTPHEADER, $headers) // headers
 | 
			
		||||
		;
 | 
			
		||||
		$request->addListener('complete', function (Event $event) use (&$function) {
 | 
			
		||||
			$error   = null;
 | 
			
		||||
			$content = null;
 | 
			
		||||
			if ($event->response->hasError()) {
 | 
			
		||||
				$error = $event->response->getError()->getMessage();
 | 
			
		||||
			} else {
 | 
			
		||||
				$content = $event->response->getContent();
 | 
			
		||||
			}
 | 
			
		||||
			call_user_func($function, $content, $error);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		$this->addRequest($request);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										131
									
								
								core/Files/BackupUtil.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								core/Files/BackupUtil.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,131 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace ManiaControl\Files;
 | 
			
		||||
 | 
			
		||||
use ManiaControl\Logger;
 | 
			
		||||
use ManiaControl\ManiaControl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Backup Utility Class
 | 
			
		||||
 *
 | 
			
		||||
 * @author    ManiaControl Team <mail@maniacontrol.com>
 | 
			
		||||
 * @copyright 2014 ManiaControl Team
 | 
			
		||||
 * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3
 | 
			
		||||
 */
 | 
			
		||||
abstract class BackupUtil {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Perform a Full Backup of ManiaControl
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function performFullBackup() {
 | 
			
		||||
		$backupFolder = self::getBackupFolder();
 | 
			
		||||
		if (!$backupFolder) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d_H-i') . '_' . time() . '.zip';
 | 
			
		||||
		$backupZip      = new \ZipArchive();
 | 
			
		||||
		if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
 | 
			
		||||
			Logger::logError("Couldn't create backup zip!");
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$excludes      = array();
 | 
			
		||||
		$baseFileNames = array('configs', 'core', 'plugins', 'ManiaControl.php');
 | 
			
		||||
		$pathInfo      = pathInfo(MANIACONTROL_PATH);
 | 
			
		||||
		$parentPath    = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
 | 
			
		||||
		$dirName       = $pathInfo['basename'];
 | 
			
		||||
		$backupZip->addEmptyDir($dirName);
 | 
			
		||||
		self::zipDirectory($backupZip, MANIACONTROL_PATH, strlen($parentPath), $excludes, $baseFileNames);
 | 
			
		||||
		return $backupZip->close();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the Backup Folder Path and create it if necessary
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return string|bool
 | 
			
		||||
	 */
 | 
			
		||||
	private static function getBackupFolder() {
 | 
			
		||||
		$backupFolder = MANIACONTROL_PATH . 'backup' . DIRECTORY_SEPARATOR;
 | 
			
		||||
		if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
 | 
			
		||||
			Logger::logError("Couldn't create backup folder!");
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		if (!is_writeable($backupFolder)) {
 | 
			
		||||
			Logger::logError("ManiaControl doesn't have the necessary write rights for the backup folder!");
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		return $backupFolder;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Add a Directory to the ZipArchive
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param \ZipArchive $zipArchive
 | 
			
		||||
	 * @param string      $folderName
 | 
			
		||||
	 * @param int         $prefixLength
 | 
			
		||||
	 * @param array       $excludes
 | 
			
		||||
	 * @param array       $baseFileNames
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array(), array $baseFileNames = array()) {
 | 
			
		||||
		$folderHandle = opendir($folderName);
 | 
			
		||||
		if (!is_resource($folderHandle)) {
 | 
			
		||||
			Logger::logError("Couldn't open folder '{$folderName}' for backup!");
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$useBaseFileNames = !empty($baseFileNames);
 | 
			
		||||
		while (false !== ($file = readdir($folderHandle))) {
 | 
			
		||||
			if (substr($file, 0, 1) === '.') {
 | 
			
		||||
				// Skip such .files
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			if (in_array($file, $excludes)) {
 | 
			
		||||
				// Excluded
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			if ($useBaseFileNames && !in_array($file, $baseFileNames)) {
 | 
			
		||||
				// Not one of the base files
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			$filePath  = $folderName . DIRECTORY_SEPARATOR . $file;
 | 
			
		||||
			$localPath = substr($filePath, $prefixLength);
 | 
			
		||||
			if (is_file($filePath)) {
 | 
			
		||||
				$zipArchive->addFile($filePath, $localPath);
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			if (is_dir($filePath)) {
 | 
			
		||||
				$zipArchive->addEmptyDir($localPath);
 | 
			
		||||
				self::zipDirectory($zipArchive, $filePath, $prefixLength, $excludes);
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		closedir($folderHandle);
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Perform a Backup of the Plugins
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function performPluginsBackup() {
 | 
			
		||||
		$backupFolder = self::getBackupFolder();
 | 
			
		||||
		if (!$backupFolder) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$backupFileName = $backupFolder . 'backup_plugins_' . ManiaControl::VERSION . date('y-m-d_H-i') . '_' . time() . '.zip';
 | 
			
		||||
		$backupZip      = new \ZipArchive();
 | 
			
		||||
		if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
 | 
			
		||||
			Logger::logError("Couldn't create backup zip!");
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$directory  = MANIACONTROL_PATH . 'plugins';
 | 
			
		||||
		$pathInfo   = pathInfo($directory);
 | 
			
		||||
		$parentPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR;
 | 
			
		||||
		$dirName    = $pathInfo['basename'];
 | 
			
		||||
		$backupZip->addEmptyDir($dirName);
 | 
			
		||||
		self::zipDirectory($backupZip, $directory, strlen($parentPath));
 | 
			
		||||
		return $backupZip->close();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										210
									
								
								core/Files/FileUtil.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										210
									
								
								core/Files/FileUtil.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,210 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace ManiaControl\Files;
 | 
			
		||||
 | 
			
		||||
use ManiaControl\Logger;
 | 
			
		||||
use ManiaControl\Utils\Formatter;
 | 
			
		||||
use ManiaControl\Utils\WebReader;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Files Utility Class
 | 
			
		||||
 *
 | 
			
		||||
 * @author    ManiaControl Team <mail@maniacontrol.com>
 | 
			
		||||
 * @copyright 2014 ManiaControl Team
 | 
			
		||||
 * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3
 | 
			
		||||
 */
 | 
			
		||||
abstract class FileUtil {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @deprecated
 | 
			
		||||
	 * @see \ManiaControl\Utils\WebReader::loadUrl()
 | 
			
		||||
	 */
 | 
			
		||||
	public static function loadFile($url) {
 | 
			
		||||
		$response = WebReader::getUrl($url);
 | 
			
		||||
		return $response->getContent();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Load Config XML File
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $fileName
 | 
			
		||||
	 * @return \SimpleXMLElement
 | 
			
		||||
	 */
 | 
			
		||||
	public static function loadConfig($fileName) {
 | 
			
		||||
		$fileLocation = MANIACONTROL_PATH . 'configs' . DIRECTORY_SEPARATOR . $fileName;
 | 
			
		||||
		if (!file_exists($fileLocation)) {
 | 
			
		||||
			Logger::log("Config file doesn't exist! ({$fileName})");
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
		if (!is_readable($fileLocation)) {
 | 
			
		||||
			Logger::log("Config file isn't readable! Please check the file permissions. ({$fileName})");
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
		$configXml = @simplexml_load_file($fileLocation);
 | 
			
		||||
		if (!$configXml) {
 | 
			
		||||
			Logger::log("Config file isn't maintained properly! ({$fileName})");
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
		return $configXml;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Return file name cleared from special characters
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $fileName
 | 
			
		||||
	 * @return string
 | 
			
		||||
	 */
 | 
			
		||||
	public static function getClearedFileName($fileName) {
 | 
			
		||||
		$fileName = Formatter::stripCodes($fileName);
 | 
			
		||||
		$fileName = Formatter::utf8($fileName);
 | 
			
		||||
		$fileName = preg_replace('/[^0-9A-Za-z\-\+\.\_\ ]/', null, $fileName);
 | 
			
		||||
		$fileName = preg_replace('/ /', '_', $fileName);
 | 
			
		||||
		return $fileName;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Delete the temporary folder if it's empty
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function deleteTempFolder() {
 | 
			
		||||
		return self::deleteFolder(self::getTempFolder());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return string|bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function getTempFolder() {
 | 
			
		||||
		$tempFolder = MANIACONTROL_PATH . 'temp' . DIRECTORY_SEPARATOR;
 | 
			
		||||
		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;
 | 
			
		||||
		}
 | 
			
		||||
		return $tempFolder;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Check if ManiaControl has sufficient Access to write to Files in the given Directories
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param mixed $directories
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function checkWritePermissions($directories) {
 | 
			
		||||
		if (!is_array($directories)) {
 | 
			
		||||
			$directories = array($directories);
 | 
			
		||||
		}
 | 
			
		||||
		foreach ($directories as $directory) {
 | 
			
		||||
			$dir = new \RecursiveDirectoryIterator(MANIACONTROL_PATH . $directory);
 | 
			
		||||
			foreach (new \RecursiveIteratorIterator($dir) as $fileName => $file) {
 | 
			
		||||
				if (substr($fileName, 0, 1) === '.') {
 | 
			
		||||
					continue;
 | 
			
		||||
				}
 | 
			
		||||
				if (!is_writable($fileName)) {
 | 
			
		||||
					Logger::log("Write access missing for file '{$fileName}'!");
 | 
			
		||||
					return false;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Clean the given directory by deleting old files
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $directory
 | 
			
		||||
	 * @param float  $maxFileAgeInDays
 | 
			
		||||
	 * @param bool   $recursive
 | 
			
		||||
	 * @return bool
 | 
			
		||||
	 */
 | 
			
		||||
	public static function cleanDirectory($directory, $maxFileAgeInDays = 10., $recursive = false) {
 | 
			
		||||
		if (!$directory || !is_dir($directory) || !is_readable($directory)) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$dirHandle = opendir($directory);
 | 
			
		||||
		if (!is_resource($dirHandle)) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
		$directory = self::appendDirectorySeparator($directory);
 | 
			
		||||
		$time      = time();
 | 
			
		||||
		while ($fileName = readdir($dirHandle)) {
 | 
			
		||||
			$filePath = $directory . $fileName;
 | 
			
		||||
			if (!is_readable($filePath)) {
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
			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);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		closedir($dirHandle);
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 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');
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user