# HG changeset patch # User Matti Hamalainen # Date 1381473085 -10800 # Node ID 30a4420e85aba48225a5f994c7013680088e407a # Parent 3ae137411706225cda195d86de808cc3c5020601 stGenerateUserKey() simply returns a generated key now (string) or FALSE if there was a fatal error. diff -r 3ae137411706 -r 30a4420e85ab msite.inc.php --- a/msite.inc.php Fri Oct 11 09:29:44 2013 +0300 +++ b/msite.inc.php Fri Oct 11 09:31:25 2013 +0300 @@ -466,35 +466,29 @@ } -function stGenerateUserKey($uid) +function stGenerateUserKey() { + global $db; $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; while (TRUE) { - // Generate one keycode + // Generate one randomized keycode $key = ""; for ($n = 0; $n < stGetSetting("userKeyLength"); $n++) $key .= $keyChars[rand() % strlen($keyChars)]; // Check if it already exists, to avoid duplicates + // We need custom query code here, because stFetchSQLColumn() + // won't work due to it returning FALSE in error cases. $sql = stPrepareSQL("SELECT * FROM attendees WHERE key=%s", $key); if (($res = @$db->query($sql)) !== FALSE) { + // Did we get results? if ($res->fetchColumn() === FALSE) { - // Nope, add into database - $sql = stPrepareSQL( - "UPDATE attendees SET key=%s,active=0 WHERE id=%d", - $key, $uid); - - if (($res = $db->query($sql)) === FALSE) - { - stLogSQLError($sql); - return FALSE; - } - - return TRUE; + // Nope, return key + return $key; } } else