# HG changeset patch # User Matti Hamalainen # Date 1380966691 -10800 # Node ID 72b22729ae7e48d37e73eb977da2ab5c3378d0c9 # Parent e1b673be98ec54cc8d3493713f89f63ea1da1d2d Add function for generating vote keys into site lib. diff -r e1b673be98ec -r 72b22729ae7e msite.inc.php --- a/msite.inc.php Sat Oct 05 12:51:13 2013 +0300 +++ b/msite.inc.php Sat Oct 05 12:51:31 2013 +0300 @@ -466,4 +466,42 @@ } +function stGenerateUserKey($uid) +{ + $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; + + while (TRUE) + { + // Generate one keycode + $key = ""; + for ($n = 0; $n < stGetSetting("userKeyLength"); $n++) + $key .= $keyChars[rand() % strlen($keyChars)]; + + // Check if it already exists, to avoid duplicates + $sql = stPrepareSQL("SELECT * FROM attendees WHERE key=%s", $key); + if (($res = @$db->query($sql)) !== FALSE) + { + 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; + } + } + else + { + stLogSQLError($sql); + return FALSE; + } + } +} ?> \ No newline at end of file