PasswordController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\Setting;
  5. use App\Models\User;
  6. use App\Services\Cache;
  7. use App\Services\Captcha;
  8. use App\Services\Password;
  9. use App\Services\RateLimit;
  10. use App\Utils\Hash;
  11. use App\Utils\ResponseHelper;
  12. use Exception;
  13. use Psr\Http\Client\ClientExceptionInterface;
  14. use Psr\Http\Message\ResponseInterface;
  15. use RedisException;
  16. use Slim\Http\Response;
  17. use Slim\Http\ServerRequest;
  18. use voku\helper\AntiXSS;
  19. use function strlen;
  20. /*
  21. * Class Password
  22. *
  23. * @package App\Controllers
  24. * 密码重置
  25. */
  26. final class PasswordController extends BaseController
  27. {
  28. /**
  29. * @throws Exception
  30. */
  31. public function reset(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  32. {
  33. $captcha = [];
  34. if (Setting::obtain('enable_reset_password_captcha')) {
  35. $captcha = Captcha::generate();
  36. }
  37. return $response->write(
  38. $this->view()
  39. ->assign('captcha', $captcha)
  40. ->fetch('password/reset.tpl')
  41. );
  42. }
  43. /**
  44. * @throws RedisException
  45. */
  46. public function handleReset(ServerRequest $request, Response $response, array $args): ResponseInterface
  47. {
  48. if (Setting::obtain('enable_reset_password_captcha')) {
  49. $ret = Captcha::verify($request->getParams());
  50. if (! $ret) {
  51. return ResponseHelper::error($response, '系统无法接受你的验证结果,请刷新页面后重试');
  52. }
  53. }
  54. $antiXss = new AntiXSS();
  55. $email = strtolower($antiXss->xss_clean($request->getParam('email')));
  56. if ($email === '') {
  57. return ResponseHelper::error($response, '未填写邮箱');
  58. }
  59. if (! RateLimit::checkEmailIpLimit($request->getServerParam('REMOTE_ADDR')) ||
  60. ! RateLimit::checkEmailAddressLimit($email)
  61. ) {
  62. return ResponseHelper::error($response, '你的请求过于频繁,请稍后再试');
  63. }
  64. $user = User::where('email', $email)->first();
  65. $msg = '如果你的账户存在于我们的数据库中,那么重置密码的链接将会发送到你账户所对应的邮箱。';
  66. if ($user !== null) {
  67. try {
  68. Password::sendResetEmail($email);
  69. } catch (ClientExceptionInterface|RedisException $e) {
  70. $msg = '邮件发送失败,请联系网站管理员。';
  71. }
  72. }
  73. return ResponseHelper::successfully($response, $msg);
  74. }
  75. /**
  76. * @throws Exception
  77. */
  78. public function token(ServerRequest $request, Response $response, array $args)
  79. {
  80. $antiXss = new AntiXSS();
  81. $token = $antiXss->xss_clean($args['token']);
  82. $redis = Cache::initRedis();
  83. $email = $redis->get($token);
  84. if (! $email) {
  85. return $response->withStatus(302)->withHeader('Location', '/password/reset');
  86. }
  87. return $response->write(
  88. $this->view()->fetch('password/token.tpl')
  89. );
  90. }
  91. /**
  92. * @throws RedisException
  93. */
  94. public function handleToken(ServerRequest $request, Response $response, array $args): ResponseInterface
  95. {
  96. $antiXss = new AntiXSS();
  97. $token = $antiXss->xss_clean($args['token']);
  98. $password = $request->getParam('password');
  99. $repasswd = $request->getParam('repasswd');
  100. if ($password !== $repasswd) {
  101. return ResponseHelper::error($response, '两次输入不符合');
  102. }
  103. if (strlen($password) < 8) {
  104. return ResponseHelper::error($response, '密码过短');
  105. }
  106. $redis = Cache::initRedis();
  107. $email = $redis->get($token);
  108. if (! $email) {
  109. return ResponseHelper::error($response, '链接无效');
  110. }
  111. $user = User::where('email', $email)->first();
  112. if ($user === null) {
  113. return ResponseHelper::error($response, '链接无效');
  114. }
  115. // reset password
  116. $hashPassword = Hash::passwordHash($password);
  117. $user->pass = $hashPassword;
  118. if (! $user->save()) {
  119. return ResponseHelper::error($response, '重置失败,请重试');
  120. }
  121. if (Setting::obtain('enable_forced_replacement')) {
  122. $user->cleanLink();
  123. }
  124. $redis->del($token);
  125. return ResponseHelper::successfully($response, '重置成功');
  126. }
  127. }