<?php
/**
 * =====================================================
 * PPQCMS 主入口 (安全防屏蔽防绕过版)
 * - 防止 ?verify_slide=1 手动绕过
 * - 验证签名 Cookie（timestamp|HMAC）
 * - 兼容 PHP 7.4
 * =====================================================
 */

/*** 部署指纹：覆盖上传后自动清空模板编译缓存，无需手动清理 ***/
$deploy_sig = __DIR__ . '/cache/.deploy';
$deploy_now = 'ppqsql_v1_translatejs_20260902a';
if (!file_exists($deploy_sig) || trim(@file_get_contents($deploy_sig)) !== $deploy_now) {
    if (is_dir(__DIR__ . '/cache')) {
        $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/cache', FilesystemIterator::SKIP_DOTS));
        foreach ($rii as $f) {
            if ($f->isFile()) @unlink($f->getPathname());
        }
    }
    @file_put_contents($deploy_sig, $deploy_now);
}

/*** 版权完整性校验 (盗版检测+水印+停用核心) ***/
define('PPQ_ROOT', __DIR__ . '/');
// 若校验文件本身被删除/改名, 直接判定盗版并启用水印
if (!file_exists(__DIR__ . '/Php/Public/Integrity.php')) {
    $GLOBALS['PPQ_PIRACY'] = true;
    if (!defined('PPQ_OB_STARTED')) { define('PPQ_OB_STARTED', true); ob_start(function($b){
        $wm = "\n" . '<div id="ppq-piracy-warn" style="position:fixed;left:0;right:0;bottom:0;z-index:99999;background:#111;color:#ff5252;font-size:14px;line-height:1.6;padding:10px 14px;text-align:center;font-family:sans-serif;border-top:2px solid #ff5252;">⚠ 此为盗版/未授权副本 · 请勿使用以防受骗 · 请联系 啪啪雀 <a href="https://papaq.xyz" target="_blank" style="color:#4fc3f7;text-decoration:underline;">papaq.xyz</a> 举报获取正版</div>' . "\n";
        if (stripos($b,'</body>')!==false) return str_ireplace('</body>',$wm.'</body>',$b);
        return $b.$wm;
    }); }
} else {
    include('./Php/Public/Integrity.php');
}

/*** 基础模块加载 ***/
include('./Php/Public/Mysql.php');
include('./Php/Public/Page.php');
include('./Php/Public/Helper.php');
include('./Php/Home/PPQSQL.php');

/*** 读取系统配置 ***/
$AdminBasic = json_decode(file_get_contents("./PPQSQL/Admin/Basic/AdminBasic.php"), true);
$AntiBlockOpen = isset($AdminBasic['AntiBlockOpen']) ? $AdminBasic['AntiBlockOpen'] : '0';

/*** 安全密钥（必须与 anti_block_verify.php 一致） ***/
$secretKey = 'ppqcms_secure_key_2025';

/*** PHP5.6 兼容函数 ***/
if (!function_exists('hash_equals')) {
    function hash_equals($a, $b) {
        if (!is_string($a) || !is_string($b)) return false;
        $len = strlen($a);
        if ($len !== strlen($b)) return false;
        $res = $a ^ $b;
        $ret = 0;
        for ($i = $len - 1; $i >= 0; $i--) $ret |= ord($res[$i]);
        return !$ret;
    }
}

/*** 验证签名 Cookie ***/
function verifyAntiBlockCookie($cookie, $secretKey) {
    if (empty($cookie)) return false;
    $parts = explode('|', $cookie);
    if (count($parts) !== 2) return false;

    $ts = $parts[0];
    $hash = $parts[1];

    // 时间格式验证
    if (!ctype_digit($ts)) return false;

    // 超过 24 小时失效
    if (time() - (int)$ts > 86400) return false;

    // 签名验证
    $expect = hash_hmac('sha256', $ts, $secretKey);
    return hash_equals($expect, $hash);
}

/*** 过滤可疑 UA ***/
function allowHumanOrSpider($ua) {
    if (empty($ua)) return false;
    $ua = strtolower($ua);
    $allow = array('chrome','safari','firefox','edge','opera','micromessenger','ucbrowser','baiduboxapp','mqqbrowser','alipay','quark');
    foreach ($allow as $a) if (strpos($ua,$a)!==false) return true;
    $bots = array('googlebot','baiduspider','bingbot','bytespider','sogou','petalbot','yandex','duckduckbot');
    foreach ($bots as $b) if (strpos($ua,$b)!==false) return true;
    $deny = array('curl','python','wget','aiohttp','requests','okhttp','gptbot','claudebot','anthropic','postman','datadog','powershell');
    foreach ($deny as $d) if (strpos($ua,$d)!==false) return false;
    return true;
}

/*** 主逻辑入口 ***/
if ($AntiBlockOpen == '1') {
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

    // 拒绝异常爬虫
    if (!allowHumanOrSpider($ua)) {
        header('HTTP/1.1 403 Forbidden');
        exit('Access Denied.');
    }

    // 检查签名 cookie
    $cookie = isset($_COOKIE['anti_block_verified']) ? $_COOKIE['anti_block_verified'] : '';
    $isValid = verifyAntiBlockCookie($cookie, $secretKey);

    // 不合法则进入验证页
    if (!$isValid) {
        include('./Php/Home/anti_block_verify.php');
        exit;
    }
}

/*** =================== 加载核心模块 =================== ***/
include('./Php/Home/GET.php');
include('./Php/Home/PCorwap.php');
include('./Php/Home/mysql.php');
include('./Php/Home/IeUrl.php');
include('./Php/Home/Ad.php');
include('./Php/Home/Host.php');
include('./Php/Home/index.php');
?>
