/** * Echos an error message & other information, then exits. * * @param Tracker $tracker * @param Exception $e * @param int $statusCode eg 500 */ public function outputException(Tracker $tracker, Exception $e, $statusCode) { Common::sendResponseCode($statusCode); $this->logExceptionToErrorLog($e); $result = $this->formatException($tracker, $e); echo json_encode($result); }
public function dispatch() { $module = Common::getRequestVar('module', '', 'string'); $action = Common::getRequestVar('action', '', 'string'); if ($module == 'CoreUpdater' || $module == 'Proxy' || $module == 'Installation' || $module == 'LanguagesManager' && $action == 'saveLanguage') { return; } $updater = new PiwikCoreUpdater(); $updates = $updater->getComponentsWithNewVersion(array('core' => Version::VERSION)); if (!empty($updates)) { Filesystem::deleteAllCacheOnUpdate(); } if ($updater->getComponentUpdates() !== null) { if (FrontController::shouldRethrowException()) { throw new Exception("Piwik and/or some plugins have been upgraded to a new version. \n" . "--> Please run the update process first. See documentation: http://piwik.org/docs/update/ \n"); } elseif ($module === 'API') { $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST)); $response = new ResponseBuilder($outputFormat); $e = new Exception('Database Upgrade Required. Your Piwik database is out-of-date, and must be upgraded before you can continue.'); echo $response->getResponseException($e); Common::sendResponseCode(503); exit; } else { Piwik::redirectToModule('CoreUpdater'); } } }
public static function errorHandler($errno, $errstr, $errfile, $errline) { // if the error has been suppressed by the @ we don't handle the error if (error_reporting() == 0) { return; } switch ($errno) { case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_CORE_WARNING: case E_COMPILE_ERROR: case E_COMPILE_WARNING: case E_USER_ERROR: Common::sendResponseCode(500); // Convert the error to an exception with an HTML message $e = new \Exception(); $message = self::getHtmlMessage($errno, $errstr, $errfile, $errline, $e->getTraceAsString()); throw new ErrorException($message, 0, $errno, $errfile, $errline); break; case E_WARNING: case E_NOTICE: case E_USER_WARNING: case E_USER_NOTICE: case E_STRICT: case E_RECOVERABLE_ERROR: case E_DEPRECATED: case E_USER_DEPRECATED: default: Log::warning(self::createLogMessage($errno, $errstr, $errfile, $errline)); break; } }
public function displayDbConnectionMessage($exception = null) { Common::sendResponseCode(500); $errorMessage = $exception->getMessage(); if (Request::isApiRequest($_GET)) { $ex = new DatabaseConnectionFailedException($errorMessage); throw $ex; } $view = new PiwikView("@Installation/cannotConnectToDb"); $view->exceptionMessage = $errorMessage; $ex = new DatabaseConnectionFailedException($view->render()); $ex->setIsHtmlMessage(); throw $ex; }
protected function handleMaintenanceMode() { if (Config::getInstance()->General['maintenance_mode'] != 1 || Common::isPhpCliMode()) { return; } Common::sendResponseCode(503); $logoUrl = null; $faviconUrl = null; try { $logo = new CustomLogo(); $logoUrl = $logo->getHeaderLogoUrl(); $faviconUrl = $logo->getPathUserFavicon(); } catch (Exception $ex) { } $logoUrl = $logoUrl ?: 'plugins/Morpheus/images/logo-header.png'; $faviconUrl = $faviconUrl ?: 'plugins/CoreHome/images/favicon.ico'; $page = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/maintenance.tpl'); $page = str_replace('%logoUrl%', $logoUrl, $page); $page = str_replace('%faviconUrl%', $faviconUrl, $page); $page = str_replace('%piwikTitle%', Piwik::getRandomTitle(), $page); echo $page; exit; }
private function outputApiResponse(Tracker $tracker) { if ($tracker->isDebugModeEnabled()) { return; } if ($this->hasAlreadyPrintedOutput()) { return; } $request = $_GET + $_POST; if (array_key_exists('send_image', $request) && $request['send_image'] === '0') { Common::sendResponseCode(204); return; } $this->outputTransparentGif(); }
private static function redirectToUrlNoExit($url) { if (UrlHelper::isLookLikeUrl($url) || strpos($url, 'index.php') === 0) { Common::sendResponseCode(302); Common::sendHeader("Location: {$url}"); } else { echo "Invalid URL to redirect to."; } if (Common::isPhpCliMode()) { throw new Exception("If you were using a browser, Piwik would redirect you to this URL: {$url} \n\n"); } }
private function handleFatalErrors() { register_shutdown_function(function () { $lastError = error_get_last(); if (!empty($lastError) && $lastError['type'] == E_ERROR) { Common::sendResponseCode(500); } }); }
public function download() { Piwik::checkUserHasSuperUserAccess(); $this->dieIfPluginsAdminIsDisabled(); $pluginName = new PluginName(); $pluginName = $pluginName->getPluginName(); Nonce::checkNonce($pluginName); $filename = $pluginName . '.zip'; try { $pathToPlugin = $this->marketplaceApi->download($pluginName); ProxyHttp::serverStaticFile($pathToPlugin, 'application/zip', $expire = 0, $start = false, $end = false, $filename); } catch (Exception $e) { Common::sendResponseCode(500); Log::warning('Could not download file . ' . $e->getMessage()); } if (!empty($pathToPlugin)) { Filesystem::deleteFileIfExists($pathToPlugin); } }