auth: support unhashed passwords
[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         $userdir = 'login/'.strtolower($inuser);
11         $pwfile = "$userdir/.passwd";
12         if (!file_exists($pwfile)) return;
13         $usertest = trim(file_get_contents($pwfile));
14         if (!$usertest) return;
15
16         # verify password
17         $authhash = md5($usertest);
18         if (isset($inpass)) {
19                 if (substr($usertest, 0, 1) == '$') {
20                         if (!password_verify($inpass, $usertest)) return;
21                 }
22                 else {
23                         if ($inpass !== $usertest) return;
24                 }
25         }
26         else {
27                 if ($inauth !== $authhash) return;
28         }
29
30         if (function_exists('apache_note')) apache_note('user', $inuser);
31
32         if ($log = @fopen("$userdir/last.log", 'w')) {
33                 fwrite($log, "{$_SERVER['REMOTE_ADDR']} {$_SERVER['HTTP_USER_AGENT']}\n");
34         }
35
36         return [
37                 'name'  => $inuser,
38                 'admin' => file_exists("$userdir/.admin"),
39                 'auth'  => "$inuser:$authhash",
40         ];
41 }
42
43 if (isset($_COOKIE['login'])) {
44         global $User;
45         $User = login($_COOKIE['login']);
46 }
47