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