temp folder and write permissions methods

This commit is contained in:
Steffen Schröder 2014-05-02 04:02:42 +02:00
parent f3086956b2
commit d5e6d1ca8b

View File

@ -7,16 +7,20 @@ use ManiaControl\Formatter;
/** /**
* Files Utility Class * Files Utility Class
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
abstract class FileUtil { abstract class FileUtil {
/*
* Constants
*/
const FOLDER_NAME_TEMP = '/temp/';
/** /**
* Load a remote file * Load a remote file
* *
* @param string $url * @param string $url
* @param string $contentType * @param string $contentType
* @return string || null * @return string || null
@ -25,48 +29,48 @@ abstract class FileUtil {
if (!$url) { if (!$url) {
return null; return null;
} }
$urlData = parse_url($url); $urlData = parse_url($url);
$port = (isset($urlData['port']) ? $urlData['port'] : 80); $port = (isset($urlData['port']) ? $urlData['port'] : 80);
$urlQuery = isset($urlData['query']) ? "?" . $urlData['query'] : ""; $urlQuery = isset($urlData['query']) ? "?" . $urlData['query'] : "";
$fsock = fsockopen($urlData['host'], $port); $fsock = fsockopen($urlData['host'], $port);
stream_set_timeout($fsock, 3); stream_set_timeout($fsock, 3);
$query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.0' . PHP_EOL; $query = 'GET ' . $urlData['path'] . $urlQuery . ' HTTP/1.0' . PHP_EOL;
$query .= 'Host: ' . $urlData['host'] . PHP_EOL; $query .= 'Host: ' . $urlData['host'] . PHP_EOL;
$query .= 'Content-Type: ' . $contentType . PHP_EOL; $query .= 'Content-Type: ' . $contentType . PHP_EOL;
$query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL; $query .= 'User-Agent: ManiaControl v' . ManiaControl::VERSION . PHP_EOL;
$query .= PHP_EOL; $query .= PHP_EOL;
fwrite($fsock, $query); fwrite($fsock, $query);
$buffer = ''; $buffer = '';
$info = array('timed_out' => false); $info = array('timed_out' => false);
while(!feof($fsock) && !$info['timed_out']) { while (!feof($fsock) && !$info['timed_out']) {
$buffer .= fread($fsock, 1024); $buffer .= fread($fsock, 1024);
$info = stream_get_meta_data($fsock); $info = stream_get_meta_data($fsock);
} }
fclose($fsock); fclose($fsock);
if ($info['timed_out'] || !$buffer) { if ($info['timed_out'] || !$buffer) {
return null; return null;
} }
if (substr($buffer, 9, 3) != "200") { if (substr($buffer, 9, 3) != "200") {
return null; return null;
} }
$result = explode("\r\n\r\n", $buffer, 2); $result = explode("\r\n\r\n", $buffer, 2);
if (count($result) < 2) { if (count($result) < 2) {
return null; return null;
} }
return $result[1]; return $result[1];
} }
/** /**
* Load config xml-file * Load config xml-file
* *
* @param string $fileName * @param string $fileName
* @return \SimpleXMLElement * @return \SimpleXMLElement
*/ */
@ -85,14 +89,66 @@ abstract class FileUtil {
/** /**
* Return file name cleared from special characters * Return file name cleared from special characters
* *
* @param string $fileName * @param string $fileName
* @return string * @return string
*/ */
public static function getClearedFileName($fileName) { public static function getClearedFileName($fileName) {
$fileName = Formatter::stripCodes($fileName); $fileName = Formatter::stripCodes($fileName);
$fileName = str_replace(array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName); $fileName = str_replace(array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), '_', $fileName);
$fileName = preg_replace( '/[^[:print:]]/', '', $fileName); $fileName = preg_replace('/[^[:print:]]/', '', $fileName);
return $fileName; return $fileName;
} }
/**
* Get the Temporary Folder and create it if necessary
*
* @param bool $createIfNecessary
* @return string
*/
public static function getTempFolder($createIfNecessary = true) {
$tempFolder = ManiaControlDir . self::FOLDER_NAME_TEMP;
if ($createIfNecessary && !is_dir($tempFolder)) {
mkdir($tempFolder);
}
return $tempFolder;
}
/**
* Delete the Temporary Folder if it's empty
*
* @return bool
*/
public static function removeTempFolder() {
$tempFolder = self::getTempFolder(false);
return @rmdir($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(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}'!";
$this->maniaControl->log($message);
return false;
}
}
}
return true;
}
} }