<?php

function trazer1($str, $start, $end) {
    $parts = explode($start, $str);
    if (isset($parts[1])) {
        $subParts = explode($end, $parts[1]);
        return $subParts[0];
    }
}

function extrairCA($html) {
    $cas = [];
    $padrao = "/\/consultapublica\/ConsultaPublica\/DetalheProcessoConsultaPublica\/listView\.seam\?ca=([a-f0-9]{40,})/";
    if (preg_match_all($padrao, $html, $matches)) {
        $cas = array_unique($matches[1]);
    }
    return $cas;
}

$userAgents = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.6367.208 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.78 Safari/537.36"
];
$userAgent = $userAgents[array_rand($userAgents)];

$url = "https://pje1g.trf1.jus.br/consultapublica/ConsultaPublica/listView.seam";
$curl = curl_init($url);
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_COOKIEJAR => __DIR__ . "/cookies.txt",
    CURLOPT_USERAGENT => $userAgent,
]);
$resp = curl_exec($curl);
$tk = trazer1($resp, 'input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="','"');

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_HEADER => true,
    CURLOPT_ENCODING => true,
    CURLOPT_COOKIEFILE => __DIR__ . "/cookies.txt",
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POSTFIELDS => "AJAXREQUEST=_viewRoot&fPP%3AnumProcesso-inputNumeroProcessoDecoration%3AnumProcesso-inputNumeroProcesso=&mascaraProcessoReferenciaRadio=on&fPP%3Aj_id152%3AprocessoReferenciaInput=&fPP%3Adnp%3AnomeParte=&fPP%3Aj_id170%3AnomeAdv=&fPP%3Aj_id179%3AclasseProcessualProcessoHidden=&tipoMascaraDocumento=on&fPP%3AdpDec%3AdocumentoParte=&fPP%3ADecoration%3AnumeroOAB=7777&fPP%3ADecoration%3Aj_id214=&fPP%3ADecoration%3AestadoComboOAB=6&fPP=fPP&autoScroll=&javax.faces.ViewState=$tk&fPP%3Aj_id220=fPP%3Aj_id220&AJAX%3AEVENTS_COUNT=1&",
    CURLOPT_HTTPHEADER => [
        'Host: pje1g.trf1.jus.br',
        'Connection: keep-alive',
        'User-Agent: ' . $userAgent,
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'Accept: */*',
        'Origin: https://pje1g.trf1.jus.br',
        'Referer: https://pje1g.trf1.jus.br/consultapublica/ConsultaPublica/listView.seam'
    ]
]);

$html = curl_exec($curl);
curl_close($curl);

$listaCA = extrairCA($html);

// foreach ($listaCA as $ca) {
//     echo "CA encontrado: $ca\n";
// }

// exit;
$total = count($listaCA);
$sucesso = 0;
$falha = 0;

echo "🔍 Total de CAs encontrados: $total\n\n";

foreach ($listaCA as $ca) {
    $resultado = consultarCA($ca);
    if ($resultado === true) {
        echo "[✔️] CA $ca retornou TRUE\n";
        $sucesso++;
    } elseif ($resultado === false) {
        echo "[❌] CA $ca retornou FALSE\n";
        $falha++;
    } else {
        echo "[⚠️] CA $ca retornou RESPOSTA INVÁLIDA: $resultado\n";
    }
    sleep(1); // Evita sobrecarregar o servidor com muitas requisições rápidas
}

// 🎯 Relatório final
echo "\n=========================\n";
echo "✅ Sucesso: $sucesso\n";
echo "❌ Falha: $falha\n";
echo "🎯 Total Processado: " . ($sucesso + $falha) . "\n";
echo "=========================\n";

// Função de consulta CA na s.php
function consultarCA($ca) {
    $url = "https://bot1.botworkapifullinfo.shop/STJ_V3_TRD/app1.php?ca=" . urlencode($ca); // substitua com a URL real
    $resposta = @file_get_contents($url);
    if ($resposta === false) {
        return null;
    }

    $resposta = trim($resposta);
    if ($resposta === "true") return true;
    if ($resposta === "false") return false;
    return $resposta; // resposta inesperada
}



