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