<?php

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

function recaptcha_bypass() {
    $site_key = "6LdYfNgeAAAAAPQ4vOgHA1-iLTPcma5gjb6xR0Rv";
    $base_url = "https://www.google.com/recaptcha/api2/";
    
    // Gerçek treasurehouseofmakeup anchor parametreleri
    $co = "aHR0cHM6Ly93d3cudHJlYXN1cmVob3VzZW9mbWFrZXVwLmNvLnVrOjQ0Mw..";
    $v = "-PgDGgfmUF0ySmnjMTJjzqak";
    $cb = "lcym" . rand(1000, 9999) . "18xr";
    
    // Step 1: Anchor request - c token al (invisible modda)
    $anchor_url = $base_url . "anchor?ar=1&k={$site_key}&co={$co}&hl=tr&v={$v}&size=invisible&cb={$cb}";
    
    $headers = [
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
        "Pragma: no-cache",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: tr-TR,tr;q=0.9"
    ];

    // 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);
    $anchor_response = curl_exec($ch);
    curl_close($ch);

    // DEBUG
    file_put_contents(__DIR__ . '/anchor_debug.txt', $anchor_response);

    // C token (anchor token) çıkar - idefix pattern
    if (preg_match('/<input type="hidden" id="recaptcha-token" value="([^"]+)"/', $anchor_response, $token_match)) {
        $c_token = $token_match[1];
    } elseif (preg_match('/id="recaptcha-token" value="([^"]+)"/', $anchor_response, $token_match)) {
        $c_token = $token_match[1];
    } elseif (preg_match('/value="([^"]{100,})"/', $anchor_response, $token_match)) {
        $c_token = $token_match[1];
    } else {
        // Tüm input value'ları bul
        preg_match_all('/value="([^"]+)"/', $anchor_response, $all_matches);
        throw new Exception("Anchor token bulunamadı! Debug: anchor_debug.txt - Values found: " . count($all_matches[1]));
    }

    // Step 2: Reload request - final token al
    $reload_url = $base_url . "reload?k={$site_key}";
    
    // Reload data - v parametresini de ekle
    $post_data = "v={$v}&reason=q&c={$c_token}&k={$site_key}&co={$co}&hl=tr&size=invisible&chr=%5B89%2C64%2C27%5D&vh=13599012192&bg=!q62grYxHRvVxjUIjSMMCLf6-PodnbDe93QI6H6xtD_7777777fSSfSfS";

    $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, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, [
        '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);

    // Final token parse et: ["rresp","TOKEN",...]
    if (preg_match('/\["rresp","([^"]+)"/', $reload_response, $rresp_match)) {
        return $rresp_match[1];
    }
    
    throw new Exception("Final token bulunamadı! Reload response: " . substr($reload_response, 0, 200));
}

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