优化网站
This commit is contained in:
1 parent
9bb6e6a41e
commit
0e7265481f
22 files changed
+1071
-238
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* auth.php —— 后台登录会话工具
|
||||
* 密码存储算法(按需求):md5(base64(明文))
|
||||
* 密码存储算法:PHP password_hash 强散列(bcrypt/argon2);兼容旧库 md5(base64(明文)) 并自动升级。
|
||||
*/
|
||||
require_once __DIR__ . '/db.php';
|
||||
|
||||
|
||||
+67
-1
@@ -215,7 +215,7 @@ function db_init(PDO $pdo): void
|
||||
'search_engines' => '', // 搜索引擎列表 JSON;空 = 使用默认 4 个
|
||||
'last_updated' => '',
|
||||
'hero_bg' => '', // 首页顶部标识栏背景(纯色 / CSS 渐变),空 = 主题默认
|
||||
'hero_mode' => 'weather', // 首页顶部组件模式:weather=天气(默认) / info=访客信息 / blank=空白精简
|
||||
'hero_mode' => 'weather', // 首页顶部组件模式:weather=天气(默认) / info=信息 / blank=精简 / red=渗透测试 / blue=安全巡检 / posture=安全态势 / trend=访问趋势 / clock=大字时钟 / notice=公告滚动 / slogan=打字机标语
|
||||
'cdn_ranges' => '', // CDN 段库文本;空 = 使用默认内置列表
|
||||
'article_note' => '', // 文章区首页顶部注要(Markdown 风格,纯文本)
|
||||
];
|
||||
@@ -568,6 +568,72 @@ function visitors_entries(): array
|
||||
return array_reverse($out); // 新→旧
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页顶部组件「安全态势 / 访问趋势」所需的汇总数据(一次遍历访问日志 + 两条轻量查询)。
|
||||
* 返回:今日/累计访问、今日/累计独立 IP、拦截次数、黑名单条数、近 N 天每日访问、最近一次成功登录。
|
||||
*/
|
||||
function home_security_stats(int $days = 7): array
|
||||
{
|
||||
$entries = visitors_entries();
|
||||
$total = count($entries);
|
||||
$today = 0;
|
||||
$allIps = [];
|
||||
$todayIps = [];
|
||||
$blockedTotal = 0;
|
||||
$blockedToday = 0;
|
||||
$todayStart = date('Y-m-d 00:00:00');
|
||||
// 近 N 天日期桶(含今天,由旧到新)
|
||||
$buckets = [];
|
||||
$order = [];
|
||||
for ($i = $days - 1; $i >= 0; $i--) {
|
||||
$d = date('Y-m-d', strtotime('-' . $i . ' day'));
|
||||
$buckets[$d] = 0;
|
||||
$order[] = $d;
|
||||
}
|
||||
foreach ($entries as $e) {
|
||||
$allIps[$e['ip']] = 1;
|
||||
if ((int) $e['status'] === 403) {
|
||||
$blockedTotal++;
|
||||
}
|
||||
$day = substr((string) $e['created_at'], 0, 10);
|
||||
if (isset($buckets[$day])) {
|
||||
$buckets[$day]++;
|
||||
}
|
||||
if ((string) $e['created_at'] >= $todayStart) {
|
||||
$today++;
|
||||
$todayIps[$e['ip']] = 1;
|
||||
if ((int) $e['status'] === 403) {
|
||||
$blockedToday++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$daily = [];
|
||||
foreach ($order as $d) {
|
||||
$daily[] = ['day' => $d, 'cnt' => $buckets[$d]];
|
||||
}
|
||||
$ban = (int) db()->query('SELECT COUNT(*) FROM ip_blacklist')->fetchColumn();
|
||||
$lastLogin = null;
|
||||
$row = db()->query("SELECT username, ip, created_at FROM login_log WHERE status = 'ok' ORDER BY created_at DESC, id DESC LIMIT 1")->fetch();
|
||||
if ($row) {
|
||||
$lastLogin = [
|
||||
'username' => (string) ($row['username'] ?? ''),
|
||||
'ip' => (string) ($row['ip'] ?? ''),
|
||||
'time' => (string) ($row['created_at'] ?? ''),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'total' => $total,
|
||||
'today' => $today,
|
||||
'ips_total' => count($allIps),
|
||||
'ips_today' => count($todayIps),
|
||||
'blocked_total' => $blockedTotal,
|
||||
'blocked_today' => $blockedToday,
|
||||
'ban' => $ban,
|
||||
'daily' => $daily,
|
||||
'last_login' => $lastLogin,
|
||||
];
|
||||
}
|
||||
|
||||
/** 自动启动:注册访问日志;黑名单对所有请求(前台与后台登录页)统一拦截;自动风控自检仅针对前台 */
|
||||
function access_boot(): void
|
||||
{
|
||||
|
||||
@@ -146,28 +146,6 @@ function layout_footer(): void
|
||||
echo '</div></footer>' . "\n";
|
||||
}
|
||||
|
||||
/** 读取首页展示的名言(quota.txt 随机一行) */
|
||||
function home_quote(): string
|
||||
{
|
||||
$file = DATA_DIR . '/quota.txt';
|
||||
$lines = [];
|
||||
if (is_file($file)) {
|
||||
$raw = @file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if (is_array($raw)) {
|
||||
foreach ($raw as $ln) {
|
||||
$ln = trim($ln);
|
||||
if ($ln !== '' && strpos($ln, '#') !== 0) {
|
||||
$lines[] = $ln;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$lines) {
|
||||
return '知识就是力量。';
|
||||
}
|
||||
return $lines[array_rand($lines)];
|
||||
}
|
||||
|
||||
/** 首页访客信息(hero 信息模式):IP 与浏览器/系统/内核由服务端解析 UA 提供,屏幕/语言由前端补齐 */
|
||||
function visitor_brief(): array
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user