Skip to content

Commit

Permalink
Fixed issue #06300 : Token can be duplicated under some condition (Fo…
Browse files Browse the repository at this point in the history
…r Yii : dummy token)

Dev: Fill the token table with existing token for dummytokens
  • Loading branch information
Shnoulle committed Jul 5, 2012
1 parent eab7c22 commit 8e3bd1a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
59 changes: 47 additions & 12 deletions application/controllers/admin/tokens.php
Expand Up @@ -822,6 +822,17 @@ function addDummies($iSurveyId, $subaction = '')
$amount = sanitize_int(Yii::app()->request->getPost('amount'));
$tokenlength = sanitize_int(Yii::app()->request->getPost('tokenlen'));

// Fill an array with all existing tokens
$criteria = Tokens_dynamic::model($iSurveyId)->getDbCriteria();
$criteria->select = 'token';
$ntresult = Tokens_dynamic::model($iSurveyId)->findAllAsArray($criteria); //Use AsArray to skip active record creation
$existingtokens=array();
foreach ($ntresult as $tkrow)
{
$existingtokens[] = $tkrow['token'];
}
$invalidtokencount=0;
$newDummyToken=0;
for ($i = 0; $i < $amount; $i++)
{
$aDataToInsert = $aData;
Expand All @@ -830,25 +841,49 @@ function addDummies($iSurveyId, $subaction = '')
$aDataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $i, $aDataToInsert['email']);

$isvalidtoken = false;
while ($isvalidtoken == false)
while ($isvalidtoken == false && $invalidtokencount<50)
{
$newtoken = randomChars($tokenlength);
if (!isset($existingtokens[$newtoken]))
if (!in_array($newtoken, $existingtokens))
{
$isvalidtoken = true;
$existingtokens[$newtoken] = null;
$existingtokens[] = $newtoken;
$invalidtokencount=0;
}
else
{
$invalidtokencount ++;
}
}

$aDataToInsert['token'] = $newtoken;
Tokens_dynamic::insertToken($iSurveyId, $aDataToInsert);
if(!$invalidtokencount)
{
$aDataToInsert['token'] = $newtoken;
Tokens_dynamic::insertToken($iSurveyId, $aDataToInsert);
$newDummyToken ++;
}

}

$this->_renderWrappedTemplate('token', array('message' => array(
'title' => $clang->gT("Success"),
'message' => $clang->gT("New dummy tokens were added.") . "<br /><br />\n<input type='button' value='"
. $clang->gT("Display tokens") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/browse/surveyid/$iSurveyId") . "', '_top')\" />\n"
) ));
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;
if(!$invalidtokencount)
{
$aData['success'] = false;
$message=array('title' => $clang->gT("Success"),
'message' => $clang->gT("New dummy tokens were added.") . "<br /><br />\n<input type='button' value='"
. $clang->gT("Display tokens") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/browse/surveyid/$iSurveyId") . "', '_top')\" />\n"
);
}
else
{
$aData['success'] = true;
$message= array(
'title' => $clang->gT("Failed"),
'message' => sprintf($clang->gT("Only %s new dummy tokens were added."),$newDummyToken) . "<br /><br />\n<input type='button' value='"
. $clang->gT("Display tokens") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/browse/surveyid/$iSurveyId") . "', '_top')\" />\n"
);
}
$this->_renderWrappedTemplate('token', array('tokenbar','message' => $message),$aData);

}
else
{
Expand Down
21 changes: 15 additions & 6 deletions application/models/Tokens_dynamic.php
Expand Up @@ -135,7 +135,6 @@ function selectEmptyTokens($iSurveyID)
{
return Yii::app()->db->createCommand("SELECT tid FROM {{tokens_{$iSurveyID}}} WHERE token IS NULL OR token=''")->queryAll();
}

/**
* Creates tokens for all token records that have empty token fields and returns the number
* of tokens created
Expand Down Expand Up @@ -163,7 +162,7 @@ function createTokens($iSurveyID)
//Add some criteria to select only the token field
$criteria = $this->getDbCriteria();
$criteria->select = 'token';
$ntresult = $this->findAllAsArray($criteria); //Use AsArray to skip active record creation
$ntresult = $this->findAllAsArray($criteria); //Use AsArray to skip active record creation

// select all existing tokens
foreach ($ntresult as $tkrow)
Expand All @@ -172,19 +171,29 @@ function createTokens($iSurveyID)
}

$newtokencount = 0;
$invalidtokencount=0;
foreach ($tkresult as $tkrow)
{
$bIsValidToken = false;
while ($bIsValidToken == false)
while ($bIsValidToken == false && $invalidtokencount<50)
{
$newtoken = randomChars($iTokenLength);
if (!in_array($newtoken, $existingtokens)) {
if (!in_array($newtoken, $existingtokens))
{
$existingtokens[] = $newtoken;
$bIsValidToken = true;
$invalidtokencount=0;
}
else
{
$invalidtokencount ++;
}
}
$itresult = $this->updateToken($tkrow['tid'], $newtoken);
$newtokencount++;
if(!$invalidtokencount)
{
$itresult = $this->updateToken($tkrow['tid'], $newtoken);
$newtokencount++;
}
}

return $newtokencount;
Expand Down

0 comments on commit 8e3bd1a

Please sign in to comment.