Skip to content

Commit

Permalink
fixes matomo-org#6823 When there are several Super Users in Piwik, ar…
Browse files Browse the repository at this point in the history
…chive.php web cron should accept any of those Super User tokens
  • Loading branch information
mattab committed Dec 7, 2014
1 parent 56b9f7b commit 80bfa67
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
19 changes: 13 additions & 6 deletions core/CronArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class CronArchive
private $segments = array();
private $piwikUrl = false;
private $token_auth = false;
private $validTokenAuths = array();
private $visitsToday = 0;
private $requests = 0;
private $output = '';
Expand Down Expand Up @@ -961,19 +962,25 @@ public function initWebsiteIds()

private function initTokenAuth()
{
$token = '';
$tokens = array();

/**
* @ignore
*/
Piwik::postEvent('CronArchive.getTokenAuth', array(&$token));

$this->token_auth = $token;
Piwik::postEvent('CronArchive.getTokenAuth', array(&$tokens));

$this->validTokenAuths = $tokens;
$this->token_auth = array_shift($tokens);
}

public function getTokenAuth()
public function isTokenAuthSuperUserToken($token_auth)
{
return $this->token_auth;
if(empty($token_auth)
|| strlen($token_auth) != 32) {
return false;
}

return in_array($token_auth, $this->validTokenAuths);
}

private function initPiwikHost($piwikUrl = false)
Expand Down
5 changes: 2 additions & 3 deletions misc/cron/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@
if (!Piwik\Common::isPhpCliMode()) {
$token_auth = Piwik\Common::getRequestVar('token_auth', '', 'string');

if ($token_auth !== $archiver->getTokenAuth()
|| strlen($token_auth) != 32
) {
if (!$archiver->isTokenAuthSuperUserToken($token_auth)) {
var_dump($token_auth);
die('<b>You must specify the Super User token_auth as a parameter to this script, eg. <code>?token_auth=XYZ</code> if you wish to run this script through the browser. </b><br>
However it is recommended to run it <a href="http://piwik.org/docs/setup-auto-archiving/">via cron in the command line</a>, since it can take a long time to run.<br/>
In a shell, execute for example the following to trigger archiving on the local Piwik server:<br/>
Expand Down
8 changes: 3 additions & 5 deletions plugins/UsersManager/UsersManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ public function recordAdminUsersInCache(&$attributes, $idSite)
$attributes['admin_token_auth'] = $tokens;
}

public function getCronArchiveTokenAuth(&$token)
public function getCronArchiveTokenAuth(&$tokens)
{
$model = new Model();
$superUsers = $model->getUsersHavingSuperUserAccess();

if (!empty($superUsers)) {
$superUser = array_shift($superUsers);

$token = $superUser['token_auth'];
foreach($superUsers as $superUser) {
$tokens[] = $superUser['token_auth'];
}
}

Expand Down

0 comments on commit 80bfa67

Please sign in to comment.