From 802245209252a70bfeadd4a310ce261dd9cd9eb7 Mon Sep 17 00:00:00 2001
From: Tim Clark <eclipse@sucs,org>
Date: Sun, 13 Sep 2009 23:57:54 +0000
Subject: [PATCH] signup system now generates ldifs, and adds members to the
 mailing lists, dont forget to remove the dev mods before deploying

---
 components/signup.php    |  45 +++++++++++++++++
 lib/member_functions.php | 102 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)

diff --git a/components/signup.php b/components/signup.php
index c53f362..04a5b41 100644
--- a/components/signup.php
+++ b/components/signup.php
@@ -3,6 +3,13 @@
 //include("../member/signup.php");
 //$output = ob_get_contents();
 //ob_end_clean();
+//
+
+// -------------------------------------------------------------
+// TODO: CHANGE THIS TO "sudo /usr/local/sbin/" DEFORE DEPLOYING
+// -------------------------------------------------------------
+
+$script_path="/home/member/eclipse/signuptests/";
 
 //set defaults
 $mode = 'login';
@@ -89,9 +96,47 @@ if(isset($_REQUEST['signupid'])&&isset($_REQUEST['signuppw'])){
 		}
 
 		if($valid){
+		    // include membership adding functions
+		    require_once("../lib/member_functions.php");
 		    $mode='result';
+		    // determine the uid range
+		    if($row[type]==2){
+			    $baseuid=8;
+		    }
+		    else{
+			    $baseuid=28;
+		    }
+		    $minuid=$baseuid*1000;
+		    $maxuid=$minuid+999;
+		    //get the new uid
+		    $uid=findUid($minuid,$maxuid);
+		    // make a password
+		    $password=make_password();
+		    // make the ldif
+		    $ldif=generateLdif($uid,$password,$row[type],$_POST['realname'],$_POST['username']);
+		    // write ldif file
+		    file_put_contents('/tmp/useradd.'.$_POST['username'].'.ldif',$ldif);
+		    system(
+			    $script_path.'useradd.apache '.
+			    sh_escape($_POST['username']).' '.
+			    sh_escape($_POST['studentid']).' '.
+			    sh_escape($_POST['email'])
+		    );
+
+	            $addtolist ="".$_POST['email']."\n".$_POST['studentid']."@swan.ac.uk";
+	            file_put_contents('/tmp/listadd.'.$_POST['username'],$addtolist);
+		    system(
+			    $script_path.'listadd.apache '.
+			    sh_escape($_POST['username'])
+		    );
+
 		    //TODO: add membership add code here
+		    $_POST[uid]=$uid;
+		    $_POST[password]=$password;
+		    $_POST[ldif]=$ldif;
 		    $smarty->assign("post",$_POST);
+		    
+
 		}
 		else{
 		    //re-show form
diff --git a/lib/member_functions.php b/lib/member_functions.php
index 471f06d..b12198e 100644
--- a/lib/member_functions.php
+++ b/lib/member_functions.php
@@ -1,2 +1,104 @@
 <?
+//Escape spaces in a shell command
+function sh_escape($text)
+{
+        $text = escapeshellcmd($text);
+        return str_replace(' ', '\ ', $text);
+}
+
+function make_password($length=8)
+{
+        $vowels = "aeiouy";
+        $consonants = "bcdfghjklmnprst";
+        $password = "";
+
+        $cn = strlen($consonants)-1;
+        $vn = strlen($vowels)-1;
+
+        // Start on cons or vowel
+        $alt = mt_rand(0, 1);
+
+        // How many numbers
+        $len = mt_rand($length-3,$length);
+
+        for ($i = 0; $i < $len; $i++)
+        {
+                if ($alt == 1)
+                {
+                        $password .= $consonants[ mt_rand(0,$cn) ];
+                        $alt = 0;
+                }
+                else
+                {
+                        $password .= $vowels[ mt_rand(0,$vn) ];
+                        $alt = 1;
+                }
+        }
+        for ($i = 0; $i < $length-$len; $i++)
+        {
+                $password .= mt_rand(0,9);
+        }
+        return $password;
+}
+function findUid($start, $end) {
+	$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;
+                }
+        }
+
+        return $safeuid;
+}
+
+function generateLdif($uid, $password, $type, $realname, $username){
+	// explode the realname
+	$nameexplode = explode(' ', trim($realname));
+	// hash the password
+	$ldappassword = "{SHA}" . base64_encode(pack("H*", sha1($password)));
+	// compile ldif
+	$ldif  = "dn: uid=".$username.",ou=People,dc=sucs,dc=org\n";
+	$ldif .= "uid: ".$username."\n";
+	$ldif .= "cn: ".$realname."\n";
+	// if only has 1 part to real name (and therefore a soc) then set it as sn otherwise set first name to given name and last name to sn
+	if(count($nameexplode)>1){
+	    $ldif .= "givenName: ".$nameexplode[0]."\n";
+	    $ldif .= "sn: ".$nameexplode[count($nameexplode)-1]."\n";
+	}
+	else{
+	    $ldif .= "sn: ".$realname."\n";
+	}
+	$ldif .= "mail: ".$username."@sucs.org\n";
+	$ldif .= "objectClass: person\n";
+	$ldif .= "objectClass: organizationalPerson\n";
+	$ldif .= "objectClass: inetOrgPerson\n";
+	$ldif .= "objectClass: posixAccount\n";
+	$ldif .= "objectClass: top\n";
+	$ldif .= "userPassword: ".$ldappassword. "\n";
+	$ldif .= "loginShell: /bin/bash\n";
+	$ldif .= "uidNumber: ".$uid."\n";
+	// make some society specific changes
+	if($type==2){
+	    $gid=113;
+	    $homebase="society";
+	}
+	else {
+	    $gid=100;
+	    $homebase="member";
+	}
+	$ldif .= "gidNumber: ".$gid."\n";
+	$ldif .= "homeDirectory: /home/".$homebase."/".$username."\n";
+	$ldif .= "gcos: ".$realname."\n\n";
+
+	return $ldif;
+}
+
 ?>
-- 
GitLab