improved logs, backup & temp folder creation
This commit is contained in:
parent
488b6fc4a4
commit
6fd042b57b
@ -9,7 +9,7 @@ namespace ManiaControl;
|
|||||||
* @copyright 2014 ManiaControl Team
|
* @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
|
||||||
*/
|
*/
|
||||||
class AutoLoader {
|
abstract class AutoLoader {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the Auto Loader
|
* Register the Auto Loader
|
||||||
@ -17,6 +17,7 @@ class AutoLoader {
|
|||||||
public static function register() {
|
public static function register() {
|
||||||
spl_autoload_register(array(get_class(), 'autoload'));
|
spl_autoload_register(array(get_class(), 'autoload'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to autoload the Class with the given Name
|
* Try to autoload the Class with the given Name
|
||||||
*
|
*
|
||||||
|
@ -12,10 +12,6 @@ use ManiaControl\ManiaControl;
|
|||||||
* @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 BackupUtil {
|
abstract class BackupUtil {
|
||||||
/*
|
|
||||||
* Constants
|
|
||||||
*/
|
|
||||||
const FOLDER_NAME_BACKUP = 'backup';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform a Full Backup of ManiaControl
|
* Perform a Full Backup of ManiaControl
|
||||||
@ -30,7 +26,7 @@ abstract class BackupUtil {
|
|||||||
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d_H-i') . '_' . time() . '.zip';
|
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d_H-i') . '_' . time() . '.zip';
|
||||||
$backupZip = new \ZipArchive();
|
$backupZip = new \ZipArchive();
|
||||||
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
||||||
trigger_error("Couldn't create Backup Zip!");
|
trigger_error("Couldn't create backup zip!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$excludes = array();
|
$excludes = array();
|
||||||
@ -46,16 +42,16 @@ abstract class BackupUtil {
|
|||||||
/**
|
/**
|
||||||
* Get the Backup Folder Path and create it if necessary
|
* Get the Backup Folder Path and create it if necessary
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string|bool
|
||||||
*/
|
*/
|
||||||
private static function getBackupFolder() {
|
private static function getBackupFolder() {
|
||||||
$backupFolder = ManiaControlDir . self::FOLDER_NAME_BACKUP . DIRECTORY_SEPARATOR;
|
$backupFolder = ManiaControlDir . 'backup' . DIRECTORY_SEPARATOR;
|
||||||
if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
|
if (!is_dir($backupFolder) && !mkdir($backupFolder)) {
|
||||||
trigger_error("Couldn't create Backup Folder!");
|
trigger_error("Couldn't create backup folder!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!is_writeable($backupFolder)) {
|
if (!is_writeable($backupFolder)) {
|
||||||
trigger_error("ManiaControl doesn't have the necessary Writing Rights for the Backup Folder!");
|
trigger_error("ManiaControl doesn't have the necessary write rights for the backup folder!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $backupFolder;
|
return $backupFolder;
|
||||||
@ -71,10 +67,11 @@ abstract class BackupUtil {
|
|||||||
* @param array $baseFileNames
|
* @param array $baseFileNames
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array(), array $baseFileNames = array()) {
|
private static function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array(),
|
||||||
|
array $baseFileNames = array()) {
|
||||||
$folderHandle = opendir($folderName);
|
$folderHandle = opendir($folderName);
|
||||||
if (!is_resource($folderHandle)) {
|
if (!is_resource($folderHandle)) {
|
||||||
trigger_error("Couldn't open Folder '{$folderName}' for Backup!");
|
trigger_error("Couldn't open folder '{$folderName}' for backup!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$useBaseFileNames = !empty($baseFileNames);
|
$useBaseFileNames = !empty($baseFileNames);
|
||||||
@ -120,7 +117,7 @@ abstract class BackupUtil {
|
|||||||
$backupFileName = $backupFolder . 'backup_plugins_' . ManiaControl::VERSION . date('y-m-d_H-i') . '_' . time() . '.zip';
|
$backupFileName = $backupFolder . 'backup_plugins_' . ManiaControl::VERSION . date('y-m-d_H-i') . '_' . time() . '.zip';
|
||||||
$backupZip = new \ZipArchive();
|
$backupZip = new \ZipArchive();
|
||||||
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== true) {
|
||||||
trigger_error("Couldn't create Backup Zip!");
|
trigger_error("Couldn't create backup zip!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$directory = ManiaControlDir . 'plugins';
|
$directory = ManiaControlDir . 'plugins';
|
||||||
|
@ -70,7 +70,7 @@ abstract class FileUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load config xml-file
|
* Load Config XML File
|
||||||
*
|
*
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
* @return \SimpleXMLElement
|
* @return \SimpleXMLElement
|
||||||
@ -78,11 +78,11 @@ abstract class FileUtil {
|
|||||||
public static function loadConfig($fileName) {
|
public static function loadConfig($fileName) {
|
||||||
$fileLocation = ManiaControlDir . 'configs' . DIRECTORY_SEPARATOR . $fileName;
|
$fileLocation = ManiaControlDir . 'configs' . DIRECTORY_SEPARATOR . $fileName;
|
||||||
if (!file_exists($fileLocation)) {
|
if (!file_exists($fileLocation)) {
|
||||||
Logger::log("Config File doesn't exist! ({$fileName})");
|
Logger::log("Config file doesn't exist! ({$fileName})");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!is_readable($fileLocation)) {
|
if (!is_readable($fileLocation)) {
|
||||||
Logger::log("Config File isn't readable! Please check the File Permissions. ({$fileName})");
|
Logger::log("Config file isn't readable! Please check the file permissions. ({$fileName})");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$configXml = @simplexml_load_file($fileLocation);
|
$configXml = @simplexml_load_file($fileLocation);
|
||||||
@ -112,20 +112,24 @@ abstract class FileUtil {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function removeTempFolder() {
|
public static function removeTempFolder() {
|
||||||
$tempFolder = self::getTempFolder(false);
|
$tempFolder = self::getTempFolder();
|
||||||
return @rmdir($tempFolder);
|
return @rmdir($tempFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Temporary Folder and create it if necessary
|
* Get the Temporary Folder and create it if necessary
|
||||||
*
|
*
|
||||||
* @param bool $createIfNecessary
|
* @return string|bool
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public static function getTempFolder($createIfNecessary = true) {
|
public static function getTempFolder() {
|
||||||
$tempFolder = ManiaControlDir . 'temp' . DIRECTORY_SEPARATOR;
|
$tempFolder = ManiaControlDir . 'temp' . DIRECTORY_SEPARATOR;
|
||||||
if ($createIfNecessary && !is_dir($tempFolder)) {
|
if (!is_dir($tempFolder) && !mkdir($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;
|
return $tempFolder;
|
||||||
}
|
}
|
||||||
@ -140,7 +144,6 @@ abstract class FileUtil {
|
|||||||
if (!is_array($directories)) {
|
if (!is_array($directories)) {
|
||||||
$directories = array($directories);
|
$directories = array($directories);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($directories as $directory) {
|
foreach ($directories as $directory) {
|
||||||
$dir = new \RecursiveDirectoryIterator(ManiaControlDir . $directory);
|
$dir = new \RecursiveDirectoryIterator(ManiaControlDir . $directory);
|
||||||
foreach (new \RecursiveIteratorIterator($dir) as $fileName => $file) {
|
foreach (new \RecursiveIteratorIterator($dir) as $fileName => $file) {
|
||||||
@ -148,8 +151,7 @@ abstract class FileUtil {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!is_writable($fileName)) {
|
if (!is_writable($fileName)) {
|
||||||
$message = "Write-Access missing for File '{$fileName}'!";
|
Logger::log("Write access missing for file '{$fileName}'!");
|
||||||
Logger::log($message);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,7 +167,7 @@ abstract class FileUtil {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function deleteOldLogFiles($maxFileAgeInDays = 10.) {
|
public static function deleteOldLogFiles($maxFileAgeInDays = 10.) {
|
||||||
$logsFolderPath = Logger::getLogsFolderPath();
|
$logsFolderPath = Logger::getLogsFolder();
|
||||||
if (!is_readable($logsFolderPath)) {
|
if (!is_readable($logsFolderPath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -37,39 +37,21 @@ class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the Logs Folder and return its Path if successful
|
* Get the Logs Folder and create it if necessary
|
||||||
*
|
*
|
||||||
* @return bool|string
|
* @return string|bool
|
||||||
*/
|
*/
|
||||||
private static function createLogsFolder() {
|
public static function getLogsFolder() {
|
||||||
$logsFolderPath = self::getLogsFolderPath();
|
$logsFolder = ManiaControlDir . 'logs' . DIRECTORY_SEPARATOR;
|
||||||
if (!is_dir($logsFolderPath) && !mkdir($logsFolderPath)) {
|
if (!is_dir($logsFolder) && !mkdir($logsFolder)) {
|
||||||
self::output("Couldn't create Logs Folder, please check the File Permissions!");
|
trigger_error("Couldn't create the logs folder!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $logsFolderPath;
|
if (!is_writeable($logsFolder)) {
|
||||||
}
|
trigger_error("ManiaControl doesn't have the necessary write rights for the logs folder!");
|
||||||
|
return false;
|
||||||
/**
|
|
||||||
* Build the Logs Folder Path
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function getLogsFolderPath() {
|
|
||||||
return ManiaControlDir . 'logs' . DIRECTORY_SEPARATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Echo the given Message
|
|
||||||
*
|
|
||||||
* @param string $message
|
|
||||||
* @param bool $eol
|
|
||||||
*/
|
|
||||||
public static function output($message, $eol = true) {
|
|
||||||
if ($eol) {
|
|
||||||
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
|
|
||||||
}
|
}
|
||||||
echo $message;
|
return $logsFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,6 +80,19 @@ class Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Echo the given Message
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
* @param bool $eol
|
||||||
|
*/
|
||||||
|
public static function output($message, $eol = true) {
|
||||||
|
if ($eol) {
|
||||||
|
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
|
||||||
|
}
|
||||||
|
echo $message;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log and echo the given Info Message
|
* Log and echo the given Info Message
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user