2026-09-28 15:48:35 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 简航 SimpleNav —— 管理员登录 / 首次初始化
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
require_once __DIR__ . '/functions.php';
|
|
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
|
|
|
|
session_start();
|
|
|
|
|
}
|
2026-09-28 16:43:10 +08:00
|
|
|
send_no_cache();
|
2026-09-28 15:48:35 +08:00
|
|
|
|
|
|
|
|
$hasAdmin = (int)db()->query('SELECT COUNT(*) FROM users')->fetchColumn() > 0;
|
|
|
|
|
$err = '';
|
|
|
|
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
|
$u = trim((string)($_POST['username'] ?? ''));
|
|
|
|
|
$p = (string)($_POST['password'] ?? '');
|
|
|
|
|
if ($u === '' || $p === '') {
|
|
|
|
|
$err = '请输入用户名和密码';
|
|
|
|
|
} elseif (mb_strlen($p) < 4) {
|
|
|
|
|
$err = '密码至少 4 位';
|
|
|
|
|
} elseif (!$hasAdmin) {
|
|
|
|
|
// 首次创建管理员
|
|
|
|
|
$st = db()->prepare('INSERT INTO users (username, password_hash, created_at) VALUES (?,?,?)');
|
|
|
|
|
$st->execute([$u, password_hash($p, PASSWORD_DEFAULT), date('Y-m-d H:i:s')]);
|
|
|
|
|
$_SESSION['uid'] = (int)db()->lastInsertId();
|
|
|
|
|
header('Location: ' . url('admin/'));
|
|
|
|
|
exit;
|
|
|
|
|
} else {
|
|
|
|
|
// 登录校验
|
|
|
|
|
$st = db()->prepare('SELECT * FROM users WHERE username=? LIMIT 1');
|
|
|
|
|
$st->execute([$u]);
|
|
|
|
|
$row = $st->fetch();
|
|
|
|
|
if ($row && password_verify($p, $row['password_hash'])) {
|
|
|
|
|
$_SESSION['uid'] = (int)$row['id'];
|
|
|
|
|
header('Location: ' . url('admin/'));
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
$err = '用户名或密码错误';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$title = $hasAdmin ? '登录' : '初始化管理员';
|
|
|
|
|
?><!DOCTYPE html>
|
|
|
|
|
<html lang="zh-CN">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title><?= e($title) ?> · <?= e(site_name()) ?></title>
|
2026-09-28 16:43:10 +08:00
|
|
|
<link rel="stylesheet" href="<?= e(asset('assets/css/style.css')) ?>">
|
|
|
|
|
<link rel="stylesheet" href="<?= e(asset('assets/css/admin.css')) ?>">
|
2026-09-28 22:28:23 +08:00
|
|
|
<?= site_icon_head() ?>
|
2026-09-28 15:48:35 +08:00
|
|
|
</head>
|
|
|
|
|
<body class="admin">
|
|
|
|
|
<div class="login-wrap">
|
|
|
|
|
<form class="login-card" method="post" action="">
|
2026-09-28 22:28:23 +08:00
|
|
|
<img class="login-logo" src="<?= e(site_icon_url('logo', 512) ?: url('assets/img/logo.svg')) ?>" alt="" onerror="this.style.display='none'">
|
2026-09-28 15:48:35 +08:00
|
|
|
<h1><?= e(site_name()) ?></h1>
|
|
|
|
|
<p class="sub"><?= $hasAdmin ? '管理员登录' : '首次使用,请创建管理员账号' ?></p>
|
|
|
|
|
<?php if ($err): ?><div class="alert"><?= e($err) ?></div><?php endif; ?>
|
|
|
|
|
<input type="text" name="username" placeholder="用户名" autocomplete="username" required>
|
|
|
|
|
<input type="password" name="password" placeholder="密码" autocomplete="<?= $hasAdmin ? 'current-password' : 'new-password' ?>" required>
|
|
|
|
|
<button type="submit" class="btn primary block"><?= $hasAdmin ? '登录' : '创建并进入' ?></button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|