Skip to content

Commit

Permalink
Properly catch exception on failed authentication for bulk import
Browse files Browse the repository at this point in the history
Set correct header for application/json

refs #4141
  • Loading branch information
halfdan committed Sep 8, 2013
1 parent 1a6b565 commit f985d16
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions core/Tracker.php
Expand Up @@ -181,7 +181,7 @@ private function initBulkTrackingRequests($rawData)
}
$tokenAuth = Common::getRequestVar('token_auth', false, null, $jsonData);
if (empty($tokenAuth)) {
throw new Exception(" token_auth must be specified when using Bulk Tracking Import. See <a href='http://piwik.org/docs/tracking-api/reference/'>Tracking Doc</a>");
throw new Exception("token_auth must be specified when using Bulk Tracking Import. See <a href='http://piwik.org/docs/tracking-api/reference/'>Tracking Doc</a>");
}
if (!empty($this->requests)) {
$idSitesForAuthentication = array();
Expand All @@ -207,7 +207,7 @@ private function initBulkTrackingRequests($rawData)
foreach($idSitesForAuthentication as $idSiteForAuthentication) {
// a Bulk Tracking request that is not authenticated should fail
if (!Request::authenticateSuperUserOrAdmin($tokenAuth, $idSiteForAuthentication)) {
throw new Exception(" token_auth specified is not valid for site " . intval($idSiteForAuthentication));
throw new Exception("token_auth specified is not valid for site " . intval($idSiteForAuthentication));
}
}
}
Expand All @@ -221,7 +221,11 @@ private function initBulkTrackingRequests($rawData)
*/
public function main($args = null)
{
$tokenAuth = $this->initRequests($args);
try {
$tokenAuth = $this->initRequests($args);
} catch (Exception $ex) {
$this->exitWithException($ex, true);
}

if (!empty($this->requests)) {
foreach ($this->requests as $params) {
Expand Down Expand Up @@ -389,9 +393,11 @@ protected function exitWithException($e, $authenticated = false)
if ((isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) || $authenticated) {
$result['error'] = $this->getMessageFromException($e);
}
$this->sendHeader('Content-Type: application/json');
echo Common::json_encode($result);
exit;
}

if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) {
Common::sendHeader('Content-Type: text/html; charset=utf-8');
$trailer = '<span style="color: #888888">Backtrace:<br /><pre>' . $e->getTraceAsString() . '</pre></span>';
Expand Down Expand Up @@ -607,9 +613,7 @@ protected function loadTrackerPlugins(Request $request)
{
// Adding &dp=1 will disable the provider plugin, if token_auth is used (used to speed up bulk imports)
$disableProvider = $request->getParam('dp');
if (!empty($disableProvider)
&& $request->isAuthenticated()
) {
if (!empty($disableProvider) && $request->isAuthenticated()) {
Tracker::setPluginsNotToLoad(array('Provider'));
}

Expand Down

0 comments on commit f985d16

Please sign in to comment.