<?php
// ================= 1. 核心配置 =================
$main_domain = 'wpsbk.com.cn';
$record_file = 'domains.txt'; // 自动生成，无需手动创建
$current_host = $_SERVER['HTTP_HOST'];

// ================= 2. 如果是抓取 Sitemap，直接输出 XML =================
if (strpos($_SERVER['REQUEST_URI'], 'sitemap.xml') !== false || strpos($_SERVER['REQUEST_URI'], 'sitemap.php') !== false) {
    header("Content-Type: application/xml; charset=utf-8");
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    echo "<url><loc>https://{$main_domain}/</loc></url>";
    
    if (file_exists($record_file)) {
        $domains = file($record_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach (array_unique($domains) as $sub) {
            echo "<url><loc>https://{$sub}/</loc><lastmod>".date('Y-m-d')."</lastmod></url>";
        }
    }
    echo '</urlset>';
    exit; // 输出完地图直接停止，不往下执行
}

// ================= 3. 如果是访问泛域名，静默记录并展示页面 =================
// 排除主域名和带 www 的域名
if ($current_host !== $main_domain && $current_host !== 'www.'.$main_domain) {
    if (file_exists($record_file)) {
        $domains = file($record_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    } else {
        $domains = [];
    }
    // 如果没有记录过，就追加进去
    if (!in_array($current_host, $domains)) {
        file_put_contents($record_file, $current_host . "\n", FILE_APPEND | LOCK_EX);
    }
}

// ================= 4. 正常输出原本的网页内容 =================
// 直接把原本的 index.html 吐出来给用户和蜘蛛看
if (file_exists('index.html')) {
    echo file_get_contents('index.html');
} else {
    echo "Welcome to " . $current_host;
}
?>