34 define(
'DEBUG',
false);
39 require_once
'WSSSoapClient.php';
43 require_once (
'WSCMIME/WSCMime.php');
77 const KEYCLIENTE_AUTENTICA =
'autentica';
78 const KEYCLIENTE_AUTORIZA =
'autoriza';
79 const KEYCLIENTE_FIRMA =
'firma';
80 const KEYCLIENTE_GDE =
'gde';
81 const KEYCLIENTE_SALT =
'salt';
82 const KEYCLIENTE_CSV =
'csv';
83 const KEYCLIENTE_CATASTRO =
'catastro';
84 const KEYCLIENTE_CATASTRO_BIENES =
'catbienes';
85 const KEYCLIENTE_GDE2 =
'gde2';
86 const KEYCLIENTE_PF =
'pfcons';
87 const KEYCLIENTE_PF_MOD =
'pfmod';
88 const KEYCLIENTE_PF_ADM =
'pfadm';
89 const KEYCLIENTE_GVLOGIN =
'gvlogin';
95 const TIPOBUSQUEDA_USUARIO =
'USUARIO';
96 const TIPOBUSQUEDA_GRUPO =
'GRUPO';
105 protected static $MYSOAPOP_TRACE =
true;
106 protected static $MYSOAPOP_WSDL_CACHE = WSDL_CACHE_BOTH;
107 protected static $MYSOAPOP_SOAP_VERSION = SOAP_1_1;
114 protected static $NAMESPACE_GVA =
'http://dgm.gva.es/ayf/war/schemas/v2_00';
115 protected static $NAMESPACE_GDE =
'urn:es:gva:dgm:tra:gde:vista:model';
116 protected static $NAMESPACE_SALT =
'http://salt.ws.edu.gva.es/';
117 protected static $NAMESPACE_CSVGVA =
'urn:es:gva:dgm:tra:csvgva';
118 protected static $NAMESPACE_GDE2 =
'urn:es:gva:dgm:tra:gde:vista:v2:model';
119 protected static $NAMESPACE_CAT_ESP =
'http://intermediacion.redsara.es/scsp/esquemas/datosespecificos';
120 protected static $NAMESPACE_CAT_PET =
'http://intermediacion.redsara.es/scsp/esquemas/V3/peticion';
121 protected static $NAMESPACE_CAT_SR =
'http://intermediacion.redsara.es/scsp/esquemas/V3/solicitudRespuesta';
122 protected static $NAMESPACE_PF = null;
123 protected static $NAMESPACE_PF_MOD = null;
124 protected static $NAMESPACE_PF_ADM = null;
125 protected static $NAMESPACE_LOGIN =
'urn:es:gva:gvlogin:sso:model';
139 private $WSDLTimeOut;
146 protected $v_clienteWS;
161 protected $passPhrase=null;
168 private $certFile=
'';
191 protected $vTrazabilidad;
215 protected $enableWSSecurity =
true;
223 protected $enableTrazabilidadPAI =
true;
244 if (!is_array($v_wsdl))
247 __CLASS__.
':'.__METHOD__.
'['.__FILE__.
'-'.__LINE__.
']'.
248 'v_wsdl debe ser un array asociativo (autenticacion,autorizacion,firma) con las URI de los WSDL' 252 $this->v_wsdl = $v_wsdl;
255 $this->v_clienteWS = array
257 self::KEYCLIENTE_AUTENTICA => null,
258 self::KEYCLIENTE_AUTORIZA => null,
259 self::KEYCLIENTE_CATASTRO => null,
260 self::KEYCLIENTE_CATASTRO_BIENES => null,
261 self::KEYCLIENTE_CSV => null,
262 self::KEYCLIENTE_FIRMA => null,
263 self::KEYCLIENTE_GDE => null,
264 self::KEYCLIENTE_GDE2 => null,
265 self::KEYCLIENTE_PF => null,
266 self::KEYCLIENTE_PF_ADM => null,
267 self::KEYCLIENTE_PF_MOD => null,
268 self::KEYCLIENTE_SALT => null,
269 self::KEYCLIENTE_GVLOGIN => null
273 $this->nsCert = null;
274 $this->enableWSSecurity =
true;
275 $this->enableTrazabilidadPAI =
true;
278 if (is_array($v_opciones))
281 if (array_key_exists(
'certFile', $v_opciones))
283 $this->setCertFile($v_opciones[
'certFile']);
284 $this->nsCert = $this->getCertificateSerial();
287 if (array_key_exists(
'keyFile', $v_opciones))
289 $this->setKeyFile($v_opciones[
'keyFile']);
292 if (array_key_exists(
'passPhrase', $v_opciones))
294 $this->setPassPhrase($v_opciones[
'passPhrase']);
297 if (array_key_exists(
'enableWSSecurity', $v_opciones))
299 $this->enableWSSecurity = $v_opciones[
'enableWSSecurity'] &&
true;
303 if (array_key_exists(
'enableTrazabilidadPAI', $v_opciones))
305 $this->enableTrazabilidadPAI = $v_opciones[
'enableTrazabilidadPAI'] &&
true;
309 if (array_key_exists(
'idApp', $v_opciones))
311 $this->idApp = $v_opciones[
'idApp'];
315 $this->vTrazabilidad = array
317 'idApp' => $this->idApp,
318 'nsCert'=> $this->nsCert
322 $this->setDebugMode(
DEBUG);
323 $this->_debugInfo = array();
324 $this->WSDLTimeOut = 15;
347 case self::KEYCLIENTE_CATASTRO:
348 case self::KEYCLIENTE_CATASTRO_BIENES:
349 require_once
'WS/catastro/WSClientCatastro.php';
350 require_once
'WS/catastro/ComposerCatastro.php';
351 require_once
'WS/catastro/objCatastro.php';
356 case self::KEYCLIENTE_CSV:
357 require_once
'WS/CSV/WSClientCSV.php';
362 case self::KEYCLIENTE_AUTENTICA:
363 require_once
'WS/SAFE/WSClientAutentica.php';
367 case self::KEYCLIENTE_AUTORIZA:
368 require_once
'WS/SAFE/WSClientAutoriza.php';
372 case self::KEYCLIENTE_FIRMA:
373 require_once
'WS/SAFE/WSClientFirma.php';
378 case self::KEYCLIENTE_GDE:
379 require_once
'WS/GDE/WSClientGDE.php';
383 case self::KEYCLIENTE_GDE2:
384 require_once
'WS/GDE/WSClientGDE2.php';
385 require_once
'WS/GDE/ComposerGDE2.php';
389 case self::KEYCLIENTE_SALT:
390 require_once
'WS/SALT/WSClientSALT.php';
395 case self::KEYCLIENTE_GVLOGIN:
396 require_once
'WS/GVLOGIN/WSClientGVLogin.php';
401 case self::KEYCLIENTE_PF:
402 case self::KEYCLIENTE_PF_MOD:
403 case self::KEYCLIENTE_PF_ADM:
404 require_once
'WS/PORTAFIRMAS/WSClientPortafirmas.php';
405 require_once
'WS/PORTAFIRMAS/ComposerPortafirmas.php';
414 error_log(
'Clave cliente no reconocida. ('.$wsClient.
')');
419 if (is_array($v_opciones))
422 if (array_key_exists(
'certFile', $v_opciones))
424 $wsClient->setCertFile($v_opciones[
'certFile']);
425 $wsClient->nsCert = $wsClient->getCertificateSerial();
428 if (array_key_exists(
'keyFile', $v_opciones))
430 $wsClient->setKeyFile($v_opciones[
'keyFile']);
433 if (array_key_exists(
'passPhrase', $v_opciones))
435 $wsClient->setPassPhrase($v_opciones[
'passPhrase']);
438 if (array_key_exists(
'enableWSSecurity', $v_opciones))
440 $wsClient->enableWSSecurity = $v_opciones[
'enableWSSecurity'] &&
true;
444 if (array_key_exists(
'enableTrazabilidadPAI', $v_opciones))
446 $wsClient->enableTrazabilidadPAI = $v_opciones[
'enableTrazabilidadPAI'] &&
true;
450 if (array_key_exists(
'idApp', $v_opciones))
452 $wsClient->idApp = $v_opciones[
'idApp'];
455 $wsClient->loadTracertPAI($v_opciones);
471 return($this->_debugMode);
483 return $this->_debugInfo;
496 self::$MYSOAPOP_TRACE =
true;
497 $this->_debugMode = $activo;
500 self::$MYSOAPOP_WSDL_CACHE = WSDL_CACHE_NONE;
504 self::$MYSOAPOP_WSDL_CACHE = WSDL_CACHE_MEMORY;
505 $this->_debugInfo = array();
520 $this->_debugInfo[] = $info;
535 $this->keyFile = $pathKey;
547 $this->passPhrase = $passphrase;
559 $this->certFile = $pathCert;
571 $this->WSDLTimeOut = $segs;
584 if (empty($this->certFile))
586 throw new Exception(
'No se ha fijado valor para el certificado o el mismo no existe.');
589 $certFile = realpath($this->certFile);
590 if ($certFile ===
false)
592 throw new Exception(
'No existe el fichero de cetificado: '.$this->certFile);
595 $cert = file_get_contents($certFile);
598 throw new Exception(
'No puede leerse el certificado : '.$certFile);
601 $v_certData = openssl_x509_parse($cert,
true);
602 $nsCert = strtoupper(self::numberBaseConvert($v_certData[
'serialNumber']));
605 $this->nsCert = $nsCert;
623 $certPath = realpath($ruta);
625 $certContent = file_get_contents($certPath);
626 if ($certContent ===
false)
629 __CLASS__.
':'.__METHOD__.
'['.__FILE__.
']-L'.__LINE__.
630 '. El certificado no se encuentra en la ruta: '.$certPath
636 $formato = pathinfo($certPath, PATHINFO_EXTENSION);
639 $formato = trim(strtoupper($formato));
644 $v_certPEM = array();
645 openssl_pkcs12_read($certContent, $v_certPEM,
'');
646 $cert = $v_certPEM[
'cert'];
651 $cert = $certContent;
655 $v_certData = openssl_x509_parse($cert,
true);
656 if ($v_certData ===
false)
659 __CLASS__.
':'.__METHOD__.
'['.__FILE__.
']-L'.__LINE__.
'.'.
660 'El formato del certificado no se corresponde con '.$formato
678 if (!is_array($vTrazabilidadPAI))
680 throw new Exception(
'Las opciones de trazabilidad deben ser un array asociativo [idApp, nsCert]');
683 if (array_key_exists(
'idApp', $vTrazabilidadPAI))
685 $this->idApp = $vTrazabilidadPAI[
'idApp'];
688 if (array_key_exists(
'nsCert', $vTrazabilidadPAI))
690 $this->nsCert = $vTrazabilidadPAI[
'nsCert'];
701 $this->enableWSSecurity =
true;
711 $this->enableWSSecurity =
false;
721 $this->enableTrazabilidadPAI =
true;
731 $this->enableTrazabilidadPAI =
false;
746 if (empty($tipo))
return null;
748 $tipo = trim(strtolower($tipo));
751 $this->__clienteOn($tipo);
753 if (!is_object($this->v_clienteWS[$tipo]))
756 return ($this->v_clienteWS[$tipo]);
781 protected function __clienteOn($tipo, $trazabilidadPai=
true, $opcionesClienteWS = null)
783 $vTiposCliente = array();
786 $tipo = trim(strtolower($tipo));
788 if (array_key_exists($tipo, $this->v_clienteWS))
790 $vTiposCliente = array($tipo);
794 if ($this->getDebugMode())
796 $this->addDebugInfo(__CLASS__.
':'.__METHOD__.
'['.__FILE__.
'-'.__LINE__.
']'.
' Tipo de cliente WS ('.$tipo.
') no reconocido.');
799 __CLASS__.
':'.__METHOD__.
'['.__FILE__.
']-L'.__LINE__.
'.'.
800 'Tipo de cliente WS ('.$tipo.
') no reconocido' 821 if (!is_array($opcionesClienteWS))
837 $opcionesClienteWS = array (
838 'soap_version' => SOAP_1_1,
839 'user_agent' =>
'WSSSoapClient',
840 'exceptions' =>
true,
841 'cache_wsdl' => self::$MYSOAPOP_WSDL_CACHE,
842 'trace' => self::$MYSOAPOP_TRACE
847 $opcionesClienteWS[
'soap_version'] = SOAP_1_1;
848 $opcionesClienteWS[
'user_agent'] =
'WSSSoapClient';
849 $opcionesClienteWS[
'exceptions'] =
true;
850 $opcionesClienteWS[
'cache_wsdl'] = self::$MYSOAPOP_WSDL_CACHE;
851 $opcionesClienteWS[
'trace'] = self::$MYSOAPOP_TRACE;
854 if ($this->getDebugMode())
856 $this->addDebugInfo(
"Opciones:\n ".print_r($opcionesClienteWS,
true));
859 foreach ($vTiposCliente as $claveTipo)
861 if (is_object($this->v_clienteWS[$claveTipo]))
continue;
865 if (ini_get(
'allow_url_fopen') ==
true)
869 'timeout' => $this->WSDLTimeOut
872 'timeout' => $this->WSDLTimeOut
875 'verify_peer' =>
false,
876 'allow_self_signed' =>
true 880 $sc = stream_context_create($opciones);
881 $fd = fopen($this->v_wsdl[$claveTipo],
'r',
false, $sc);
884 throw new Exception(
'allow_url_fopen activo. La URL '.$this->v_wsdl[$claveTipo].
' no puede alcanzarse.');
939 if ($this->getDebugMode())
941 $this->addDebugInfo(__CLASS__.
':'.__METHOD__.
'['.__FILE__.
'-'.__LINE__.
']'.
' allow_url_fopen NO ACTIVO y Extensión CURL NO ACTIVA');
946 $clienteWS =
new WSSSoapClient($this->v_wsdl[$claveTipo], $opcionesClienteWS);
948 if ($this->enableWSSecurity ==
true)
950 $clienteWS->setCertFile($this->certFile);
951 $clienteWS->setKeyFile($this->keyFile);
952 $clienteWS->setPassPhrase($this->passPhrase);
953 $clienteWS->enableWSSecurity();
957 $clienteWS->disableWSSecurity();
960 if ($this->enableTrazabilidadPAI)
962 $clienteWS->loadTracertPAI($this->vTrazabilidad);
964 $this->v_clienteWS[$claveTipo] = $clienteWS;
968 if ($this->getDebugMode())
970 $this->addDebugInfo(__CLASS__.
':'.__METHOD__.
'['.__FILE__.
'-'.__LINE__.
']'.
"\n");
971 if (is_object($clienteWS))
973 $this->addDebugInfo(
"RqH:\n ".$clienteWS->__getLastRequestHeaders());
974 $this->addDebugInfo(
"Rq:\n ".$clienteWS->__getLastRequest());
975 $this->addDebugInfo(
"RsH:\n ".$clienteWS->__getLastResponse());
976 $this->addDebugInfo(
"Rs:\n ".$clienteWS->__getLastResponseHeaders());
979 $this->addDebugInfo(
"\n");
997 if (!is_array($array))
return;
999 if (is_numeric(key($array)))
1001 foreach ($array as $key => $value)
1003 $array[$key] = $this->array2ObjectTree($value);
1008 foreach ($array as $key => $value)
1010 if (is_array($value))
1012 $Object->$key = $this->array2ObjectTree($value);
1016 $Object->$key = $value;
1031 if (is_array($obj) || is_object($obj))
1034 foreach ($obj as $key => $value)
1036 $result[$key] = $this->objectTree2array($value);
1044 protected function tratarExcepcionEstandar($e, $clienteWS)
1047 WSComunPAIException::tryToThrow($e);
1050 if ($this->getDebugMode())
1052 $this->addDebugInfo(__CLASS__.
':'.__METHOD__.
'['.__FILE__.
'-'.__LINE__.
']'.
"\n");
1053 if (is_object($clienteWS))
1055 $this->addDebugInfo(
"RqH:\n ".$clienteWS->__getLastRequestHeaders());
1056 $this->addDebugInfo(
"Rq:\n ".$clienteWS->__getLastRequest());
1057 $this->addDebugInfo(
"RsH:\n ".$clienteWS->__getLastResponse());
1058 $this->addDebugInfo(
"Rs:\n ".$clienteWS->__getLastResponseHeaders());
1082 $mimeBoundary =
'--MIME_Boundary';
1083 $vBodyResponse = explode($mimeBoundary, $response);
1084 $numElementos = count($vBodyResponse);
1087 if ($numElementos<=1)
1089 $mimeBoundary =
'--uuid:';
1090 $vBodyResponse = explode($mimeBoundary, $response);
1091 $numElementos = count($vBodyResponse);
1092 $srcData = $response;
1094 elseif ($numElementos>1)
1096 $srcData = $vBodyResponse[1];
1099 $vRespuesta = array();
1100 ini_set(
'pcre.backtrack_limit',
'100000000');
1101 preg_match(
"/<[a-z]*:Envelope.*?>(.*)<\/[a-z]*:Envelope>/is", $srcData, $vRespuesta);
1102 if (preg_last_error()!=PREG_NO_ERROR)
1105 $start = stripos($srcData,
':Envelope');
1106 $start = strripos($srcData,
'<', $start - strlen($srcData));
1107 $end = strripos($srcData,
':Envelope>');
1108 $cadenaRespuesta = substr($srcData, $start, $end);
1110 elseif (count($vRespuesta)<1)
1112 throw new Exception(
'No puede ubicarse RESPONSE dentro de MTOM');
1116 $cadenaRespuesta = $vRespuesta[0];
1121 $dom->loadXML($cadenaRespuesta);
1122 $xpath = new \DOMXpath($dom);
1123 $vNodoRespuesta = $xpath->query(
"/*[local-name()='Envelope']/*[local-name()='Body']/*/*[local-name()='respuesta']");
1125 if (!empty($itemToReturn))
1127 $subStrXpath =
"/*[local-name()='{$itemToReturn}']";
1129 $vNodoRespuesta = $xpath->query(
"/*[local-name()='Envelope']/*[local-name()='Body']/*".$subStrXpath);
1130 $nodoRespuesta = $vNodoRespuesta->item(0);
1132 if ($numElementos>0)
1135 $oMime = $oMimeParser->decodeMTOM($vBodyResponse);
1136 unset($oMime->body);$oMime->body=null;gc_collect_cycles();
1138 $vNodoInclude = $xpath->query(
"//*[local-name()='Include']");
1139 foreach ($vNodoInclude as $nodoItem)
1141 $subIdBuscado = (string) $nodoItem->getAttribute(
'href');
1142 $subIdBuscado = urldecode($subIdBuscado);
1143 $idBuscado =
'<'.substr($subIdBuscado, 4).
'>';
1144 foreach ($oMime->parts as &$parte)
1146 if ($idBuscado == ($parte->headers[
'content-id']))
1148 if ( strtolower(substr($parte->mimetype, 0, 4)) !==
'text')
1150 $parte->body = base64_encode($parte->body);
1152 $textNode = $dom->createTextNode(($parte->body));
1153 $nodoItem->parentNode->replaceChild($textNode, $nodoItem);
1158 return($this->xml_to_array($nodoRespuesta));
1164 protected function xml_to_array(&$root)
1167 if ($root->hasAttributes())
1169 $attrs = $root->attributes;
1170 foreach ($attrs as $attr)
1172 $result[
'@attributes'][$attr->name] = $attr->value;
1175 if ($root->hasChildNodes())
1177 $children = $root->childNodes;
1178 if ($children->length == 1)
1180 $child = $children->item(0);
1181 if ($child->nodeType == XML_TEXT_NODE)
1183 $result[
'_value'] = $child->nodeValue;
1184 return count($result) == 1 ? $result[
'_value'] : $result;
1188 foreach ($children as $child)
1190 if (!isset($result[$child->nodeName]))
1192 $result[$child->nodeName] = $this->xml_to_array($child);
1196 if (!isset($groups[$child->nodeName]))
1198 $result[$child->nodeName] = array($result[$child->nodeName]);
1199 $groups[$child->nodeName] = 1;
1201 $result[$child->nodeName][] = $this->xml_to_array($child);
1222 $chars =
"0123456789abcdefghijklmnopqrstuvwxyz";
1223 $tostring = substr($chars, 0, $tobase);
1224 $length = strlen($numstring);
1227 for ($i = 0; $i < $length; $i++)
1229 $number[$i] = strpos($chars, $numstring{$i});
1235 for ($i = 0; $i < $length; $i++)
1237 $divide = $divide * $frombase + $number[$i];
1238 if ($divide >= $tobase)
1240 $number[$newlen++] = (int)($divide / $tobase);
1241 $divide = $divide % $tobase;
1242 } elseif ($newlen > 0)
1244 $number[$newlen++] = 0;
1248 $result = $tostring{$divide} . $result;
1249 }
while ($newlen != 0);
1261 if (getenv(
'HTTP_CLIENT_IP'))
1263 $ip = getenv(
'HTTP_CLIENT_IP');
1265 else if(getenv(
'HTTP_X_FORWARDED_FOR'))
1267 $ip = getenv(
'HTTP_X_FORWARDED_FOR');
1269 else if (getenv(
'HTTP_X_FORWARDED'))
1271 $ip = getenv(
'HTTP_X_FORWARDED');
1273 else if(getenv(
'HTTP_FORWARDED_FOR'))
1275 $ip = getenv(
'HTTP_FORWARDED_FOR');
1277 else if(getenv(
'HTTP_FORWARDED'))
1279 $ip = getenv(
'HTTP_FORWARDED');
1281 else if(getenv(
'REMOTE_ADDR'))
1283 $ip = getenv(
'REMOTE_ADDR');
1309 const ERROR_SERVICIO =
'0101';
1310 const TIMESTAMP_INVALIDO =
'0230';
1311 const ORGANISMO_NO_AUTORIZADO =
'0301';
1312 const ESTRUCURA_XML_NO_CORRESPONDE_A_ESQUEMA =
'0401';
1313 const MENSAJE_XML_INVALIDO =
'0403';
1314 const OPERACION_SOLICITADA_INCORRECTA =
'0800';
1315 const FALTA_CABECERA_TRAZABILIDAD =
'0807';
1316 const ERROR_GENERAL_INDEFINIDO =
'0904';
1319 private $CodigoEstado =
'';
1320 private $CodigoEstadoSecundario =
'';
1321 private $LiteralError =
'';
1322 private $LiteralErrorSec =
'';
1324 public function getCodigoEstado() {
1325 return $this->CodigoEstado;
1327 public function getCodigoEstadoSecundario() {
1328 return $this->CodigoEstadoSecundario;
1330 public function getLiteralError() {
1331 return $this->LiteralError;
1333 public function getLiteralErrorSec() {
1334 return $this->LiteralErrorSec;
1339 public function __construct($soapFault) {
1341 $faultCode = intval($soapFault->detail->Atributos->Estado->CodigoEstado);
1342 $faultString = $soapFault->detail->Atributos->Estado->LiteralError;
1345 parent::__construct($faultString, $faultCode, $soapFault);
1348 $this->CodigoEstado = $soapFault->detail->Atributos->Estado->CodigoEstado;
1349 $this->CodigoEstadoSecundario = $soapFault->detail->Atributos->Estado->CodigoEstadoSecundario;
1350 $this->LiteralError = $soapFault->detail->Atributos->Estado->LiteralError;
1351 $this->LiteralErrorSec = $soapFault->detail->Atributos->Estado->LiteralErrorSec;
1354 public function __toString() {
1355 return __CLASS__ .
": [{$this->code}] {$this->message}\n";
1369 if (($e instanceof \SoapFault) && ($e->faultcode ==
'soap-env:PAI')) {
static numberBaseConvert($numstring, $frombase=10, $tobase=16)
setPassPhrase($passphrase)
__construct($v_wsdl, $v_opciones=null)
loadTracertPAI($vTrazabilidadPAI)
__clienteOn($tipo, $trazabilidadPai=true, $opcionesClienteWS=null)
static getCertificateInfo($ruta, $formato=null)
tratarMTOMEstandar($response, $itemToReturn=null)
static makeWSClient($wsID, $v_wsdl, $v_opciones=null)
getCertificateSerial($set=true)