lostPassword.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. require_once '../inc/global.inc.php';
  18. // Custom pages
  19. // Had to move the form handling in here, because otherwise there would
  20. // already be some display output.
  21. global $_configuration;
  22. if (CustomPages::enabled()) {
  23. // Reset Password when user goes to the link
  24. if (isset($_GET['reset']) && $_GET['reset'] &&
  25. isset($_GET['id']) && $_GET['id']
  26. ) {
  27. $mesg = Login::reset_password($_GET["reset"], $_GET["id"], true);
  28. CustomPages::display(CustomPages::INDEX_UNLOGGED, array('info' => $mesg));
  29. }
  30. // Check email/username and do the right thing
  31. if (isset($_POST['user'])) {
  32. $usersRelatedToUsername = Login::get_user_accounts_by_username($_POST['user']);
  33. if ($usersRelatedToUsername) {
  34. $by_username = true;
  35. foreach ($usersRelatedToUsername as $user) {
  36. if ($_configuration['password_encryption'] != 'none') {
  37. Login::handle_encrypted_password($user, $by_username);
  38. } else {
  39. Login::send_password_to_user($user, $by_username);
  40. }
  41. }
  42. } else {
  43. CustomPages::display(
  44. CustomPages::LOST_PASSWORD,
  45. array('error' => get_lang('NoUserAccountWithThisEmailAddress'))
  46. );
  47. }
  48. } else {
  49. CustomPages::display(CustomPages::LOST_PASSWORD);
  50. }
  51. CustomPages::display(
  52. CustomPages::INDEX_UNLOGGED,
  53. array('info' => get_lang('YourPasswordHasBeenEmailed'))
  54. );
  55. }
  56. $tool_name = get_lang('LostPassword');
  57. $this_section = SECTION_CAMPUS;
  58. $tool_name = get_lang('LostPass');
  59. // Forbidden to retrieve the lost password
  60. if (api_get_setting('allow_lostpassword') == 'false') {
  61. api_not_allowed(true);
  62. }
  63. $formToString = '';
  64. if (isset($_GET['reset']) && isset($_GET['id'])) {
  65. $message = Display::return_message(
  66. Login::reset_password($_GET["reset"], $_GET["id"], true),
  67. 'normal',
  68. false
  69. );
  70. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php" class="btn btn-back" >'.get_lang('Back').'</a>';
  71. Display::addFlash($message);
  72. } else {
  73. $form = new FormValidator('lost_password');
  74. $form->addElement('header', $tool_name);
  75. $form->addElement(
  76. 'text',
  77. 'user',
  78. array(
  79. get_lang('LoginOrEmailAddress'),
  80. get_lang('EnterEmailUserAndWellSendYouPassword'),
  81. ),
  82. array('size' => '40')
  83. );
  84. $form->addButtonSend(get_lang('Send'));
  85. // Setting the rules
  86. $form->addRule('user', get_lang('ThisFieldIsRequired'), 'required');
  87. if ($form->validate()) {
  88. $values = $form->exportValues();
  89. $usersRelatedToUsername = Login::get_user_accounts_by_username(
  90. $values['user']
  91. );
  92. if ($usersRelatedToUsername) {
  93. $by_username = true;
  94. foreach ($usersRelatedToUsername as $user) {
  95. if ($_configuration['password_encryption'] != 'none') {
  96. $setting = api_get_setting('user_reset_password');
  97. if ($setting === 'true') {
  98. $userObj = Database::getManager()->getRepository('ChamiloUserBundle:User')->find($user['uid']);
  99. Login::sendResetEmail($userObj);
  100. } else {
  101. $message = Login::handle_encrypted_password($user, $by_username);
  102. Display::addFlash($message);
  103. }
  104. } else {
  105. $message = Login::send_password_to_user($user, $by_username);
  106. Display::addFlash($message);
  107. }
  108. }
  109. } else {
  110. Display::addFlash(
  111. Display::return_message(
  112. get_lang('NoUserAccountWithThisEmailAddress'),
  113. 'warning'
  114. )
  115. );
  116. }
  117. } else {
  118. $formToString = $form->returnForm();
  119. }
  120. }
  121. $controller = new IndexManager($tool_name);
  122. $controller->set_login_form();
  123. $tpl = $controller->tpl;
  124. $tpl->assign('form', $formToString);
  125. $template = $tpl->get_template('auth/lost_password.tpl');
  126. $tpl->display($template);