上传文件至 /
This commit is contained in:
114
change_password.php
Normal file
114
change_password.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/includes/layout.php';
|
||||
|
||||
$user = require_login($conn);
|
||||
$forced = !empty($user['must_change_password']);
|
||||
$locked = !empty($user['password_locked']) && !$forced;
|
||||
|
||||
$message = null;
|
||||
$ok = false;
|
||||
$password_rule = '至少 8 位,须同时包含字母和数字';
|
||||
|
||||
function strong_password_ok($pw)
|
||||
{
|
||||
return strlen($pw) >= 8 && strlen($pw) <= 64
|
||||
&& preg_match('/[A-Za-z]/', $pw) && preg_match('/\d/', $pw)
|
||||
&& preg_match('/^[\x21-\x7e]+$/', $pw); // 可打印 ASCII,禁空格/中文
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password']) && !$locked) {
|
||||
if (!csrf_ok()) {
|
||||
$message = '请求校验失败,请刷新页面重试。';
|
||||
} else {
|
||||
$current = $_POST['current_password'] ?? '';
|
||||
$new = $_POST['new_password'] ?? '';
|
||||
|
||||
if (!strong_password_ok($new)) {
|
||||
$message = $password_rule . '。';
|
||||
} elseif ($new === $current) {
|
||||
$message = '新密码不能与当前密码相同。';
|
||||
} else {
|
||||
$stmt = $conn->prepare("SELECT password FROM photo_users WHERE user_id = ?");
|
||||
$stmt->bind_param("i", $user['user_id']);
|
||||
$stmt->execute();
|
||||
$hash = $stmt->get_result()->fetch_assoc()['password'] ?? '';
|
||||
$stmt->close();
|
||||
|
||||
if (!password_verify($current, $hash)) {
|
||||
$message = '当前密码错误。';
|
||||
} else {
|
||||
$newHash = password_hash($new, PASSWORD_DEFAULT);
|
||||
$upd = $conn->prepare("UPDATE photo_users SET password = ?, must_change_password = 0 WHERE user_id = ?");
|
||||
$upd->bind_param("si", $newHash, $user['user_id']);
|
||||
$ok = $upd->execute();
|
||||
$upd->close();
|
||||
$message = $ok ? '密码已修改成功。' : '修改失败,请稍后再试。';
|
||||
if ($ok && $forced) {
|
||||
$_SESSION['login_pending_pwd'] = false;
|
||||
flash_set('success', '密码已更新,欢迎使用');
|
||||
header($user['is_admin'] ? 'Location: admin.php' : 'Location: gallery.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render_header('修改密码', $user, 'change_password.php', '账号安全');
|
||||
?>
|
||||
<div class="page-head">
|
||||
<div>
|
||||
<div class="page-head__title"><?php echo $forced ? '首次登录请先改密码' : '修改密码'; ?></div>
|
||||
<div class="page-head__desc">
|
||||
<?php if ($locked): ?>
|
||||
管理员已锁定此账号的密码,如需修改请联系管理员。
|
||||
<?php elseif ($forced): ?>
|
||||
这是管理员为你设置的初始密码,改成你自己的密码后才能继续使用。
|
||||
<?php else: ?>
|
||||
定期更换密码,保护你的相册安全
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="max-width:520px">
|
||||
<div class="card__body">
|
||||
<?php if ($message): ?>
|
||||
<div class="alert alert--<?php echo $ok ? 'success' : 'danger'; ?>">
|
||||
<?php echo icon($ok ? 'check' : 'warn'); ?><span><?php echo h($message); ?></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($locked): ?>
|
||||
<div class="alert alert--warning">
|
||||
<?php echo icon('lock'); ?><span>密码已锁定,无法自行修改。需要新密码请让管理员重置。</span>
|
||||
</div>
|
||||
<a href="logout.php" class="btn btn--ghost"><?php echo icon('logout', 15); ?>退出登录</a>
|
||||
<?php else: ?>
|
||||
<form method="post">
|
||||
<input type="hidden" name="csrf_token" value="<?php echo h(csrf_token()); ?>">
|
||||
<div class="field">
|
||||
<label class="label" for="current_password">当前密码</label>
|
||||
<input class="input" type="password" id="current_password" name="current_password" autocomplete="current-password" required autofocus>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="new_password">新密码</label>
|
||||
<div class="input-group">
|
||||
<input class="input" type="password" id="new_password" name="new_password" minlength="8" autocomplete="new-password" required>
|
||||
<button type="button" class="btn btn--ghost btn--icon" data-toggle-password="new_password" aria-label="显示密码"><?php echo icon('eye'); ?></button>
|
||||
</div>
|
||||
<div class="help"><?php echo h($password_rule); ?></div>
|
||||
</div>
|
||||
<div class="toolbar" style="margin:0">
|
||||
<button type="submit" name="change_password" class="btn btn--primary"><?php echo icon('lock', 15); ?>修改密码</button>
|
||||
<?php if (!$forced): ?>
|
||||
<a href="<?php echo $user['is_admin'] ? 'admin.php' : 'gallery.php'; ?>" class="btn btn--ghost">返回</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php if ($forced): ?>
|
||||
<p class="muted small" style="margin-top:.75rem">不想现在改?<a href="logout.php">退出登录</a></p>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php render_footer($user, 'change_password.php');
|
||||
Reference in New Issue
Block a user