From a0a800c0fb6ff385ce8324fd7f25c39260e7e7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Sat, 3 May 2014 21:47:53 +0200 Subject: [PATCH] methods to clean up files --- application/core/Files/FileUtil.php | 47 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/application/core/Files/FileUtil.php b/application/core/Files/FileUtil.php index 9e898841..cb7a594b 100644 --- a/application/core/Files/FileUtil.php +++ b/application/core/Files/FileUtil.php @@ -16,7 +16,7 @@ abstract class FileUtil { /* * Constants */ - const FOLDER_NAME_TEMP = '/temp/'; + const FOLDER_NAME_TEMP = 'temp/'; /** * Load a remote file @@ -75,7 +75,7 @@ abstract class FileUtil { * @return \SimpleXMLElement */ public static function loadConfig($fileName) { - $fileLocation = ManiaControlDir . '/configs/' . $fileName; + $fileLocation = ManiaControlDir . 'configs' . DIRECTORY_SEPARATOR . $fileName; if (!file_exists($fileLocation)) { trigger_error("Config file doesn't exist! ({$fileName})"); return null; @@ -151,4 +151,47 @@ abstract class FileUtil { return true; } + + /** + * Try to delete the given Plugin Files + * + * @param mixed $pluginFileNames + */ + public static function cleanPluginFiles($pluginFileNames) { + $pluginFileNames = self::buildFileNamesArray($pluginFileNames); + $fileNames = array(); + foreach ($pluginFileNames as $pluginFileName) { + $fileName = 'plugins' . DIRECTORY_SEPARATOR . $pluginFileName; + array_push($fileNames, $fileName); + } + self::cleanFiles($fileNames); + } + + /** + * Make sure we're dealing with an Array of File Names + * + * @param mixed $fileNamesParam + * @return array + */ + private static function buildFileNamesArray($fileNamesParam) { + if (!is_array($fileNamesParam)) { + $fileNamesParam = array($fileNamesParam); + } + return $fileNamesParam; + } + + /** + * Try to delete the given Files + * + * @param mixed $fileNames + */ + public static function cleanFiles($fileNames) { + $fileNames = self::buildFileNamesArray($fileNames); + foreach ($fileNames as $fileName) { + $filePath = ManiaControlDir . $fileName; + if (file_exists($filePath) && is_writeable($filePath)) { + unlink($filePath); + } + } + } }