| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Smarty error handler to fix new error levels in PHP8 for backwards compatibility
|
|
|
5 |
*
|
|
|
6 |
* @package Smarty
|
|
|
7 |
* @subpackage PluginsInternal
|
|
|
8 |
* @author Simon Wisselink
|
|
|
9 |
*
|
|
|
10 |
*/
|
|
|
11 |
class Smarty_Internal_ErrorHandler
|
|
|
12 |
{
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Allows {$foo} where foo is unset.
|
|
|
16 |
* @var bool
|
|
|
17 |
*/
|
|
|
18 |
public $allowUndefinedVars = true;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Allows {$foo.bar} where bar is unset and {$foo.bar1.bar2} where either bar1 or bar2 is unset.
|
|
|
22 |
* @var bool
|
|
|
23 |
*/
|
|
|
24 |
public $allowUndefinedArrayKeys = true;
|
|
|
25 |
|
|
|
26 |
private $previousErrorHandler = null;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Enable error handler to intercept errors
|
|
|
30 |
*/
|
|
|
31 |
public function activate() {
|
|
|
32 |
/*
|
|
|
33 |
Error muting is done because some people implemented custom error_handlers using
|
|
|
34 |
https://php.net/set_error_handler and for some reason did not understand the following paragraph:
|
|
|
35 |
|
|
|
36 |
It is important to remember that the standard PHP error handler is completely bypassed for the
|
|
|
37 |
error types specified by error_types unless the callback function returns FALSE.
|
|
|
38 |
error_reporting() settings will have no effect and your error handler will be called regardless -
|
|
|
39 |
however you are still able to read the current value of error_reporting and act appropriately.
|
|
|
40 |
Of particular note is that this value will be 0 if the statement that caused the error was
|
|
|
41 |
prepended by the @ error-control operator.
|
|
|
42 |
*/
|
|
|
43 |
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Disable error handler
|
|
|
48 |
*/
|
|
|
49 |
public function deactivate() {
|
|
|
50 |
restore_error_handler();
|
|
|
51 |
$this->previousErrorHandler = null;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Error Handler to mute expected messages
|
|
|
56 |
*
|
|
|
57 |
* @link https://php.net/set_error_handler
|
|
|
58 |
*
|
|
|
59 |
* @param integer $errno Error level
|
|
|
60 |
* @param $errstr
|
|
|
61 |
* @param $errfile
|
|
|
62 |
* @param $errline
|
|
|
63 |
* @param $errcontext
|
|
|
64 |
*
|
|
|
65 |
* @return bool
|
|
|
66 |
*/
|
|
|
67 |
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
|
|
|
68 |
{
|
|
|
69 |
if ($this->allowUndefinedVars && $errstr == 'Attempt to read property "value" on null') {
|
|
|
70 |
return; // suppresses this error
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if ($this->allowUndefinedArrayKeys && preg_match(
|
|
|
74 |
'/^(Undefined array key|Trying to access array offset on value of type null)/',
|
|
|
75 |
$errstr
|
|
|
76 |
)) {
|
|
|
77 |
return; // suppresses this error
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// pass all other errors through to the previous error handler or to the default PHP error handler
|
|
|
81 |
return $this->previousErrorHandler ?
|
|
|
82 |
call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext) : false;
|
|
|
83 |
}
|
|
|
84 |
}
|