Mercurial > hg > fapweb
comparison msite.inc.php @ 69:30a4420e85ab
stGenerateUserKey() simply returns a generated key now (string) or FALSE if
there was a fatal error.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 11 Oct 2013 09:31:25 +0300 |
parents | 230aacc22cb4 |
children | d31fc2c53b2b |
comparison
equal
deleted
inserted
replaced
68:3ae137411706 | 69:30a4420e85ab |
---|---|
464 } | 464 } |
465 } | 465 } |
466 } | 466 } |
467 | 467 |
468 | 468 |
469 function stGenerateUserKey($uid) | 469 function stGenerateUserKey() |
470 { | 470 { |
471 global $db; | |
471 $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; | 472 $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
472 | 473 |
473 while (TRUE) | 474 while (TRUE) |
474 { | 475 { |
475 // Generate one keycode | 476 // Generate one randomized keycode |
476 $key = ""; | 477 $key = ""; |
477 for ($n = 0; $n < stGetSetting("userKeyLength"); $n++) | 478 for ($n = 0; $n < stGetSetting("userKeyLength"); $n++) |
478 $key .= $keyChars[rand() % strlen($keyChars)]; | 479 $key .= $keyChars[rand() % strlen($keyChars)]; |
479 | 480 |
480 // Check if it already exists, to avoid duplicates | 481 // Check if it already exists, to avoid duplicates |
482 // We need custom query code here, because stFetchSQLColumn() | |
483 // won't work due to it returning FALSE in error cases. | |
481 $sql = stPrepareSQL("SELECT * FROM attendees WHERE key=%s", $key); | 484 $sql = stPrepareSQL("SELECT * FROM attendees WHERE key=%s", $key); |
482 if (($res = @$db->query($sql)) !== FALSE) | 485 if (($res = @$db->query($sql)) !== FALSE) |
483 { | 486 { |
487 // Did we get results? | |
484 if ($res->fetchColumn() === FALSE) | 488 if ($res->fetchColumn() === FALSE) |
485 { | 489 { |
486 // Nope, add into database | 490 // Nope, return key |
487 $sql = stPrepareSQL( | 491 return $key; |
488 "UPDATE attendees SET key=%s,active=0 WHERE id=%d", | |
489 $key, $uid); | |
490 | |
491 if (($res = $db->query($sql)) === FALSE) | |
492 { | |
493 stLogSQLError($sql); | |
494 return FALSE; | |
495 } | |
496 | |
497 return TRUE; | |
498 } | 492 } |
499 } | 493 } |
500 else | 494 else |
501 { | 495 { |
502 stLogSQLError($sql); | 496 stLogSQLError($sql); |