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
4 years ago
Http
4 years ago
Intl
4 years ago
Mail
4 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
4 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
4 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
3 years ago
ExceptionHandler.php
4 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
4 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
4 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
210 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\Plugins\CoreAdminHome\CustomLogo; |
| 20 | use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor; |
| 21 | use Psr\Log\LoggerInterface; |
| 22 | |
| 23 | /** |
| 24 | * Contains Piwik's uncaught exception handler. |
| 25 | */ |
| 26 | class ExceptionHandler |
| 27 | { |
| 28 | public static function setUp() |
| 29 | { |
| 30 | set_exception_handler(['Piwik\ExceptionHandler', 'handleException']); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param Exception|\Throwable $exception |
| 35 | */ |
| 36 | public static function handleException($exception) |
| 37 | { |
| 38 | if (Common::isPhpCliMode()) { |
| 39 | self::dieWithCliError($exception); |
| 40 | } |
| 41 | |
| 42 | self::dieWithHtmlErrorPage($exception); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param Exception|\Throwable $exception |
| 47 | */ |
| 48 | public static function dieWithCliError($exception) |
| 49 | { |
| 50 | self::logException($exception); |
| 51 | |
| 52 | $message = $exception->getMessage(); |
| 53 | |
| 54 | if (!method_exists($exception, 'isHtmlMessage') || !$exception->isHtmlMessage()) { |
| 55 | $message = strip_tags(str_replace('<br />', PHP_EOL, $message)); |
| 56 | } |
| 57 | |
| 58 | $message = sprintf( |
| 59 | "Uncaught exception in %s line %d:\n%s\n", |
| 60 | $exception->getFile(), |
| 61 | $exception->getLine(), |
| 62 | ExceptionToTextProcessor::getMessageAndWholeBacktrace($exception) |
| 63 | ); |
| 64 | |
| 65 | echo $message; |
| 66 | |
| 67 | exit(1); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param Exception|\Throwable $exception |
| 72 | */ |
| 73 | public static function dieWithHtmlErrorPage($exception) |
| 74 | { |
| 75 | // Set an appropriate HTTP response code. |
| 76 | switch (true) { |
| 77 | case ( ($exception instanceof \Piwik\Http\HttpCodeException || $exception instanceof \Piwik\Exception\NotSupportedBrowserException) && $exception->getCode() > 0): |
| 78 | // For these exception types, use the exception-provided error code. |
| 79 | http_response_code($exception->getCode()); |
| 80 | break; |
| 81 | case ($exception instanceof \Piwik\Exception\NotYetInstalledException): |
| 82 | http_response_code(404); |
| 83 | break; |
| 84 | default: |
| 85 | http_response_code(500); |
| 86 | } |
| 87 | |
| 88 | // Log the error with an appropriate loglevel. |
| 89 | switch (true) { |
| 90 | case ($exception instanceof \Piwik\Exception\NotSupportedBrowserException): |
| 91 | // These unsupported browsers are really a client-side problem, so log only at DEBUG level. |
| 92 | self::logException($exception, Log::DEBUG); |
| 93 | break; |
| 94 | default: |
| 95 | self::logException($exception); |
| 96 | } |
| 97 | |
| 98 | Common::sendHeader('Content-Type: text/html; charset=utf-8'); |
| 99 | |
| 100 | try { |
| 101 | echo self::getErrorResponse($exception); |
| 102 | } catch (Exception $e) { |
| 103 | // When there are failures while generating the HTML error response itself, |
| 104 | // we simply print out the error message instead. |
| 105 | echo $exception->getMessage(); |
| 106 | } |
| 107 | |
| 108 | exit(1); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param Exception|\Throwable $ex |
| 113 | */ |
| 114 | private static function getErrorResponse($ex) |
| 115 | { |
| 116 | $debugTrace = $ex->getTraceAsString(); |
| 117 | |
| 118 | $message = $ex->getMessage(); |
| 119 | |
| 120 | $isHtmlMessage = method_exists($ex, 'isHtmlMessage') && $ex->isHtmlMessage(); |
| 121 | |
| 122 | if (!$isHtmlMessage && Request::isApiRequest($_GET)) { |
| 123 | $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST)); |
| 124 | $response = new ResponseBuilder($outputFormat); |
| 125 | return $response->getResponseException($ex); |
| 126 | } elseif (!$isHtmlMessage) { |
| 127 | $message = Common::sanitizeInputValue($message); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | $logoHeaderUrl = 'plugins/Morpheus/images/logo.svg'; |
| 132 | $logoFaviconUrl = 'plugins/CoreHome/images/favicon.png'; |
| 133 | try { |
| 134 | $logo = new CustomLogo(); |
| 135 | if ($logo->hasSVGLogo()) { |
| 136 | $logoHeaderUrl = $logo->getSVGLogoUrl(); |
| 137 | } else { |
| 138 | $logoHeaderUrl = $logo->getHeaderLogoUrl(); |
| 139 | } |
| 140 | $logoFaviconUrl = $logo->getPathUserFavicon(); |
| 141 | } catch (Exception $ex) { |
| 142 | try { |
| 143 | Log::debug($ex); |
| 144 | } catch (\Exception $otherEx) { |
| 145 | // DI container may not be setup at this point |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Unsupported browser errors shouldn't be written to the web server log. At DEBUG logging level this error will |
| 150 | // be written to the application log instead |
| 151 | $writeErrorLog = !($ex instanceof \Piwik\Exception\NotSupportedBrowserException); |
| 152 | |
| 153 | $hostname = Url::getRFCValidHostname(); |
| 154 | $hostStr = $hostname ? "[$hostname] " : '- '; |
| 155 | |
| 156 | $result = Piwik_GetErrorMessagePage( |
| 157 | $message, |
| 158 | $debugTrace, |
| 159 | true, |
| 160 | true, |
| 161 | $logoHeaderUrl, |
| 162 | $logoFaviconUrl, |
| 163 | null, |
| 164 | $hostStr, |
| 165 | $writeErrorLog |
| 166 | ); |
| 167 | |
| 168 | try { |
| 169 | /** |
| 170 | * Triggered before a Piwik error page is displayed to the user. |
| 171 | * |
| 172 | * This event can be used to modify the content of the error page that is displayed when |
| 173 | * an exception is caught. |
| 174 | * |
| 175 | * @param string &$result The HTML of the error page. |
| 176 | * @param Exception $ex The Exception displayed in the error page. |
| 177 | */ |
| 178 | Piwik::postEvent('FrontController.modifyErrorPage', [&$result, $ex]); |
| 179 | } catch (ContainerDoesNotExistException $ex) { |
| 180 | // this can happen when an error occurs before the Piwik environment is created |
| 181 | } |
| 182 | |
| 183 | return $result; |
| 184 | } |
| 185 | |
| 186 | private static function logException($exception, $loglevel = Log::ERROR) |
| 187 | { |
| 188 | try { |
| 189 | switch ($loglevel) { |
| 190 | case (Log::DEBUG): |
| 191 | StaticContainer::get(LoggerInterface::class)->debug('Uncaught exception: {exception}', [ |
| 192 | 'exception' => $exception, |
| 193 | 'ignoreInScreenWriter' => true, |
| 194 | ]); |
| 195 | break; |
| 196 | case (Log::ERROR): |
| 197 | default: |
| 198 | StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [ |
| 199 | 'exception' => $exception, |
| 200 | 'ignoreInScreenWriter' => true, |
| 201 | ]); |
| 202 | } |
| 203 | } catch (DependencyException $ex) { |
| 204 | // ignore (occurs if exception is thrown when resolving DI entries) |
| 205 | } catch (ContainerDoesNotExistException $ex) { |
| 206 | // ignore |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 |