|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Creates a new user object. |
|
4
|
|
|
* |
|
5
|
|
|
* A user are recognized by the session-id using getUserBySessionId(), by his |
|
6
|
|
|
* using getUserById() or by his nickname (login) using getUserByLogin(). New |
|
7
|
|
|
* are created using createNewUser(). |
|
8
|
|
|
* |
|
9
|
|
|
* PHP Version 5.4 |
|
10
|
|
|
* |
|
11
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
|
12
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
|
13
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
|
14
|
|
|
* |
|
15
|
|
|
* @category phpMyFAQ |
|
16
|
|
|
* @package User |
|
17
|
|
|
* @author Lars Tiedemann <[email protected]> |
|
18
|
|
|
* @author Thorsten Rinne <[email protected]> |
|
19
|
|
|
* @author Sarah Hermann <[email protected]> |
|
20
|
|
|
* @copyright 2005-2014 phpMyFAQ Team |
|
21
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
|
22
|
|
|
* @link http://www.phpmyfaq.de |
|
23
|
|
|
* @since 2005-09-17 |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
|
27
|
|
|
exit(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (!defined('PMF_ENCRYPTION_TYPE')) { |
|
31
|
|
|
define('PMF_ENCRYPTION_TYPE', 'md5'); // Fallback to md5() |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* User |
|
36
|
|
|
* |
|
37
|
|
|
* @category phpMyFAQ |
|
38
|
|
|
* @package User |
|
39
|
|
|
* @author Lars Tiedemann <[email protected]> |
|
40
|
|
|
* @author Thorsten Rinne <[email protected]> |
|
41
|
|
|
* @author Sarah Hermann <[email protected]> |
|
42
|
|
|
* @copyright 2005-2014 phpMyFAQ Team |
|
43
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
|
44
|
|
|
* @link http://www.phpmyfaq.de |
|
45
|
|
|
* @since 2005-09-17 |
|
46
|
|
|
*/ |
|
47
|
|
|
class PMF_User |
|
48
|
|
|
{ |
|
49
|
|
|
const ERROR_UNDEFINED_PARAMETER = 'Following parameter must to be defined: '; |
|
50
|
|
|
const ERROR_USER_ADD = 'Account could not be created. '; |
|
51
|
|
|
const ERROR_USER_CANNOT_CREATE_USER = 'User account could not be created. '; |
|
52
|
|
|
const ERROR_USER_CANNOT_CREATE_USERDATA = 'Entry for user data could not be created. '; |
|
53
|
|
|
const ERROR_USER_CANNOT_DELETE_USER = 'User account could not be deleted. '; |
|
54
|
|
|
const ERROR_USER_CANNOT_DELETE_USERDATA = 'Entry for user data could not be deleted. '; |
|
55
|
|
|
const ERROR_USER_CANNOT_UPDATE_USERDATA = 'Entry for user data could not be updated. '; |
|
56
|
|
|
const ERROR_USER_CHANGE = 'Account could not be updated. '; |
|
57
|
|
|
const ERROR_USER_DELETE = 'Account could not be deleted. '; |
|
58
|
|
|
const ERROR_USER_INCORRECT_LOGIN = 'Specified login could not be found. '; |
|
59
|
|
|
const ERROR_USER_INCORRECT_PASSWORD = 'Specified password is not correct.'; |
|
60
|
|
|
const ERROR_USER_INVALID_STATUS = 'Undefined user status.'; |
|
61
|
|
|
const ERROR_USER_LOGINNAME_TOO_SHORT = 'The chosen loginname is too short.'; |
|
62
|
|
|
const ERROR_USER_LOGIN_NOT_UNIQUE = 'Specified login name already exists. '; |
|
63
|
|
|
const ERROR_USER_LOGIN_INVALID = 'The chosen login is invalid. A valid login has at least four characters. Only letters, numbers and underscore _ are allowed. The first letter must be a letter. '; |
|
64
|
|
|
const ERROR_USER_NO_AUTH = 'No authentication method specified. '; |
|
65
|
|
|
const ERROR_USER_NO_DB = 'No database specified.'; |
|
66
|
|
|
const ERROR_USER_NO_PERM = 'No permission container specified.'; |
|
67
|
|
|
const ERROR_USER_NO_USERID = 'No user-ID found. '; |
|
68
|
|
|
const ERROR_USER_NO_USERLOGINDATA = 'No user login data found. '; |
|
69
|
|
|
const ERROR_USER_NOT_FOUND = 'User account could not be found. '; |
|
70
|
|
|
const ERROR_USER_NOWRITABLE = 'No authentication object is writable. '; |
|
71
|
|
|
const ERROR_USER_NO_LOGIN_DATA = 'A username and password must be provided. '; |
|
72
|
|
|
|
|
73
|
|
|
const STATUS_USER_PROTECTED = 'User account is protected. '; |
|
74
|
|
|
const STATUS_USER_BLOCKED = 'User account is blocked. '; |
|
75
|
|
|
const STATUS_USER_ACTIVE = 'User account is active. '; |
|
76
|
|
|
|
|
77
|
|
|
// --- ATTRIBUTES --- |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Permission container |
|
81
|
|
|
* |
|
82
|
|
|
* @var PMF_Perm_Basic|PMF_Perm_Medium |
|
83
|
|
|
*/ |
|
84
|
|
|
public $perm = null; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* User-data storage container |
|
88
|
|
|
* |
|
89
|
|
|
* @var PMF_User_UserData |
|
90
|
|
|
*/ |
|
91
|
|
|
public $userdata = null; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Default Authentication properties |
|
95
|
|
|
* |
|
96
|
|
|
* @var array |
|
97
|
|
|
*/ |
|
98
|
|
|
private $authData = array( |
|
99
|
|
|
'authSource' => array( |
|
100
|
|
|
'name' => 'db', |
|
101
|
|
|
'type' => 'local' |
|
102
|
|
|
), |
|
103
|
|
|
'encType' => PMF_ENCRYPTION_TYPE, |
|
104
|
|
|
'readOnly' => false |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Public array that contains error messages. |
|
109
|
|
|
* |
|
110
|
|
|
* @var array |
|
111
|
|
|
*/ |
|
112
|
|
|
public $errors = []; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* authentication container |
|
116
|
|
|
* |
|
117
|
|
|
* @var array |
|
118
|
|
|
*/ |
|
119
|
|
|
protected $authContainer = []; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* login string |
|
123
|
|
|
* |
|
124
|
|
|
* @var string |
|
125
|
|
|
*/ |
|
126
|
|
|
private $login = ''; |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* minimum length of login string (default: 2) |
|
130
|
|
|
* |
|
131
|
|
|
* @var int |
|
132
|
|
|
*/ |
|
133
|
|
|
private $loginMinLength = 2; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* regular expression to find invalid login strings |
|
137
|
|
|
* (default: /^[a-z0-9][\w\.\-@]+/i ) |
|
138
|
|
|
* |
|
139
|
|
|
* @var string |
|
140
|
|
|
*/ |
|
141
|
|
|
private $validUsername = '/^[a-z0-9][\w\.\-@]+/i'; |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* user ID |
|
145
|
|
|
* |
|
146
|
|
|
* @var integer |
|
147
|
|
|
*/ |
|
148
|
|
|
private $userId = -1; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Status of user |
|
152
|
|
|
* |
|
153
|
|
|
* @var string |
|
154
|
|
|
*/ |
|
155
|
|
|
private $status = ''; |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* array of allowed values for status |
|
159
|
|
|
* |
|
160
|
|
|
* @var array |
|
161
|
|
|
*/ |
|
162
|
|
|
private $allowedStatus = array( |
|
163
|
|
|
'active' => self::STATUS_USER_ACTIVE, |
|
164
|
|
|
'blocked' => self::STATUS_USER_BLOCKED, |
|
165
|
|
|
'protected' => self::STATUS_USER_PROTECTED |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Configuration |
|
170
|
|
|
* |
|
171
|
|
|
* @var PMF_Configuration |
|
172
|
|
|
*/ |
|
173
|
|
|
protected $config = null; |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Constructor |
|
177
|
|
|
* |
|
178
|
|
|
* @param PMF_Configuration $config |
|
179
|
|
|
* |
|
180
|
|
|
* @return PMF_User |
|
181
|
|
|
*/ |
|
182
|
|
|
public function __construct(PMF_Configuration $config) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->config = $config; |
|
185
|
|
|
|
|
186
|
|
|
$perm = PMF_Perm::selectPerm($this->config->get('security.permLevel'), $this->config); |
|
187
|
|
|
if (!$this->addPerm($perm)) { |
|
188
|
|
|
return; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// authentication objects |
|
192
|
|
|
// always make a 'local' $auth object (see: $authData) |
|
193
|
|
|
$this->authContainer = []; |
|
194
|
|
|
$auth = new PMF_Auth($this->config); |
|
195
|
|
|
$authLocal = $auth->selectAuth($this->getAuthSource('name')); |
|
196
|
|
|
$authLocal->selectEncType($this->getAuthData('encType')); |
|
197
|
|
|
$authLocal->setReadOnly($this->getAuthData('readOnly')); |
|
198
|
|
|
|
|
199
|
|
|
if (!$this->addAuth($authLocal, $this->getAuthSource('type'))) { |
|
200
|
|
|
return; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// additionally, set given $auth objects |
|
204
|
|
|
if (count($auth) > 0) { |
|
205
|
|
|
foreach ($auth as $name => $authObject) { |
|
206
|
|
|
if (!$authObject instanceof PMF_Auth_Driver && !$this->addAuth($authObject, $name)) { |
|
207
|
|
|
break; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// user data object |
|
213
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
// --- OPERATIONS --- |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* adds a permission object to the user. |
|
221
|
|
|
* |
|
222
|
|
|
* @param PMF_Perm $perm Permission object |
|
223
|
|
|
* @return boolean |
|
224
|
|
|
*/ |
|
225
|
|
|
public function addPerm(PMF_Perm $perm) |
|
226
|
|
|
{ |
|
227
|
|
|
if ($this->checkPerm($perm)) { |
|
228
|
|
|
$this->perm = $perm; |
|
|
|
|
|
|
229
|
|
|
return true; |
|
230
|
|
|
} |
|
231
|
|
|
$this->perm = null; |
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns the User ID of the user. |
|
237
|
|
|
* |
|
238
|
|
|
* @return integer |
|
239
|
|
|
*/ |
|
240
|
|
|
public function getUserId() |
|
241
|
|
|
{ |
|
242
|
|
|
if (isset($this->userId) && is_int($this->userId)) { |
|
243
|
|
|
return (int)$this->userId; |
|
244
|
|
|
} |
|
245
|
|
|
$this->userId = -1; |
|
246
|
|
|
$this->errors[] = self::ERROR_USER_NO_USERID; |
|
247
|
|
|
|
|
248
|
|
|
return -1; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Loads basic user information from the database selecting the user with |
|
253
|
|
|
* specified user-ID. |
|
254
|
|
|
* |
|
255
|
|
|
* @param integer $userId User ID |
|
256
|
|
|
* @return bool |
|
257
|
|
|
*/ |
|
258
|
|
|
public function getUserById($userId) |
|
259
|
|
|
{ |
|
260
|
|
|
$select = sprintf(" |
|
261
|
|
|
SELECT |
|
262
|
|
|
user_id, |
|
263
|
|
|
login, |
|
264
|
|
|
account_status |
|
265
|
|
|
FROM |
|
266
|
|
|
%sfaquser |
|
267
|
|
|
WHERE |
|
268
|
|
|
user_id = %d", |
|
269
|
|
|
PMF_Db::getTablePrefix(), |
|
270
|
|
|
(int) $userId); |
|
271
|
|
|
|
|
272
|
|
|
$res = $this->config->getDb()->query($select); |
|
273
|
|
View Code Duplication |
if ($this->config->getDb()->numRows($res) != 1) { |
|
|
|
|
|
|
274
|
|
|
$this->errors[] = self::ERROR_USER_NO_USERID . 'error(): ' . $this->config->getDb()->error(); |
|
275
|
|
|
return false; |
|
276
|
|
|
} |
|
277
|
|
|
$user = $this->config->getDb()->fetchArray($res); |
|
278
|
|
|
$this->userId = (int) $user['user_id']; |
|
279
|
|
|
$this->login = (string)$user['login']; |
|
280
|
|
|
$this->status = (string)$user['account_status']; |
|
281
|
|
|
|
|
282
|
|
|
// get encrypted password |
|
283
|
|
|
// TODO: Add a getEncPassword method to the Auth* classes for the (local and remote) Auth Sources. |
|
284
|
|
|
if ('db' === $this->getAuthSource('name')) { |
|
285
|
|
|
$select = sprintf(" |
|
286
|
|
|
SELECT |
|
287
|
|
|
pass |
|
288
|
|
|
FROM |
|
289
|
|
|
%sfaquserlogin |
|
290
|
|
|
WHERE |
|
291
|
|
|
login = '%s'", |
|
292
|
|
|
PMF_Db::getTablePrefix(), |
|
293
|
|
|
$this->login |
|
294
|
|
|
); |
|
295
|
|
|
|
|
296
|
|
|
$res = $this->config->getDb()->query($select); |
|
297
|
|
View Code Duplication |
if ($this->config->getDb()->numRows($res) != 1) { |
|
|
|
|
|
|
298
|
|
|
$this->errors[] = self::ERROR_USER_NO_USERLOGINDATA . 'error(): ' . $this->config->getDb()->error(); |
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
// get user-data |
|
303
|
|
|
if (!$this->userdata instanceof PMF_User_UserData) { |
|
304
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
305
|
|
|
} |
|
306
|
|
|
$this->userdata->load($this->getUserId()); |
|
307
|
|
|
return true; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* loads basic user information from the database selecting the user with |
|
312
|
|
|
* specified login. |
|
313
|
|
|
* |
|
314
|
|
|
* @param string $login Login name |
|
315
|
|
|
* @param bool $raiseError Raise error? |
|
316
|
|
|
* @return bool |
|
317
|
|
|
*/ |
|
318
|
|
View Code Duplication |
public function getUserByLogin($login, $raiseError = true) |
|
319
|
|
|
{ |
|
320
|
|
|
$select = sprintf(" |
|
321
|
|
|
SELECT |
|
322
|
|
|
user_id, |
|
323
|
|
|
login, |
|
324
|
|
|
account_status |
|
325
|
|
|
FROM |
|
326
|
|
|
%sfaquser |
|
327
|
|
|
WHERE |
|
328
|
|
|
login = '%s'", |
|
329
|
|
|
PMF_Db::getTablePrefix(), |
|
330
|
|
|
$this->config->getDb()->escape($login) |
|
331
|
|
|
); |
|
332
|
|
|
|
|
333
|
|
|
$res = $this->config->getDb()->query($select); |
|
334
|
|
|
if ($this->config->getDb()->numRows($res) !== 1) { |
|
335
|
|
|
if ($raiseError) { |
|
336
|
|
|
|
|
337
|
|
|
$this->errors[] = self::ERROR_USER_INCORRECT_LOGIN; |
|
338
|
|
|
} |
|
339
|
|
|
return false; |
|
340
|
|
|
} |
|
341
|
|
|
$user = $this->config->getDb()->fetchArray($res); |
|
342
|
|
|
$this->userId = (int) $user['user_id']; |
|
343
|
|
|
$this->login = (string) $user['login']; |
|
344
|
|
|
$this->status = (string) $user['account_status']; |
|
345
|
|
|
|
|
346
|
|
|
// get user-data |
|
347
|
|
|
if (!$this->userdata instanceof PMF_User_UserData) { |
|
348
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
349
|
|
|
} |
|
350
|
|
|
$this->userdata->load($this->getUserId()); |
|
351
|
|
|
return true; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* loads basic user information from the database selecting the user with |
|
356
|
|
|
* specified cookie information. |
|
357
|
|
|
* |
|
358
|
|
|
* @param string $cookie |
|
359
|
|
|
* |
|
360
|
|
|
* @return boolean |
|
361
|
|
|
*/ |
|
362
|
|
View Code Duplication |
public function getUserByCookie($cookie) |
|
363
|
|
|
{ |
|
364
|
|
|
$select = sprintf(" |
|
365
|
|
|
SELECT |
|
366
|
|
|
user_id, |
|
367
|
|
|
login, |
|
368
|
|
|
account_status |
|
369
|
|
|
FROM |
|
370
|
|
|
%sfaquser |
|
371
|
|
|
WHERE |
|
372
|
|
|
remember_me = '%s'", |
|
373
|
|
|
PMF_Db::getTablePrefix(), |
|
374
|
|
|
$this->config->getDb()->escape($cookie) |
|
375
|
|
|
); |
|
376
|
|
|
|
|
377
|
|
|
$res = $this->config->getDb()->query($select); |
|
378
|
|
|
if ($this->config->getDb()->numRows($res) !== 1) { |
|
379
|
|
|
$this->errors[] = self::ERROR_USER_INCORRECT_LOGIN; |
|
380
|
|
|
return false; |
|
381
|
|
|
} |
|
382
|
|
|
$user = $this->config->getDb()->fetchArray($res); |
|
383
|
|
|
|
|
384
|
|
|
// Don't ever login via anonymous user |
|
385
|
|
|
if (-1 === $user['user_id']) { |
|
386
|
|
|
return false; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
$this->userId = (int) $user['user_id']; |
|
390
|
|
|
$this->login = (string) $user['login']; |
|
391
|
|
|
$this->status = (string) $user['account_status']; |
|
392
|
|
|
|
|
393
|
|
|
// get user-data |
|
394
|
|
|
if (!$this->userdata instanceof PMF_User_UserData) { |
|
395
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
396
|
|
|
} |
|
397
|
|
|
$this->userdata->load($this->getUserId()); |
|
398
|
|
|
return true; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* search users by login |
|
403
|
|
|
* |
|
404
|
|
|
* @param string $search Login name |
|
405
|
|
|
* @return array |
|
406
|
|
|
*/ |
|
407
|
|
|
public function searchUsers($search) |
|
408
|
|
|
{ |
|
409
|
|
|
$select = sprintf(" |
|
410
|
|
|
SELECT |
|
411
|
|
|
login, |
|
412
|
|
|
user_id, |
|
413
|
|
|
account_status |
|
414
|
|
|
FROM |
|
415
|
|
|
%sfaquser |
|
416
|
|
|
WHERE |
|
417
|
|
|
login LIKE '%s'", |
|
418
|
|
|
PMF_Db::getTablePrefix(), |
|
419
|
|
|
$this->config->getDb()->escape($search.'%') |
|
420
|
|
|
); |
|
421
|
|
|
|
|
422
|
|
|
$res = $this->config->getDb()->query($select); |
|
423
|
|
|
if (!$res) { |
|
424
|
|
|
return []; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
$result = []; |
|
428
|
|
|
while ($row = $this->config->getDb()->fetchArray($res)) { |
|
429
|
|
|
$result[] = $row; |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
return $result; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* creates a new user and stores basic data in the database. |
|
437
|
|
|
* |
|
438
|
|
|
* @param string $login Login name |
|
439
|
|
|
* @param string $pass Password |
|
440
|
|
|
* @param integer $userId User ID |
|
441
|
|
|
* @return mixed |
|
442
|
|
|
*/ |
|
443
|
|
|
public function createUser($login, $pass = '', $userId = 0) |
|
444
|
|
|
{ |
|
445
|
|
|
foreach ($this->authContainer as $auth) { |
|
446
|
|
|
if (!$this->checkAuth($auth)) { |
|
447
|
|
|
return false; |
|
448
|
|
|
} |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
// is $login valid? |
|
452
|
|
|
$login = (string)$login; |
|
453
|
|
|
if (!$this->isValidLogin($login)) { |
|
454
|
|
|
$this->errors[] = self::ERROR_USER_LOGINNAME_TOO_SHORT; |
|
455
|
|
|
return false; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
// does $login already exist? |
|
459
|
|
|
if ($this->getUserByLogin($login, false)) { |
|
460
|
|
|
$this->errors[] = self::ERROR_USER_LOGIN_NOT_UNIQUE; |
|
461
|
|
|
return false; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
// set user-ID |
|
465
|
|
|
if (0 == $userId) { |
|
466
|
|
|
$this->userId = (int) $this->config->getDb()->nextId(PMF_Db::getTablePrefix() . 'faquser', 'user_id'); |
|
467
|
|
|
} else { |
|
468
|
|
|
$this->userId = $userId; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
// create user entry |
|
472
|
|
|
$insert = sprintf(" |
|
473
|
|
|
INSERT INTO |
|
474
|
|
|
%sfaquser |
|
475
|
|
|
(user_id, login, session_timestamp, member_since) |
|
476
|
|
|
VALUES |
|
477
|
|
|
(%d, '%s', %d, '%s')", |
|
478
|
|
|
PMF_Db::getTablePrefix(), |
|
479
|
|
|
$this->getUserId(), |
|
480
|
|
|
$this->config->getDb()->escape($login), |
|
481
|
|
|
$_SERVER['REQUEST_TIME'], |
|
482
|
|
|
date('YmdHis', $_SERVER['REQUEST_TIME']) |
|
483
|
|
|
); |
|
484
|
|
|
|
|
485
|
|
|
$this->config->getDb()->query($insert); |
|
486
|
|
|
if (!$this->userdata instanceof PMF_User_UserData) { |
|
487
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
488
|
|
|
} |
|
489
|
|
|
$data = $this->userdata->add($this->getUserId()); |
|
490
|
|
|
if (!$data) { |
|
491
|
|
|
$this->errors[] = self::ERROR_USER_CANNOT_CREATE_USERDATA; |
|
492
|
|
|
return false; |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
// create authentication entry |
|
496
|
|
|
if ($pass == '') { |
|
497
|
|
|
$pass = $this->createPassword(); |
|
498
|
|
|
} |
|
499
|
|
|
$success = false; |
|
500
|
|
|
|
|
501
|
|
|
foreach ($this->authContainer as $name => $auth) { |
|
502
|
|
|
if ($auth->setReadOnly()) { |
|
503
|
|
|
continue; |
|
504
|
|
|
} |
|
505
|
|
|
if (!$auth->add($login, $pass)) { |
|
506
|
|
|
$this->errors[] = self::ERROR_USER_CANNOT_CREATE_USER . 'in Auth ' . $name; |
|
507
|
|
|
} else { |
|
508
|
|
|
$success = true; |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
if (!$success) { |
|
512
|
|
|
return false; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
$this->perm->autoJoin($this->userId); |
|
516
|
|
|
return $this->getUserByLogin($login, false); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
/** |
|
520
|
|
|
* deletes the user from the database. |
|
521
|
|
|
* |
|
522
|
|
|
* @return boolean |
|
523
|
|
|
*/ |
|
524
|
|
|
public function deleteUser() |
|
525
|
|
|
{ |
|
526
|
|
|
if (!isset($this->userId) || $this->userId == 0) { |
|
527
|
|
|
$this->errors[] = self::ERROR_USER_NO_USERID; |
|
528
|
|
|
return false; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
if (!isset($this->login) || strlen($this->login) == 0) { |
|
532
|
|
|
$this->errors[] = self::ERROR_USER_LOGIN_INVALID; |
|
533
|
|
|
return false; |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
if (isset($this->allowedStatus[$this->status]) && $this->allowedStatus[$this->status] == self::STATUS_USER_PROTECTED) { |
|
537
|
|
|
$this->errors[] = self::ERROR_USER_CANNOT_DELETE_USER . self::STATUS_USER_PROTECTED; |
|
538
|
|
|
return false; |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
$this->perm->refuseAllUserRights($this->userId); |
|
542
|
|
|
|
|
543
|
|
|
$delete = sprintf(" |
|
544
|
|
|
DELETE FROM |
|
545
|
|
|
%sfaquser |
|
546
|
|
|
WHERE |
|
547
|
|
|
user_id = %d", |
|
548
|
|
|
PMF_Db::getTablePrefix(), |
|
549
|
|
|
$this->userId |
|
550
|
|
|
); |
|
551
|
|
|
|
|
552
|
|
|
$res = $this->config->getDb()->query($delete); |
|
553
|
|
View Code Duplication |
if (!$res) { |
|
554
|
|
|
$this->errors[] = self::ERROR_USER_CANNOT_DELETE_USER . 'error(): ' . $this->config->getDb()->error(); |
|
555
|
|
|
return false; |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
if (!$this->userdata instanceof PMF_User_UserData) { |
|
559
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
560
|
|
|
} |
|
561
|
|
|
$data = $this->userdata->delete($this->getUserId()); |
|
562
|
|
|
if (!$data) { |
|
563
|
|
|
$this->errors[] = self::ERROR_USER_CANNOT_DELETE_USERDATA; |
|
564
|
|
|
return false; |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
$readOnly = 0; |
|
568
|
|
|
$authCount = 0; |
|
569
|
|
|
$delete = []; |
|
570
|
|
|
foreach ($this->authContainer as $auth) { |
|
571
|
|
|
$authCount++; |
|
572
|
|
|
if ($auth->setReadOnly()) { |
|
573
|
|
|
$readOnly++; |
|
574
|
|
|
continue; |
|
575
|
|
|
} |
|
576
|
|
|
$delete[] = $auth->delete($this->login); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
if ($readOnly == $authCount) { |
|
580
|
|
|
$this->errors[] = self::ERROR_USER_NO_AUTH_WRITABLE; |
|
581
|
|
|
} |
|
582
|
|
|
if (!in_array(true, $delete)) { |
|
583
|
|
|
return false; |
|
584
|
|
|
} |
|
585
|
|
|
return true; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* changes the user's password. If $pass is omitted, a new |
|
590
|
|
|
* password is generated using the createPassword() method. |
|
591
|
|
|
* |
|
592
|
|
|
* @param string $pass Password |
|
593
|
|
|
* @return boolean |
|
594
|
|
|
*/ |
|
595
|
|
|
public function changePassword($pass = '') |
|
596
|
|
|
{ |
|
597
|
|
|
foreach ($this->authContainer as $auth) { |
|
598
|
|
|
if (!$this->checkAuth($auth)) { |
|
599
|
|
|
return false; |
|
600
|
|
|
} |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
$login = $this->getLogin(); |
|
604
|
|
|
if ($pass == '') { |
|
605
|
|
|
$pass = $this->createPassword(); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
$success = false; |
|
609
|
|
|
foreach ($this->authContainer as $auth) { |
|
610
|
|
|
if ($auth->setReadOnly()) { |
|
611
|
|
|
continue; |
|
612
|
|
|
} |
|
613
|
|
|
if (!$auth->changePassword($login, $pass)) { |
|
614
|
|
|
continue; |
|
615
|
|
|
} |
|
616
|
|
|
else { |
|
617
|
|
|
$success = true; |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
return $success; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* returns the user's status. |
|
625
|
|
|
* |
|
626
|
|
|
* @return string |
|
627
|
|
|
*/ |
|
628
|
|
|
public function getStatus() |
|
629
|
|
|
{ |
|
630
|
|
|
if (isset($this->status) && strlen($this->status) > 0) { |
|
631
|
|
|
return $this->status; |
|
632
|
|
|
} |
|
633
|
|
|
return false; |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* sets the user's status and updates the database entry. |
|
638
|
|
|
* |
|
639
|
|
|
* @param string $status Status |
|
640
|
|
|
* @return boolean |
|
641
|
|
|
*/ |
|
642
|
|
|
public function setStatus($status) |
|
643
|
|
|
{ |
|
644
|
|
|
// is status allowed? |
|
645
|
|
|
$status = strtolower($status); |
|
646
|
|
|
if (!in_array($status, array_keys($this->allowedStatus))) { |
|
647
|
|
|
$this->errors[] = self::ERROR_USER_INVALID_STATUS; |
|
648
|
|
|
return false; |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
// update status |
|
652
|
|
|
$this->status = $status; |
|
653
|
|
|
$update = sprintf(" |
|
654
|
|
|
UPDATE |
|
655
|
|
|
%sfaquser |
|
656
|
|
|
SET |
|
657
|
|
|
account_status = '%s' |
|
658
|
|
|
WHERE |
|
659
|
|
|
user_id = %d", |
|
660
|
|
|
PMF_Db::getTablePrefix(), |
|
661
|
|
|
$this->config->getDb()->escape($status), |
|
662
|
|
|
$this->userId |
|
663
|
|
|
); |
|
664
|
|
|
|
|
665
|
|
|
$res = $this->config->getDb()->query($update); |
|
666
|
|
|
|
|
667
|
|
|
if ($res) { |
|
668
|
|
|
return true; |
|
669
|
|
|
} |
|
670
|
|
|
return false; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Returns a string with error messages. |
|
675
|
|
|
* |
|
676
|
|
|
* The string returned by error() contains messages for all errors that |
|
677
|
|
|
* during object procesing. Messages are separated by new lines. |
|
678
|
|
|
* |
|
679
|
|
|
* Error messages are stored in the public array errors. |
|
680
|
|
|
* |
|
681
|
|
|
* @return string |
|
682
|
|
|
*/ |
|
683
|
|
|
public function error() |
|
684
|
|
|
{ |
|
685
|
|
|
$message = ''; |
|
686
|
|
|
foreach ($this->errors as $error) { |
|
687
|
|
|
$message .= $error . "<br>\n"; |
|
688
|
|
|
} |
|
689
|
|
|
$this->errors = []; |
|
690
|
|
|
return $message . '<br>' . implode('<br>', debug_backtrace()); |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
/** |
|
694
|
|
|
* returns true if login is a valid login string. |
|
695
|
|
|
* |
|
696
|
|
|
* $this->loginMinLength defines the minimum length the |
|
697
|
|
|
* login string. If login has more characters than allowed, |
|
698
|
|
|
* false is returned. |
|
699
|
|
|
* $this->login_invalidRegExp is a regular expression. |
|
700
|
|
|
* If login matches this false is returned. |
|
701
|
|
|
* |
|
702
|
|
|
* @param string $login Login name |
|
703
|
|
|
* @return boolean |
|
704
|
|
|
*/ |
|
705
|
|
|
public function isValidLogin($login) |
|
706
|
|
|
{ |
|
707
|
|
|
$login = (string) $login; |
|
708
|
|
|
|
|
709
|
|
|
if (strlen($login) < $this->loginMinLength || !preg_match($this->validUsername, $login)) { |
|
710
|
|
|
$this->errors[] = self::ERROR_USER_LOGIN_INVALID; |
|
711
|
|
|
return false; |
|
712
|
|
|
} |
|
713
|
|
|
return true; |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
|
|
/** |
|
717
|
|
|
* adds a new authentication object to the user object. |
|
718
|
|
|
* |
|
719
|
|
|
* @param PMF_Auth_Driver $auth PMF_Auth_Driver object |
|
720
|
|
|
* @param string $name Auth name |
|
721
|
|
|
* @return boolean |
|
722
|
|
|
*/ |
|
723
|
|
|
public function addAuth($auth, $name) |
|
724
|
|
|
{ |
|
725
|
|
|
if ($this->checkAuth($auth)) { |
|
|
|
|
|
|
726
|
|
|
$this->authContainer[$name] = $auth; |
|
727
|
|
|
return true; |
|
728
|
|
|
} |
|
729
|
|
|
return false; |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
|
|
/** |
|
733
|
|
|
* returns true if auth is a valid authentication object. |
|
734
|
|
|
* |
|
735
|
|
|
* @param PMF_Auth $auth Auth object |
|
736
|
|
|
* @return bool |
|
737
|
|
|
*/ |
|
738
|
|
|
protected function checkAuth($auth) |
|
739
|
|
|
{ |
|
740
|
|
|
$methods = array('checkPassword'); |
|
741
|
|
|
foreach ($methods as $method) { |
|
742
|
|
|
if (!method_exists($auth, strtolower($method))) { |
|
743
|
|
|
$this->errors[] = self::ERROR_USER_NO_AUTH; |
|
744
|
|
|
return false; |
|
745
|
|
|
break; |
|
746
|
|
|
} |
|
747
|
|
|
} |
|
748
|
|
|
return true; |
|
749
|
|
|
} |
|
750
|
|
|
|
|
751
|
|
|
/** |
|
752
|
|
|
* Returns the data aof the auth container |
|
753
|
|
|
* @return array |
|
754
|
|
|
*/ |
|
755
|
|
|
public function getAuthContainer() |
|
756
|
|
|
{ |
|
757
|
|
|
return $this->authContainer; |
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
|
|
/** |
|
761
|
|
|
* Returns a specific entry from the auth data source array |
|
762
|
|
|
* |
|
763
|
|
|
* @param string $key |
|
764
|
|
|
* |
|
765
|
|
|
* @return string|null |
|
766
|
|
|
*/ |
|
767
|
|
|
public function getAuthSource($key) |
|
768
|
|
|
{ |
|
769
|
|
|
if (isset($this->authData['authSource'][$key])) { |
|
770
|
|
|
return $this->authData['authSource'][$key]; |
|
771
|
|
|
} else { |
|
772
|
|
|
return null; |
|
773
|
|
|
} |
|
774
|
|
|
} |
|
775
|
|
|
|
|
776
|
|
|
/** |
|
777
|
|
|
* Returns a specific entry from the auth data array |
|
778
|
|
|
* |
|
779
|
|
|
* @param string $key |
|
780
|
|
|
* |
|
781
|
|
|
* @return string|null |
|
782
|
|
|
*/ |
|
783
|
|
|
public function getAuthData($key) |
|
784
|
|
|
{ |
|
785
|
|
|
if (isset($this->authData[$key])) { |
|
786
|
|
|
return $this->authData[$key]; |
|
787
|
|
|
} else { |
|
788
|
|
|
return null; |
|
789
|
|
|
} |
|
790
|
|
|
} |
|
791
|
|
|
|
|
792
|
|
|
/** |
|
793
|
|
|
* returns true if perm is a valid permission object. |
|
794
|
|
|
* |
|
795
|
|
|
* @param PMF_Perm $perm Perm object |
|
796
|
|
|
* |
|
797
|
|
|
* @return bool |
|
798
|
|
|
*/ |
|
799
|
|
|
private function checkPerm($perm) |
|
800
|
|
|
{ |
|
801
|
|
|
if ($perm instanceof PMF_Perm) { |
|
802
|
|
|
return true; |
|
803
|
|
|
} |
|
804
|
|
|
$this->errors[] = self::ERROR_USER_NO_PERM; |
|
805
|
|
|
return false; |
|
806
|
|
|
} |
|
807
|
|
|
|
|
808
|
|
|
/** |
|
809
|
|
|
* returns the user's login. |
|
810
|
|
|
* |
|
811
|
|
|
* @return string |
|
812
|
|
|
*/ |
|
813
|
|
|
public function getLogin() |
|
814
|
|
|
{ |
|
815
|
|
|
return $this->login; |
|
816
|
|
|
} |
|
817
|
|
|
|
|
818
|
|
|
/** |
|
819
|
|
|
* returns a new password. |
|
820
|
|
|
* |
|
821
|
|
|
* @return string |
|
822
|
|
|
*/ |
|
823
|
|
|
private function createPassword() |
|
824
|
|
|
{ |
|
825
|
|
|
srand((double)microtime() * 1000000); |
|
826
|
|
|
return (string) uniqid(rand()); |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
/** |
|
830
|
|
|
* Returns the data of the current user |
|
831
|
|
|
* |
|
832
|
|
|
* @param string $field Field |
|
833
|
|
|
* @return array |
|
834
|
|
|
*/ |
|
835
|
|
|
public function getUserData($field = '*') |
|
836
|
|
|
{ |
|
837
|
|
|
if (!($this->userdata instanceof PMF_User_UserData)) { |
|
838
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
839
|
|
|
} |
|
840
|
|
|
return $this->userdata->get($field); |
|
841
|
|
|
} |
|
842
|
|
|
|
|
843
|
|
|
/** |
|
844
|
|
|
* Adds user data |
|
845
|
|
|
* |
|
846
|
|
|
* @param array $data Array with user data |
|
847
|
|
|
* @return bool |
|
848
|
|
|
*/ |
|
849
|
|
|
public function setUserData(Array $data) |
|
850
|
|
|
{ |
|
851
|
|
|
if (!($this->userdata instanceof PMF_User_UserData)) { |
|
852
|
|
|
$this->userdata = new PMF_User_UserData($this->config); |
|
853
|
|
|
} |
|
854
|
|
|
$this->userdata->load($this->getUserId()); |
|
855
|
|
|
return $this->userdata->set(array_keys($data), array_values($data)); |
|
856
|
|
|
} |
|
857
|
|
|
|
|
858
|
|
|
/** |
|
859
|
|
|
* Returns an array with the user-IDs of all users found in |
|
860
|
|
|
* the database. By default, the Anonymous User will not be returned. |
|
861
|
|
|
* |
|
862
|
|
|
* @param boolean $withoutAnonymous Without anonymous? |
|
863
|
|
|
* @return array |
|
864
|
|
|
*/ |
|
865
|
|
View Code Duplication |
public function getAllUsers($withoutAnonymous = true) |
|
|
|
|
|
|
866
|
|
|
{ |
|
867
|
|
|
$select = sprintf(" |
|
868
|
|
|
SELECT |
|
869
|
|
|
user_id |
|
870
|
|
|
FROM |
|
871
|
|
|
%sfaquser |
|
872
|
|
|
%s |
|
873
|
|
|
ORDER BY user_id ASC", |
|
874
|
|
|
PMF_Db::getTablePrefix(), |
|
875
|
|
|
($withoutAnonymous ? 'WHERE user_id <> -1' : '') |
|
876
|
|
|
); |
|
877
|
|
|
|
|
878
|
|
|
$res = $this->config->getDb()->query($select); |
|
879
|
|
|
if (!$res) { |
|
880
|
|
|
return []; |
|
881
|
|
|
} |
|
882
|
|
|
|
|
883
|
|
|
$result = []; |
|
884
|
|
|
while ($row = $this->config->getDb()->fetchArray($res)) { |
|
885
|
|
|
$result[] = $row['user_id']; |
|
886
|
|
|
} |
|
887
|
|
|
|
|
888
|
|
|
return $result; |
|
889
|
|
|
} |
|
890
|
|
|
|
|
891
|
|
|
/** |
|
892
|
|
|
* Returns an array of all users found in the database. By default, the |
|
893
|
|
|
* anonymous User will not be returned. The returned array contains the |
|
894
|
|
|
* user ID as key, the values are login name, account status, authentication |
|
895
|
|
|
* source and the user creation date. |
|
896
|
|
|
* |
|
897
|
|
|
* @param boolean $withoutAnonymous Without anonymous? |
|
898
|
|
|
* @return array |
|
899
|
|
|
*/ |
|
900
|
|
View Code Duplication |
public function getAllUserData($withoutAnonymous = true) |
|
901
|
|
|
{ |
|
902
|
|
|
$select = sprintf(" |
|
903
|
|
|
SELECT |
|
904
|
|
|
user_id, login, account_status, auth_source, member_since |
|
905
|
|
|
FROM |
|
906
|
|
|
%sfaquser |
|
907
|
|
|
%s |
|
908
|
|
|
ORDER BY |
|
909
|
|
|
login ASC", |
|
910
|
|
|
PMF_Db::getTablePrefix(), |
|
911
|
|
|
($withoutAnonymous ? 'WHERE user_id <> -1' : '')); |
|
912
|
|
|
|
|
913
|
|
|
$res = $this->config->getDb()->query($select); |
|
914
|
|
|
if (!$res) { |
|
915
|
|
|
return []; |
|
916
|
|
|
} |
|
917
|
|
|
|
|
918
|
|
|
$result = []; |
|
919
|
|
|
while ($row = $this->config->getDb()->fetchArray($res)) { |
|
920
|
|
|
$result[$row['user_id']] = $row; |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
return $result; |
|
924
|
|
|
} |
|
925
|
|
|
|
|
926
|
|
|
/** |
|
927
|
|
|
* Get all users in <option> tags |
|
928
|
|
|
* |
|
929
|
|
|
* @param integer $id Selected user ID |
|
930
|
|
|
* |
|
931
|
|
|
* @return string |
|
932
|
|
|
*/ |
|
933
|
|
|
public function getAllUserOptions($id = 1) |
|
934
|
|
|
{ |
|
935
|
|
|
$options = ''; |
|
936
|
|
|
$allUsers = $this->getAllUsers(); |
|
937
|
|
|
foreach ($allUsers as $userId) { |
|
938
|
|
|
if (-1 != $userId) { |
|
939
|
|
|
$this->getUserById($userId); |
|
940
|
|
|
$options .= sprintf( |
|
941
|
|
|
'<option value="%d"%s>%s (%s)</option>', |
|
942
|
|
|
$userId, |
|
943
|
|
|
(($userId == $id) ? ' selected="selected"' : ''), |
|
944
|
|
|
$this->getUserData('display_name'), |
|
945
|
|
|
$this->getLogin() |
|
946
|
|
|
); |
|
947
|
|
|
} |
|
948
|
|
|
} |
|
949
|
|
|
return $options; |
|
950
|
|
|
} |
|
951
|
|
|
|
|
952
|
|
|
/** |
|
953
|
|
|
* sets the minimum login string length |
|
954
|
|
|
* |
|
955
|
|
|
* @param integer $loginMinLength Minimum length of login name |
|
956
|
|
|
* |
|
957
|
|
|
* @return void |
|
958
|
|
|
*/ |
|
959
|
|
|
public function setLoginMinLength($loginMinLength) |
|
960
|
|
|
{ |
|
961
|
|
|
if (is_int($loginMinLength)) { |
|
962
|
|
|
$this->loginMinLength = $loginMinLength; |
|
963
|
|
|
} |
|
964
|
|
|
} |
|
965
|
|
|
} |
|
966
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.