12 require_once(
'XMLSecLibs/WSSESoap.php');
38 protected $keyFile=
'';
44 protected $passPhrase=
'';
50 protected $certFile=
'';
55 private $conTrazabilidadPAI =
true;
60 private $enableWSSecurity =
true;
65 protected $nsCert = null;
70 protected $idApp = null;
76 protected $mtom =
false;
94 $this->conTrazabilidadPAI =
true;
95 $this->enableWSSecurity =
true;
97 if (is_array($vOptions))
99 $vOptions[
'trace'] =
true;
103 $vOptions = array(
'trace' =>
true);
106 return parent::__construct($wsdl, $vOptions);
118 $this->keyFile = $pathKey;
130 return($this->keyFile);
142 $this->passPhrase = $passphrase;
154 return($this->passPhrase);
166 $this->certFile = $pathCert;
177 return($this->certFile);
189 if (!is_array($vCertData))
190 throw new \Exception(
'La información debe venir como array asociativo');
192 if (array_key_exists(
'certFile', $vCertData))
194 $this->certFile = $vCertData[
'certFile'];
198 throw new \Exception(
'El array asociativo debe contener la ruta al certificado');
201 if (array_key_exists(
'keyFile', $vCertData))
203 $this->keyFile = $vCertData[
'keyFile'];
207 throw new \Exception(
'El array asociativo debe contener la ruta al fichero de clave');
210 if (array_key_exists(
'passPhrase', $vCertData))
212 $this->passPhrase = $vCertData[
'passPhrase'];
242 if (!is_array($vTrazabilidadPAI))
244 throw new \Exception(
'Las opciones de trazabilidad deben ser un array asociativo');
247 if (array_key_exists(
'idApp', $vTrazabilidadPAI))
249 $this->idApp = $vTrazabilidadPAI[
'idApp'];
253 throw new \Exception(
'El array asociativo debe contener el ID de la aplicación que consume el WS');
256 if ($this->enableWSSecurity)
258 $this->getCertificateSerial();
271 if (empty ($vTrazabilidadPAI))
273 if (empty($this->idApp))
275 throw new \Exception(__CLASS__.
":: Debe fijarse el idApp");
278 if ($this->enableWSSecurity())
280 if(empty($this->nsCert))
282 $this->nsCert = $this->getCertificateSerial();
290 $this->loadTrazabilidad($vTrazabilidadPAI);
297 $this->conTrazabilidadPAI =
true;
307 $this->conTrazabilidadPAI =
false;
318 $this->enableWSSecurity =
true;
328 $this->enableWSSecurity =
false;
341 $certFile = realpath($this->certFile);
342 $cert = file_get_contents($certFile);
345 throw new \Exception(__FILE__.
'::'.__CLASS__.
" - No puedo leere el contenido de $certFile ");
347 $v_certData = openssl_x509_parse($cert,
true);
348 $this->nsCert = strtoupper(self::numberBaseConvert($v_certData[
'serialNumber']));
349 return $this->nsCert;
364 private static function numberBaseConvert($numstring, $frombase=10, $tobase=16)
366 $chars =
"0123456789abcdefghijklmnopqrstuvwxyz";
367 $tostring = substr($chars, 0, $tobase);
368 $length = strlen($numstring);
371 for ($i = 0; $i < $length; $i++)
373 $number[$i] = strpos($chars, $numstring{$i});
379 for ($i = 0; $i < $length; $i++)
381 $divide = $divide * $frombase + $number[$i];
382 if ($divide >= $tobase)
384 $number[$newlen++] = (int)($divide / $tobase);
385 $divide = $divide % $tobase;
386 } elseif ($newlen > 0)
388 $number[$newlen++] = 0;
392 $result = $tostring{$divide} . $result;
393 }
while ($newlen != 0);
406 private function getPAITraceToken()
408 $t = microtime(
true);
409 $micro = sprintf(
"%06d",($t - floor($t)) * 1000000);
410 $date = new \DateTime( date(
'Y-m-d H:i:s.'.$micro, $t));
411 $token = $this->nsCert.
'-'.$this->idApp.
'-'.substr($date->format(
'YmdHisu'), 0, 17);
427 public function __doRequest($newRequest, $location, $action, $version, $one_way = null)
429 if ($this->enableWSSecurity==
true)
432 $doc = new \DOMDocument(
'1.0');
435 $doc->loadXML($newRequest);
438 $objWSSE =
new WSSESoap($doc,
false,
false);
441 $objKey =
new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array(
'type'=>
'private'));
443 $objKey->passphrase = $this->getPassPhrase();
445 $objKey->loadKey($this->getKeyFile(),
true);
448 $objWSSE->signSoapDoc($objKey);
451 $token = $objWSSE->addBinaryToken(file_get_contents($this->getCertFile()),
true);
453 $objWSSE->attachTokentoSig($token);
456 $objWSSE->addTimestamp(3600);
459 $newRequest = $objWSSE->saveXML();
462 if ($this->conTrazabilidadPAI)
464 $dom = new \DOMDocument(
'1.0');
465 $dom->loadXML($newRequest);
466 $xpath = new \DOMXpath($dom);
467 $headers = $xpath->query(
"/*[local-name()='Envelope']/*[local-name()='Header']");
469 if (($headers->length) > 0)
471 $header = $headers->item(0);
475 $envelop = $dom->firstChild;
476 $prefijo = $dom->lookupPrefix (
'http://schemas.xmlsoap.org/soap/envelope/');
477 $header = $dom->createElementNS (
'http://schemas.xmlsoap.org/soap/envelope/',$prefijo.
':Header',
'');
478 $body = $envelop->firstChild;
479 $header = $envelop->insertBefore($header, $body);
482 $nodoTraza = $dom->createElementNS(
'http://dgti.gva.es/interoperabilidad',
'Id_trazabilidad');
483 $nodoTraza->appendChild(
new \DOMText( (
string) $this->getPAITraceToken()));
484 $header->appendChild($nodoTraza);
485 unset($newRequest);$newRequest=null;gc_collect_cycles();
486 $newRequest = $dom->saveXML();
490 $response = parent::__doRequest($newRequest, $location, $action, $version, $one_way);
493 if (strpos($response,
"Content-Type: application/xop+xml") ===
false)
loadTracertPAI($vTrazabilidadPAI)
enablePAITrace___($vTrazabilidadPAI=null)
__construct($wsdl, $vOptions=null)
__doRequest($newRequest, $location, $action, $version, $one_way=null)
setPassPhrase($passphrase)