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