<?php

require_once __DIR__ . '/common.php';

admin_require_login();

$pdo       = db();
$adminUser = admin_current_user();
$siteTitle = get_setting('site.title', '六柱个人博客');

$errors = [];





function admin_seed_default_sensitive_words(PDO $pdo): void
{
    static $done = false;
    if ($done) {
        return;
    }
    $done = true;

    $defaults = [
        
        ['word' => '傻逼',      'category' => 'abuse'],
        ['word' => 'SB',        'category' => 'abuse'],
        ['word' => '傻叉',      'category' => 'abuse'],
        ['word' => '傻子',      'category' => 'abuse'],
        ['word' => '脑残',      'category' => 'abuse'],
        ['word' => '垃圾玩意',  'category' => 'abuse'],
        ['word' => '垃圾人',    'category' => 'abuse'],
        ['word' => '废物',      'category' => 'abuse'],
        ['word' => '去死',      'category' => 'abuse'],
        ['word' => '滚蛋',      'category' => 'abuse'],
        ['word' => '你妈',      'category' => 'abuse'],
        ['word' => '狗东西',    'category' => 'abuse'],

        
        ['word' => '广告',      'category' => 'ads'],
        ['word' => '推广',      'category' => 'ads'],
        ['word' => '赚大钱',    'category' => 'ads'],
        ['word' => '赚钱项目',  'category' => 'ads'],
        ['word' => '日赚',      'category' => 'ads'],
        ['word' => '暴利项目',  'category' => 'ads'],
        ['word' => '暴利',      'category' => 'ads'],
        ['word' => '返利',      'category' => 'ads'],
        ['word' => '优惠券',    'category' => 'ads'],
        ['word' => '粉丝裂变',  'category' => 'ads'],
        ['word' => '代理招商',  'category' => 'ads'],
        ['word' => '代理加盟',  'category' => 'ads'],
        ['word' => '加盟',      'category' => 'ads'],
        ['word' => '微商',      'category' => 'ads'],
        ['word' => '品牌代理',  'category' => 'ads'],
        ['word' => '拉人头',    'category' => 'ads'],
        ['word' => '兼职加群',  'category' => 'ads'],
        ['word' => '高薪兼职',  'category' => 'ads'],
        ['word' => '刷单',      'category' => 'ads'],
        ['word' => '网赚',      'category' => 'ads'],

        
        ['word' => '博彩',      'category' => 'ads'],
        ['word' => '彩票',      'category' => 'ads'],
        ['word' => '下注',      'category' => 'ads'],
        ['word' => '投注',      'category' => 'ads'],
        ['word' => '赌博',      'category' => 'ads'],
        ['word' => '赌钱',      'category' => 'ads'],
        ['word' => '赌坊',      'category' => 'ads'],
        ['word' => '赌场',      'category' => 'ads'],
        ['word' => '线上赌场',  'category' => 'ads'],
        ['word' => '线上赌',    'category' => 'ads'],
        ['word' => '网赌',      'category' => 'ads'],
        ['word' => '真人赌场',  'category' => 'ads'],
        ['word' => '真钱棋牌',  'category' => 'ads'],
        ['word' => '棋牌赌博',  'category' => 'ads'],
        ['word' => '百家乐',    'category' => 'ads'],
        ['word' => '六合彩',    'category' => 'ads'],
        ['word' => '时时彩',    'category' => 'ads'],
        ['word' => '牛牛棋牌',  'category' => 'ads'],
        ['word' => '炸金花',    'category' => 'ads'],

        
        ['word' => '贷款',      'category' => 'ads'],
        ['word' => '小额贷',    'category' => 'ads'],
        ['word' => '小额贷款',  'category' => 'ads'],
        ['word' => '信用贷',    'category' => 'ads'],
        ['word' => '无抵押贷款','category' => 'ads'],
        ['word' => '极速放款',  'category' => 'ads'],
        ['word' => '当天放款',  'category' => 'ads'],
        ['word' => '秒下款',    'category' => 'ads'],
        ['word' => '低利息贷款','category' => 'ads'],
        ['word' => '无前期费用','category' => 'ads'],
        ['word' => '黑户可办',  'category' => 'ads'],
        ['word' => '征信不好也能贷', 'category' => 'ads'],
        ['word' => '套现',      'category' => 'ads'],
        ['word' => '信用卡套现','category' => 'ads'],
        ['word' => '花呗套现',  'category' => 'ads'],
        ['word' => '借呗套现',  'category' => 'ads'],

        
        ['word' => '色情',      'category' => 'ads'],
        ['word' => '黄网',      'category' => 'ads'],
        ['word' => '色聊',      'category' => 'ads'],
        ['word' => '裸聊',      'category' => 'ads'],
        ['word' => '视频裸聊',  'category' => 'ads'],
        ['word' => '一对一裸聊','category' => 'ads'],
        ['word' => '同城约',    'category' => 'ads'],
        ['word' => '同城约炮',  'category' => 'ads'],
        ['word' => '看片',      'category' => 'ads'],
        ['word' => '福利视频',  'category' => 'ads'],
        ['word' => '番号',      'category' => 'ads'],
        ['word' => '约炮',      'category' => 'ads'],
        ['word' => '一夜情',    'category' => 'ads'],
        ['word' => '开车群',    'category' => 'ads'],
        ['word' => '车牌号资源','category' => 'ads'],

        
        ['word' => '微信',      'category' => 'ads'],
        ['word' => 'VX',        'category' => 'ads'],
        ['word' => 'vx',        'category' => 'ads'],
        ['word' => 'v信',       'category' => 'ads'],
        ['word' => '威信',      'category' => 'ads'],
        ['word' => '加微信',    'category' => 'ads'],
        ['word' => 'V：',       'category' => 'ads'],
        ['word' => 'v：',       'category' => 'ads'],
        ['word' => 'V:',        'category' => 'ads'],
        ['word' => 'v:',        'category' => 'ads'],
        ['word' => 'Q Q',       'category' => 'ads'],
        ['word' => 'QQ',        'category' => 'ads'],
        ['word' => '扣扣',      'category' => 'ads'],
        ['word' => '企鹅',      'category' => 'ads'],
        ['word' => 'qq群',      'category' => 'ads'],
        ['word' => 'Q群',       'category' => 'ads'],
        ['word' => '加群',      'category' => 'ads'],
        ['word' => '交流群',    'category' => 'ads'],
        ['word' => '联系我',    'category' => 'ads'],
        ['word' => '联系vx',    'category' => 'ads'],
        ['word' => '联系微信',  'category' => 'ads'],
        ['word' => '联系方式',  'category' => 'ads'],
        ['word' => '联系电话',  'category' => 'ads'],
        ['word' => '手机号码',  'category' => 'ads'],
        ['word' => '手机号',    'category' => 'ads'],

        
        ['word' => 'http://',   'category' => 'ads'],
        ['word' => 'https://',  'category' => 'ads'],
        ['word' => 'www.',      'category' => 'ads'],
        ['word' => '.com',      'category' => 'ads'],
        ['word' => '.cn',       'category' => 'ads'],
        ['word' => '.net',      'category' => 'ads'],
        ['word' => '.cc',       'category' => 'ads'],
        ['word' => '.xyz',      'category' => 'ads'],
        ['word' => '.vip',      'category' => 'ads'],
        ['word' => '.top',      'category' => 'ads'],
        ['word' => '.shop',     'category' => 'ads'],
    ];

    try {
        $stmt = $pdo->prepare("
            INSERT OR IGNORE INTO sensitive_words (word, category, is_enabled, created_at)
            VALUES (:word, :category, 1, :t)
        ");
        $now = time();
        foreach ($defaults as $item) {
            $stmt->execute([
                ':word'     => $item['word'],
                ':category' => $item['category'],
                ':t'        => $now,
            ]);
        }
    } catch (Throwable $e) {
        
    }
}


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!admin_csrf_check($_POST['csrf_token'] ?? null)) {
        $errors[] = '请求已失效（CSRF 校验失败），请刷新页面后重试。';
    } else {
        $action = $_POST['action'] ?? '';
        try {
            if ($action === 'create') {
                $word     = trim((string)($_POST['word'] ?? ''));
                $category = trim((string)($_POST['category'] ?? 'other'));
                if ($word !== '') {
                    $stmt = $pdo->prepare("
                    INSERT INTO sensitive_words (word, category, is_enabled, created_at)
                    VALUES (:word, :category, 1, :t)
                ");
                    $stmt->execute([
                    ':word'     => $word,
                    ':category' => $category !== '' ? $category : 'other',
                    ':t'        => time(),
                ]);
                }
            } elseif ($action === 'toggle') {
                $id        = (int)($_POST['id'] ?? 0);
                $isEnabled = (int)($_POST['is_enabled'] ?? 0);
                if ($id > 0) {
                    $stmt = $pdo->prepare("
                    UPDATE sensitive_words
                    SET is_enabled = :en
                    WHERE id = :id
                ");
                    $stmt->execute([
                    ':en' => $isEnabled ? 1 : 0,
                    ':id' => $id,
                ]);
                }
            } elseif ($action === 'delete') {
                $id = (int)($_POST['id'] ?? 0);
                if ($id > 0) {
                    $stmt = $pdo->prepare("DELETE FROM sensitive_words WHERE id = :id");
                    $stmt->execute([':id' => $id]);
                }
            }
        } catch (Throwable $e) {
            $errors[] = '操作失败：' . $e->getMessage();
        }
    }
}


admin_seed_default_sensitive_words($pdo);


$words = [];

try {
    $stmt = $pdo->query("
        SELECT id, word, category, is_enabled, created_at
        FROM sensitive_words
        ORDER BY created_at DESC, id DESC
        LIMIT 200
    ");
    $words = $stmt->fetchAll() ?: [];
} catch (Throwable $e) {
    $words   = [];
    $errors[] = '无法读取敏感词列表（可能尚未初始化数据库）。';
}

?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>敏感词管理 - <?php echo h($siteTitle); ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        html, body { height:100%;  margin:0; padding:0; }
        body {
            margin: 0; padding:0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
            background: #020617;
            color: #e5e7eb;
        }
        a {
            color: #60a5fa;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
        .topbar {
            background: #0f172a;
            border-bottom: 1px solid #1f2937;
            padding:18px 16px 6px;
            display: flex;
            align-items:flex-start;
            justify-content: space-between;
         margin:0;  margin-top:-12px; }
        .topbar-title {
            font-size: 16px;
            font-weight: 600;
        }
        .topbar-user {
            font-size: 13px;
            color: #9ca3af;
        }
        .topbar-nav a {
            margin-left: 10px;
            font-size: 13px;
        }
        .page {
            max-width: 900px;
            margin: 0 auto;
            padding: 16px;
        }
        .card {
            background: #020617;
            border-radius: 6px;
            border: 1px solid #1f2937;
            padding: 12px 14px;
            margin-bottom: 12px;
        }
        .card-title {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 8px;
        }
        .form-row {
            display: flex;
            gap: 6px;
            margin-bottom: 6px;
        }
        .form-row input,
        .form-row select {
            padding: 4px 6px;
            border-radius: 4px;
            border: 1px solid #374151;
            background: #020617;
            color: #e5e7eb;
            font-size: 13px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
        }
        th, td {
            border-bottom: 1px solid #1f2937;
            padding: 6px 4px;
            text-align: left;
        }
        th {
            color: #9ca3af;
            font-weight: 500;
        }
        .btn {
            border-radius: 999px;
            border: 1px solid #3b82f6;
            background: #1d4ed8;
            color: #e5e7eb;
            font-size: 12px;
            padding: 4px 8px;
            cursor: pointer;
        }
        .small-text {
            font-size: 12px;
            color: #9ca3af;
            margin-top: 4px;
        }
        .errors {
            background: #7f1d1d;
            border: 1px solid #fecaca;
            color: #fee2e2;
            padding: 8px 10px;
            border-radius: 4px;
            font-size: 12px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body style="margin:0;padding:0;"><div class="topbar">
        <div class="topbar-title">敏感词管理</div>
        <div class="topbar-user">
            管理员：<?php echo h($adminUser['nickname'] ?? $adminUser['email'] ?? ''); ?>
            <span class="topbar-nav">
                <a href="index.php">仪表盘</a>
                <a href="articles.php">文章</a>
                <a href="categories.php">分类</a>
                <a href="tags.php">&#26631;&#31614;</a>
                <a href="comments.php">评论</a>
                <a href="links.php">友链</a>
                <a href="media.php">文件</a>
                <a href="users.php">会员</a>
                <a href="sensitive_words.php"><strong>敏感词</strong></a>
                <a href="stats.php">流量</a>
                <a href="security.php">防护</a>
                <a href="logs.php">日志</a>
                <a href="settings.php">设置</a>
                <a href="backup_sqlite.php">&#25968;&#25454;</a>
                <a href="../index.php">前台</a>
                <a href="../logout.php">退出</a>
            </span>
        </div>
    </div>

    <div class="page">
        <?php if ($errors): ?>
            <div class="errors">
                <?php foreach ($errors as $err): ?>
                    <div><?php echo h($err); ?></div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-title">新增敏感词</div>
            <form method="post">
                <input type="hidden" name="csrf_token" value="<?php echo h(admin_csrf_token()); ?>">
                <input type="hidden" name="action" value="create">
                <div class="form-row">
                    <input type="text" name="word" placeholder="敏感词，如：广告、某域名等" required>
                    <select name="category">
                        <option value="abuse">辱骂</option>
                        <option value="ads">广告</option>
                        <option value="sensitive">敏感</option>
                        <option value="other" selected>其他</option>
                    </select>
                </div>
                <button type="submit" class="btn">添加敏感词</button>
            </form>
            <div class="small-text">
                评论中包含这些词时，会被标记为“待审核”；当前版本已经内置了一批“严格模式”词库（骂人、广告、联系方式、带链接），
                你可以在下面列表里查看、停用或删除。
            </div>
        </div>

        <div class="card">
            <div class="card-title">敏感词列表（最多 200 条）</div>
            <?php if ($words): ?>
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>词</th>
                            <th>类型</th>
                            <th>启用</th>
                            <th>时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($words as $w): ?>
                            <tr>
                                <td><?php echo (int)$w['id']; ?></td>
                                <td><?php echo h($w['word']); ?></td>
                                <td><?php echo h($w['category']); ?></td>
                                <td><?php echo ((int)$w['is_enabled']) ? '是' : '否'; ?></td>
                                <td><?php echo date('Y-m-d H:i', (int)$w['created_at']); ?></td>
                                <td>
                                    <form method="post" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?php echo h(admin_csrf_token()); ?>">
                                        <input type="hidden" name="action" value="toggle">
                                        <input type="hidden" name="id" value="<?php echo (int)$w['id']; ?>">
                                        <input type="hidden" name="is_enabled" value="<?php echo (int)!$w['is_enabled']; ?>">
                                        <button type="submit" class="btn">
                                            <?php echo ((int)$w['is_enabled']) ? '停用' : '启用'; ?>
                                        </button>
                                    </form>
                                    <form method="post" style="display:inline;">
                                        <input type="hidden" name="csrf_token" value="<?php echo h(admin_csrf_token()); ?>">
                                        <input type="hidden" name="action" value="delete">
                                        <input type="hidden" name="id" value="<?php echo (int)$w['id']; ?>">
                                        <button type="submit" class="btn" onclick="return confirm('确定删除该敏感词？')">删除</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p class="small-text">暂无敏感词记录。</p>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
