<?php

header('Content-Type: application/json');

function recaptcha_bypass() {
    $site_key = "6LdSC88qAAAAAA8gafnzkux0W-HSwRuVGEj0OeAk";
    $base_url = "https://www.google.com/recaptcha/api2/";
    $anchor_url = $base_url . "anchor?ar=1&k=" . $site_key . "&co=aHR0cHM6Ly9jaGVja291dC5zdG9yeXRlbC5jb206NDQz&hl=tr&v=-PgDGgfmUF0ySmnjMTJjzqak&size=invisible&badge=inline&anchor-ms=20000&execute-ms=15000&cb=wo2agc3o04ed";
    $reload_url = $base_url . "reload?k=" . $site_key;

    $headers = [
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
    ];

    // Anchor isteği
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $anchor_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec($ch);
    $effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);

    // Token çıkar
    if (!preg_match('/value="([^"]+)"/', $response, $token_match)) {
        throw new Exception("Anchor token bulunamadı!");
    }
    $token = $token_match[1];

    // URL parametrelerini parse et
    $parsed_url = parse_url($anchor_url);
    parse_str($parsed_url['query'], $params);

    // POST data hazırla
    $data = [
        'v' => $params['v'],
        'reason' => 'q',
        'c' => $token,
        'k' => $params['k'],
        'co' => $params['co'],
        'hl' => 'tr',
        'size' => 'invisible'
    ];

    // Reload isteği
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $reload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, [
        "Referer: " . $effective_url,
        "Content-Type: application/x-www-form-urlencoded"
    ]));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $reload_response = curl_exec($ch);
    curl_close($ch);

    // reCAPTCHA token çıkar
    if (!preg_match('/\["rresp","([^"]+)"/', $reload_response, $rresp_match)) {
        throw new Exception("reCAPTCHA yanıtı parse edilemedi!");
    }

    $recaptcha_token = $rresp_match[1];
    return $recaptcha_token;
}

try {
    $token = recaptcha_bypass();
    echo json_encode([
        "status" => "success",
        "recaptcha_token" => $token
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => $e->getMessage()
    ]);
}
?>
