WSCOMUN  2.0.0
Web Services Comunes para PHP/GVHidra
WSClientGVLogin.php
1 <?php
2 namespace WSCOMUN\GVLOGIN;
4 
17 {
31  public function obtenerContexto($codApp, $tokenSSO, $ip=null, $agent=null)
32  {
33  try
34  {
35  $opcionesClienteWS = array (
36  'soap_version' => SOAP_1_1, //SOAP 1.2
37  'user_agent' => 'WSSSoapClient',
38  'exceptions' => true,
39  'cache_wsdl' => self::$MYSOAPOP_WSDL_CACHE,
40  'trace' => self::$MYSOAPOP_TRACE
41  );
42 
43  $this->__clienteOn(self::KEYCLIENTE_GVLOGIN, true, $opcionesClienteWS);
44  $clienteWS = $this->__getClient(self::KEYCLIENTE_GVLOGIN);
45 
46 
47  // Valores por defecto
48  if (($ip == null) || empty($ip))
49  {
50  $ip = $this->getAgentIP();
51  }
52  if (($agent == null) || empty($agent))
53  {
54  $agent = $this->getUserAgent();
55  }
56 
57 
58  // Compone la peticion
59  $origenWS = new \ArrayObject();
60  $origenWS->append(new \SoapVar($ip, XSD_STRING, null, null,'ip', self::$NAMESPACE_LOGIN));
61  $origenWS->append(new \SoapVar($agent, XSD_STRING, null, null,'agent', self::$NAMESPACE_LOGIN));
62 
63  $mainWS = new \ArrayObject();
64  $mainWS->append(new \SoapVar($codApp, XSD_STRING, null, null,'aplicacion', self::$NAMESPACE_LOGIN));
65  $mainWS->append(new \SoapVar($tokenSSO, XSD_STRING, null, null,'tokenSSO', self::$NAMESPACE_LOGIN));
66  $mainWS->append(new \SoapVar($origenWS, SOAP_ENC_OBJECT,null,null,'origen', self::$NAMESPACE_LOGIN));
67 
68  $miSoapRQ = new \SoapVar($mainWS, SOAP_ENC_OBJECT,null,null,'obtenerContexto', self::$NAMESPACE_LOGIN);
69  $respuesta = $clienteWS->obtenerContexto($miSoapRQ);
70 
71  // Devuelve la respuesta, ya formateada en un objeto autonomo
72  return new LoginResponse($respuesta);
73  }
74  catch (\Exception $e)
75  {
76  return $this->tratarInnerException($clienteWS, $e);
77  }
78  }//obtenerContexto
79 
80 
81 
95  public function verificarContexto($tokenSSO, $ip=null, $agent=null)
96  {
97  try
98  {
99  $opcionesClienteWS = array (
100  'soap_version' => SOAP_1_1, //SOAP 1.2
101  'user_agent' => 'WSSSoapClient',
102  'exceptions' => true,
103  'cache_wsdl' => self::$MYSOAPOP_WSDL_CACHE,
104  'trace' => self::$MYSOAPOP_TRACE
105  );
106 
107  $this->__clienteOn(self::KEYCLIENTE_GVLOGIN, true, $opcionesClienteWS);
108  $clienteWS = $this->__getClient(self::KEYCLIENTE_GVLOGIN);
109 
110 
111  // Valores por defecto
112  if (($ip == null) || empty($ip))
113  {
114  $ip = $this->getAgentIP();
115  }
116  if (($agent == null) || empty($agent))
117  {
118  $agent = $this->getUserAgent();
119  }
120 
121 
122  // Compone la peticion
123  $origenWS = new \ArrayObject();
124  $origenWS->append(new \SoapVar($ip, XSD_STRING, null, null,'ip', self::$NAMESPACE_LOGIN));
125  $origenWS->append(new \SoapVar($agent, XSD_STRING, null, null,'agent', self::$NAMESPACE_LOGIN));
126 
127  $mainWS = new \ArrayObject();
128  $mainWS->append(new \SoapVar($tokenSSO, XSD_STRING, null, null,'tokenSSO', self::$NAMESPACE_LOGIN));
129  $mainWS->append(new \SoapVar($origenWS, SOAP_ENC_OBJECT,null,null,'origen', self::$NAMESPACE_LOGIN));
130 
131  $miSoapRQ = new \SoapVar($mainWS, SOAP_ENC_OBJECT,null,null,'verificarContexto', self::$NAMESPACE_LOGIN);
132  $respuesta = $clienteWS->verificarContexto($miSoapRQ);
133 
134  // Devuelve la respuesta, ya formateada en un objeto autonomo
135  return new LoginResponse($respuesta);
136  }
137  catch (\Exception $e)
138  {
139  return $this->tratarInnerException($clienteWS, $e);
140  }
141  }//verificarContexto
142 
143 
152  public function logout($tokenSSO)
153  {
154  try
155  {
156  $opcionesClienteWS = array (
157  'soap_version' => SOAP_1_1, //SOAP 1.2
158  'user_agent' => 'WSSSoapClient',
159  'exceptions' => true,
160  'cache_wsdl' => self::$MYSOAPOP_WSDL_CACHE,
161  'trace' => self::$MYSOAPOP_TRACE
162  );
163 
164  $this->__clienteOn(self::KEYCLIENTE_GVLOGIN, true, $opcionesClienteWS);
165  $clienteWS = $this->__getClient(self::KEYCLIENTE_GVLOGIN);
166 
167  $mainWS = new \ArrayObject();
168  $mainWS->append(new \SoapVar($tokenSSO, XSD_STRING, null, null,'tokenSSO', self::$NAMESPACE_LOGIN));
169 
170  $miSoapRQ = new \SoapVar($mainWS, SOAP_ENC_OBJECT,null,null,'logout', self::$NAMESPACE_LOGIN);
171  $respuesta = $clienteWS->logout($miSoapRQ);
172 
173  // Devuelve la respuesta, ya formateada en un objeto autonomo
174  return new LoginResponse($respuesta);
175  }
176  catch (\Exception $e)
177  {
178  return $this->tratarInnerException($clienteWS, $e);
179  }
180  }//logout
181 
182 
183 
184 
191  private function tratarInnerException($clienteWS, $e)
192  {
193  // Comprobamos si estamos ante excepcion MTOM
194  $mensaje = $e->getMessage();
195  $mensaje = trim(strtolower($mensaje));
196  switch ($mensaje)
197  {
198  case 'looks like we got no xml document' :
199  // Obtiene la respuesta
200  $response = $clienteWS->__getLastResponse();
201  $respuesta = $this->tratarMTOMEstandar($response);
202  return $respuesta;
203  break;
204 
205  default:
206  $this->tratarExcepcionEstandar($e, $clienteWS);
207  break;
208  }
209  }//tratarInnerException
210 
211 
217  private function getUserAgent()
218  {
219  if (isset($_SERVER['HTTP_X_USER_AGENT']))
220  return $_SERVER['HTTP_X_USER_AGENT'];
221  return $_SERVER['HTTP_USER_AGENT'];
222  }
223 
224 
229  private function getAgentIP()
230  {
231  $ip = '';
232  if (getenv('HTTP_CLIENT_IP'))
233  {
234  $ip = getenv('HTTP_CLIENT_IP');
235  }
236  else if(getenv('HTTP_X_FORWARDED_FOR'))
237  {
238  $ip = getenv('HTTP_X_FORWARDED_FOR');
239  }
240  else if (getenv('HTTP_X_FORWARDED'))
241  {
242  $ip = getenv('HTTP_X_FORWARDED');
243  }
244  else if(getenv('HTTP_FORWARDED_FOR'))
245  {
246  $ip = getenv('HTTP_FORWARDED_FOR');
247  }
248  else if(getenv('HTTP_FORWARDED'))
249  {
250  $ip = getenv('HTTP_FORWARDED');
251  }
252  else if(getenv('REMOTE_ADDR'))
253  {
254  $ip = getenv('REMOTE_ADDR');
255  }
256  else
257  {
258  $ip = 'UNKNOWN';
259  }
260  return $ip;
261  }//getAgentIP
262 
263 
268  private function getServerIP()
269  {
270  //return $_SERVER['REMOTE_ADDR'];
271  $ip = $_SERVER['SERVER_ADDR'];
272 
273  if (in_array(PHP_OS, array('WINNT', 'WIN32', 'Windows')))
274  {
275  $ip = getHostByName(getHostName());
276  }
277  elseif (in_array(PHP_OS, array('Linux', 'Unix')))
278  {
279  $command="/sbin/ifconfig";
280  $output = array();
281  exec($command, $output);
282  // var_dump($output);
283  $pattern = '/inet addr:?([^ ]+)/';
284 
285  $ip = array();
286  foreach ($output as $subject)
287  {
288  $subpattern = array();
289  $result = preg_match_all($pattern, $subject, $subpattern);
290  if ($result == 1)
291  {
292  if ($subpattern[1][0] != "127.0.0.1")
293  {
294  $ip = $subpattern[1][0];
295  }
296  }
297  //var_dump($subpattern);
298  }
299  }
300  return $ip;
301  }//getServerIP
302 
303 
304 }//Class WSClientGVLogin
305 
306 /* ----------------------------------------------------- */
307 /* --------- Clases de estructura del servicio --------- */
308 /* ----------------------------------------------------- */
310 {
311  private $resultado = false;
312  private $error = null;
313 
314  private $dni = null;
315  private $nombre = null;
316  private $apellido1 = null;
317  private $apellido2 = null;
318  private $mail = null;
319 
320  private $vParamInfoAmpliada = null;
321  private $vRoles = null;
322 
329  public function getResultado()
330  {
331  return $this->resultado;
332  }
333 
334 
341  public function hasError()
342  {
343  return is_null($this->error);
344  }
345 
346 
353  public function getError()
354  {
355  return $this->error;
356  }
357 
358 
365  public function getDni()
366  {
367  return $this->dni;
368  }
369 
376  public function getNombre()
377  {
378  return $this->nombre;
379  }
380 
387  public function getApellido1()
388  {
389  return $this->apellido1;
390  }
391 
398  public function getApellido2()
399  {
400  return $this->apellido2;
401  }
402 
409  public function getMail()
410  {
411  return $this->mail;
412  }
413 
414 
421  public function hasInfoAmpliada()
422  {
423  return ($this->sizeOfInfoAmpliada() > 0);
424  }
425 
426 
433  public function sizeOfInfoAmpliada()
434  {
435  if (is_null($this->vParamInfoAmpliada))
436  {
437  return 0;
438  }
439  return sizeof($this->vParamInfoAmpliada);
440  }
441 
449  public function getInfoAmpliada($index)
450  {
451  return $this->vParamInfoAmpliada[$index];
452  }
453 
454 
461  public function hasRoles()
462  {
463  return ($this->sizeOfRoles() > 0);
464  }
465 
466 
473  public function sizeOfRoles()
474  {
475  if (is_null($this->vRoles))
476  {
477  return 0;
478  }
479  return sizeof($this->vRoles);
480  }
481 
489  public function getRoles($index)
490  {
491  return $this->vRoles[$index];
492  }
493 
494 
495 
502  public function __construct($response)
503  {
504  $this->resultado = $response->resultado;
505 
506  if (($this->resultado == true) && isset($response->datos))
507  {
508  // Intenta capturar el resto de datos
509  $datos = $response->datos;
510 
511  // Datos basicos
512  if (isset($datos->dni))
513  {
514  $this->dni = $datos->dni;
515  }
516  if (isset($datos->nombre))
517  {
518  $this->nombre = $datos->nombre;
519  }
520  if (isset($datos->apellido1))
521  {
522  $this->apellido1 = $datos->apellido1;
523  }
524  if (isset($datos->apellido2))
525  {
526  $this->apellido2 = $datos->apellido2;
527  }
528  if (isset($datos->mail))
529  {
530  $this->mail = $datos->mail;
531  }
532 
533  // InfoAmpliada
534  if (isset($datos->infoAmpliada) && isset($datos->infoAmpliada->parametro))
535  {
536  // Normaliza asegurandose de que llega un array
537  $vParamInfoAmpliada = $datos->infoAmpliada->parametro;
538  if (!is_array($vParamInfoAmpliada)) $vParamInfoAmpliada = array($vParamInfoAmpliada);
539 
540  // Almacena los parametros
541  for ($iParam=0; $iParam < sizeof($vParamInfoAmpliada); $iParam++)
542  {
543  $param = $vParamInfoAmpliada[$iParam];
544  $this->vParamInfoAmpliada[] = new LoginParametro($param->nombreParametro, $param->valorParametro);
545  }
546  }
547 
548  // Roles
549  if (isset($datos->roles) && isset($datos->roles->role))
550  {
551  // Normaliza asegurandose de que llega un array
552  $vRoles = $datos->roles->role;
553  if (!is_array($vRoles)) $vRoles = array($vRoles);
554 
555  // Almacena los roles y crea el array de roles
556  $this->vRoles = array();
557  for ($iRol=0; $iRol < sizeof($vRoles); $iRol++)
558  {
559  $this->vRoles[] = new LoginRol( $vRoles[$iRol] );
560  }
561  }
562  }
563  else
564  {
565  if (!empty($response->error))
566  {
567  $this->error = new BasicError($response->error);
568  }
569  }
570  }
571 }
572 
574 {
575 
576  private $nombre = null;
577  private $valor = null;
578 
585  public function getNombre() {
586  return $this->nombre;
587  }
588 
595  public function getValor() {
596  return $this->valor;
597  }
598 
599 
606  public function __construct($nombre, $valor) {
607  $this->nombre = $nombre;
608  $this->valor = $valor;
609  }
610 }
611 
612 class LoginRol
613 {
614 
615  private $codigo = null;
616  private $vParametro = null;
617 
624  public function getCodigo() {
625  return $this->codigo;
626  }
627 
634  public function hasParametro() {
635  return ($this->sizeOfParametro() > 0);
636  }
637 
638 
645  public function sizeOfParametro() {
646  if (is_null($this->vParametro)) {
647  return 0;
648  }
649  return sizeof($this->vParametro);
650  }
651 
659  public function getParametro($index) {
660  return $this->vParametro[$index];
661  }
662 
663 
670  public function __construct($datos) {
671  // Datos basicos
672  if (isset($datos->codigo)) {
673  $this->codigo = $datos->codigo;
674  }
675 
676  // Parametros
677  if (isset($datos->parametros) && isset($datos->parametros->parametro)) {
678  // Normaliza asegurandose de que llega un array
679  $vParam = $datos->parametros->parametro;
680  if (!is_array($vParam)) $vParam = array($vParam);
681 
682  // Almacena los parametros y crea el array de parametros
683  $this->vParametro = array();
684 
685  for ($iParam=0; $iParam < sizeof($vParam); $iParam++) {
686  $param = $vParam[$iParam];
687  $this->vParametro[] = new LoginParametro($param->nombreParametro, $param->valorParametro);
688  }
689  }
690  }
691 }
692 
693 
694 
696 {
697 
698 /*
699 001 - Token no Existente - No existe el token.
700 002 - Aplicacion no Existente - La aplicación no existe en el sistema.
701 003 - Token Caducado - El token ha caducado
702 004 - El usuario no existe en CLAU - No existe el usuario en el sistema CLAU
703 005 - El origen no coincide con el esperado - El origen no coincide con el esperado.
704 006 - Componente de postProcesamiento no encontrado - No se ha encontrado el componente para el tipo de postprocesamiento
705 007 - El usuario no esta activo en CLAU - El usuario no está activo en el sistema CLAU
706 008 - No se ha encontrado el componente para el tipo de autorizacion - No se ha encontrado el componente para el tipo de autorización
707 009 - El usuario no ha sido autorizado por Boperit - El usuario no ha sido autorizado por Boperit
708 010 - Error al invocar a los servicios de autorizacion - Error al invocar a los servicios de autorizacion.
709 011 - El usuario no tiene roles para la aplicacion - El usuario no tiene roles para la aplicacion
710 012 - Error al invocar a los servicios de postprocesamiento - Error al invocar a los servicios de postprocesamiento
711 013 - Aplicacion No Cumple Nivel Minimo Seguridad - La aplicación no cumple el nivel mínimo de seguridad configurado para su acceso
712 GV-999 - Error Inesperado - Error genérico
713 */
714 
715  const TOKEN_NO_EXISTENTE = '001';
716  const APLICACION_NO_EXISTENTE = '002';
717  const TOKEN_CADUCADO = '003';
718  const USUARIO_NO_EXISTE_EN_CLAU = '004';
719  const ORIGEN_NO_ESPERADO = '005';
720  const COMPONENTE_PROSPROCESAMIENTO_NO_ENCONTRADO = '006';
721  const USUARIO_NO_ACTIVO_EN_CLAU = '007';
722  const COMPONENTE_AUTORIZACION_NO_ENCONTRADO = '008';
723  const USUARIO_NO_AUTORIZADO_BOPERIT = '009';
724  const ERROR_INVOCANDO_AUTORIZACION = '010';
725  const USUARIO_SIN_ROLES = '011';
726  const ERROR_INVOCANDO_POSTPROCESAMIENTO = '012';
727  const APLICION_NO_CUMPLE_SEGURIDAD_MINIMA = '013';
728  const ERROR_INESPERADO = '999'; // GV-999
729 
730 
731  private $codigoError = '';
732  private $mensajeError = '';
733 
734 
735  public function getCodigoError()
736  {
737  return $this->codigoError;
738  }
739  public function getMensajeError()
740  {
741  return $this->mensajeError;
742  }
743 
744 
745 
746  public function __construct($objError)
747  {
748  $this->codigoError = $objError->codigoError;
749  $this->mensajeError = $objError->mensajeError;
750  }
751 
752  public function __toString()
753  {
754  return __CLASS__ . ": [{$this->codigoError}] {$this->mensajeError}\n";
755  }
756 
757 }
758 
759 ?>
verificarContexto($tokenSSO, $ip=null, $agent=null)
__construct($v_wsdl, $v_opciones=null)
__clienteOn($tipo, $trazabilidadPai=true, $opcionesClienteWS=null)
obtenerContexto($codApp, $tokenSSO, $ip=null, $agent=null)
tratarMTOMEstandar($response, $itemToReturn=null)