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