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