2013-11-09 11:19:21 +01:00
|
|
|
<?php
|
2013-11-10 12:51:41 +01:00
|
|
|
|
2013-12-09 15:43:08 +01:00
|
|
|
// Run configuration
|
2014-01-27 20:16:20 +01:00
|
|
|
|
2013-12-09 15:43:08 +01:00
|
|
|
define('LOG_WRITE_CURRENT_FILE', 'ManiaControl.log'); // Write current log to extra file in base dir
|
|
|
|
define('LOG_NAME_USE_DATE', true); // Use current date as suffix for log file name in logs folder
|
|
|
|
define('LOG_NAME_USE_PID', true); // Use current process id as suffix for log file name in logs folder
|
2014-01-27 20:16:20 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
// Define base dir
|
2013-11-09 11:19:21 +01:00
|
|
|
define('ManiaControlDir', __DIR__);
|
|
|
|
|
|
|
|
// Set process settings
|
2013-12-09 15:43:08 +01:00
|
|
|
ini_set('memory_limit', '64M');
|
2013-11-09 11:19:21 +01:00
|
|
|
if (function_exists('date_default_timezone_get') && function_exists('date_default_timezone_set')) {
|
|
|
|
date_default_timezone_set(@date_default_timezone_get());
|
|
|
|
}
|
|
|
|
|
2013-12-09 15:43:08 +01:00
|
|
|
// Build log file name
|
|
|
|
$logFileName = ManiaControlDir . '/logs/';
|
|
|
|
if (!is_dir($logFileName)) {
|
|
|
|
mkdir($logFileName);
|
2013-11-10 17:23:00 +01:00
|
|
|
}
|
2013-12-09 15:43:08 +01:00
|
|
|
$logFileName .= '/ManiaControl';
|
2014-01-27 20:16:20 +01:00
|
|
|
if (LOG_NAME_USE_DATE) {
|
|
|
|
$logFileName .= '_' . date('Y-m-d');
|
|
|
|
}
|
|
|
|
if (LOG_NAME_USE_PID) {
|
|
|
|
$logFileName .= '_' . getmypid();
|
|
|
|
}
|
2013-12-09 15:43:08 +01:00
|
|
|
$logFileName .= '.log';
|
|
|
|
define('LOG_FILE', $logFileName);
|
2013-11-09 11:19:21 +01:00
|
|
|
|
2013-12-09 15:43:08 +01:00
|
|
|
// Delete old current log file
|
|
|
|
if (LOG_WRITE_CURRENT_FILE) {
|
|
|
|
$currentLogFileName = ManiaControlDir . '/' . LOG_WRITE_CURRENT_FILE;
|
|
|
|
if (file_exists($currentLogFileName) && is_writable($currentLogFileName)) {
|
|
|
|
unlink($currentLogFileName);
|
|
|
|
}
|
|
|
|
define('LOG_CURRENT_FILE', $currentLogFileName);
|
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
|
2013-12-14 23:30:51 +01:00
|
|
|
/**
|
|
|
|
* Log and echo the given text
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
*/
|
2013-12-09 15:43:08 +01:00
|
|
|
function logMessage($message) {
|
|
|
|
$message .= PHP_EOL;
|
2014-01-27 20:16:20 +01:00
|
|
|
if (defined('LOG_CURRENT_FILE')) {
|
|
|
|
file_put_contents(LOG_CURRENT_FILE, $message, FILE_APPEND);
|
|
|
|
}
|
2013-12-09 15:43:08 +01:00
|
|
|
file_put_contents(LOG_FILE, $message, FILE_APPEND);
|
|
|
|
echo $message;
|
|
|
|
}
|
2013-11-09 11:19:21 +01:00
|
|
|
|
2013-12-14 23:30:51 +01:00
|
|
|
/**
|
|
|
|
* Get the prefix for the given error level
|
2014-01-27 20:16:20 +01:00
|
|
|
*
|
2013-12-14 23:30:51 +01:00
|
|
|
* @param int $errorLevel
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-12-09 15:43:08 +01:00
|
|
|
function getErrorTag($errorLevel) {
|
|
|
|
if ($errorLevel == E_NOTICE) {
|
|
|
|
return '[PHP NOTICE]';
|
|
|
|
}
|
|
|
|
if ($errorLevel == E_WARNING) {
|
|
|
|
return '[PHP WARNING]';
|
|
|
|
}
|
|
|
|
if ($errorLevel == E_ERROR) {
|
|
|
|
return '[PHP ERROR]';
|
|
|
|
}
|
|
|
|
if ($errorLevel == E_USER_NOTICE) {
|
|
|
|
return '[ManiaControl NOTICE]';
|
|
|
|
}
|
|
|
|
if ($errorLevel == E_USER_WARNING) {
|
|
|
|
return '[ManiaControl WARNING]';
|
|
|
|
}
|
|
|
|
if ($errorLevel == E_USER_ERROR) {
|
|
|
|
return '[ManiaControl ERROR]';
|
|
|
|
}
|
|
|
|
return "[PHP {$errorLevel}]";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register error handler
|
2014-01-27 20:16:20 +01:00
|
|
|
set_error_handler('errorHandler', -1);
|
|
|
|
set_exception_handler('exceptionHandler');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error Handler
|
|
|
|
*
|
|
|
|
* @param $errorNumber
|
|
|
|
* @param $errorString
|
|
|
|
* @param $errorFile
|
|
|
|
* @param $errorLine
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function errorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
|
|
|
|
if (error_reporting() == 0) {
|
|
|
|
// Error suppressed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Log error
|
|
|
|
$errorTag = getErrorTag($errorNumber);
|
|
|
|
$message = "{$errorTag}: {$errorString} in File '{$errorFile}' on Line {$errorLine}!";
|
|
|
|
logMessage($message);
|
|
|
|
if ($errorNumber == E_ERROR || $errorNumber == E_USER_ERROR) {
|
|
|
|
logMessage('Stopping execution...');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception Handler
|
|
|
|
*
|
|
|
|
* @param Exception $ex
|
|
|
|
*/
|
|
|
|
function exceptionHandler(Exception $ex) {
|
|
|
|
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()} Trace: {$ex->getTraceAsString()}!";
|
|
|
|
logMessage($message);
|
|
|
|
}
|
2013-12-09 15:43:08 +01:00
|
|
|
|
2014-01-27 09:07:25 +01:00
|
|
|
// Autoload Function that loads ManiaControl Class Files on Demand
|
2014-01-27 20:16:20 +01:00
|
|
|
spl_autoload_register(function ($className) {
|
|
|
|
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
|
|
|
|
$classPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
|
|
|
|
$filePath = ManiaControlDir . DIRECTORY_SEPARATOR . $classPath . '.php';
|
|
|
|
if (file_exists($filePath)) {
|
|
|
|
require_once $filePath;
|
|
|
|
}
|
|
|
|
});
|
2014-01-27 09:07:25 +01:00
|
|
|
|
2013-12-09 15:43:08 +01:00
|
|
|
// Start ManiaControl
|
2014-01-27 09:07:25 +01:00
|
|
|
$maniaControl = new \ManiaControl\ManiaControl();
|
2013-11-10 02:55:08 +01:00
|
|
|
$maniaControl->run();
|