xref: /dokuwiki/inc/ErrorHandler.php (revision cb4cefebb32128c1e6893c596b119ef03d82a7f2)
1642e976cSAndreas Gohr<?php
2642e976cSAndreas Gohr
3642e976cSAndreas Gohrnamespace dokuwiki;
4642e976cSAndreas Gohr
5642e976cSAndreas Gohrclass ErrorHandler
6642e976cSAndreas Gohr{
7642e976cSAndreas Gohr
8642e976cSAndreas Gohr    /**
9642e976cSAndreas Gohr     * Register the default error handling
10642e976cSAndreas Gohr     */
11642e976cSAndreas Gohr    public static function register()
12642e976cSAndreas Gohr    {
13642e976cSAndreas Gohr        set_error_handler([ErrorHandler::class, 'errorConverter']);
14642e976cSAndreas Gohr        if (!defined('DOKU_UNITTEST')) {
15642e976cSAndreas Gohr            set_exception_handler([ErrorHandler::class, 'fatalException']);
16*cb4cefebSAndreas Gohr            register_shutdown_function([ErrorHandler::class, 'fatalShutdown']);
17642e976cSAndreas Gohr        }
18642e976cSAndreas Gohr    }
19642e976cSAndreas Gohr
20642e976cSAndreas Gohr    /**
21642e976cSAndreas Gohr     * Default Exception handler to show a nice user message before dieing
22642e976cSAndreas Gohr     *
23642e976cSAndreas Gohr     * The exception is logged to the error log
24642e976cSAndreas Gohr     *
25642e976cSAndreas Gohr     * @param \Throwable $e
26642e976cSAndreas Gohr     */
27642e976cSAndreas Gohr    public static function fatalException($e)
28642e976cSAndreas Gohr    {
297e0b27f2SAndreas Gohr        $plugin = self::guessPlugin($e);
30642e976cSAndreas Gohr        $title = hsc(get_class($e) . ': ' . $e->getMessage());
31642e976cSAndreas Gohr        $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.';
327e0b27f2SAndreas Gohr        if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.';
33642e976cSAndreas Gohr        $logged = self::logException($e)
34642e976cSAndreas Gohr            ? 'More info has been written to the DokuWiki _error.log'
35642e976cSAndreas Gohr            : $e->getFile() . ':' . $e->getLine();
36642e976cSAndreas Gohr
37642e976cSAndreas Gohr        echo <<<EOT
38642e976cSAndreas Gohr<!DOCTYPE html>
39642e976cSAndreas Gohr<html>
40642e976cSAndreas Gohr<head><title>$title</title></head>
41642e976cSAndreas Gohr<body style="font-family: Arial, sans-serif">
42642e976cSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
43642e976cSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
44642e976cSAndreas Gohr        <h1 style="font-size: 120%">$title</h1>
45642e976cSAndreas Gohr        <p>$msg</p>
46642e976cSAndreas Gohr        <p>$logged</p>
47642e976cSAndreas Gohr    </div>
48642e976cSAndreas Gohr</body>
49642e976cSAndreas Gohr</html>
50642e976cSAndreas GohrEOT;
51642e976cSAndreas Gohr    }
52642e976cSAndreas Gohr
53642e976cSAndreas Gohr    /**
54642e976cSAndreas Gohr     * Convenience method to display an error message for the given Exception
55642e976cSAndreas Gohr     *
56642e976cSAndreas Gohr     * @param \Throwable $e
57642e976cSAndreas Gohr     * @param string $intro
58642e976cSAndreas Gohr     */
59642e976cSAndreas Gohr    public static function showExceptionMsg($e, $intro = 'Error!')
60642e976cSAndreas Gohr    {
61ffa84f81SAndreas Gohr        $msg = hsc($intro) . '<br />' . hsc(get_class($e) . ': ' . $e->getMessage());
62ffa84f81SAndreas Gohr        if (self::logException($e)) $msg .= '<br />More info is available in the _error.log';
63ffa84f81SAndreas Gohr        msg($msg, -1);
64642e976cSAndreas Gohr    }
65642e976cSAndreas Gohr
66642e976cSAndreas Gohr    /**
67642e976cSAndreas Gohr     * Default error handler to convert old school warnings, notices, etc to exceptions
68642e976cSAndreas Gohr     *
69642e976cSAndreas Gohr     * You should not need to call this directly!
70642e976cSAndreas Gohr     *
71642e976cSAndreas Gohr     * @param int $errno
72642e976cSAndreas Gohr     * @param string $errstr
73642e976cSAndreas Gohr     * @param string $errfile
74642e976cSAndreas Gohr     * @param int $errline
75642e976cSAndreas Gohr     * @return bool
76642e976cSAndreas Gohr     * @throws \ErrorException
77642e976cSAndreas Gohr     */
78642e976cSAndreas Gohr    public static function errorConverter($errno, $errstr, $errfile, $errline)
79642e976cSAndreas Gohr    {
80*cb4cefebSAndreas Gohr
81642e976cSAndreas Gohr        if (!(error_reporting() & $errno)) {
82642e976cSAndreas Gohr            // This error code is not included in error_reporting, so let it fall
83642e976cSAndreas Gohr            // through to the standard PHP error handler
84642e976cSAndreas Gohr            return false;
85642e976cSAndreas Gohr        }
86*cb4cefebSAndreas Gohr
87642e976cSAndreas Gohr        throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
88642e976cSAndreas Gohr    }
89642e976cSAndreas Gohr
90642e976cSAndreas Gohr    /**
91*cb4cefebSAndreas Gohr     * Last resort to handle fatal errors that still can't be cought
92*cb4cefebSAndreas Gohr     */
93*cb4cefebSAndreas Gohr    public static function fatalShutdown()
94*cb4cefebSAndreas Gohr    {
95*cb4cefebSAndreas Gohr        $error = error_get_last();
96*cb4cefebSAndreas Gohr        // Check if it's a core/fatal error, otherwise it's a normal shutdown
97*cb4cefebSAndreas Gohr        if (
98*cb4cefebSAndreas Gohr            $error !== null &&
99*cb4cefebSAndreas Gohr            in_array(
100*cb4cefebSAndreas Gohr                $error['type'],
101*cb4cefebSAndreas Gohr                [
102*cb4cefebSAndreas Gohr                    E_ERROR,
103*cb4cefebSAndreas Gohr                    E_CORE_ERROR,
104*cb4cefebSAndreas Gohr                    E_COMPILE_ERROR,
105*cb4cefebSAndreas Gohr                ]
106*cb4cefebSAndreas Gohr            )
107*cb4cefebSAndreas Gohr        ) {
108*cb4cefebSAndreas Gohr            self::fatalException(
109*cb4cefebSAndreas Gohr                new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])
110*cb4cefebSAndreas Gohr            );
111*cb4cefebSAndreas Gohr        }
112*cb4cefebSAndreas Gohr    }
113*cb4cefebSAndreas Gohr
114*cb4cefebSAndreas Gohr    /**
115642e976cSAndreas Gohr     * Log the given exception to the error log
116642e976cSAndreas Gohr     *
117642e976cSAndreas Gohr     * @param \Throwable $e
118642e976cSAndreas Gohr     * @return bool false if the logging failed
119642e976cSAndreas Gohr     */
120642e976cSAndreas Gohr    public static function logException($e)
121642e976cSAndreas Gohr    {
122642e976cSAndreas Gohr        global $conf;
123642e976cSAndreas Gohr
1240e69c9afSAndreas Gohr        $log = join("\t", [
1250e69c9afSAndreas Gohr                gmdate('c'),
1260e69c9afSAndreas Gohr                get_class($e),
1270e69c9afSAndreas Gohr                $e->getFile() . '(' . $e->getLine() . ')',
1280e69c9afSAndreas Gohr                $e->getMessage(),
1290e69c9afSAndreas Gohr            ]) . "\n";
1300e69c9afSAndreas Gohr        $log .= $e->getTraceAsString() . "\n";
131642e976cSAndreas Gohr        return io_saveFile($conf['cachedir'] . '/_error.log', $log, true);
132642e976cSAndreas Gohr    }
1337e0b27f2SAndreas Gohr
1347e0b27f2SAndreas Gohr    /**
1357e0b27f2SAndreas Gohr     * Checks the the stacktrace for plugin files
1367e0b27f2SAndreas Gohr     *
1377e0b27f2SAndreas Gohr     * @param \Throwable $e
1387e0b27f2SAndreas Gohr     * @return false|string
1397e0b27f2SAndreas Gohr     */
1407e0b27f2SAndreas Gohr    protected static function guessPlugin($e)
1417e0b27f2SAndreas Gohr    {
1427e0b27f2SAndreas Gohr        foreach ($e->getTrace() as $line) {
1437e0b27f2SAndreas Gohr            if (
1447e0b27f2SAndreas Gohr                isset($line['class']) &&
1457e0b27f2SAndreas Gohr                preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match)
1467e0b27f2SAndreas Gohr            ) {
1477e0b27f2SAndreas Gohr                return $match[1];
1487e0b27f2SAndreas Gohr            }
1497e0b27f2SAndreas Gohr
1507e0b27f2SAndreas Gohr            if (
1517e0b27f2SAndreas Gohr                isset($line['file']) &&
1527e0b27f2SAndreas Gohr                preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match)
1537e0b27f2SAndreas Gohr            ) {
1547e0b27f2SAndreas Gohr                return $match[1];
1557e0b27f2SAndreas Gohr            }
1567e0b27f2SAndreas Gohr        }
1577e0b27f2SAndreas Gohr
1587e0b27f2SAndreas Gohr        return false;
1597e0b27f2SAndreas Gohr    }
160642e976cSAndreas Gohr}
161