user_registrationpassword.pages.inc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * User page callback file for the user_registrationpassword module.
  5. */
  6. /**
  7. * Menu callback; process one time login link and redirects to the user page on success.
  8. */
  9. function user_registrationpassword_confirm_account($form, &$form_state, $uid, $timestamp, $hashed_pass) {
  10. global $user;
  11. // When processing the one-time login link, we have to make sure that a user
  12. // isn't already logged in.
  13. if ($user->uid) {
  14. // The existing user is already logged in.
  15. if ($user->uid == $uid) {
  16. drupal_set_message(t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array('%user' => $user->name, '!user_edit' => url("user/$user->uid/edit"))));
  17. }
  18. // A different user is already logged in on the computer.
  19. else {
  20. $reset_link_account = user_load($uid);
  21. if (!empty($reset_link_account)) {
  22. 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.',
  23. array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))));
  24. }
  25. else {
  26. // Invalid one-time link specifies an unknown user.
  27. drupal_set_message(t('The one-time login link you clicked is invalid.'));
  28. }
  29. }
  30. drupal_goto();
  31. }
  32. else {
  33. // Time out, in seconds, until login URL expires. 24 hours = 86400 seconds.
  34. $timeout = 86400;
  35. $current = REQUEST_TIME;
  36. // Some redundant checks for extra security ?
  37. $users = user_load_multiple(array($uid), array('status' => '0'));
  38. if ($timestamp <= $current && $account = reset($users)) {
  39. // No time out for first time login.
  40. if ($account->login && $current - $timestamp > $timeout) {
  41. 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.'));
  42. drupal_goto('user/password');
  43. }
  44. elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
  45. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
  46. // Activate the user.
  47. $account = user_save($account, array('status' => 1));
  48. // Set the new user.
  49. $user = $account;
  50. // user_login_finalize() also updates the login timestamp of the
  51. // user, which invalidates further use of the one-time login link.
  52. user_login_finalize();
  53. drupal_set_message(t('You have just used your one-time login link. Your account is now active.'));
  54. drupal_goto('user');
  55. }
  56. else {
  57. 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.'));
  58. drupal_goto('user/password');
  59. }
  60. }
  61. else {
  62. // Deny access, no more clues.
  63. // Everything will be in the watchdog's URL for the administrator to check.
  64. drupal_access_denied();
  65. }
  66. }
  67. }