reset.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /* For license terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $token = isset($_GET['token']) ? $_GET['token'] : '';
  5. if (!ctype_alnum($token)) {
  6. $token = '';
  7. }
  8. // Build the form
  9. $form = new FormValidator('reset', 'POST', api_get_self().'?token='.$token);
  10. $form->addElement('header', get_lang('ResetPassword'));
  11. $form->addHidden('token', $token);
  12. $form->addElement('password', 'pass1', get_lang('Password'));
  13. $form->addElement(
  14. 'password',
  15. 'pass2',
  16. get_lang('Confirmation'),
  17. ['id' => 'pass2', 'size' => 20, 'autocomplete' => 'off']
  18. );
  19. $form->addRule('pass1', get_lang('ThisFieldIsRequired'), 'required');
  20. $form->addRule('pass2', get_lang('ThisFieldIsRequired'), 'required');
  21. $form->addRule(['pass1', 'pass2'], get_lang('PassTwo'), 'compare');
  22. $form->addButtonSave(get_lang('Update'));
  23. $ttl = api_get_setting('user_reset_password_token_limit');
  24. if (empty($ttl)) {
  25. $ttl = 3600;
  26. }
  27. if ($form->validate()) {
  28. $values = $form->exportValues();
  29. $password = $values['pass1'];
  30. $token = $values['token'];
  31. /** @var \Chamilo\UserBundle\Entity\User $user */
  32. $user = UserManager::getManager()->findUserByConfirmationToken($token);
  33. if ($user) {
  34. if (!$user->isPasswordRequestNonExpired($ttl)) {
  35. Display::addFlash(Display::return_message(get_lang('LinkExpired')), 'warning');
  36. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php');
  37. exit;
  38. }
  39. $user->setPlainPassword($password);
  40. $userManager = UserManager::getManager();
  41. $userManager->updateUser($user, true);
  42. $user->setConfirmationToken(null);
  43. $user->setPasswordRequestedAt(null);
  44. Database::getManager()->persist($user);
  45. Database::getManager()->flush();
  46. Display::addFlash(Display::return_message(get_lang('Updated')));
  47. header('Location: '.api_get_path(WEB_PATH));
  48. exit;
  49. } else {
  50. Display::addFlash(
  51. Display::return_message(get_lang('LinkExpired'))
  52. );
  53. }
  54. }
  55. $tpl = new Template(null);
  56. $tpl->assign('content', $form->toHtml());
  57. $tpl->display_one_col_template();