Guest User

Untitled

a guest
Nov 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3. const USERNAME = '???';
  4. const PASSWORD = '???';
  5. header ( "content-type: text/plain;charset=utf8" );
  6. require_once ('hhb_.inc.php');
  7. $hc = new hhb_curl ( '', true );
  8. $html = $hc->exec ( 'https://app.cfe.gob.mx/Aplicaciones/CCFE/Recibos/Consulta/login.aspx' )->getStdOut ();
  9. $domd = @DOMDocument::loadHTML ( $html );
  10. $inputsRaw = getDOMDocumentFormInputs ( $domd, true ) ['aspnetForm'];
  11. $inputs = array ();
  12. foreach ( $inputsRaw as $tmp ) {
  13. $inputs [$tmp->getAttribute ( "name" )] = $tmp->getAttribute ( "value" );
  14. }
  15. assert ( isset ( $inputs ['__VIEWSTATE'], $inputs ['__EVENTVALIDATION'] ) );
  16. $inputs ['ctl00$PHContenidoPag$UCLogin2$LoginUsuario$UserName'] = USERNAME;
  17. $inputs ['ctl00$PHContenidoPag$UCLogin2$LoginUsuario$Password'] = PASSWORD;
  18. hhb_var_dump ( $inputs );
  19. $html = $hc->setopt_array ( array (
  20. CURLOPT_URL => 'https://app.cfe.gob.mx/Aplicaciones/CCFE/Recibos/Consulta/login.aspx',
  21. CURLOPT_POST => true,
  22. CURLOPT_POSTFIELDS => http_build_query ( $inputs )
  23. ) )->exec ()->getStdOut ();
  24. // hhb_var_dump($html) & die();
  25. $domd = @DOMDocument::loadHTML ( $html );
  26. $xp = new DOMXPath ( $domd );
  27. $loginErrors = $xp->query ( '//*[(contains(@style,"color:Red") or contains(@color,"Red")) and not(contains(@style,"hidden"))]' );
  28. foreach ( $loginErrors as $tmp ) {
  29. echo "login error!! ";
  30. var_dump ( $tmp->textContent );
  31. }
  32. if (0 === $loginErrors->length) {
  33. echo "login success!";
  34. }
  35.  
  36. function getDOMDocumentFormInputs(DOMDocument $domd, bool $getOnlyFirstMatches = false): array {
  37. // :DOMNodeList?
  38. $forms = $domd->getElementsByTagName ( 'form' );
  39. $parsedForms = array ();
  40. $isDescendantOf = function (DOMNode $decendant, DOMNode $ele): bool {
  41. $parent = $decendant;
  42. while ( NULL !== ($parent = $parent->parentNode) ) {
  43. if ($parent === $ele) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. };
  49. // i can't use array_merge on DOMNodeLists :(
  50. $merged = function () use (&$domd): array {
  51. $ret = array ();
  52. foreach ( $domd->getElementsByTagName ( "input" ) as $input ) {
  53. $ret [] = $input;
  54. }
  55. foreach ( $domd->getElementsByTagName ( "textarea" ) as $textarea ) {
  56. $ret [] = $textarea;
  57. }
  58. foreach ( $domd->getElementsByTagName ( "button" ) as $button ) {
  59. $ret [] = $button;
  60. }
  61. return $ret;
  62. };
  63. $merged = $merged ();
  64. foreach ( $forms as $form ) {
  65. $inputs = function () use (&$domd, &$form, &$isDescendantOf, &$merged): array {
  66. $ret = array ();
  67. foreach ( $merged as $input ) {
  68. // hhb_var_dump ( $input->getAttribute ( "name" ), $input->getAttribute ( "id" ) );
  69. if ($input->hasAttribute ( "disabled" )) {
  70. // ignore disabled elements?
  71. continue;
  72. }
  73. $name = $input->getAttribute ( "name" );
  74. if ($name === '') {
  75. // echo "inputs with no name are ignored when submitted by mainstream browsers (presumably because of specs)... follow suite?", PHP_EOL;
  76. continue;
  77. }
  78. if (! $isDescendantOf ( $input, $form ) && $form->getAttribute ( "id" ) !== '' && $input->getAttribute ( "form" ) !== $form->getAttribute ( "id" )) {
  79. // echo "this input does not belong to this form.", PHP_EOL;
  80. continue;
  81. }
  82. if (! array_key_exists ( $name, $ret )) {
  83. $ret [$name] = array (
  84. $input
  85. );
  86. } else {
  87. $ret [$name] [] = $input;
  88. }
  89. }
  90. return $ret;
  91. };
  92. $inputs = $inputs (); // sorry about that, Eclipse gets unstable on IIFE syntax.
  93. $hasName = true;
  94. $name = $form->getAttribute ( "id" );
  95. if ($name === '') {
  96. $name = $form->getAttribute ( "name" );
  97. if ($name === '') {
  98. $hasName = false;
  99. }
  100. }
  101. if (! $hasName) {
  102. $parsedForms [] = array (
  103. $inputs
  104. );
  105. } else {
  106. if (! array_key_exists ( $name, $parsedForms )) {
  107. $parsedForms [$name] = array (
  108. $inputs
  109. );
  110. } else {
  111. $parsedForms [$name] [] = $tmp;
  112. }
  113. }
  114. }
  115. unset ( $form, $tmp, $hasName, $name, $i, $input );
  116. if ($getOnlyFirstMatches) {
  117. foreach ( $parsedForms as $key => $val ) {
  118. $parsedForms [$key] = $val [0];
  119. }
  120. unset ( $key, $val );
  121. foreach ( $parsedForms as $key1 => $val1 ) {
  122. foreach ( $val1 as $key2 => $val2 ) {
  123. $parsedForms [$key1] [$key2] = $val2 [0];
  124. }
  125. }
  126. }
  127. return $parsedForms;
  128. }
Add Comment
Please, Sign In to add comment