-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass.auth.php
301 lines (249 loc) · 9.19 KB
/
class.auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?PHP
class Auth
{
// Singleton object. Leave $me alone.
private static $me;
public $id;
public $username;
public $level;
public $user; // DBObject User object (if available)
public $loggedIn;
// Call with no arguments to attempt to restore a previous logged in session
// which then falls back to a guest user (which can then be logged in using
// $this->login($un, $pw). Or pass a user_id to simply login that user. The
// $seriously is just a safeguard to be certain you really do want to blindly
// login a user. Set it to true.
private function __construct($user_to_impersonate = null)
{
$this->id = null;
$this->username = null;
$this->level = 'free user';
$this->user = null;
$this->loggedIn = false;
if (class_exists('User') && (is_subclass_of('User', 'DBObject')))
$this->user = new User();
if (!is_null($user_to_impersonate))
return $this->impersonate($user_to_impersonate);
if ($this->attemptSessionLogin())
return;
if ($this->attemptCookieLogin())
return;
}
/**
* Standard singleton
* @return Auth
*/
public static function getAuth($user_to_impersonate = null)
{
if (is_null(self::$me))
self::$me = new Auth($user_to_impersonate);
return self::$me;
}
// You'll typically call this function when a user logs in using
// a form. Pass in their username and password.
// Takes a username and a *plain text* password
public function login($un, $pw)
{
$pw = $this->createHashedPassword($pw);
return $this->attemptLogin($un, $pw);
}
public function logout()
{
$Config = Config::getConfig();
$this->id = null;
$this->username = null;
$this->level = 'guest';
$this->user = null;
$this->loggedIn = false;
if (class_exists('User') && (is_subclass_of('User', 'DBObject')))
$this->user = new User();
$_SESSION['un'] = '';
$_SESSION['pw'] = '';
unset($_SESSION['un']);
unset($_SESSION['pw']);
setcookie('spf', '.', time() - 3600, '/', $Config->authDomain);
}
// Assumes you have already checked for duplicate usernames
public function changeUsername($new_username)
{
$db = Database::getDatabase();
$db->query('UPDATE users SET username = :username WHERE id = :id', array('username' => $new_username, 'id' => $this->id));
if ($db->affectedRows() == 1)
{
$this->impersonate($this->id);
return true;
}
return false;
}
public function changePassword($new_password)
{
$db = Database::getDatabase();
$Config = Config::getConfig();
if ($Config->useHashedPasswords === true)
$new_password = $this->createHashedPassword($new_password);
$db->query('UPDATE users SET password = :password WHERE id = :id', array('password' => $new_password, 'id' => $this->id));
if ($db->affectedRows() == 1)
{
$this->impersonate($this->id);
return true;
}
return false;
}
// Is a user logged in? This was broken out into its own function
// in case extra logic is ever required beyond a simple bool value.
public function loggedIn()
{
return $this->loggedIn;
}
// Helper function that redirects away from 'admin only' pages
public function requireAdmin($url)
{
if (!$this->loggedIn() || $this->level != 'admin')
{
$validLogin = false;
/* if there is an attempt to login */
if (($_REQUEST['username']) && ($_REQUEST['password']))
{
if ($this->login($_REQUEST['username'], $_REQUEST['password']))
{
if ($this->level == 'admin')
{
$validLogin = true;
}
}
else
{
redirect("login.php?error=1");
}
}
if ($validLogin == false)
{
redirect("login.php");
}
}
}
// Helper function that redirects away from 'member only' pages
public function requireUser($url)
{
if (!$this->loggedIn())
redirect($url);
}
// Check if the submitted password matches what we have on file.
// Takes a *plain text* password
public function passwordIsCorrect($pw)
{
$db = Database::getDatabase();
$Config = Config::getConfig();
if ($Config->useHashedPasswords === true)
$pw = $this->createHashedPassword($pw);
$db->query('SELECT COUNT(*) FROM users WHERE username = :username AND password = BINARY :password', array('username' => $this->username, 'password' => $pw));
return $db->getValue() == 1;
}
// Login a user simply by passing in their username or id. Does
// not check against a password. Useful for allowing an admin user
// to temporarily login as a standard user for troubleshooting.
// Takes an id or username
public function impersonate($user_to_impersonate)
{
$db = Database::getDatabase();
$Config = Config::getConfig();
if (ctype_digit($user_to_impersonate))
$row = $db->getRow('SELECT * FROM users WHERE id = ' . $db->quote($user_to_impersonate));
else
$row = $db->getRow('SELECT * FROM users WHERE username = ' . $db->quote($user_to_impersonate));
if (is_array($row))
{
$this->id = $row['id'];
$this->username = $row['username'];
$this->level = $row['level'];
// Load any additional user info if DBObject and User are available
if (class_exists('User') && (is_subclass_of('User', 'DBObject')))
{
$this->user = new User();
$this->user->id = $row['id'];
$this->user->load($row);
}
if ($Config->useHashedPasswords === false)
$row['password'] = $this->createHashedPassword($row['password']);
$this->storeSessionData($this->username, $row['password']);
$this->loggedIn = true;
return true;
}
return false;
}
// Attempt to login using data stored in the current session
private function attemptSessionLogin()
{
if (isset($_SESSION['un']) && isset($_SESSION['pw']))
return $this->attemptLogin($_SESSION['un'], $_SESSION['pw'], true);
else
return false;
}
// Attempt to login using data stored in a cookie
private function attemptCookieLogin()
{
if (isset($_COOKIE['spf']) && is_string($_COOKIE['spf']))
{
$s = json_decode($_COOKIE['spf'], true);
if (isset($s['un']) && isset($s['pw']))
{
return $this->attemptLogin($s['un'], $s['pw']);
}
}
return false;
}
// The function that actually verifies an attempted login and
// processes it if successful.
// Takes a username and a *hashed* password
private function attemptLogin($un, $pw, $sessionLogin = false)
{
$db = Database::getDatabase();
$Config = Config::getConfig();
// We SELECT * so we can load the full user record into the user DBObject later
$row = $db->getRow('SELECT * FROM users WHERE username = ' . $db->quote($un));
if ($row === false)
return false;
if ($Config->useHashedPasswords === false)
$row['password'] = $this->createHashedPassword($row['password']);
if ($pw != $row['password'])
return false;
if ($row['status'] != "active")
return false;
$this->id = $row['id'];
$this->username = $row['username'];
$this->email = $row['email'];
$this->level = $row['level'];
$this->paidExpiryDate = $row['paidExpiryDate'];
$this->paymentTracker = $row['paymentTracker'];
// Load any additional user info if DBObject and User are available
if (class_exists('User') && (is_subclass_of('User', 'DBObject')))
{
$this->user = new User();
$this->user->id = $row['id'];
$this->user->load($row);
}
/* update lastlogindate */
if ($sessionLogin == false)
{
$db->query('UPDATE users SET lastlogindate = NOW(), lastloginip = :ip WHERE id = :id', array('ip' => $_SERVER['REMOTE_ADDR'], 'id' => $this->id));
}
$this->storeSessionData($un, $pw);
$this->loggedIn = true;
return true;
}
// Takes a username and a *hashed* password
private function storeSessionData($un, $pw)
{
if (headers_sent ())
return false;
$Config = Config::getConfig();
$_SESSION['un'] = $un;
$_SESSION['pw'] = $pw;
$s = json_encode(array('un' => $un, 'pw' => $pw));
return setcookie('spf', $s, time() + 60 * 60 * 24 * 30, '/', $Config->authDomain);
}
private function createHashedPassword($pw)
{
return MD5($pw);
}
}