added user registration confirmation, email "from" setting, lots of
authorPaul J R <me@pjr.cc>
Wed, 19 Sep 2012 20:41:01 +0000 (06:41 +1000)
committerPaul J R <me@pjr.cc>
Wed, 19 Sep 2012 20:41:01 +0000 (06:41 +1000)
other little registration components
added user delete from admin page

gwvpmini/gwvpmini.php
gwvpmini/gwvpmini_admin.php
gwvpmini/gwvpmini_auth.php
gwvpmini/gwvpmini_db.php
gwvpmini/gwvpmini_gitrepo.php
gwvpmini/gwvpmini_register.php [new file with mode: 0644]

index 8c510b6..42538f1 100644 (file)
@@ -8,6 +8,7 @@ require_once("gwvpmini_gitrepo.php");
 require_once("gwvpmini_search.php");
 require_once("gwvpmini_gitbackend.php");
 require_once("gwvpmini_view.php");
+require_once("gwvpmini_register.php");
 require_once("gwvpmini_user.php");
 if(gwvpmini_isLoggedIn()) if(gwvpmini_isUserAdmin()) {
        require_once("gwvpmini_admin.php");
index e289d45..7078542 100644 (file)
@@ -6,6 +6,8 @@ if(gwvpmini_isLoggedIn()) if(gwvpmini_isUserAdmin()) {
        $CALL_ME_FUNCTIONS["admin"] = "gwvpmini_AdminCallMe";
 }
 
+
+
 function gwvpmini_AdminCallMe()\r
 {\r
 \r
@@ -18,7 +20,22 @@ function gwvpmini_AdminCallMe()
                                if(isset($qspl[1])) {\r
                                        if($qspl[1] == "user") {\r
                                                return "gwvpmini_AdminUserCreate";\r
+                                       }
+                                       if($qspl[1] == "changereg") {
+                                               return "gwvpmini_ChangeRegistration";
+                                       }
+                                       if($qspl[1] == "changeconfirm") {
+                                               return "gwvpmini_ChangeRegConfig";
+                                       }
+                                       if($qspl[1] == "changefromemail") {
+                                               return "gwvpmini_ChangeFromAddress";
+                                       }
+                                       if($qspl[1] == "removeuser") {
+                                               return "gwvpmini_RemoveUserPage";
                                        }\r
+                                       if($qspl[1] == "confremoveuser") {
+                                               return "gwvpmini_ConfRemoveUser";
+                                       }
                                } else {\r
                                        error_log("i got here, where next?");\r
                                        return "gwvpmini_AdminMainPage";\r
@@ -31,6 +48,12 @@ function gwvpmini_AdminCallMe()
        return false;\r
 }
 
+function gwvpmini_RemoveUserPage()
+{
+       gwvpmini_goMainPage("gwvpmini_RemoveUserPageBody");
+       
+}
+
 function gwvpmini_AdminMainPage()
 {
        gwvpmini_goMainPage("gwvpmini_AdminMainPageBody");
@@ -39,20 +62,56 @@ function gwvpmini_AdminMainPage()
 function gwvpmini_AdminMainPageBody()
 {
        global $BASE_URL;
+       global $can_register, $reg_reqs_confirm, $confirm_from_address;
+       
+       if($can_register) {
+               $register = "Registration Enabled (<a href=\"$BASE_URL/admin/changereg\">Disable</a>)";
+       } else {
+               $register = "Registration Disabled (<a href=\"$BASE_URL/admin/changereg\">Enable</a>)";
+       }
        
+       if($reg_reqs_confirm) {
+               $regconfirm = "Registration Requires Confirmation (<a href=\"$BASE_URL/admin/changeconfirm\">Disable</a>)";
+       } else {
+               $regconfirm = "Registration Doesnt Require Confirmation (<a href=\"$BASE_URL/admin/changeconfirm\">Enable</a>)";
+       }
        $totalusers = gwvpmini_GetNUsers();
        echo "<table><tr valign=\"top\"><td>";
        echo "<h2>Users - $totalusers</h2>";
+       echo "$register<br>$regconfirm<br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/admin/changefromemail\">";
+       echo "Address emails are sent from <input type=\"text\" name=\"fromemail\" value=\"$confirm_from_address\"><input type=\"submit\" name=\"Update\" value=\"Update\"><br>";
+       echo "</form>"; 
+
        echo "<table border=\"1\">";
-       echo "<tr><th>Username</th><th>Email Address</th><th>Full Name</th><th>Description</th><th>Control</th></tr>";
+       echo "<tr><th>Username</th><th>Email Address</th><th>Full Name</th><th>Description</th><th>Status</th><th>Control</th></tr>";
        foreach(gwvpmini_GetUsers() as $key => $val) {
                $id = $key;
                $un = $val["username"];
                $em = $val["email"];
                $fn = $val["fullname"];
                $ds = $val["desc"];
+               $st_t = $val["status"];
+               $st_l = $val["level"];
+               
+               $level = "WTF";
+               if($st_l == 0) $level = "User";
+               if($st_l == 1) $level = "Admin";
+               
+               $status = "";
+               if($st_t[0] == "1") {
+                       $status = ", disabled";
+               }
+               if($st_t[0] == "2") {
+                       $vl = explode(":", $st_t);
+                       error_log("VL: ".print_r($vl, true));
+                       $status = " Awaiting Confirmation (<a href=\"$BASE_URL/register/confirmreg/".$vl[1]."\">Confirm</a>)";
+               }
+               
+               $st = "$level$status";
+               
                $unlval = "<a href=\"$BASE_URL/user/$un\">$un</a>";
-               echo "<tr><td>$unlval</td><td>$em</td><td>$fn</td><td>$ds</td><td><a href=\"$BASE_URL/admin/removeuser&id=$id\">Remove</a> <a href=\"$BASE_URL/admin/disableuser&id=$id\">Disable</a></td></tr>";
+               echo "<tr><td>$unlval</td><td>$em</td><td>$fn</td><td>$ds</td><td>$st</td><td><a href=\"$BASE_URL/admin/removeuser/$id\">Remove</a> <a href=\"$BASE_URL/admin/disableuser&id=$id\">Disable</a></td></tr>";
        }
        
        
@@ -62,8 +121,8 @@ function gwvpmini_AdminMainPageBody()
        echo "<form method=\"post\" action=\"$BASE_URL/admin/user/create\">";
        echo "<table border=\"1\">";\r
        echo "<tr><th>Username</th><td><input type=\"text\" name=\"username\"></td></tr>";
-       echo "<tr><th>Password</th><td><input type=\"text\" name=\"password\"></td></tr>";
-       echo "<tr><th>Confirm Password</th><td><input type=\"text\" name=\"confpassword\"></td></tr>";
+       echo "<tr><th>Password</th><td><input type=\"password\" name=\"password\"></td></tr>";
+       echo "<tr><th>Confirm Password</th><td><input type=\"password\" name=\"confpassword\"></td></tr>";
        echo "<tr><th>Full Name</th><td><input type=\"text\" name=\"fullname\"></td></tr>";
        echo "<tr><th>Description</th><td><input type=\"text\" name=\"desc\"></td></tr>";
        echo "<tr><th>Email</th><td><input type=\"text\" name=\"email\"></td></tr>";
@@ -127,4 +186,108 @@ function gwvpmini_AdminUserCreate()
        return;
        
 }
+
+
+function gwvpmini_ChangeRegistration()
+{
+       global $can_register, $BASE_URL;
+       
+       if($can_register) {
+               gwvpmini_setConfigVal("canregister", "0");
+               gwvpmini_SendMessage("info", "Registration disabled");
+       } else {
+               gwvpmini_setConfigVal("canregister", "1");\r
+               gwvpmini_SendMessage("info", "Registration enabled");
+       }
+       
+       header("Location: $BASE_URL/admin");
+}
+
+
+function gwvpmini_ChangeRegConfig()
+{
+       global $reg_reqs_confirm, $BASE_URL;\r
+       \r
+       if($reg_reqs_confirm) {\r
+               gwvpmini_setConfigVal("registerrequiresconfirm", "0");\r
+               gwvpmini_SendMessage("info", "Registration Confirmation disabled");\r
+       } else {\r
+               gwvpmini_setConfigVal("registerrequiresconfirm", "1");\r
+               gwvpmini_SendMessage("info", "Registration Confirmation enabled");\r
+       }\r
+       \r
+       header("Location: $BASE_URL/admin");
+}
+
+function gwvpmini_ChangeFromAddress()
+{
+       global $BASE_URL;
+       
+       $newfrom = $_REQUEST["fromemail"];\r
+       \r
+       gwvpmini_setConfigVal("eamilfromaddress", "$newfrom");\r
+       gwvpmini_SendMessage("info", "Email from address updated to \"$newfrom\"");\r
+       \r
+       header("Location: $BASE_URL/admin");\r
+       
+}
+
+function gwvpmini_RemoveUserPageBody()
+{
+       global $BASE_URL;
+       
+       $uid = -1;
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               if(isset($qspl[2])) {
+                       $uid = $qspl[2];\r
+               }
+       }
+       
+       if($uid != -1) {
+               $details = gwvpmini_GetUsers($uid, 1);
+               $username = $details[$uid]["username"];
+               $fullname = $details[$uid]["fullname"];
+               $email = $details[$uid]["email"];
+               $desc = $details[$uid]["desc"];
+               
+               error_log("user dets:".print_r($details, true));
+               
+               echo "<h2>Remove User?</h2>";
+               echo "Are you sure you wish to remove the user, $username ($uid) - $fullname - $email - $desc?<br>";
+               echo "<a href=\"$BASE_URL/admin/confremoveuser/$uid\">Yes</a> <a href=\"$BASE_URL/admin\">No</a><br>";
+       } else {
+               echo "<h2>How?</h2>";
+               echo "You got here in a weird way or the uid of the user you were trying to delete is invalid<br>";
+               echo "<a href=\"$BASE_URL/admin\">Go Back</a>";
+       }
+       
+}
+
+function gwvpmini_ConfRemoveUser()\r
+{\r
+       global $BASE_URL;\r
+\r
+       $uid = -1;\r
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               if(isset($qspl[2])) {\r
+                       $uid = $qspl[2];\r
+               }\r
+       }\r
+\r
+       if($uid > 0) {
+               $details = gwvpmini_getUser(null, null, $uid);
+               $uname = $details["username"];
+               gwvpmini_RemoveUser($uid);
+               gwvpmini_SendMessage("info", "User $uname ($uid) has been removed");\r
+       } else {
+               gwvpmini_SendMessage("info", "Problem deleteing user with uid $uid");
+       }\r
+
+       header("Location: $BASE_URL/admin");\r
+}\r
+
 ?>
\ No newline at end of file
index be5e410..f54bd13 100644 (file)
@@ -70,15 +70,16 @@ function gwvpmini_SingleLineLoginForm()
        global $BASE_URL;\r
 \r
        echo "<form method=\"post\" action=\"$BASE_URL/login\">Username <input type=\"text\" name=\"username\" class=\"login\">";\r
-       echo " Passowrd <input type=\"text\" name=\"password\" class=\"login\"><input type=\"submit\" name=\"login\" value=\"Login\" class=\"loginbutton\">";\r
+       echo " Passowrd <input type=\"password\" name=\"password\" class=\"login\"><input type=\"submit\" name=\"login\" value=\"Login\" class=\"loginbutton\">";\r
        if(gwvpmini_IsRegistrationEnabled()) echo "<a href=\"$BASE_URL/register\">Register</a></form>";\r
-       else echo "</form><br>";\r
+       else echo "</form>";\r
 }\r
 
 
 function gwvpmini_IsRegistrationEnabled()
 {
-       return true;
+       global $can_register;
+       return $can_register;
 }
 
 function gwvpmini_isLoggedIn()
index cf3e0fd..5ce6b9a 100644 (file)
@@ -53,6 +53,17 @@ function gwvpmini_getUser($username=null, $email=null, $id=null)
 \r
        return $returns;\r
 \r
+}
+
+function gwvpmini_RemoveUser($uid)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       
+       if($uid < 0) return;
+       
+       $sql = "delete from users where user_id='$uid'";
+       
+       return $conn->query($sql);
 }\r
 
 function gwvpmini_ConnectDB()\r
@@ -86,6 +97,27 @@ function gwvpmini_ConnectDB()
        return $DB_CONNECTION;\r
 }
 
+function gwvpmini_UpdateStatusFromConfirm($confirmhash)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       $sql = "select count(*) from users where user_status='2:$confirmhash'";
+       
+       $res = $conn->query($sql);
+       
+       if(!$res) return false;
+       
+       foreach($res as $row) {
+               $retval = $row[0];
+       }
+       
+       if($retval > 0) {
+               $sql = "update users set user_status='0' where user_status='2:$confirmhash'";
+               return $conn->query($sql);
+       } else return false;
+}
+
+
 function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level, $status)
 {
        \r
@@ -330,7 +362,10 @@ function gwvpmini_setConfigVal($confname, $confval)
        */\r
        gwvpmini_eraseConfigVal($confname);\r
 \r
-       $conn = gwvpmini_ConnectDB();\r
+       $conn = gwvpmini_ConnectDB();
+       
+       $sql = "delete from config where config_name='$confname'";
+       $conn->query($sql);\r
 \r
        $sql = "insert into config values('$confname', '$confval')";\r
 \r
@@ -447,7 +482,7 @@ function gwvpmini_GetUsers($startat = 0, $num = 10)
 
         */\r
        \r
-       $sql = "select * from users where user_id>'$startat' order by user_id asc limit $num";
+       $sql = "select * from users where user_id>='$startat' order by user_id asc limit $num";
        
        $res = $conn->query($sql);
        
index 8ad5725..a120da4 100644 (file)
@@ -53,8 +53,8 @@ function gwvpmini_RepoMainPageBody()
 {
        global $BASE_URL;
        
-       gwvpmini_GitCreateRepoForm();
        if(gwvpmini_isLoggedIn()) {
+               gwvpmini_GitCreateRepoForm();
                $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
                if(!$repos) {
                        echo "You currently own no repos<br>";  
diff --git a/gwvpmini/gwvpmini_register.php b/gwvpmini/gwvpmini_register.php
new file mode 100644 (file)
index 0000000..4fdd90e
--- /dev/null
@@ -0,0 +1,183 @@
+<?php
+$CALL_ME_FUNCTIONS["register"] = "gwvpmini_RegisterCallMe";
+
+global $can_register, $reg_reqs_confirm, $confirm_from_address;\r
+
+$reg = gwvpmini_getConfigVal("canregister");\r
+$reg2 = gwvpmini_getConfigVal("registerrequiresconfirm");
+$reg3 = gwvpmini_getConfigVal("eamilfromaddress");\r
+\r
+if($reg == null) {\r
+       gwvpmini_setConfigVal("canregister", "1");\r
+} else if($reg == 1) {\r
+       $can_register = true;\r
+} else {\r
+       $can_register = false;\r
+}
+
+if($reg2 == null) {\r
+       gwvpmini_setConfigVal("registerrequiresconfirm", "0");\r
+} else if($reg2 == 1) {\r
+       $reg_reqs_confirm = true;\r
+} else {\r
+       $reg_reqs_confirm = false;\r
+}\r
+\r
+if($reg3 == null) {\r
+       gwvpmini_setConfigVal("eamilfromaddress", "admin@localhost");
+       $confirm_from_address = "admin@localhost";\r
+} else {\r
+       $confirm_from_address = $reg3;\r
+}\r
+
+
+function gwvpmini_RegisterCallMe()
+{
+       
+       
+       error_log("in admin callme");
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               if(isset($qspl[0])) {
+                       if($qspl[0] == "register") {
+                               if(isset($qspl[1])) {
+                                       if($qspl[1] == "sendinfo") {
+                                               return "gwvpmini_RegisterUser";
+                                       }
+                                       if($qspl[1] == "confirmreg") {
+                                               return "gwvpmini_ConfirmRegistration";
+                                       }
+                               } else return "gwvpmini_RegisterPage";
+                       } else return false;
+               }
+               else return false;
+       }
+
+       return false;
+       
+       
+}
+
+function gwvpmini_RegisterPage()
+{
+       global $user_view_call, $MENU_ITEMS, $BASE_URL;
+       
+       $MENU_ITEMS["40thisuser"]["text"] = "Register";
+       $MENU_ITEMS["40thisuser"]["link"] = "$BASE_URL/register";
+       
+       gwvpmini_goMainPage("gwvpmini_RegisterPageBody");
+}
+
+function gwvpmini_RegisterPageBody()
+{
+       global $user_view_call, $can_register, $BASE_URL, $reg_reqs_confirm;
+       
+       echo "<h2>Registration</h2>";
+       echo "Complete the following form for registration<br>";
+       if($reg_reqs_confirm) {
+               echo "Email address will be confirmed after this form is completed, so make sure its available and viewable<br>";
+       }
+       echo "<form method=\"post\" action=\"$BASE_URL/register/sendinfo\">";
+       echo "<table border=\"1\">";
+       echo "<tr><th>Name to go by (full name/nickname/etc)</th><td><input type=\"text\" name=\"fullname\"></td></tr>";
+       echo "<tr><th>Username (desired username for login)</th><td><input type=\"text\" name=\"username\"></td></tr>";
+       echo "<tr><th>Password</th><td><input type=\"password\" name=\"password\"></td></tr>";\r
+       echo "<tr><th>Confirm Password</th><td><input type=\"password\" name=\"confpassword\"></td></tr>";\r
+       echo "<tr><th>Description of yourself</th><td><input type=\"text\" name=\"desc\"></td></tr>";\r
+       echo "<tr><th>Email</th><td><input type=\"text\" name=\"email\"></td></tr>";\r
+       echo "<tr><th>Confirm Email</th><td><input type=\"text\" name=\"confemail\"></td></tr>";
+       echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr>";\r
+       echo "</table>";
+       echo "</form>";
+       
+}
+
+function gwvpmini_RegisterUser()
+{
+       global $can_register, $BASE_URL, $reg_reqs_confirm;
+       
+       $reg_succeeded = true;
+       $failed_error = "oops";\r
+       
+       $uname = $_REQUEST["username"];
+       $fname = $_REQUEST["fullname"];
+       $pass1 = $_REQUEST["password"];
+       $pass2 = $_REQUEST["confpassword"];
+       $email1 = $_REQUEST["email"];
+       $email2 = $_REQUEST["confemail"];
+       $desc = $_REQUEST["desc"];
+       
+       if($pass1 != $pass2) {
+               $failed_error = "Password and confirmation password differ (hit back to try again)";
+               $reg_succeeded = false;
+       }
+       
+       if($email1 != $email2) {
+               $failed_error = "email and confirmation email differ (hit back to try again)";
+               $reg_succeeded = false;
+       }
+       
+       if(gwvpmini_GetUserId($uname) !== false) {
+               $failed_error = "Username already in use (hit back and try a new one)";
+               $reg_succeeded = false;
+       }
+       
+       if(!$reg_succeeded) {
+               gwvpmini_SendMessage("error", $failed_error);
+       } else {
+               //function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level, $status)
+               if($reg_reqs_confirm) {
+                       $hash = gwvpmini_GenerateHash();
+                       $s = "2:$hash";
+                       gwvpmini_SendMessage("info", "An email has been sent to the registered email address with details to continue the registration process $hash");
+               } else {
+                       gwvpmini_SendMessage("info", "Congratulations, you are now registered, login to continue");
+                       $s = 0;
+               }
+               
+               gwvpmini_AddUser($uname, $pass1, $fname, $email1, $desc, 0, $s);
+               
+       }
+       
+       header("Location: $BASE_URL");
+}
+
+function gwvpmini_GenerateHash()
+{
+       $hashlen = 64;
+       $hashchars = "abcdefghijlkmnopqrstuvwxyz01234567890";
+       
+       $hash = "";
+       for($i=0; $i<$hashlen; $i++) {
+               $hash .= $hashchars[rand(0,strlen($hashchars)-1)];
+       }
+       
+       return $hash;
+}
+
+function gwvpmini_ConfirmRegistration()
+{
+       global $can_register, $BASE_URL, $reg_reqs_confirm;
+       
+       $hash = "";
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);
+               if(isset($qspl[2])) {
+                       $hash = $qspl[2];
+               }\r
+       }
+       
+       if($hash == "") {
+               gwvpmini_SendMessage("error", "Confirmation failed, Confirm the url you used and try again");
+       } else if(gwvpmini_UpdateStatusFromConfirm($hash)) {
+               gwvpmini_SendMessage("info", "Confirmation succeeded, you may now login with your username and password");
+       } else {
+               gwvpmini_SendMessage("error", "Confirmation failed, Confirm the url you used and try again");
+       }
+       
+       header("Location: $BASE_URL");
+}
+
+?>
\ No newline at end of file