made my star image a bit nicer
[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                 $_SESSION["isloggedin"] = true;
126                 $_SESSION["username"] = "$user";
127                 $_SESSION["fullname"] = $details["fullname"];
128                 if(gwvp_IsUserAdmin($details["email"])) {
129                         $_SESSION["usertype"] = "admin";
130                 } else {
131                         $_SESSION["usertype"] = "user";
132                 }
133                 gwvp_SendMessage("info", "Welcome, ".$details["fullname"]." you are logged in");
134                 header("Location: $BASE_URL");
135                 return true;
136         }
137
138 }
139
140 function gwvp_RegistrationPageBody()
141 {
142         global $BASE_URL;
143         
144         // TODO: registration page needs to be prettier - mostly the image for the captcha
145         
146         ?>
147 <form method="post" action="<?php echo $BASE_URL?>/register/try">
148         <table>
149                 <tr>
150                         <td>Name</td>
151                         <td><input name="name" type="text"></td>
152                         <td>Your Full Name</td>
153                         <td rowspan="4">
154                                 <?php if(gwvp_haveCaptcha()) {?>
155                                 <img id="captcha" src="<?php echo $BASE_URL?>/securimage/" alt="CAPTCHA Image" /><br>
156                                 <input type="text" name="captcha_code" size="10" maxlength="6" />
157                                 <a href="#" onclick="document.getElementById('captcha').src = '<?php echo $BASE_URL?>/securimage/' + Math.random(); return false">[ Different Image ]</a>
158                                 <?php } ?>
159                         </td>
160                 </tr>
161                 <tr>
162                         <td>Email</td>
163                         <td><input name="email" type="text"></td>
164                         <td>Your Email Address</td>
165                 </tr>
166                 <tr>
167                         <td>User Name</td>
168                         <td><input name="username" type="text"></td>
169                         <td>The Name Used to Refer to you on the site</td>
170                 </tr>
171                 
172                 
173                 <tr>
174                         <td><input type="submit" name="register" value="Register"></td>
175                 </tr>
176         </table>
177 </form>
178         <?php
179 }
180
181 function qwvp_attemptRegistration()
182 {
183         if(gwvp_haveCaptcha()) {
184                 $securimage = new Securimage();
185                 if ($securimage->check($_POST['captcha_code']) == false) {
186                   // the code was incorrect
187                   // you should handle the error so that the form processor doesn't continue
188                 
189                   // or you can use the following code if there is no validation or you do not know how
190                   echo "The security code entered was incorrect.<br /><br />";
191                   echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
192                 } else {
193                         echo "code was right";
194                 }
195                 
196         }
197 }
198
199 function gwvp_checkBasicAuthLogin()
200 {
201         $user = false;
202         $pass = false;
203         if(isset($_SERVER["PHP_AUTH_USER"])) {
204                 $user = $_SERVER["PHP_AUTH_USER"];
205         } else return false;
206         
207         if(isset($_SERVER["PHP_AUTH_PW"])) {
208                 $pass = $_SERVER["PHP_AUTH_PW"];
209         } else return false;
210         
211         return gwvp_authUserPass($user, $pass);
212 }
213
214 function gwvp_IsLoggedIn()
215 {
216         if(isset($_SESSION["isloggedin"])) {
217                 if($_SESSION["isloggedin"]) {
218                         return $_SESSION["username"];
219                 } else return false;
220         } else return false;
221 }
222
223 function gwvp_SingleLineLoginForm()
224 {
225         global $BASE_URL;
226
227         echo "<form method=\"post\" action=\"$BASE_URL/login\">Username <input type=\"text\" name=\"username\" class=\"login\">";
228         echo " Passowrd <input type=\"text\" name=\"password\" class=\"login\"><input type=\"submit\" name=\"login\" value=\"Login\" class=\"loginbutton\">";
229         if(gwvp_IsRegistrationEnabled()) echo "<a href=\"$BASE_URL/register\">Register</a></form>";
230         else echo "</form>";
231 }
232
233
234 function gwvp_IsRegistrationEnabled()
235 {
236         return true;
237 }
238
239 // TODO translate info here
240 function gwvp_GetFullName($login)
241 {
242         return $login;
243 }
244 ?>