user.pages.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. /**
  3. * @file
  4. * User page callback file for the user module.
  5. */
  6. /**
  7. * Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing users.
  8. */
  9. function user_autocomplete($string = '') {
  10. $matches = array();
  11. if ($string) {
  12. $result = db_select('users')->fields('users', array('name'))->condition('name', db_like($string) . '%', 'LIKE')->range(0, 10)->execute();
  13. foreach ($result as $user) {
  14. $matches[$user->name] = check_plain($user->name);
  15. }
  16. }
  17. drupal_json_output($matches);
  18. }
  19. /**
  20. * Form builder; Request a password reset.
  21. *
  22. * @ingroup forms
  23. * @see user_pass_validate()
  24. * @see user_pass_submit()
  25. */
  26. function user_pass() {
  27. global $user;
  28. $form['name'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Username or e-mail address'),
  31. '#size' => 60,
  32. '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH),
  33. '#required' => TRUE,
  34. '#default_value' => isset($_GET['name']) ? $_GET['name'] : '',
  35. );
  36. // Allow logged in users to request this also.
  37. if ($user->uid > 0) {
  38. $form['name']['#type'] = 'value';
  39. $form['name']['#value'] = $user->mail;
  40. $form['mail'] = array(
  41. '#prefix' => '<p>',
  42. // As of https://www.drupal.org/node/889772 the user no longer must log
  43. // out (if they are still logged in when using the password reset link,
  44. // they will be logged out automatically then), but this text is kept as
  45. // is to avoid breaking translations as well as to encourage the user to
  46. // log out manually at a time of their own choosing (when it will not
  47. // interrupt anything else they may have been in the middle of doing).
  48. '#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)),
  49. '#suffix' => '</p>',
  50. );
  51. }
  52. $form['actions'] = array('#type' => 'actions');
  53. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password'));
  54. return $form;
  55. }
  56. /**
  57. * Form validation handler for user_pass().
  58. *
  59. * @see user_pass_submit()
  60. */
  61. function user_pass_validate($form, &$form_state) {
  62. if (isset($form_state['values']['name']) && !is_scalar($form_state['values']['name'])) {
  63. form_set_error('name', t('An illegal value has been detected. Please contact the site administrator.'));
  64. return;
  65. }
  66. $user_pass_reset_ip_window = variable_get('user_pass_reset_ip_window', 3600);
  67. // Do not allow any password reset from the current user's IP if the limit
  68. // has been reached. Default is 50 attempts allowed in one hour. This is
  69. // independent of the per-user limit to catch attempts from one IP to request
  70. // resets for many different user accounts. We have a reasonably high limit
  71. // since there may be only one apparent IP for all users at an institution.
  72. if (!flood_is_allowed('pass_reset_ip', variable_get('user_pass_reset_ip_limit', 50), $user_pass_reset_ip_window)) {
  73. form_set_error('name', t('Sorry, too many password reset attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href="@url">request a new password</a>.', array('@url' => url('user/password'))));
  74. return;
  75. }
  76. // Always register an per-IP event.
  77. flood_register_event('pass_reset_ip', $user_pass_reset_ip_window);
  78. $name = trim($form_state['values']['name']);
  79. // Try to load by email.
  80. $users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
  81. $account = reset($users);
  82. if (!$account) {
  83. // No success, try to load by name.
  84. $users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
  85. $account = reset($users);
  86. }
  87. if (isset($account->uid)) {
  88. // Register user flood events based on the uid only, so they can be cleared
  89. // when a password is reset successfully.
  90. $identifier = $account->uid;
  91. $user_pass_reset_user_window = variable_get('user_pass_reset_user_window', 21600);
  92. $user_pass_reset_user_limit = variable_get('user_pass_reset_user_limit', 5);
  93. // Don't allow password reset if the limit for this user has been reached.
  94. // Default is to allow 5 passwords resets every 6 hours.
  95. if (!flood_is_allowed('pass_reset_user', $user_pass_reset_user_limit, $user_pass_reset_user_window, $identifier)) {
  96. form_set_error('name', format_plural($user_pass_reset_user_limit, 'Sorry, there has been more than one password reset attempt for this account. It is temporarily blocked. Try again later or <a href="@url">login with your password</a>.', 'Sorry, there have been more than @count password reset attempts for this account. It is temporarily blocked. Try again later or <a href="@url">login with your password</a>.', array('@url' => url('user/login'))));
  97. return;
  98. }
  99. // Register a per-user event.
  100. flood_register_event('pass_reset_user', $user_pass_reset_user_window, $identifier);
  101. form_set_value(array('#parents' => array('account')), $account, $form_state);
  102. }
  103. else {
  104. form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
  105. }
  106. }
  107. /**
  108. * Form submission handler for user_pass().
  109. *
  110. * @see user_pass_validate()
  111. */
  112. function user_pass_submit($form, &$form_state) {
  113. global $language;
  114. $account = $form_state['values']['account'];
  115. // Mail one time login URL and instructions using current language.
  116. $mail = _user_mail_notify('password_reset', $account, $language);
  117. if (!empty($mail)) {
  118. watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
  119. drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
  120. }
  121. $form_state['redirect'] = 'user';
  122. return;
  123. }
  124. /**
  125. * Menu callback; process one time login link and redirects to the user page on success.
  126. *
  127. * In order to never disclose password reset hashes via referrer headers or
  128. * web browser history, this function always issues a redirect when a valid
  129. * password reset hash is in the URL.
  130. */
  131. function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
  132. global $user;
  133. // Check for a reset hash in the session. Immediately remove it (to prevent it
  134. // from being reused, for example if this page is visited again via the
  135. // browser history) and store it for later use.
  136. if (isset($_SESSION['pass_reset_hash'])) {
  137. $session_reset_hash = $_SESSION['pass_reset_hash'];
  138. unset($_SESSION['pass_reset_hash']);
  139. }
  140. else {
  141. $session_reset_hash = NULL;
  142. }
  143. // When processing the one-time login link, we have to make sure that a user
  144. // isn't already logged in.
  145. if ($user->uid) {
  146. // The existing user is already logged in. Log them out and reload the
  147. // current page so the password reset process can continue.
  148. if ($user->uid == $uid) {
  149. // Preserve the current destination (if any) and ensure the redirect goes
  150. // back to the current page; any custom destination set in
  151. // hook_user_logout() and intended for regular logouts would not be
  152. // appropriate here.
  153. $destination = array();
  154. if (isset($_GET['destination'])) {
  155. $destination = drupal_get_destination();
  156. }
  157. user_logout_current_user();
  158. unset($_GET['destination']);
  159. drupal_goto(current_path(), array('query' => drupal_get_query_parameters() + $destination));
  160. }
  161. // A different user is already logged in on the computer.
  162. else {
  163. $reset_link_account = user_load($uid);
  164. if (!empty($reset_link_account)) {
  165. drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.',
  166. array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))), 'warning');
  167. } else {
  168. // Invalid one-time link specifies an unknown user.
  169. drupal_set_message(t('The one-time login link you clicked is invalid.'), 'error');
  170. }
  171. drupal_goto();
  172. }
  173. }
  174. else {
  175. // Time out, in seconds, until login URL expires. Defaults to 24 hours =
  176. // 86400 seconds.
  177. $timeout = variable_get('user_password_reset_timeout', 86400);
  178. $current = REQUEST_TIME;
  179. // Some redundant checks for extra security ?
  180. $users = user_load_multiple(array($uid), array('status' => '1'));
  181. if ($timestamp <= $current && $account = reset($users)) {
  182. // No time out for first time login.
  183. if ($account->login && $current - $timestamp > $timeout) {
  184. drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'), 'error');
  185. drupal_goto('user/password');
  186. }
  187. // Validate the reset hash from the URL or from the session.
  188. $is_valid = FALSE;
  189. if ($account->uid && $timestamp >= $account->login && $timestamp <= $current) {
  190. // If the reset hash in the URL is valid, put it in the session and
  191. // redirect to the same URL but with the hash replaced by an invalid
  192. // one ("confirm"). This prevents disclosing the hash via referrer
  193. // headers or web browser history.
  194. if ($hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
  195. if ($action === 'login') {
  196. // The 'login' action redirects directly to the user edit form.
  197. $is_valid = TRUE;
  198. }
  199. else {
  200. $_SESSION['pass_reset_hash'] = $hashed_pass;
  201. $args = arg();
  202. foreach ($args as &$arg) {
  203. if ($arg == $hashed_pass) {
  204. $arg = 'confirm';
  205. }
  206. }
  207. $path = implode('/', $args);
  208. drupal_goto($path, array('query' => drupal_get_query_parameters()));
  209. }
  210. }
  211. // If the reset hash from the session is valid, use that.
  212. elseif ($session_reset_hash && $session_reset_hash == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
  213. $is_valid = TRUE;
  214. }
  215. }
  216. if ($is_valid) {
  217. // First stage is a confirmation form, then login
  218. if ($action == 'login') {
  219. // Set the new user.
  220. $user = $account;
  221. // user_login_finalize() also updates the login timestamp of the
  222. // user, which invalidates further use of the one-time login link.
  223. user_login_finalize();
  224. // Clear any password reset flood events for this user.
  225. flood_clear_event('pass_reset_user', $account->uid);
  226. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
  227. drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
  228. // Let the user's password be changed without the current password check.
  229. $token = drupal_random_key();
  230. $_SESSION['pass_reset_' . $user->uid] = $token;
  231. drupal_goto('user/' . $user->uid . '/edit', array('query' => array('pass-reset-token' => $token)));
  232. }
  233. else {
  234. $form['message'] = array('#markup' => t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to log in to the site and change your password.</p>', array('%user_name' => $account->name, '%expiration_date' => format_date($timestamp + $timeout))));
  235. $form['help'] = array('#markup' => '<p>' . t('This login can be used only once.') . '</p>');
  236. $form['actions'] = array('#type' => 'actions');
  237. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
  238. $form['#action'] = url("user/reset/$uid/$timestamp/$session_reset_hash/login");
  239. // Prevent the browser from storing this page so that the token will
  240. // not be visible in the form action if the back button is used to
  241. // revisit this page.
  242. drupal_add_http_header('Cache-Control', 'no-store');
  243. return $form;
  244. }
  245. }
  246. else {
  247. drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'error');
  248. drupal_goto('user/password');
  249. }
  250. }
  251. else {
  252. // Deny access, no more clues.
  253. // Everything will be in the watchdog's URL for the administrator to check.
  254. drupal_access_denied();
  255. drupal_exit();
  256. }
  257. }
  258. }
  259. /**
  260. * Menu callback; logs the current user out, and redirects to the home page.
  261. */
  262. function user_logout() {
  263. user_logout_current_user();
  264. drupal_goto();
  265. }
  266. /**
  267. * Logs the current user out.
  268. */
  269. function user_logout_current_user() {
  270. global $user;
  271. watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
  272. module_invoke_all('user_logout', $user);
  273. // Destroy the current session, and reset $user to the anonymous user.
  274. session_destroy();
  275. }
  276. /**
  277. * Process variables for user-profile.tpl.php.
  278. *
  279. * @param array $variables
  280. * An associative array containing:
  281. * - elements: An associative array containing the user information and any
  282. * fields attached to the user. Properties used:
  283. * - #account: The user account of the profile being viewed.
  284. *
  285. * @see user-profile.tpl.php
  286. */
  287. function template_preprocess_user_profile(&$variables) {
  288. $account = $variables['elements']['#account'];
  289. // Helpful $user_profile variable for templates.
  290. foreach (element_children($variables['elements']) as $key) {
  291. $variables['user_profile'][$key] = $variables['elements'][$key];
  292. }
  293. // Preprocess fields.
  294. field_attach_preprocess('user', $account, $variables['elements'], $variables);
  295. }
  296. /**
  297. * Process variables for user-profile-item.tpl.php.
  298. *
  299. * The $variables array contains the following arguments:
  300. * - $element
  301. *
  302. * @see user-profile-item.tpl.php
  303. */
  304. function template_preprocess_user_profile_item(&$variables) {
  305. $variables['title'] = $variables['element']['#title'];
  306. $variables['value'] = $variables['element']['#markup'];
  307. $variables['attributes'] = '';
  308. if (isset($variables['element']['#attributes'])) {
  309. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  310. }
  311. }
  312. /**
  313. * Process variables for user-profile-category.tpl.php.
  314. *
  315. * The $variables array contains the following arguments:
  316. * - $element
  317. *
  318. * @see user-profile-category.tpl.php
  319. */
  320. function template_preprocess_user_profile_category(&$variables) {
  321. $variables['title'] = check_plain($variables['element']['#title']);
  322. $variables['profile_items'] = $variables['element']['#children'];
  323. $variables['attributes'] = '';
  324. if (isset($variables['element']['#attributes'])) {
  325. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  326. }
  327. }
  328. /**
  329. * Form builder; edit a user account or one of their profile categories.
  330. *
  331. * @ingroup forms
  332. * @see user_account_form()
  333. * @see user_account_form_validate()
  334. * @see user_profile_form_validate()
  335. * @see user_profile_form_submit()
  336. * @see user_cancel_confirm_form_submit()
  337. */
  338. function user_profile_form($form, &$form_state, $account, $category = 'account') {
  339. global $user;
  340. // During initial form build, add the entity to the form state for use during
  341. // form building and processing. During a rebuild, use what is in the form
  342. // state.
  343. if (!isset($form_state['user'])) {
  344. $form_state['user'] = $account;
  345. }
  346. else {
  347. $account = $form_state['user'];
  348. }
  349. // @todo Legacy support. Modules are encouraged to access the entity using
  350. // $form_state. Remove in Drupal 8.
  351. $form['#user'] = $account;
  352. $form['#user_category'] = $category;
  353. if ($category == 'account') {
  354. user_account_form($form, $form_state);
  355. // Attach field widgets.
  356. $langcode = entity_language('user', $account);
  357. field_attach_form('user', $account, $form, $form_state, $langcode);
  358. }
  359. $form['actions'] = array('#type' => 'actions');
  360. $form['actions']['submit'] = array(
  361. '#type' => 'submit',
  362. '#value' => t('Save'),
  363. );
  364. if ($category == 'account') {
  365. $form['actions']['cancel'] = array(
  366. '#type' => 'submit',
  367. '#value' => t('Cancel account'),
  368. '#submit' => array('user_edit_cancel_submit'),
  369. '#access' => $account->uid > 1 && (($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users')),
  370. );
  371. }
  372. $form['#validate'][] = 'user_profile_form_validate';
  373. // Add the final user profile form submit handler.
  374. $form['#submit'][] = 'user_profile_form_submit';
  375. return $form;
  376. }
  377. /**
  378. * Form validation handler for user_profile_form().
  379. *
  380. * @see user_profile_form_submit()
  381. */
  382. function user_profile_form_validate($form, &$form_state) {
  383. entity_form_field_validate('user', $form, $form_state);
  384. }
  385. /**
  386. * Form submission handler for user_profile_form().
  387. *
  388. * @see user_profile_form_validate()
  389. */
  390. function user_profile_form_submit($form, &$form_state) {
  391. $account = $form_state['user'];
  392. $category = $form['#user_category'];
  393. // Remove unneeded values.
  394. form_state_values_clean($form_state);
  395. // Before updating the account entity, keep an unchanged copy for use with
  396. // user_save() later. This is necessary for modules implementing the user
  397. // hooks to be able to react on changes by comparing the values of $account
  398. // and $edit.
  399. $account_unchanged = clone $account;
  400. entity_form_submit_build_entity('user', $account, $form, $form_state);
  401. // Populate $edit with the properties of $account, which have been edited on
  402. // this form by taking over all values, which appear in the form values too.
  403. $edit = array_intersect_key((array) $account, $form_state['values']);
  404. user_save($account_unchanged, $edit, $category);
  405. $form_state['values']['uid'] = $account->uid;
  406. if ($category == 'account' && !empty($edit['pass'])) {
  407. // Remove the password reset tag since a new password was saved.
  408. unset($_SESSION['pass_reset_'. $account->uid]);
  409. }
  410. // Clear the page cache because pages can contain usernames and/or profile information:
  411. cache_clear_all();
  412. drupal_set_message(t('The changes have been saved.'));
  413. }
  414. /**
  415. * Submit function for the 'Cancel account' button on the user edit form.
  416. */
  417. function user_edit_cancel_submit($form, &$form_state) {
  418. $destination = array();
  419. if (isset($_GET['destination'])) {
  420. $destination = drupal_get_destination();
  421. unset($_GET['destination']);
  422. }
  423. // Note: We redirect from user/uid/edit to user/uid/cancel to make the tabs disappear.
  424. $form_state['redirect'] = array("user/" . $form['#user']->uid . "/cancel", array('query' => $destination));
  425. }
  426. /**
  427. * Form builder; confirm form for cancelling user account.
  428. *
  429. * @ingroup forms
  430. * @see user_edit_cancel_submit()
  431. */
  432. function user_cancel_confirm_form($form, &$form_state, $account) {
  433. global $user;
  434. $form['_account'] = array('#type' => 'value', '#value' => $account);
  435. // Display account cancellation method selection, if allowed.
  436. $admin_access = user_access('administer users');
  437. $can_select_method = $admin_access || user_access('select account cancellation method');
  438. $form['user_cancel_method'] = array(
  439. '#type' => 'item',
  440. '#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
  441. '#access' => $can_select_method,
  442. );
  443. $form['user_cancel_method'] += user_cancel_methods();
  444. // Allow user administrators to skip the account cancellation confirmation
  445. // mail (by default), as long as they do not attempt to cancel their own
  446. // account.
  447. $override_access = $admin_access && ($account->uid != $user->uid);
  448. $form['user_cancel_confirm'] = array(
  449. '#type' => 'checkbox',
  450. '#title' => t('Require e-mail confirmation to cancel account.'),
  451. '#default_value' => ($override_access ? FALSE : TRUE),
  452. '#access' => $override_access,
  453. '#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
  454. );
  455. // Also allow to send account canceled notification mail, if enabled.
  456. $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
  457. $form['user_cancel_notify'] = array(
  458. '#type' => 'checkbox',
  459. '#title' => t('Notify user when account is canceled.'),
  460. '#default_value' => ($override_access ? FALSE : $default_notify),
  461. '#access' => $override_access && $default_notify,
  462. '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
  463. );
  464. // Prepare confirmation form page title and description.
  465. if ($account->uid == $user->uid) {
  466. $question = t('Are you sure you want to cancel your account?');
  467. }
  468. else {
  469. $question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
  470. }
  471. $description = '';
  472. if ($can_select_method) {
  473. $description = t('Select the method to cancel the account above.');
  474. foreach (element_children($form['user_cancel_method']) as $element) {
  475. unset($form['user_cancel_method'][$element]['#description']);
  476. }
  477. }
  478. else {
  479. // The radio button #description is used as description for the confirmation
  480. // form.
  481. foreach (element_children($form['user_cancel_method']) as $element) {
  482. if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
  483. $description = $form['user_cancel_method'][$element]['#description'];
  484. }
  485. unset($form['user_cancel_method'][$element]['#description']);
  486. }
  487. }
  488. // Always provide entity id in the same form key as in the entity edit form.
  489. $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  490. return confirm_form($form,
  491. $question,
  492. 'user/' . $account->uid,
  493. $description . ' ' . t('This action cannot be undone.'),
  494. t('Cancel account'), t('Cancel'));
  495. }
  496. /**
  497. * Submit handler for the account cancellation confirm form.
  498. *
  499. * @see user_cancel_confirm_form()
  500. * @see user_multiple_cancel_confirm_submit()
  501. */
  502. function user_cancel_confirm_form_submit($form, &$form_state) {
  503. global $user;
  504. $account = $form_state['values']['_account'];
  505. // Cancel account immediately, if the current user has administrative
  506. // privileges, no confirmation mail shall be sent, and the user does not
  507. // attempt to cancel the own account.
  508. if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
  509. user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
  510. $form_state['redirect'] = 'admin/people';
  511. }
  512. else {
  513. // Store cancelling method and whether to notify the user in $account for
  514. // user_cancel_confirm().
  515. $edit = array(
  516. 'user_cancel_method' => $form_state['values']['user_cancel_method'],
  517. 'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
  518. );
  519. $account = user_save($account, $edit);
  520. _user_mail_notify('cancel_confirm', $account);
  521. drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
  522. watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
  523. $form_state['redirect'] = "user/$account->uid";
  524. }
  525. }
  526. /**
  527. * Helper function to return available account cancellation methods.
  528. *
  529. * See documentation of hook_user_cancel_methods_alter().
  530. *
  531. * @return
  532. * An array containing all account cancellation methods as form elements.
  533. *
  534. * @see hook_user_cancel_methods_alter()
  535. * @see user_admin_settings()
  536. * @see user_cancel_confirm_form()
  537. * @see user_multiple_cancel_confirm()
  538. */
  539. function user_cancel_methods() {
  540. $methods = array(
  541. 'user_cancel_block' => array(
  542. 'title' => t('Disable the account and keep its content.'),
  543. 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'),
  544. ),
  545. 'user_cancel_block_unpublish' => array(
  546. 'title' => t('Disable the account and unpublish its content.'),
  547. 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'),
  548. ),
  549. 'user_cancel_reassign' => array(
  550. 'title' => t('Delete the account and make its content belong to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  551. 'description' => t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  552. ),
  553. 'user_cancel_delete' => array(
  554. 'title' => t('Delete the account and its content.'),
  555. 'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
  556. 'access' => user_access('administer users'),
  557. ),
  558. );
  559. // Allow modules to customize account cancellation methods.
  560. drupal_alter('user_cancel_methods', $methods);
  561. // Turn all methods into real form elements.
  562. $default_method = variable_get('user_cancel_method', 'user_cancel_block');
  563. foreach ($methods as $name => $method) {
  564. $form[$name] = array(
  565. '#type' => 'radio',
  566. '#title' => $method['title'],
  567. '#description' => (isset($method['description']) ? $method['description'] : NULL),
  568. '#return_value' => $name,
  569. '#default_value' => $default_method,
  570. '#parents' => array('user_cancel_method'),
  571. );
  572. }
  573. return $form;
  574. }
  575. /**
  576. * Menu callback; Cancel a user account via e-mail confirmation link.
  577. *
  578. * @see user_cancel_confirm_form()
  579. * @see user_cancel_url()
  580. */
  581. function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
  582. // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
  583. $timeout = 86400;
  584. $current = REQUEST_TIME;
  585. // Basic validation of arguments.
  586. if (isset($account->data['user_cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
  587. // Validate expiration and hashed password/login.
  588. if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
  589. $edit = array(
  590. 'user_cancel_notify' => isset($account->data['user_cancel_notify']) ? $account->data['user_cancel_notify'] : variable_get('user_mail_status_canceled_notify', FALSE),
  591. );
  592. user_cancel($edit, $account->uid, $account->data['user_cancel_method']);
  593. // Since user_cancel() is not invoked via Form API, batch processing needs
  594. // to be invoked manually and should redirect to the front page after
  595. // completion.
  596. batch_process('');
  597. }
  598. else {
  599. drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'error');
  600. drupal_goto("user/$account->uid/cancel");
  601. }
  602. }
  603. return MENU_ACCESS_DENIED;
  604. }
  605. /**
  606. * Page callback: Displays the user page.
  607. *
  608. * Displays user profile if user is logged in, or login form for anonymous
  609. * users.
  610. *
  611. * @return
  612. * A render array for either a user profile or a login form.
  613. *
  614. * @see user_view_page()
  615. * @see user_login()
  616. */
  617. function user_page() {
  618. global $user;
  619. if ($user->uid) {
  620. menu_set_active_item('user/' . $user->uid);
  621. return menu_execute_active_handler(NULL, FALSE);
  622. }
  623. else {
  624. return drupal_get_form('user_login');
  625. }
  626. }