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

@ -13,6 +13,10 @@ use ManiaControl\Formatter;
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class FileUtil {
/*
* Constants
*/
const FOLDER_NAME_TEMP = '/temp/';
/**
* Load a remote file
@ -95,4 +99,56 @@ abstract class FileUtil {
$fileName = preg_replace('/[^[:print:]]/', '', $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;
}
}