API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
6 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
6 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
6 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
6 years ago
CronArchive.php
6 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
6 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
6 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
6 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
ErrorHandler.php
206 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Piwik\Container\StaticContainer; |
| 12 | use Piwik\Exception\ErrorException; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | |
| 15 | /** |
| 16 | * Piwik's error handler function. |
| 17 | */ |
| 18 | class ErrorHandler |
| 19 | { |
| 20 | private static $fatalErrorStackTrace = []; |
| 21 | |
| 22 | /** |
| 23 | * Fatal errors in PHP do not leave behind backtraces, which can make it impossible to determine |
| 24 | * the exact cause of one. We can, however, save a partial stack trace by remembering certain execution |
| 25 | * points. This method and popFatalErrorBreadcrumb() are used for that purpose. |
| 26 | * |
| 27 | * To use this method, surround a function call w/ pushFatalErrorBreadcrumb() & popFatalErrorBreadcrumb() |
| 28 | * like so: |
| 29 | * |
| 30 | * public function theMethodIWantToAppearInFatalErrorStackTraces() |
| 31 | * { |
| 32 | * try { |
| 33 | * ErrorHandler::pushFatalErrorBreadcrumb(static::class); |
| 34 | * |
| 35 | * // ... |
| 36 | * } finally { |
| 37 | * ErrorHandler::popFatalErrorBreadcrumb(); |
| 38 | * } |
| 39 | * } |
| 40 | * |
| 41 | * If a fatal error occurs, theMethodIWantToAppearInFatalErrorStackTraces will appear in the stack trace, |
| 42 | * if PIWIK_PRINT_ERROR_BACKTRACE is true. |
| 43 | */ |
| 44 | public static function pushFatalErrorBreadcrumb($className = null, $importantArgs = null) |
| 45 | { |
| 46 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit = 2); |
| 47 | $backtrace[1]['class'] = $className; // knowing the derived class name is far more useful |
| 48 | $backtrace[1]['args'] = empty($importantArgs) ? [] : array_map('json_encode', $importantArgs); |
| 49 | array_unshift(self::$fatalErrorStackTrace, $backtrace[1]); |
| 50 | } |
| 51 | |
| 52 | public static function popFatalErrorBreadcrumb() |
| 53 | { |
| 54 | array_shift(self::$fatalErrorStackTrace); |
| 55 | } |
| 56 | |
| 57 | public static function getFatalErrorPartialBacktrace() |
| 58 | { |
| 59 | $result = ''; |
| 60 | foreach (self::$fatalErrorStackTrace as $index => $entry) { |
| 61 | $function = $entry['function']; |
| 62 | if (!empty($entry['class'])) { |
| 63 | $function = $entry['class'] . $entry['type'] . $function; |
| 64 | } |
| 65 | |
| 66 | $args = ''; |
| 67 | if (!empty($entry['args'])) { |
| 68 | $isFirst = true; |
| 69 | foreach ($entry['args'] as $name => $value) { |
| 70 | if ($isFirst) { |
| 71 | $isFirst = false; |
| 72 | } else { |
| 73 | $args .= ', '; |
| 74 | } |
| 75 | $args .= $name . '=' . $value; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $result .= sprintf("#%s %s(%s): %s(%s)\n", $index, $entry['file'], $entry['line'], $function, $args); |
| 80 | } |
| 81 | return $result; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns a string description of a PHP error number. |
| 86 | * |
| 87 | * @param int $errno `E_ERROR`, `E_WARNING`, `E_PARSE`, etc. |
| 88 | * @return string |
| 89 | */ |
| 90 | public static function getErrNoString($errno) |
| 91 | { |
| 92 | switch ($errno) { |
| 93 | case E_ERROR: |
| 94 | return "Error"; |
| 95 | case E_WARNING: |
| 96 | return "Warning"; |
| 97 | case E_PARSE: |
| 98 | return "Parse Error"; |
| 99 | case E_NOTICE: |
| 100 | return "Notice"; |
| 101 | case E_CORE_ERROR: |
| 102 | return "Core Error"; |
| 103 | case E_CORE_WARNING: |
| 104 | return "Core Warning"; |
| 105 | case E_COMPILE_ERROR: |
| 106 | return "Compile Error"; |
| 107 | case E_COMPILE_WARNING: |
| 108 | return "Compile Warning"; |
| 109 | case E_USER_ERROR: |
| 110 | return "User Error"; |
| 111 | case E_USER_WARNING: |
| 112 | return "User Warning"; |
| 113 | case E_USER_NOTICE: |
| 114 | return "User Notice"; |
| 115 | case E_STRICT: |
| 116 | return "Strict Notice"; |
| 117 | case E_RECOVERABLE_ERROR: |
| 118 | return "Recoverable Error"; |
| 119 | case E_DEPRECATED: |
| 120 | return "Deprecated"; |
| 121 | case E_USER_DEPRECATED: |
| 122 | return "User Deprecated"; |
| 123 | default: |
| 124 | return "Unknown error ($errno)"; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public static function registerErrorHandler() |
| 129 | { |
| 130 | set_error_handler(array('Piwik\ErrorHandler', 'errorHandler')); |
| 131 | } |
| 132 | |
| 133 | public static function errorHandler($errno, $errstr, $errfile, $errline) |
| 134 | { |
| 135 | // if the error has been suppressed by the @ we don't handle the error |
| 136 | if (error_reporting() == 0) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | switch ($errno) { |
| 141 | case E_ERROR: |
| 142 | case E_PARSE: |
| 143 | case E_CORE_ERROR: |
| 144 | case E_CORE_WARNING: |
| 145 | case E_COMPILE_ERROR: |
| 146 | case E_COMPILE_WARNING: |
| 147 | case E_USER_ERROR: |
| 148 | Common::sendResponseCode(500); |
| 149 | // Convert the error to an exception with an HTML message |
| 150 | $e = new \Exception(); |
| 151 | $message = self::getHtmlMessage($errno, $errstr, $errfile, $errline, $e->getTraceAsString()); |
| 152 | throw new ErrorException($message, 0, $errno, $errfile, $errline); |
| 153 | break; |
| 154 | |
| 155 | case E_WARNING: |
| 156 | case E_NOTICE: |
| 157 | case E_USER_WARNING: |
| 158 | case E_USER_NOTICE: |
| 159 | case E_STRICT: |
| 160 | case E_RECOVERABLE_ERROR: |
| 161 | case E_DEPRECATED: |
| 162 | case E_USER_DEPRECATED: |
| 163 | default: |
| 164 | $context = array('trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15)); |
| 165 | try { |
| 166 | StaticContainer::get(LoggerInterface::class)->warning( |
| 167 | self::createLogMessage($errno, $errstr, $errfile, $errline), |
| 168 | $context |
| 169 | ); |
| 170 | } catch (\Exception $ex) { |
| 171 | // ignore (it's possible for this to happen if the StaticContainer hasn't been created yet) |
| 172 | } |
| 173 | |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private static function createLogMessage($errno, $errstr, $errfile, $errline) |
| 179 | { |
| 180 | return sprintf( |
| 181 | "%s(%d): %s - %s - Matomo " . (class_exists('Piwik\Version') ? Version::VERSION : '') . " - Please report this message in the Matomo forums: https://forum.matomo.org (please do a search first as it might have been reported already)", |
| 182 | $errfile, |
| 183 | $errline, |
| 184 | ErrorHandler::getErrNoString($errno), |
| 185 | $errstr |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | private static function getHtmlMessage($errno, $errstr, $errfile, $errline, $trace) |
| 190 | { |
| 191 | $trace = Log::$debugBacktraceForTests ?: $trace; |
| 192 | |
| 193 | $message = ErrorHandler::getErrNoString($errno) . ' - ' . $errstr; |
| 194 | |
| 195 | $html = "<p>There is an error. Please report the message (Matomo " . (class_exists('Piwik\Version') ? Version::VERSION : '') . ") |
| 196 | and full backtrace in the <a target='_blank' rel='noreferrer noopener' href='https://forum.matomo.org'>Matomo forums</a> (please do a search first as it might have been reported already!).</p>"; |
| 197 | $html .= "<p><strong>{$message}</strong> in <em>{$errfile}</em>"; |
| 198 | $html .= " on line {$errline}</p>"; |
| 199 | $html .= "Backtrace:<pre>"; |
| 200 | $html .= str_replace("\n", "\n", $trace); |
| 201 | $html .= "</pre>"; |
| 202 | |
| 203 | return $html; |
| 204 | } |
| 205 | } |
| 206 |