<?php

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

function recaptcha_bypass() {
    $site_key = "6Le9aHkUAAAAAKvbtMfUhgIgZ1jyyA9hyeIhlcn0";
    $base_url = "https://www.google.com/recaptcha/api2/";
    
    // Güncel v parametresi
    $v_param = "g8_L4xXxRhU";  // Güncel reCAPTCHA versiyonu
    
    $anchor_url = $base_url . "anchor?ar=1&k=" . $site_key . "&co=aHR0cHM6Ly93d3cuZW51eWd1bi5jb206NDQz&hl=tr&v=" . $v_param . "&size=invisible&cb=".rand(100000000, 999999999);
    $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/131.0.0.0 Safari/537.36",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7"
    ];

    // 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);

    // Debug: Anchor yanıtını logla
    file_put_contents(__DIR__ . '/recpapi5_debug.txt', "ANCHOR RESPONSE (ilk 1000 karakter):\n" . substr($response, 0, 1000) . "\n\n", FILE_APPEND);

    // Token çıkar - Farklı pattern'leri dene
    if (preg_match('/value="([^"]+)"/', $response, $token_match)) {
        $token = $token_match[1];
    } elseif (preg_match('/id="recaptcha-token"\s+value="([^"]+)"/', $response, $token_match)) {
        $token = $token_match[1];
    } elseif (preg_match('/<textarea[^>]*id="[^"]*token[^"]*"[^>]*>([^<]+)</', $response, $token_match)) {
        $token = $token_match[1];
    } else {
        file_put_contents(__DIR__ . '/recpapi5_debug.txt', "ANCHOR TOKEN BULUNAMADI\n\n", FILE_APPEND);
        throw new Exception("Anchor token bulunamadı!");
    }

    // 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',
        'chr' => '[89,64,27]',
        'vh' => rand(100000000, 999999999),
        'bg' => '!q62grYxHRvVxjUIjSFNd0mlvrZ-iCgKzi_8xAG2A0FEiBCbCH4T0RmLEzW_eXxLy8iFV35wMPPi-b2VPtHaLq-SZAFg91NM8waFXvwSXdpDmKiWF9N5P-Y0nV0YVAYFe0U6HwEyg9pWKZX9DiJB5Qb0mPXUVDMqbINMOXqA0fpCKCaXaFXXYPvFCfaWKXKTCxMKR_vHSRBEcKqd0EB_DqWLxYm9XMfkbqSPQMYGaHZ5D7c0fVQXN9R0lPHaBLBmKMHqp_z2GNAhxeWFKuZFSyD6J3nPQnYl5C0t0FBMVSA0aMXnBJVb0nGYyDGBEZLtFQ8TjGdgG8M15qHXB7JFKmkDqbSBiBcqM7I_YXHQl7'
    ];

    // 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);

    // Debug: Yanıtı dosyaya kaydet
    file_put_contents(__DIR__ . '/recpapi5_debug.txt', "RELOAD RESPONSE:\n" . $reload_response . "\n\n", FILE_APPEND);

    // reCAPTCHA token çıkar - Farklı pattern'leri dene
    if (preg_match('/\["rresp","([^"]+)"/', $reload_response, $rresp_match)) {
        $recaptcha_token = $rresp_match[1];
    } elseif (preg_match('/rresp["\']?\s*[:=]\s*["\']([^"\']+)/', $reload_response, $rresp_match)) {
        $recaptcha_token = $rresp_match[1];
    } elseif (preg_match('/"uvresp","([^"]+)"/', $reload_response, $rresp_match)) {
        $recaptcha_token = $rresp_match[1];
    } else {
        // Debug: Pattern bulunamazsa yanıtın başını logla
        file_put_contents(__DIR__ . '/recpapi5_debug.txt', "PATTERN BULUNAMADI - İlk 500 karakter:\n" . substr($reload_response, 0, 500) . "\n\n", FILE_APPEND);
        throw new Exception("reCAPTCHA yanıtı parse edilemedi!");
    }

    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()
    ]);
}
?>
