lostPassword.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 __DIR__.'/../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. // Forbidden to retrieve the lost password
  22. if (api_get_setting('allow_lostpassword') == 'false') {
  23. api_not_allowed(true);
  24. }
  25. $reset = Request::get('reset');
  26. $userId = Request::get('id');
  27. $this_section = SECTION_CAMPUS;
  28. $tool_name = get_lang('LostPassword');
  29. if ($reset && $userId) {
  30. $messageText = Login::reset_password($reset, $userId, true);
  31. if (CustomPages::enabled() && CustomPages::exists(CustomPages::INDEX_UNLOGGED)) {
  32. CustomPages::display(
  33. CustomPages::INDEX_UNLOGGED,
  34. ['info' => $messageText]
  35. );
  36. exit;
  37. }
  38. Display::addFlash(
  39. Display::return_message($messageText, 'info', false)
  40. );
  41. header('Location: '.api_get_path(WEB_PATH));
  42. exit;
  43. }
  44. $form = new FormValidator('lost_password');
  45. $form->addHeader($tool_name);
  46. $form->addText('user',
  47. [
  48. get_lang('LoginOrEmailAddress'),
  49. get_lang('EnterEmailUserAndWellSendYouPassword'),
  50. ],
  51. true
  52. );
  53. $captcha = api_get_setting('allow_captcha');
  54. $allowCaptcha = $captcha === 'true';
  55. if ($allowCaptcha) {
  56. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  57. $options = array(
  58. 'width' => 220,
  59. 'height' => 90,
  60. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  61. 'sessionVar' => basename(__FILE__, '.php'),
  62. 'imageOptions' => array(
  63. 'font_size' => 20,
  64. 'font_path' => api_get_path(SYS_FONTS_PATH).'opensans/',
  65. 'font_file' => 'OpenSans-Regular.ttf',
  66. //'output' => 'gif'
  67. )
  68. );
  69. $captcha_question = $form->addElement(
  70. 'CAPTCHA_Image',
  71. 'captcha_question',
  72. '',
  73. $options
  74. );
  75. $form->addElement('static', null, null, get_lang('ClickOnTheImageForANewOne'));
  76. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'), array('size' => 40));
  77. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  78. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  79. }
  80. $form->addButtonSend(get_lang('Send'));
  81. if ($form->validate()) {
  82. $values = $form->exportValues();
  83. $user = Login::get_user_accounts_by_username($values['user']);
  84. if (!$user) {
  85. $messageText = get_lang('NoUserAccountWithThisEmailAddress');
  86. if (CustomPages::enabled() && CustomPages::exists(CustomPages::LOST_PASSWORD)) {
  87. CustomPages::display(
  88. CustomPages::LOST_PASSWORD,
  89. ['info' => $messageText]
  90. );
  91. exit;
  92. }
  93. Display::addFlash(
  94. Display::return_message($messageText, 'error', false)
  95. );
  96. header('Location: '.api_get_self());
  97. exit;
  98. }
  99. $passwordEncryption = api_get_configuration_value('password_encryption');
  100. if ($passwordEncryption === 'none') {
  101. $messageText = Login::send_password_to_user($user, true);
  102. if (CustomPages::enabled() && CustomPages::exists(CustomPages::INDEX_UNLOGGED)) {
  103. CustomPages::display(
  104. CustomPages::INDEX_UNLOGGED,
  105. ['info' => $messageText]
  106. );
  107. exit;
  108. }
  109. Display::addFlash(
  110. Display::return_message($messageText, 'info', false)
  111. );
  112. header('Location: '.api_get_path(WEB_PATH));
  113. exit;
  114. }
  115. if ($user['auth_source'] == 'extldap') {
  116. Display::addFlash(
  117. Display::return_message(get_lang('CouldNotResetPasswordBecauseLDAP'), 'info', false)
  118. );
  119. header('Location: '.api_get_path(WEB_PATH));
  120. exit;
  121. }
  122. $userResetPasswordSetting = api_get_setting('user_reset_password');
  123. if ($userResetPasswordSetting === 'true') {
  124. $userObj = Database::getManager()->getRepository('ChamiloUserBundle:User')->find($user['uid']);
  125. Login::sendResetEmail($userObj, true);
  126. if (CustomPages::enabled() && CustomPages::exists(CustomPages::INDEX_UNLOGGED)) {
  127. CustomPages::display(
  128. CustomPages::INDEX_UNLOGGED,
  129. ['info' => get_lang('CheckYourEmailAndFollowInstructions')]
  130. );
  131. exit;
  132. }
  133. header('Location: '.api_get_path(WEB_PATH));
  134. exit;
  135. }
  136. $messageText = Login::handle_encrypted_password($user, true);
  137. if (CustomPages::enabled() && CustomPages::exists(CustomPages::INDEX_UNLOGGED)) {
  138. CustomPages::display(
  139. CustomPages::INDEX_UNLOGGED,
  140. ['info' => $messageText]
  141. );
  142. exit;
  143. }
  144. Display::addFlash(
  145. Display::return_message($messageText, 'info', false)
  146. );
  147. header('Location: '.api_get_path(WEB_PATH));
  148. exit;
  149. }
  150. if (CustomPages::enabled() && CustomPages::exists(CustomPages::LOST_PASSWORD)) {
  151. CustomPages::display(
  152. CustomPages::LOST_PASSWORD,
  153. ['form' => $form->returnForm()]
  154. );
  155. exit;
  156. }
  157. $controller = new IndexManager($tool_name);
  158. $controller->set_login_form();
  159. $controller->tpl->assign('form', $form->returnForm());
  160. $template = $controller->tpl->get_template('auth/lost_password.tpl');
  161. $controller->tpl->display($template);