Elgg  Version 2.3
user_settings.php
Go to the documentation of this file.
1 <?php
20  $current_password = get_input('current_password', null, false);
21  $password = get_input('password', null, false);
22  $password2 = get_input('password2', null, false);
23  $user_guid = get_input('guid');
24 
25  if ($user_guid) {
27  } else {
29  }
30 
31  if ($user && $password) {
32  // let admin user change anyone's password without knowing it except his own.
34  $credentials = array(
35  'username' => $user->username,
36  'password' => $current_password
37  );
38 
39  try {
40  pam_auth_userpass($credentials);
41  } catch (LoginException $e) {
42  register_error(elgg_echo('LoginException:ChangePasswordFailure'));
43  return false;
44  }
45  }
46 
47  try {
49  } catch (RegistrationException $e) {
50  register_error($e->getMessage());
51  return false;
52  }
53 
54  if ($result) {
55  if ($password == $password2) {
56  $user->setPassword($password);
57  _elgg_services()->persistentLogin->handlePasswordChange($user, elgg_get_logged_in_user_entity());
58 
59  if ($user->save()) {
60  system_message(elgg_echo('user:password:success'));
61  return true;
62  } else {
63  register_error(elgg_echo('user:password:fail'));
64  }
65  } else {
66  register_error(elgg_echo('user:password:fail:notsame'));
67  }
68  } else {
69  register_error(elgg_echo('user:password:fail:tooshort'));
70  }
71  } else {
72  // no change
73  return;
74  }
75 
76  return false;
77 }
78 
88 function _elgg_set_user_name() {
89  $name = get_input('name');
90  $user_guid = get_input('guid');
91 
92  if (!isset($name)) {
93  return;
94  }
95 
96  $name = strip_tags($name);
97  if ($user_guid) {
99  } else {
101  }
102 
103  if (elgg_strlen($name) > 50) {
104  register_error(elgg_echo('user:name:fail'));
105  return false;
106  }
107 
108  if ($user && $user->canEdit() && $name) {
109  if ($name != $user->name) {
110  $user->name = $name;
111  if ($user->save()) {
112  system_message(elgg_echo('user:name:success'));
113  return true;
114  } else {
115  register_error(elgg_echo('user:name:fail'));
116  }
117  } else {
118  // no change
119  return;
120  }
121  } else {
122  register_error(elgg_echo('user:name:fail'));
123  }
124  return false;
125 }
126 
137  $language = get_input('language');
138  $user_guid = get_input('guid');
139 
140  if (!isset($language)) {
141  return;
142  }
143 
144  if ($user_guid) {
146  } else {
148  }
149 
150  if ($user && $language) {
151  if (strcmp($language, $user->language) != 0) {
152  $user->language = $language;
153  if ($user->save()) {
154  system_message(elgg_echo('user:language:success'));
155  return true;
156  } else {
157  register_error(elgg_echo('user:language:fail'));
158  }
159  } else {
160  // no change
161  return;
162  }
163  } else {
164  register_error(elgg_echo('user:language:fail'));
165  }
166  return false;
167 }
168 
179  $email = get_input('email');
180  $user_guid = get_input('guid');
181 
182  if (!isset($email)) {
183  return;
184  }
185 
186  if ($user_guid) {
188  } else {
190  }
191 
192  if (!is_email_address($email)) {
193  register_error(elgg_echo('email:save:fail'));
194  return false;
195  }
196 
197  if ($user) {
198  if (strcmp($email, $user->email) != 0) {
199  if (!get_user_by_email($email)) {
200  if ($user->email != $email) {
201 
202  $user->email = $email;
203  if ($user->save()) {
204  system_message(elgg_echo('email:save:success'));
205  return true;
206  } else {
207  register_error(elgg_echo('email:save:fail'));
208  }
209  }
210  } else {
211  register_error(elgg_echo('registration:dupeemail'));
212  }
213  } else {
214  // no change
215  return;
216  }
217  } else {
218  register_error(elgg_echo('email:save:fail'));
219  }
220  return false;
221 }
222 
233 
234  if (!elgg_get_config('allow_user_default_access')) {
235  return;
236  }
237 
238  $default_access = get_input('default_access');
239  $user_guid = get_input('guid');
240 
241  if ($user_guid) {
243  } else {
245  }
246 
247  if ($user) {
248  $current_default_access = $user->getPrivateSetting('elgg_default_access');
249  if ($default_access !== $current_default_access) {
250  if ($user->setPrivateSetting('elgg_default_access', $default_access)) {
251  system_message(elgg_echo('user:default_access:success'));
252  return true;
253  } else {
254  register_error(elgg_echo('user:default_access:failure'));
255  }
256  } else {
257  // no change
258  return;
259  }
260  } else {
261  register_error(elgg_echo('user:default_access:failure'));
262  }
263 
264  return false;
265 }
266 
275 
276  if (!$user) {
277  return;
278  }
279 
280  if (!elgg_in_context("settings")) {
281  return;
282  }
283 
284  $params = array(
285  'name' => '1_account',
286  'text' => elgg_echo('usersettings:user:opt:linktext'),
287  'href' => "settings/user/{$user->username}",
288  'section' => 'configure',
289  );
291  $params = array(
292  'name' => '1_plugins',
293  'text' => elgg_echo('usersettings:plugins:opt:linktext'),
294  'href' => '#',
295  'section' => 'configure',
296  );
298  $params = array(
299  'name' => '1_statistics',
300  'text' => elgg_echo('usersettings:statistics:opt:linktext'),
301  'href' => "settings/statistics/{$user->username}",
302  'section' => 'configure',
303  );
305 
306  // register plugin user settings menu items
307  $active_plugins = elgg_get_plugins();
308 
309  foreach ($active_plugins as $plugin) {
310  $plugin_id = $plugin->getID();
311  if (elgg_view_exists("usersettings/$plugin_id/edit") || elgg_view_exists("plugins/$plugin_id/usersettings")) {
312  if (elgg_language_key_exists($plugin_id . ':usersettings:title')) {
313  $title = elgg_echo($plugin_id . ':usersettings:title');
314  } else {
315  $title = $plugin->getFriendlyName();
316  }
317  $params = array(
318  'name' => $plugin_id,
319  'text' => $title,
320  'href' => "settings/plugins/{$user->username}/$plugin_id",
321  'parent_name' => '1_plugins',
322  'section' => 'configure',
323  );
325  }
326  }
327 
328  elgg_register_plugin_hook_handler("prepare", "menu:page", "_elgg_user_settings_menu_prepare");
329 }
330 
343  if (empty($value)) {
344  return $value;
345  }
346 
347  if (!elgg_in_context("settings")) {
348  return $value;
349  }
350 
351  $configure = elgg_extract("configure", $value);
352  if (empty($configure)) {
353  return $value;
354  }
355 
356  foreach ($configure as $index => $menu_item) {
357  if (!($menu_item instanceof ElggMenuItem)) {
358  continue;
359  }
360 
361  if ($menu_item->getName() == "1_plugins") {
362  if (!$menu_item->getChildren()) {
363  // no need for this menu item if it has no children
364  unset($value["configure"][$index]);
365  }
366  }
367  }
368 
369  return $value;
370 }
371 
381  if (!isset($page[0])) {
382  $page[0] = 'user';
383  }
384 
385  if (isset($page[1])) {
386  $user = get_user_by_username($page[1]);
388  } else {
391  }
392 
393  $vars['username'] = $user->username;
394 
395  switch ($page[0]) {
396  case 'statistics':
397  echo elgg_view_resource('settings/statistics', $vars);
398  return true;
399  case 'plugins':
400  if (isset($page[2])) {
401  $vars['plugin_id'] = $page[2];
402  echo elgg_view_resource('settings/tools', $vars);
403  return true;
404  }
405  break;
406  case 'user':
407  echo elgg_view_resource("settings/account", $vars);
408  return true;
409  }
410 
411  return false;
412 }
413 
421  elgg_register_page_handler('settings', '_elgg_user_settings_page_handler');
422 
423  elgg_register_event_handler('pagesetup', 'system', '_elgg_user_settings_menu_setup');
424 
425  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_language');
426  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_password');
427  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_default_access');
428  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_name');
429  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_email');
430 
431  elgg_register_action("usersettings/save");
432 
433  // extend the account settings form
434  elgg_extend_view('forms/account/settings', 'core/settings/account/name', 100);
435  elgg_extend_view('forms/account/settings', 'core/settings/account/password', 100);
436  elgg_extend_view('forms/account/settings', 'core/settings/account/email', 100);
437  elgg_extend_view('forms/account/settings', 'core/settings/account/language', 100);
438  elgg_extend_view('forms/account/settings', 'core/settings/account/default_access', 100);
439 }
440 
441 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
442  $events->registerHandler('init', 'system', '_elgg_user_settings_init');
443 };
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:133
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:293
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$password2
Definition: register.php:14
_elgg_user_settings_page_handler($page)
Page handler for user settings.
$plugin
get_user_by_email($email)
Get an array of users from an email address.
Definition: users.php:120
_elgg_set_user_language()
Set a user&#39;s language Returns null if no change is required or input is not present in the form Retur...
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:60
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
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
$e
Definition: metadata.php:12
$value
Definition: longtext.php:42
_elgg_user_settings_menu_setup()
Set up the menu for user settings.
$email
Definition: register.php:15
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
$vars['entity']
pam_auth_userpass(array $credentials=array())
Hook into the PAM system which accepts a username and password and attempts to authenticate it agains...
Definition: sessions.php:158
elgg_set_page_owner_guid($guid)
Set the guid of the entity that owns this page.
Definition: pageowner.php:72
$title
Definition: save.php:22
get_user_by_username($username)
Get user by username.
Definition: users.php:98
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
$params
Definition: login.php:72
is_email_address($address)
Validates an email address.
Definition: input.php:88
_elgg_set_user_name()
Set a user&#39;s display name Returns null if no change is required or input is not present in the form R...
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
validate_password($password)
Simple validation of a password.
Definition: users.php:267
$language
Definition: useradd.php:20
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
$plugin_id
Definition: save.php:16
_elgg_user_settings_menu_prepare($hook, $type, $value, $params)
Prepares the page menu to strip out empty plugins menu item for user settings.
$user
Definition: ban.php:13
_elgg_set_user_default_access()
Set a user&#39;s default access level Returns null if no change is required or input is not present in th...
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
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_set_user_email()
Set a user&#39;s email address Returns null if no change is required or input is not present in the form ...
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:56
_elgg_user_settings_init()
Initialize the user settings library.
_elgg_set_user_password()
Set a user&#39;s password Returns null if no change is required Returns true or false indicating success ...
elgg_extend_view($view, $view_extension, $priority=501)
Extends a view with another view.
Definition: views.php:380
_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 system_message
Wrapper function for system_messages.
Definition: elgglib.js:390
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:550
elgg_get_plugins($status= 'active', $site_guid=null)
Returns an ordered list of plugins.
Definition: plugins.php:132
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:399
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$index
Definition: gallery.php:49
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:96
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
if(!$user instanceof ElggUser) $default_access
$configure
Definition: save.php:23
if(!$display_name) $type
Definition: delete.php:27