Guest User

Untitled

a guest
Nov 16th, 2019
122
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 => http_build_query(["auth_token" => "ABC123", "service" => "recaptcha_v2", "siteKey" => $site_key, "url" => $url]),
  43. ));
  44.  
  45. $response = curl_exec($curl);
  46. $err = curl_error($curl);
  47.  
  48. curl_close($curl);
  49.  
  50. if ($err) {
  51. return false;
  52. } else {
  53. return json_decode($response);
  54. }
  55. }
  56.  
  57. public function solve_captcha($site_key, $url, $tries = 0) {
  58. // tenta no AZ primerio, se nao conseguir vai p o botstation
  59. if ($task = $this->create_botstation_captcha($site_key, $url)) {
  60. if ($task->status && $task->result->task_id) {
  61. $response = $this->get_botstation_captcha_response($task->result->task_id);
  62. $timer = 0;
  63. while ($timer <= 120 && in_array($response->result->status, ["QUEUED", "RUNNING"])) {
  64. sleep(1);
  65. $timer ++;
  66. $response = $this->get_botstation_captcha_response($task->result->task_id);
  67. }
  68. if (!empty($response->result->output->token)) {
  69. return $response->result->output->token;
  70. } else {
  71. if ($tries <= 3) {
  72. $tries++;
  73. return $this->solve_captcha($site_key, $url, $tries);
  74. } else {
  75. return false;
  76. }
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83. private function parse_rows($html) {
  84. $debitos = [];
  85. $total = 0;
  86. $doc = new DOMDocument();
  87. $doc->loadHTML($html);
  88.  
  89. $tables = $doc->getElementsByTagName('table');
  90. if (!empty($tables)) {
  91. foreach ($tables as $key => $tmp) {
  92. if ($tmp->getAttribute("class") != "alternate") {
  93. continue;
  94. }
  95. $table = $tmp;
  96. }
  97. }
  98. $rows = $table->getElementsByTagName("tr");
  99. foreach ($rows as $row) {
  100. if (!preg_match("/[0-9\.\,]/", $row->textContent))
  101. continue;
  102.  
  103. $valor = preg_replace("/\,/", ".", preg_replace("/[^0-9\,]/", "", trim($row->textContent)));
  104. $debitos[] = $valor;
  105. $total += $valor;
  106. }
  107.  
  108. return ['total' => $total, 'debitos' => $debitos];
  109. }
  110.  
  111. public function consulta($renavam, $tries = 0) {
  112. try {
  113. print_r("consulta renavam: {$renavam} started\n");
  114. $captcha_token = $this->solve_captcha('6LeeDDoUAAAAAL7awoPJgSMuiF6AuJW5rf0zqEfy', 'https://www.dividaativa.pge.sp.gov.br');
  115. if ($captcha_token) {
  116. print_r("captcha solved\n");
  117. $start_time = time();
  118.  
  119. $options = array(
  120. CURLOPT_RETURNTRANSFER => true, // return web page
  121. CURLOPT_HEADER => false, // don't return headers
  122. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  123. 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
  124. CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
  125. );
  126. //
  127. /** JSESSIONID */
  128. $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/");
  129. curl_setopt_array($handle, $options);
  130. curl_setopt($handle, CURLOPT_HEADER, true);
  131. $response = curl_exec($handle);
  132. curl_close($handle);
  133. $m = [];
  134. preg_match("/Set\-Cookie\:\sJSESSIONID=(?P<JSESSIONID>.*)\;/", $response, $m);
  135. $cookie = "Cookie: JSESSIONID=" . $m['JSESSIONID'] . "; path=/da-ic-web;";
  136. // print_r($cookie);
  137.  
  138.  
  139. $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/consultaDebitosComunicadoCadinVerificaCaptcha.do");
  140. curl_setopt_array($handle, $options);
  141. curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
  142. curl_setopt($handle, CURLOPT_POST, 1);
  143. curl_setopt($handle, CURLOPT_POSTFIELDS, "tipoDocumento=7&g-recaptcha-response={$captcha_token}&valorDocumento={$renavam}&tipoConsulta=termoAceite&operation=buscar");
  144. $response = curl_exec($handle);
  145. curl_close($handle);
  146.  
  147. print_r("html OK\n");
  148.  
  149. $doc = new DOMDocument();
  150. @$doc->loadHTML($response);
  151.  
  152. $debitos = [];
  153. $debitos_return = [];
  154. $total = 0;
  155. if ($doc->getElementById('idTipoDebitoCliente')) {
  156. $tmp = $this->parse_rows($response);
  157. $debitos_return = $tmp['debitos'];
  158. $total = $tmp['total'];
  159. } else {
  160. preg_match_all("/preencheHidden\(\'idDebito\'\,\s\'([0-9]*)\'\,\s\'idPessoa\'\,\s\'([0-9]*)\'\)/", $response, $tmp);
  161. $debitos = $tmp[1];
  162. $ids = $tmp[2];
  163. }
  164.  
  165. if (!empty($debitos)) {
  166. foreach ($debitos as $key => $debito) {
  167.  
  168. $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/escolherDevedorDebito.do");
  169. curl_setopt_array($handle, $options);
  170.  
  171. curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
  172. curl_setopt($handle, CURLOPT_POST, 1);
  173. curl_setopt($handle, CURLOPT_POSTFIELDS, "idDebito={$debito}&idPessoa={$ids[$key]}&operation=selecionar&devedor=1");
  174. $response = curl_exec($handle);
  175. curl_close($handle);
  176.  
  177. $tmp = $this->parse_rows($response);
  178. $debitos_return = array_merge($debitos_return, $tmp['debitos']);
  179. $total += $tmp['total'];
  180. }
  181. }
  182. print_r("executed in " . (time() - $start_time ) . " seconds\n");
  183. return ["success" => true, "debitos" => $debitos_return, "total" => $total];
  184. } else {
  185. return ["success" => false, "msg" => "Captcha Error!"];
  186. }
  187. } catch (Exception $ex) {
  188. if ($tries <= 3) {
  189. $tries++;
  190. return $this->consulta($renavam, $tries);
  191. } else {
  192. return ["success" => false, "msg" => "Órgão fora de operação!"];
  193. }
  194. }
  195. }
  196.  
  197. }
  198.  
  199. $da = new Divida_ativa();
  200. //print_r($da->consulta('123123123'));
  201. print_r($da->consulta('945509537'));
  202. ?>
RAW Paste Data