Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
408 function set_last_action($user_guid) {
409  _elgg_services()->usersTable->setLastAction($user_guid);
410 }
411 
419 function set_last_login($user_guid) {
420  _elgg_services()->usersTable->setLastLogin($user_guid);
421 }
422 
433 function user_create_hook_add_site_relationship($event, $object_type, $object) {
434  add_entity_relationship($object->getGUID(), 'member_of_site', elgg_get_site_entity()->guid);
435 }
436 
447 function user_avatar_hook($hook, $entity_type, $returnvalue, $params) {
448  $user = elgg_extract('entity', $params);
449  $size = elgg_extract('size', $params, 'medium');
450 
451  if (!$user instanceof ElggUser) {
452  return;
453  }
454 
455  $default_url = elgg_get_simplecache_url("icons/user/default{$size}.gif");
456  if (!isset($user->icontime)) {
457  return $default_url;
458  }
459 
460  if (_elgg_view_may_be_altered('resources/avatar/view', 'resources/avatar/view.php')) {
461  // For BC with 2.0 if a plugin is suspected of using this view/page handler we need to use it.
462  // /avatar page handler will issue a deprecation notice.
463  return "avatar/view/$user->username/$size/$user->icontime";
464  }
465 
466  $filehandler = new ElggFile();
467  $filehandler->owner_guid = $user->guid;
468  $filehandler->setFilename("profile/{$user->guid}{$size}.jpg");
469  $use_cookie = elgg_get_config('walled_garden'); // don't serve avatars with public URLs in a walled garden mode
470  $avatar_url = elgg_get_inline_url($filehandler, $use_cookie);
471 
472  return $avatar_url ? : $default_url;
473 }
474 
479 function elgg_user_hover_menu($hook, $type, $return, $params) {
480  $user = elgg_extract('entity', $params);
481  /* @var \ElggUser $user */
482 
483  if (!$user instanceof \ElggUser) {
484  return;
485  }
486 
487  if (!elgg_is_logged_in()) {
488  return;
489  }
490 
491  if (elgg_get_logged_in_user_guid() == $user->guid) {
492  $url = "profile/$user->username/edit";
493  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
494  $item->setSection('action');
495  $return[] = $item;
496 
497  $url = "avatar/edit/$user->username";
498  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
499  $item->setSection('action');
500  $return[] = $item;
501  }
502 
503  // prevent admins from banning or deleting themselves
504  if (elgg_get_logged_in_user_guid() == $user->guid) {
505  return $return;
506  }
507 
508  if (elgg_is_admin_logged_in()) {
509  $actions = array();
510  if (!$user->isBanned()) {
511  $actions[] = 'ban';
512  } else {
513  $actions[] = 'unban';
514  }
515  $actions[] = 'delete';
516  $actions[] = 'resetpassword';
517  if (!$user->isAdmin()) {
518  $actions[] = 'makeadmin';
519  } else {
520  $actions[] = 'removeadmin';
521  }
522 
523  foreach ($actions as $action) {
524  $url = "action/admin/user/$action?guid={$user->guid}";
525  $url = elgg_add_action_tokens_to_url($url);
526  $item = new \ElggMenuItem($action, elgg_echo($action), $url);
527  $item->setSection('admin');
528  $item->setConfirmText(true);
529 
530  $return[] = $item;
531  }
532 
533  $url = "profile/$user->username/edit";
534  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
535  $item->setSection('admin');
536  $return[] = $item;
537 
538  $url = "avatar/edit/$user->username";
539  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
540  $item->setSection('admin');
541  $return[] = $item;
542 
543  $url = "settings/user/$user->username";
544  $item = new \ElggMenuItem('settings:edit', elgg_echo('settings:edit'), $url);
545  $item->setSection('admin');
546  $return[] = $item;
547 
548  $url = "activity/owner/$user->username";
549  $item = new \ElggMenuItem('activity:owner', elgg_echo('activity:owner'), $url);
550  $item->setSection('action');
551  $return[] = $item;
552  }
553 
554  return $return;
555 }
556 
568 function elgg_users_setup_entity_menu($hook, $type, $return, $params) {
569  if (elgg_in_context('widgets')) {
570  return $return;
571  }
572 
573  $entity = $params['entity'];
574  if (!elgg_instanceof($entity, 'user')) {
575  return $return;
576  }
577  /* @var \ElggUser $entity */
578 
579  if ($entity->isBanned()) {
580  $banned = elgg_echo('banned');
581  $options = array(
582  'name' => 'banned',
583  'text' => "<span>$banned</span>",
584  'href' => false,
585  'priority' => 0,
586  );
587  $return = array(\ElggMenuItem::factory($options));
588  } else {
589  $return = array();
590  $location = $entity->location;
591  if (is_string($location) && $location !== '') {
592  $location = htmlspecialchars($location, ENT_QUOTES, 'UTF-8', false);
593  $options = array(
594  'name' => 'location',
595  'text' => "<span>$location</span>",
596  'href' => false,
597  'priority' => 150,
598  );
599  $return[] = \ElggMenuItem::factory($options);
600  }
601  }
602 
603  return $return;
604 }
605 
614 function elgg_profile_fields_setup() {
615  global $CONFIG;
616 
617  $profile_defaults = array (
618  'description' => 'longtext',
619  'briefdescription' => 'text',
620  'location' => 'location',
621  'interests' => 'tags',
622  'skills' => 'tags',
623  'contactemail' => 'email',
624  'phone' => 'text',
625  'mobile' => 'text',
626  'website' => 'url',
627  'twitter' => 'text',
628  );
629 
630  $loaded_defaults = array();
631  $fieldlist = elgg_get_config('profile_custom_fields');
632  if ($fieldlist || $fieldlist === '0') {
633  $fieldlistarray = explode(',', $fieldlist);
634  foreach ($fieldlistarray as $listitem) {
635  if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
636  $type = elgg_get_config("admin_defined_profile_type_{$listitem}");
637  $loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
638  add_translation(get_current_language(), array("profile:admin_defined_profile_{$listitem}" => $translation));
639  }
640  }
641  }
642 
643  if (count($loaded_defaults)) {
644  $CONFIG->profile_using_custom = true;
645  $profile_defaults = $loaded_defaults;
646  }
647 
648  $CONFIG->profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
649 
650  // register any tag metadata names
651  foreach ($CONFIG->profile_fields as $name => $type) {
652  if ($type == 'tags' || $type == 'location' || $type == 'tag') {
653  elgg_register_tag_metadata_name($name);
654  // register a tag name translation
655  add_translation(get_current_language(), array("tag_names:$name" => elgg_echo("profile:$name")));
656  }
657  }
658 }
659 
669 function elgg_avatar_page_handler($page) {
670  $user = get_user_by_username(elgg_extract(1, $page));
671  if ($user) {
672  elgg_set_page_owner_guid($user->getGUID());
673  }
674 
675  if ($page[0] == 'edit') {
676  echo elgg_view_resource("avatar/edit");
677  } else {
678  elgg_deprecated_notice("/avatar/view page handler has been deprecated and will be removed. Use elgg_get_inline_url() instead.", '2.2');
679  echo elgg_view_resource("avatar/view", [
680  'size' => elgg_extract(2, $page),
681  ]);
682  }
683 
684  return true;
685 }
686 
695  $user = get_user_by_username($page[0]);
697 
698  if ($page[1] == 'edit') {
699  echo elgg_view_resource("profile/edit");
700  return true;
701  }
702  return false;
703 }
704 
711 function users_pagesetup() {
712 
714  $viewer = elgg_get_logged_in_user_entity();
715 
716  if ($owner) {
717  elgg_register_menu_item('page', array(
718  'name' => 'edit_avatar',
719  'href' => "avatar/edit/{$owner->username}",
720  'text' => elgg_echo('avatar:edit'),
721  'section' => '1_profile',
722  'contexts' => array('settings'),
723  ));
724 
725  elgg_register_menu_item('page', array(
726  'name' => 'edit_profile',
727  'href' => "profile/{$owner->username}/edit",
728  'text' => elgg_echo('profile:edit'),
729  'section' => '1_profile',
730  'contexts' => array('settings'),
731  ));
732  }
733 
734  // topbar
735  if ($viewer) {
736  elgg_register_menu_item('topbar', array(
737  'name' => 'usersettings',
738  'href' => "settings/user/{$viewer->username}",
739  'text' => elgg_view_icon('settings') . elgg_echo('settings'),
740  'priority' => 500,
741  'section' => 'alt',
742  ));
743 
744  elgg_register_menu_item('topbar', array(
745  'name' => 'logout',
746  'href' => "action/logout",
747  'text' => elgg_echo('logout'),
748  'is_action' => true,
749  'priority' => 1000,
750  'section' => 'alt',
751  ));
752  }
753 }
754 
765 
766  $entity = elgg_extract('entity', $params);
767  $size = elgg_extract('size', $params, 'medium');
768 
769  $icon->owner_guid = $entity->guid;
770  $icon->setFilename("profile/{$entity->guid}{$size}.jpg");
771 
772  return $icon;
773 }
774 
781 function users_init() {
782 
783  elgg_register_page_handler('register', 'elgg_user_account_page_handler');
784  elgg_register_page_handler('forgotpassword', 'elgg_user_account_page_handler');
785  elgg_register_page_handler('changepassword', 'elgg_user_account_page_handler');
786  elgg_register_page_handler('login', 'elgg_user_account_page_handler');
787  elgg_register_page_handler('avatar', 'elgg_avatar_page_handler');
788  elgg_register_page_handler('profile', 'elgg_profile_page_handler');
789 
790  elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu');
791 
792  elgg_register_action('register', '', 'public');
793  elgg_register_action('useradd', '', 'admin');
794  elgg_register_action('avatar/upload');
795  elgg_register_action('avatar/crop');
796  elgg_register_action('avatar/remove');
797  elgg_register_action('profile/edit');
798 
799  elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
800 
801  elgg_register_action('user/changepassword', '', 'public');
802  elgg_register_action('user/requestnewpassword', '', 'public');
803 
804  // Register the user type
805  elgg_register_entity_type('user', '');
806 
807  elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_users_setup_entity_menu', 501);
808 
809  elgg_register_event_handler('create', 'user', 'user_create_hook_add_site_relationship');
810 
811  elgg_register_plugin_hook_handler('entity:icon:file', 'user', '_elgg_user_set_icon_file');
812 }
813 
825 function users_test($hook, $type, $value, $params) {
826  global $CONFIG;
827  $value[] = "{$CONFIG->path}engine/tests/ElggUserTest.php";
828  return $value;
829 }
830 
831 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
832  $events->registerHandler('init', 'system', 'users_init', 0);
833  $events->registerHandler('init', 'system', 'elgg_profile_fields_setup', 10000); // Ensure this runs after other plugins
834  $events->registerHandler('pagesetup', 'system', 'users_pagesetup', 0);
835  $hooks->registerHandler('unit_test', 'system', 'users_test');
836 };
users_init()
Users initialisation function, which establishes the page handler.
Definition: users.php:781
elgg_view_icon($name, $vars=array())
View one of the icons.
Definition: views.php:1447
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:831
$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's registered email address.
Definition: users.php:153
$action
Definition: full.php:125
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
action($action, $forwarder="")
Perform an action.
Definition: actions.php:47
font size
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:500
force_user_password_reset($user_guid, $password)
Low level function to reset a given user'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:26
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:764
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:715
unban_user($user_guid)
Unban a user.
Definition: users.php:52
users_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: users.php:825
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'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's URL for inline display Suitable for displaying cacheable resources, such as user avatars.
Definition: filestore.php:613
get_user_by_code($hash)
Get user by persistent login password.
Definition: users.php:109
$owner_guid
$limit
Definition: userpicker.php:38
$owner
Definition: crop.php:8
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
get_user_entity_as_row($guid)
Return the user specific details of a user by a row.
Definition: users.php:18
global $CONFIG
$user
Definition: ban.php:13
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:711
_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:1310
$password
Definition: login.php:25
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:538
$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:85
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:506
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg widget edit
remove_user_admin($user_guid)
Removes user $guid's admin flag.
Definition: users.php:74
elgg_profile_page_handler($page)
Profile page handler.
Definition: users.php:694
if(!$display_name) $type
Definition: delete.php:27