reset.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. array('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(array('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. $em = Database::getManager();
  29. $values = $form->exportValues();
  30. $password = $values['pass1'];
  31. $token = $values['token'];
  32. /** @var \Chamilo\UserBundle\Entity\User $user */
  33. $user = UserManager::getManager()->findUserByConfirmationToken($token);
  34. if ($user) {
  35. if (!$user->isPasswordRequestNonExpired($ttl)) {
  36. Display::addFlash(Display::return_message(get_lang('LinkExpired')), 'warning');
  37. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/lostPassword.php');
  38. exit;
  39. }
  40. $user->setPlainPassword($password);
  41. $userManager = UserManager::getManager();
  42. $userManager->updateUser($user, true);
  43. $user->setConfirmationToken(null);
  44. $user->setPasswordRequestedAt(null);
  45. Database::getManager()->persist($user);
  46. Database::getManager()->flush();
  47. Display::addFlash(Display::return_message(get_lang('Updated')));
  48. header('Location: '.api_get_path(WEB_PATH));
  49. exit;
  50. } else {
  51. Display::addFlash(
  52. Display::return_message(get_lang('LinkExpired'))
  53. );
  54. }
  55. }
  56. $tpl = new Template(null);
  57. $tpl->assign('form', $form->toHtml());
  58. $content = $tpl->get_template('auth/set_temp_password.tpl');
  59. $tpl->assign('content', $tpl->fetch($content));
  60. $tpl->display_one_col_template();