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