[SUCS Devel] [Git][sucssite/sucs-site][master] 3 commits: Replace the old UID generation.

Laurence Sebastian Bowes elbows at sucs.org
Wed Feb 17 12:37:46 GMT 2016


Laurence Sebastian Bowes pushed to branch master at sucssite / sucs-site


Commits:
a16d95d0 by Imran Hussain at 2015-12-29T17:07:38+00:00
Replace the old UID generation.

New UID generation system that uses the full year as a prefix. Should stop UIDs
from being reused.

- - - - -
080478fe by Laurence Sebastian Bowes at 2016-02-13T03:26:59+00:00
Twat
- - - - -
c2f7394d by Laurence Sebastian Bowes at 2016-02-13T03:44:17+00:00
Minor fixes. Happy with this code now.
- - - - -


2 changed files:

- components/signup.php
- lib/member_functions.php


Changes:

=====================================
components/signup.php
=====================================
--- a/components/signup.php
+++ b/components/signup.php
@@ -128,16 +128,8 @@ if (isset($_REQUEST['signupid']) && isset($_REQUEST['signuppw'])) {
                         );
                         $failed = true;
                     } else {
-                        // determine the uid range
-                        if ($row[type] == 2) {
-                            $baseuid = 8;
-                        } else {
-                            $baseuid = 29;
-                        }
-                        $minuid = $baseuid * 1000;
-                        $maxuid = $minuid + 999;
-                        //get the new uid
-                        $uid = findUid($minuid, $maxuid);
+                        //generate the new uid
+                        $uid = generateUid();
                         // make a password
                         $password = make_password();
                         // make the ldif


=====================================
lib/member_functions.php
=====================================
--- a/lib/member_functions.php
+++ b/lib/member_functions.php
@@ -29,21 +29,30 @@ function make_password($length = 8)
     return $password;
 }
 
-function findUid($start, $end)
+function generateUid()
 {
-    $ds = ldap_connect("localhost");
-    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
-    $r = ldap_bind($ds);
-    $sr = ldap_search($ds, "dc=sucs,dc=org", "uid=*", array(uidNumber));
-    $info = ldap_get_entries($ds, $sr);
-    for ($i = 0; $i < $info[count]; $i++) {
-        $uids[$info[$i][uidnumber][0]] = true;
-    }
-    for ($i = $start; $i < $end; $i++) {
-        if (!isset($uids[$i])) {
-            $safeuid = $i;
-            break;
+
+    //get the year, this'll be the start/prefix of the uid
+    $prefix = date("Y");
+
+    //generate a uid
+    //check to see if it's taken/safe to use
+    $ok = false;
+    while ($ok == false) {
+
+        //generate random number between 00000 and 99999
+        $uid = sprintf("%05d", mt_rand(0, 99999));
+
+        //id return 1 for error (safe to take). 0 for success (taken) not safe
+        exec("id ".$prefix.$uid, $output, $returnVal);
+
+        //check the result of id
+        if ($returnVal == 1) {
+            // We have an unused one!
+            $ok = true;
+            $safeuid = $prefix.$uid;
         }
+
     }
 
     return $safeuid;



View it on GitLab: https://projects.sucs.org/sucssite/sucs-site/compare/d788d45fc86397a139212b181fdc8861332c9e75...c2f7394d9ed8408f824008a6a2ed5a57f03af51e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/devel/attachments/20160217/7a4fa366/attachment.html>


More information about the Devel mailing list