API
3 years ago
Access
3 years ago
Application
4 years ago
Archive
3 years ago
ArchiveProcessor
3 years ago
Archiver
5 years ago
AssetManager
3 years ago
Auth
3 years ago
Category
5 years ago
Changes
4 years ago
CliMulti
4 years ago
Columns
3 years ago
Concurrency
3 years ago
Config
4 years ago
Container
4 years ago
CronArchive
3 years ago
DataAccess
3 years ago
DataFiles
5 years ago
DataTable
3 years ago
Db
3 years ago
DeviceDetector
3 years ago
Email
5 years ago
Exception
3 years ago
Http
4 years ago
Intl
4 years ago
Mail
3 years ago
Measurable
5 years ago
Menu
3 years ago
Metrics
4 years ago
Notification
4 years ago
Period
4 years ago
Plugin
3 years ago
ProfessionalServices
4 years ago
Report
5 years ago
ReportRenderer
3 years ago
Scheduler
4 years ago
Segment
3 years ago
Session
4 years ago
Settings
3 years ago
Tracker
3 years ago
Translation
4 years ago
UpdateCheck
5 years ago
Updater
3 years ago
Updates
3 years ago
Validators
4 years ago
View
4 years ago
ViewDataTable
3 years ago
Visualization
4 years ago
Widget
5 years ago
.htaccess
6 years ago
Access.php
4 years ago
Archive.php
4 years ago
ArchiveProcessor.php
4 years ago
AssetManager.php
4 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
4 years ago
Common.php
3 years ago
Config.php
3 years ago
Console.php
3 years ago
Context.php
5 years ago
Cookie.php
3 years ago
CronArchive.php
3 years ago
DataArray.php
4 years ago
DataTable.php
3 years ago
Date.php
4 years ago
Db.php
4 years ago
DbHelper.php
3 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
3 years ago
ExceptionHandler.php
3 years ago
FileIntegrity.php
5 years ago
Filechecks.php
4 years ago
Filesystem.php
3 years ago
FrontController.php
3 years ago
Http.php
3 years ago
IP.php
4 years ago
Log.php
4 years ago
LogDeleter.php
4 years ago
Mail.php
4 years ago
Metrics.php
3 years ago
NoAccessException.php
5 years ago
Nonce.php
3 years ago
Notification.php
5 years ago
NumberFormatter.php
4 years ago
Option.php
4 years ago
Period.php
5 years ago
Piwik.php
3 years ago
Plugin.php
4 years ago
Profiler.php
4 years ago
ProxyHeaders.php
5 years ago
ProxyHttp.php
3 years ago
QuickForm2.php
5 years ago
RankingQuery.php
3 years ago
ReportRenderer.php
4 years ago
Segment.php
3 years ago
Sequence.php
5 years ago
Session.php
3 years ago
SettingsPiwik.php
3 years ago
SettingsServer.php
5 years ago
Singleton.php
5 years ago
Site.php
3 years ago
SiteContentDetector.php
3 years ago
SupportedBrowser.php
3 years ago
TCPDF.php
5 years ago
Theme.php
5 years ago
Timer.php
5 years ago
Tracker.php
4 years ago
Twig.php
4 years ago
Unzip.php
5 years ago
UpdateCheck.php
5 years ago
Updater.php
4 years ago
UpdaterErrorException.php
5 years ago
Updates.php
5 years ago
Url.php
3 years ago
UrlHelper.php
3 years ago
Version.php
3 years ago
View.php
3 years ago
bootstrap.php
3 years ago
dispatch.php
5 years ago
testMinimumPhpVersion.php
3 years ago
ExceptionHandler.php
220 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | namespace Piwik; |
| 12 | |
| 13 | use DI\DependencyException; |
| 14 | use Exception; |
| 15 | use Piwik\API\Request; |
| 16 | use Piwik\API\ResponseBuilder; |
| 17 | use Piwik\Container\ContainerDoesNotExistException; |
| 18 | use Piwik\Container\StaticContainer; |
| 19 | use Piwik\Exception\IRedirectException; |
| 20 | use Piwik\Plugins\CoreAdminHome\CustomLogo; |
| 21 | use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor; |
| 22 | use Psr\Log\LoggerInterface; |
| 23 | |
| 24 | /** |
| 25 | * Contains Piwik's uncaught exception handler. |
| 26 | */ |
| 27 | class ExceptionHandler |
| 28 | { |
| 29 | public static function setUp() |
| 30 | { |
| 31 | set_exception_handler(['Piwik\ExceptionHandler', 'handleException']); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param Exception|\Throwable $exception |
| 36 | */ |
| 37 | public static function handleException($exception) |
| 38 | { |
| 39 | if (Common::isPhpCliMode()) { |
| 40 | self::dieWithCliError($exception); |
| 41 | } |
| 42 | |
| 43 | self::dieWithHtmlErrorPage($exception); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param Exception|\Throwable $exception |
| 48 | */ |
| 49 | public static function dieWithCliError($exception) |
| 50 | { |
| 51 | self::logException($exception); |
| 52 | |
| 53 | $message = $exception->getMessage(); |
| 54 | |
| 55 | if (!method_exists($exception, 'isHtmlMessage') || !$exception->isHtmlMessage()) { |
| 56 | $message = strip_tags(str_replace('<br />', PHP_EOL, $message)); |
| 57 | } |
| 58 | |
| 59 | $message = sprintf( |
| 60 | "Uncaught exception in %s line %d:\n%s\n", |
| 61 | $exception->getFile(), |
| 62 | $exception->getLine(), |
| 63 | ExceptionToTextProcessor::getMessageAndWholeBacktrace($exception) |
| 64 | ); |
| 65 | |
| 66 | echo $message; |
| 67 | |
| 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param Exception|\Throwable $exception |
| 73 | */ |
| 74 | public static function dieWithHtmlErrorPage($exception) |
| 75 | { |
| 76 | // Set an appropriate HTTP response code. |
| 77 | switch (true) { |
| 78 | case ( ($exception instanceof \Piwik\Http\HttpCodeException || $exception instanceof \Piwik\Exception\NotSupportedBrowserException) && $exception->getCode() > 0): |
| 79 | // For these exception types, use the exception-provided error code. |
| 80 | http_response_code($exception->getCode()); |
| 81 | break; |
| 82 | case ($exception instanceof \Piwik\Exception\NotYetInstalledException): |
| 83 | http_response_code(404); |
| 84 | break; |
| 85 | default: |
| 86 | http_response_code(500); |
| 87 | } |
| 88 | |
| 89 | // Log the error with an appropriate loglevel. |
| 90 | switch (true) { |
| 91 | case ($exception instanceof \Piwik\Exception\NotSupportedBrowserException): |
| 92 | // These unsupported browsers are really a client-side problem, so log only at DEBUG level. |
| 93 | self::logException($exception, Log::DEBUG); |
| 94 | break; |
| 95 | default: |
| 96 | self::logException($exception); |
| 97 | } |
| 98 | |
| 99 | Common::sendHeader('Content-Type: text/html; charset=utf-8'); |
| 100 | |
| 101 | try { |
| 102 | echo self::getErrorResponse($exception); |
| 103 | } catch (Exception $e) { |
| 104 | // When there are failures while generating the HTML error response itself, |
| 105 | // we simply print out the error message instead. |
| 106 | echo $exception->getMessage(); |
| 107 | } |
| 108 | |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param Exception|\Throwable $ex |
| 114 | */ |
| 115 | private static function getErrorResponse($ex) |
| 116 | { |
| 117 | $debugTrace = $ex->getTraceAsString(); |
| 118 | |
| 119 | $message = $ex->getMessage(); |
| 120 | |
| 121 | $isHtmlMessage = method_exists($ex, 'isHtmlMessage') && $ex->isHtmlMessage(); |
| 122 | |
| 123 | if (!$isHtmlMessage && Request::isApiRequest($_GET)) { |
| 124 | $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST)); |
| 125 | $response = new ResponseBuilder($outputFormat); |
| 126 | return $response->getResponseException($ex); |
| 127 | } elseif (!$isHtmlMessage) { |
| 128 | $message = Common::sanitizeInputValue($message); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | $logoHeaderUrl = 'plugins/Morpheus/images/logo.svg'; |
| 133 | $logoFaviconUrl = 'plugins/CoreHome/images/favicon.png'; |
| 134 | try { |
| 135 | $logo = new CustomLogo(); |
| 136 | if ($logo->hasSVGLogo()) { |
| 137 | $logoHeaderUrl = $logo->getSVGLogoUrl(); |
| 138 | } else { |
| 139 | $logoHeaderUrl = $logo->getHeaderLogoUrl(); |
| 140 | } |
| 141 | $logoFaviconUrl = $logo->getPathUserFavicon(); |
| 142 | } catch (Exception $ex) { |
| 143 | try { |
| 144 | Log::debug($ex); |
| 145 | } catch (\Exception $otherEx) { |
| 146 | // DI container may not be setup at this point |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Unsupported browser errors shouldn't be written to the web server log. At DEBUG logging level this error will |
| 151 | // be written to the application log instead |
| 152 | $writeErrorLog = !($ex instanceof \Piwik\Exception\NotSupportedBrowserException); |
| 153 | |
| 154 | $redirectUrl = null; |
| 155 | $countdownToRedirect = null; |
| 156 | if ($ex instanceof IRedirectException) { |
| 157 | $redirectUrl = $ex->getRedirectionUrl(); |
| 158 | $countdownToRedirect = $ex->getCountdown(); |
| 159 | } |
| 160 | |
| 161 | $hostname = Url::getRFCValidHostname(); |
| 162 | $hostStr = $hostname ? "[$hostname] " : '- '; |
| 163 | |
| 164 | $result = Piwik_GetErrorMessagePage( |
| 165 | $message, |
| 166 | $debugTrace, |
| 167 | true, |
| 168 | true, |
| 169 | $logoHeaderUrl, |
| 170 | $logoFaviconUrl, |
| 171 | null, |
| 172 | $hostStr, |
| 173 | $writeErrorLog, |
| 174 | $redirectUrl, |
| 175 | $countdownToRedirect |
| 176 | ); |
| 177 | |
| 178 | try { |
| 179 | /** |
| 180 | * Triggered before a Piwik error page is displayed to the user. |
| 181 | * |
| 182 | * This event can be used to modify the content of the error page that is displayed when |
| 183 | * an exception is caught. |
| 184 | * |
| 185 | * @param string &$result The HTML of the error page. |
| 186 | * @param Exception $ex The Exception displayed in the error page. |
| 187 | */ |
| 188 | Piwik::postEvent('FrontController.modifyErrorPage', [&$result, $ex]); |
| 189 | } catch (ContainerDoesNotExistException $ex) { |
| 190 | // this can happen when an error occurs before the Piwik environment is created |
| 191 | } |
| 192 | |
| 193 | return $result; |
| 194 | } |
| 195 | |
| 196 | private static function logException($exception, $loglevel = Log::ERROR) |
| 197 | { |
| 198 | try { |
| 199 | switch ($loglevel) { |
| 200 | case (Log::DEBUG): |
| 201 | StaticContainer::get(LoggerInterface::class)->debug('Uncaught exception: {exception}', [ |
| 202 | 'exception' => $exception, |
| 203 | 'ignoreInScreenWriter' => true, |
| 204 | ]); |
| 205 | break; |
| 206 | case (Log::ERROR): |
| 207 | default: |
| 208 | StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [ |
| 209 | 'exception' => $exception, |
| 210 | 'ignoreInScreenWriter' => true, |
| 211 | ]); |
| 212 | } |
| 213 | } catch (DependencyException $ex) { |
| 214 | // ignore (occurs if exception is thrown when resolving DI entries) |
| 215 | } catch (ContainerDoesNotExistException $ex) { |
| 216 | // ignore |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 |