login: store passwords in separate user files
[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         # find password data by user name
10         $pwfile = sprintf('login/%s/.passwd', strtolower($inuser));
11         if (!file_exists($pwfile)) return;
12         $usertest = trim(file_get_contents($pwfile));
13         if (!$usertest) return;
14
15         # verify password
16         $authhash = md5($usertest);
17         if (isset($inpass)) {
18                 if (!password_verify($inpass, $usertest)) return;
19         }
20         else {
21                 if ($inauth !== $authhash) return;
22         }
23
24         return [
25                 'name'  => $inuser,
26                 'admin' => !empty($inuser) && strtolower($inuser) != 'lid',
27                 'auth'  => "$inuser:$authhash",
28         ];
29 }
30
31 if (isset($_COOKIE['login'])) {
32         global $User;
33         $User = login($_COOKIE['login']);
34 }
35