changeset 65:72b22729ae7e

Add function for generating vote keys into site lib.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 05 Oct 2013 12:51:31 +0300
parents e1b673be98ec
children 230aacc22cb4
files msite.inc.php
diffstat 1 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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