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]
  • Loading branch information
Shnoulle committed Jul 5, 2012
1 parent f8a1699 commit 5afab72
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions admin/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -2114,21 +2114,21 @@
$ntresult = db_execute_assoc($ntquery);
while ($tkrow = $ntresult->FetchRow())
{
$existingtokens[]=$tkrow['token'];
$existingtokens[$tkrow['token']]=true;
}
$newtokencount = 0;
$invalidtokencount=0;
$tkquery = "SELECT tid FROM ".db_table_name("tokens_$surveyid")." WHERE token IS NULL OR token=''";
$tkresult = db_execute_assoc($tkquery) or safe_die ("Mucked up!<br />$tkquery<br />".$connect->ErrorMsg());
while ($tkrow = $tkresult->FetchRow())
while (($tkrow = $tkresult->FetchRow()) && $invalidtokencount<50)
{
$isvalidtoken = false;
while ($isvalidtoken == false && $invalidtokencount<50)
{
$newtoken = sRandomChars($tokenlength);
if (!in_array($newtoken,$existingtokens)) {
if (!isset($existingtokens[$newtoken])) {
$isvalidtoken = true;
$existingtokens[]=$newtoken;
$existingtokens[$newtoken]=true;
$invalidtokencount=0;
}
else
Expand All @@ -2146,8 +2146,9 @@
if(!$invalidtokencount){
$tokenoutput .= "<div class='successheader'>".sprintf($clang->gT("%s tokens have been created"),$newtokencount)."</div>\n";
}else{
$tokenoutput .= "\t\t<div class='errorheader'>".$clang->gT("Error")."</div>\n";
$tokenoutput .= sprintf($clang->gT("Only %s tokens have been created"),$newtokencount)."\n";
$tokenoutput .= "\t\t<div class='errorheader'>".$clang->gT("Error")."</div>\n"
."\t\t<p>".sprintf($clang->gT("Only %s new tokens were added after %s trials."),$newtokencount,$invalidtokencount)."\n"
."\t\t".$clang->gT("Try with a bigger token length.")."</p>\n";
}
}
$tokenoutput .= "</div>\n";
Expand Down Expand Up @@ -2682,34 +2683,38 @@
{
$data[$attr_name]=$_POST[$attr_name];
}
$tblInsert=db_table_name('tokens_'.$surveyid);
$amount = sanitize_int($_POST['amount']);
$tokenlength = sanitize_int($_POST['tokenlen']);
$invalidtokencount=0;
$newdummytoken = 0;
// select all existing tokens
$ntquery = "SELECT token FROM ".db_table_name("tokens_$surveyid")." group by token";
$ntresult = db_execute_assoc($ntquery);
$existingtokens=array();
while ($tkrow = $ntresult->FetchRow())
{
$existingtokens[]=$tkrow['token'];
$existingtokens[$tkrow['token']]=true;
}
$tblInsert=db_table_name('tokens_'.$surveyid);
$amount = sanitize_int($_POST['amount']);
$tokenlength = sanitize_int($_POST['tokenlen']);
$invalidtokencount=0;
$newdummytoken = 0;
for ($i=0; $i<$amount;$i++){
while ($newdummytoken<$amount && $invalidtokencount<50){
$dataToInsert = $data;
$dataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['firstname']);
$dataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['lastname']);
$dataToInsert['email'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['email']);
$dataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}',"$newdummytoken",$dataToInsert['firstname']);
$dataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}',"$newdummytoken",$dataToInsert['lastname']);
$dataToInsert['email'] = str_replace('{TOKEN_COUNTER}',"$newdummytoken",$dataToInsert['email']);
$isvalidtoken = false;
$invalidtokencount=0;
while ($isvalidtoken == false && $invalidtokencount<50)
{
$newtoken = sRandomChars($tokenlength);
if (!in_array($newtoken,$existingtokens)) {
if (!isset($existingtokens[$newtoken])) {
$isvalidtoken = true;
$existingtokens[]=$newtoken;
$existingtokens[$newtoken]=true;
$invalidtokencount=0;
$newdummytoken++;
}
else
{
Expand All @@ -2720,6 +2725,7 @@
$dataToInsert['token'] = $newtoken;
$tblInsert=db_table_name('tokens_'.$surveyid);
$inresult = $connect->AutoExecute($tblInsert, $dataToInsert, 'INSERT') or safe_die ("Add new record failed:<br />\n$inquery<br />\n".$connect->ErrorMsg());
$newdummytoken++;
}
}
if(!$invalidtokencount)
Expand All @@ -2732,7 +2738,8 @@
else
{
$tokenoutput .= "\t\t<div class='errorheader'>".$clang->gT("Error")."</div>\n"
."\t\t<br />".sprintf($clang->gT("Only %s new dummy tokens were added."),$newdummytoken)."<br /><br />\n"
."\t\t<p>".sprintf($clang->gT("Only %s new dummy tokens were added after %s trials."),$newdummytoken,$invalidtokencount)."\n"
."\t\t".$clang->gT("Try with a bigger token length.")."</p>\n"
."\t\t<input type='button' value='".$clang->gT("Display tokens")."' onclick=\"window.open('$scriptname?action=tokens&amp;sid=$surveyid&amp;subaction=browse', '_top')\" /><br />\n";
$tokenoutput .= "\t</div>";
}
Expand Down

0 comments on commit 5afab72

Please sign in to comment.