login: replace page editability var by admin status
[minimedit.git] / auth.inc.php
1 <?php
2 global $User, $Admin;
3
4 call_user_func(function () {
5         if (isset($_SERVER['PHP_AUTH_USER'])) {
6                 $authinfo = [ $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ];
7         }
8         elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
9                 // cgi compatibility
10                 $authinfo = explode(':' , base64_decode(substr($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 6)));
11         }
12         else {
13                 return;
14         }
15
16         $pwdata = file_get_contents(__DIR__.'/.htpasswd');
17         $pwlist = [];
18         foreach (explode("\n", $pwdata) as $line) {
19                 if (!$line) continue;
20                 list ($username, $pass) = explode(':', $line);
21                 $pwlist[$username] = $pass;
22         }
23
24         list ($authname, $authpass) = $authinfo;
25         $usertest = $pwlist[ strtolower($authname) ];
26         if (!$usertest) return;
27
28         $salt = substr($usertest, 0, 2);
29         if (crypt($authpass, $salt) != $usertest) return;
30
31         global $User, $Admin;
32         $User = $authname;
33         $Admin = !empty($User) && $User != 'lid' ? $User : FALSE;
34 });
35