<?php
// ============================================================
//  🛡️ ANTI-BOT CONTROLLER - النظام المتكامل
// ============================================================

require_once __DIR__ . '/ip_cache.php';

// ===== القائمة البيضاء =====
$whitelist_ips = ['YOUR_IP_HERE', '127.0.0.1', '::1'];
if (in_array($_SESSION['ip_info']['ip'], $whitelist_ips)) {
    // تمرير الزائر مباشرة بدون حظر
    return;
}

// ===== التحقق من البوت =====
$ip_info = $_SESSION['ip_info'];
$is_bot = $ip_info['is_bot'] ?? false;
$bot_reason = $ip_info['reason'] ?? 'unknown';

// ===== إذا كان بوتاً =====
if ($is_bot) {
    // تسجيل المحاولة
    $log = date('Y-m-d H:i:s') . " | BOT BLOCKED | IP: {$ip_info['ip']} | Reason: $bot_reason | UA: {$_SERVER['HTTP_USER_AGENT']}\n";
    file_put_contents(__DIR__ . '/bots_blocked.log', $log, FILE_APPEND);
    
    // إعادة التوجيه إلى رابط الخروج
    $exit_link = "https://mediapart.fr";
    header("Location: $exit_link");
    exit;
}

// ===== إذا لم يكن بوتاً =====
// تسجيل الزيارة
$log = date('Y-m-d H:i:s') . " | REAL VISITOR | IP: {$ip_info['ip']} | Country: {$ip_info['country']} | Reason: $bot_reason\n";
file_put_contents(__DIR__ . '/visitors.log', $log, FILE_APPEND);

// تمرير الزائر إلى الصفحة التالية
return;
?>