|
|
<?php
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
use App\Models\User;
|
|
|
use App\Services\CaptchaConfig;
|
|
|
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
|
|
|
{
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->applyDomainTheme($this->getDomainConfigurations());
|
|
|
$this->initializePublicLocale();
|
|
|
}
|
|
|
|
|
|
public function reset()
|
|
|
{
|
|
|
return $this->publicView()
|
|
|
->assign('captch_respass', CaptchaConfig::get('captch_respass'))
|
|
|
->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->publicView()
|
|
|
->assign('token', $token)
|
|
|
->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')) {
|
|
|
$message = $error !== null
|
|
|
? $this->publicText(
|
|
|
$error,
|
|
|
'The password must be 12–72 bytes and include uppercase and lowercase letters, a number, and a symbol.'
|
|
|
)
|
|
|
: $this->publicText('两次密码不一致', 'The passwords do not match.');
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => $message]);
|
|
|
}
|
|
|
$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' => $this->publicText('链接无效或已过期', 'This link is invalid or has expired.'),
|
|
|
]);
|
|
|
}
|
|
|
$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($this->publicText(
|
|
|
'链接无效或已过期',
|
|
|
'This link is invalid or has expired.'
|
|
|
));
|
|
|
}
|
|
|
$user = User::where('email', $token->email)->lockForUpdate()->first();
|
|
|
if ($user === null) {
|
|
|
throw new \InvalidArgumentException($this->publicText(
|
|
|
'链接无效或已过期',
|
|
|
'This link is invalid or has expired.'
|
|
|
));
|
|
|
}
|
|
|
$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' => $this->publicText('重置失败,请稍后重试', 'Password reset failed. Please try again later.'),
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
return $this->echoJson($response, [
|
|
|
'ret' => 1,
|
|
|
'msg' => $this->publicText('密码已重置,请重新登录', 'Your password has been reset. Please log in again.'),
|
|
|
]);
|
|
|
}
|
|
|
}
|