/
UsersTable.php
557 lines (466 loc) · 14.4 KB
/
UsersTable.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
namespace Elgg\Database;
/// Map a username to a cached GUID
/**
* @var int[] $USERNAME_TO_GUID_MAP_CACHE
* @access private
*/
global $USERNAME_TO_GUID_MAP_CACHE;
$USERNAME_TO_GUID_MAP_CACHE = array();
/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
*
* @access private
*
* @package Elgg.Core
* @subpackage Database
* @since 1.10.0
*/
class UsersTable {
/**
* Global Elgg configuration
*
* @var \stdClass
*/
private $CONFIG;
/**
* Constructor
*/
public function __construct() {
global $CONFIG;
$this->CONFIG = $CONFIG;
}
/**
* Return the user specific details of a user by a row.
*
* @param int $guid The \ElggUser guid
*
* @return mixed
* @access private
*/
function getRow($guid) {
$guid = (int)$guid;
return _elgg_services()->db->getDataRow("SELECT * from {$this->CONFIG->dbprefix}users_entity where guid=$guid");
}
/**
* Disables all of a user's entities
*
* @param int $owner_guid The owner GUID
*
* @return bool Depending on success
*/
function disableEntities($owner_guid) {
$owner_guid = (int) $owner_guid;
if ($entity = get_entity($owner_guid)) {
if (_elgg_services()->events->trigger('disable', $entity->type, $entity)) {
if ($entity->canEdit()) {
$query = "UPDATE {$this->CONFIG->dbprefix}entities
set enabled='no' where owner_guid={$owner_guid}
or container_guid = {$owner_guid}";
$res = _elgg_services()->db->updateData($query);
return $res;
}
}
}
return false;
}
/**
* Ban a user (calls events, stores the reason)
*
* @param int $user_guid The user guid
* @param string $reason A reason
*
* @return bool
*/
function ban($user_guid, $reason = "") {
$user_guid = (int)$user_guid;
$user = get_entity($user_guid);
if (($user instanceof \ElggUser) && $user->canEdit()) {
if (_elgg_services()->events->trigger('ban', 'user', $user)) {
// Add reason
if ($reason) {
create_metadata($user_guid, 'ban_reason', $reason, '', 0, ACCESS_PUBLIC);
}
// invalidate memcache for this user
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new \ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$newentity_cache->delete($user_guid);
}
return $this->markBanned($user_guid, true);
}
return false;
}
return false;
}
/**
* Mark a user entity banned or unbanned.
*
* @note Use ban() or unban()
*
* @param int $guid User GUID
* @param bool $banned Mark the user banned?
*
* @return int Num rows affected
*/
public function markBanned($guid, $banned) {
$banned = $banned ? 'yes' : 'no';
$query = "
UPDATE {$this->CONFIG->dbprefix}users_entity
SET banned = '$banned'
WHERE guid = $guid
";
return _elgg_services()->db->updateData($query);
}
/**
* Unban a user (calls events, removes the reason)
*
* @param int $user_guid Unban a user.
*
* @return bool
*/
function unban($user_guid) {
$user_guid = (int)$user_guid;
$user = get_entity($user_guid);
if (($user) && ($user->canEdit()) && ($user instanceof \ElggUser)) {
if (_elgg_services()->events->trigger('unban', 'user', $user)) {
create_metadata($user_guid, 'ban_reason', '', '', 0, ACCESS_PUBLIC);
// invalidate memcache for this user
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new \ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$newentity_cache->delete($user_guid);
}
return $this->markBanned($user_guid, false);
}
return false;
}
return false;
}
/**
* Makes user $guid an admin.
*
* @param int $user_guid User guid
*
* @return bool
*/
function makeAdmin($user_guid) {
$user = get_entity((int)$user_guid);
if (($user) && ($user instanceof \ElggUser) && ($user->canEdit())) {
if (_elgg_services()->events->trigger('make_admin', 'user', $user)) {
// invalidate memcache for this user
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new \ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$newentity_cache->delete($user_guid);
}
$r = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}users_entity set admin='yes' where guid=$user_guid");
_elgg_invalidate_cache_for_entity($user_guid);
return $r;
}
return false;
}
return false;
}
/**
* Removes user $guid's admin flag.
*
* @param int $user_guid User GUID
*
* @return bool
*/
function removeAdmin($user_guid) {
$user = get_entity((int)$user_guid);
if (($user) && ($user instanceof \ElggUser) && ($user->canEdit())) {
if (_elgg_services()->events->trigger('remove_admin', 'user', $user)) {
// invalidate memcache for this user
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new \ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$newentity_cache->delete($user_guid);
}
$r = _elgg_services()->db->updateData("UPDATE {$this->CONFIG->dbprefix}users_entity set admin='no' where guid=$user_guid");
_elgg_invalidate_cache_for_entity($user_guid);
return $r;
}
return false;
}
return false;
}
/**
* Get user by username
*
* @param string $username The user's username
*
* @return \ElggUser|false Depending on success
*/
function getByUsername($username) {
global $USERNAME_TO_GUID_MAP_CACHE;
// Fixes #6052. Username is frequently sniffed from the path info, which,
// unlike $_GET, is not URL decoded. If the username was not URL encoded,
// this is harmless.
$username = rawurldecode($username);
$username = sanitise_string($username);
$access = _elgg_get_access_where_sql();
// Caching
if ((isset($USERNAME_TO_GUID_MAP_CACHE[$username]))
&& (_elgg_retrieve_cached_entity($USERNAME_TO_GUID_MAP_CACHE[$username]))) {
return _elgg_retrieve_cached_entity($USERNAME_TO_GUID_MAP_CACHE[$username]);
}
$query = "SELECT e.* FROM {$this->CONFIG->dbprefix}users_entity u
JOIN {$this->CONFIG->dbprefix}entities e ON e.guid = u.guid
WHERE u.username = '$username' AND $access";
$entity = _elgg_services()->db->getDataRow($query, 'entity_row_to_elggstar');
if ($entity) {
$USERNAME_TO_GUID_MAP_CACHE[$username] = $entity->guid;
} else {
$entity = false;
}
return $entity;
}
/**
* Get an array of users from an email address
*
* @param string $email Email address.
*
* @return array
*/
function getByEmail($email) {
$email = sanitise_string($email);
$access = _elgg_get_access_where_sql();
$query = "SELECT e.* FROM {$this->CONFIG->dbprefix}entities e
JOIN {$this->CONFIG->dbprefix}users_entity u ON e.guid = u.guid
WHERE email = '$email' AND $access";
return _elgg_services()->db->getData($query, 'entity_row_to_elggstar');
}
/**
* Return users (or the number of them) who have been active within a recent period.
*
* @param array $options Array of options with keys:
*
* seconds (int) => Length of period (default 600 = 10min)
* limit (int) => Limit (default 10)
* offset (int) => Offset (default 0)
* count (bool) => Return a count instead of users? (default false)
*
* Formerly this was the seconds parameter.
*
* @param int $limit Limit (deprecated usage, use $options)
* @param int $offset Offset (deprecated usage, use $options)
* @param bool $count Count (deprecated usage, use $options)
*
* @return \ElggUser[]|int
*/
function findActive($options = array(), $limit = 10, $offset = 0, $count = false) {
$seconds = 600; //default value
if (!is_array($options)) {
elgg_deprecated_notice("find_active_users() now accepts an \$options array", 1.9);
if (!$options) {
$options = $seconds; //assign default value
}
$options = array('seconds' => $options);
}
if ($limit === null) {
$limit = _elgg_services()->config->get('default_limit');
}
$options = array_merge(array(
'seconds' => $seconds,
'limit' => $limit,
'offset' => $offset,
'count' => $count,
), $options);
// cast options we're sending to hook
foreach (array('seconds', 'limit', 'offset') as $key) {
$options[$key] = (int)$options[$key];
}
$options['count'] = (bool)$options['count'];
// allow plugins to override
$params = array(
'seconds' => $options['seconds'],
'limit' => $options['limit'],
'offset' => $options['offset'],
'count' => $options['count'],
'options' => $options,
);
$data = _elgg_services()->hooks->trigger('find_active_users', 'system', $params, null);
// check null because the handler could legitimately return falsey values.
if ($data !== null) {
return $data;
}
$dbprefix = _elgg_services()->config->get('dbprefix');
$time = time() - $options['seconds'];
return elgg_get_entities(array(
'type' => 'user',
'limit' => $options['limit'],
'offset' => $options['offset'],
'count' => $options['count'],
'joins' => array("join {$dbprefix}users_entity u on e.guid = u.guid"),
'wheres' => array("u.last_action >= {$time}"),
'order_by' => "u.last_action desc",
));
}
/**
* Registers a user, returning false if the username already exists
*
* @param string $username The username of the new user
* @param string $password The password
* @param string $name The user's display name
* @param string $email The user's email address
* @param bool $allow_multiple_emails Allow the same email address to be
* registered multiple times?
*
* @return int|false The new user's GUID; false on failure
* @throws \RegistrationException
*/
function register($username, $password, $name, $email, $allow_multiple_emails = false) {
// no need to trim password.
$username = trim($username);
$name = trim(strip_tags($name));
$email = trim($email);
// A little sanity checking
if (empty($username)
|| empty($password)
|| empty($name)
|| empty($email)) {
return false;
}
// Make sure a user with conflicting details hasn't registered and been disabled
$access_status = access_get_show_hidden_status();
access_show_hidden_entities(true);
if (!validate_email_address($email)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:emailnotvalid'));
}
if (!validate_password($password)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:passwordnotvalid'));
}
if (!validate_username($username)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:usernamenotvalid'));
}
if ($user = get_user_by_username($username)) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:userexists'));
}
if ((!$allow_multiple_emails) && (get_user_by_email($email))) {
throw new \RegistrationException(_elgg_services()->translator->translate('registration:dupeemail'));
}
access_show_hidden_entities($access_status);
// Create user
$user = new \ElggUser();
$user->username = $username;
$user->email = $email;
$user->name = $name;
$user->access_id = ACCESS_PUBLIC;
$user->setPassword($password);
$user->owner_guid = 0; // Users aren't owned by anyone, even if they are admin created.
$user->container_guid = 0; // Users aren't contained by anyone, even if they are admin created.
$user->language = _elgg_services()->translator->getCurrentLanguage();
if ($user->save() === false) {
return false;
}
// Turn on email notifications by default
set_user_notification_setting($user->getGUID(), 'email', true);
return $user->getGUID();
}
/**
* Generates a unique invite code for a user
*
* @param string $username The username of the user sending the invitation
*
* @return string Invite code
* @see validateInviteCode
*/
function generateInviteCode($username) {
$time = time();
return "$time." . _elgg_services()->crypto->getHmac([(int)$time, $username])->getToken();
}
/**
* Validate a user's invite code
*
* @param string $username The username
* @param string $code The invite code
*
* @return bool
* @see generateInviteCode
*/
function validateInviteCode($username, $code) {
// validate the format of the token created by ->generateInviteCode()
if (!preg_match('~^(\d+)\.([a-zA-Z0-9\-_]+)$~', $code, $m)) {
return false;
}
$time = $m[1];
$mac = $m[2];
return _elgg_services()->crypto->getHmac([(int)$time, $username])->matchesToken($mac);
}
/**
* Set the validation status for a user.
*
* @param int $user_guid The user's GUID
* @param bool $status Validated (true) or unvalidated (false)
* @param string $method Optional method to say how a user was validated
* @return bool
*/
function setValidationStatus($user_guid, $status, $method = '') {
$result1 = create_metadata($user_guid, 'validated', $status, '', 0, ACCESS_PUBLIC, false);
$result2 = create_metadata($user_guid, 'validated_method', $method, '', 0, ACCESS_PUBLIC, false);
if ($result1 && $result2) {
return true;
} else {
return false;
}
}
/**
* Gets the validation status of a user.
*
* @param int $user_guid The user's GUID
* @return bool|null Null means status was not set for this user.
*/
function getValidationStatus($user_guid) {
$md = elgg_get_metadata(array(
'guid' => $user_guid,
'metadata_name' => 'validated'
));
if ($md == false) {
return null;
}
if ($md[0]->value) {
return true;
}
return false;
}
/**
* Sets the last action time of the given user to right now.
*
* @param int $user_guid The user GUID
*
* @return void
*/
function setLastAction($user_guid) {
$user_guid = (int) $user_guid;
$time = time();
$query = "UPDATE {$this->CONFIG->dbprefix}users_entity
set prev_last_action = last_action,
last_action = {$time} where guid = {$user_guid}";
execute_delayed_write_query($query);
}
/**
* Sets the last logon time of the given user to right now.
*
* @param int $user_guid The user GUID
*
* @return void
*/
function setLastLogin($user_guid) {
$user_guid = (int) $user_guid;
$time = time();
$query = "UPDATE {$this->CONFIG->dbprefix}users_entity
set prev_last_login = last_login, last_login = {$time} where guid = {$user_guid}";
execute_delayed_write_query($query);
}
}