[784] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | class ErrorHandler
|
---|
| 4 | {
|
---|
[887] | 5 | public string $Encoding;
|
---|
| 6 | public bool $ShowError;
|
---|
| 7 | public int $UserErrors;
|
---|
| 8 | public $OnError;
|
---|
[784] | 9 |
|
---|
| 10 | function __construct()
|
---|
| 11 | {
|
---|
| 12 | $this->Encoding = 'utf-8';
|
---|
| 13 | $this->ShowError = false;
|
---|
| 14 | $this->UserErrors = E_ALL; //E_ERROR | E_WARNING | E_PARSE;
|
---|
| 15 | $this->OnError = array(array($this, 'ShowDefaultError'));
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[915] | 18 | function Start(): void
|
---|
[784] | 19 | {
|
---|
| 20 | set_error_handler(array($this, 'ErrorHandler'));
|
---|
| 21 | set_exception_handler(array($this, 'ExceptionHandler'));
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[915] | 24 | function Stop(): void
|
---|
[784] | 25 | {
|
---|
| 26 | restore_error_handler();
|
---|
| 27 | restore_exception_handler();
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[915] | 30 | function ErrorHandler(int $Number, string $Message, string $FileName, int $LineNumber, array $Variables = array()): bool
|
---|
[784] | 31 | {
|
---|
| 32 | $ErrorType = array
|
---|
| 33 | (
|
---|
| 34 | 1 => 'Error',
|
---|
| 35 | 2 => 'Warning',
|
---|
| 36 | 4 => 'Parsing Error',
|
---|
| 37 | 8 => 'Notice',
|
---|
| 38 | 16 => 'Core Error',
|
---|
| 39 | 32 => 'Core Warning',
|
---|
| 40 | 64 => 'Compile Error',
|
---|
| 41 | 128 => 'Compile Warning',
|
---|
| 42 | 256 => 'User Error',
|
---|
| 43 | 512 => 'User Warning',
|
---|
| 44 | 1024 => 'User Notice'
|
---|
| 45 | );
|
---|
| 46 |
|
---|
[873] | 47 | if (($this->UserErrors & $Number))
|
---|
[784] | 48 | {
|
---|
| 49 | // Error was suppressed with the @-operator
|
---|
[873] | 50 | if (0 === error_reporting())
|
---|
[784] | 51 | {
|
---|
| 52 | return false;
|
---|
| 53 | }
|
---|
| 54 | $Backtrace = debug_backtrace();
|
---|
| 55 | $Backtrace[0]['function'] = $Message;
|
---|
| 56 | $Backtrace[0]['args'] = '';
|
---|
| 57 | $Backtrace[0]['file'] = $FileName;
|
---|
| 58 | $Backtrace[0]['line'] = $LineNumber;
|
---|
| 59 | $this->Report($Backtrace);
|
---|
[873] | 60 | //if ((E_ERROR | E_PARSE) & $Number)
|
---|
[784] | 61 | die();
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[915] | 65 | function ExceptionHandler(Throwable $Exception): void
|
---|
[784] | 66 | {
|
---|
| 67 | $Backtrace = $Exception->getTrace();
|
---|
| 68 | array_unshift($Backtrace, array(
|
---|
| 69 | 'function' => $Exception->getMessage(),
|
---|
| 70 | 'file' => $Exception->getFile(),
|
---|
| 71 | 'line' => $Exception->getLine(),
|
---|
| 72 | ));
|
---|
| 73 | $this->Report($Backtrace);
|
---|
| 74 | die();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[887] | 77 | function ShowDefaultError(string $Message): void
|
---|
[784] | 78 | {
|
---|
| 79 | $Output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>'."\n".
|
---|
| 80 | '<meta http-equiv="Content-Language" content="en">'."\n".
|
---|
| 81 | '<meta http-equiv="Content-Type" content="text/html; charset='.$this->Encoding.'"></head><body>'."\n".
|
---|
| 82 | 'An internal error occurred!<br/>'.
|
---|
| 83 | 'Administrator will be notified and the problem will be investigated and fixed soon.'.'<br/><br/>';
|
---|
[873] | 84 | if ($this->ShowError == true)
|
---|
[784] | 85 | $Output .= '<pre>'.$Message.'</pre><br/>';
|
---|
| 86 | $Output .= '</body></html>';
|
---|
| 87 | echo($Output);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[915] | 90 | function Report(array $Backtrace): void
|
---|
[784] | 91 | {
|
---|
| 92 | $Date = date('Y-m-d H:i:s');
|
---|
| 93 | $Error = '# '.$Date."\n";
|
---|
[873] | 94 | foreach ($Backtrace as $Item)
|
---|
[784] | 95 | {
|
---|
[873] | 96 | if (!array_key_exists('line', $Item)) $Item['line'] = '';
|
---|
| 97 | if (!array_key_exists('file', $Item)) $Item['file'] = '';
|
---|
[784] | 98 |
|
---|
| 99 | $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
|
---|
| 100 | $Arguments = '';
|
---|
[873] | 101 | if (array_key_exists('args', $Item) and is_array($Item['args']))
|
---|
| 102 | foreach ($Item['args'] as $Item)
|
---|
[784] | 103 | {
|
---|
[873] | 104 | if (is_object($Item)) ;
|
---|
| 105 | else if (is_array($Item)) $Arguments .= "'".serialize($Item)."',";
|
---|
[784] | 106 | else $Arguments .= "'".$Item."',";
|
---|
| 107 | }
|
---|
[873] | 108 | if (strlen($Arguments) > 0) $Error .= '('.substr($Arguments, 0, -1).')';
|
---|
[784] | 109 | $Error .= "\n";
|
---|
| 110 | }
|
---|
| 111 | $Error .= "\n";
|
---|
| 112 |
|
---|
[873] | 113 | foreach ($this->OnError as $OnError)
|
---|
[784] | 114 | call_user_func($OnError, $Error);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|