password-reset.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * copyright 2009 Lucas Baudin <xapantu@gmail.com>
  4. * 2012 - 2014 Stephen Just <stephenjust@gmail.com>
  5. * 2013 Glenn De Jonghe
  6. * 2014 - 2016 Daniel Butum <danibutum at gmail dot com>
  7. * This file is part of stk-addons.
  8. *
  9. * stk-addons is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * stk-addons is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with stk-addons. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. require_once(__DIR__ . DIRECTORY_SEPARATOR . "config.php");
  23. Util::validateCaptchaKeysSet();
  24. $tpl = StkTemplate::get('password-reset.tpl')
  25. ->assignTitle(_h('Reset Password'))
  26. ->addScriptIncludeWeb('https://www.google.com/recaptcha/api.js');
  27. // Fill out various templates
  28. $pw_res = [
  29. 'reset_form' => [
  30. 'display' => true,
  31. 'captcha_site_key' => CAPTCHA_SITE_KEY,
  32. ],
  33. 'pass_form' => [
  34. 'display' => false,
  35. 'user_id' => "",
  36. 'verification_code' => ""
  37. ]
  38. ];
  39. // define possibly undefined variables
  40. $_GET['action'] = isset($_GET['action']) ? $_GET['action'] : null;
  41. switch ($_GET['action'])
  42. {
  43. case 'reset': // user sent reset activation link
  44. $pw_res['reset_form']['display'] = false;
  45. // Look up username and try to reset
  46. try
  47. {
  48. if (Validate::ensureNotEmpty($_POST, ['g-recaptcha-response']))
  49. throw new UserException(_h('You did not complete the reCAPTCHA field'));
  50. // Check CAPTCHA
  51. $captcha = new \ReCaptcha\ReCaptcha(CAPTCHA_SECRET);
  52. $response = $captcha->verify($_POST['g-recaptcha-response'], Util::getClientIp());
  53. if (!$response->isSuccess())
  54. {
  55. // codes reference https://developers.google.com/recaptcha/docs/verify#error-code-reference
  56. throw new UserException(_h("The reCAPTCHA wasn't entered correctly. Go back and try it again."));
  57. }
  58. User::recover($_POST['user'], $_POST['mail']);
  59. $tpl->assign("success", _h("Password reset link sent. Please reset your password using the link emailed to you."));
  60. }
  61. catch(UserException $e)
  62. {
  63. $tpl->assign("errors", $e->getMessage());
  64. }
  65. break;
  66. case 'valid': // user comes from activation link
  67. try
  68. {
  69. $user_id = isset($_GET['user']) ? $_GET['user'] : 0;
  70. $verification_code = isset($_GET['num']) ? $_GET['num'] : "";
  71. Verification::verify($user_id, $verification_code);
  72. $pw_res['reset_form']['display'] = false;
  73. $pw_res['pass_form'] = [
  74. 'display' => true,
  75. 'user_id' => $user_id,
  76. 'verification_code' => $verification_code
  77. ];
  78. }
  79. catch(UserException $e)
  80. {
  81. $tpl->assign("errors", $e->getMessage() . ". " . _h('Could not reset your password. The link you followed is not valid.'));
  82. }
  83. break;
  84. case 'change': // change password clicked in the 'valid' page
  85. $user_id = isset($_POST['user']) ? $_POST['user'] : 0;
  86. $verification_code = isset($_POST['verify']) ? $_POST['verify'] : "";
  87. $pass1 = isset($_POST['pass1']) ? $_POST['pass1'] : "";
  88. $pass2 = isset($_POST['pass2']) ? $_POST['pass2'] : "";
  89. try
  90. {
  91. // validate
  92. Verification::verify($user_id, $verification_code);
  93. User::validateNewPassword($pass1, $pass2);
  94. // change password and clean up
  95. User::changePassword($user_id, $pass1);
  96. Verification::delete($user_id);
  97. $pw_res['reset_form']['display'] = false;
  98. $tpl->assign("success", _h('Changed password was successful.') . '<a href="login.php"> ' . _h('Click here to login') . '</a>');
  99. }
  100. catch(UserException $e)
  101. {
  102. $tpl->assign("errors", $e->getMessage());
  103. $pw_res['reset_form']['display'] = false;
  104. $pw_res['pass_form'] = [
  105. 'display' => true,
  106. 'user_id' => $user_id,
  107. 'verification_code' => $verification_code
  108. ];
  109. }
  110. break;
  111. default:
  112. break;
  113. }
  114. $tpl->assign('pass_reset', $pw_res);
  115. echo $tpl;