3e0ddcd8cdaae062da3df4d5a2b78cc9b1b17725
[gwvp.git] / gwvplib / gwvpauth.php
1 <?php
2
3 // we call it 00aaa so it gets called first
4 $CALL_ME_FUNCTIONS["00aaa"] = "gwvp_AuthCallMe";
5
6 function gwvp_AuthCallMe()
7 {
8         global $LOGIN_TYPE;
9         session_start();
10
11         if(isset($_REQUEST["q"])) {
12                 $query = explode("/", $_REQUEST["q"]);
13                 if($query[0] == "login") return "gwvp_AuthHandleLogin";
14                 if($query[0] == "logout") return "gwvp_AuthHandleLogout";
15                 if($query[0] == "register") {
16                         if(isset($query[1])) {
17                                 return "qwvp_attemptRegistration";
18                         }
19                         return "gwvp_RegistrationCall";
20                 }
21         }
22         $login = gwvp_isLoggedIn();
23         
24         error_log("authcallme as $login");
25         if($login!== false) {
26                 if(gwvp_IsUserAdmin(null, $login)) {
27                         $LOGIN_TYPE = "admin";
28                 } else {
29                         $LOGIN_TYPE = "user";
30                 }
31         } else {
32                 $LOGIN_TYPE = "anon";
33         }
34         
35         return false;
36 }
37
38 function gwvp_AskForBasicAuth()
39 {
40         if(!isset($_SERVER["PHP_AUTH_USER"])) {
41                 header('WWW-Authenticate: Basic realm="My Realm"');
42                 header('HTTP/1.0 401 Unauthorized');
43                 exit(0);
44         } else return; 
45 }
46
47 // $levels is checked against $LOGIN_TYPE, levels can be either just "admin" or admin,user anon,user anon, etc.
48 function gwvp_CheckAuthLevel($levels)
49 {
50         global $LOGIN_TYPE;
51         
52         $spl = explode(",", $levels);
53         foreach($spl as $levs) {
54                 if($LOGIN_TYPE == $levs) {
55                         return true;
56                 }
57         }
58         
59         return false;
60 }
61
62 function gwvp_fourZeroThree()
63 {
64         header("HTTP/1.0 403 Permission Denied");
65         exit(0);
66 }
67
68 function gwvp_AuthNoPerms()
69 {
70         gwvp_goMainPage("gwvp_AuthNoPermsBody");
71 }
72
73 function gwvp_AuthNoPermsBody()
74 {
75         echo "You have no permissions for this page, do you need to login?";
76 }
77
78 function gwvp_AuthHandleLogout()
79 {
80         global $BASE_URL;
81         
82         unset($_SESSION["isloggedin"]);
83         unset($_SESSION["username"]);
84         unset($_SESSION["fullname"]);
85         unset($_SESSION["usertype"]);
86         
87         gwvp_SendMessage("info", "Logged out");
88         header("Location: $BASE_URL");
89 }
90
91 function gwvp_RegistrationCall()
92 {
93         if(gwvp_IsRegistrationEnabled()) {
94                 gwvp_goMainPage("gwvp_RegistrationPageBody");
95         } else {
96                 gwvp_goMainPage("gwvp_RegistrationDisabledBody");
97         }
98 }
99
100 function gwvp_authUserPass($user, $pass)
101 {
102         $details = gwvp_getUser($user);
103         if($details == false) {
104                 return false;
105         }
106         
107         if(sha1($pass)!=$details["password"]) return false;
108         
109         return $details["username"];
110 }
111
112 function gwvp_AuthHandleLogin()
113 {
114         global $BASE_URL;
115
116         $user = "";
117         $pass = "";
118         if(isset($_REQUEST["username"])) $user = $_REQUEST["username"];
119         if(isset($_REQUEST["password"])) $pass = $_REQUEST["password"];
120
121         if(gwvp_authUserPass($user, $pass) === false) {
122                 gwvp_SendMessage("error", "Login Failed");
123                 header("Location: $BASE_URL");
124         } else {
125                 $details = gwvp_getUser($user);
126                 $_SESSION["isloggedin"] = true;
127                 $_SESSION["username"] = "$user";
128                 $_SESSION["fullname"] = $details["fullname"];
129                 $_SESSION["id"] = $details["id"];
130                 if(gwvp_IsUserAdmin($details["email"])) {
131                         $_SESSION["usertype"] = "admin";
132                 } else {
133                         $_SESSION["usertype"] = "user";
134                 }
135                 gwvp_SendMessage("info", "Welcome, ".$details["fullname"]." you are logged in");
136                 header("Location: $BASE_URL");
137                 return true;
138         }
139
140 }
141
142 function gwvp_RegistrationPageBody()
143 {
144         global $BASE_URL;
145         
146         // TODO: registration page needs to be prettier - mostly the image for the captcha
147         
148         ?>
149 <form method="post" action="<?php echo $BASE_URL?>/register/try">
150         <table>
151                 <tr>
152                         <td>Name</td>
153                         <td><input name="name" type="text"></td>
154                         <td>Your Full Name</td>
155                         <td rowspan="4">
156                                 <?php if(gwvp_haveCaptcha()) {?>
157                                 <img id="captcha" src="<?php echo $BASE_URL?>/securimage/" alt="CAPTCHA Image" /><br>
158                                 <input type="text" name="captcha_code" size="10" maxlength="6" />
159                                 <a href="#" onclick="document.getElementById('captcha').src = '<?php echo $BASE_URL?>/securimage/' + Math.random(); return false">[ Different Image ]</a>
160                                 <?php } ?>
161                         </td>
162                 </tr>
163                 <tr>
164                         <td>Email</td>
165                         <td><input name="email" type="text"></td>
166                         <td>Your Email Address</td>
167                 </tr>
168                 <tr>
169                         <td>User Name</td>
170                         <td><input name="username" type="text"></td>
171                         <td>The Name Used to Refer to you on the site</td>
172                 </tr>
173                 
174                 
175                 <tr>
176                         <td><input type="submit" name="register" value="Register"></td>
177                 </tr>
178         </table>
179 </form>
180         <?php
181 }
182
183 function qwvp_attemptRegistration()
184 {
185         if(gwvp_haveCaptcha()) {
186                 $securimage = new Securimage();
187                 if ($securimage->check($_POST['captcha_code']) == false) {
188                   // the code was incorrect
189                   // you should handle the error so that the form processor doesn't continue
190                 
191                   // or you can use the following code if there is no validation or you do not know how
192                   echo "The security code entered was incorrect.<br /><br />";
193                   echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
194                 } else {
195                         echo "code was right";
196                 }
197                 
198         }
199 }
200
201 function gwvp_checkBasicAuthLogin()
202 {
203         $user = false;
204         $pass = false;
205         if(isset($_SERVER["PHP_AUTH_USER"])) {
206                 $user = $_SERVER["PHP_AUTH_USER"];
207         } else return false;
208         
209         if(isset($_SERVER["PHP_AUTH_PW"])) {
210                 $pass = $_SERVER["PHP_AUTH_PW"];
211         } else return false;
212         
213         return gwvp_authUserPass($user, $pass);
214 }
215
216 function gwvp_IsLoggedIn()
217 {
218         if(isset($_SESSION["isloggedin"])) {
219                 if($_SESSION["isloggedin"]) {
220                         return $_SESSION["username"];
221                 } else return false;
222         } else return false;
223 }
224
225 function gwvp_SingleLineLoginForm()
226 {
227         global $BASE_URL;
228
229         echo "<form method=\"post\" action=\"$BASE_URL/login\">Username <input type=\"text\" name=\"username\" class=\"login\">";
230         echo " Passowrd <input type=\"text\" name=\"password\" class=\"login\"><input type=\"submit\" name=\"login\" value=\"Login\" class=\"loginbutton\">";
231         if(gwvp_IsRegistrationEnabled()) echo "<a href=\"$BASE_URL/register\">Register</a></form>";
232         else echo "</form><br>";
233 }
234
235
236 function gwvp_IsRegistrationEnabled()
237 {
238         return true;
239 }
240
241 // TODO translate info here
242 function gwvp_GetFullName($login)
243 {
244         return $login;
245 }
246 ?>