You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/PasswordController.php

120 lines
4.9 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\User;
use App\Models\Configurations;
use App\Services\CaptchaConfig;
use App\Services\RedisClient;
use App\Models\PasswordReset;
use App\Services\Password;
use App\Utils\Hash;
use App\Utils\Cookie;
/***
* Class Password
* @package App\Controllers
* 密码重置
*/
class PasswordController extends BaseController
{
private $language = 'false';
public function __construct()
{
$this->applyDomainTheme($this->getDomainConfigurations());
$redis = new RedisClient();
$this->language = $redis->get('language', 60, function () {
$value = Configurations::where('name', 'language')->value('value');
return $value === null ? 'false' : $value;
});
}
public function reset()
{
return $this->view()
->assign('captch_respass', CaptchaConfig::get('captch_respass'))
->assign('language', $this->language)
->display('password/reset.tpl');
}
public function handleReset($request, $response, $args)
{
Cookie::startSession();
$captch = $request->getParam('captch');
$email = $request->getParam('email');
// check limit
if (CaptchaConfig::get('captch_respass') == 'true') {
$expectedCaptcha = isset($_SESSION['captch']) && is_scalar($_SESSION['captch'])
? strtolower(trim((string) $_SESSION['captch'])) : '';
$providedCaptcha = is_scalar($captch) ? strtolower(trim((string) $captch)) : '';
if ($expectedCaptcha === '' || $providedCaptcha === '' || !hash_equals($expectedCaptcha, $providedCaptcha)) {
$res['ret'] = 0;
$res['msg'] = $this->language === 'true' ? 'The verification code is incorrect.' : '验证码错误。';
return $response->getBody()->write(json_encode($res));
}
unset($_SESSION['captch']);
}
// send email
$user = User::where('email', $email)->first();
if ($user == null) {
$rs['ret'] = 0;
$rs['msg'] = $this->language === 'true' ? 'This email address does not exist.' : '该电子邮件不存在。';
return $response->getBody()->write(json_encode($rs));
}
if (!Password::sendResetEmail($email, $this->language === 'true')) {
$rs['ret'] = 0;
$rs['msg'] = $this->language === 'true' ? 'The email could not be sent. Please try again later.' : '邮件发送失败,请稍后重试。';
return $response->getBody()->write(json_encode($rs));
}
$rs['ret'] = 1;
$rs['msg'] = $this->language === 'true' ? 'The email has been sent.' : '邮件已发送。';
return $response->getBody()->write(json_encode($rs));
}
public function token($request, $response, $args)
{
$token = $args['token'];
return $this->view()
->assign('token', $token)
->assign('language', $this->language)
->display('password/token.tpl');
}
public function handleToken($request, $response, $args)
{
$password = $request->getParam('password');
$error = Hash::newPasswordError($password);
if ($error !== null || $password !== $request->getParam('repasswd')) {
return $this->echoJson($response, ['ret' => 0, 'msg' => $error ?? '两次密码不一致']);
}
$tokenStr = isset($args['token']) && is_string($args['token']) ? $args['token'] : '';
if (preg_match('/^[a-f0-9]{64}$/', $tokenStr) !== 1) {
return $this->echoJson($response, ['ret' => 0, 'msg' => '链接无效或已过期']);
}
$connection = (new User())->getConnection();
try {
$connection->transaction(function () use ($tokenStr, $password) {
$token = PasswordReset::where('token', $tokenStr)->lockForUpdate()->first();
if ($token === null || (int) $token->expire_time < time()) {
throw new \InvalidArgumentException('链接无效或已过期');
}
$user = User::where('email', $token->email)->lockForUpdate()->first();
if ($user === null) {
throw new \InvalidArgumentException('链接无效或已过期');
}
$user->updatePassword($password);
PasswordReset::where('email', $token->email)->delete();
});
} catch (\InvalidArgumentException $exception) {
return $this->echoJson($response, ['ret' => 0, 'msg' => $exception->getMessage()]);
} catch (\Throwable $exception) {
error_log('Password recovery failed: ' . get_class($exception));
return $this->echoJson($response, ['ret' => 0, 'msg' => '重置失败,请稍后重试']);
}
return $this->echoJson($response, ['ret' => 1, 'msg' => '密码已重置,请重新登录']);
}
}