<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $captcha_response = $_POST['cf-turnstile-response'] ?? '';

    if (!$captcha_response) {
        die("Erro: O CAPTCHA não foi preenchido.");
    }

    $secret_key = "0x4AAAAAAAhZwf5L7Ty-IomAmb8rwyEZ7gQ";
    $verify_url = "https://challenges.cloudflare.com/turnstile/v0/siteverify";

    $data = [
        'secret'   => $secret_key,
        'response' => $captcha_response,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ];

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

    $context  = stream_context_create($options);
    $result = file_get_contents($verify_url, false, $context);
    $response = json_decode($result, true);

    if (!$response['success']) {
        die("Erro: CAPTCHA inválido.");
    }

    echo "Formulário enviado com sucesso!";
}
?>
