lostPassword.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Custom pages
  24. // Had to move the form handling in here, because otherwise there would
  25. // already be some display output.
  26. global $_configuration;
  27. if (CustomPages::enabled()) {
  28. //Reset Password when user goes to the link
  29. if ($_GET['reset'] && $_GET['id']){
  30. $mesg = Login::reset_password($_GET["reset"], $_GET["id"], true);
  31. CustomPages::display(CustomPages::INDEX_UNLOGGED, array('info' => $mesg));
  32. }
  33. //Check email/username and do the right thing
  34. if (isset ($_POST['user']) && isset ($_POST['email'])) {
  35. $user = $_POST['user'];
  36. $email = $_POST['email'];
  37. $condition = '';
  38. if (!empty($email)) {
  39. $condition = " AND LOWER(email) = '".Database::escape_string($email)."' ";
  40. }
  41. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  42. $query = " SELECT user_id AS uid, lastname AS lastName, firstname AS firstName,
  43. username AS loginName, password, email, status AS status,
  44. official_code, phone, picture_uri, creator_id
  45. FROM ".$tbl_user."
  46. WHERE ( username = '".Database::escape_string($user)."' $condition ) ";
  47. $result = Database::query($query);
  48. $num_rows = Database::num_rows($result);
  49. if ($result && $num_rows > 0) {
  50. if ($num_rows > 1) {
  51. $by_username = false; // more than one user
  52. while ($data = Database::fetch_array($result)) {
  53. $user[] = $data;
  54. }
  55. } else {
  56. $by_username = true; // single user (valid user + email)
  57. $user = Database::fetch_array($result);
  58. }
  59. if ($_configuration['password_encryption'] != 'none') {
  60. //Send email with secret link to user
  61. Login::handle_encrypted_password($user, $by_username);
  62. } else {
  63. Login::send_password_to_user($user, $by_username);
  64. }
  65. } else {
  66. CustomPages::display(CustomPages::LOST_PASSWORD, array('error' => get_lang('NoUserAccountWithThisEmailAddress')));
  67. }
  68. } else {
  69. CustomPages::display(CustomPages::LOGGED_OUT);
  70. }
  71. CustomPages::display(CustomPages::INDEX_UNLOGGED, array('info' => get_lang('YourPasswordHasBeenEmailed')));
  72. }
  73. $tool_name = get_lang('LostPassword');
  74. Display :: display_header($tool_name);
  75. $this_section = SECTION_CAMPUS;
  76. $tool_name = get_lang('LostPass');
  77. // Forbidden to retrieve the lost password
  78. if (api_get_setting('allow_lostpassword') == 'false') {
  79. api_not_allowed();
  80. }
  81. if (isset($_GET['reset']) && isset($_GET['id'])) {
  82. $message = Display::return_message(Login::reset_password($_GET["reset"], $_GET["id"], true), 'normal', false);
  83. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="btn" >'.get_lang('Back').'</a>';
  84. echo $message;
  85. } else {
  86. $form = new FormValidator('lost_password');
  87. $form->addElement('header', $tool_name);
  88. $form->addElement('text', 'user', array(get_lang('LoginOrEmailAddress'), get_lang('EnterEmailUserAndWellSendYouPassword')), array('size'=>'40'));
  89. $form->addElement('style_submit_button', 'submit', get_lang('Send'),'class="btn"');
  90. // setting the rules
  91. $form->addRule('user', get_lang('ThisFieldIsRequired'), 'required');
  92. if ($form->validate()) {
  93. $values = $form->exportValues();
  94. $users_related_to_username = Login::get_user_accounts_by_username($values['user']);
  95. if ($users_related_to_username) {
  96. $by_username = true;
  97. foreach ($users_related_to_username as $user) {
  98. if ($_configuration['password_encryption'] != 'none') {
  99. Login::handle_encrypted_password($user, $by_username);
  100. } else {
  101. Login::send_password_to_user($user, $by_username);
  102. }
  103. }
  104. } else {
  105. Display::display_warning_message(get_lang('NoUserAccountWithThisEmailAddress'));
  106. }
  107. } else {
  108. $form->display();
  109. }
  110. }
  111. Display::display_footer();