Elgg  Version 2.3
users.php
Go to the documentation of this file.
1 <?php
19  return _elgg_services()->usersTable->getRow($guid);
20 }
21 
30  return _elgg_services()->usersTable->disableEntities($owner_guid);
31 }
32 
41 function ban_user($user_guid, $reason = "") {
42  return _elgg_services()->usersTable->ban($user_guid, $reason);
43 }
44 
53  return _elgg_services()->usersTable->unban($user_guid);
54 }
55 
64  return _elgg_services()->usersTable->makeAdmin($user_guid);
65 }
66 
75  return _elgg_services()->usersTable->removeAdmin($user_guid);
76 }
77 
87 function get_user($guid) {
88  return _elgg_services()->entityTable->get($guid, 'user');
89 }
90 
99  return _elgg_services()->usersTable->getByUsername($username);
100 }
101 
109 function get_user_by_code($hash) {
110  return _elgg_services()->persistentLogin->getUserFromHash($hash);
111 }
112 
121  return _elgg_services()->usersTable->getByEmail($email);
122 }
123 
142 function find_active_users($options = array(), $limit = 10, $offset = 0, $count = false) {
143  return _elgg_services()->usersTable->findActive($options, $limit, $offset, $count);
144 }
145 
154  return _elgg_services()->passwords->sendNewPasswordRequest($user_guid);
155 }
156 
168  return _elgg_services()->passwords->forcePasswordReset($user_guid, $password);
169 }
170 
180 function execute_new_password_request($user_guid, $conf_code, $password = null) {
181  return _elgg_services()->passwords->executeNewPasswordReset($user_guid, $conf_code, $password);
182 }
183 
190  return _elgg_services()->crypto->getRandomString(12, \ElggCrypto::CHARS_PASSWORD);
191 }
192 
193 
194 
206  global $CONFIG;
207 
208  // Basic, check length
209  if (!isset($CONFIG->minusername)) {
210  $CONFIG->minusername = 4;
211  }
212 
213  if (strlen($username) < $CONFIG->minusername) {
214  $msg = elgg_echo('registration:usernametooshort', array($CONFIG->minusername));
215  throw new \RegistrationException($msg);
216  }
217 
218  // username in the database has a limit of 128 characters
219  if (strlen($username) > 128) {
220  $msg = elgg_echo('registration:usernametoolong', array(128));
221  throw new \RegistrationException($msg);
222  }
223 
224  // Blacklist for bad characters (partially nicked from mediawiki)
225  $blacklist = '/[' .
226  '\x{0080}-\x{009f}' . // iso-8859-1 control chars
227  '\x{00a0}' . // non-breaking space
228  '\x{2000}-\x{200f}' . // various whitespace
229  '\x{2028}-\x{202f}' . // breaks and control chars
230  '\x{3000}' . // ideographic space
231  '\x{e000}-\x{f8ff}' . // private use
232  ']/u';
233 
234  if (preg_match($blacklist, $username)) {
235  // @todo error message needs work
236  throw new \RegistrationException(elgg_echo('registration:invalidchars'));
237  }
238 
239  // Belts and braces
240  // @todo Tidy into main unicode
241  $blacklist2 = '\'/\\"*& ?#%^(){}[]~?<>;|¬`@+=';
242 
243  $blacklist2 = elgg_trigger_plugin_hook('username:character_blacklist', 'user',
244  array('blacklist' => $blacklist2), $blacklist2);
245 
246  for ($n = 0; $n < strlen($blacklist2); $n++) {
247  if (strpos($username, $blacklist2[$n]) !== false) {
248  $msg = elgg_echo('registration:invalidchars', array($blacklist2[$n], $blacklist2));
249  $msg = htmlspecialchars($msg, ENT_QUOTES, 'UTF-8');
250  throw new \RegistrationException($msg);
251  }
252  }
253 
254  $result = true;
255  return elgg_trigger_plugin_hook('registeruser:validate:username', 'all',
256  array('username' => $username), $result);
257 }
258 
267 function validate_password($password) {
268  global $CONFIG;
269 
270  if (!isset($CONFIG->min_password_length)) {
271  $CONFIG->min_password_length = 6;
272  }
273 
274  if (strlen($password) < $CONFIG->min_password_length) {
275  $msg = elgg_echo('registration:passwordtooshort', array($CONFIG->min_password_length));
276  throw new \RegistrationException($msg);
277  }
278 
279  $result = true;
280  return elgg_trigger_plugin_hook('registeruser:validate:password', 'all',
281  array('password' => $password), $result);
282 }
283 
292 function validate_email_address($address) {
293  if (!is_email_address($address)) {
294  throw new \RegistrationException(elgg_echo('registration:notemail'));
295  }
296 
297  // Got here, so lets try a hook (defaulting to ok)
298  $result = true;
299  return elgg_trigger_plugin_hook('registeruser:validate:email', 'all',
300  array('email' => $address), $result);
301 }
302 
316 function register_user($username, $password, $name, $email, $allow_multiple_emails = false) {
317  return _elgg_services()->usersTable->register($username, $password, $name, $email, $allow_multiple_emails);
318 }
319 
328 function generate_invite_code($username) {
329  return _elgg_services()->usersTable->generateInviteCode($username);
330 }
331 
342 function elgg_validate_invite_code($username, $code) {
343  return _elgg_services()->usersTable->validateInviteCode($username, $code);
344 }
345 
355 function elgg_set_user_validation_status($user_guid, $status, $method = '') {
356  return _elgg_services()->usersTable->setValidationStatus($user_guid, $status, $method);
357 }
358 
366 function elgg_get_user_validation_status($user_guid) {
367  return _elgg_services()->usersTable->getValidationStatus($user_guid);
368 }
369 
379 function elgg_user_account_page_handler($page_elements, $handler) {
380 
381  switch ($handler) {
382  case 'login':
383  echo elgg_view_resource("account/login");
384  break;
385  case 'forgotpassword':
386  echo elgg_view_resource("account/forgotten_password");
387  break;
388  case 'changepassword':
389  echo elgg_view_resource("account/change_password");
390  break;
391  case 'register':
392  echo elgg_view_resource("account/register");
393  break;
394  default:
395  return false;
396  }
397 
398  return true;
399 }
400 
411 function elgg_get_registration_url(array $query = [], $fragment = '') {
412  $url = elgg_normalize_url('register');
413  $url = elgg_http_add_url_query_elements($url, $query) . $fragment;
414  return elgg_trigger_plugin_hook('registration_url', 'site', $query, $url);
415 }
416 
426 function elgg_get_login_url(array $query = [], $fragment = '') {
427  $url = elgg_normalize_url('login');
428  $url = elgg_http_add_url_query_elements($url, $query) . $fragment;
429  return elgg_trigger_plugin_hook('login_url', 'site', $query, $url);
430 }
431 
438 function set_last_action($user_guid) {
439  $user = get_user($user_guid);
440  if (!$user) {
441  return;
442  }
443  _elgg_services()->usersTable->setLastAction($user);
444 }
445 
452 function set_last_login($user_guid) {
453  $user = get_user($user_guid);
454  if (!$user) {
455  return;
456  }
457  _elgg_services()->usersTable->setLastLogin($user);
458 }
459 
470 function user_create_hook_add_site_relationship($event, $object_type, $object) {
471  add_entity_relationship($object->getGUID(), 'member_of_site', elgg_get_site_entity()->guid);
472 }
473 
484 function user_avatar_hook($hook, $entity_type, $returnvalue, $params) {
485  $user = elgg_extract('entity', $params);
486  $size = elgg_extract('size', $params, 'medium');
487 
488  if (!$user instanceof ElggUser) {
489  return;
490  }
491 
492  $default_url = elgg_get_simplecache_url("icons/user/default{$size}.gif");
493  if (!isset($user->icontime)) {
494  return $default_url;
495  }
496 
497  if (_elgg_view_may_be_altered('resources/avatar/view', 'resources/avatar/view.php')) {
498  // For BC with 2.0 if a plugin is suspected of using this view/page handler we need to use it.
499  // /avatar page handler will issue a deprecation notice.
500  return "avatar/view/$user->username/$size/$user->icontime";
501  }
502 
503  $filehandler = new ElggFile();
504  $filehandler->owner_guid = $user->guid;
505  $filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
506  $use_cookie = elgg_get_config('walled_garden'); // don't serve avatars with public URLs in a walled garden mode
507  $avatar_url = elgg_get_inline_url($filehandler, $use_cookie);
508 
509  return $avatar_url ? : $default_url;
510 }
511 
516 function elgg_user_hover_menu($hook, $type, $return, $params) {
517  $user = elgg_extract('entity', $params);
518  /* @var \ElggUser $user */
519 
520  if (!$user instanceof \ElggUser) {
521  return;
522  }
523 
524  if (!elgg_is_logged_in()) {
525  return;
526  }
527 
528  if (elgg_get_logged_in_user_guid() == $user->guid) {
529  $url = "profile/$user->username/edit";
530  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
531  $item->setSection('action');
532  $return[] = $item;
533 
534  $url = "avatar/edit/$user->username";
535  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
536  $item->setSection('action');
537  $return[] = $item;
538  }
539 
540  // prevent admins from banning or deleting themselves
541  if (elgg_get_logged_in_user_guid() == $user->guid) {
542  return $return;
543  }
544 
545  if (elgg_is_admin_logged_in()) {
546  $actions = array();
547  if (!$user->isBanned()) {
548  $actions[] = 'ban';
549  } else {
550  $actions[] = 'unban';
551  }
552  $actions[] = 'delete';
553  $actions[] = 'resetpassword';
554  if (!$user->isAdmin()) {
555  $actions[] = 'makeadmin';
556  } else {
557  $actions[] = 'removeadmin';
558  }
559 
560  foreach ($actions as $action) {
561  $url = "action/admin/user/$action?guid={$user->guid}";
562  $url = elgg_add_action_tokens_to_url($url);
563  $item = new \ElggMenuItem($action, elgg_echo($action), $url);
564  $item->setSection('admin');
565  $item->setConfirmText(true);
566 
567  $return[] = $item;
568  }
569 
570  $url = "profile/$user->username/edit";
571  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
572  $item->setSection('admin');
573  $return[] = $item;
574 
575  $url = "avatar/edit/$user->username";
576  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
577  $item->setSection('admin');
578  $return[] = $item;
579 
580  $url = "settings/user/$user->username";
581  $item = new \ElggMenuItem('settings:edit', elgg_echo('settings:edit'), $url);
582  $item->setSection('admin');
583  $return[] = $item;
584 
585  $url = "activity/owner/$user->username";
586  $item = new \ElggMenuItem('activity:owner', elgg_echo('activity:owner'), $url);
587  $item->setSection('action');
588  $return[] = $item;
589  }
590 
591  return $return;
592 }
593 
605 function elgg_users_setup_entity_menu($hook, $type, $return, $params) {
606  if (elgg_in_context('widgets')) {
607  return $return;
608  }
609 
610  $entity = $params['entity'];
611  if (!elgg_instanceof($entity, 'user')) {
612  return $return;
613  }
614  /* @var \ElggUser $entity */
615 
616  if ($entity->isBanned()) {
617  $banned = elgg_echo('banned');
618  $options = array(
619  'name' => 'banned',
620  'text' => "<span>$banned</span>",
621  'href' => false,
622  'priority' => 0,
623  );
624  $return = array(\ElggMenuItem::factory($options));
625  } else {
626  $return = array();
627  $location = $entity->location;
628  if (is_string($location) && $location !== '') {
629  $location = htmlspecialchars($location, ENT_QUOTES, 'UTF-8', false);
630  $options = array(
631  'name' => 'location',
632  'text' => "<span>$location</span>",
633  'href' => false,
634  'priority' => 150,
635  );
636  $return[] = \ElggMenuItem::factory($options);
637  }
638  }
639 
640  return $return;
641 }
642 
651 function elgg_profile_fields_setup() {
652  global $CONFIG;
653 
654  $profile_defaults = array (
655  'description' => 'longtext',
656  'briefdescription' => 'text',
657  'location' => 'location',
658  'interests' => 'tags',
659  'skills' => 'tags',
660  'contactemail' => 'email',
661  'phone' => 'text',
662  'mobile' => 'text',
663  'website' => 'url',
664  'twitter' => 'text',
665  );
666 
667  $loaded_defaults = array();
668  $fieldlist = elgg_get_config('profile_custom_fields');
669  if ($fieldlist || $fieldlist === '0') {
670  $fieldlistarray = explode(',', $fieldlist);
671  foreach ($fieldlistarray as $listitem) {
672  if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
673  $type = elgg_get_config("admin_defined_profile_type_{$listitem}");
674  $loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
675  add_translation(get_current_language(), array("profile:admin_defined_profile_{$listitem}" => $translation));
676  }
677  }
678  }
679 
680  if (count($loaded_defaults)) {
681  $CONFIG->profile_using_custom = true;
682  $profile_defaults = $loaded_defaults;
683  }
684 
685  $CONFIG->profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
686 
687  // register any tag metadata names
688  foreach ($CONFIG->profile_fields as $name => $type) {
689  if ($type == 'tags' || $type == 'location' || $type == 'tag') {
690  elgg_register_tag_metadata_name($name);
691  // register a tag name translation
692  add_translation(get_current_language(), array("tag_names:$name" => elgg_echo("profile:$name")));
693  }
694  }
695 }
696 
706 function elgg_avatar_page_handler($page) {
707  $user = get_user_by_username(elgg_extract(1, $page));
708  if ($user) {
709  elgg_set_page_owner_guid($user->getGUID());
710  }
711 
712  if ($page[0] == 'edit') {
713  echo elgg_view_resource("avatar/edit");
714  } else {
715  elgg_deprecated_notice("/avatar/view page handler has been deprecated and will be removed. Use elgg_get_inline_url() instead.", '2.2');
716  echo elgg_view_resource("avatar/view", [
717  'size' => elgg_extract(2, $page),
718  ]);
719  }
720 
721  return true;
722 }
723 
732  $user = get_user_by_username($page[0]);
734 
735  if ($page[1] == 'edit') {
736  echo elgg_view_resource("profile/edit");
737  return true;
738  }
739  return false;
740 }
741 
748 function users_pagesetup() {
749 
751  $viewer = elgg_get_logged_in_user_entity();
752 
753  if ($owner) {
754  elgg_register_menu_item('page', array(
755  'name' => 'edit_avatar',
756  'href' => "avatar/edit/{$owner->username}",
757  'text' => elgg_echo('avatar:edit'),
758  'section' => '1_profile',
759  'contexts' => array('settings'),
760  ));
761 
762  elgg_register_menu_item('page', array(
763  'name' => 'edit_profile',
764  'href' => "profile/{$owner->username}/edit",
765  'text' => elgg_echo('profile:edit'),
766  'section' => '1_profile',
767  'contexts' => array('settings'),
768  ));
769  }
770 
771  // topbar
772  if ($viewer) {
773  elgg_register_menu_item('topbar', array(
774  'name' => 'usersettings',
775  'href' => "settings/user/{$viewer->username}",
776  'text' => elgg_view_icon('settings') . elgg_echo('settings'),
777  'priority' => 500,
778  'section' => 'alt',
779  ));
780 
781  elgg_register_menu_item('topbar', array(
782  'name' => 'logout',
783  'href' => "action/logout",
784  'text' => elgg_echo('logout'),
785  'is_action' => true,
786  'priority' => 1000,
787  'section' => 'alt',
788  ));
789  }
790 }
791 
802 
803  $entity = elgg_extract('entity', $params);
804  $size = elgg_extract('size', $params, 'medium');
805 
806  $icon->owner_guid = $entity->guid;
807  $icon->setFilename("profile/{$entity->guid}{$size}.jpg");
808 
809  return $icon;
810 }
811 
818 function users_init() {
819 
820  elgg_register_page_handler('register', 'elgg_user_account_page_handler');
821  elgg_register_page_handler('forgotpassword', 'elgg_user_account_page_handler');
822  elgg_register_page_handler('changepassword', 'elgg_user_account_page_handler');
823  elgg_register_page_handler('login', 'elgg_user_account_page_handler');
824  elgg_register_page_handler('avatar', 'elgg_avatar_page_handler');
825  elgg_register_page_handler('profile', 'elgg_profile_page_handler');
826 
827  elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu');
828 
829  elgg_register_action('register', '', 'public');
830  elgg_register_action('useradd', '', 'admin');
831  elgg_register_action('avatar/upload');
832  elgg_register_action('avatar/crop');
833  elgg_register_action('avatar/remove');
834  elgg_register_action('profile/edit');
835 
836  elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
837 
838  elgg_register_action('user/changepassword', '', 'public');
839  elgg_register_action('user/requestnewpassword', '', 'public');
840 
841  // Register the user type
842  elgg_register_entity_type('user', '');
843 
844  elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_users_setup_entity_menu', 501);
845 
846  elgg_register_event_handler('create', 'user', 'user_create_hook_add_site_relationship');
847 
848  elgg_register_plugin_hook_handler('entity:icon:file', 'user', '_elgg_user_set_icon_file');
849 }
850 
862 function users_test($hook, $type, $value, $params) {
863  global $CONFIG;
864  $value[] = "{$CONFIG->path}engine/tests/ElggUserTest.php";
865  return $value;
866 }
867 
868 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
869  $events->registerHandler('init', 'system', 'users_init', 0);
870  $events->registerHandler('init', 'system', 'elgg_profile_fields_setup', 10000); // Ensure this runs after other plugins
871  $events->registerHandler('pagesetup', 'system', 'users_pagesetup', 0);
872  $hooks->registerHandler('unit_test', 'system', 'users_test');
873 };
users_init()
Users initialisation function, which establishes the page handler.
Definition: users.php:818
elgg_view_icon($name, $vars=array())
View one of the icons.
Definition: views.php:1582
execute_new_password_request($user_guid, $conf_code, $password=null)
Validate and change password for a user.
Definition: users.php:180
return function(\Elgg\EventsService $events,\Elgg\HooksRegistrationService $hooks)
Definition: users.php:868
$CONFIG view
The current view type.
Definition: config.php:149
send_new_password_request($user_guid)
Generate and send a password request email to a given user&#39;s registered email address.
Definition: users.php:153
$action
Definition: full.php:133
get_user_by_email($email)
Get an array of users from an email address.
Definition: users.php:120
make_user_admin($user_guid)
Makes user $guid an admin.
Definition: users.php:63
$username
Definition: delete.php:22
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
const CHARS_PASSWORD
Character set for temp passwords (no risk of embedded profanity/glyphs that look similar) ...
Definition: ElggCrypto.php:18
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_view_resource($name, array $vars=[])
Render a resource view.
Definition: views.php:510
force_user_password_reset($user_guid, $password)
Low level function to reset a given user&#39;s password.
Definition: users.php:167
find_active_users($options=array(), $limit=10, $offset=0, $count=false)
Return users (or the number of them) who have been active within a recent period. ...
Definition: users.php:142
return
Definition: delete.php:11
$value
Definition: longtext.php:42
validate_username($username)
Simple function which ensures that a username contains only valid characters.
Definition: users.php:205
_elgg_user_set_icon_file($hook, $type, $icon, $params)
Set user icon file.
Definition: users.php:801
if(!$count) $offset
Definition: pagination.php:26
$guid
Removes an admin notice.
$email
Definition: register.php:15
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
elgg form settings
Definition: admin.css.php:627
ui datepicker td span
Definition: admin.css.php:680
unban_user($user_guid)
Unban a user.
Definition: users.php:52
users_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: users.php:862
elgg_set_page_owner_guid($guid)
Set the guid of the entity that owns this page.
Definition: pageowner.php:72
$options
Elgg admin footer.
Definition: footer.php:6
get_user_by_username($username)
Get user by username.
Definition: users.php:98
if(!$owner) $icon
Definition: default.php:16
disable_user_entities($owner_guid)
Disables all of a user&#39;s entities.
Definition: users.php:29
$params
Definition: login.php:72
ban_user($user_guid, $reason="")
Ban a user.
Definition: users.php:41
elgg_get_inline_url(\ElggFile $file, $use_cookie=false, $expires= '')
Returns file&#39;s URL for inline display Suitable for displaying cacheable resources, such as user avatars.
Definition: filestore.php:502
get_user_by_code($hash)
Get user by persistent login password.
Definition: users.php:109
$owner_guid
$limit
Definition: userpicker.php:38
font size
Definition: admin.css.php:81
$owner
Definition: crop.php:8
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
elgg widget edit
Definition: admin.css.php:1299
get_user_entity_as_row($guid)
Return the user specific details of a user by a row.
Definition: users.php:18
global $CONFIG
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$user
Definition: ban.php:13
elgg echo
Translates a string.
Definition: languages.js:48
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:56
users_pagesetup()
Sets up user-related menu items.
Definition: users.php:748
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
$password
Definition: login.php:25
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:550
$size
Definition: default.php:20
login(\ElggUser $user, $persistent=false)
Logs in a specified .
Definition: sessions.php:320
$entity
Definition: delete.php:7
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
generate_random_cleartext_password()
Generate a random 12 character clear text password.
Definition: users.php:189
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:96
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:526
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg menu user
Definition: admin.css.php:192
remove_user_admin($user_guid)
Removes user $guid&#39;s admin flag.
Definition: users.php:74
elgg action
Definition: ajax.js:200
elgg_profile_page_handler($page)
Profile page handler.
Definition: users.php:731
if(!$display_name) $type
Definition: delete.php:27