edit: replace executable check by filename validation
[minimedit.git] / auth.inc.php
1 <?php
2 function login($inuser, $inpass = NULL)
3 {
4         if (empty($inuser)) return;
5         if (!isset($inpass)) {
6                 @list ($inuser, $inauth) = explode(':', $inuser, 2);
7         }
8
9         # create pwlist table from htpasswd
10         $pwdata = file_get_contents('./.htpasswd');
11         $pwlist = [];
12         foreach (explode("\n", $pwdata) as $line) {
13                 if (!$line) continue;
14                 list ($username, $pass) = explode(':', $line);
15                 $pwlist[$username] = $pass;
16         }
17
18         # find user by name
19         $usertest = @$pwlist[ strtolower($inuser) ];
20         if (!$usertest) return;
21
22         # verify password
23         $authhash = md5($usertest);
24         if (isset($inpass)) {
25                 if (!password_verify($inpass, $usertest)) return;
26         }
27         else {
28                 if ($inauth !== $authhash) return;
29         }
30
31         return [
32                 'name'  => $inuser,
33                 'admin' => !empty($inuser) && strtolower($inuser) != 'lid',
34                 'auth'  => "$inuser:$authhash",
35         ];
36 }
37
38 if (isset($_COOKIE['login'])) {
39         global $User;
40         $User = login($_COOKIE['login']);
41 }
42