edit: distinct admin template for missing pages
[minimedit.git] / auth.inc.php
index 6a5d1d2d83458fc1f84f56dd96093c1cb36c0c5b..363fc71538b0527be14ac901fe3fb9c99692c604 100755 (executable)
@@ -1,20 +1,13 @@
 <?php
-global $User, $editable;
-$User = FALSE;
-
-function Auth() {
-       if (isset($_SERVER['PHP_AUTH_USER'])) {
-               $authinfo = [ $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ];
-       }
-       elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
-               // cgi compatibility
-               $authinfo = explode(':' , base64_decode(substr($_SERVER['REDIRECT_HTTP_AUTHORIZATION'], 6)));
-       }
-       else {
-               return;
+function login($inuser, $inpass = NULL)
+{
+       if (empty($inuser)) return;
+       if (!isset($inpass)) {
+               @list ($inuser, $inauth) = explode(':', $inuser, 2);
        }
 
-       $pwdata = file_get_contents(__DIR__.'/.htpasswd');
+       # create pwlist table from htpasswd
+       $pwdata = file_get_contents('./.htpasswd');
        $pwlist = [];
        foreach (explode("\n", $pwdata) as $line) {
                if (!$line) continue;
@@ -22,17 +15,28 @@ function Auth() {
                $pwlist[$username] = $pass;
        }
 
-       list ($authname, $authpass) = $authinfo;
-       $usertest = $pwlist[ strtolower($authname) ];
+       # find user by name
+       $usertest = @$pwlist[ strtolower($inuser) ];
        if (!$usertest) return;
 
-       $salt = substr($usertest, 0, 2);
-       if (crypt($authpass, $salt) != $usertest) return;
+       # verify password
+       $authhash = md5($usertest);
+       if (isset($inpass)) {
+               if (!password_verify($inpass, $usertest)) return;
+       }
+       else {
+               if ($inauth !== $authhash) return;
+       }
 
-       $GLOBALS['User'] = $authname;
+       return [
+               'name'  => $inuser,
+               'admin' => !empty($inuser) && strtolower($inuser) != 'lid',
+               'auth'  => "$inuser:$authhash",
+       ];
 }
 
-Auth();
-
-$editable = !empty($User) && $User != 'lid';
+if (isset($_COOKIE['login'])) {
+       global $User;
+       $User = login($_COOKIE['login']);
+}