nieuws: article lists in left layout with images
[minimedit.git] / auth.inc.php
1 <?php
2 date_default_timezone_set('Europe/Amsterdam');
3
4 class User
5 {
6         function __construct($dir)
7         {
8                 $this->dir = $dir;
9                 $this->login = basename($dir);
10         }
11
12         function __get($col)
13         {
14                 return $this->$col = $this->$col();  # run method and cache
15         }
16
17         function name()
18         {
19                 $name = @file_get_contents("{$this->dir}/name.txt");
20                 return htmlspecialchars(implode(' & ', explode("\n", $name)));
21         }
22
23         function admin()
24         {
25                 return @file_exists("{$this->dir}/.admin");
26         }
27
28         function seen()
29         {
30                 return @filemtime("{$this->dir}/last.log");
31         }
32 }
33
34 function login_password_verify($input, $test)
35 {
36         if (substr($test, 0, 1) != '$') {
37                 # plaintext match for uncrypted passwords
38                 return $input === $test;
39         }
40         return password_verify($input, $test);
41 }
42
43 function login_setcookie()
44 {
45         global $User;
46         return setcookie('login', $User['auth'], 0, '/');
47 }
48
49 function login($inuser, $inpass = NULL)
50 {
51         if (empty($inuser)) return;
52         if (!isset($inpass)) {
53                 @list ($inuser, $inauth) = explode(':', $inuser, 2);
54         }
55
56         # find password data by user name
57         $userdir = 'profile/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($inuser));
58         $pwfile = "$userdir/.passwd";
59         if (!file_exists($pwfile)) return;
60         $usertest = trim(file_get_contents($pwfile));
61         if (!$usertest) return;
62
63         # verify password
64         $authhash = md5($usertest);
65         if (isset($inpass)) {
66                 if (!login_password_verify($inpass, $usertest)) return;
67         }
68         else {
69                 if ($inauth !== $authhash) return;
70         }
71
72         if (function_exists('apache_note')) apache_note('user', $inuser);
73
74         if ($log = @fopen("$userdir/last.log", 'w')) {
75                 fwrite($log, "{$_SERVER['REMOTE_ADDR']} {$_SERVER['HTTP_USER_AGENT']}\n");
76         }
77
78         return [
79                 'name'  => $inuser,
80                 'dir'   => $userdir,
81                 'admin' => file_exists("$userdir/.admin"),
82                 'pass'  => $usertest,
83                 'auth'  => "$inuser:$authhash",
84         ];
85 }
86
87 if (isset($_COOKIE['login'])) {
88         global $User;
89         $User = login($_COOKIE['login']);
90 }
91