Skip to content

Commit

Permalink
Refs matomo-org#3632 Small refactor and support parameter forceLargeW…
Browse files Browse the repository at this point in the history
…indowLookBackForVisitor=1 in tests when replaying tracking logs
  • Loading branch information
mattab committed Mar 13, 2014
1 parent 64e3e8b commit 4328fd8
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions core/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static private function updateTrackerConfig($name, $value)

protected function initRequests($args)
{
$rawData = file_get_contents("php://input");
$rawData = self::getRawBulkRequest();
if (!empty($rawData)) {
$this->usingBulkTracking = strpos($rawData, '"requests"') || strpos($rawData, "'requests'");
if ($this->usingBulkTracking) {
Expand All @@ -177,22 +177,31 @@ protected function initRequests($args)
$this->requests = $args ? $args : (!empty($_GET) || !empty($_POST) ? array($_GET + $_POST) : array());
}

private function authenticateBulkTrackingRequests($rawData)
private static function getRequestsArrayFromBulkRequest($rawData)
{
$rawData = trim($rawData);
$rawData = Common::sanitizeLineBreaks($rawData);

// POST data can be array of string URLs or array of arrays w/ visit info
$jsonData = json_decode($rawData, $assoc = true);

$tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData);

$requests = array();
if (isset($jsonData['requests'])) {
$this->requests = $jsonData['requests'];
$requests = $jsonData['requests'];
}

$tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData);
return array( $requests, $tokenAuth);
}

private function authenticateBulkTrackingRequests($rawData)
{
list($this->requests, $tokenAuth) = $this->getRequestsArrayFromBulkRequest($rawData);

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>");
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>");
}
if (!empty($this->requests)) {

Expand All @@ -213,7 +222,8 @@ private function authenticateBulkTrackingRequests($rawData)

// a Bulk Tracking request that is not authenticated should fail
if (!$requestObj->isAuthenticated()) {
throw new Exception(sprintf("token_auth specified does not have Admin permission for idsite=%s", $requestObj->getIdSite()));
throw new Exception(sprintf("token_auth specified does not have Admin permission for idsite=%s",
$requestObj->getIdSite()));
}

$request = $requestObj;
Expand Down Expand Up @@ -730,7 +740,8 @@ protected function handleTrackingApi(Request $request)
public static function setTestEnvironment($args = null, $requestMethod = null)
{
if (is_null($args)) {
$args = $_GET + $_POST;
$postData = self::getRequestsArrayFromBulkRequest(self::getRawBulkRequest());
$args = $_GET + $postData;
}
if (is_null($requestMethod) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
$requestMethod = $_SERVER['REQUEST_METHOD'];
Expand All @@ -753,7 +764,9 @@ public static function setTestEnvironment($args = null, $requestMethod = null)
}

// Tests using window_look_back_for_visitor
if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1) {
if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1
// also look for this in bulk requests (see fake_logs_replay.log)
|| strpos( $args, '"forceLargeWindowLookBackForVisitor":"1"' ) !== false) {
self::updateTrackerConfig('window_look_back_for_visitor', 2678400);
}

Expand Down Expand Up @@ -866,4 +879,12 @@ protected function runScheduledTasksIfAllowed($isAuthenticated)
$this->exitWithException($e);
}
}

/**
* @return string
*/
protected static function getRawBulkRequest()
{
return file_get_contents("php://input");
}
}

0 comments on commit 4328fd8

Please sign in to comment.