Advertisement
diabliyo

validador_xsd_sat

Sep 19th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. URLs de XSDs que uso
  5. http://core.moneybox.com.mx/xsd/Divisas.xsd
  6. http://core.moneybox.com.mx/xsd/TimbreFiscalDigital.xsd
  7. http://core.moneybox.com.mx/xsd/TuristaPasajeroExtranjero.xsd
  8. http://core.moneybox.com.mx/xsd/cfdiregistrofiscal.xsd
  9. http://core.moneybox.com.mx/xsd/cfdv2.xsd
  10. http://core.moneybox.com.mx/xsd/cfdv22.xsd
  11. http://core.moneybox.com.mx/xsd/cfdv22complemento.xsd
  12. http://core.moneybox.com.mx/xsd/cfdv2complemento.xsd
  13. http://core.moneybox.com.mx/xsd/cfdv2psgecfd.xsd
  14. http://core.moneybox.com.mx/xsd/cfdv3.xsd
  15. http://core.moneybox.com.mx/xsd/cfdv32.xsd
  16. http://core.moneybox.com.mx/xsd/cfdv32complemento.xsd
  17. http://core.moneybox.com.mx/xsd/cfdv3complemento.xsd
  18. http://core.moneybox.com.mx/xsd/cfdv3tfd.xsd
  19. http://core.moneybox.com.mx/xsd/detallista.xsd
  20. http://core.moneybox.com.mx/xsd/donat11.xsd
  21. http://core.moneybox.com.mx/xsd/ecc.xsd
  22. http://core.moneybox.com.mx/xsd/iedu.xsd
  23. http://core.moneybox.com.mx/xsd/implocal.xsd
  24. http://core.moneybox.com.mx/xsd/nomina.xsd
  25. http://core.moneybox.com.mx/xsd/nomina11.xsd
  26. http://core.moneybox.com.mx/xsd/pfic.xsd
  27. http://core.moneybox.com.mx/xsd/spei.xsd
  28. http://core.moneybox.com.mx/xsd/terceros11.xsd
  29. http://core.moneybox.com.mx/xsd/ventavehiculos.xsd
  30. */
  31.  
  32. $xml_txt= file_get_contents($_FILES["upload_xml"]["tmp_name"]); # el XML viene del upload
  33.  
  34. # quitamos posible addenda
  35. $xml_txt= preg_replace('{<Addenda.*/Addenda>}is', '<Addenda/>', $xml_txt); # quitamos
  36. $xml_txt= preg_replace('{<cfdi:Addenda.*/cfdi:Addenda>}is', '<cfdi:Addenda/>', $xml_txt); # quitamos
  37.  
  38. if( !valida_cfdisat($xml_txt, "xsd", 0) ) # si no cumple estandar
  39.     echo 'El XML fue mal generado o presenta errores (no cumple estandar).';
  40. else        echo 'Exito'; # lo cumplio
  41.  
  42.  
  43.  
  44. # validador de XMLs
  45. function valida_cfdisat($file, $op, $ret )
  46.     {
  47.     $r=0;
  48.     if( !$file )        $r=0;
  49.     else if( !$op ) $r=0;
  50.     else
  51.         {
  52.         $xml= new DOMDocument();
  53.         $xml->preserveWhiteSpace= FALSE;
  54.         $xml->loadXML($file);
  55.         $cfdi= xml_getallatributos( $xml, "Comprobante" );
  56.  
  57.         if( !strcmp($op, "xsd") ) # validar XSD
  58.             {
  59.             $version= $cfdi["Comprobante"]["version"];
  60.             libxml_use_internal_errors(true);
  61.             $arr_v= array( "2.0"=>HTTP_SERVER. "/xsd/cfdv2complemento.xsd",
  62.                                 "2.2"=>HTTP_SERVER. "/xsd/cfdv22complemento.xsd",
  63.                                 "3.0"=>HTTP_SERVER. "/xsd/cfdv3complemento.xsd",
  64.                                 "3.2"=>HTTP_SERVER. "/xsd/cfdv32.xsd"
  65.                             );
  66.            
  67.             foreach( $arr_v as $key=>$val )
  68.                 {
  69.                 if( !strcmp( $key, $version) )
  70.                     {
  71.                     if( !($xml->schemaValidate($val)) )
  72.                         $r= array( "r"=>0, "msg"=>"Esquema detectado ". $key. ", estructura invalida" );
  73.                     else        $r= array( "r"=>1, "msg"=>"Esquema detectado ". $key. ", estructura valida" );
  74.                     }
  75.                 }
  76.            
  77.             if( !$r ) # si continua vacio, no hubo coincidencia
  78.                 $r= array( "r"=>0, "msg"=>"Versi". acento("o"). "on invalida [". desproteger_cadena($version). "]" );
  79.  
  80.             unset($version, $arr_v);
  81.             }
  82.         else if( !strcmp($op, "cert") ) # validar Certificado
  83.             {
  84.             }
  85.         else if( !strcmp($op, "sello") ) # validar Sello
  86.             {
  87.             }
  88.         else    $r= array( "r"=>0, "msg"=>"operacion invalida (cmd error)" );
  89.  
  90.         unset($xml, $of, $xp);     
  91.        
  92.         # valor de retorno
  93.         if( $ret ) # activado retorno detallado
  94.             return $r;
  95.         else # retorno simple
  96.             return $r["r"];
  97.         }
  98.     return $r;
  99.     }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement