xref: /dokuwiki/inc/ErrorHandler.php (revision 743a69085ea848604e66e72287d87479b11949d6)
1<?php
2
3namespace dokuwiki;
4
5use dokuwiki\Exception\FatalException;
6
7/**
8 * Manage the global handling of errors and exceptions
9 *
10 * Developer may use this to log and display exceptions themselves
11 */
12class ErrorHandler
13{
14    /**
15     * Standard error codes used in PHP errors
16     * @see https://www.php.net/manual/en/errorfunc.constants.php
17     */
18    protected const ERRORCODES = [
19        1 => 'E_ERROR',
20        2 => 'E_WARNING',
21        4 => 'E_PARSE',
22        8 => 'E_NOTICE',
23        16 => 'E_CORE_ERROR',
24        32 => 'E_CORE_WARNING',
25        64 => 'E_COMPILE_ERROR',
26        128 => 'E_COMPILE_WARNING',
27        256 => 'E_USER_ERROR',
28        512 => 'E_USER_WARNING',
29        1024 => 'E_USER_NOTICE',
30        2048 => 'E_STRICT',
31        4096 => 'E_RECOVERABLE_ERROR',
32        8192 => 'E_DEPRECATED',
33        16384 => 'E_USER_DEPRECATED',
34    ];
35
36    /**
37     * Register the default error handling
38     */
39    public static function register()
40    {
41        if (!defined('DOKU_UNITTEST')) {
42            set_exception_handler(ErrorHandler::fatalException(...));
43            register_shutdown_function([ErrorHandler::class, 'fatalShutdown']);
44            set_error_handler(
45                ErrorHandler::errorHandler(...),
46                E_WARNING | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR
47            );
48        }
49    }
50
51    /**
52     * Default Exception handler to show a nice user message before dieing
53     *
54     * The exception is logged to the error log
55     *
56     * @param \Throwable $e
57     */
58    public static function fatalException($e)
59    {
60        $plugin = self::guessPlugin($e);
61        $title = hsc($e::class . ': ' . $e->getMessage());
62        $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.';
63        if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.';
64        $logged = self::logException($e)
65            ? 'More info has been written to the DokuWiki error log.'
66            : $e->getFile() . ':' . $e->getLine();
67
68        echo <<<EOT
69<!DOCTYPE html>
70<html>
71<head><title>$title</title></head>
72<body style="font-family: Arial, sans-serif">
73    <div style="width:60%; margin: auto; background-color: #fcc;
74                border: 1px solid #faa; padding: 0.5em 1em;">
75        <h1 style="font-size: 120%">$title</h1>
76        <p>$msg</p>
77        <p>$logged</p>
78    </div>
79</body>
80</html>
81EOT;
82    }
83
84    /**
85     * Convenience method to display an error message for the given Exception
86     *
87     * @param \Throwable $e
88     * @param string $intro
89     */
90    public static function showExceptionMsg($e, $intro = 'Error!')
91    {
92        $msg = hsc($intro) . '<br />' . hsc($e::class . ': ' . $e->getMessage());
93        if (self::logException($e)) $msg .= '<br />More info is available in the error log.';
94        msg($msg, -1);
95    }
96
97    /**
98     * Last resort to handle fatal errors that still can't be caught
99     */
100    public static function fatalShutdown()
101    {
102        $error = error_get_last();
103        // Check if it's a core/fatal error, otherwise it's a normal shutdown
104        if (
105            $error !== null &&
106            in_array(
107                $error['type'],
108                [
109                    E_ERROR,
110                    E_CORE_ERROR,
111                    E_COMPILE_ERROR,
112                ],
113                true
114            )
115        ) {
116            self::fatalException(
117                new FatalException($error['message'], 0, $error['type'], $error['file'], $error['line'])
118            );
119        }
120    }
121
122    /**
123     * Log the given exception to the error log
124     *
125     * @param \Throwable $e
126     * @return bool false if the logging failed
127     */
128    public static function logException($e)
129    {
130        if ($e instanceof \ErrorException) {
131            $prefix = self::ERRORCODES[$e->getSeverity()];
132        } else {
133            $prefix = $e::class;
134        }
135
136        return Logger::getInstance()->log(
137            $prefix . ': ' . $e->getMessage(),
138            $e->getTraceAsString(),
139            $e->getFile(),
140            $e->getLine()
141        );
142    }
143
144    /**
145     * Error handler to log non-exception errors
146     *
147     * @param int $errno
148     * @param string $errstr
149     * @param string $errfile
150     * @param int $errline
151     * @return bool
152     */
153    public static function errorHandler($errno, $errstr, $errfile, $errline)
154    {
155        global $conf;
156
157        // ignore supressed warnings
158        if (!(error_reporting() & $errno)) return false;
159
160        $ex = new \ErrorException(
161            $errstr,
162            0,
163            $errno,
164            $errfile,
165            $errline
166        );
167        self::logException($ex);
168
169        if ($ex->getSeverity() === E_WARNING && $conf['hidewarnings']) {
170            return true;
171        }
172
173        return false;
174    }
175
176    /**
177     * Checks the the stacktrace for plugin files
178     *
179     * @param \Throwable $e
180     * @return false|string
181     */
182    protected static function guessPlugin($e)
183    {
184        if (preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $e->getFile()), $match)) {
185            return $match[1];
186        }
187
188        foreach ($e->getTrace() as $line) {
189            if (
190                isset($line['class']) &&
191                preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match)
192            ) {
193                return $match[1];
194            }
195
196            if (
197                isset($line['file']) &&
198                preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match)
199            ) {
200                return $match[1];
201            }
202        }
203
204        return false;
205    }
206}
207