SHARE
TWEET

Untitled

a guest Nov 16th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Divida_ativa {
  4.  
  5.     private function get_botstation_captcha_response($task_id) {
  6.  
  7.     $curl = curl_init();
  8.  
  9.     curl_setopt_array($curl, array(
  10.         CURLOPT_URL => "http://local.botstation.com/api/task?auth_token=ABC123&service=recaptcha_v2&task_id=$task_id",
  11.         CURLOPT_RETURNTRANSFER => true,
  12.         CURLOPT_ENCODING => "",
  13.         CURLOPT_MAXREDIRS => 10,
  14.         CURLOPT_TIMEOUT => 30,
  15.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  16.         CURLOPT_CUSTOMREQUEST => "GET",
  17.     ));
  18.  
  19.     $response = curl_exec($curl);
  20.     $err = curl_error($curl);
  21.  
  22.     curl_close($curl);
  23.  
  24.     if ($err) {
  25.         return false;
  26.     } else {
  27.         return json_decode($response);
  28.     }
  29.     }
  30.  
  31.     private function create_botstation_captcha($site_key, $url) {
  32.     $curl = curl_init();
  33.  
  34.     curl_setopt_array($curl, array(
  35.         CURLOPT_URL => "http://local.botstation.com/api/task",
  36.         CURLOPT_RETURNTRANSFER => true,
  37.         CURLOPT_ENCODING => "",
  38.         CURLOPT_MAXREDIRS => 10,
  39.         CURLOPT_TIMEOUT => 30,
  40.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  41.         CURLOPT_CUSTOMREQUEST => "POST",
  42. //      CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"auth_token\"\r\n\r\nABC123\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"service\"\r\n\r\nrecaptcha_v2\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"siteKey\"\r\n\r\n$site_key\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"url\"\r\n\r\n$url\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  43.         CURLOPT_POSTFIELDS => http_build_query(["auth_token" => "ABC123", "service" => "recaptcha_v2", "siteKey" => $site_key, "url" => $url]),
  44.     ));
  45.  
  46.     $response = curl_exec($curl);
  47.     $err = curl_error($curl);
  48.  
  49.     curl_close($curl);
  50.  
  51.     if ($err) {
  52.         return false;
  53.     } else {
  54.         return json_decode($response);
  55.     }
  56.     }
  57.  
  58.     public function solve_captcha($site_key, $url, $tries = 0) {
  59. //  tenta no AZ primerio, se nao conseguir vai p o botstation
  60.     if ($task = $this->create_botstation_captcha($site_key, $url)) {
  61.         if ($task->status && $task->result->task_id) {
  62.         $response = $this->get_botstation_captcha_response($task->result->task_id);
  63.         $timer = 0;
  64.         while (in_array($response->result->status, ["QUEUED", "RUNNING"])) {
  65.             sleep(1);
  66.             $timer ++;
  67.             $response = $this->get_botstation_captcha_response($task->result->task_id);
  68.         }
  69.         if (!empty($response->result->output->token)) {
  70.             return $response->result->output->token;
  71.         } else {
  72.             if ($tries <= 3) {
  73.             $tries++;
  74.             return $this->solve_captcha($site_key, $url, $tries);
  75.             } else {
  76.             return false;
  77.             }
  78.         }
  79.         }
  80.     }
  81.     return false;
  82.     }
  83.  
  84.     private function parse_rows($html) {
  85.     $debitos = [];
  86.     $total = 0;
  87.     $doc = new DOMDocument();
  88.     $doc->loadHTML($html);
  89.  
  90.     $tables = $doc->getElementsByTagName('table');
  91.     if (!empty($tables)) {
  92.         foreach ($tables as $key => $tmp) {
  93.         if ($tmp->getAttribute("class") != "alternate") {
  94.             continue;
  95.         }
  96.         $table = $tmp;
  97.         }
  98.     }
  99.     $rows = $table->getElementsByTagName("tr");
  100.     foreach ($rows as $row) {
  101.         if (!preg_match("/[0-9\.\,]/", $row->textContent))
  102.         continue;
  103.  
  104.         $valor = preg_replace("/\,/", ".", preg_replace("/[^0-9\,]/", "", trim($row->textContent)));
  105.         $debitos[] = $valor;
  106.         $total += $valor;
  107.     }
  108.  
  109.     return ['total' => $total, 'debitos' => $debitos];
  110.     }
  111.  
  112.     public function consulta($renavam, $tries = 0) {
  113.     try {
  114.         print_r("consulta renavam: {$renavam} started\n");
  115.         $captcha_token = $this->solve_captcha('6LeeDDoUAAAAAL7awoPJgSMuiF6AuJW5rf0zqEfy', 'https://www.dividaativa.pge.sp.gov.br');
  116.         if ($captcha_token) {
  117.         print_r("captcha solved\n");
  118.         $start_time = time();
  119.  
  120.         $options = array(
  121.             CURLOPT_RETURNTRANSFER => true, // return web page
  122.             CURLOPT_HEADER => false, // don't return headers
  123.             CURLOPT_FOLLOWLOCATION => true, // follow redirects
  124.             CURLOPT_USERAGENT => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36", // who am i
  125.             CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
  126.         );
  127. //
  128.         /**  JSESSIONID */
  129.         $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/");
  130.         curl_setopt_array($handle, $options);
  131.         curl_setopt($handle, CURLOPT_HEADER, true);
  132.         $response = curl_exec($handle);
  133.         curl_close($handle);
  134.         $m = [];
  135.         preg_match("/Set\-Cookie\:\sJSESSIONID=(?P<JSESSIONID>.*)\;/", $response, $m);
  136.         $cookie = "Cookie: JSESSIONID=" . $m['JSESSIONID'] . "; path=/da-ic-web;";
  137. //  print_r($cookie);
  138.  
  139.  
  140.         $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/consultaDebitosComunicadoCadinVerificaCaptcha.do");
  141.         curl_setopt_array($handle, $options);
  142.         curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
  143.         curl_setopt($handle, CURLOPT_POST, 1);
  144.         curl_setopt($handle, CURLOPT_POSTFIELDS, "tipoDocumento=7&g-recaptcha-response={$captcha_token}&valorDocumento={$renavam}&tipoConsulta=termoAceite&operation=buscar");
  145.         $response = curl_exec($handle);
  146.         curl_close($handle);
  147.  
  148.         print_r("html OK\n");
  149.  
  150.         $doc = new DOMDocument();
  151.         @$doc->loadHTML($response);
  152.  
  153.         $debitos = [];
  154.         $debitos_return = [];
  155.         $total = 0;
  156.         if ($doc->getElementById('idTipoDebitoCliente')) {
  157.             $tmp = $this->parse_rows($response);
  158.             $debitos_return = $tmp['debitos'];
  159.             $total = $tmp['total'];
  160.         } else {
  161.             preg_match_all("/preencheHidden\(\'idDebito\'\,\s\'([0-9]*)\'\,\s\'idPessoa\'\,\s\'([0-9]*)\'\)/", $response, $tmp);
  162.             $debitos = $tmp[1];
  163.             $ids = $tmp[2];
  164.         }
  165.  
  166.         if (!empty($debitos)) {
  167.             foreach ($debitos as $key => $debito) {
  168.  
  169.             $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/escolherDevedorDebito.do");
  170.             curl_setopt_array($handle, $options);
  171.  
  172.             curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
  173.             curl_setopt($handle, CURLOPT_POST, 1);
  174.             curl_setopt($handle, CURLOPT_POSTFIELDS, "idDebito={$debito}&idPessoa={$ids[$key]}&operation=selecionar&devedor=1");
  175.             $response = curl_exec($handle);
  176.             curl_close($handle);
  177.  
  178.             $tmp = $this->parse_rows($response);
  179.             $debitos_return = array_merge($debitos_return, $tmp['debitos']);
  180.             $total += $tmp['total'];
  181.             }
  182.         }
  183.         print_r("executed in " . (time() - $start_time ) . " seconds\n");
  184.         return ["success" => true, "debitos" => $debitos_return, "total" => $total];
  185.         } else {
  186.         return ["success" => false, "msg" => "Captcha Error!"];
  187.         }
  188.     } catch (Exception $ex) {
  189.         if ($tries <= 3) {
  190.         $tries++;
  191.         return $this->consulta($renavam, $tries);
  192.         } else {
  193.         return ["success" => false, "msg" => "Órgão fora de operação!"];
  194.         }
  195.     }
  196.     }
  197.  
  198. }
  199.  
  200. $da = new Divida_ativa();
  201. //print_r($da->consulta('123123123'));
  202. print_r($da->consulta('945509537'));
  203. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top