lostPassword.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. require_once '../inc/global.inc.php';
  21. // Custom pages
  22. // Had to move the form handling in here, because otherwise there would
  23. // already be some display output.
  24. global $_configuration;
  25. if (CustomPages::enabled()) {
  26. // Reset Password when user goes to the link
  27. if (isset($_GET['reset']) && $_GET['reset'] &&
  28. isset($_GET['id']) && $_GET['id']
  29. ) {
  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'])) {
  35. $usersRelatedToUsername = Login::get_user_accounts_by_username($_POST['user']);
  36. if ($usersRelatedToUsername) {
  37. $by_username = true;
  38. foreach ($usersRelatedToUsername as $user) {
  39. if ($_configuration['password_encryption'] != 'none') {
  40. Login::handle_encrypted_password($user, $by_username);
  41. } else {
  42. Login::send_password_to_user($user, $by_username);
  43. }
  44. }
  45. } else {
  46. CustomPages::display(
  47. CustomPages::LOST_PASSWORD,
  48. array('error' => get_lang('NoUserAccountWithThisEmailAddress'))
  49. );
  50. }
  51. } else {
  52. CustomPages::display(CustomPages::LOST_PASSWORD);
  53. }
  54. CustomPages::display(
  55. CustomPages::INDEX_UNLOGGED,
  56. array('info' => get_lang('YourPasswordHasBeenEmailed'))
  57. );
  58. }
  59. $tool_name = get_lang('LostPassword');
  60. Display :: display_header($tool_name);
  61. $this_section = SECTION_CAMPUS;
  62. $tool_name = get_lang('LostPass');
  63. // Forbidden to retrieve the lost password
  64. if (api_get_setting('allow_lostpassword') == 'false') {
  65. api_not_allowed();
  66. }
  67. if (isset($_GET['reset']) && isset($_GET['id'])) {
  68. $message = Display::return_message(Login::reset_password($_GET["reset"], $_GET["id"], true), 'normal', false);
  69. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="btn btn-back" >'.get_lang('Back').'</a>';
  70. echo $message;
  71. } else {
  72. $form = new FormValidator('lost_password');
  73. $form->addElement('header', $tool_name);
  74. $form->addElement('text', 'user', array(get_lang('LoginOrEmailAddress'), get_lang('EnterEmailUserAndWellSendYouPassword')), array('size'=>'40'));
  75. $form->addButtonSend(get_lang('Send'));
  76. // Setting the rules
  77. $form->addRule('user', get_lang('ThisFieldIsRequired'), 'required');
  78. if ($form->validate()) {
  79. $values = $form->exportValues();
  80. $users_related_to_username = Login::get_user_accounts_by_username(
  81. $values['user']
  82. );
  83. if ($users_related_to_username) {
  84. $by_username = true;
  85. foreach ($users_related_to_username as $user) {
  86. if ($_configuration['password_encryption'] != 'none') {
  87. Login::handle_encrypted_password($user, $by_username);
  88. } else {
  89. Login::send_password_to_user($user, $by_username);
  90. }
  91. }
  92. } else {
  93. Display::display_warning_message(get_lang('NoUserAccountWithThisEmailAddress'));
  94. }
  95. } else {
  96. $form->display();
  97. }
  98. }
  99. Display::display_footer();