Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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
 
991 lars 26
    /**
27
     * Allows {$foo->bar} where bar is not an object (e.g. null or false).
28
     * @var bool
29
     */
30
    public $allowDereferencingNonObjects = true;
31
 
148 lars 32
    private $previousErrorHandler = null;
33
 
34
    /**
35
     * Enable error handler to intercept errors
36
     */
37
    public function activate() {
38
        /*
39
            Error muting is done because some people implemented custom error_handlers using
40
            https://php.net/set_error_handler and for some reason did not understand the following paragraph:
41
 
42
            It is important to remember that the standard PHP error handler is completely bypassed for the
43
            error types specified by error_types unless the callback function returns FALSE.
44
            error_reporting() settings will have no effect and your error handler will be called regardless -
45
            however you are still able to read the current value of error_reporting and act appropriately.
46
            Of particular note is that this value will be 0 if the statement that caused the error was
47
            prepended by the @ error-control operator.
48
        */
49
        $this->previousErrorHandler = set_error_handler([$this, 'handleError']);
50
    }
51
 
52
    /**
53
     * Disable error handler
54
     */
55
    public function deactivate() {
56
        restore_error_handler();
57
        $this->previousErrorHandler = null;
58
    }
59
 
60
    /**
61
     * Error Handler to mute expected messages
62
     *
63
     * @link https://php.net/set_error_handler
64
     *
65
     * @param integer $errno Error level
66
     * @param         $errstr
67
     * @param         $errfile
68
     * @param         $errline
69
     * @param         $errcontext
70
     *
71
     * @return bool
72
     */
73
    public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
74
    {
991 lars 75
 
76
        if ($this->allowUndefinedVars && preg_match(
77
                '/^(Attempt to read property "value" on null|Trying to get property (\'value\' )?of non-object)/',
78
                $errstr
79
            )) {
148 lars 80
            return; // suppresses this error
81
        }
82
 
83
        if ($this->allowUndefinedArrayKeys && preg_match(
991 lars 84
            '/^(Undefined index|Undefined array key|Trying to access array offset on value of type)/',
148 lars 85
            $errstr
86
        )) {
87
            return; // suppresses this error
88
        }
89
 
991 lars 90
        if ($this->allowDereferencingNonObjects && preg_match(
91
                '/^Attempt to read property ".+?" on/',
92
                $errstr
93
            )) {
94
            return; // suppresses this error
95
        }
96
 
148 lars 97
        // pass all other errors through to the previous error handler or to the default PHP error handler
98
        return $this->previousErrorHandler ?
99
            call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext) : false;
100
    }
101
}