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