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