Elgg  Version 1.11
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  $base_dir = elgg_get_root_path() . 'pages/account';
382  switch ($handler) {
383  case 'login':
384  require_once("$base_dir/login.php");
385  break;
386  case 'forgotpassword':
387  require_once("$base_dir/forgotten_password.php");
388  break;
389  case 'changepassword':
390  require_once("$base_dir/change_password.php");
391  break;
392  case 'register':
393  require_once("$base_dir/register.php");
394  break;
395  default:
396  return false;
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 = $params['entity'];
449  $size = $params['size'];
450 
451  if (isset($user->icontime)) {
452  return "avatar/view/$user->username/$size/$user->icontime";
453  } else {
454  return "_graphics/icons/user/default{$size}.gif";
455  }
456 }
457 
462 function elgg_user_hover_menu($hook, $type, $return, $params) {
463  $user = $params['entity'];
464  /* @var \ElggUser $user */
465 
466  if (elgg_is_logged_in()) {
467  if (elgg_get_logged_in_user_guid() == $user->guid) {
468  $url = "profile/$user->username/edit";
469  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
470  $item->setSection('action');
471  $return[] = $item;
472 
473  $url = "avatar/edit/$user->username";
474  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
475  $item->setSection('action');
476  $return[] = $item;
477  }
478  }
479 
480  // prevent admins from banning or deleting themselves
481  if (elgg_get_logged_in_user_guid() == $user->guid) {
482  return $return;
483  }
484 
485  if (elgg_is_admin_logged_in()) {
486  $actions = array();
487  if (!$user->isBanned()) {
488  $actions[] = 'ban';
489  } else {
490  $actions[] = 'unban';
491  }
492  $actions[] = 'delete';
493  $actions[] = 'resetpassword';
494  if (!$user->isAdmin()) {
495  $actions[] = 'makeadmin';
496  } else {
497  $actions[] = 'removeadmin';
498  }
499 
500  foreach ($actions as $action) {
501  $url = "action/admin/user/$action?guid={$user->guid}";
502  $url = elgg_add_action_tokens_to_url($url);
503  $item = new \ElggMenuItem($action, elgg_echo($action), $url);
504  $item->setSection('admin');
505  $item->setConfirmText(true);
506 
507  $return[] = $item;
508  }
509 
510  $url = "profile/$user->username/edit";
511  $item = new \ElggMenuItem('profile:edit', elgg_echo('profile:edit'), $url);
512  $item->setSection('admin');
513  $return[] = $item;
514 
515  $url = "avatar/edit/$user->username";
516  $item = new \ElggMenuItem('avatar:edit', elgg_echo('avatar:edit'), $url);
517  $item->setSection('admin');
518  $return[] = $item;
519 
520  $url = "settings/user/$user->username";
521  $item = new \ElggMenuItem('settings:edit', elgg_echo('settings:edit'), $url);
522  $item->setSection('admin');
523  $return[] = $item;
524 
525  $url = "activity/owner/$user->username";
526  $item = new \ElggMenuItem('activity:owner', elgg_echo('activity:owner'), $url);
527  $item->setSection('action');
528  $return[] = $item;
529  }
530 
531  return $return;
532 }
533 
545 function elgg_users_setup_entity_menu($hook, $type, $return, $params) {
546  if (elgg_in_context('widgets')) {
547  return $return;
548  }
549 
550  $entity = $params['entity'];
551  if (!elgg_instanceof($entity, 'user')) {
552  return $return;
553  }
554  /* @var \ElggUser $entity */
555 
556  if ($entity->isBanned()) {
557  $banned = elgg_echo('banned');
558  $options = array(
559  'name' => 'banned',
560  'text' => "<span>$banned</span>",
561  'href' => false,
562  'priority' => 0,
563  );
564  $return = array(\ElggMenuItem::factory($options));
565  } else {
566  $return = array();
567  $location = $entity->location;
568  if (is_string($location) && $location !== '') {
569  $location = htmlspecialchars($location, ENT_QUOTES, 'UTF-8', false);
570  $options = array(
571  'name' => 'location',
572  'text' => "<span>$location</span>",
573  'href' => false,
574  'priority' => 150,
575  );
576  $return[] = \ElggMenuItem::factory($options);
577  }
578  }
579 
580  return $return;
581 }
582 
591 function elgg_profile_fields_setup() {
592  global $CONFIG;
593 
594  $profile_defaults = array (
595  'description' => 'longtext',
596  'briefdescription' => 'text',
597  'location' => 'location',
598  'interests' => 'tags',
599  'skills' => 'tags',
600  'contactemail' => 'email',
601  'phone' => 'text',
602  'mobile' => 'text',
603  'website' => 'url',
604  'twitter' => 'text',
605  );
606 
607  $loaded_defaults = array();
608  $fieldlist = elgg_get_config('profile_custom_fields');
609  if ($fieldlist || $fieldlist === '0') {
610  $fieldlistarray = explode(',', $fieldlist);
611  foreach ($fieldlistarray as $listitem) {
612  if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
613  $type = elgg_get_config("admin_defined_profile_type_{$listitem}");
614  $loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
615  add_translation(get_current_language(), array("profile:admin_defined_profile_{$listitem}" => $translation));
616  }
617  }
618  }
619 
620  if (count($loaded_defaults)) {
621  $CONFIG->profile_using_custom = true;
622  $profile_defaults = $loaded_defaults;
623  }
624 
625  $CONFIG->profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
626 
627  // register any tag metadata names
628  foreach ($CONFIG->profile_fields as $name => $type) {
629  if ($type == 'tags' || $type == 'location' || $type == 'tag') {
630  elgg_register_tag_metadata_name($name);
631  // register a tag name translation
632  add_translation(get_current_language(), array("tag_names:$name" => elgg_echo("profile:$name")));
633  }
634  }
635 }
636 
647 function elgg_avatar_page_handler($page) {
648  global $CONFIG;
649 
650  $user = get_user_by_username($page[1]);
651  if ($user) {
652  elgg_set_page_owner_guid($user->getGUID());
653  }
654 
655  if ($page[0] == 'edit') {
656  require_once("{$CONFIG->path}pages/avatar/edit.php");
657  return true;
658  } else {
659  set_input('size', $page[2]);
660  require_once("{$CONFIG->path}pages/avatar/view.php");
661  return true;
662  }
663  return false;
664 }
665 
673 function elgg_profile_page_handler($page) {
674  global $CONFIG;
675 
676  $user = get_user_by_username($page[0]);
677  elgg_set_page_owner_guid($user->guid);
678 
679  if ($page[1] == 'edit') {
680  require_once("{$CONFIG->path}pages/profile/edit.php");
681  return true;
682  }
683  return false;
684 }
685 
692 function users_pagesetup() {
693 
694  $owner = elgg_get_page_owner_entity();
695  $viewer = elgg_get_logged_in_user_entity();
696 
697  if ($owner) {
698  elgg_register_menu_item('page', array(
699  'name' => 'edit_avatar',
700  'href' => "avatar/edit/{$owner->username}",
701  'text' => elgg_echo('avatar:edit'),
702  'section' => '1_profile',
703  'contexts' => array('settings'),
704  ));
705 
706  elgg_register_menu_item('page', array(
707  'name' => 'edit_profile',
708  'href' => "profile/{$owner->username}/edit",
709  'text' => elgg_echo('profile:edit'),
710  'section' => '1_profile',
711  'contexts' => array('settings'),
712  ));
713  }
714 
715  // topbar
716  if ($viewer) {
717  elgg_register_menu_item('topbar', array(
718  'name' => 'usersettings',
719  'href' => "settings/user/{$viewer->username}",
720  'text' => elgg_view_icon('settings') . elgg_echo('settings'),
721  'priority' => 500,
722  'section' => 'alt',
723  ));
724 
725  elgg_register_menu_item('topbar', array(
726  'name' => 'logout',
727  'href' => "action/logout",
728  'text' => elgg_echo('logout'),
729  'is_action' => true,
730  'priority' => 1000,
731  'section' => 'alt',
732  ));
733  }
734 }
735 
742 function users_init() {
743 
744  elgg_register_page_handler('register', 'elgg_user_account_page_handler');
745  elgg_register_page_handler('forgotpassword', 'elgg_user_account_page_handler');
746  elgg_register_page_handler('changepassword', 'elgg_user_account_page_handler');
747  elgg_register_page_handler('login', 'elgg_user_account_page_handler');
748  elgg_register_page_handler('avatar', 'elgg_avatar_page_handler');
749  elgg_register_page_handler('profile', 'elgg_profile_page_handler');
750 
751  elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu');
752 
753  elgg_register_action('register', '', 'public');
754  elgg_register_action('useradd', '', 'admin');
755  elgg_register_action('avatar/upload');
756  elgg_register_action('avatar/crop');
757  elgg_register_action('avatar/remove');
758  elgg_register_action('profile/edit');
759 
760  elgg_register_plugin_hook_handler('entity:icon:url', 'user', 'user_avatar_hook');
761 
762  elgg_register_action('user/changepassword', '', 'public');
763  elgg_register_action('user/requestnewpassword', '', 'public');
764 
765  // Register the user type
766  elgg_register_entity_type('user', '');
767 
768  elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_users_setup_entity_menu', 501);
769 
770  elgg_register_event_handler('create', 'user', 'user_create_hook_add_site_relationship');
771 }
772 
784 function users_test($hook, $type, $value, $params) {
785  global $CONFIG;
786  $value[] = "{$CONFIG->path}engine/tests/ElggUserTest.php";
787  return $value;
788 }
789 
790 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
791  $events->registerHandler('init', 'system', 'users_init', 0);
792  $events->registerHandler('init', 'system', 'elgg_profile_fields_setup', 10000); // Ensure this runs after other plugins
793  $events->registerHandler('pagesetup', 'system', 'users_pagesetup', 0);
794  $hooks->registerHandler('unit_test', 'system', 'users_test');
795 };
execute_new_password_request($user_guid, $conf_code, $password=null)
Validate and change password for a user.
Definition: users.php:180
$CONFIG view
The current view type.
Definition: config.php:192
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
get_user_by_email($email)
Get an array of users from an email address.
Definition: users.php:120
elgg widget edit
Definition: admin.php:1239
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:15
$size
Definition: view.php:10
force_user_password_reset($user_guid, $password)
Low level function to reset a given user&#39;s password.
Definition: users.php:167
elgg form settings
Definition: admin.php:579
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
validate_username($username)
Simple function which ensures that a username contains only valid characters.
Definition: users.php:205
if(!$count) $offset
Definition: pagination.php:25
elgg menu user
Definition: admin.php:192
$guid
Removes an admin notice.
$email
Definition: register.php:15
unban_user($user_guid)
Unban a user.
Definition: users.php:52
$action
get_user_by_username($username)
Get user by username.
Definition: users.php:98
disable_user_entities($owner_guid)
Disables all of a user&#39;s entities.
Definition: users.php:29
$options
Definition: index.php:14
ban_user($user_guid, $reason="")
Ban a user.
Definition: users.php:41
get_user_by_code($hash)
Get user by persistent login password.
Definition: users.php:109
$owner_guid
$limit
Definition: userpicker.php:31
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg avatar
Definition: admin.php:1350
$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
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
$user
Definition: ban.php:13
elgg global
Pointer to the global context.
Definition: elgglib.js:12
ui datepicker td span
Definition: admin.php:634
$password
Definition: login.php:25
logout()
Log the current user out.
Definition: sessions.php:372
login(\ElggUser $user, $persistent=false)
Logs in a specified .
Definition: sessions.php:320
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
generate_random_cleartext_password()
Generate a random 12 character clear text password.
Definition: users.php:189
$user_guid
Avatar remove action.
Definition: remove.php:6
remove_user_admin($user_guid)
Removes user $guid&#39;s admin flag.
Definition: users.php:74
elgg action
Definition: ajax.js:197