From 097393972e41a5c8a429b6b625cd2bf0b16b178d Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Tue, 2 Sep 2014 08:55:13 +0200 Subject: [PATCH] refs #4996 make sure redirect works even if mysql is down or any other error occurs --- core/Tracker.php | 60 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/core/Tracker.php b/core/Tracker.php index ae289d7be52..3b6e5ba178a 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -172,7 +172,7 @@ private static function getRequestsArrayFromBulkRequest($rawData) $requests = $jsonData['requests']; } - return array( $requests, $tokenAuth); + return array($requests, $tokenAuth); } private function isBulkTrackingRequireTokenAuth() @@ -185,8 +185,8 @@ private function authenticateBulkTrackingRequests($rawData) list($this->requests, $tokenAuth) = $this->getRequestsArrayFromBulkRequest($rawData); $bulkTrackingRequireTokenAuth = $this->isBulkTrackingRequireTokenAuth(); - if($bulkTrackingRequireTokenAuth) { - if(empty($tokenAuth)) { + if ($bulkTrackingRequireTokenAuth) { + if (empty($tokenAuth)) { throw new Exception("token_auth must be specified when using Bulk Tracking Import. " . " See Tracking Doc"); } @@ -208,8 +208,9 @@ private function authenticateBulkTrackingRequests($rawData) $requestObj = new Request($request, $tokenAuth); $this->loadTrackerPlugins($requestObj); - if($bulkTrackingRequireTokenAuth - && !$requestObj->isAuthenticated()) { + if ($bulkTrackingRequireTokenAuth + && !$requestObj->isAuthenticated() + ) { throw new Exception(sprintf("token_auth specified does not have Admin permission for idsite=%s", $requestObj->getIdSite())); } $request = $requestObj; @@ -243,7 +244,7 @@ public function main($args = null) } $this->runScheduledTasksIfAllowed($isAuthenticated); $this->commitTransaction(); - } catch(DbException $e) { + } catch (DbException $e) { Common::printDebug($e->getMessage()); $this->rollbackTransaction(); } @@ -258,11 +259,7 @@ public function main($args = null) $this->flushOutputBuffer(); - $redirectUrl = Common::getRequestVar('redirecturl', false, 'string'); - if ($redirectUrl) { - // TODO only redirecti if domain is trusted in config? - header('Location: ' . $redirectUrl); - } + $this->performRedirectToUrlIfSet(); } protected function initOutputBuffer() @@ -283,7 +280,7 @@ protected function getOutputBuffer() protected function beginTransaction() { $this->transactionId = null; - if(!$this->shouldUseTransactions()) { + if (!$this->shouldUseTransactions()) { return; } $this->transactionId = self::getDatabase()->beginTransaction(); @@ -291,7 +288,7 @@ protected function beginTransaction() protected function commitTransaction() { - if(empty($this->transactionId)) { + if (empty($this->transactionId)) { return; } self::getDatabase()->commit($this->transactionId); @@ -299,7 +296,7 @@ protected function commitTransaction() protected function rollbackTransaction() { - if(empty($this->transactionId)) { + if (empty($this->transactionId)) { return; } self::getDatabase()->rollback($this->transactionId); @@ -319,7 +316,7 @@ protected function shouldUseTransactions() */ protected function isTransactionSupported() { - return (bool) Config::getInstance()->Tracker['bulk_requests_use_transaction']; + return (bool)Config::getInstance()->Tracker['bulk_requests_use_transaction']; } protected function shouldRunScheduledTasks() @@ -436,13 +433,18 @@ public static function initCorePiwikInTrackerMode() */ protected function exitWithException($e, $authenticated = false) { + if ($this->hasRedirectUrl()) { + $this->performRedirectToUrlIfSet(); + exit; + } + Common::sendHeader('HTTP/1.1 500 Internal Server Error'); error_log(sprintf("Error in Piwik (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e)))); if ($this->usingBulkTracking) { // when doing bulk tracking we return JSON so the caller will know how many succeeded $result = array( - 'status' => 'error', + 'status' => 'error', 'tracked' => $this->countOfLoggedRequests ); // send error when in debug mode or when authenticated (which happens when doing log importing, @@ -505,7 +507,7 @@ protected function end() { if ($this->usingBulkTracking) { $result = array( - 'status' => 'success', + 'status' => 'success', 'tracked' => $this->countOfLoggedRequests ); Common::sendHeader('Content-Type: application/json'); @@ -799,7 +801,8 @@ public static function setTestEnvironment($args = null, $requestMethod = null) // Tests using window_look_back_for_visitor if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1 // also look for this in bulk requests (see fake_logs_replay.log) - || strpos( json_encode($args, true), '"forceLargeWindowLookBackForVisitor":"1"' ) !== false) { + || strpos(json_encode($args, true), '"forceLargeWindowLookBackForVisitor":"1"') !== false + ) { self::updateTrackerConfig('window_look_back_for_visitor', 2678400); } @@ -920,4 +923,25 @@ protected static function getRawBulkRequest() return file_get_contents("php://input"); } + private function getRedirectUrl() + { + // TODO only redirecti if domain is trusted in config? + return Common::getRequestVar('redirecturl', false, 'string'); + } + + private function hasRedirectUrl() + { + $redirectUrl = $this->getRedirectUrl(); + + return !empty($redirectUrl); + } + + private function performRedirectToUrlIfSet() + { + if ($this->hasRedirectUrl()) { + $redirectUrl = $this->getRedirectUrl(); + header('Location: ' . $redirectUrl); + } + } + }