PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
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 SupportedBrowser.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
220 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 private static $lastError = '';
23
24 /**
25 * Fatal errors in PHP do not leave behind backtraces, which can make it impossible to determine
26 * the exact cause of one. We can, however, save a partial stack trace by remembering certain execution
27 * points. This method and popFatalErrorBreadcrumb() are used for that purpose.
28 *
29 * To use this method, surround a function call w/ pushFatalErrorBreadcrumb() & popFatalErrorBreadcrumb()
30 * like so:
31 *
32 * public function theMethodIWantToAppearInFatalErrorStackTraces()
33 * {
34 * try {
35 * ErrorHandler::pushFatalErrorBreadcrumb(static::class);
36 *
37 * // ...
38 * } finally {
39 * ErrorHandler::popFatalErrorBreadcrumb();
40 * }
41 * }
42 *
43 * If a fatal error occurs, theMethodIWantToAppearInFatalErrorStackTraces will appear in the stack trace,
44 * if PIWIK_PRINT_ERROR_BACKTRACE is true.
45 */
46 public static function pushFatalErrorBreadcrumb($className = null, $importantArgs = null)
47 {
48 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit = 2);
49 $backtrace[1]['class'] = $className; // knowing the derived class name is far more useful
50 $backtrace[1]['args'] = empty($importantArgs) ? [] : array_map('json_encode', $importantArgs);
51 array_unshift(self::$fatalErrorStackTrace, $backtrace[1]);
52 }
53
54 public static function popFatalErrorBreadcrumb()
55 {
56 array_shift(self::$fatalErrorStackTrace);
57 }
58
59 public static function getFatalErrorPartialBacktrace()
60 {
61 $result = '';
62 foreach (self::$fatalErrorStackTrace as $index => $entry) {
63 $function = $entry['function'];
64 if (!empty($entry['class'])) {
65 $function = $entry['class'] . $entry['type'] . $function;
66 }
67
68 $args = '';
69 if (!empty($entry['args'])) {
70 $isFirst = true;
71 foreach ($entry['args'] as $name => $value) {
72 if ($isFirst) {
73 $isFirst = false;
74 } else {
75 $args .= ', ';
76 }
77 $args .= $name . '=' . $value;
78 }
79 }
80
81 $result .= sprintf("#%s %s(%s): %s(%s)\n", $index, $entry['file'], $entry['line'], $function, $args);
82 }
83 return $result;
84 }
85
86 /**
87 * Returns a string description of a PHP error number.
88 *
89 * @param int $errno `E_ERROR`, `E_WARNING`, `E_PARSE`, etc.
90 * @return string
91 */
92 public static function getErrNoString($errno)
93 {
94 switch ($errno) {
95 case E_ERROR:
96 return "Error";
97 case E_WARNING:
98 return "Warning";
99 case E_PARSE:
100 return "Parse Error";
101 case E_NOTICE:
102 return "Notice";
103 case E_CORE_ERROR:
104 return "Core Error";
105 case E_CORE_WARNING:
106 return "Core Warning";
107 case E_COMPILE_ERROR:
108 return "Compile Error";
109 case E_COMPILE_WARNING:
110 return "Compile Warning";
111 case E_USER_ERROR:
112 return "User Error";
113 case E_USER_WARNING:
114 return "User Warning";
115 case E_USER_NOTICE:
116 return "User Notice";
117 case E_STRICT:
118 return "Strict Notice";
119 case E_RECOVERABLE_ERROR:
120 return "Recoverable Error";
121 case E_DEPRECATED:
122 return "Deprecated";
123 case E_USER_DEPRECATED:
124 return "User Deprecated";
125 default:
126 return "Unknown error ($errno)";
127 }
128 }
129
130 public static function registerErrorHandler()
131 {
132 set_error_handler(array('Piwik\ErrorHandler', 'errorHandler'));
133 }
134
135 public static function errorHandler($errno, $errstr, $errfile, $errline)
136 {
137 self::$lastError = self::createLogMessage($errno, $errstr, $errfile, $errline);
138
139 // if the error has been suppressed by the @ we don't handle the error
140 if (!(error_reporting() & $errno)) {
141 return;
142 }
143
144 switch ($errno) {
145 case E_ERROR:
146 case E_PARSE:
147 case E_CORE_ERROR:
148 case E_CORE_WARNING:
149 case E_COMPILE_ERROR:
150 case E_COMPILE_WARNING:
151 case E_USER_ERROR:
152 Common::sendResponseCode(500);
153 // Convert the error to an exception with an HTML message
154 $e = new \Exception();
155 $backtrace = \Piwik_ShouldPrintBackTraceWithMessage() ? $e->getTraceAsString() : '';
156 $message = self::getHtmlMessage($errno, $errstr, $errfile, $errline, $backtrace);
157 throw new ErrorException($message, 0, $errno, $errfile, $errline);
158 case E_WARNING:
159 case E_NOTICE:
160 case E_USER_WARNING:
161 case E_USER_NOTICE:
162 case E_STRICT:
163 case E_RECOVERABLE_ERROR:
164 case E_DEPRECATED:
165 case E_USER_DEPRECATED:
166 default:
167 $context = array('trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15));
168 try {
169 StaticContainer::get(LoggerInterface::class)->warning(
170 self::createLogMessage($errno, $errstr, $errfile, $errline),
171 $context
172 );
173 } catch (\Exception $ex) {
174 // ignore (it's possible for this to happen if the StaticContainer hasn't been created yet)
175 }
176
177 break;
178 }
179 }
180
181 public static function getLastError()
182 {
183 $lastError = error_get_last();
184
185 if (!empty($lastError['message'])) {
186 return $lastError['message'];
187 }
188
189 return self::$lastError;
190 }
191
192 private static function createLogMessage($errno, $errstr, $errfile, $errline)
193 {
194 return sprintf(
195 "%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)",
196 $errfile,
197 $errline,
198 ErrorHandler::getErrNoString($errno),
199 $errstr
200 );
201 }
202
203 private static function getHtmlMessage($errno, $errstr, $errfile, $errline, $trace)
204 {
205 $trace = Log::$debugBacktraceForTests ?: $trace;
206
207 $message = ErrorHandler::getErrNoString($errno) . ' - ' . $errstr;
208
209 $html = "<p>There is an error. Please report the message (Matomo " . (class_exists('Piwik\Version') ? Version::VERSION : '') . ")
210 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>";
211 $html .= "<p><strong>{$message}</strong> in <em>{$errfile}</em>";
212 $html .= " on line {$errline}</p>";
213 $html .= "Backtrace:<pre>";
214 $html .= str_replace("\n", "\n", $trace);
215 $html .= "</pre>";
216
217 return $html;
218 }
219 }
220