xref: /dokuwiki/inc/ErrorHandler.php (revision be6462f4bb18e8b92e47ced9cfc4141997d0687a)
1642e976cSAndreas Gohr<?php
2642e976cSAndreas Gohr
3642e976cSAndreas Gohrnamespace dokuwiki;
4642e976cSAndreas Gohr
5dbe0790dSAndreas Gohruse dokuwiki\Exception\FatalException;
6dbe0790dSAndreas Gohr
7dbe0790dSAndreas Gohr/**
8dbe0790dSAndreas Gohr * Manage the global handling of errors and exceptions
9dbe0790dSAndreas Gohr *
10dbe0790dSAndreas Gohr * Developer may use this to log and display exceptions themselves
11dbe0790dSAndreas Gohr */
12642e976cSAndreas Gohrclass ErrorHandler
13642e976cSAndreas Gohr{
14642e976cSAndreas Gohr    /**
15642e976cSAndreas Gohr     * Register the default error handling
16642e976cSAndreas Gohr     */
17642e976cSAndreas Gohr    public static function register()
18642e976cSAndreas Gohr    {
19642e976cSAndreas Gohr        if (!defined('DOKU_UNITTEST')) {
20642e976cSAndreas Gohr            set_exception_handler([ErrorHandler::class, 'fatalException']);
21cb4cefebSAndreas Gohr            register_shutdown_function([ErrorHandler::class, 'fatalShutdown']);
22*be6462f4SAndreas Gohr            set_error_handler([ErrorHandler::class, 'logWarning'], E_WARNING);
23642e976cSAndreas Gohr        }
24642e976cSAndreas Gohr    }
25642e976cSAndreas Gohr
26642e976cSAndreas Gohr    /**
27642e976cSAndreas Gohr     * Default Exception handler to show a nice user message before dieing
28642e976cSAndreas Gohr     *
29642e976cSAndreas Gohr     * The exception is logged to the error log
30642e976cSAndreas Gohr     *
31642e976cSAndreas Gohr     * @param \Throwable $e
32642e976cSAndreas Gohr     */
33642e976cSAndreas Gohr    public static function fatalException($e)
34642e976cSAndreas Gohr    {
357e0b27f2SAndreas Gohr        $plugin = self::guessPlugin($e);
36642e976cSAndreas Gohr        $title = hsc(get_class($e) . ': ' . $e->getMessage());
37642e976cSAndreas Gohr        $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.';
387e0b27f2SAndreas Gohr        if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.';
39642e976cSAndreas Gohr        $logged = self::logException($e)
400ecde6ceSAndreas Gohr            ? 'More info has been written to the DokuWiki error log.'
41642e976cSAndreas Gohr            : $e->getFile() . ':' . $e->getLine();
42642e976cSAndreas Gohr
43642e976cSAndreas Gohr        echo <<<EOT
44642e976cSAndreas Gohr<!DOCTYPE html>
45642e976cSAndreas Gohr<html>
46642e976cSAndreas Gohr<head><title>$title</title></head>
47642e976cSAndreas Gohr<body style="font-family: Arial, sans-serif">
48642e976cSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
49642e976cSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
50642e976cSAndreas Gohr        <h1 style="font-size: 120%">$title</h1>
51642e976cSAndreas Gohr        <p>$msg</p>
52642e976cSAndreas Gohr        <p>$logged</p>
53642e976cSAndreas Gohr    </div>
54642e976cSAndreas Gohr</body>
55642e976cSAndreas Gohr</html>
56642e976cSAndreas GohrEOT;
57642e976cSAndreas Gohr    }
58642e976cSAndreas Gohr
59642e976cSAndreas Gohr    /**
60642e976cSAndreas Gohr     * Convenience method to display an error message for the given Exception
61642e976cSAndreas Gohr     *
62642e976cSAndreas Gohr     * @param \Throwable $e
63642e976cSAndreas Gohr     * @param string $intro
64642e976cSAndreas Gohr     */
65642e976cSAndreas Gohr    public static function showExceptionMsg($e, $intro = 'Error!')
66642e976cSAndreas Gohr    {
67ffa84f81SAndreas Gohr        $msg = hsc($intro) . '<br />' . hsc(get_class($e) . ': ' . $e->getMessage());
680ecde6ceSAndreas Gohr        if (self::logException($e)) $msg .= '<br />More info is available in the error log.';
69ffa84f81SAndreas Gohr        msg($msg, -1);
70642e976cSAndreas Gohr    }
71642e976cSAndreas Gohr
72642e976cSAndreas Gohr    /**
73dbe0790dSAndreas Gohr     * Last resort to handle fatal errors that still can't be caught
74cb4cefebSAndreas Gohr     */
75cb4cefebSAndreas Gohr    public static function fatalShutdown()
76cb4cefebSAndreas Gohr    {
77cb4cefebSAndreas Gohr        $error = error_get_last();
78cb4cefebSAndreas Gohr        // Check if it's a core/fatal error, otherwise it's a normal shutdown
79cb4cefebSAndreas Gohr        if (
80cb4cefebSAndreas Gohr            $error !== null &&
81cb4cefebSAndreas Gohr            in_array(
82cb4cefebSAndreas Gohr                $error['type'],
83cb4cefebSAndreas Gohr                [
84cb4cefebSAndreas Gohr                    E_ERROR,
85cb4cefebSAndreas Gohr                    E_CORE_ERROR,
86cb4cefebSAndreas Gohr                    E_COMPILE_ERROR,
87cb4cefebSAndreas Gohr                ]
88cb4cefebSAndreas Gohr            )
89cb4cefebSAndreas Gohr        ) {
90cb4cefebSAndreas Gohr            self::fatalException(
91dbe0790dSAndreas Gohr                new FatalException($error['message'], 0, $error['type'], $error['file'], $error['line'])
92cb4cefebSAndreas Gohr            );
93cb4cefebSAndreas Gohr        }
94cb4cefebSAndreas Gohr    }
95cb4cefebSAndreas Gohr
96cb4cefebSAndreas Gohr    /**
97642e976cSAndreas Gohr     * Log the given exception to the error log
98642e976cSAndreas Gohr     *
99642e976cSAndreas Gohr     * @param \Throwable $e
100642e976cSAndreas Gohr     * @return bool false if the logging failed
101642e976cSAndreas Gohr     */
102642e976cSAndreas Gohr    public static function logException($e)
103642e976cSAndreas Gohr    {
104*be6462f4SAndreas Gohr        if (is_a($e, \ErrorException::class) && $e->getSeverity() === E_WARNING) {
105*be6462f4SAndreas Gohr            $prefix = 'Warning';
106*be6462f4SAndreas Gohr        } else {
107*be6462f4SAndreas Gohr            $prefix = get_class($e);
108*be6462f4SAndreas Gohr        }
109*be6462f4SAndreas Gohr
110*be6462f4SAndreas Gohr
1110ecde6ceSAndreas Gohr        return Logger::getInstance()->log(
112*be6462f4SAndreas Gohr            $prefix . ': ' . $e->getMessage(),
1130ecde6ceSAndreas Gohr            $e->getTraceAsString(),
1140ecde6ceSAndreas Gohr            $e->getFile(),
1150ecde6ceSAndreas Gohr            $e->getLine()
1160ecde6ceSAndreas Gohr        );
117642e976cSAndreas Gohr    }
1187e0b27f2SAndreas Gohr
1197e0b27f2SAndreas Gohr    /**
120*be6462f4SAndreas Gohr     * Log a warning
121*be6462f4SAndreas Gohr     *
122*be6462f4SAndreas Gohr     * @param int $errno
123*be6462f4SAndreas Gohr     * @param string $errstr
124*be6462f4SAndreas Gohr     * @param string $errfile
125*be6462f4SAndreas Gohr     * @param int $errline
126*be6462f4SAndreas Gohr     * @return bool
127*be6462f4SAndreas Gohr     */
128*be6462f4SAndreas Gohr    public static function logWarning($errno, $errstr, $errfile, $errline)
129*be6462f4SAndreas Gohr    {
130*be6462f4SAndreas Gohr        global $conf;
131*be6462f4SAndreas Gohr
132*be6462f4SAndreas Gohr        // ignore supressed warnings
133*be6462f4SAndreas Gohr        if (!(error_reporting() & $errno)) return false;
134*be6462f4SAndreas Gohr
135*be6462f4SAndreas Gohr        $ex = new \ErrorException(
136*be6462f4SAndreas Gohr            $errstr,
137*be6462f4SAndreas Gohr            0,
138*be6462f4SAndreas Gohr            $errno,
139*be6462f4SAndreas Gohr            $errfile,
140*be6462f4SAndreas Gohr            $errline
141*be6462f4SAndreas Gohr        );
142*be6462f4SAndreas Gohr        self::logException($ex);
143*be6462f4SAndreas Gohr
144*be6462f4SAndreas Gohr        return (bool)$conf['hidewarnings'];
145*be6462f4SAndreas Gohr    }
146*be6462f4SAndreas Gohr
147*be6462f4SAndreas Gohr    /**
1487e0b27f2SAndreas Gohr     * Checks the the stacktrace for plugin files
1497e0b27f2SAndreas Gohr     *
1507e0b27f2SAndreas Gohr     * @param \Throwable $e
1517e0b27f2SAndreas Gohr     * @return false|string
1527e0b27f2SAndreas Gohr     */
1537e0b27f2SAndreas Gohr    protected static function guessPlugin($e)
1547e0b27f2SAndreas Gohr    {
155dbe0790dSAndreas Gohr        if (preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $e->getFile()), $match)) {
156dbe0790dSAndreas Gohr            return $match[1];
157dbe0790dSAndreas Gohr        }
158dbe0790dSAndreas Gohr
1597e0b27f2SAndreas Gohr        foreach ($e->getTrace() as $line) {
1607e0b27f2SAndreas Gohr            if (
1617e0b27f2SAndreas Gohr                isset($line['class']) &&
1627e0b27f2SAndreas Gohr                preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match)
1637e0b27f2SAndreas Gohr            ) {
1647e0b27f2SAndreas Gohr                return $match[1];
1657e0b27f2SAndreas Gohr            }
1667e0b27f2SAndreas Gohr
1677e0b27f2SAndreas Gohr            if (
1687e0b27f2SAndreas Gohr                isset($line['file']) &&
1697e0b27f2SAndreas Gohr                preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match)
1707e0b27f2SAndreas Gohr            ) {
1717e0b27f2SAndreas Gohr                return $match[1];
1727e0b27f2SAndreas Gohr            }
1737e0b27f2SAndreas Gohr        }
1747e0b27f2SAndreas Gohr
1757e0b27f2SAndreas Gohr        return false;
1767e0b27f2SAndreas Gohr    }
177642e976cSAndreas Gohr}
178