Skip to content

Commit

Permalink
refs #4996 make sure redirect works even if mysql is down or any othe…
Browse files Browse the repository at this point in the history
…r error occurs
  • Loading branch information
tsteur committed Sep 2, 2014
1 parent 7d27436 commit 0973939
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions core/Tracker.php
Expand Up @@ -172,7 +172,7 @@ private static function getRequestsArrayFromBulkRequest($rawData)
$requests = $jsonData['requests'];
}

return array( $requests, $tokenAuth);
return array($requests, $tokenAuth);
}

private function isBulkTrackingRequireTokenAuth()
Expand All @@ -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 <a href='http://developer.piwik.org/api-reference/tracking-api'>Tracking Doc</a>");
}
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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()
Expand All @@ -283,23 +280,23 @@ protected function getOutputBuffer()
protected function beginTransaction()
{
$this->transactionId = null;
if(!$this->shouldUseTransactions()) {
if (!$this->shouldUseTransactions()) {
return;
}
$this->transactionId = self::getDatabase()->beginTransaction();
}

protected function commitTransaction()
{
if(empty($this->transactionId)) {
if (empty($this->transactionId)) {
return;
}
self::getDatabase()->commit($this->transactionId);
}

protected function rollbackTransaction()
{
if(empty($this->transactionId)) {
if (empty($this->transactionId)) {
return;
}
self::getDatabase()->rollback($this->transactionId);
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}

}

0 comments on commit 0973939

Please sign in to comment.