62 行
2.6 KiB
PHP
62 行
2.6 KiB
PHP
<?php
|
|
require __DIR__ . '/includes/init.php';
|
|
$pageTitle = '重置密码';
|
|
$token = trim($_GET['token'] ?? '');
|
|
$msg = '';
|
|
$ok = false;
|
|
$valid = false;
|
|
$user = null;
|
|
if ($token !== '') {
|
|
$stmt = db()->prepare('SELECT id, username, reset_expire FROM ' . tn('users') . ' WHERE reset_token = ?');
|
|
$stmt->execute([$token]);
|
|
$user = $stmt->fetch();
|
|
if ($user && !empty($user['reset_expire']) && strtotime($user['reset_expire']) > time()) {
|
|
$valid = true;
|
|
} else {
|
|
$msg = '重置链接无效或已过期,请重新申请。';
|
|
}
|
|
} else {
|
|
$msg = '链接不完整,请使用邮件中的完整链接。';
|
|
}
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $valid) {
|
|
verifyCsrf();
|
|
$pw = $_POST['password'] ?? '';
|
|
$pw2 = $_POST['password2'] ?? '';
|
|
if (strlen($pw) < 6) {
|
|
$msg = '新密码至少 6 位';
|
|
} elseif ($pw !== $pw2) {
|
|
$msg = '两次输入的密码不一致';
|
|
} else {
|
|
db()->prepare('UPDATE ' . tn('users') . ' SET password = ?, reset_token = ?, reset_expire = NULL WHERE id = ?')
|
|
->execute([password_hash($pw, PASSWORD_DEFAULT), '', $user['id']]);
|
|
$ok = true;
|
|
$msg = '密码已重置成功,请使用新密码登录。';
|
|
}
|
|
}
|
|
require 'includes/header.php';
|
|
?>
|
|
<section class="section auth-section">
|
|
<div class="auth-card verify-card">
|
|
<div class="verify-icon <?= $ok ? 'ok' : ($valid ? 'bad' : 'bad') ?>">
|
|
<i class="fas <?= $ok ? 'fa-check-circle' : ($valid ? 'fa-key' : 'fa-exclamation-triangle') ?>"></i>
|
|
</div>
|
|
<h2 class="verify-title"><?= $ok ? '重置成功' : ($valid ? '设置新密码' : '无法重置') ?></h2>
|
|
<p class="verify-msg"><?= h($msg) ?></p>
|
|
<?php if ($valid && !$ok): ?>
|
|
<form method="post" action="" class="auth-form" style="margin-top:8px">
|
|
<?= csrfField() ?>
|
|
<label>新密码<input type="password" name="password" autocomplete="new-password" required></label>
|
|
<label>确认新密码<input type="password" name="password2" autocomplete="new-password" required></label>
|
|
<button type="submit" class="btn btn-primary"><i class="fas fa-key"></i> 确认重置</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<div class="verify-actions">
|
|
<a href="login.php?mode=login" class="btn btn-primary">前往登录</a>
|
|
<?php if (!$valid && !$ok): ?>
|
|
<a href="login.php?mode=forgot" class="btn btn-ghost">重新申请</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<?php require 'includes/footer.php'; ?>
|