木漏茶室源码下载中心
整站 PHP 源码:可浏览文件、复制路径、下载单文件或打包 ZIP。
发布版本 v6.25.232
升级后自动生成独立源码包,下载中心只发送这份冻结包
已排除 config.php / 用户数据 / 上传文件 / 升级备份
COMMUNITY DEPLOYMENTS
已经跑起来的木漏茶室
从源码下载中心下载、完成安装并回传验证的站外站点。每个站点只计 1 次部署下载量;已记录站点会实时读取名称、版本、Logo 与在线状态,本机、内网和证书异常站点无法完成回传。
实时检测
2
个站点已部署
木
木漏茶室
mlcs.xnn.lol · v6.25.232
访问 ↗
2026.11.21
在线
木
木漏茶室
mlcs.nbnb.mom · v6.25.232
访问 ↗
2026.11.20
在线
已展示全部 2 个站点。
pages/login.php
37.8 KB · 发布包 v6.25.232
<?php
/**
* 登录(前台与后台共用同一账号体系)
*/
if (current_user()) {
redirect(page_url('home'));
}
$next = input('next', '');
$loginMode = login_mode();
$loginMethod = input('method', '') === 'email' ? 'email_code' : (input('method', '') === 'password' ? 'password' : $loginMode);
$loginIdentifier = trim((string) ($_POST['username'] ?? ''));
$loginNeedCaptcha = $loginIdentifier !== '' && security_recent_count('login_fail', $loginIdentifier, 900) >= (int) setting('captcha_login_after', 3);
$rememberChecked = $_SERVER['REQUEST_METHOD'] !== 'POST' ? true : (($_POST['remember'] ?? '') === '1');
// 邮箱验证步骤:第一步密码正确后把待登录账号放在 session,验证码通过后才真正登录。
$emailPending = $_SESSION['login_email_pending'] ?? null;
if (!is_array($emailPending) || (int) ($emailPending['created_at'] ?? 0) < time() - 600) {
unset($_SESSION['login_email_pending']);
$emailPending = null;
}
// TOTP 动态验证步骤:密码正确后、邮箱验证码之前。
$totpPending = $_SESSION['login_totp_pending'] ?? null;
if (!is_array($totpPending) || (int) ($totpPending['created_at'] ?? 0) < time() - 600) {
unset($_SESSION['login_totp_pending']);
$totpPending = null;
}
// 邮箱验证码直接登录(与密码后的邮箱二次验证分开)
$emailCodePending = $_SESSION['login_email_code_pending'] ?? null;
if (!is_array($emailCodePending) || (int) ($emailCodePending['created_at'] ?? 0) < time() - 600) {
unset($_SESSION['login_email_code_pending']);
$emailCodePending = null;
}
/** 登录成功后的统一收尾:会话、最后登录、一次性历史标记与跳转。 */
function login_complete($user, $remember, $next, $username)
{
unset($_SESSION['login_email_pending'], $_SESSION['login_totp_pending']);
login_session($user, (bool) $remember);
security_clear('login_fail', $username);
security_clear('login_email_verify_fail', (string) ($user['email'] ?? ''));
DB::run('UPDATE ' . DB::table('users') . ' SET last_login = ?, last_ip = ?, last_ip_at = ? WHERE id = ?', [now(), client_ip(), now(), $user['id']]);
login_log_record($user, 'success', (string) ($user['username'] ?? ''), '');
// 一次性成功标记,由 login-history.js 在跳转后的页面写入“当前设备登录历史”。
if (!headers_sent()) {
$loginHttps = (!empty($_SERVER['HTTPS']) && strtolower((string) $_SERVER['HTTPS']) !== 'off')
|| (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');
setcookie('km_login_ok', '1', [
'expires' => time() + 180,
'path' => '/',
'secure' => $loginHttps,
'httponly' => false,
'samesite' => 'Lax',
]);
}
flash('欢迎回来,' . display_name($user));
if (session_status() === PHP_SESSION_ACTIVE) {
// 先落盘会话,避免虚拟主机响应结束时才写 session 导致跳转后掉登录。
session_write_close();
}
redirect(safe_next($next, 'index.php?page=home'));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
check_csrf();
$act = trim((string) input('act', ''));
if ($act === 'email_code_send') {
$email = trim((string) input('email', ''));
$remember = ($_POST['remember'] ?? '') === '1';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
flash('请输入正确的邮箱地址', 'err');
redirect(page_url('login', ['next' => $next]));
}
if (security_rate_limited('login_email_code', 3, 900, $email)) {
flash('验证码发送次数过多,请稍后再试', 'err');
redirect(page_url('login', ['next' => $next]));
}
$user = DB::one('SELECT * FROM ' . DB::table('users') . " WHERE email = ? AND status = 1 ORDER BY id ASC LIMIT 1", [$email]);
if ($user && is_official_account($user)) {
// 官方通知账号禁止登录,也不发送验证码;对外统一提示,避免暴露账号身份。
$user = null;
}
if ($user) {
security_event('login_email_code', $email);
list($ok, $msg) = issue_email_code((int) $user['id'], $email, 'login_email_code');
if ($ok) {
$_SESSION['login_email_code_pending'] = [
'uid' => (int) $user['id'],
'email' => $email,
'next' => $next,
'remember' => $remember,
'created_at' => time(),
'last_send' => time(),
];
flash('验证码已发送至 ' . login_email_mask($email));
} else {
flash('验证码发送失败,请稍后重试或联系管理员检查邮件服务', 'err');
}
} else {
// 不暴露邮箱是否已注册
flash('如果该邮箱已绑定账号,验证码将发送到邮箱');
}
redirect(page_url('login', ['next' => $next]));
}
if ($act === 'email_code_resend') {
if (!$emailCodePending) {
flash('验证码已过期,请重新输入邮箱', 'err');
redirect(page_url('login', ['next' => $next]));
}
$email = (string) $emailCodePending['email'];
if (time() - (int) ($emailCodePending['last_send'] ?? 0) < 60) {
flash('发送太频繁,请 60 秒后再重发', 'err');
} elseif (security_rate_limited('login_email_code', 3, 900, $email)) {
flash('验证码发送次数过多,请稍后再试', 'err');
} else {
security_event('login_email_code', $email);
list($ok) = issue_email_code((int) $emailCodePending['uid'], $email, 'login_email_code');
if ($ok) {
$emailCodePending['last_send'] = time();
$emailCodePending['created_at'] = time();
$_SESSION['login_email_code_pending'] = $emailCodePending;
flash('新的验证码已发送至 ' . login_email_mask($email));
} else {
flash('验证码发送失败,请稍后重试', 'err');
}
}
redirect(page_url('login', ['next' => $next]));
}
if ($act === 'email_code_cancel') {
unset($_SESSION['login_email_code_pending']);
flash('已取消邮箱验证码登录');
redirect(page_url('login', ['next' => $next]));
}
if ($act === 'email_code_login') {
if (!$emailCodePending) {
flash('验证码已过期,请重新输入邮箱', 'err');
redirect(page_url('login', ['next' => $next]));
}
$user = refresh_user_security(user_by_id((int) $emailCodePending['uid']));
if (!$user || (int) $user['status'] !== 1 || trim((string) $user['email']) === '') {
unset($_SESSION['login_email_code_pending']);
flash('账号状态已变化,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
if (is_official_account($user)) {
unset($_SESSION['login_email_code_pending']);
flash('官方通知账号仅用于系统发布公告和通知,禁止登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$code = trim((string) ($_POST['code'] ?? ''));
if (!preg_match('/^\d{6}$/', $code) || !consume_email_code((int) $user['id'], $code, 'login_email_code')) {
flash('邮箱验证码不正确或已过期,请重新输入', 'err');
redirect(page_url('login', ['next' => $next]));
}
$remember = !empty($emailCodePending['remember']);
$pendingNext = (string) ($emailCodePending['next'] ?? $next);
unset($_SESSION['login_email_code_pending']);
if (totp_login_required_for($user)) {
$_SESSION['login_totp_pending'] = [
'uid' => (int) $user['id'],
'username' => (string) $user['username'],
'name' => display_name($user),
'remember' => $remember,
'next' => $pendingNext,
'created_at' => time(),
];
unset($_SESSION['login_totp_verified_at']);
flash('邮箱验证通过,请输入动态验证码');
redirect(page_url('login', ['next' => $pendingNext]));
}
login_complete($user, $remember, $pendingNext, (string) $user['username']);
}
if ($act === 'email_login') {
if (!$emailPending) {
flash('邮箱验证已过期,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$uid = (int) ($emailPending['uid'] ?? 0);
$user = refresh_user_security(user_by_id($uid));
if (!$user || (int) $user['status'] !== 1 || !login_email_verify_required_for($user)) {
unset($_SESSION['login_email_pending']);
flash('账号状态已变化,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
if (totp_login_required_for($user) && empty($_SESSION['login_totp_verified_at'])) {
unset($_SESSION['login_email_pending']);
flash('该账号需要先完成动态验证,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$email = (string) $user['email'];
$code = trim((string) ($_POST['code'] ?? ''));
if (security_rate_limited('login_email_verify_fail', 5, 900, $email)) {
unset($_SESSION['login_email_pending']);
flash('验证码错误次数过多,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
if (!preg_match('/^\d{6}$/', $code) || !consume_email_code($uid, $code, 'login_2fa')) {
security_event('login_email_verify_fail', $email);
login_log_record($user, 'fail', (string) $user['username'], '邮箱验证码不正确或已过期');
flash('邮箱验证码不正确或已过期,请重新输入', 'err');
redirect(page_url('login', ['next' => $emailPending['next'] ?? $next]));
}
unset($_SESSION['login_email_pending']);
login_complete($user, !empty($emailPending['remember']), (string) ($emailPending['next'] ?? $next), (string) ($emailPending['username'] ?? $user['username']));
}
if ($act === 'email_resend') {
if (!$emailPending) {
flash('邮箱验证已过期,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$uid = (int) ($emailPending['uid'] ?? 0);
$user = refresh_user_security(user_by_id($uid));
if (!$user || (int) $user['status'] !== 1 || !login_email_verify_required_for($user)) {
unset($_SESSION['login_email_pending']);
flash('账号状态已变化,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$email = (string) $user['email'];
if (time() - (int) ($emailPending['last_send'] ?? 0) < 60) {
flash('发送太频繁,请 60 秒后再重发', 'err');
} elseif (security_rate_limited('login_email_code', 3, 900, $email)) {
flash('验证码发送次数过多,请稍后再试', 'err');
} else {
security_event('login_email_code', $email);
list($ok, $msg) = issue_email_code($uid, $email, 'login_2fa');
$emailPending['last_send'] = time();
$emailPending['created_at'] = time();
$_SESSION['login_email_pending'] = $emailPending;
flash($ok ? '新的验证码已发送至 ' . login_email_mask($email) : '验证码发送失败,请稍后重试或联系管理员检查邮件服务', $ok ? '' : 'err');
}
redirect(page_url('login', ['next' => $next]));
}
if ($act === 'email_cancel') {
unset($_SESSION['login_email_pending'], $_SESSION['login_totp_verified_at']);
flash('已取消本次邮箱验证,请重新登录');
redirect(page_url('login', ['next' => $next]));
}
if ($act === 'totp_login') {
if (!$totpPending) {
flash('动态验证已过期,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$uid = (int) ($totpPending['uid'] ?? 0);
$user = refresh_user_security(user_by_id($uid));
if (!$user || (int) $user['status'] !== 1 || !totp_login_required_for($user)) {
unset($_SESSION['login_totp_pending']);
flash('账号状态已变化,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
if (security_rate_limited('login_totp_fail', 5, 900, (string) $uid)) {
unset($_SESSION['login_totp_pending']);
flash('动态验证码错误次数过多,请重新登录', 'err');
redirect(page_url('login', ['next' => $next]));
}
$code = trim((string) ($_POST['code'] ?? ''));
list($ok, $via) = totp_verify_user_code($user, $code);
if (!$ok) {
security_event('login_totp_fail', (string) $uid);
login_log_record($user, 'fail', (string) $user['username'], '动态验证码或恢复码不正确');
flash('动态验证码或恢复码不正确,请重试', 'err');
redirect(page_url('login', ['next' => $next]));
}
unset($_SESSION['login_totp_pending']);
$_SESSION['login_totp_verified_at'] = time();
security_clear('login_totp_fail', (string) $uid);
$remember = !empty($totpPending['remember']);
$pendingNext = (string) ($totpPending['next'] ?? $next);
$pendingUsername = (string) ($totpPending['username'] ?? $user['username']);
if (login_email_verify_required_for($user)) {
$email = trim((string) $user['email']);
$_SESSION['login_email_pending'] = [
'uid' => (int) $user['id'],
'username' => (string) $user['username'],
'email' => $email,
'remember' => $remember,
'next' => $pendingNext,
'created_at' => time(),
'last_send' => time(),
];
security_event('login_email_code', $email);
list($mailOk) = issue_email_code((int) $user['id'], $email, 'login_2fa');
if ($mailOk) {
flash('动态验证通过,邮箱验证码已发送至 ' . login_email_mask($email));
} else {
flash('动态验证通过,但邮箱验证码发送失败,请稍后重试或联系管理员', 'err');
}
redirect(page_url('login', ['next' => $pendingNext]));
}
login_complete($user, $remember, $pendingNext, $pendingUsername);
}
if ($act === 'totp_cancel') {
unset($_SESSION['login_totp_pending'], $_SESSION['login_totp_verified_at']);
flash('已取消本次动态验证,请重新登录');
redirect(page_url('login', ['next' => $next]));
}
$username = trim((string) ($_POST['username'] ?? ''));
$password = (string) ($_POST['password'] ?? '');
$user = null;
// 只允许用户名登录,已移除按顺序 ID 查账号的兼容逻辑。
if ($username !== '') {
$user = refresh_user_security(user_by_login($username));
}
$lockRemaining = $user ? login_lockout_remaining($user) : 0;
if ($lockRemaining > 0) {
login_log_record($user, 'blocked', $username, '密码错误次数过多,账号临时锁定');
flash('密码错误次数过多,该账号已被限制登录,请在 ' . login_lockout_text($lockRemaining) . '后重试', 'err');
} elseif (security_rate_limited('login_fail', 8, 900, $username)) {
login_log_record(null, 'blocked', $username, '登录失败次数过多');
flash('登录失败次数过多,请 15 分钟后再试', 'err');
} elseif ($loginNeedCaptcha && !captcha_check($_POST['captcha'] ?? '')) {
flash('验证码不正确,请重新计算', 'err');
security_event('login_fail', $username);
login_log_record(null, 'fail', $username, '验证码错误');
$loginNeedCaptcha = true;
} else {
if (!$user || !verify_password($password, $user['password'])) {
$lockSpan = $user ? login_lockout_register_failure($user) : 0;
if ($lockSpan > 0) {
flash('密码错误次数过多,该账号已被限制登录,请在 ' . login_lockout_text($lockSpan) . '后重试', 'err');
login_log_record($user, 'blocked', $username, '密码错误次数达到上限,账号临时锁定');
} else {
flash('用户名或密码不正确', 'err');
login_log_record($user, 'fail', $username, '用户名或密码不正确');
}
security_event('login_fail', $username);
} elseif ((int) $user['status'] !== 1) {
login_lockout_clear($user);
login_log_record($user, 'blocked', (string) $user['username'], '账号已被封禁');
flash('该账号已被封禁,请联系管理员', 'err');
} elseif (is_official_account($user)) {
security_event('login_official_blocked', $username);
login_log_record($user, 'blocked', (string) $user['username'], '官方通知账号禁止登录');
flash('官方通知账号仅用于系统发布公告和通知,禁止登录', 'err');
} elseif (totp_login_required_for($user)) {
login_lockout_clear($user);
$_SESSION['login_totp_pending'] = [
'uid' => (int) $user['id'],
'username' => (string) $user['username'],
'name' => display_name($user),
'remember' => ($_POST['remember'] ?? '') === '1',
'next' => $next,
'created_at' => time(),
];
unset($_SESSION['login_email_pending']);
security_clear('login_fail', $username);
redirect(page_url('login', ['next' => $next]));
} elseif (login_email_verify_required_for($user)) {
login_lockout_clear($user);
$email = trim((string) $user['email']);
$_SESSION['login_email_pending'] = [
'uid' => (int) $user['id'],
'username' => (string) $user['username'],
'email' => $email,
'remember' => ($_POST['remember'] ?? '') === '1',
'next' => $next,
'created_at' => time(),
'last_send' => time(),
];
security_clear('login_fail', $username);
security_event('login_email_code', $email);
list($ok, $msg) = issue_email_code((int) $user['id'], $email, 'login_2fa');
if ($ok) {
flash('密码正确,验证码已发送至 ' . login_email_mask($email));
redirect(page_url('login', ['next' => $next]));
}
// 邮件服务不可用时不能把已通过密码校验的账号锁在登录页,直接完成登录并提示。
flash('邮箱验证码发送失败,已使用密码直接登录;请尽快检查邮件服务', 'err');
login_complete($user, ($_POST['remember'] ?? '') === '1', $next, $username);
} else {
login_complete($user, ($_POST['remember'] ?? '') === '1', $next, $username);
}
}
}
$GLOBALS['PAGE'] = 'login';
render_header('登录');
?>
<?php if (input('bye', '') === '1'): ?>
<div class="notice ok">账号已注销,你的帖子和网盘等全部内容都已清空。</div>
<?php endif; ?>
<section class="auth-wrap"<?= $totpPending ? ' data-login-totp-step="1"' : ($emailPending ? ' data-login-email-step="1"' : '') ?>>
<div class="auth-card">
<div class="auth-art" aria-hidden="true">
<span class="auth-flake" id="authFlake">❄</span>
<img class="auth-avatar" id="authAvatar" src="" alt="" hidden>
<video class="auth-avatar" id="authAvatarVideo" muted loop playsinline preload="none" hidden></video>
<span class="auth-ring"></span>
</div>
<?php if ($totpPending): ?>
<h1 id="authTitle">动态验证</h1>
<p class="auth-sub">密码已验证,请输入验证器 App 中的 6 位动态验证码。</p>
<div class="auth-email-step">
<div class="auth-email-target">
<span>当前账号</span>
<strong><?= e((string) ($totpPending['name'] ?? $totpPending['username'] ?? '账号')) ?></strong>
</div>
<form method="post" class="auth-form">
<?= csrf_field() ?>
<input type="hidden" name="act" value="totp_login">
<input type="hidden" name="next" value="<?= e((string) ($totpPending['next'] ?? $next)) ?>">
<label>动态验证码 / 恢复码</label>
<input type="text" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="12" required autofocus placeholder="6 位动态验证码,或 XXXX-XXXX 恢复码">
<button class="btn-primary block" type="submit">验证并登录</button>
</form>
<div class="auth-email-actions">
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="act" value="totp_cancel">
<input type="hidden" name="next" value="<?= e((string) ($totpPending['next'] ?? $next)) ?>">
<button class="link-btn" type="submit">取消,返回登录</button>
</form>
</div>
</div>
<div class="auth-tip">动态验证码每 30 秒变化一次。手机时间不准确时可能验证失败;验证器丢失时可输入绑定成功时保存的一次性恢复码。</div>
<?php elseif ($emailPending): ?>
<h1 id="authTitle">邮箱验证</h1>
<p class="auth-sub">密码已验证,请输入发送到邮箱的 6 位验证码。</p>
<div class="auth-email-step">
<div class="auth-email-target">
<span>验证码已发送至</span>
<strong><?= e(login_email_mask((string) ($emailPending['email'] ?? ''))) ?></strong>
</div>
<form method="post" class="auth-form">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_login">
<input type="hidden" name="next" value="<?= e((string) ($emailPending['next'] ?? $next)) ?>">
<label>邮箱验证码</label>
<input type="text" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]{6}" required autofocus placeholder="请输入 6 位验证码">
<button class="btn-primary block" type="submit">验证并登录</button>
</form>
<div class="auth-email-actions">
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_resend">
<input type="hidden" name="next" value="<?= e((string) ($emailPending['next'] ?? $next)) ?>">
<button class="btn-mini ghost" type="submit">重新发送验证码</button>
</form>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_cancel">
<input type="hidden" name="next" value="<?= e((string) ($emailPending['next'] ?? $next)) ?>">
<button class="link-btn" type="submit">取消,返回登录</button>
</form>
</div>
</div>
<div class="auth-tip">验证码 10 分钟内有效;如果没收到,请检查垃圾邮件或稍后重新发送。</div>
<?php elseif ($emailCodePending): ?>
<h1 id="authTitle">邮箱验证码登录</h1>
<p class="auth-sub">请输入发送到邮箱的 6 位验证码。</p>
<div class="auth-email-step">
<div class="auth-email-target">
<span>验证码已发送至</span>
<strong><?= e(login_email_mask((string) ($emailCodePending['email'] ?? ''))) ?></strong>
</div>
<form method="post" class="auth-form">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_code_login">
<input type="hidden" name="next" value="<?= e((string) ($emailCodePending['next'] ?? $next)) ?>">
<label>邮箱验证码</label>
<input type="text" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]{6}" required autofocus placeholder="请输入 6 位验证码">
<button class="btn-primary block" type="submit">验证并登录</button>
</form>
<div class="auth-email-actions">
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_code_resend">
<input type="hidden" name="next" value="<?= e((string) ($emailCodePending['next'] ?? $next)) ?>">
<button class="btn-mini ghost" type="submit">重新发送验证码</button>
</form>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_code_cancel">
<input type="hidden" name="next" value="<?= e((string) ($emailCodePending['next'] ?? $next)) ?>">
<button class="link-btn" type="submit">取消,重新输入邮箱</button>
</form>
</div>
</div>
<div class="auth-tip">验证码 10 分钟内有效;如果没收到,请检查垃圾邮件或稍后重新发送。</div>
<?php else: ?>
<h1 id="authTitle"><?= $loginMethod === 'email_code' ? '邮箱验证码登录' : '回到茶室' ?></h1>
<p class="auth-sub"><?= $loginMethod === 'email_code' ? '输入账号已绑定的邮箱,我们会发送 6 位登录验证码。' : '一室茶香,半窗霜雪。' ?></p>
<?php if ($loginMethod === 'email_code'): ?>
<form method="post" class="auth-form">
<?= csrf_field() ?>
<input type="hidden" name="act" value="email_code_send">
<input type="hidden" name="next" value="<?= e($next) ?>">
<label>绑定邮箱</label>
<input type="email" name="email" required autofocus autocomplete="email" placeholder="请输入账号已绑定的邮箱">
<label class="auth-switch" for="loginEmailRemember">
<span class="auth-switch-copy"><strong>永久免登录</strong><small>不主动退出就一直保持登录</small></span>
<span class="auth-switch-control">
<input type="checkbox" name="remember" value="1" id="loginEmailRemember">
<span class="auth-switch-track" aria-hidden="true"><span class="auth-switch-dot"></span></span>
</span>
</label>
<button class="btn-primary block" type="submit">发送登录验证码</button>
</form>
<?php else: ?>
<div class="login-history">
<button type="button" class="login-history-btn" id="loginHistoryBtn" aria-expanded="false" aria-controls="loginHistoryPanel">
<span class="lh-btn-icon" aria-hidden="true">◷</span>
<span>历史账号</span>
<span class="lh-count" id="loginHistoryCount" hidden>0</span>
</button>
<div class="login-history-panel" id="loginHistoryPanel" hidden>
<div class="login-history-head">
<strong>本设备登录过的账号</strong>
<button type="button" class="login-history-clear" id="loginHistoryClear">清空记录</button>
</div>
<div class="login-history-list" id="loginHistoryList"></div>
<p class="login-history-note">只有开启「记住密码」的账号会自动填写密码;记录仅保存在当前设备。</p>
</div>
</div>
<form method="post" class="auth-form">
<?= csrf_field() ?>
<input type="hidden" name="next" value="<?= e($next) ?>">
<label>用户名</label>
<input type="text" name="username" required autofocus autocomplete="username" placeholder="请输入用户名或邮箱" value="<?= e($_POST['username'] ?? '') ?>">
<label>密码</label>
<input type="password" name="password" required autocomplete="current-password">
<label class="auth-switch" for="loginRememberPassword">
<span class="auth-switch-copy"><strong>记住密码</strong><small>仅保存在当前设备,下次自动填写;换设备需重新开启</small></span>
<span class="auth-switch-control">
<input type="checkbox" name="remember_password" value="1" id="loginRememberPassword">
<span class="auth-switch-track" aria-hidden="true"><span class="auth-switch-dot"></span></span>
</span>
</label>
<label class="auth-switch" for="loginRemember">
<span class="auth-switch-copy"><strong>永久免登录</strong><small>不主动退出就一直保持登录</small></span>
<span class="auth-switch-control">
<input type="checkbox" name="remember" value="1" id="loginRemember" <?= $rememberChecked ? 'checked' : '' ?>>
<span class="auth-switch-track" aria-hidden="true"><span class="auth-switch-dot"></span></span>
</span>
</label>
<?php if ($loginNeedCaptcha): ?>
<label>安全验证:<?= e(captcha_question()) ?></label>
<input type="text" name="captcha" inputmode="numeric" required placeholder="请输入计算结果" autocomplete="off">
<?php endif; ?>
<button class="btn-primary block" type="submit">登录</button>
</form>
<?php endif; ?>
<div class="auth-links">
<?php if (setting('register_open', '1') === '1'): ?><a href="<?= e(page_url('register')) ?>">注册新账号</a><?php endif; ?>
<?php if ($loginMethod !== 'email_code'): ?><a href="<?= e(page_url('forgot')) ?>">忘记密码</a><?php endif; ?>
<?php if ($loginMethod === 'email_code' && $loginMode === 'password'): ?>
<a href="<?= e(page_url('login', ['method' => 'password', 'next' => $next])) ?>">使用用户名密码登录</a>
<?php elseif ($loginMethod !== 'email_code'): ?>
<a href="<?= e(page_url('login', ['method' => 'email', 'next' => $next])) ?>">使用邮箱验证码登录</a>
<?php endif; ?>
<a href="<?= e(page_url('home')) ?>">回首页</a>
</div>
<div class="auth-tip"><?= $loginMethod === 'email_code' ? '邮箱验证码登录对已绑定邮箱的账号开放,官方通知账号除外。' : '管理员使用同一入口登录,登录后可从右上角进入后台。' ?></div>
<?php endif; ?>
</div>
</section>
<script>
(function () {
var input = document.querySelector('.auth-form input[name="username"]');
var avatar = document.getElementById('authAvatar');
var avatarVideo = document.getElementById('authAvatarVideo');
var flake = document.getElementById('authFlake');
var title = document.getElementById('authTitle');
if (!input || !avatar || !flake || !avatarVideo) return;
var defaultTitle = title ? title.textContent : '回到茶室';
var BASE = window.KM_BASE || '';
var timer = 0, seq = 0;
function showAvatar(url) {
var isVideo = /\.(mp4|m4v|webm|ogv|mov)(?:[?#].*)?$/i.test(url || '');
avatar.hidden = true;
avatar.removeAttribute('src');
avatarVideo.hidden = true;
avatarVideo.removeAttribute('src');
try { avatarVideo.pause(); } catch (e) {}
if (url && isVideo) {
avatarVideo.src = url;
avatarVideo.hidden = false;
avatarVideo.muted = true;
var p = avatarVideo.play();
if (p && p.catch) p.catch(function () {});
flake.hidden = true;
} else if (url) {
avatar.src = url;
avatar.hidden = false;
flake.hidden = true;
} else {
flake.hidden = false;
}
}
function resetTitle() {
if (title) title.textContent = defaultTitle;
}
function lookup() {
var username = input.value.trim();
var ticket = ++seq;
if (!username) {
showAvatar('');
resetTitle();
return;
}
fetch(BASE + '/api.php?action=user_avatar&username=' + encodeURIComponent(username), {
credentials: 'same-origin'
}).then(function (r) { return r.json(); }).then(function (res) {
if (ticket !== seq) return;
showAvatar(res && res.ok ? res.avatar : '');
if (title) {
title.textContent = (res && res.ok && res.name) ? res.name : defaultTitle;
}
}).catch(function () {
if (ticket === seq) {
showAvatar('');
resetTitle();
}
});
}
input.addEventListener('input', function () {
clearTimeout(timer);
timer = setTimeout(lookup, 350);
});
if (input.value.trim()) lookup();
})();
document.addEventListener('DOMContentLoaded', function () {
var form = document.querySelector('.auth-form');
var usernameInput = form && form.querySelector('input[name="username"]');
var passwordInput = form && form.querySelector('input[name="password"]');
var rememberPassword = form && form.querySelector('input[name="remember_password"]');
var title = document.getElementById('authTitle');
var avatar = document.getElementById('authAvatar');
var avatarVideo = document.getElementById('authAvatarVideo');
var historyBtn = document.getElementById('loginHistoryBtn');
var historyPanel = document.getElementById('loginHistoryPanel');
var historyList = document.getElementById('loginHistoryList');
var historyCount = document.getElementById('loginHistoryCount');
var historyClear = document.getElementById('loginHistoryClear');
if (!form || !usernameInput || !passwordInput || !historyBtn || !historyPanel || !historyList) return;
var defaultTitle = title ? title.textContent.trim() : '回到茶室';
function closePanel() {
historyPanel.hidden = true;
historyBtn.setAttribute('aria-expanded', 'false');
}
function initialOf(text) {
var value = String(text || '').trim();
if (!value) return '茶';
try { return Array.from(value)[0] || '茶'; } catch (e) { return value.charAt(0); }
}
function formatTime(ts) {
var date = new Date(Number(ts) || 0);
if (!date.getTime()) return '';
try {
return date.toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' });
} catch (e) {
return date.toLocaleDateString();
}
}
function makeAvatar(item) {
var wrap = document.createElement('span');
wrap.className = 'lh-item-avatar';
if (item.avatar && !/\.(mp4|m4v|webm|ogv|mov)(?:[?#].*)?$/i.test(item.avatar)) {
var img = document.createElement('img');
img.src = item.avatar;
img.alt = '';
img.loading = 'lazy';
wrap.appendChild(img);
img.addEventListener('error', function () {
wrap.textContent = initialOf(item.name || item.username);
});
} else {
wrap.textContent = initialOf(item.name || item.username);
}
return wrap;
}
function fillAccount(item) {
usernameInput.value = item.username || '';
passwordInput.value = '';
if (rememberPassword) rememberPassword.checked = !!item.rememberPassword;
if (title && item.name) title.textContent = item.name;
usernameInput.dispatchEvent(new Event('input', { bubbles: true }));
closePanel();
if (item.rememberPassword && item.password && window.KMHistory) {
window.KMHistory.decryptPassword(item.password).then(function (password) {
if (password) {
passwordInput.value = password;
var submit = form.querySelector('button[type="submit"]');
if (submit) submit.focus();
return;
}
passwordInput.focus();
});
} else {
passwordInput.focus();
}
}
function renderHistory() {
if (!window.KMHistory) return;
var items = window.KMHistory.list();
historyList.textContent = '';
if (!items.length) {
var empty = document.createElement('div');
empty.className = 'login-history-empty';
empty.textContent = '还没有登录记录,登录成功后会保存在本设备。';
historyList.appendChild(empty);
} else {
items.forEach(function (item) {
var row = document.createElement('button');
row.type = 'button';
row.className = 'login-history-item';
row.title = '使用 ' + (item.name || item.username) + ' 登录';
row.appendChild(makeAvatar(item));
var main = document.createElement('span');
main.className = 'lh-item-main';
var name = document.createElement('strong');
name.textContent = item.name || item.username;
var meta = document.createElement('small');
var date = formatTime(item.updatedAt);
meta.textContent = (item.username && item.username !== (item.name || item.username) ? '@' + item.username + ' · ' : '') + (date ? '上次登录 ' + date : '已保存在本设备');
main.appendChild(name);
main.appendChild(meta);
row.appendChild(main);
var state = document.createElement('span');
state.className = 'lh-item-state ' + (item.rememberPassword ? 'is-saved' : '');
state.textContent = item.rememberPassword ? '已记住密码' : '需要密码';
row.appendChild(state);
row.addEventListener('click', function () { fillAccount(item); });
historyList.appendChild(row);
});
}
historyCount.textContent = String(items.length);
historyCount.hidden = items.length === 0;
}
historyBtn.addEventListener('click', function (e) {
e.stopPropagation();
var open = historyPanel.hidden;
if (open) renderHistory();
historyPanel.hidden = !open;
historyBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
});
if (historyClear) {
historyClear.addEventListener('click', function (e) {
e.stopPropagation();
if (!window.KMHistory) return;
if (!window.confirm('清空当前设备保存的登录账号和密码?')) return;
window.KMHistory.clear();
renderHistory();
});
}
document.addEventListener('click', function (e) {
if (historyPanel.hidden) return;
if (e.target.closest('.login-history')) return;
closePanel();
});
form.addEventListener('submit', function () {
if (!window.KMHistory) return;
var username = usernameInput.value.trim();
if (!username) return;
var remember = !!(rememberPassword && rememberPassword.checked);
var displayName = title && title.textContent.trim() !== defaultTitle ? title.textContent.trim() : '';
var avatarUrl = '';
if (avatar && !avatar.hidden && avatar.src) avatarUrl = avatar.src;
else if (avatarVideo && !avatarVideo.hidden && avatarVideo.src) avatarUrl = avatarVideo.src;
window.KMHistory.savePending({
username: username,
password: remember ? passwordInput.value : '',
remember: remember,
name: displayName,
avatar: avatarUrl,
ts: Date.now()
});
});
window.addEventListener('km:login-saved', function () {
if (!historyPanel.hidden) renderHistory();
});
renderHistory();
});
</script>
<?php render_footer(); ?>