xref: /dokuwiki/inc/ErrorHandler.php (revision dbe0790d4cb5bac92c977818735d040e84836756)
1642e976cSAndreas Gohr<?php
2642e976cSAndreas Gohr
3642e976cSAndreas Gohrnamespace dokuwiki;
4642e976cSAndreas Gohr
5*dbe0790dSAndreas Gohruse dokuwiki\Exception\FatalException;
6*dbe0790dSAndreas Gohr
7*dbe0790dSAndreas Gohr/**
8*dbe0790dSAndreas Gohr * Manage the global handling of errors and exceptions
9*dbe0790dSAndreas Gohr *
10*dbe0790dSAndreas Gohr * Developer may use this to log and display exceptions themselves
11*dbe0790dSAndreas Gohr */
12642e976cSAndreas Gohrclass ErrorHandler
13642e976cSAndreas Gohr{
14642e976cSAndreas Gohr
15642e976cSAndreas Gohr    /**
16642e976cSAndreas Gohr     * Register the default error handling
17642e976cSAndreas Gohr     */
18642e976cSAndreas Gohr    public static function register()
19642e976cSAndreas Gohr    {
20642e976cSAndreas Gohr        set_error_handler([ErrorHandler::class, 'errorConverter']);
21642e976cSAndreas Gohr        if (!defined('DOKU_UNITTEST')) {
22642e976cSAndreas Gohr            set_exception_handler([ErrorHandler::class, 'fatalException']);
23cb4cefebSAndreas Gohr            register_shutdown_function([ErrorHandler::class, 'fatalShutdown']);
24642e976cSAndreas Gohr        }
25642e976cSAndreas Gohr    }
26642e976cSAndreas Gohr
27642e976cSAndreas Gohr    /**
28642e976cSAndreas Gohr     * Default Exception handler to show a nice user message before dieing
29642e976cSAndreas Gohr     *
30642e976cSAndreas Gohr     * The exception is logged to the error log
31642e976cSAndreas Gohr     *
32642e976cSAndreas Gohr     * @param \Throwable $e
33642e976cSAndreas Gohr     */
34642e976cSAndreas Gohr    public static function fatalException($e)
35642e976cSAndreas Gohr    {
367e0b27f2SAndreas Gohr        $plugin = self::guessPlugin($e);
37642e976cSAndreas Gohr        $title = hsc(get_class($e) . ': ' . $e->getMessage());
38642e976cSAndreas Gohr        $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.';
397e0b27f2SAndreas Gohr        if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.';
40642e976cSAndreas Gohr        $logged = self::logException($e)
41642e976cSAndreas Gohr            ? 'More info has been written to the DokuWiki _error.log'
42642e976cSAndreas Gohr            : $e->getFile() . ':' . $e->getLine();
43642e976cSAndreas Gohr
44642e976cSAndreas Gohr        echo <<<EOT
45642e976cSAndreas Gohr<!DOCTYPE html>
46642e976cSAndreas Gohr<html>
47642e976cSAndreas Gohr<head><title>$title</title></head>
48642e976cSAndreas Gohr<body style="font-family: Arial, sans-serif">
49642e976cSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
50642e976cSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
51642e976cSAndreas Gohr        <h1 style="font-size: 120%">$title</h1>
52642e976cSAndreas Gohr        <p>$msg</p>
53642e976cSAndreas Gohr        <p>$logged</p>
54642e976cSAndreas Gohr    </div>
55642e976cSAndreas Gohr</body>
56642e976cSAndreas Gohr</html>
57642e976cSAndreas GohrEOT;
58642e976cSAndreas Gohr    }
59642e976cSAndreas Gohr
60642e976cSAndreas Gohr    /**
61642e976cSAndreas Gohr     * Convenience method to display an error message for the given Exception
62642e976cSAndreas Gohr     *
63642e976cSAndreas Gohr     * @param \Throwable $e
64642e976cSAndreas Gohr     * @param string $intro
65642e976cSAndreas Gohr     */
66642e976cSAndreas Gohr    public static function showExceptionMsg($e, $intro = 'Error!')
67642e976cSAndreas Gohr    {
68ffa84f81SAndreas Gohr        $msg = hsc($intro) . '<br />' . hsc(get_class($e) . ': ' . $e->getMessage());
69ffa84f81SAndreas Gohr        if (self::logException($e)) $msg .= '<br />More info is available in the _error.log';
70ffa84f81SAndreas Gohr        msg($msg, -1);
71642e976cSAndreas Gohr    }
72642e976cSAndreas Gohr
73642e976cSAndreas Gohr    /**
74642e976cSAndreas Gohr     * Default error handler to convert old school warnings, notices, etc to exceptions
75642e976cSAndreas Gohr     *
76642e976cSAndreas Gohr     * You should not need to call this directly!
77642e976cSAndreas Gohr     *
78642e976cSAndreas Gohr     * @param int $errno
79642e976cSAndreas Gohr     * @param string $errstr
80642e976cSAndreas Gohr     * @param string $errfile
81642e976cSAndreas Gohr     * @param int $errline
82642e976cSAndreas Gohr     * @return bool
83642e976cSAndreas Gohr     * @throws \ErrorException
84642e976cSAndreas Gohr     */
85642e976cSAndreas Gohr    public static function errorConverter($errno, $errstr, $errfile, $errline)
86642e976cSAndreas Gohr    {
87cb4cefebSAndreas Gohr
88642e976cSAndreas Gohr        if (!(error_reporting() & $errno)) {
89642e976cSAndreas Gohr            // This error code is not included in error_reporting, so let it fall
90642e976cSAndreas Gohr            // through to the standard PHP error handler
91642e976cSAndreas Gohr            return false;
92642e976cSAndreas Gohr        }
93cb4cefebSAndreas Gohr
94642e976cSAndreas Gohr        throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
95642e976cSAndreas Gohr    }
96642e976cSAndreas Gohr
97642e976cSAndreas Gohr    /**
98*dbe0790dSAndreas Gohr     * Last resort to handle fatal errors that still can't be caught
99cb4cefebSAndreas Gohr     */
100cb4cefebSAndreas Gohr    public static function fatalShutdown()
101cb4cefebSAndreas Gohr    {
102cb4cefebSAndreas Gohr        $error = error_get_last();
103cb4cefebSAndreas Gohr        // Check if it's a core/fatal error, otherwise it's a normal shutdown
104cb4cefebSAndreas Gohr        if (
105cb4cefebSAndreas Gohr            $error !== null &&
106cb4cefebSAndreas Gohr            in_array(
107cb4cefebSAndreas Gohr                $error['type'],
108cb4cefebSAndreas Gohr                [
109cb4cefebSAndreas Gohr                    E_ERROR,
110cb4cefebSAndreas Gohr                    E_CORE_ERROR,
111cb4cefebSAndreas Gohr                    E_COMPILE_ERROR,
112cb4cefebSAndreas Gohr                ]
113cb4cefebSAndreas Gohr            )
114cb4cefebSAndreas Gohr        ) {
115cb4cefebSAndreas Gohr            self::fatalException(
116*dbe0790dSAndreas Gohr                new FatalException($error['message'], 0, $error['type'], $error['file'], $error['line'])
117cb4cefebSAndreas Gohr            );
118cb4cefebSAndreas Gohr        }
119cb4cefebSAndreas Gohr    }
120cb4cefebSAndreas Gohr
121cb4cefebSAndreas Gohr    /**
122642e976cSAndreas Gohr     * Log the given exception to the error log
123642e976cSAndreas Gohr     *
124642e976cSAndreas Gohr     * @param \Throwable $e
125642e976cSAndreas Gohr     * @return bool false if the logging failed
126642e976cSAndreas Gohr     */
127642e976cSAndreas Gohr    public static function logException($e)
128642e976cSAndreas Gohr    {
129642e976cSAndreas Gohr        global $conf;
130642e976cSAndreas Gohr
1310e69c9afSAndreas Gohr        $log = join("\t", [
1320e69c9afSAndreas Gohr                gmdate('c'),
1330e69c9afSAndreas Gohr                get_class($e),
1340e69c9afSAndreas Gohr                $e->getFile() . '(' . $e->getLine() . ')',
1350e69c9afSAndreas Gohr                $e->getMessage(),
1360e69c9afSAndreas Gohr            ]) . "\n";
1370e69c9afSAndreas Gohr        $log .= $e->getTraceAsString() . "\n";
138642e976cSAndreas Gohr        return io_saveFile($conf['cachedir'] . '/_error.log', $log, true);
139642e976cSAndreas Gohr    }
1407e0b27f2SAndreas Gohr
1417e0b27f2SAndreas Gohr    /**
1427e0b27f2SAndreas Gohr     * Checks the the stacktrace for plugin files
1437e0b27f2SAndreas Gohr     *
1447e0b27f2SAndreas Gohr     * @param \Throwable $e
1457e0b27f2SAndreas Gohr     * @return false|string
1467e0b27f2SAndreas Gohr     */
1477e0b27f2SAndreas Gohr    protected static function guessPlugin($e)
1487e0b27f2SAndreas Gohr    {
149*dbe0790dSAndreas Gohr        if (preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $e->getFile()), $match)) {
150*dbe0790dSAndreas Gohr            return $match[1];
151*dbe0790dSAndreas Gohr        }
152*dbe0790dSAndreas Gohr
1537e0b27f2SAndreas Gohr        foreach ($e->getTrace() as $line) {
1547e0b27f2SAndreas Gohr            if (
1557e0b27f2SAndreas Gohr                isset($line['class']) &&
1567e0b27f2SAndreas Gohr                preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match)
1577e0b27f2SAndreas Gohr            ) {
1587e0b27f2SAndreas Gohr                return $match[1];
1597e0b27f2SAndreas Gohr            }
1607e0b27f2SAndreas Gohr
1617e0b27f2SAndreas Gohr            if (
1627e0b27f2SAndreas Gohr                isset($line['file']) &&
1637e0b27f2SAndreas Gohr                preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match)
1647e0b27f2SAndreas Gohr            ) {
1657e0b27f2SAndreas Gohr                return $match[1];
1667e0b27f2SAndreas Gohr            }
1677e0b27f2SAndreas Gohr        }
1687e0b27f2SAndreas Gohr
1697e0b27f2SAndreas Gohr        return false;
1707e0b27f2SAndreas Gohr    }
171642e976cSAndreas Gohr}
172