1<?php 2 3namespace dokuwiki; 4 5class ErrorHandler 6{ 7 8 /** 9 * Register the default error handling 10 */ 11 public static function register() 12 { 13 set_error_handler([ErrorHandler::class, 'errorConverter']); 14 if (!defined('DOKU_UNITTEST')) { 15 set_exception_handler([ErrorHandler::class, 'fatalException']); 16 register_shutdown_function([ErrorHandler::class, 'fatalShutdown']); 17 } 18 } 19 20 /** 21 * Default Exception handler to show a nice user message before dieing 22 * 23 * The exception is logged to the error log 24 * 25 * @param \Throwable $e 26 */ 27 public static function fatalException($e) 28 { 29 $plugin = self::guessPlugin($e); 30 $title = hsc(get_class($e) . ': ' . $e->getMessage()); 31 $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.'; 32 if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.'; 33 $logged = self::logException($e) 34 ? 'More info has been written to the DokuWiki _error.log' 35 : $e->getFile() . ':' . $e->getLine(); 36 37 echo <<<EOT 38<!DOCTYPE html> 39<html> 40<head><title>$title</title></head> 41<body style="font-family: Arial, sans-serif"> 42 <div style="width:60%; margin: auto; background-color: #fcc; 43 border: 1px solid #faa; padding: 0.5em 1em;"> 44 <h1 style="font-size: 120%">$title</h1> 45 <p>$msg</p> 46 <p>$logged</p> 47 </div> 48</body> 49</html> 50EOT; 51 } 52 53 /** 54 * Convenience method to display an error message for the given Exception 55 * 56 * @param \Throwable $e 57 * @param string $intro 58 */ 59 public static function showExceptionMsg($e, $intro = 'Error!') 60 { 61 $msg = hsc($intro) . '<br />' . hsc(get_class($e) . ': ' . $e->getMessage()); 62 if (self::logException($e)) $msg .= '<br />More info is available in the _error.log'; 63 msg($msg, -1); 64 } 65 66 /** 67 * Default error handler to convert old school warnings, notices, etc to exceptions 68 * 69 * You should not need to call this directly! 70 * 71 * @param int $errno 72 * @param string $errstr 73 * @param string $errfile 74 * @param int $errline 75 * @return bool 76 * @throws \ErrorException 77 */ 78 public static function errorConverter($errno, $errstr, $errfile, $errline) 79 { 80 81 if (!(error_reporting() & $errno)) { 82 // This error code is not included in error_reporting, so let it fall 83 // through to the standard PHP error handler 84 return false; 85 } 86 87 throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); 88 } 89 90 /** 91 * Last resort to handle fatal errors that still can't be cought 92 */ 93 public static function fatalShutdown() 94 { 95 $error = error_get_last(); 96 // Check if it's a core/fatal error, otherwise it's a normal shutdown 97 if ( 98 $error !== null && 99 in_array( 100 $error['type'], 101 [ 102 E_ERROR, 103 E_CORE_ERROR, 104 E_COMPILE_ERROR, 105 ] 106 ) 107 ) { 108 self::fatalException( 109 new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']) 110 ); 111 } 112 } 113 114 /** 115 * Log the given exception to the error log 116 * 117 * @param \Throwable $e 118 * @return bool false if the logging failed 119 */ 120 public static function logException($e) 121 { 122 global $conf; 123 124 $log = join("\t", [ 125 gmdate('c'), 126 get_class($e), 127 $e->getFile() . '(' . $e->getLine() . ')', 128 $e->getMessage(), 129 ]) . "\n"; 130 $log .= $e->getTraceAsString() . "\n"; 131 return io_saveFile($conf['cachedir'] . '/_error.log', $log, true); 132 } 133 134 /** 135 * Checks the the stacktrace for plugin files 136 * 137 * @param \Throwable $e 138 * @return false|string 139 */ 140 protected static function guessPlugin($e) 141 { 142 foreach ($e->getTrace() as $line) { 143 if ( 144 isset($line['class']) && 145 preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match) 146 ) { 147 return $match[1]; 148 } 149 150 if ( 151 isset($line['file']) && 152 preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match) 153 ) { 154 return $match[1]; 155 } 156 } 157 158 return false; 159 } 160} 161