Skip to content

Commit

Permalink
Dev : Fixed #6301,#6300 AND old #3058
Browse files Browse the repository at this point in the history
Dev : remove in_array test, put true for $existingtokens[$newtoken]
Dev tested with 1 000 000 token too
  • Loading branch information
Shnoulle committed Jul 5, 2012
1 parent 8e3bd1a commit f11f8e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
37 changes: 26 additions & 11 deletions application/controllers/admin/tokens.php
Expand Up @@ -829,33 +829,33 @@ function addDummies($iSurveyId, $subaction = '')
$existingtokens=array();
foreach ($ntresult as $tkrow)
{
$existingtokens[] = $tkrow['token'];
$existingtokens[$tkrow['token']] = true ;
}
$invalidtokencount=0;
$newDummyToken=0;
for ($i = 0; $i < $amount; $i++)
while ($newDummyToken<$amount && $invalidtokencount<50)
{
$aDataToInsert = $aData;
$aDataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}', $i, $aDataToInsert['firstname']);
$aDataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}', $i, $aDataToInsert['lastname']);
$aDataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $i, $aDataToInsert['email']);
$aDataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['firstname']);
$aDataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['lastname']);
$aDataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['email']);

$isvalidtoken = false;
while ($isvalidtoken == false && $invalidtokencount<50)
{
$newtoken = randomChars($tokenlength);
if (!in_array($newtoken, $existingtokens))
if (!isset($existingtokens[$newtoken]))
{
$isvalidtoken = true;
$existingtokens[] = $newtoken;
$existingtokens[$newtoken] = true;
$invalidtokencount=0;
}
else
{
$invalidtokencount ++;
}
}
if(!$invalidtokencount)
if($isvalidtoken)
{
$aDataToInsert['token'] = $newtoken;
Tokens_dynamic::insertToken($iSurveyId, $aDataToInsert);
Expand All @@ -878,7 +878,9 @@ function addDummies($iSurveyId, $subaction = '')
$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='"
'message' => "<p>".sprintf($clang->gT("Only %s new dummy tokens were added after %s trials."),$newDummyToken,$invalidtokencount)
.$clang->gT("Try with a bigger token length.")."</p>"
."\n<input type='button' value='"
. $clang->gT("Display tokens") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/browse/surveyid/$iSurveyId") . "', '_top')\" />\n"
);
}
Expand Down Expand Up @@ -1931,10 +1933,23 @@ function tokenify($iSurveyId)
else
{
//get token length from survey settings
$newtokencount = Tokens_dynamic::model($iSurveyId)->createTokens($iSurveyId);
$newtoken = Tokens_dynamic::model($iSurveyId)->createTokens($iSurveyId);
$newtokencount = $newtoken['0'];
$neededtokencount = $newtoken['1'];
if($neededtokencount>$newtokencount)
{
$aData['success'] = false;
$message = sprintf($clang->ngT('Only %s token has been created.','Only %s tokens have been created.',$newtokencount),$newtokencount)
.sprintf($clang->ngT('Need %s token.','Need %s tokens.',$neededtokencount),$neededtokencount);
}
else
{
$aData['success'] = true;
$message = sprintf($clang->ngT('%s token has been created.','%s tokens have been created.',$newtokencount),$newtokencount);
}
$this->_renderWrappedTemplate('token', array('tokenbar', 'message' => array(
'title' => $clang->gT("Create tokens"),
'message' => sprintf($clang->ngT('%s token has been created.','%s tokens have been created.',$newtokencount),$newtokencount)
'message' => $message
)), $aData);
}
}
Expand Down
20 changes: 11 additions & 9 deletions application/models/Tokens_dynamic.php
Expand Up @@ -140,14 +140,13 @@ function selectEmptyTokens($iSurveyID)
* of tokens created
*
* @param int $iSurveyID
* @return int number of created tokens
* @return array ( int number of created tokens, int number to be created tokens)
*/
function createTokens($iSurveyID)
{
$tkresult = $this->selectEmptyTokens($iSurveyID);

//Exit early if there are not empty tokens
if (count($tkresult)===0) return 0;
if (count($tkresult)===0) return array(0,0);

//get token length from survey settings
$tlrow = Survey::model()->findByAttributes(array("sid"=>$iSurveyID));
Expand All @@ -158,7 +157,6 @@ function createTokens($iSurveyID)
{
$iTokenLength = 15;
}

//Add some criteria to select only the token field
$criteria = $this->getDbCriteria();
$criteria->select = 'token';
Expand All @@ -167,7 +165,7 @@ function createTokens($iSurveyID)
// select all existing tokens
foreach ($ntresult as $tkrow)
{
$existingtokens[] = $tkrow['token'];
$existingtokens[$tkrow['token']] = true;
}

$newtokencount = 0;
Expand All @@ -178,9 +176,9 @@ function createTokens($iSurveyID)
while ($bIsValidToken == false && $invalidtokencount<50)
{
$newtoken = randomChars($iTokenLength);
if (!in_array($newtoken, $existingtokens))
if (!isset($existingtokens[$newtoken]))
{
$existingtokens[] = $newtoken;
$existingtokens[$newtoken] = true;
$bIsValidToken = true;
$invalidtokencount=0;
}
Expand All @@ -189,14 +187,18 @@ function createTokens($iSurveyID)
$invalidtokencount ++;
}
}
if(!$invalidtokencount)
if($bIsValidToken)
{
$itresult = $this->updateToken($tkrow['tid'], $newtoken);
$newtokencount++;
}
else
{
break;
}
}

return $newtokencount;
return array($newtokencount,count($tkresult));
}

public function search()
Expand Down

0 comments on commit f11f8e5

Please sign in to comment.