<?php
error_reporting(0);


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

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

if ($_GET['process'] == '') {
    # code...
    die('err');
}





// User-Agents reais e modernos (2024–2025)
$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"
];

// Seleciona User-Agent aleatoriamente
$userAgent = $userAgents[array_rand($userAgents)];


// /consultapublica/ConsultaPublica/DetalheProcessoConsultaPublica/listView.seam?ca=0203ee62bef9c95d08c8bc23435cac85ae18f5479178b8f1

function extrairCA($html) {
    $cas = [];

    // Expressão regular para capturar o parâmetro `ca` nas chamadas `openPopUp(...)`
    // $padrao = "/\/pje\/ConsultaPublica\/DetalheProcessoConsultaPublica\/listView\.seam\?ca=([a-f0-9]{40,})/";
    $padrao = "/\/consultapublica\/ConsultaPublica\/DetalheProcessoConsultaPublica\/listView\.seam\?ca=([a-f0-9]{40,})/";


    // Procura todas as ocorrências
    if (preg_match_all($padrao, $html, $matches)) {
        $cas = array_unique($matches[1]); // Remove duplicatas
    }

    return $cas;
}






$url = "https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retorna a resposta em vez de exibir
curl_setopt($curl, CURLOPT_POST, false); // Define o método POST
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // Define o método POST
curl_setopt($curl, CURLOPT_HEADER, true); // Define o método POST
curl_setopt($curl, CURLOPT_ENCODING, true); 

$headers = array(
    'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'sec-ch-ua-mobile: ?0',
    'sec-ch-ua-platform: "Windows"',
    'Upgrade-Insecure-Requests: 1',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: navigate',
    'Sec-Fetch-User: ?1',
    'Sec-Fetch-Dest: document',
    'Referer: https://pje1g.trf1.jus.br/pje/QuadroAviso/listViewQuadroAvisoMensagem.seam?cid=5794',
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__ . "/cookies.txt"); // Usa cookies salvos
$resp = curl_exec($curl);

$tk = trazer1($resp, 'input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="','"');


// $_GET['process'];

$processo =  preg_replace('/[^0-9]/', '', $_GET['process']);

if (strlen($processo) !== 20) {
    die("Número de processo inválido. Deve conter 20 dígitos.");
}

// Divide em partes
$numeroSequencial        = substr($processo, 0, 7);
$digitoVerificador       = substr($processo, 7, 2);
$ano                     = substr($processo, 9, 4);
$ramoJustica             = substr($processo, 13, 1); // n nescessario
$respectivoTribunal      = substr($processo, 14, 2); // n nescessario
$numeroOrgaoJustica      = substr($processo, 16, 4);



curl_setopt($curl, CURLOPT_URL, 'https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retorna a resposta em vez de exibir
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); // Define o método POST
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // Define o método POST
curl_setopt($curl, CURLOPT_HEADER, true); // Define o método POST
curl_setopt($curl, CURLOPT_ENCODING, true); // Define o método POST
curl_setopt($curl, CURLOPT_POSTFIELDS, "AJAXREQUEST=_viewRoot&fPP%3Aj_id149%3AnomeParte=&fPP%3Aj_id158%3AoutrosNomesAlcunha=&fPP%3Aj_id167%3AnomeAdvogado=&tipoMascaraDocumento=on&fPP%3AdpDec%3AdocumentoParte=&fPP%3AnumeroProcesso%3AnumeroSequencial=$numeroSequencial&fPP%3AnumeroProcesso%3AnumeroDigitoVerificador=$digitoVerificador&fPP%3AnumeroProcesso%3AAno=$ano&fPP%3AnumeroProcesso%3AramoJustica=$ramoJustica&fPP%3AnumeroProcesso%3ArespectivoTribunal=$respectivoTribunal&fPP%3AnumeroProcesso%3ANumeroOrgaoJustica=$numeroOrgaoJustica&fPP%3AprocessoReferenciaDecoration%3AhabilitarMascaraProcessoReferencia=true&fPP%3AprocessoReferenciaDecoration%3AIdProcessoReferenciaComMascaraDecoration%3AIdProcessoReferenciaComMascara=&fPP%3Aj_id247%3Aassunto=&fPP%3Aj_id256%3AclasseJudicial=&fPP%3Aj_id265%3AnumeroDocumento=&fPP%3AdecorationDados%3AnumeroOAB=&fPP%3AdecorationDados%3AletraOAB=&fPP%3AdecorationDados%3AufOABCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AjurisdicaoComboDecoration%3AjurisdicaoCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AorgaoJulgadorComboDecoration%3AorgaoJulgadorCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoInicioInputDate=&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoInicioInputCurrentDate=07%2F2025&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoFimInputDate=&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoFimInputCurrentDate=07%2F2025&fPP%3AvalorDaCausaDecoration%3AvalorCausaInicial=&fPP%3AvalorDaCausaDecoration%3AvalorCausaFinal=&fPP%3Aj_id406%3AmovimentacaoProcessualSuggest=&fPP%3Aj_id406%3Aj_id417_selection=&fPP%3Aj_id423%3Aj_id428=&fPP%3Aj_id423%3Aj_id430%3AorgaoOrigemCriminal=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3Aj_id423%3AnumeroProcedCriminal%3AnumeroProcedCriminal=&fPP%3Aj_id423%3AnumeroProcedCriminal%3AanoProcedCriminal=&fPP%3Aj_id423%3AnumeroProtocoloPolicia%3AnumeroProtocoloPolicia=&fPP=fPP&autoScroll=&javax.faces.ViewState=$tk&fPP%3Aj_id465=fPP%3Aj_id465&AJAX%3AEVENTS_COUNT=1&"); // Envia os dados do formulário
curl_setopt($curl, CURLOPT_HTTPHEADER, [
   'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    // 'Content-Length: 1940',
    'sec-ch-ua-platform: "Windows"',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'sec-ch-ua-mobile: ?0',
    'Accept: */*',
    'Origin: https://pje1g.trf1.jus.br',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Referer: https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam',

]);

// Configura para armazenar cookies e manter a sessão

curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__ . "/cookies.txt"); // Usa cookies salvos
$html = curl_exec($curl);

$viwwww = trazer($html, 'name="javax.faces.ViewState" id="javax.faces.ViewState" value="','"');
$babhut = trazer($html,"idProcessoSelecionado':",",");





$curl = curl_init('https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam');

curl_setopt($curl, CURLOPT_URL, 'https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retorna a resposta em vez de exibir
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); // Define o método POST
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // Define o método POST
curl_setopt($curl, CURLOPT_HEADER, true); // Define o método POST
curl_setopt($curl, CURLOPT_ENCODING, true); // Define o método POST
curl_setopt($curl, CURLOPT_POSTFIELDS, "AJAXREQUEST=_viewRoot&fPP%3Aj_id149%3AnomeParte=&fPP%3Aj_id158%3AoutrosNomesAlcunha=&fPP%3Aj_id167%3AnomeAdvogado=&tipoMascaraDocumento=on&fPP%3AdpDec%3AdocumentoParte=&fPP%3AnumeroProcesso%3AnumeroSequencial=$numeroSequencial&fPP%3AnumeroProcesso%3AnumeroDigitoVerificador=$digitoVerificador&fPP%3AnumeroProcesso%3AAno=$ano&fPP%3AnumeroProcesso%3AramoJustica=$ramoJustica&fPP%3AnumeroProcesso%3ArespectivoTribunal=$respectivoTribunal&fPP%3AnumeroProcesso%3ANumeroOrgaoJustica=$numeroOrgaoJustica&fPP%3AprocessoReferenciaDecoration%3AhabilitarMascaraProcessoReferencia=true&fPP%3AprocessoReferenciaDecoration%3AIdProcessoReferenciaComMascaraDecoration%3AIdProcessoReferenciaComMascara=&fPP%3Aj_id247%3Aassunto=&fPP%3Aj_id256%3AclasseJudicial=&fPP%3Aj_id265%3AnumeroDocumento=&fPP%3AdecorationDados%3AnumeroOAB=&fPP%3AdecorationDados%3AletraOAB=&fPP%3AdecorationDados%3AufOABCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AjurisdicaoComboDecoration%3AjurisdicaoCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AorgaoJulgadorComboDecoration%3AorgaoJulgadorCombo=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoInicioInputDate=&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoInicioInputCurrentDate=07%2F2025&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoFimInputDate=&fPP%3AdataAutuacaoDecoration%3AdataAutuacaoFimInputCurrentDate=07%2F2025&fPP%3AvalorDaCausaDecoration%3AvalorCausaInicial=&fPP%3AvalorDaCausaDecoration%3AvalorCausaFinal=&fPP%3Aj_id406%3AmovimentacaoProcessualSuggest=&fPP%3Aj_id406%3Aj_id417_selection=&fPP%3Aj_id423%3Aj_id428=&fPP%3Aj_id423%3Aj_id430%3AorgaoOrigemCriminal=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&fPP%3Aj_id423%3AnumeroProcedCriminal%3AnumeroProcedCriminal=&fPP%3Aj_id423%3AnumeroProcedCriminal%3AanoProcedCriminal=&fPP%3Aj_id423%3AnumeroProtocoloPolicia%3AnumeroProtocoloPolicia=&fPP=fPP&autoScroll=&javax.faces.ViewState=$viwwww&fPP%3AprocessosTable%3A$babhut%3Aj_id481=fPP%3AprocessosTable%3A$babhut%3Aj_id481&idProcessoSelecionado=$babhut&ajaxSingle=fPP%3AprocessosTable%3A$babhut%3Aj_id481&AJAX%3AEVENTS_COUNT=1&"); // Envia os dados do formulário
curl_setopt($curl, CURLOPT_HTTPHEADER, [
   'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    // 'Content-Length: 2080',
    'sec-ch-ua-platform: "Windows"',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'sec-ch-ua-mobile: ?0',
    'Accept: */*',
    'Origin: https://pje1g.trf1.jus.br',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Referer: https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam',

]);

// Configura para armazenar cookies e manter a sessão

curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__ . "/cookies.txt"); // Usa cookies salvos
$html = curl_exec($curl);





$ggrttt = trazer($html, '"&ca=','"');
$grr_ID = trazer($html, 'seam?id=','"');


// $curl = curl_init('https://pje.tjmg.jus.br/pje/Processo/ConsultaProcesso/Detalhe/listProcessoCompletoAdvogado.seam?id='.trim($babhut).'&ca='.trim($ggrttt).'&aba=');
// 

$curl = curl_init('https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/Detalhe/listProcessoCompletoAdvogado.seam?id='.trim($grr_ID).'&ca='.trim($ggrttt).'&aba=');
curl_setopt($curl, CURLOPT_URL, 'https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/Detalhe/listProcessoCompletoAdvogado.seam?id='.trim($grr_ID).'&ca='.trim($ggrttt).'&aba=');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retorna a resposta em vez de exibir
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // Define o método POST
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // Define o método POST
curl_setopt($curl, CURLOPT_HEADER, true); // Define o método POST
curl_setopt($curl, CURLOPT_ENCODING, true); // Define o método POST
curl_setopt($curl, CURLOPT_HTTPHEADER, [
   'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'sec-ch-ua-mobile: ?0',
    'sec-ch-ua-platform: "Windows"',
    'Upgrade-Insecure-Requests: 1',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: navigate',
    'Sec-Fetch-User: ?1',
    'Sec-Fetch-Dest: document',
    'Referer: https://pje1g.trf1.jus.br/pje/Processo/ConsultaProcesso/listView.seam',

]);

// Configura para armazenar cookies e manter a sessão

curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__ . "/cookies.txt"); // Usa cookies salvos
 $resp = curl_exec($curl);
$cashh = trazer($resp, '<dt>Valor da causa</dt>','</d').'"';
echo $cashh1 = trazer($cashh, '<dd>','"');