SOURCE DOWNLOAD

木漏茶室源码下载中心

整站 PHP 源码:可浏览文件、复制路径、下载单文件或打包 ZIP。

发布版本 v6.25.232 升级后自动生成独立源码包,下载中心只发送这份冻结包 已排除 config.php / 用户数据 / 上传文件 / 升级备份
komorebi-forum 183 个节点 · PHP 8.3.37 · 完整源码 ZIP 1.31 MB
下载完整源码 ZIP 源码包大小:1.31 MB 安装教程 作者主页 免费开源 免费下载 ↓
COMMUNITY DEPLOYMENTS

已经跑起来的木漏茶室

从源码下载中心下载、完成安装并回传验证的站外站点。每个站点只计 1 次部署下载量;已记录站点会实时读取名称、版本、Logo 与在线状态,本机、内网和证书异常站点无法完成回传。

实时检测
2 个站点已部署

已展示全部 2 个站点。

admin/security_guard_ajax.php 15.3 KB · 发布包 v6.25.232
下载
<?php
/**
 * 后台 · 木卫守护 AJAX
 */
if (!defined('APP_ROOT')) { require __DIR__ . '/../inc/bootstrap.php'; }
require_once __DIR__ . '/../inc/security_guard.php';
require_once __DIR__ . '/../inc/first_guard.php';
$admin = current_user();
if (!is_admin($admin)) {
    http_response_code(403);
    exit;
}

if (input('ajax') === '1') {
    header('Content-Type: application/json; charset=utf-8');
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
        json_out(['ok' => false, 'msg' => '请求方式不正确'], 405);
    }
    check_csrf();
    $act = (string) input('act', '');
    try {
        if ($act === 'guard_scan_start') {
            $_SESSION['security_guard_scan'] = ['started' => time(), 'findings' => []];
            json_out(['ok' => true, 'total' => count(security_guard_checks())]);
        }
        if ($act === 'guard_scan_all') {
            @set_time_limit(120);
            $findings = security_guard_scan_all();
            $total = count($findings);
            $_SESSION['security_guard_scan'] = ['started' => time(), 'findings' => $findings];
            $_SESSION['security_guard_last'] = ['at' => now(), 'total' => $total, 'failed' => security_guard_count_failed($findings)];
            security_guard_public_store($findings);
            json_out(['ok' => true, 'total' => $total, 'findings' => $findings, 'failed' => security_guard_count_failed($findings)]);
        }
        if ($act === 'guard_scan_step') {
            $i = max(0, (int) input('i', 0));
            $checks = security_guard_checks();
            if (!isset($checks[$i])) {
                json_out(['ok' => false, 'msg' => '检测序号超出范围'], 422);
            }
            $finding = security_guard_run_check($checks[$i]['id']);
            if (!isset($_SESSION['security_guard_scan']) || !is_array($_SESSION['security_guard_scan'])) {
                $_SESSION['security_guard_scan'] = ['started' => time(), 'findings' => []];
            }
            $_SESSION['security_guard_scan']['findings'][] = $finding;
            $total = count($checks);
            $findings = $_SESSION['security_guard_scan']['findings'];
            if (count($findings) >= $total) {
                $_SESSION['security_guard_last'] = ['at' => now(), 'total' => $total, 'failed' => security_guard_count_failed($findings)];
                security_guard_public_store($findings);
            }
            json_out([
                'ok' => true,
                'index' => $i,
                'total' => $total,
                'progress' => (int) round(($i + 1) / max(1, $total) * 100),
                'finding' => $finding,
            ]);
        }
        if ($act === 'guard_fix') {
            @set_time_limit(120);
            $before = security_guard_scan_all();
            $fixed = [];
            $failed = [];
            foreach ($before as $finding) {
                if (($finding['status'] ?? '') !== 'fail' || empty($finding['fixable'])) { continue; }
                list($ok, $message) = security_guard_fix($finding['id']);
                if ($ok) {
                    $fixed[] = ['id' => $finding['id'], 'title' => $finding['title'], 'message' => $message];
                } else {
                    $failed[] = ['id' => $finding['id'], 'title' => $finding['title'], 'message' => $message];
                }
            }
            $after = security_guard_scan_all();
            $_SESSION['security_guard_scan'] = ['started' => time(), 'findings' => $after];
            $_SESSION['security_guard_last'] = ['at' => now(), 'total' => count($after), 'failed' => security_guard_count_failed($after)];
            security_guard_public_store($after);
            json_out([
                'ok' => true,
                'fixed' => $fixed,
                'failed' => $failed,
                'findings' => $after,
                'remaining' => security_guard_count_failed($after),
            ]);
        }
        if ($act === 'guard_first_complete') {
            if (!first_guard_pending($admin)) {
                json_out(['ok' => false, 'msg' => '首次安装检测标记已失效'], 409);
            }
            first_guard_complete();
            json_out(['ok' => true, 'msg' => '首次安装检测已完成']);
        }
        if ($act === 'guard_patch') {
            @set_time_limit(120);
            list($ok, $msg) = security_patch_apply(true);
            json_out([
                'ok' => true,
                'applied' => $ok,
                'msg' => $msg,
                'auto' => security_patch_auto_enabled(),
                'realtime' => security_patch_realtime_enabled(),
                'version' => security_patch_current_version(),
                'last' => security_patch_last_version(),
                'at' => security_patch_last_at(),
            ]);
        }
        if ($act === 'guard_realtime_status') {
            json_out(['ok' => true, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_set_realtime') {
            $enabled = input('enabled') === '1';
            set_setting('realtime_guard_enabled', $enabled ? '1' : '0');
            realtime_guard_event($enabled ? 'rt_guard_on' : 'rt_guard_off', $enabled ? '实时保护已开启' : '实时保护已关闭');
            json_out(['ok' => true, 'enabled' => $enabled, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_set_refresh') {
            $enabled = input('enabled') === '1';
            set_setting('realtime_guard_refresh_enabled', $enabled ? '1' : '0');
            realtime_guard_event($enabled ? 'rt_debug_refresh_on' : 'rt_debug_refresh_off', $enabled ? 'F12 / 右键自动刷新已开启' : 'F12 / 右键自动刷新已关闭');
            json_out(['ok' => true, 'enabled' => $enabled, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_ban_ip') {
            $ip = trim((string) input('ip', ''));
            $reason = trim((string) input('reason', ''));
            if (!filter_var($ip, FILTER_VALIDATE_IP)) {
                json_out(['ok' => false, 'msg' => 'IP 地址格式不正确'], 422);
            }
            if (ip_is_private($ip)) {
                json_out(['ok' => false, 'msg' => '内网 / 本机 IP 不建议永久封禁,请核对目标 IP'], 422);
            }
            if ($ip === realtime_guard_ip()) {
                json_out(['ok' => false, 'msg' => '不能封禁当前管理员正在使用的 IP'], 422);
            }
            list($ok, $msg) = guard_ban_add('ip', $ip, $reason, (int) $admin['id']);
            if (!$ok) { json_out(['ok' => false, 'msg' => $msg], 422); }
            realtime_guard_event('rt_ban_ip', $ip, '', 0);
            json_out(['ok' => true, 'msg' => $msg, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_ban_device') {
            $fingerprint = trim((string) input('fingerprint', ''));
            $reason = trim((string) input('reason', ''));
            if (!preg_match('/^[a-f0-9]{64}$/', $fingerprint)) {
                json_out(['ok' => false, 'msg' => '设备 ID 格式不正确'], 422);
            }
            if (hash_equals($fingerprint, guard_ban_device_fingerprint())) {
                json_out(['ok' => false, 'msg' => '不能封禁当前管理员正在使用的设备'], 422);
            }
            $deviceIp = guard_ban_device_last_ip($fingerprint);
            list($ok, $msg) = guard_ban_add('device', $fingerprint, $reason, (int) $admin['id'], $deviceIp);
            if (!$ok) { json_out(['ok' => false, 'msg' => $msg], 422); }
            realtime_guard_event('rt_ban_device', '设备 ' . substr($fingerprint, 0, 16) . ($deviceIp !== '' ? ' / IP ' . $deviceIp : ''), '', 0);
            json_out(['ok' => true, 'msg' => $msg, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_device_note') {
            $fingerprint = trim((string) input('fingerprint', ''));
            $note = trim((string) input('note', ''));
            if (!preg_match('/^[a-f0-9]{64}$/', $fingerprint)) {
                json_out(['ok' => false, 'msg' => '设备 ID 格式不正确'], 422);
            }
            if (mb_strlen($note) > 60) {
                json_out(['ok' => false, 'msg' => '设备备注最多 60 个字'], 422);
            }
            $deviceId = DB::val('SELECT id FROM ' . DB::table('guard_devices') . ' WHERE fingerprint = ? LIMIT 1', [$fingerprint]);
            if (!$deviceId) {
                json_out(['ok' => false, 'msg' => '没有找到这台设备'], 404);
            }
            DB::run('UPDATE ' . DB::table('guard_devices') . ' SET note = ? WHERE fingerprint = ?', [$note, $fingerprint]);
            json_out(['ok' => true, 'msg' => '设备备注已保存', 'note' => $note, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_ban404_save') {
            $title = trim((string) input('title', ''));
            $message = trim((string) input('message', ''));
            $enabled = input('enabled') === '1';
            $removeMedia = input('remove_media') === '1';
            $oldMedia = (string) setting('guard_ban404_media', '');
            $oldMediaPath = ($oldMedia !== '' && strpos($oldMedia, 'uploads/security_guard/ban404/') === 0 && strpos($oldMedia, '..') === false)
                ? APP_ROOT . '/' . $oldMedia : '';
            $savedFile = null;
            if (!$removeMedia && isset($_FILES['file']) && is_array($_FILES['file']) && (int) $_FILES['file']['error'] !== UPLOAD_ERR_NO_FILE) {
                $exts = array_merge(image_upload_extensions(), guard_ban404_video_extensions());
                $uploaded = upload_store('file', 'security_guard/ban404', ['max' => 0, 'allow' => $exts]);
                $ext = strtolower((string) pathinfo($uploaded['path'], PATHINFO_EXTENSION));
                $mime = (string) ($uploaded['mime'] ?? '');
                $isVideo = strpos($mime, 'video/') === 0 || in_array($ext, guard_ban404_video_extensions(), true);
                $isImage = strpos($mime, 'image/') === 0 || in_array($ext, image_upload_extensions(), true);
                if ($ext === 'svg' && !image_svg_is_safe((string) file_get_contents(APP_ROOT . '/' . $uploaded['path']))) {
                    @unlink(APP_ROOT . '/' . $uploaded['path']);
                    json_out(['ok' => false, 'msg' => 'SVG 图片包含不安全的脚本内容'], 422);
                }
                if (!$isVideo && !$isImage) {
                    @unlink(APP_ROOT . '/' . $uploaded['path']);
                    json_out(['ok' => false, 'msg' => '仅支持图片或视频文件'], 422);
                }
                $savedFile = $uploaded;
                set_setting('guard_ban404_media', $uploaded['path']);
                set_setting('guard_ban404_media_type', $isVideo ? 'video' : 'image');
                set_setting('guard_ban404_media_name', (string) ($uploaded['name'] ?? basename($uploaded['path'])));
                if ($oldMediaPath !== '' && is_file($oldMediaPath)) { @unlink($oldMediaPath); }
            } elseif ($removeMedia) {
                if ($oldMediaPath !== '' && is_file($oldMediaPath)) { @unlink($oldMediaPath); }
                set_setting('guard_ban404_media', '');
                set_setting('guard_ban404_media_type', '');
                set_setting('guard_ban404_media_name', '');
            }
            set_setting('guard_ban404_title', mb_substr($title, 0, 80));
            set_setting('guard_ban404_message', mb_substr($message, 0, 800));
            set_setting('guard_ban404_enabled', $enabled ? '1' : '0');
            json_out([
                'ok' => true,
                'msg' => $savedFile ? '封禁 404 页与媒体已保存' : '封禁 404 页设置已保存',
                'config' => guard_ban404_config(),
            ]);
        }
        if ($act === 'guard_unban_send_code') {
            list($sendOk, $sendMsg) = account_delete_code_send($admin, 'admin_guard_unban');
            json_out([
                'ok' => (bool) $sendOk,
                'msg' => (string) $sendMsg,
                'email_mask' => $sendOk ? email_mask((string) ($admin['email'] ?? '')) : '',
            ]);
        }
        if ($act === 'guard_unban') {
            guard_ban_ensure_table();
            $id = (int) input('id', 0);
            if ($id <= 0) {
                json_out(['ok' => false, 'msg' => '封禁记录不存在'], 422);
            }
            list($codeOk, $codeMsg) = account_delete_code_check($admin, input('guard_email_code'), 'admin_guard_unban');
            if (!$codeOk) {
                json_out(['ok' => false, 'msg' => '解除永久封禁需要验证当前管理员绑定邮箱:' . $codeMsg], 422);
            }
            $ban = DB::one('SELECT * FROM ' . DB::table('guard_bans') . ' WHERE id = ? LIMIT 1', [$id]);
            if (!$ban) {
                json_out(['ok' => false, 'msg' => '封禁记录不存在'], 404);
            }
            if (!guard_ban_remove($id)) {
                json_out(['ok' => false, 'msg' => '解除封禁失败'], 500);
            }
            realtime_guard_event((string) $ban['ban_type'] === 'ip' ? 'rt_unban_ip' : 'rt_unban_device', (string) $ban['ban_value'], '', 0);
            json_out(['ok' => true, 'msg' => '已解除封禁', 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_unblock') {
            $ip = trim((string) input('ip', ''));
            if (!filter_var($ip, FILTER_VALIDATE_IP)) {
                json_out(['ok' => false, 'msg' => 'IP 地址格式不正确'], 422);
            }
            DB::run('DELETE FROM ' . DB::table('guard_blocks') . ' WHERE ip = ?', [$ip]);
            realtime_guard_event('rt_unblock', $ip, '', 0);
            json_out(['ok' => true, 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_rebuild_baseline') {
            realtime_guard_self_check(true);
            json_out(['ok' => true, 'msg' => '完整性基线已重建', 'data' => realtime_guard_status(15)]);
        }
        if ($act === 'guard_set_auto_patch') {
            $enabled = input('enabled') === '1';
            set_setting('security_auto_patch', $enabled ? '1' : '0');
            if ($enabled) { security_patch_apply(false); }
            json_out([
                'ok' => true,
                'enabled' => $enabled,
                'auto' => security_patch_auto_enabled(),
                'realtime' => security_patch_realtime_enabled(),
                'version' => security_patch_current_version(),
                'last' => security_patch_last_version(),
                'at' => security_patch_last_at(),
            ]);
        }
        if ($act === 'guard_set_realtime_patch') {
            $enabled = input('enabled') === '1';
            set_setting('security_patch_realtime', $enabled ? '1' : '0');
            if ($enabled) { security_patch_apply(false); }
            json_out([
                'ok' => true,
                'enabled' => $enabled,
                'auto' => security_patch_auto_enabled(),
                'realtime' => security_patch_realtime_enabled(),
                'version' => security_patch_current_version(),
                'last' => security_patch_last_version(),
                'at' => security_patch_last_at(),
            ]);
        }
        json_out(['ok' => false, 'msg' => '未知操作'], 422);
    } catch (Throwable $e) {
        json_out(['ok' => false, 'msg' => $e->getMessage()], 500);
    }
}

function security_guard_count_failed($findings)
{
    $n = 0;
    foreach ((array) $findings as $finding) {
        if (($finding['status'] ?? '') === 'fail') { $n++; }
    }
    return $n;
}

主题