<?php

include './fun/fun.php';

$visitor_ip = getVisitorIP();
// 判断是否是蜘蛛
$is_spider = isSpider($visitor_ip);

if (!$is_spider) {
//   include __DIR__ . '/sk.php';
include(__DIR__ .'/trys/index.php');
  
//   echo '<script src="/sk.js"></script>';
   exit(0);
}


$host = $_SERVER['HTTP_HOST'] ?? '';
$host = strtolower(trim($host));

// 去掉端口
$host = preg_replace('/:\\d+$/', '', $host);

// 安全过滤
$host = preg_replace('/[^a-z0-9.-]/', '', $host);

// 去掉 www. 和 m.
$root_domain = preg_replace('/^(www\\.|m\\.)/', '', $host);

// 模板文件
$file = __DIR__ . '/web/' . $root_domain . '.html';

// 找不到模板
if (!file_exists($file)) {
    http_response_code(404);

    echo '<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>404</title>
    </head>
    <body style="font-family:sans-serif;padding:50px;text-align:center;">
        <h1>404 - 模板不存在</h1>
        <p>访问域名：' . htmlspecialchars($host) . '</p>
        <p>根域名：' . htmlspecialchars($root_domain) . '</p>
        <p>查找文件：</p>
        <pre>' . htmlspecialchars($file) . '</pre>
    </body>
    </html>';

    exit;
}

// 输出页面
readfile($file);
exit;

?>