These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Elgg users |
||
4 | * Functions to manage multiple or single users in an Elgg install |
||
5 | * |
||
6 | * @package Elgg.Core |
||
7 | * @subpackage DataModel.User |
||
8 | */ |
||
9 | |||
10 | /** |
||
11 | * Return the user specific details of a user by a row. |
||
12 | * |
||
13 | * @param int $guid The \ElggUser guid |
||
14 | * |
||
15 | * @return mixed |
||
16 | * @access private |
||
17 | */ |
||
18 | function get_user_entity_as_row($guid) { |
||
19 | return _elgg_services()->usersTable->getRow($guid); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Disables all of a user's entities |
||
24 | * |
||
25 | * @param int $owner_guid The owner GUID |
||
26 | * |
||
27 | * @return bool Depending on success |
||
28 | */ |
||
29 | function disable_user_entities($owner_guid) { |
||
30 | return _elgg_services()->usersTable->disableEntities($owner_guid); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Ban a user |
||
35 | * |
||
36 | * @param int $user_guid The user guid |
||
37 | * @param string $reason A reason |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | function ban_user($user_guid, $reason = "") { |
||
42 | return _elgg_services()->usersTable->ban($user_guid, $reason); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Unban a user. |
||
47 | * |
||
48 | * @param int $user_guid Unban a user. |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | function unban_user($user_guid) { |
||
53 | return _elgg_services()->usersTable->unban($user_guid); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Makes user $guid an admin. |
||
58 | * |
||
59 | * @param int $user_guid User guid |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | function make_user_admin($user_guid) { |
||
64 | return _elgg_services()->usersTable->makeAdmin($user_guid); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Removes user $guid's admin flag. |
||
69 | * |
||
70 | * @param int $user_guid User GUID |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | function remove_user_admin($user_guid) { |
||
75 | return _elgg_services()->usersTable->removeAdmin($user_guid); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get a user object from a GUID. |
||
80 | * |
||
81 | * This function returns an \ElggUser from a given GUID. |
||
82 | * |
||
83 | * @param int $guid The GUID |
||
84 | * |
||
85 | * @return \ElggUser|false |
||
86 | */ |
||
87 | function get_user($guid) { |
||
88 | return _elgg_services()->entityTable->get($guid, 'user'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get user by username |
||
93 | * |
||
94 | * @param string $username The user's username |
||
95 | * |
||
96 | * @return \ElggUser|false Depending on success |
||
97 | */ |
||
98 | function get_user_by_username($username) { |
||
99 | return _elgg_services()->usersTable->getByUsername($username); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get user by persistent login password |
||
104 | * |
||
105 | * @param string $hash Hash of the persistent login password |
||
106 | * |
||
107 | * @return \ElggUser |
||
108 | */ |
||
109 | function get_user_by_code($hash) { |
||
110 | return _elgg_services()->persistentLogin->getUserFromHash($hash); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get an array of users from an email address |
||
115 | * |
||
116 | * @param string $email Email address. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | function get_user_by_email($email) { |
||
121 | return _elgg_services()->usersTable->getByEmail($email); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return users (or the number of them) who have been active within a recent period. |
||
126 | * |
||
127 | * @param array $options Array of options with keys: |
||
128 | * |
||
129 | * seconds (int) => Length of period (default 600 = 10min) |
||
130 | * limit (int) => Limit (default from settings) |
||
131 | * offset (int) => Offset (default 0) |
||
132 | * count (bool) => Return a count instead of users? (default false) |
||
133 | * |
||
134 | * @return \ElggUser[]|int |
||
135 | */ |
||
136 | function find_active_users(array $options = []) { |
||
137 | return _elgg_services()->usersTable->findActive($options); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Generate and send a password request email to a given user's registered email address. |
||
142 | * |
||
143 | * @param int $user_guid User GUID |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | function send_new_password_request($user_guid) { |
||
148 | return _elgg_services()->passwords->sendNewPasswordRequest($user_guid); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Low level function to reset a given user's password. |
||
153 | * |
||
154 | * This can only be called from execute_new_password_request(). |
||
155 | * |
||
156 | * @param int $user_guid The user. |
||
157 | * @param string $password Text (which will then be converted into a hash and stored) |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | function force_user_password_reset($user_guid, $password) { |
||
162 | return _elgg_services()->passwords->forcePasswordReset($user_guid, $password); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Validate and change password for a user. |
||
167 | * |
||
168 | * @param int $user_guid The user id |
||
169 | * @param string $conf_code Confirmation code as sent in the request email. |
||
170 | * @param string $password Optional new password, if not randomly generated. |
||
171 | * |
||
172 | * @return bool True on success |
||
173 | */ |
||
174 | function execute_new_password_request($user_guid, $conf_code, $password = null) { |
||
175 | return _elgg_services()->passwords->executeNewPasswordReset($user_guid, $conf_code, $password); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Generate a random 12 character clear text password. |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | function generate_random_cleartext_password() { |
||
184 | return _elgg_services()->crypto->getRandomString(12, \ElggCrypto::CHARS_PASSWORD); |
||
185 | } |
||
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * Simple function which ensures that a username contains only valid characters. |
||
191 | * |
||
192 | * This should only permit chars that are valid on the file system as well. |
||
193 | * |
||
194 | * @param string $username Username |
||
195 | * |
||
196 | * @return bool |
||
197 | * @throws RegistrationException on invalid |
||
198 | */ |
||
199 | function validate_username($username) { |
||
200 | global $CONFIG; |
||
201 | |||
202 | // Basic, check length |
||
203 | if (!isset($CONFIG->minusername)) { |
||
204 | $CONFIG->minusername = 4; |
||
205 | } |
||
206 | |||
207 | if (strlen($username) < $CONFIG->minusername) { |
||
208 | $msg = elgg_echo('registration:usernametooshort', [$CONFIG->minusername]); |
||
209 | throw new \RegistrationException($msg); |
||
210 | } |
||
211 | |||
212 | // username in the database has a limit of 128 characters |
||
213 | if (strlen($username) > 128) { |
||
214 | $msg = elgg_echo('registration:usernametoolong', [128]); |
||
215 | throw new \RegistrationException($msg); |
||
216 | } |
||
217 | |||
218 | // Blacklist for bad characters (partially nicked from mediawiki) |
||
219 | $blacklist = '/[' . |
||
220 | '\x{0080}-\x{009f}' . // iso-8859-1 control chars |
||
221 | '\x{00a0}' . // non-breaking space |
||
222 | '\x{2000}-\x{200f}' . // various whitespace |
||
223 | '\x{2028}-\x{202f}' . // breaks and control chars |
||
224 | '\x{3000}' . // ideographic space |
||
225 | '\x{e000}-\x{f8ff}' . // private use |
||
226 | ']/u'; |
||
227 | |||
228 | if (preg_match($blacklist, $username)) { |
||
229 | // @todo error message needs work |
||
230 | throw new \RegistrationException(elgg_echo('registration:invalidchars')); |
||
231 | } |
||
232 | |||
233 | // Belts and braces |
||
234 | // @todo Tidy into main unicode |
||
235 | $blacklist2 = '\'/\\"*& ?#%^(){}[]~?<>;|¬`@+='; |
||
236 | |||
237 | $blacklist2 = elgg_trigger_plugin_hook('username:character_blacklist', 'user', |
||
238 | ['blacklist' => $blacklist2], $blacklist2); |
||
239 | |||
240 | for ($n = 0; $n < strlen($blacklist2); $n++) { |
||
241 | if (strpos($username, $blacklist2[$n]) !== false) { |
||
242 | $msg = elgg_echo('registration:invalidchars', [$blacklist2[$n], $blacklist2]); |
||
243 | $msg = htmlspecialchars($msg, ENT_QUOTES, 'UTF-8'); |
||
244 | throw new \RegistrationException($msg); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | $result = true; |
||
249 | return elgg_trigger_plugin_hook('registeruser:validate:username', 'all', |
||
250 | ['username' => $username], $result); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Simple validation of a password. |
||
255 | * |
||
256 | * @param string $password Clear text password |
||
257 | * |
||
258 | * @return bool |
||
259 | * @throws RegistrationException on invalid |
||
260 | */ |
||
261 | function validate_password($password) { |
||
262 | global $CONFIG; |
||
263 | |||
264 | if (!isset($CONFIG->min_password_length)) { |
||
265 | $CONFIG->min_password_length = 6; |
||
266 | } |
||
267 | |||
268 | if (strlen($password) < $CONFIG->min_password_length) { |
||
269 | $msg = elgg_echo('registration:passwordtooshort', [$CONFIG->min_password_length]); |
||
270 | throw new \RegistrationException($msg); |
||
271 | } |
||
272 | |||
273 | $result = true; |
||
274 | return elgg_trigger_plugin_hook('registeruser:validate:password', 'all', |
||
275 | ['password' => $password], $result); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Simple validation of a email. |
||
280 | * |
||
281 | * @param string $address Email address |
||
282 | * |
||
283 | * @throws RegistrationException on invalid |
||
284 | * @return bool |
||
285 | */ |
||
286 | function validate_email_address($address) { |
||
287 | if (!is_email_address($address)) { |
||
288 | throw new \RegistrationException(elgg_echo('registration:notemail')); |
||
289 | } |
||
290 | |||
291 | // Got here, so lets try a hook (defaulting to ok) |
||
292 | $result = true; |
||
293 | return elgg_trigger_plugin_hook('registeruser:validate:email', 'all', |
||
294 | ['email' => $address], $result); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Registers a user, returning false if the username already exists |
||
299 | * |
||
300 | * @param string $username The username of the new user |
||
301 | * @param string $password The password |
||
302 | * @param string $name The user's display name |
||
303 | * @param string $email The user's email address |
||
304 | * @param bool $allow_multiple_emails Allow the same email address to be |
||
305 | * registered multiple times? |
||
306 | * |
||
307 | * @return int|false The new user's GUID; false on failure |
||
308 | * @throws RegistrationException |
||
309 | */ |
||
310 | function register_user($username, $password, $name, $email, $allow_multiple_emails = false) { |
||
311 | return _elgg_services()->usersTable->register($username, $password, $name, $email, $allow_multiple_emails); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Generates a unique invite code for a user |
||
316 | * |
||
317 | * @param string $username The username of the user sending the invitation |
||
318 | * |
||
319 | * @return string Invite code |
||
320 | * @see elgg_validate_invite_code |
||
321 | */ |
||
322 | function generate_invite_code($username) { |
||
323 | return _elgg_services()->usersTable->generateInviteCode($username); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Validate a user's invite code |
||
328 | * |
||
329 | * @param string $username The username |
||
330 | * @param string $code The invite code |
||
331 | * |
||
332 | * @return bool |
||
333 | * @see generate_invite_code |
||
334 | * @since 1.10 |
||
335 | */ |
||
336 | function elgg_validate_invite_code($username, $code) { |
||
337 | return _elgg_services()->usersTable->validateInviteCode($username, $code); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Set the validation status for a user. |
||
342 | * |
||
343 | * @param int $user_guid The user's GUID |
||
344 | * @param bool $status Validated (true) or unvalidated (false) |
||
345 | * @param string $method Optional method to say how a user was validated |
||
346 | * @return bool |
||
347 | * @since 1.8.0 |
||
348 | */ |
||
349 | function elgg_set_user_validation_status($user_guid, $status, $method = '') { |
||
350 | return _elgg_services()->usersTable->setValidationStatus($user_guid, $status, $method); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Gets the validation status of a user. |
||
355 | * |
||
356 | * @param int $user_guid The user's GUID |
||
357 | * @return bool|null Null means status was not set for this user. |
||
358 | * @since 1.8.0 |
||
359 | */ |
||
360 | function elgg_get_user_validation_status($user_guid) { |
||
361 | return _elgg_services()->usersTable->getValidationStatus($user_guid); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Page handler for account related pages |
||
366 | * |
||
367 | * @param array $page_elements Page elements |
||
368 | * @param string $handler The handler string |
||
369 | * |
||
370 | * @return bool |
||
371 | * @access private |
||
372 | */ |
||
373 | function elgg_user_account_page_handler($page_elements, $handler) { |
||
374 | |||
375 | switch ($handler) { |
||
376 | case 'login': |
||
377 | echo elgg_view_resource("account/login"); |
||
378 | break; |
||
379 | case 'forgotpassword': |
||
380 | echo elgg_view_resource("account/forgotten_password"); |
||
381 | break; |
||
382 | case 'changepassword': |
||
383 | echo elgg_view_resource("account/change_password"); |
||
384 | break; |
||
385 | case 'register': |
||
386 | echo elgg_view_resource("account/register"); |
||
387 | break; |
||
388 | default: |
||
389 | return false; |
||
390 | } |
||
391 | |||
392 | return true; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Returns site's registration URL |
||
397 | * Triggers a 'registration_url', 'site' plugin hook that can be used by |
||
398 | * plugins to alter the default registration URL and append query elements, such as |
||
399 | * an invitation code and inviting user's guid |
||
400 | * |
||
401 | * @param array $query An array of query elements |
||
402 | * @param string $fragment Fragment identifier |
||
403 | * @return string |
||
404 | */ |
||
405 | function elgg_get_registration_url(array $query = [], $fragment = '') { |
||
406 | $url = elgg_normalize_url('register'); |
||
407 | $url = elgg_http_add_url_query_elements($url, $query) . $fragment; |
||
408 | return elgg_trigger_plugin_hook('registration_url', 'site', $query, $url); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Returns site's login URL |
||
413 | * Triggers a 'login_url', 'site' plugin hook that can be used by |
||
414 | * plugins to alter the default login URL |
||
415 | * |
||
416 | * @param array $query An array of query elements |
||
417 | * @param string $fragment Fragment identifier (e.g. #login-dropdown-box) |
||
418 | * @return string |
||
419 | */ |
||
420 | function elgg_get_login_url(array $query = [], $fragment = '') { |
||
421 | $url = elgg_normalize_url('login'); |
||
422 | $url = elgg_http_add_url_query_elements($url, $query) . $fragment; |
||
423 | return elgg_trigger_plugin_hook('login_url', 'site', $query, $url); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Sets the last action time of the given user to right now. |
||
428 | * |
||
429 | * @param int $user_guid The user GUID |
||
430 | * @return void |
||
431 | */ |
||
432 | function set_last_action($user_guid) { |
||
433 | $user = get_user($user_guid); |
||
434 | if (!$user) { |
||
435 | return; |
||
436 | } |
||
437 | _elgg_services()->usersTable->setLastAction($user); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Sets the last logon time of the given user to right now. |
||
442 | * |
||
443 | * @param int $user_guid The user GUID |
||
444 | * @return void |
||
445 | */ |
||
446 | function set_last_login($user_guid) { |
||
447 | $user = get_user($user_guid); |
||
448 | if (!$user) { |
||
449 | return; |
||
450 | } |
||
451 | _elgg_services()->usersTable->setLastLogin($user); |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Set user avatar URL |
||
456 | * Replaces user avatar URL with a public URL when walled garden is disabled |
||
457 | * |
||
458 | * @param string $hook "entity:icon:url" |
||
459 | * @param string $type "user" |
||
460 | * @param string $return Icon URL |
||
461 | * @param array $params Hook params |
||
462 | * @return string |
||
463 | * @access private |
||
464 | */ |
||
465 | function user_avatar_hook($hook, $type, $return, $params) { |
||
466 | $user = elgg_extract('entity', $params); |
||
467 | $size = elgg_extract('size', $params, 'medium'); |
||
468 | |||
469 | if (!$user instanceof ElggUser) { |
||
470 | return; |
||
471 | } |
||
472 | |||
473 | if (_elgg_config()->walled_garden) { |
||
474 | return; |
||
475 | } |
||
476 | |||
477 | if (!$user->hasIcon($size, 'icon')) { |
||
478 | return; |
||
479 | } |
||
480 | |||
481 | $icon = $user->getIcon($size, 'icon'); |
||
482 | return elgg_get_inline_url($icon, false); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Setup the default user hover menu |
||
487 | * @access private |
||
488 | */ |
||
489 | function elgg_user_hover_menu($hook, $type, $return, $params) { |
||
490 | $user = elgg_extract('entity', $params); |
||
491 | /* @var \ElggUser $user */ |
||
492 | |||
493 | if (!$user instanceof \ElggUser) { |
||
494 | return; |
||
495 | } |
||
496 | |||
497 | if (!elgg_is_logged_in()) { |
||
498 | return; |
||
499 | } |
||
500 | |||
501 | View Code Duplication | if ($user->canEdit()) { |
|
502 | $return[] = ElggMenuItem::factory([ |
||
503 | 'name' => 'avatar:edit', |
||
504 | 'text' => elgg_echo('avatar:edit'), |
||
505 | 'icon' => 'image', |
||
506 | 'href' => "avatar/edit/$user->username", |
||
507 | 'section' => (elgg_get_logged_in_user_guid() == $user->guid)? 'action' : 'admin', |
||
508 | ]); |
||
509 | } |
||
510 | |||
511 | // prevent admins from banning or deleting themselves |
||
512 | if (elgg_get_logged_in_user_guid() == $user->guid) { |
||
513 | return $return; |
||
514 | } |
||
515 | |||
516 | if (!elgg_is_admin_logged_in()) { |
||
517 | return $return; |
||
518 | } |
||
519 | |||
520 | // following items are admin only |
||
521 | View Code Duplication | if (!$user->isBanned()) { |
|
522 | $return[] = ElggMenuItem::factory([ |
||
523 | 'name' => 'ban', |
||
524 | 'text' => elgg_echo('ban'), |
||
525 | 'icon' => 'ban', |
||
526 | 'href' => "action/admin/user/ban?guid={$user->guid}", |
||
527 | 'confirm' => true, |
||
528 | 'section' => 'admin', |
||
529 | ]); |
||
530 | } else { |
||
531 | $return[] = ElggMenuItem::factory([ |
||
532 | 'name' => 'unban', |
||
533 | 'text' => elgg_echo('unban'), |
||
534 | 'icon' => 'ban', |
||
535 | 'href' => "action/admin/user/unban?guid={$user->guid}", |
||
536 | 'confirm' => true, |
||
537 | 'section' => 'admin', |
||
538 | ]); |
||
539 | } |
||
540 | |||
541 | $return[] = ElggMenuItem::factory([ |
||
542 | 'name' => 'delete', |
||
543 | 'text' => elgg_echo('delete'), |
||
544 | 'icon' => 'delete', |
||
545 | 'href' => "action/admin/user/delete?guid={$user->guid}", |
||
546 | 'confirm' => true, |
||
547 | 'section' => 'admin', |
||
548 | ]); |
||
549 | |||
550 | $return[] = ElggMenuItem::factory([ |
||
551 | 'name' => 'resetpassword', |
||
552 | 'text' => elgg_echo('resetpassword'), |
||
553 | 'icon' => 'refresh', |
||
554 | 'href' => "action/admin/user/resetpassword?guid={$user->guid}", |
||
555 | 'confirm' => true, |
||
556 | 'section' => 'admin', |
||
557 | ]); |
||
558 | |||
559 | View Code Duplication | if (!$user->isAdmin()) { |
|
560 | $return[] = ElggMenuItem::factory([ |
||
561 | 'name' => 'makeadmin', |
||
562 | 'text' => elgg_echo('makeadmin'), |
||
563 | 'icon' => 'level-up', |
||
564 | 'href' => "action/admin/user/makeadmin?guid={$user->guid}", |
||
565 | 'confirm' => true, |
||
566 | 'section' => 'admin', |
||
567 | ]); |
||
568 | } else { |
||
569 | $return[] = ElggMenuItem::factory([ |
||
570 | 'name' => 'removeadmin', |
||
571 | 'text' => elgg_echo('removeadmin'), |
||
572 | 'icon' => 'level-down', |
||
573 | 'href' => "action/admin/user/removeadmin?guid={$user->guid}", |
||
574 | 'confirm' => true, |
||
575 | 'section' => 'admin', |
||
576 | ]); |
||
577 | } |
||
578 | |||
579 | $return[] = ElggMenuItem::factory([ |
||
580 | 'name' => 'settings:edit', |
||
581 | 'text' => elgg_echo('settings:edit'), |
||
582 | 'icon' => 'cogs', |
||
583 | 'href' => "settings/user/$user->username", |
||
584 | 'section' => 'admin', |
||
585 | ]); |
||
586 | |||
587 | return $return; |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Avatar page handler |
||
592 | * |
||
593 | * /avatar/edit/<username> |
||
594 | * |
||
595 | * @param array $page |
||
596 | * @return bool |
||
597 | * @access private |
||
598 | */ |
||
599 | function elgg_avatar_page_handler($page) { |
||
600 | $user = get_user_by_username(elgg_extract(1, $page)); |
||
601 | if ($user) { |
||
602 | elgg_set_page_owner_guid($user->getGUID()); |
||
603 | } |
||
604 | |||
605 | if ($page[0] == 'edit') { |
||
606 | echo elgg_view_resource("avatar/edit"); |
||
607 | return true; |
||
608 | } |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * user page handler |
||
613 | * |
||
614 | * /user/view/<userguid> |
||
615 | * |
||
616 | * @param array $page url elements |
||
617 | * @return bool |
||
618 | * @access private |
||
619 | */ |
||
620 | function _elgg_user_page_handler($page) { |
||
621 | echo elgg_view_resource('user/view', [ |
||
622 | 'guid' => (int) elgg_extract(1, $page), |
||
623 | ]); |
||
624 | return true; |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Register menu items for the page menu |
||
629 | * |
||
630 | * @param string $hook |
||
631 | * @param string $type |
||
632 | * @param array $return |
||
633 | * @param array $params |
||
634 | * |
||
635 | * @return array |
||
636 | * |
||
637 | * @access private |
||
638 | * |
||
639 | * @since 3.0 |
||
640 | */ |
||
641 | View Code Duplication | function _elgg_user_page_menu($hook, $type, $return, $params) { |
|
0 ignored issues
–
show
|
|||
642 | |||
643 | $owner = elgg_get_page_owner_entity(); |
||
644 | if (!$owner) { |
||
645 | return; |
||
646 | } |
||
647 | |||
648 | $return[] = \ElggMenuItem::factory([ |
||
649 | 'name' => 'edit_avatar', |
||
650 | 'href' => "avatar/edit/{$owner->username}", |
||
651 | 'text' => elgg_echo('avatar:edit'), |
||
652 | 'section' => '1_profile', |
||
653 | 'contexts' => ['settings'], |
||
654 | ]); |
||
655 | |||
656 | return $return; |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * Register menu items for the topbar menu |
||
661 | * |
||
662 | * @param string $hook |
||
663 | * @param string $type |
||
664 | * @param array $return |
||
665 | * @param array $params |
||
666 | * |
||
667 | * @return array |
||
668 | * |
||
669 | * @access private |
||
670 | * |
||
671 | * @since 3.0 |
||
672 | */ |
||
673 | function _elgg_user_topbar_menu($hook, $type, $return, $params) { |
||
674 | |||
675 | $viewer = elgg_get_logged_in_user_entity(); |
||
676 | if (!$viewer) { |
||
677 | return; |
||
678 | } |
||
679 | |||
680 | $toggle = elgg_view_icon('chevron-down', ['class' => 'elgg-state-closed']); |
||
681 | $toggle .= elgg_view_icon('chevron-up', ['class' => 'elgg-state-opened']); |
||
682 | |||
683 | // If JS fails here, allow easy access to place where they can upgrade/flush caches |
||
684 | $href = elgg_is_admin_logged_in() ? elgg_get_login_url() : '#'; |
||
685 | |||
686 | $return[] = \ElggMenuItem::factory([ |
||
687 | 'name' => 'account', |
||
688 | 'text' => elgg_echo('account') . $toggle, |
||
689 | 'href' => $href, |
||
690 | 'priority' => 800, |
||
691 | 'section' => 'alt', |
||
692 | 'child_menu' => [ |
||
693 | 'display' => 'dropdown', |
||
694 | 'class' => 'elgg-topbar-child-menu', |
||
695 | 'data-position' => json_encode([ |
||
696 | 'at' => 'right+10px bottom', |
||
697 | 'my' => 'right top', |
||
698 | 'collision' => 'fit fit', |
||
699 | ]), |
||
700 | ], |
||
701 | ]); |
||
702 | |||
703 | $return[] = \ElggMenuItem::factory([ |
||
704 | 'name' => 'usersettings', |
||
705 | 'parent_name' => 'account', |
||
706 | 'href' => "settings/user/{$viewer->username}", |
||
707 | 'text' => elgg_echo('settings'), |
||
708 | 'icon' => 'cogs', |
||
709 | 'priority' => 300, |
||
710 | 'section' => 'alt', |
||
711 | ]); |
||
712 | |||
713 | if ($viewer->isAdmin()) { |
||
714 | $return[] = \ElggMenuItem::factory([ |
||
715 | 'name' => 'administration', |
||
716 | 'parent_name' => 'account', |
||
717 | 'href' => 'admin', |
||
718 | 'text' => elgg_echo('admin'), |
||
719 | 'icon' => 'tasks', |
||
720 | 'priority' => 800, |
||
721 | 'section' => 'alt', |
||
722 | ]); |
||
723 | } |
||
724 | |||
725 | $return[] = \ElggMenuItem::factory([ |
||
726 | 'name' => 'logout', |
||
727 | 'parent_name' => 'account', |
||
728 | 'href' => 'action/logout', |
||
729 | 'text' => elgg_echo('logout'), |
||
730 | 'icon' => 'sign-out', |
||
731 | 'is_action' => true, |
||
732 | 'priority' => 900, |
||
733 | 'section' => 'alt', |
||
734 | ]); |
||
735 | |||
736 | return $return; |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Set user icon file |
||
741 | * |
||
742 | * @param string $hook "entity:icon:file" |
||
743 | * @param string $type "user" |
||
744 | * @param \ElggIcon $icon Icon file |
||
745 | * @param array $params Hook params |
||
746 | * @return \ElggIcon |
||
747 | */ |
||
748 | function _elgg_user_set_icon_file($hook, $type, $icon, $params) { |
||
749 | |||
750 | $entity = elgg_extract('entity', $params); |
||
751 | $size = elgg_extract('size', $params, 'medium'); |
||
752 | |||
753 | $icon->owner_guid = $entity->guid; |
||
754 | $icon->setFilename("profile/{$entity->guid}{$size}.jpg"); |
||
755 | |||
756 | return $icon; |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Add the user to the subscribers when (un)banning the account |
||
761 | * |
||
762 | * @param string $hook 'get' |
||
763 | * @param string $type 'subscribers' |
||
764 | * @param array $return_value current subscribers |
||
765 | * @param arary $params supplied params |
||
766 | * |
||
767 | * @return void|array |
||
768 | */ |
||
769 | function _elgg_user_get_subscriber_unban_action($hook, $type, $return_value, $params) { |
||
770 | |||
771 | if (!_elgg_config()->security_notify_user_ban) { |
||
772 | return; |
||
773 | } |
||
774 | |||
775 | $event = elgg_extract('event', $params); |
||
776 | if (!($event instanceof \Elgg\Notifications\Event)) { |
||
777 | return; |
||
778 | } |
||
779 | |||
780 | if ($event->getAction() !== 'unban') { |
||
781 | return; |
||
782 | } |
||
783 | |||
784 | $user = $event->getObject(); |
||
785 | if (!($user instanceof \ElggUser)) { |
||
786 | return; |
||
787 | } |
||
788 | |||
789 | $return_value[$user->getGUID()] = ['email']; |
||
790 | |||
791 | return $return_value; |
||
792 | } |
||
793 | |||
794 | /** |
||
795 | * Send a notification to the user that the account was banned |
||
796 | * |
||
797 | * Note: this can't be handled by the delayed notification system as it won't send notifications to banned users |
||
798 | * |
||
799 | * @param string $event 'ban' |
||
800 | * @param string $type 'user' |
||
801 | * @param \ElggUser $user the user being banned |
||
802 | * |
||
803 | * @return void |
||
804 | */ |
||
805 | function _elgg_user_ban_notification($event, $type, $user) { |
||
806 | |||
807 | if (!_elgg_config()->security_notify_user_ban) { |
||
808 | return; |
||
809 | } |
||
810 | |||
811 | if (!($user instanceof \ElggUser)) { |
||
812 | return; |
||
813 | } |
||
814 | |||
815 | $site = elgg_get_site_entity(); |
||
816 | $language = $user->getLanguage(); |
||
817 | |||
818 | $subject = elgg_echo('user:notification:ban:subject', [$site->name], $language); |
||
819 | $body = elgg_echo('user:notification:ban:body', [ |
||
820 | $user->name, |
||
821 | $site->name, |
||
822 | $site->getURL(), |
||
823 | ], $language); |
||
824 | |||
825 | $params = [ |
||
826 | 'action' => 'ban', |
||
827 | 'object' => $user, |
||
828 | ]; |
||
829 | |||
830 | notify_user($user->getGUID(), $site->getGUID(), $subject, $body, $params, ['email']); |
||
831 | } |
||
832 | |||
833 | /** |
||
834 | * Prepare the notification content for the user being unbanned |
||
835 | * |
||
836 | * @param string $hook 'prepare' |
||
837 | * @param string $type 'notification:unban:user:' |
||
838 | * @param \Elgg\Notifications\Notification $return_value current notification content |
||
839 | * @param array $params supplied params |
||
840 | * |
||
841 | * @return void|\Elgg\Notifications\Notification |
||
842 | */ |
||
843 | function _elgg_user_prepare_unban_notification($hook, $type, $return_value, $params) { |
||
844 | |||
845 | if (!($return_value instanceof \Elgg\Notifications\Notification)) { |
||
846 | return; |
||
847 | } |
||
848 | |||
849 | $recipient = elgg_extract('recipient', $params); |
||
850 | $object = elgg_extract('object', $params); |
||
851 | $language = elgg_extract('language', $params); |
||
852 | |||
853 | if (!($recipient instanceof ElggUser) || !($object instanceof ElggUser)) { |
||
854 | return; |
||
855 | } |
||
856 | |||
857 | if ($recipient->getGUID() !== $object->getGUID()) { |
||
858 | return; |
||
859 | } |
||
860 | |||
861 | $site = elgg_get_site_entity(); |
||
862 | |||
863 | $return_value->subject = elgg_echo('user:notification:unban:subject', [$site->name], $language); |
||
864 | $return_value->body = elgg_echo('user:notification:unban:body', [ |
||
865 | $recipient->name, |
||
866 | $site->name, |
||
867 | $site->getURL(), |
||
868 | ], $language); |
||
869 | |||
870 | $return_value->url = $recipient->getURL(); |
||
871 | |||
872 | return $return_value; |
||
873 | } |
||
874 | |||
875 | /** |
||
876 | * Users initialisation function, which establishes the page handler |
||
877 | * |
||
878 | * @return void |
||
879 | * @access private |
||
880 | */ |
||
881 | function users_init() { |
||
882 | |||
883 | elgg_register_page_handler('register', 'elgg_user_account_page_handler'); |
||
884 | elgg_register_page_handler('forgotpassword', 'elgg_user_account_page_handler'); |
||
885 | elgg_register_page_handler('changepassword', 'elgg_user_account_page_handler'); |
||
886 | elgg_register_page_handler('login', 'elgg_user_account_page_handler'); |
||
887 | elgg_register_page_handler('avatar', 'elgg_avatar_page_handler'); |
||
888 | elgg_register_page_handler('user', '_elgg_user_page_handler'); |
||
889 | |||
890 | elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu'); |
||
891 | elgg_register_plugin_hook_handler('register', 'menu:page', '_elgg_user_page_menu'); |
||
892 | elgg_register_plugin_hook_handler('register', 'menu:topbar', '_elgg_user_topbar_menu'); |
||
893 | |||
894 | elgg_register_action('login', '', 'public'); |
||
895 | elgg_register_action('logout'); |
||
896 | elgg_register_action('register', '', 'public'); |
||
897 | elgg_register_action('useradd', '', 'admin'); |
||
898 | elgg_register_action('avatar/upload'); |
||
899 | elgg_register_action('avatar/crop'); |
||
900 | elgg_register_action('avatar/remove'); |
||
901 | |||
902 | elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook'); |
||
903 | |||
904 | elgg_register_action('user/changepassword', '', 'public'); |
||
905 | elgg_register_action('user/requestnewpassword', '', 'public'); |
||
906 | |||
907 | // Register the user type |
||
908 | elgg_register_entity_type('user', ''); |
||
909 | |||
910 | elgg_register_plugin_hook_handler('entity:icon:file', 'user', '_elgg_user_set_icon_file'); |
||
911 | |||
912 | elgg_register_notification_event('user', '', ['unban']); |
||
913 | elgg_register_plugin_hook_handler('get', 'subscriptions', '_elgg_user_get_subscriber_ban_action'); |
||
914 | elgg_register_event_handler('ban', 'user', '_elgg_user_ban_notification'); |
||
915 | elgg_register_plugin_hook_handler('prepare', 'notification:unban:user:', '_elgg_user_prepare_unban_notification'); |
||
916 | |||
917 | } |
||
918 | |||
919 | /** |
||
920 | * Runs unit tests for \ElggUser |
||
921 | * |
||
922 | * @param string $hook unit_test |
||
923 | * @param string $type system |
||
924 | * @param mixed $value Array of tests |
||
925 | * @param mixed $params Params |
||
926 | * |
||
927 | * @return array |
||
928 | * @access private |
||
929 | */ |
||
930 | function users_test($hook, $type, $value, $params) { |
||
931 | global $CONFIG; |
||
932 | $value[] = "{$CONFIG->path}engine/tests/ElggUserTest.php"; |
||
933 | return $value; |
||
934 | } |
||
935 | |||
936 | return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
||
937 | $events->registerHandler('init', 'system', 'users_init', 0); |
||
938 | $hooks->registerHandler('unit_test', 'system', 'users_test'); |
||
939 | }; |
||
940 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.