TrackManiaControl/application/core/ErrorHandler.php

157 lines
4.0 KiB
PHP
Raw Normal View History

2014-01-27 20:28:37 +01:00
<?php
2014-02-10 22:04:09 +01:00
namespace ManiaControl;
2014-02-08 19:33:26 +01:00
use ManiaControl\Files\FileUtil;
2014-02-10 22:54:49 +01:00
use ManiaControl\Update\UpdateManager;
2014-01-27 20:28:37 +01:00
/**
* Error and Exception Manager Class
*
* @author steeffeen & kremsy
*/
class ErrorHandler {
2014-01-27 21:56:49 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
2014-01-27 20:28:37 +01:00
/**
* Construct Error Handler
*/
2014-01-27 21:56:49 +01:00
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-27 20:28:37 +01:00
set_error_handler(array(&$this, 'errorHandler'), -1);
set_exception_handler(array(&$this, 'exceptionHandler'));
}
/**
* ManiaControl ExceptionHandler
2014-01-27 21:56:49 +01:00
* ManiaControl Shuts down after exception
2014-01-27 20:28:37 +01:00
*
2014-02-10 22:54:49 +01:00
* @param \Exception $ex
2014-01-27 20:28:37 +01:00
*/
2014-01-27 21:56:49 +01:00
public function exceptionHandler(\Exception $ex) {
2014-02-13 14:19:17 +01:00
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()}" . PHP_EOL;
2014-02-13 20:41:55 +01:00
$message .= "Class: " . get_class($ex) . PHP_EOL;
2014-02-13 14:19:17 +01:00
$message .= "Trace: {$ex->getTraceAsString()}" . PHP_EOL;
2014-01-27 20:28:37 +01:00
logMessage($message);
2014-02-08 19:33:26 +01:00
$error = array();
$error["Type"] = "Exception";
$error["Message"] = $message;
$error['ManiaControlVersion'] = ManiaControl::VERSION;
$error['OperatingSystem'] = php_uname();
$error['PHPVersion'] = phpversion();
2014-02-10 15:26:08 +01:00
if ($this->maniaControl->server != null) {
$error['ServerLogin'] = $this->maniaControl->server->login;
} else {
$error['ServerLogin'] = null;
}
2014-02-08 19:33:26 +01:00
$json = json_encode($error);
$info = base64_encode($json);
2014-02-09 14:01:04 +01:00
$url = UpdateManager::URL_WEBSERVICE . "errorreport?error=" . urlencode($info);
2014-02-08 19:33:26 +01:00
$success = FileUtil::loadFile($url);
if (!json_decode($success)) {
logMessage("Exception-Report failed");
2014-02-13 20:41:55 +01:00
} else {
logMessage("Exception successfully reported!");
2014-02-08 19:33:26 +01:00
}
2014-02-13 22:37:13 +01:00
$this->maniaControl->restart();
2014-01-27 21:56:49 +01:00
exit();
2014-01-27 20:28:37 +01:00
}
/**
* Error Handler
*
* @param $errorNumber
* @param $errorString
* @param $errorFile
* @param $errorLine
* @return bool
*/
public function errorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
if (error_reporting() == 0) {
// Error suppressed
return false;
}
// Log error
$errorTag = $this->getErrorTag($errorNumber);
$message = "{$errorTag}: {$errorString} in File '{$errorFile}' on Line {$errorLine}!";
logMessage($message);
2014-02-08 19:33:26 +01:00
2014-02-10 23:03:25 +01:00
if ($errorNumber != E_USER_ERROR && $errorNumber != E_USER_WARNING && $errorNumber != E_USER_NOTICE) {
2014-02-08 19:33:26 +01:00
$error = array();
$error["Type"] = "Error";
$error["Message"] = $message;
$error['ManiaControlVersion'] = ManiaControl::VERSION;
$error['OperatingSystem'] = php_uname();
$error['PHPVersion'] = phpversion();
2014-02-10 15:26:08 +01:00
if ($this->maniaControl->server != null) {
$error['ServerLogin'] = $this->maniaControl->server->login;
} else {
$error['ServerLogin'] = null;
}
2014-02-08 19:33:26 +01:00
$json = json_encode($error);
$info = base64_encode($json);
2014-02-09 14:01:04 +01:00
$url = UpdateManager::URL_WEBSERVICE . "errorreport?error=" . urlencode($info);
2014-02-08 19:33:26 +01:00
$success = FileUtil::loadFile($url);
if (!json_decode($success)) {
2014-02-13 20:41:55 +01:00
logMessage("Error-Report failed!");
} else {
logMessage("Error successfully reported!");
2014-02-08 19:33:26 +01:00
}
2014-02-10 23:03:25 +01:00
}
2014-02-08 19:33:26 +01:00
2014-02-13 20:41:55 +01:00
if ($errorNumber == E_ERROR || $errorNumber == E_USER_ERROR || $errorNumber == E_FATAL) {
2014-01-27 20:28:37 +01:00
logMessage('Stopping execution...');
exit();
}
return false;
}
/**
* Get the prefix for the given error level
*
* @param int $errorLevel
* @return string
*/
public function getErrorTag($errorLevel) {
if ($errorLevel == E_NOTICE) {
return '[PHP NOTICE]';
}
if ($errorLevel == E_WARNING) {
return '[PHP WARNING]';
}
if ($errorLevel == E_ERROR) {
return '[PHP ERROR]';
}
2014-02-13 20:41:55 +01:00
if ($errorLevel == E_CORE_ERROR) {
return '[PHP CORE ERROR]';
}
if ($errorLevel == E_COMPILE_ERROR) {
return '[PHP COMPILE ERROR]';
}
if ($errorLevel == E_RECOVERABLE_ERROR) {
return '[PHP RECOVERABLE ERROR]';
}
2014-01-27 20:28:37 +01:00
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}]";
}
}