added user registration confirmation, email "from" setting, lots of
[gwvp-mini.git] / gwvpmini / gwvpmini_register.php
1 <?php
2 $CALL_ME_FUNCTIONS["register"] = "gwvpmini_RegisterCallMe";
3
4 global $can_register, $reg_reqs_confirm, $confirm_from_address;\r
5
6 $reg = gwvpmini_getConfigVal("canregister");\r
7 $reg2 = gwvpmini_getConfigVal("registerrequiresconfirm");
8 $reg3 = gwvpmini_getConfigVal("eamilfromaddress");\r
9 \r
10 if($reg == null) {\r
11         gwvpmini_setConfigVal("canregister", "1");\r
12 } else if($reg == 1) {\r
13         $can_register = true;\r
14 } else {\r
15         $can_register = false;\r
16 }
17
18 if($reg2 == null) {\r
19         gwvpmini_setConfigVal("registerrequiresconfirm", "0");\r
20 } else if($reg2 == 1) {\r
21         $reg_reqs_confirm = true;\r
22 } else {\r
23         $reg_reqs_confirm = false;\r
24 }\r
25 \r
26 if($reg3 == null) {\r
27         gwvpmini_setConfigVal("eamilfromaddress", "admin@localhost");
28         $confirm_from_address = "admin@localhost";\r
29 } else {\r
30         $confirm_from_address = $reg3;\r
31 }\r
32
33
34 function gwvpmini_RegisterCallMe()
35 {
36         
37         
38         error_log("in admin callme");
39         if(isset($_REQUEST["q"])) {
40                 $query = $_REQUEST["q"];
41                 $qspl = explode("/", $query);
42                 if(isset($qspl[0])) {
43                         if($qspl[0] == "register") {
44                                 if(isset($qspl[1])) {
45                                         if($qspl[1] == "sendinfo") {
46                                                 return "gwvpmini_RegisterUser";
47                                         }
48                                         if($qspl[1] == "confirmreg") {
49                                                 return "gwvpmini_ConfirmRegistration";
50                                         }
51                                 } else return "gwvpmini_RegisterPage";
52                         } else return false;
53                 }
54                 else return false;
55         }
56
57         return false;
58         
59         
60 }
61
62 function gwvpmini_RegisterPage()
63 {
64         global $user_view_call, $MENU_ITEMS, $BASE_URL;
65         
66         $MENU_ITEMS["40thisuser"]["text"] = "Register";
67         $MENU_ITEMS["40thisuser"]["link"] = "$BASE_URL/register";
68         
69         gwvpmini_goMainPage("gwvpmini_RegisterPageBody");
70 }
71
72 function gwvpmini_RegisterPageBody()
73 {
74         global $user_view_call, $can_register, $BASE_URL, $reg_reqs_confirm;
75         
76         echo "<h2>Registration</h2>";
77         echo "Complete the following form for registration<br>";
78         if($reg_reqs_confirm) {
79                 echo "Email address will be confirmed after this form is completed, so make sure its available and viewable<br>";
80         }
81         echo "<form method=\"post\" action=\"$BASE_URL/register/sendinfo\">";
82         echo "<table border=\"1\">";
83         echo "<tr><th>Name to go by (full name/nickname/etc)</th><td><input type=\"text\" name=\"fullname\"></td></tr>";
84         echo "<tr><th>Username (desired username for login)</th><td><input type=\"text\" name=\"username\"></td></tr>";
85         echo "<tr><th>Password</th><td><input type=\"password\" name=\"password\"></td></tr>";\r
86         echo "<tr><th>Confirm Password</th><td><input type=\"password\" name=\"confpassword\"></td></tr>";\r
87         echo "<tr><th>Description of yourself</th><td><input type=\"text\" name=\"desc\"></td></tr>";\r
88         echo "<tr><th>Email</th><td><input type=\"text\" name=\"email\"></td></tr>";\r
89         echo "<tr><th>Confirm Email</th><td><input type=\"text\" name=\"confemail\"></td></tr>";
90         echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr>";\r
91         echo "</table>";
92         echo "</form>";
93         
94 }
95
96 function gwvpmini_RegisterUser()
97 {
98         global $can_register, $BASE_URL, $reg_reqs_confirm;
99         
100         $reg_succeeded = true;
101         $failed_error = "oops";\r
102         
103         $uname = $_REQUEST["username"];
104         $fname = $_REQUEST["fullname"];
105         $pass1 = $_REQUEST["password"];
106         $pass2 = $_REQUEST["confpassword"];
107         $email1 = $_REQUEST["email"];
108         $email2 = $_REQUEST["confemail"];
109         $desc = $_REQUEST["desc"];
110         
111         if($pass1 != $pass2) {
112                 $failed_error = "Password and confirmation password differ (hit back to try again)";
113                 $reg_succeeded = false;
114         }
115         
116         if($email1 != $email2) {
117                 $failed_error = "email and confirmation email differ (hit back to try again)";
118                 $reg_succeeded = false;
119         }
120         
121         if(gwvpmini_GetUserId($uname) !== false) {
122                 $failed_error = "Username already in use (hit back and try a new one)";
123                 $reg_succeeded = false;
124         }
125         
126         if(!$reg_succeeded) {
127                 gwvpmini_SendMessage("error", $failed_error);
128         } else {
129                 //function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level, $status)
130                 if($reg_reqs_confirm) {
131                         $hash = gwvpmini_GenerateHash();
132                         $s = "2:$hash";
133                         gwvpmini_SendMessage("info", "An email has been sent to the registered email address with details to continue the registration process $hash");
134                 } else {
135                         gwvpmini_SendMessage("info", "Congratulations, you are now registered, login to continue");
136                         $s = 0;
137                 }
138                 
139                 gwvpmini_AddUser($uname, $pass1, $fname, $email1, $desc, 0, $s);
140                 
141         }
142         
143         header("Location: $BASE_URL");
144 }
145
146 function gwvpmini_GenerateHash()
147 {
148         $hashlen = 64;
149         $hashchars = "abcdefghijlkmnopqrstuvwxyz01234567890";
150         
151         $hash = "";
152         for($i=0; $i<$hashlen; $i++) {
153                 $hash .= $hashchars[rand(0,strlen($hashchars)-1)];
154         }
155         
156         return $hash;
157 }
158
159 function gwvpmini_ConfirmRegistration()
160 {
161         global $can_register, $BASE_URL, $reg_reqs_confirm;
162         
163         $hash = "";
164         if(isset($_REQUEST["q"])) {\r
165                 $query = $_REQUEST["q"];\r
166                 $qspl = explode("/", $query);
167                 if(isset($qspl[2])) {
168                         $hash = $qspl[2];
169                 }\r
170         }
171         
172         if($hash == "") {
173                 gwvpmini_SendMessage("error", "Confirmation failed, Confirm the url you used and try again");
174         } else if(gwvpmini_UpdateStatusFromConfirm($hash)) {
175                 gwvpmini_SendMessage("info", "Confirmation succeeded, you may now login with your username and password");
176         } else {
177                 gwvpmini_SendMessage("error", "Confirmation failed, Confirm the url you used and try again");
178         }
179         
180         header("Location: $BASE_URL");
181 }
182
183 ?>