lostPassword.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // name of the language file that needs to be included
  18. $language_file = 'registration';
  19. require_once '../inc/global.inc.php';
  20. require_once api_get_path(LIBRARY_PATH).'login.lib.php';
  21. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  22. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  23. require_once api_get_path(LIBRARY_PATH).'custompages.lib.php';
  24. // Custom pages
  25. // Had to move the form handling in here, because otherwise there would already be some display output.
  26. if (api_get_setting('use_custom_pages') == 'true') {
  27. if (isset ($_POST['user']) && isset ($_POST['email'])) {
  28. $user = $_POST['user'];
  29. $email = $_POST['email'];
  30. $condition = '';
  31. if (!empty($email)) {
  32. $condition = " AND LOWER(email) = '".Database::escape_string($email)."' ";
  33. }
  34. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  35. $query = " SELECT user_id AS uid, lastname AS lastName, firstname AS firstName,
  36. username AS loginName, password, email, status AS status,
  37. official_code, phone, picture_uri, creator_id
  38. FROM ".$tbl_user."
  39. WHERE ( username = '".Database::escape_string($user)."' $condition ) ";
  40. $result = Database::query($query);
  41. $num_rows = Database::num_rows($result);
  42. if ($result && $num_rows > 0) {
  43. if ($num_rows > 1) {
  44. $by_username = false; // more than one user
  45. while ($data = Database::fetch_array($result)) {
  46. $user[] = $data;
  47. }
  48. } else {
  49. $by_username = true; // single user (valid user + email)
  50. $user = Database::fetch_array($result);
  51. }
  52. if ($userPasswordCrypted != 'none') {
  53. Login::handle_encrypted_password($user, $by_username);
  54. } else {
  55. Login::send_password_to_user($user, $by_username);
  56. }
  57. } else {
  58. Display::display_error_message(get_lang('NoUserAccountWithThisEmailAddress'));
  59. }
  60. $msg = Login::reset_password($_GET["reset"], $_GET["id"], true);
  61. CustomPages::displayPage('lostpassword-feedback');
  62. }
  63. else {
  64. CustomPages::displayPage('lostpassword');
  65. }
  66. }
  67. $tool_name = get_lang('LostPassword');
  68. Display :: display_header($tool_name);
  69. $this_section = SECTION_CAMPUS;
  70. $tool_name = get_lang('LostPass');
  71. // Forbidden to retrieve the lost password
  72. if (api_get_setting('allow_lostpassword') == 'false') {
  73. api_not_allowed();
  74. }
  75. echo Display::tag('h1', $tool_name);
  76. if (isset ($_GET['reset']) && isset ($_GET['id'])) {
  77. //$msg = Login::reset_password($_GET["reset"], $_GET["id"], true);
  78. $msg1= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="fake_button_back" >'.get_lang('Back').'</a>';
  79. echo '<br /><br /><div class="actions" >'.$msg1.'</div>';
  80. } else {
  81. $form = new FormValidator('lost_password');
  82. $form->addElement('text', 'user', get_lang('LoginOrEmailAddress'), array('size'=>'40'));
  83. //$form->applyFilter('email','strtolower');
  84. $form->addElement('style_submit_button', 'submit', get_lang('Send'),'class="save"');
  85. // setting the rules
  86. $form->addRule('user', '<div class="required">'.get_lang('ThisFieldIsRequired').'</div>', 'required');
  87. if ($form->validate()) {
  88. $values = $form->exportValues();
  89. if(strpos($values['user'],'@')){
  90. $user = strtolower($values['user']);
  91. $email = TRUE;
  92. } else {
  93. $user = strtolower($values['user']);
  94. $email = FALSE;
  95. }
  96. $condition = '';
  97. if ($email) {
  98. $condition = "LOWER(email) = '".Database::escape_string($user)."' ";
  99. } else {
  100. $condition = "LOWER(username) = '".Database::escape_string($user)."'";
  101. }
  102. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  103. $query = "SELECT user_id AS uid, lastname AS lastName, firstname AS firstName, ".
  104. "username AS loginName, password, email, status AS status, ".
  105. "official_code, phone, picture_uri, creator_id ".
  106. "FROM ".$tbl_user." ".
  107. "WHERE ( $condition ) ";
  108. $result = Database::query($query);
  109. $num_rows = Database::num_rows($result);
  110. if ($result && $num_rows > 0) {
  111. $by_username = true;
  112. $users = Database::store_result($result);
  113. foreach( $users as $user ) {
  114. if ($userPasswordCrypted != 'none') {
  115. Login::handle_encrypted_password($user, $by_username);
  116. } else {
  117. Login::send_password_to_user($user, $by_username);
  118. }
  119. }
  120. } else {
  121. Display::display_error_message(get_lang('NoUserAccountWithThisEmailAddress'));
  122. }
  123. //$msg .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="fake_button_back" >'.get_lang('Back').'</a>';
  124. //echo '<br /><br /><div class="actions" >'.$msg.'</div>';
  125. } else {
  126. echo get_lang('EnterEmailUserAndWellSendYouPassword');
  127. echo '<br /><br />';
  128. $form->display();
  129. }
  130. }
  131. Display::display_footer();