auth: admin edit permissions for page (sub)directories
[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                 return $this->name ?: $this->login;
33         }
34
35         function email()
36         {
37                 return rtrim(@file_get_contents("{$this->dir}/email.txt"));
38         }
39
40         function admin($permission = NULL)
41         {
42                 if (isset($permission)) {
43                         if (!$this->admin) {
44                                 return FALSE;  # empty results
45                         }
46                         preg_match_all('{[ /]}', $permission, $parts, PREG_OFFSET_CAPTURE);
47                         foreach ($parts[0] as $part) {
48                                 if (isset($this->admin[substr($permission, 0, $part[1])])) {
49                                         return TRUE;  # partial match
50                                 }
51                         }
52                         return isset($this->admin[$permission]);  # check level
53                 }
54                 if (!@file_exists("{$this->dir}/.admin")) {
55                         return FALSE;  # not an admin
56                 }
57                 return array_fill_keys(explode("\n", file_get_contents("{$this->dir}/.admin")), TRUE);
58         }
59
60         function seen()
61         {
62                 return @filemtime("{$this->dir}/last.log");
63         }
64
65         function logclient()
66         {
67                 if ($log = @fopen("{$this->dir}/last.log", 'w')) {
68                         $line = $_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'];
69                         fwrite($log, $line."\n");
70                 }
71         }
72 }
73
74 function login_password_verify($input, $test)
75 {
76         if (substr($test, 0, 1) != '$') {
77                 # plaintext match for uncrypted passwords
78                 return $input === $test;
79         }
80         return password_verify($input, $test);
81 }
82
83 function login_setcookie()
84 {
85         global $User;
86         return setcookie('login', $User->auth, 0, '/');
87 }
88
89 function login($inuser, $inpass = NULL)
90 {
91         if (empty($inuser)) return;
92         if (!isset($inpass)) {
93                 @list ($inuser, $inauth) = explode(':', $inuser, 2);
94         }
95
96         # find password data by user name
97         $userdir = 'profile/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($inuser));
98         $pwfile = "$userdir/.passwd";
99         if (!file_exists($pwfile)) return;
100         $usertest = trim(file_get_contents($pwfile));
101         if (!$usertest) return;
102
103         # verify password
104         $authhash = md5($usertest);
105         if (isset($inpass)) {
106                 if (!login_password_verify($inpass, $usertest)) return;
107         }
108         else {
109                 if ($inauth !== $authhash) return;
110         }
111
112         if (function_exists('apache_note')) apache_note('user', $inuser);
113
114         $user = new User($userdir);
115         $user->logclient();
116         $user->pass = $usertest;
117         $user->auth = "$inuser:$authhash";
118         return $user;
119 }
120
121 if (isset($_COOKIE['login'])) {
122         global $User;
123         $User = login($_COOKIE['login']);
124 }
125