-
Notifications
You must be signed in to change notification settings - Fork 214
/
user.class.php
473 lines (473 loc) · 13.4 KB
/
user.class.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
/**
* Handler of the user as authenticated
*
* PHP version 5
*
* @category User
* @package FOGProject
* @author Tom Elliott <tommygunsster@gmail.com>
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link https://fogproject.org
*/
/**
* Handler of the user as authenticated
*
* @category User
* @package FOGProject
* @author Tom Elliott <tommygunsster@gmail.com>
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link https://fogproject.org
*/
class User extends FOGController
{
/**
* Stores the timeout value
*
* @var int
*/
private $_inactivitySessionTimeout;
/**
* Stores regeneration timeout
*
* @var int
*/
private $_regenerateSessionTimeout;
/**
* Stores if we should always be logged in
*
* @var bool
*/
private $_alwaysloggedin;
/**
* Was this already checked
*
* @var boot
*/
private $_checkedalready;
/**
* The session id
*
* @var string
*/
private $_sessionID;
/**
* The users table
*
* @var string
*/
protected $databaseTable = 'users';
/**
* The user table fields and common names
*
* @var array
*/
protected $databaseFields = array(
'id' => 'uId',
'name' => 'uName',
'password' => 'uPass',
'createdTime' => 'uCreateDate',
'createdBy' => 'uCreateBy',
'type' => 'uType'
);
/**
* The required fields
*
* @var array
*/
protected $databaseFieldsRequired = array(
'name',
'password',
);
/**
* The additional fields
*
* @var array
*/
protected $additionalFields = array(
'authID',
'authIP',
'authTime',
'authLastActivity',
'authUserAgent',
);
/**
* Generates an encrypted hash
*
* @param string $password the password
* @param int $cost cost of hash
*
* @return string
*/
public static function generateHash(
$password,
$cost = 11
) {
return password_hash(
$password,
PASSWORD_BCRYPT,
['cost'=>$cost]
);
}
/**
* Validates the users password and user
*
* @param string $username the username to test
* @param string $password the password to test
* @param string $adminTest the admin test
*
* @return bool
*/
public function passwordValidate(
$username,
$password,
$adminTest = false
) {
/**
* Test the username for funky characters and return
* immediately if found.
*/
$test = preg_match(
'/(?=^.{3,40}$)^[\w][\w0-9]*[._@-]?[\w0-9]*[.]?[\w0-9]+$/i',
$username
);
if (!$test) {
return false;
}
$tmpUser = self::getClass('User')
->set('name', $username)
->load('name');
$typeIsValid = true;
$type = $tmpUser->get('type');
self::$HookManager
->processEvent(
'USER_TYPE_VALID',
array(
'type' => &$type,
'typeIsValid' => &$typeIsValid
)
);
if (!$typeIsValid) {
return false;
}
if (preg_match('#^[a-f0-9]{32}$#i', $tmpUser->get('password'))
&& md5($password) === $tmpUser->get('password')
) {
$tmpUser
->set('password', $password)
->save();
}
$passValid = (bool)password_verify(
$password,
$tmpUser->get('password')
);
if ($adminTest === true) {
$tmpUser->get('type') > 0 ? $passValid = false : null;
}
return $passValid;
}
/**
* Validates only the user and password
*
* @param string $username the username
* @param string $password the password
*
* @return object
*/
public function validatePw(
$username,
$password
) {
/**
* Test the username for funky characters and return
* immediately if found.
*/
$test = preg_match(
'/(?=^.{3,40}$)^[\w][\w0-9]*[._@-]?[\w0-9]*[.]?[\w0-9]+$/i',
$username
);
if (!$test) {
return new self(0);
}
if ($this->passwordValidate($username, $password)) {
$tmpUser = self::getClass('User')
->set('name', $username)
->load('name');
if (preg_match(
'#^[a-f0-9]{32}$#i',
$tmpUser->get('password')
) && md5($password) === $tmpUser->get('password')
) {
$tmpUser->set('password', $password)->save();
}
$type = $tmpUser->get('type');
self::$HookManager
->processEvent(
'USER_TYPE_HOOK',
array('type' => &$type)
);
$this
->set('id', $tmpUser->get('id'))
->set('name', $tmpUser->get('name'))
->set('password', '', true)
->set('type', $type);
unset($tmpUser);
if (!$this->_sessionID) {
$this->_sessionID = session_id();
}
$this
->set('authUserAgent', $_SERVER['HTTP_USER_AGENT'])
->set('authIP', $_SERVER['REMOTE_ADDR'])
->set('authTime', time())
->set('authLastActivity', time())
->set('authID', $this->_sessionID);
$_SESSION['FOG_USER'] = $this->get('id');
$_SESSION['FOG_USERNAME'] = $this->get('name');
$this->log(
sprintf(
'%s %s.',
$this->get('name'),
_('user successfully logged in')
)
);
$this->_isLoggedIn();
} else {
self::$HookManager
->processEvent(
'USER_LOGGING_IN',
array(
'username' => $username,
'password' => $password
)
);
if (self::$FOGUser->isValid()) {
$type = self::$FOGUser->get('type');
self::$HookManager
->processEvent(
'USER_TYPE_HOOK',
array('type' => &$type)
);
$this
->set('id', self::$FOGUser->get('id'))
->set('name', self::$FOGUser->get('name'))
->set('password', '', true)
->set('type', $type);
if (!$this->_sessionID) {
$this->_sessionID = session_id();
}
$this
->set('authUserAgent', $_SERVER['HTTP_USER_AGENT'])
->set('authIP', $_SERVER['REMOTE_ADDR'])
->set('authTime', time())
->set('authLastActivity', time())
->set('authID', $this->_sessionID);
$_SESSION['FOG_USER'] = $this->get('id');
$_SESSION['FOG_USERNAME'] = $this->get('name');
$this->log(
sprintf(
'%s %s.',
$this->get('name'),
_('user successfully logged in')
)
);
$this->_isLoggedIn();
return $this;
}
$this->log(
sprintf(
'%s %s.',
$this->get('name'),
_('user failed to login'),
$this->get('name')
)
);
self::$EventManager->notify(
'LoginFail',
array('Failure' => $username)
);
self::$HookManager->processEvent(
'LoginFail',
array(
'username' => &$username,
'password' => &$password
)
);
$this->setMessage(self::$foglang['InvalidLogin']);
if (!isset($_SESSION['OBSOLETE'])) {
$_SESSION['OBSOLETE'] = true;
}
}
return $this;
}
/**
* Sets the passed value
*
* @param string $key the key to set
* @param mixed $value the value to set
* @param bool $override to override the setter
*
* @return object
*/
public function set(
$key,
$value,
$override = false
) {
if ($this->key($key) == 'password'
&& !$override
) {
$value = self::generateHash($value);
}
return parent::set($key, $value);
}
/**
* Tests if an object is valid
*
* @return bool
*/
public function isValid()
{
if ($this->get('id') < 1) {
return false;
}
if (!$this->get('name')) {
return false;
}
return true;
}
/**
* Tests if user is logged in
*
* @return bool
*/
private function _isLoggedIn()
{
if (!$this->isValid()) {
return false;
}
if (!$this->_checkedalready) {
list(
$this->_alwaysloggedin,
$this->_inactivitySessionTimeout,
$this->_regenerateSessionTimeout,
) = self::getSubObjectIDs(
'Service',
array(
'name' => array(
'FOG_ALWAYS_LOGGED_IN',
'FOG_INACTIVITY_TIMEOUT',
'FOG_REGENERATE_TIMEOUT',
)
),
'value',
false,
'AND',
'name',
false,
''
);
$this->_checkedalready = true;
}
$_SESSION['OBSOLETE'] = false;
if (!$this->get('authIP')
|| !$this->get('authUserAgent')
) {
return false;
} elseif ($this->get('authIP')
&& $this->get('authIP') != $_SERVER['REMOTE_ADDR']
) {
if (!$_SESSION['FOG_MESSAGES']) {
$this->setMessage(_('IP Address Changed'));
}
if (isset($_SESSION['OBSOLETE'])) {
$_SESSION['OBSOLETE'] = true;
}
} elseif ($this->get('authUserAgent')
&& $this->get('authUserAgent') != $_SERVER['HTTP_USER_AGENT']
) {
if (!$_SESSION['FOG_MESSAGES']) {
$this->setMessage(_('User Agent Changed'));
}
if (isset($_SESSION['OBSOLETE'])) {
$_SESSION['OBSOLETE'] = true;
}
} elseif ($this->get('authID')
&& $this->_sessionID != $this->get('authID')
) {
if (!$_SESSION['FOG_MESSAGES']) {
$this->setMessage(_('Session altered improperly'));
}
if (isset($_SESSION['OBSOLETE'])) {
$_SESSION['OBSOLETE'] = true;
}
} elseif ($this->get('authLastActivity')
&& !$this->_alwaysloggedin
) {
$active = time() - $this->get('authLastActivity');
$timeout = $this->_inactivitySessionTimeout * 60 * 60;
if ($active >= $timeout) {
$this->setMessage(self::$foglang['SessionTimeout']);
if (isset($_SESSION['OBSOLETE'])) {
$_SESSION['OBSOLETE'] = true;
}
}
}
if (isset($_SESSION['OBSOLETE'])
&& $_SESSION['OBSOLETE']
) {
$_SESSION['OBSOLETE'] = false;
$this->redirect('index.php?node=logout');
}
$authTime = time() - $this->get('authTime');
$regenTime = $this->_regenerateSessionTimeout * 60 * 60;
if ($authTime > $regenTime) {
session_regenerate_id(false);
$this->_sessionID = session_id();
session_write_close();
session_start();
session_id($this->_sessionID);
$this
->set('authID', $this->_sessionID)
->set('authTime', time());
}
$this->set('authLastActivity', time());
if (!isset($_SESSION['FOG_USER'])) {
$_SESSION['FOG_USER'] = $this->get('id');
$_SESSION['FOG_USERNAME'] = $this->get('name');
}
return true;
}
/**
* Perform logout cleanup
*
* @return void
*/
public function logout()
{
$this
->set('id', 0)
->set('name', '')
->set('password', '', '');
if (session_status() == PHP_SESSION_NONE) {
return;
}
$locale = $_SESSION['locale'];
$messages = $_SESSION['FOG_MESSAGES'];
// Destroy session
unset($this->_sessionID);
$this
->set('authID', null)
->set('authIP', null)
->set('authTime', null)
->set('authLastActivity', null);
session_unset();
session_destroy();
session_write_close();
session_start();
$_SESSION=array();
$_SESSION['locale'] = $locale;
if (isset($messages)) {
$this->setMessage($messages);
}
}
}