user.pages.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. }
  104. /**
  105. * Form submission handler for user_pass().
  106. *
  107. * @see user_pass_validate()
  108. */
  109. function user_pass_submit($form, &$form_state) {
  110. global $language;
  111. $name = $form_state['values']['name'];
  112. if (isset($form_state['values']['account'])) {
  113. $account = $form_state['values']['account'];
  114. // Mail one time login URL and instructions using current language.
  115. $mail = _user_mail_notify('password_reset', $account, $language);
  116. if (!empty($mail)) {
  117. watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
  118. }
  119. }
  120. else {
  121. watchdog('user', 'Password reset form was submitted with an unknown or inactive account: %name.', array('%name' => $name));
  122. }
  123. $password_reset_text = variable_get('user_password_reset_text', t('If %identifier is a valid account, an email will be sent with instructions to reset your password.'));
  124. drupal_set_message(format_string($password_reset_text, array('%identifier' => $name)));
  125. $form_state['redirect'] = 'user';
  126. return;
  127. }
  128. /**
  129. * Menu callback; process one time login link and redirects to the user page on success.
  130. *
  131. * In order to never disclose password reset hashes via referrer headers or
  132. * web browser history, this function always issues a redirect when a valid
  133. * password reset hash is in the URL.
  134. */
  135. function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
  136. global $user;
  137. // Check for a reset hash in the session. Immediately remove it (to prevent it
  138. // from being reused, for example if this page is visited again via the
  139. // browser history) and store it for later use.
  140. if (isset($_SESSION['pass_reset_hash'])) {
  141. $session_reset_hash = $_SESSION['pass_reset_hash'];
  142. unset($_SESSION['pass_reset_hash']);
  143. }
  144. else {
  145. $session_reset_hash = NULL;
  146. }
  147. // When processing the one-time login link, we have to make sure that a user
  148. // isn't already logged in.
  149. if ($user->uid) {
  150. // The existing user is already logged in. Log them out and reload the
  151. // current page so the password reset process can continue.
  152. if ($user->uid == $uid) {
  153. // Preserve the current destination (if any) and ensure the redirect goes
  154. // back to the current page; any custom destination set in
  155. // hook_user_logout() and intended for regular logouts would not be
  156. // appropriate here.
  157. $destination = array();
  158. if (isset($_GET['destination'])) {
  159. $destination = drupal_get_destination();
  160. }
  161. user_logout_current_user();
  162. unset($_GET['destination']);
  163. drupal_goto(current_path(), array('query' => drupal_get_query_parameters() + $destination));
  164. }
  165. // A different user is already logged in on the computer.
  166. else {
  167. $reset_link_account = user_load($uid);
  168. $is_valid = FALSE;
  169. if (!empty($reset_link_account) && $timestamp >= $reset_link_account->login && $timestamp <= REQUEST_TIME) {
  170. if ($hashed_pass == user_pass_rehash($reset_link_account->pass, $timestamp, $reset_link_account->login, $reset_link_account->uid, $reset_link_account->mail)) {
  171. $is_valid = TRUE;
  172. }
  173. elseif ($session_reset_hash && $session_reset_hash == user_pass_rehash($reset_link_account->pass, $timestamp, $reset_link_account->login, $reset_link_account->uid, $reset_link_account->mail)) {
  174. $is_valid = TRUE;
  175. }
  176. }
  177. if ($is_valid) {
  178. 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.',
  179. array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))), 'warning');
  180. } else {
  181. // Invalid one-time link specifies an unknown user.
  182. drupal_set_message(t('The one-time login link you clicked is invalid.'), 'error');
  183. }
  184. drupal_goto();
  185. }
  186. }
  187. else {
  188. // Time out, in seconds, until login URL expires. Defaults to 24 hours =
  189. // 86400 seconds.
  190. $timeout = variable_get('user_password_reset_timeout', 86400);
  191. $current = REQUEST_TIME;
  192. // Some redundant checks for extra security ?
  193. $users = user_load_multiple(array($uid), array('status' => '1'));
  194. if ($timestamp <= $current && $account = reset($users)) {
  195. // No time out for first time login.
  196. if ($account->login && $current - $timestamp > $timeout) {
  197. 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');
  198. drupal_goto('user/password');
  199. }
  200. // Validate the reset hash from the URL or from the session.
  201. $is_valid = FALSE;
  202. if ($account->uid && $timestamp >= $account->login && $timestamp <= $current) {
  203. // If the reset hash in the URL is valid, put it in the session and
  204. // redirect to the same URL but with the hash replaced by an invalid
  205. // one ("confirm"). This prevents disclosing the hash via referrer
  206. // headers or web browser history.
  207. if ($hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid, $account->mail)) {
  208. if ($action === 'login') {
  209. // The 'login' action redirects directly to the user edit form.
  210. $is_valid = TRUE;
  211. }
  212. else {
  213. $_SESSION['pass_reset_hash'] = $hashed_pass;
  214. $args = arg();
  215. foreach ($args as &$arg) {
  216. if ($arg == $hashed_pass) {
  217. $arg = 'confirm';
  218. }
  219. }
  220. $path = implode('/', $args);
  221. drupal_goto($path, array('query' => drupal_get_query_parameters()));
  222. }
  223. }
  224. // If the reset hash from the session is valid, use that.
  225. elseif ($session_reset_hash && $session_reset_hash == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid, $account->mail)) {
  226. $is_valid = TRUE;
  227. }
  228. }
  229. if ($is_valid) {
  230. // First stage is a confirmation form, then login
  231. if ($action == 'login') {
  232. if (variable_get('user_failed_login_identifier_uid_only', FALSE)) {
  233. // Clear flood events based on the uid only if configured.
  234. $identifier = $account->uid;
  235. }
  236. else {
  237. // The default identifier is a combination of uid and IP address.
  238. $identifier = $account->uid . '-' . ip_address();
  239. }
  240. // Only clear the user specific flood events. We cannot clear the more
  241. // broad IP address flood events because that would open a
  242. // vulnerability where an attacker with a valid account could use that
  243. // to brute force other accounts.
  244. flood_clear_event('failed_login_attempt_user', $identifier);
  245. // Set the new user.
  246. $user = $account;
  247. // user_login_finalize() also updates the login timestamp of the
  248. // user, which invalidates further use of the one-time login link.
  249. user_login_finalize();
  250. // Clear any password reset flood events for this user.
  251. flood_clear_event('pass_reset_user', $account->uid);
  252. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
  253. 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.'));
  254. // Let the user's password be changed without the current password check.
  255. $token = drupal_random_key();
  256. $_SESSION['pass_reset_' . $user->uid] = $token;
  257. drupal_goto('user/' . $user->uid . '/edit', array('query' => array('pass-reset-token' => $token)));
  258. }
  259. else {
  260. $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))));
  261. $form['help'] = array('#markup' => '<p>' . t('This login can be used only once.') . '</p>');
  262. $form['actions'] = array('#type' => 'actions');
  263. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
  264. $form['#action'] = url("user/reset/$uid/$timestamp/$session_reset_hash/login");
  265. // Prevent the browser from storing this page so that the token will
  266. // not be visible in the form action if the back button is used to
  267. // revisit this page.
  268. drupal_add_http_header('Cache-Control', 'no-store');
  269. return $form;
  270. }
  271. }
  272. else {
  273. 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');
  274. drupal_goto('user/password');
  275. }
  276. }
  277. else {
  278. // Deny access, no more clues.
  279. // Everything will be in the watchdog's URL for the administrator to check.
  280. drupal_access_denied();
  281. drupal_exit();
  282. }
  283. }
  284. }
  285. /**
  286. * Menu callback; logs the current user out, and redirects to the home page.
  287. */
  288. function user_logout() {
  289. user_logout_current_user();
  290. drupal_goto();
  291. }
  292. /**
  293. * Logs the current user out.
  294. */
  295. function user_logout_current_user() {
  296. global $user;
  297. watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
  298. module_invoke_all('user_logout', $user);
  299. // Destroy the current session, and reset $user to the anonymous user.
  300. session_destroy();
  301. }
  302. /**
  303. * Process variables for user-profile.tpl.php.
  304. *
  305. * @param array $variables
  306. * An associative array containing:
  307. * - elements: An associative array containing the user information and any
  308. * fields attached to the user. Properties used:
  309. * - #account: The user account of the profile being viewed.
  310. *
  311. * @see user-profile.tpl.php
  312. */
  313. function template_preprocess_user_profile(&$variables) {
  314. $account = $variables['elements']['#account'];
  315. // Helpful $user_profile variable for templates.
  316. foreach (element_children($variables['elements']) as $key) {
  317. $variables['user_profile'][$key] = $variables['elements'][$key];
  318. }
  319. // Preprocess fields.
  320. field_attach_preprocess('user', $account, $variables['elements'], $variables);
  321. }
  322. /**
  323. * Process variables for user-profile-item.tpl.php.
  324. *
  325. * The $variables array contains the following arguments:
  326. * - $element
  327. *
  328. * @see user-profile-item.tpl.php
  329. */
  330. function template_preprocess_user_profile_item(&$variables) {
  331. $variables['title'] = $variables['element']['#title'];
  332. $variables['value'] = $variables['element']['#markup'];
  333. $variables['attributes'] = '';
  334. if (isset($variables['element']['#attributes'])) {
  335. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  336. }
  337. }
  338. /**
  339. * Process variables for user-profile-category.tpl.php.
  340. *
  341. * The $variables array contains the following arguments:
  342. * - $element
  343. *
  344. * @see user-profile-category.tpl.php
  345. */
  346. function template_preprocess_user_profile_category(&$variables) {
  347. $variables['title'] = check_plain($variables['element']['#title']);
  348. $variables['profile_items'] = $variables['element']['#children'];
  349. $variables['attributes'] = '';
  350. if (isset($variables['element']['#attributes'])) {
  351. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  352. }
  353. }
  354. /**
  355. * Form builder; edit a user account or one of their profile categories.
  356. *
  357. * @ingroup forms
  358. * @see user_account_form()
  359. * @see user_account_form_validate()
  360. * @see user_profile_form_validate()
  361. * @see user_profile_form_submit()
  362. * @see user_cancel_confirm_form_submit()
  363. */
  364. function user_profile_form($form, &$form_state, $account, $category = 'account') {
  365. global $user;
  366. // During initial form build, add the entity to the form state for use during
  367. // form building and processing. During a rebuild, use what is in the form
  368. // state.
  369. if (!isset($form_state['user'])) {
  370. $form_state['user'] = $account;
  371. }
  372. else {
  373. $account = $form_state['user'];
  374. }
  375. // @todo Legacy support. Modules are encouraged to access the entity using
  376. // $form_state. Remove in Drupal 8.
  377. $form['#user'] = $account;
  378. $form['#user_category'] = $category;
  379. if ($category == 'account') {
  380. user_account_form($form, $form_state);
  381. // Attach field widgets.
  382. $langcode = entity_language('user', $account);
  383. field_attach_form('user', $account, $form, $form_state, $langcode);
  384. }
  385. $form['actions'] = array('#type' => 'actions');
  386. $form['actions']['submit'] = array(
  387. '#type' => 'submit',
  388. '#value' => t('Save'),
  389. );
  390. if ($category == 'account') {
  391. $form['actions']['cancel'] = array(
  392. '#type' => 'submit',
  393. '#value' => t('Cancel account'),
  394. '#submit' => array('user_edit_cancel_submit'),
  395. '#access' => $account->uid > 1 && (($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users')),
  396. );
  397. }
  398. $form['#validate'][] = 'user_profile_form_validate';
  399. // Add the final user profile form submit handler.
  400. $form['#submit'][] = 'user_profile_form_submit';
  401. return $form;
  402. }
  403. /**
  404. * Form validation handler for user_profile_form().
  405. *
  406. * @see user_profile_form_submit()
  407. */
  408. function user_profile_form_validate($form, &$form_state) {
  409. entity_form_field_validate('user', $form, $form_state);
  410. }
  411. /**
  412. * Form submission handler for user_profile_form().
  413. *
  414. * @see user_profile_form_validate()
  415. */
  416. function user_profile_form_submit($form, &$form_state) {
  417. $account = $form_state['user'];
  418. $category = $form['#user_category'];
  419. // Remove unneeded values.
  420. form_state_values_clean($form_state);
  421. // Before updating the account entity, keep an unchanged copy for use with
  422. // user_save() later. This is necessary for modules implementing the user
  423. // hooks to be able to react on changes by comparing the values of $account
  424. // and $edit.
  425. $account_unchanged = clone $account;
  426. entity_form_submit_build_entity('user', $account, $form, $form_state);
  427. // Populate $edit with the properties of $account, which have been edited on
  428. // this form by taking over all values, which appear in the form values too.
  429. $edit = array_intersect_key((array) $account, $form_state['values']);
  430. user_save($account_unchanged, $edit, $category);
  431. $form_state['values']['uid'] = $account->uid;
  432. if ($category == 'account' && !empty($edit['pass'])) {
  433. // Remove the password reset tag since a new password was saved.
  434. unset($_SESSION['pass_reset_'. $account->uid]);
  435. }
  436. // Clear the page cache because pages can contain usernames and/or profile information:
  437. cache_clear_all();
  438. drupal_set_message(t('The changes have been saved.'));
  439. }
  440. /**
  441. * Submit function for the 'Cancel account' button on the user edit form.
  442. */
  443. function user_edit_cancel_submit($form, &$form_state) {
  444. $destination = array();
  445. if (isset($_GET['destination'])) {
  446. $destination = drupal_get_destination();
  447. unset($_GET['destination']);
  448. }
  449. // Note: We redirect from user/uid/edit to user/uid/cancel to make the tabs disappear.
  450. $form_state['redirect'] = array("user/" . $form['#user']->uid . "/cancel", array('query' => $destination));
  451. }
  452. /**
  453. * Form builder; confirm form for cancelling user account.
  454. *
  455. * @ingroup forms
  456. * @see user_edit_cancel_submit()
  457. */
  458. function user_cancel_confirm_form($form, &$form_state, $account) {
  459. global $user;
  460. $form['_account'] = array('#type' => 'value', '#value' => $account);
  461. // Display account cancellation method selection, if allowed.
  462. $admin_access = user_access('administer users');
  463. $can_select_method = $admin_access || user_access('select account cancellation method');
  464. $form['user_cancel_method'] = array(
  465. '#type' => 'item',
  466. '#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
  467. '#access' => $can_select_method,
  468. );
  469. $form['user_cancel_method'] += user_cancel_methods();
  470. // Allow user administrators to skip the account cancellation confirmation
  471. // mail (by default), as long as they do not attempt to cancel their own
  472. // account.
  473. $override_access = $admin_access && ($account->uid != $user->uid);
  474. $form['user_cancel_confirm'] = array(
  475. '#type' => 'checkbox',
  476. '#title' => t('Require e-mail confirmation to cancel account.'),
  477. '#default_value' => ($override_access ? FALSE : TRUE),
  478. '#access' => $override_access,
  479. '#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
  480. );
  481. // Also allow to send account canceled notification mail, if enabled.
  482. $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
  483. $form['user_cancel_notify'] = array(
  484. '#type' => 'checkbox',
  485. '#title' => t('Notify user when account is canceled.'),
  486. '#default_value' => ($override_access ? FALSE : $default_notify),
  487. '#access' => $override_access && $default_notify,
  488. '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
  489. );
  490. // Prepare confirmation form page title and description.
  491. if ($account->uid == $user->uid) {
  492. $question = t('Are you sure you want to cancel your account?');
  493. }
  494. else {
  495. $question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
  496. }
  497. $description = '';
  498. if ($can_select_method) {
  499. $description = t('Select the method to cancel the account above.');
  500. foreach (element_children($form['user_cancel_method']) as $element) {
  501. unset($form['user_cancel_method'][$element]['#description']);
  502. }
  503. }
  504. else {
  505. // The radio button #description is used as description for the confirmation
  506. // form.
  507. foreach (element_children($form['user_cancel_method']) as $element) {
  508. if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
  509. $description = $form['user_cancel_method'][$element]['#description'];
  510. }
  511. unset($form['user_cancel_method'][$element]['#description']);
  512. }
  513. }
  514. // Always provide entity id in the same form key as in the entity edit form.
  515. $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  516. return confirm_form($form,
  517. $question,
  518. 'user/' . $account->uid,
  519. $description . ' ' . t('This action cannot be undone.'),
  520. t('Cancel account'), t('Cancel'));
  521. }
  522. /**
  523. * Submit handler for the account cancellation confirm form.
  524. *
  525. * @see user_cancel_confirm_form()
  526. * @see user_multiple_cancel_confirm_submit()
  527. */
  528. function user_cancel_confirm_form_submit($form, &$form_state) {
  529. global $user;
  530. $account = $form_state['values']['_account'];
  531. // Cancel account immediately, if the current user has administrative
  532. // privileges, no confirmation mail shall be sent, and the user does not
  533. // attempt to cancel the own account.
  534. if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
  535. user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
  536. $form_state['redirect'] = 'admin/people';
  537. }
  538. else {
  539. // Store cancelling method and whether to notify the user in $account for
  540. // user_cancel_confirm().
  541. $edit = array(
  542. 'user_cancel_method' => $form_state['values']['user_cancel_method'],
  543. 'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
  544. );
  545. $account = user_save($account, $edit);
  546. _user_mail_notify('cancel_confirm', $account);
  547. drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
  548. watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
  549. $form_state['redirect'] = "user/$account->uid";
  550. }
  551. }
  552. /**
  553. * Helper function to return available account cancellation methods.
  554. *
  555. * See documentation of hook_user_cancel_methods_alter().
  556. *
  557. * @return
  558. * An array containing all account cancellation methods as form elements.
  559. *
  560. * @see hook_user_cancel_methods_alter()
  561. * @see user_admin_settings()
  562. * @see user_cancel_confirm_form()
  563. * @see user_multiple_cancel_confirm()
  564. */
  565. function user_cancel_methods() {
  566. $methods = array(
  567. 'user_cancel_block' => array(
  568. 'title' => t('Disable the account and keep its content.'),
  569. '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.'),
  570. ),
  571. 'user_cancel_block_unpublish' => array(
  572. 'title' => t('Disable the account and unpublish its content.'),
  573. '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.'),
  574. ),
  575. 'user_cancel_reassign' => array(
  576. 'title' => t('Delete the account and make its content belong to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  577. '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')))),
  578. ),
  579. 'user_cancel_delete' => array(
  580. 'title' => t('Delete the account and its content.'),
  581. 'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
  582. 'access' => user_access('administer users'),
  583. ),
  584. );
  585. // Allow modules to customize account cancellation methods.
  586. drupal_alter('user_cancel_methods', $methods);
  587. // Turn all methods into real form elements.
  588. $default_method = variable_get('user_cancel_method', 'user_cancel_block');
  589. foreach ($methods as $name => $method) {
  590. $form[$name] = array(
  591. '#type' => 'radio',
  592. '#title' => $method['title'],
  593. '#description' => (isset($method['description']) ? $method['description'] : NULL),
  594. '#return_value' => $name,
  595. '#default_value' => $default_method,
  596. '#parents' => array('user_cancel_method'),
  597. );
  598. }
  599. return $form;
  600. }
  601. /**
  602. * Menu callback; Cancel a user account via e-mail confirmation link.
  603. *
  604. * @see user_cancel_confirm_form()
  605. * @see user_cancel_url()
  606. */
  607. function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
  608. // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
  609. $timeout = 86400;
  610. $current = REQUEST_TIME;
  611. // Basic validation of arguments.
  612. if (isset($account->data['user_cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
  613. // Validate expiration and hashed password/login.
  614. if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid, $account->mail)) {
  615. $edit = array(
  616. 'user_cancel_notify' => isset($account->data['user_cancel_notify']) ? $account->data['user_cancel_notify'] : variable_get('user_mail_status_canceled_notify', FALSE),
  617. );
  618. user_cancel($edit, $account->uid, $account->data['user_cancel_method']);
  619. // Since user_cancel() is not invoked via Form API, batch processing needs
  620. // to be invoked manually and should redirect to the front page after
  621. // completion.
  622. batch_process('');
  623. }
  624. else {
  625. 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');
  626. drupal_goto("user/$account->uid/cancel");
  627. }
  628. }
  629. return MENU_ACCESS_DENIED;
  630. }
  631. /**
  632. * Page callback: Displays the user page.
  633. *
  634. * Displays user profile if user is logged in, or login form for anonymous
  635. * users.
  636. *
  637. * @return
  638. * A render array for either a user profile or a login form.
  639. *
  640. * @see user_view_page()
  641. * @see user_login()
  642. */
  643. function user_page() {
  644. global $user;
  645. if ($user->uid) {
  646. menu_set_active_item('user/' . $user->uid);
  647. return menu_execute_active_handler(NULL, FALSE);
  648. }
  649. else {
  650. return drupal_get_form('user_login');
  651. }
  652. }