/
class.errorhandler.php
438 lines (389 loc) · 14.4 KB
/
class.errorhandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* @package core
*/
/**
* GenericExceptionHandler will handle any uncaught exceptions thrown in
* Symphony. Additionally, all errors in Symphony that are raised to Exceptions
* will be handled by this class.
* It is possible for Exceptions to be caught by their own `ExceptionHandler` which can
* override the `render` function so that it can be displayed to the user appropriately.
*/
class GenericExceptionHandler
{
/**
* Whether the `GenericExceptionHandler` should handle exceptions. Defaults to true
* @var boolean
*/
public static $enabled = true;
/**
* An instance of the Symphony Log class, used to write errors to the log
* @var Log
*/
private static $_Log = null;
/**
* Initialise will set the error handler to be the `__CLASS__::handler` function.
*
* @param Log $Log
* An instance of a Symphony Log object to write errors to
*/
public static function initialise(Log $Log = null)
{
if (!is_null($Log)) {
self::$_Log = $Log;
}
set_exception_handler(array(__CLASS__, 'handler'));
register_shutdown_function(array(__CLASS__, 'shutdown'));
}
/**
* Retrieves a window of lines before and after the line where the error
* occurred so that a developer can help debug the exception
*
* @param integer $line
* The line where the error occurred.
* @param string $file
* The file that holds the logic that caused the error.
* @param integer $window
* The number of lines either side of the line where the error occurred
* to show
* @return array
*/
protected static function __nearbyLines($line, $file, $window = 5)
{
if (!file_exists($file)) {
return array();
}
return array_slice(file($file), ($line - 1) - $window, $window * 2, true);
}
/**
* The handler function is given an Exception and will call it's render
* function to display the Exception to a user. After calling the render
* function, the output is displayed and then exited to prevent any further
* logic from occurring.
*
* @param Exception $e
* The Exception object
* @return string
* The result of the Exception's render function
*/
public static function handler(Exception $e)
{
$output = '';
try {
// Instead of just throwing an empty page, return a 404 page.
if (self::$enabled !== true) {
$e = new FrontendPageNotFoundException();
};
$exception_type = get_class($e);
if (class_exists("{$exception_type}Handler") && method_exists("{$exception_type}Handler", 'render')) {
$class = "{$exception_type}Handler";
} else {
$class = __CLASS__;
}
// Exceptions should be logged if they are not caught.
if (self::$_Log instanceof Log) {
self::$_Log->pushExceptionToLog($e, true);
}
$output = call_user_func(array($class, 'render'), $e);
// If an exception was raised trying to render the exception, fall back
// to the generic exception handler
} catch (Exception $e) {
try {
$output = call_user_func(array('GenericExceptionHandler', 'render'), $e);
// If the generic exception handler couldn't do it, well we're in bad
// shape, just output a plaintext response!
} catch (Exception $e) {
echo "<pre>";
echo 'A severe error occurred whilst trying to handle an exception, check the Symphony log for more details' . PHP_EOL;
echo $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile() . PHP_EOL;
exit;
}
}
// Pending nothing disasterous, we should have `$e` and `$output` values here.
if (!headers_sent()) {
cleanup_session_cookies();
// Inspect the exception to determine the best status code
$httpStatus = null;
if ($e instanceof SymphonyErrorPage) {
$httpStatus = $e->getHttpStatusCode();
} elseif ($e instanceof FrontendPageNotFoundException) {
$httpStatus = Page::HTTP_STATUS_NOT_FOUND;
}
if (!$httpStatus || $httpStatus == Page::HTTP_STATUS_OK) {
$httpStatus = Page::HTTP_STATUS_ERROR;
}
Page::renderStatusCode($httpStatus);
header('Content-Type: text/html; charset=utf-8');
}
echo $output;
exit;
}
/**
* Returns the path to the error-template by looking at the `WORKSPACE/template/`
* directory, then at the `TEMPLATES` directory for the convention `*.tpl`. If
* the template is not found, `false` is returned
*
* @since Symphony 2.3
* @param string $name
* Name of the template
* @return mixed
* String, which is the path to the template if the template is found,
* false otherwise
*/
public static function getTemplate($name)
{
$format = '%s/%s.tpl';
if (file_exists($template = sprintf($format, WORKSPACE . '/template', $name))) {
return $template;
} elseif (file_exists($template = sprintf($format, TEMPLATE, $name))) {
return $template;
} else {
return false;
}
}
/**
* The render function will take an Exception and output a HTML page
*
* @param Exception $e
* The Exception object
* @return string
* An HTML string
*/
public static function render(Exception $e)
{
$lines = null;
foreach (self::__nearByLines($e->getLine(), $e->getFile()) as $line => $string) {
$lines .= sprintf(
'<li%s><strong>%d</strong> <code>%s</code></li>',
(($line+1) == $e->getLine() ? ' class="error"' : null),
++$line,
str_replace("\t", ' ', htmlspecialchars($string))
);
}
$trace = null;
foreach ($e->getTrace() as $t) {
$trace .= sprintf(
'<li><code><em>[%s:%d]</em></code></li><li><code>    %s%s%s();</code></li>',
(isset($t['file']) ? $t['file'] : null),
(isset($t['line']) ? $t['line'] : null),
(isset($t['class']) ? $t['class'] : null),
(isset($t['type']) ? $t['type'] : null),
$t['function']
);
}
$queries = null;
if (is_object(Symphony::Database())) {
$debug = Symphony::Database()->debug();
if (!empty($debug)) {
foreach ($debug as $query) {
$queries .= sprintf(
'<li><em>[%01.4f]</em><code> %s;</code> </li>',
(isset($query['execution_time']) ? $query['execution_time'] : null),
htmlspecialchars($query['query'])
);
}
}
}
return self::renderHtml(
'fatalerror.generic',
($e instanceof ErrorException ? GenericErrorHandler::$errorTypeStrings[$e->getSeverity()] : 'Fatal Error'),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$lines,
$trace,
$queries
);
}
/**
* The shutdown function will capture any fatal errors and format them as a
* usual Symphony page.
*
* @since Symphony 2.4
*/
public static function shutdown()
{
$last_error = error_get_last();
if (!is_null($last_error) && $last_error['type'] === E_ERROR) {
$code = $last_error['type'];
$message = $last_error['message'];
$file = $last_error['file'];
$line = $last_error['line'];
try {
// Log the error message
if (self::$_Log instanceof Log) {
self::$_Log->pushToLog(sprintf(
'%s %s: %s%s%s',
__CLASS__,
$code,
$message,
($line ? " on line $line" : null),
($file ? " of file $file" : null)
), $code, true);
}
ob_clean();
// Display the error message
echo self::renderHtml(
'fatalerror.fatal',
'Fatal Error',
$message,
$file,
$line
);
} catch (Exception $e) {
echo "<pre>";
echo 'A severe error occurred whilst trying to handle an exception, check the Symphony log for more details' . PHP_EOL;
echo $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile() . PHP_EOL;
}
}
}
/**
* This function will fetch the desired `$template`, and output the
* Exception in a user friendly way.
*
* @since Symphony 2.4
* @param string $template
* The template name, which should correspond to something in the TEMPLATE
* directory, eg `fatalerror.fatal`.
* @param string $heading
* @param string $message
* @param string $file
* @param string $line
* @param string $lines
* @param string $trace
* @param string $queries
* @return string
* The HTML of the formatted error message.
*/
public static function renderHtml($template, $heading, $message, $file = null, $line = null, $lines = null, $trace = null, $queries = null)
{
$html = sprintf(
file_get_contents(self::getTemplate($template)),
$heading,
General::unwrapCDATA($message),
$file,
$line,
$lines,
$trace,
$queries
);
$html = str_replace('{ASSETS_URL}', ASSETS_URL, $html);
$html = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $html);
$html = str_replace('{URL}', URL, $html);
return $html;
}
}
/**
* `GenericErrorHandler` will catch any warnings or notices thrown by PHP and
* raise the errors to Exceptions so they can be dealt with by the
* `GenericExceptionHandler`. The type of errors that are raised to Exceptions
* depends on the `error_reporting` level. All errors raised, except
* `E_NOTICE` and `E_STRICT` are written to the Symphony log.
*/
class GenericErrorHandler
{
/**
* Whether the error handler is enabled or not, defaults to true.
* Setting to false will prevent any Symphony error handling from occurring
* @var boolean
*/
public static $enabled = true;
/**
* An instance of the Log class, used to write errors to the log
* @var Log
*/
private static $_Log = null;
/**
* Whether to log errors or not.
* This one is to be used temporarily, e.g., when PHP function is
* supposed to error out and throw warning and log should be kept clean.
*
* @since Symphony 2.2.2
* @var boolean
* @example
* GenericErrorHandler::$logDisabled = true;
* DoSomethingThatEndsWithWarningsYouDoNotWantInLogs();
* GenericErrorHandler::$logDisabled = false;
*/
public static $logDisabled = false;
/**
* An associative array with the PHP error constant as a key, and
* a string describing that constant as the value
* @var array
*/
public static $errorTypeStrings = array(
E_ERROR => 'Fatal Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_NOTICE => 'User Notice',
E_USER_WARNING => 'User Warning',
E_USER_ERROR => 'User Error',
E_STRICT => 'Strict Notice',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated Warning'
);
/**
* Initialise will set the error handler to be the `__CLASS__::handler`
* function.
*
* @param Log|null $Log (optional)
* An instance of a Symphony Log object to write errors to
*/
public static function initialise(Log $Log = null)
{
if (!is_null($Log)) {
self::$_Log = $Log;
}
set_error_handler(array(__CLASS__, 'handler'), error_reporting());
}
/**
* Determines if the error handler is enabled by checking that error_reporting
* is set in the php config and that $enabled is true
*
* @return boolean
*/
public static function isEnabled()
{
return (bool)error_reporting() && self::$enabled;
}
/**
* The handler function will write the error to the `$Log` if it is not `E_NOTICE`
* or `E_STRICT` before raising the error as an Exception. This allows all `E_WARNING`
* to actually be captured by an Exception handler.
*
* @param integer $code
* The error code, one of the PHP error constants
* @param string $message
* The message of the error, this will be written to the log and
* displayed as the exception message
* @param string $file
* The file that holds the logic that caused the error. Defaults to null
* @param integer $line
* The line where the error occurred.
* @throws ErrorException
* @return string
* Usually a string of HTML that will displayed to a user
*/
public static function handler($code, $message, $file = null, $line = null)
{
// Only log if the error won't be raised to an exception and the error is not `E_STRICT`
if (!self::$logDisabled && !in_array($code, array(E_STRICT)) && self::$_Log instanceof Log) {
self::$_Log->pushToLog(sprintf(
'%s %s: %s%s%s',
__CLASS__,
$code,
$message,
($line ? " on line $line" : null),
($file ? " of file $file" : null)
), $code, true);
}
if (self::isEnabled()) {
throw new ErrorException($message, 0, $code, $file, $line);
}
}
}