PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / ErrorHandler.php
matomo / app / core Last commit date
API 5 years ago Access 5 years ago Application 5 years ago Archive 5 years ago ArchiveProcessor 5 years ago Archiver 5 years ago AssetManager 5 years ago Auth 5 years ago Category 5 years ago CliMulti 5 years ago Columns 5 years ago Composer 5 years ago Concurrency 5 years ago Config 5 years ago Container 5 years ago CronArchive 5 years ago DataAccess 5 years ago DataFiles 5 years ago DataTable 5 years ago Db 5 years ago DeviceDetector 5 years ago Email 5 years ago Exception 5 years ago Http 5 years ago Intl 5 years ago Mail 5 years ago Measurable 5 years ago Menu 5 years ago Metrics 5 years ago Notification 5 years ago Period 5 years ago Plugin 5 years ago ProfessionalServices 5 years ago Report 5 years ago ReportRenderer 5 years ago Scheduler 5 years ago Segment 5 years ago Session 5 years ago Settings 5 years ago Tracker 5 years ago Translation 5 years ago UpdateCheck 5 years ago Updater 5 years ago Updates 5 years ago Validators 5 years ago View 5 years ago ViewDataTable 5 years ago Visualization 5 years ago Widget 5 years ago .htaccess 6 years ago Access.php 5 years ago Archive.php 5 years ago ArchiveProcessor.php 5 years ago AssetManager.php 5 years ago Auth.php 5 years ago AuthResult.php 5 years ago BaseFactory.php 5 years ago Cache.php 5 years ago CacheId.php 5 years ago CliMulti.php 5 years ago Common.php 5 years ago Config.php 5 years ago Console.php 5 years ago Context.php 5 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 5 years ago DataTable.php 5 years ago Date.php 5 years ago Db.php 5 years ago DbHelper.php 5 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 5 years ago ExceptionHandler.php 5 years ago FileIntegrity.php 5 years ago Filechecks.php 5 years ago Filesystem.php 5 years ago FrontController.php 5 years ago Http.php 5 years ago IP.php 5 years ago Log.php 5 years ago LogDeleter.php 5 years ago Mail.php 5 years ago Metrics.php 5 years ago NoAccessException.php 5 years ago Nonce.php 5 years ago Notification.php 5 years ago NumberFormatter.php 5 years ago Option.php 5 years ago Period.php 5 years ago Piwik.php 5 years ago Plugin.php 5 years ago Profiler.php 5 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 5 years ago QuickForm2.php 5 years ago RankingQuery.php 5 years ago ReportRenderer.php 5 years ago Segment.php 5 years ago Sequence.php 5 years ago Session.php 5 years ago SettingsPiwik.php 5 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 5 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 5 years ago Twig.php 5 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 5 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 5 years ago UrlHelper.php 5 years ago Version.php 5 years ago View.php 5 years ago bootstrap.php 5 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 5 years ago
ErrorHandler.php
205 lines
1 <?php
2 /**
3 * Matomo - 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() & $errno)) {
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 $backtrace = \Piwik_ShouldPrintBackTraceWithMessage() ? $e->getTraceAsString() : '';
152 $message = self::getHtmlMessage($errno, $errstr, $errfile, $errline, $backtrace);
153 throw new ErrorException($message, 0, $errno, $errfile, $errline);
154 case E_WARNING:
155 case E_NOTICE:
156 case E_USER_WARNING:
157 case E_USER_NOTICE:
158 case E_STRICT:
159 case E_RECOVERABLE_ERROR:
160 case E_DEPRECATED:
161 case E_USER_DEPRECATED:
162 default:
163 $context = array('trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15));
164 try {
165 StaticContainer::get(LoggerInterface::class)->warning(
166 self::createLogMessage($errno, $errstr, $errfile, $errline),
167 $context
168 );
169 } catch (\Exception $ex) {
170 // ignore (it's possible for this to happen if the StaticContainer hasn't been created yet)
171 }
172
173 break;
174 }
175 }
176
177 private static function createLogMessage($errno, $errstr, $errfile, $errline)
178 {
179 return sprintf(
180 "%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)",
181 $errfile,
182 $errline,
183 ErrorHandler::getErrNoString($errno),
184 $errstr
185 );
186 }
187
188 private static function getHtmlMessage($errno, $errstr, $errfile, $errline, $trace)
189 {
190 $trace = Log::$debugBacktraceForTests ?: $trace;
191
192 $message = ErrorHandler::getErrNoString($errno) . ' - ' . $errstr;
193
194 $html = "<p>There is an error. Please report the message (Matomo " . (class_exists('Piwik\Version') ? Version::VERSION : '') . ")
195 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>";
196 $html .= "<p><strong>{$message}</strong> in <em>{$errfile}</em>";
197 $html .= " on line {$errline}</p>";
198 $html .= "Backtrace:<pre>";
199 $html .= str_replace("\n", "\n", $trace);
200 $html .= "</pre>";
201
202 return $html;
203 }
204 }
205