auth: append login to formatted user names
[minimedit.git] / auth.inc.php
1 <?php
2 date_default_timezone_set('Europe/Amsterdam');
3
4 class User
5 {
6         function __construct($dir, $existing = TRUE)
7         {
8                 if (!file_exists($dir) and $existing) {
9                         throw new Exception("Gebruiker niet gevonden in $dir");
10                 }
11                 $this->dir = $dir;
12                 $this->login = basename($dir);
13         }
14
15         function __get($col)
16         {
17                 return $this->$col = $this->$col();  # run method and cache
18         }
19
20         function rawname()
21         {
22                 return rtrim(@file_get_contents("{$this->dir}/name.txt"));
23         }
24
25         function name()
26         {
27                 return htmlspecialchars(implode(' & ', explode("\n", $this->rawname)));
28         }
29
30         function html()
31         {
32                 $name = htmlspecialchars($this->login);
33                 if ($this->name and $this->name != $name) {
34                         $name = "{$this->name}<small> @$name</small>";
35                 }
36                 return $name;
37         }
38
39         function email()
40         {
41                 return rtrim(@file_get_contents("{$this->dir}/email.txt"));
42         }
43
44         function admin($permission = NULL)
45         {
46                 if (isset($permission)) {
47                         if (!$this->admin) {
48                                 return FALSE;  # empty results
49                         }
50                         preg_match_all('{[ /]}', $permission, $parts, PREG_OFFSET_CAPTURE);
51                         foreach ($parts[0] as $part) {
52                                 if (isset($this->admin[substr($permission, 0, $part[1])])) {
53                                         return TRUE;  # partial match
54                                 }
55                         }
56                         return isset($this->admin[$permission]);  # check level
57                 }
58                 if (!@file_exists("{$this->dir}/.admin")) {
59                         return FALSE;  # not an admin
60                 }
61                 return array_fill_keys(explode("\n", file_get_contents("{$this->dir}/.admin")), TRUE);
62         }
63
64         function seen()
65         {
66                 return @filemtime("{$this->dir}/last.log");
67         }
68
69         function logclient()
70         {
71                 if ($log = @fopen("{$this->dir}/last.log", 'w')) {
72                         $line = $_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'];
73                         fwrite($log, $line."\n");
74                 }
75         }
76 }
77
78 function login_password_verify($input, $test)
79 {
80         if (substr($test, 0, 1) != '$') {
81                 # plaintext match for uncrypted passwords
82                 return $input === $test;
83         }
84         return password_verify($input, $test);
85 }
86
87 function login_setcookie()
88 {
89         global $User;
90         return setcookie('login', $User->auth, 0, '/');
91 }
92
93 function login($inuser, $inpass = NULL)
94 {
95         if (empty($inuser)) return;
96         if (!isset($inpass)) {
97                 @list ($inuser, $inauth) = explode(':', $inuser, 2);
98         }
99
100         # find password data by user name
101         $userdir = 'profile/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($inuser));
102         $pwfile = "$userdir/.passwd";
103         if (!file_exists($pwfile)) return;
104         $usertest = trim(file_get_contents($pwfile));
105         if (!$usertest) return;
106
107         # verify password
108         $authhash = md5($usertest);
109         if (isset($inpass)) {
110                 if (!login_password_verify($inpass, $usertest)) return;
111         }
112         else {
113                 if ($inauth !== $authhash) return;
114         }
115
116         if (function_exists('apache_note')) apache_note('user', $inuser);
117
118         $user = new User($userdir);
119         $user->logclient();
120         $user->pass = $usertest;
121         $user->auth = "$inuser:$authhash";
122         return $user;
123 }
124
125 if (isset($_COOKIE['login'])) {
126         global $User;
127         $User = login($_COOKIE['login']);
128 }
129