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