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
|
2014-04-12 12:14:37 +02:00
|
|
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-01-27 20:28:37 +01:00
|
|
|
*/
|
|
|
|
class ErrorHandler {
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-02-17 12:54:31 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const MC_DEBUG_NOTICE = "ManiaControl.DebugNotice";
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-01-27 21:56:49 +01:00
|
|
|
* Private Properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
2014-02-19 15:44:00 +01:00
|
|
|
private $reportErrors = true;
|
2014-01-27 21:56:49 +01:00
|
|
|
|
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-03-19 14:23:43 +01:00
|
|
|
// Log exception
|
2014-03-19 14:00:24 +01:00
|
|
|
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()}";
|
|
|
|
$traceMessage = 'Class: ' . get_class($ex) . PHP_EOL;
|
|
|
|
$traceMessage .= 'Trace:' . PHP_EOL . $ex->getTraceAsString();
|
|
|
|
logMessage($message . PHP_EOL . $traceMessage);
|
2014-02-19 15:44:00 +01:00
|
|
|
|
|
|
|
if ($this->reportErrors) {
|
|
|
|
$error = array();
|
|
|
|
$error["Type"] = "Exception";
|
|
|
|
$error["Message"] = $message;
|
2014-03-19 14:00:24 +01:00
|
|
|
$error["Backtrace"] = $traceMessage;
|
2014-02-19 15:44:00 +01:00
|
|
|
$error['OperatingSystem'] = php_uname();
|
|
|
|
$error['PHPVersion'] = phpversion();
|
|
|
|
|
|
|
|
if ($this->maniaControl->server) {
|
|
|
|
$error['ServerLogin'] = $this->maniaControl->server->login;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error['ServerLogin'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->maniaControl->settingManager && $this->maniaControl->updateManager) {
|
|
|
|
$error['UpdateChannel'] = $this->maniaControl->settingManager->getSetting($this->maniaControl->updateManager,
|
|
|
|
UpdateManager::SETTING_UPDATECHECK_CHANNEL);
|
|
|
|
$error['ManiaControlVersion'] = $this->maniaControl->updateManager->getCurrentBuildDate();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error['UpdateChannel'] = '';
|
|
|
|
$error['ManiaControlVersion'] = ManiaControl::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_encode($error);
|
|
|
|
$info = base64_encode($json);
|
|
|
|
|
|
|
|
$url = ManiaControl::URL_WEBSERVICE . "errorreport?error=" . urlencode($info);
|
|
|
|
$success = FileUtil::loadFile($url);
|
|
|
|
|
|
|
|
if (!json_decode($success)) {
|
|
|
|
logMessage("Exception-Report failed!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logMessage("Exception successfully reported!");
|
|
|
|
}
|
2014-02-08 19:33:26 +01:00
|
|
|
}
|
2014-02-19 15:44:00 +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
|
|
|
|
*/
|
2014-03-19 14:23:43 +01:00
|
|
|
public function errorHandler($errorNumber, $errorString, $errorFile = null, $errorLine = -1) {
|
2014-01-27 20:28:37 +01:00
|
|
|
if (error_reporting() == 0) {
|
|
|
|
// Error suppressed
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-03-20 16:19:30 +01:00
|
|
|
$userError = ($errorNumber == E_USER_ERROR || $errorNumber == E_USER_WARNING || $errorNumber == E_USER_NOTICE ||
|
|
|
|
$errorNumber == E_USER_DEPRECATED);
|
|
|
|
|
2014-01-27 20:28:37 +01:00
|
|
|
// Log error
|
|
|
|
$errorTag = $this->getErrorTag($errorNumber);
|
2014-03-19 14:00:24 +01:00
|
|
|
$message = $errorTag . ': ' . $errorString;
|
|
|
|
$traceMessage = $this->parseBackTrace(debug_backtrace());
|
2014-03-20 16:19:30 +01:00
|
|
|
logMessage($message . ($userError ? '' : PHP_EOL . $traceMessage));
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-03-20 16:19:30 +01:00
|
|
|
if ($this->reportErrors && !$userError) {
|
2014-02-19 15:44:00 +01:00
|
|
|
$error = array();
|
|
|
|
$error["Type"] = "Error";
|
|
|
|
$error["Message"] = $message;
|
2014-03-19 14:00:24 +01:00
|
|
|
$error["Backtrace"] = $traceMessage;
|
2014-02-15 18:26:31 +01:00
|
|
|
$error['OperatingSystem'] = php_uname();
|
2014-02-19 15:44:00 +01:00
|
|
|
$error['PHPVersion'] = phpversion();
|
|
|
|
|
2014-02-15 19:19:03 +01:00
|
|
|
if ($this->maniaControl->server) {
|
2014-02-10 15:26:08 +01:00
|
|
|
$error['ServerLogin'] = $this->maniaControl->server->login;
|
2014-02-19 15:44:00 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-02-15 19:19:03 +01:00
|
|
|
$error['ServerLogin'] = '';
|
2014-02-10 15:26:08 +01:00
|
|
|
}
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-02-15 19:19:03 +01:00
|
|
|
if ($this->maniaControl->settingManager && $this->maniaControl->updateManager) {
|
2014-02-19 15:44:00 +01:00
|
|
|
$error['UpdateChannel'] = $this->maniaControl->settingManager->getSetting($this->maniaControl->updateManager,
|
|
|
|
UpdateManager::SETTING_UPDATECHECK_CHANNEL);
|
2014-02-15 18:26:31 +01:00
|
|
|
$error['ManiaControlVersion'] = $this->maniaControl->updateManager->getCurrentBuildDate();
|
2014-02-19 15:44:00 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error['UpdateChannel'] = '';
|
2014-02-15 18:26:31 +01:00
|
|
|
$error['ManiaControlVersion'] = ManiaControl::VERSION;
|
|
|
|
}
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-02-08 19:33:26 +01:00
|
|
|
$json = json_encode($error);
|
|
|
|
$info = base64_encode($json);
|
2014-02-19 15:44:00 +01:00
|
|
|
|
|
|
|
$url = ManiaControl::URL_WEBSERVICE . "errorreport?error=" . urlencode($info);
|
2014-02-08 19:33:26 +01:00
|
|
|
$success = FileUtil::loadFile($url);
|
2014-02-19 15:44:00 +01:00
|
|
|
|
2014-02-08 19:33:26 +01:00
|
|
|
if (!json_decode($success)) {
|
2014-02-13 20:41:55 +01:00
|
|
|
logMessage("Error-Report failed!");
|
2014-02-19 15:44:00 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-02-13 20:41:55 +01:00
|
|
|
logMessage("Error successfully reported!");
|
2014-02-08 19:33:26 +01:00
|
|
|
}
|
2014-02-10 23:03:25 +01:00
|
|
|
}
|
2014-02-19 15:44:00 +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;
|
|
|
|
}
|
|
|
|
|
2014-02-17 12:54:31 +01:00
|
|
|
/**
|
|
|
|
* Triggers a Debug Notice to the ManiaControl Website
|
|
|
|
*
|
|
|
|
* @param $message
|
|
|
|
*/
|
|
|
|
public function triggerDebugNotice($message) {
|
2014-03-19 14:23:43 +01:00
|
|
|
$this->errorHandler(self::MC_DEBUG_NOTICE, $message);
|
2014-02-17 12:54:31 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 20:28:37 +01:00
|
|
|
/**
|
|
|
|
* 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]';
|
|
|
|
}
|
2014-02-17 12:54:31 +01:00
|
|
|
if ($errorLevel == self::MC_DEBUG_NOTICE) {
|
|
|
|
return '[ManiaControl DEBUG]';
|
|
|
|
}
|
2014-01-27 20:28:37 +01:00
|
|
|
return "[PHP {$errorLevel}]";
|
|
|
|
}
|
2014-03-19 14:00:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the Debug Backtrace into a String for the Error Report
|
|
|
|
*
|
|
|
|
* return string
|
|
|
|
*/
|
|
|
|
private function parseBackTrace(array $backtrace) {
|
|
|
|
$traceString = 'Trace:';
|
|
|
|
$stepCount = 0;
|
|
|
|
foreach ($backtrace as $traceStep) {
|
|
|
|
$traceString .= PHP_EOL . '#' . $stepCount . ': ';
|
|
|
|
if (isset($traceStep['class'])) {
|
|
|
|
$traceString .= $traceStep['class'];
|
|
|
|
}
|
|
|
|
if (isset($traceStep['type'])) {
|
|
|
|
$traceString .= $traceStep['type'];
|
|
|
|
}
|
|
|
|
if (isset($traceStep['function'])) {
|
|
|
|
$traceString .= $traceStep['function'];
|
|
|
|
}
|
|
|
|
if (isset($traceStep['file'])) {
|
|
|
|
$traceString .= ' in File ';
|
|
|
|
$traceString .= $traceStep['file'];
|
|
|
|
}
|
|
|
|
if (isset($traceStep['line'])) {
|
|
|
|
$traceString .= ' on Line ';
|
|
|
|
$traceString .= $traceStep['line'];
|
|
|
|
}
|
|
|
|
$stepCount++;
|
|
|
|
}
|
|
|
|
return $traceString;
|
|
|
|
}
|
2014-01-27 20:28:37 +01:00
|
|
|
}
|