lostPassword.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * SCRIPT PURPOSE :
  5. *
  6. * This script allows users to retrieve the password of their profile(s)
  7. * on the basis of their e-mail address. The password is send via email
  8. * to the user.
  9. *
  10. * Special case : If the password are encrypted in the database, we have
  11. * to generate a new one.
  12. *
  13. * @todo refactor, move relevant functions to code libraries
  14. *
  15. * @package chamilo.auth
  16. */
  17. /**
  18. * Code
  19. */
  20. // name of the language file that needs to be included
  21. $language_file = array('registration', 'index');
  22. require_once '../inc/global.inc.php';
  23. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  24. // Custom pages
  25. // Had to move the form handling in here, because otherwise there would
  26. // already be some display output.
  27. global $_configuration;
  28. if (CustomPages::enabled()) {
  29. //Reset Password when user goes to the link
  30. if ($_GET['reset'] && $_GET['id']){
  31. $mesg = Login::reset_password($_GET["reset"], $_GET["id"], true);
  32. CustomPages::display(CustomPages::INDEX_UNLOGGED, array('info' => $mesg));
  33. }
  34. //Check email/username and do the right thing
  35. if (isset ($_POST['user']) && isset ($_POST['email'])) {
  36. $user = $_POST['user'];
  37. $email = $_POST['email'];
  38. $condition = '';
  39. if (!empty($email)) {
  40. $condition = " AND LOWER(email) = '".Database::escape_string($email)."' ";
  41. }
  42. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  43. $query = " SELECT user_id AS uid, lastname AS lastName, firstname AS firstName,
  44. username AS loginName, password, email, status AS status,
  45. official_code, phone, picture_uri, creator_id
  46. FROM ".$tbl_user."
  47. WHERE ( username = '".Database::escape_string($user)."' $condition ) ";
  48. $result = Database::query($query);
  49. $num_rows = Database::num_rows($result);
  50. if ($result && $num_rows > 0) {
  51. if ($num_rows > 1) {
  52. $by_username = false; // more than one user
  53. while ($data = Database::fetch_array($result)) {
  54. $user[] = $data;
  55. }
  56. } else {
  57. $by_username = true; // single user (valid user + email)
  58. $user = Database::fetch_array($result);
  59. }
  60. if ($_configuration['password_encryption'] != 'none') {
  61. //Send email with secret link to user
  62. Login::handle_encrypted_password($user, $by_username);
  63. } else {
  64. Login::send_password_to_user($user, $by_username);
  65. }
  66. } else {
  67. CustomPages::display(CustomPages::LOST_PASSWORD, array('error' => get_lang('NoUserAccountWithThisEmailAddress')));
  68. }
  69. } else {
  70. CustomPages::display(CustomPages::LOGGED_OUT);
  71. }
  72. CustomPages::display(CustomPages::INDEX_UNLOGGED, array('info' => get_lang('YourPasswordHasBeenEmailed')));
  73. }
  74. $tool_name = get_lang('LostPassword');
  75. Display :: display_header($tool_name);
  76. $this_section = SECTION_CAMPUS;
  77. $tool_name = get_lang('LostPass');
  78. // Forbidden to retrieve the lost password
  79. if (api_get_setting('allow_lostpassword') == 'false') {
  80. api_not_allowed();
  81. }
  82. if (isset($_GET['reset']) && isset($_GET['id'])) {
  83. $message = Display::return_message(Login::reset_password($_GET["reset"], $_GET["id"], true), 'normal', false);
  84. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="btn" >'.get_lang('Back').'</a>';
  85. echo $message;
  86. } else {
  87. $form = new FormValidator('lost_password');
  88. $form->addElement('header', $tool_name);
  89. $form->addElement('text', 'user', array(get_lang('LoginOrEmailAddress'), get_lang('EnterEmailUserAndWellSendYouPassword')), array('size'=>'40'));
  90. $form->addElement('style_submit_button', 'submit', get_lang('Send'),'class="btn"');
  91. // setting the rules
  92. $form->addRule('user', get_lang('ThisFieldIsRequired'), 'required');
  93. if ($form->validate()) {
  94. $values = $form->exportValues();
  95. $users_related_to_username = Login::get_user_accounts_by_username($values['user']);
  96. if ($users_related_to_username) {
  97. $by_username = true;
  98. foreach ($users_related_to_username as $user) {
  99. if ($_configuration['password_encryption'] != 'none') {
  100. Login::handle_encrypted_password($user, $by_username);
  101. } else {
  102. Login::send_password_to_user($user, $by_username);
  103. }
  104. }
  105. } else {
  106. Display::display_warning_message(get_lang('NoUserAccountWithThisEmailAddress'));
  107. }
  108. } else {
  109. $form->display();
  110. }
  111. }
  112. Display::display_footer();