WSCOMUN  2.1.0
Web Services Comunes para PHP/GVHidra
PFAdviceServer.php
1 <?php
2 namespace PFAdviceServer;
3 require_once 'ComposerAdvicePortafirmas.php';
4 require_once 'WSComunSoapServer.php';
5 
7 
8  private $fnCallback = null;
9 
18  public static function createSoapServer($fnCallback, $serviceURL = true) {
19  // Prepara la URL del servidor
20  $currentURL = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]";
21  if (is_string($serviceURL)) {
22  $currentURL = $serviceURL;
23  }
24 
25  // Generacion interna de WSDL
26  if (isset($_REQUEST['wsdl'])) {
27  $baseWSDL = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'PFAdviceWS.wsdl');
28  $finalWSDL = str_replace('http://servidor/AdviceService', $currentURL, $baseWSDL);
29  ob_clean();
30  header('Content-type: application/xml');
31  echo $finalWSDL;
32  ob_flush();
33  die;
34  }
35 
36  // Generacion interna de type
37  if (isset($_REQUEST['types']) && ($serviceURL !== false)) {
38  $xsdContent = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'WebServiceTypes.xsd');
39  ob_clean();
40  header('Content-type: application/xml');
41  echo $xsdContent;
42  ob_flush();
43  die;
44  }
45 
46 
47  $opts = array(
48  'typemap' => array(
49  array(
50  'type_name' => 'userJob',
51  'type_ns' => 'urn:juntadeandalucia:cice:pfirma:type:v2.0',
52  'from_xml' => 'PFAdviceServer\PFAdviceSOAPServer::userJob_from_xml',
53  )
54  )
55  );
56 
57  $service = new PFAdviceSOAPServer('PFAdviceSOAPService');
58  $service->fnCallback = $fnCallback;
59 
60  //$server = new \SoapServer($currentURL . '?wsdl', $opts);
61  $server = new \WSComunSoapServer($currentURL . '?wsdl', $opts);
62  $server->setObject($service);
63  return $server;
64  }
65 
66  // Tratamiento especifico de clases especiales
67  public static function userJob_from_xml($xml) {
68  $sxe = simplexml_load_string($xml);
69 
70  // Crea objeto y establece datos comunes/obligatorios
71  $obj = new \stdClass();
72  $obj->identifier = (string) $sxe->identifier;
73 
74  // Establece el tipo exacto
75  $attributes = "@attributes";
76  if (isset($sxe->attributes('xsi', TRUE)->type)) {
77  $obj->$attributes = array('type' => (string) $sxe->attributes('xsi', TRUE)->type);
78  } elseif (isset($sxe->attributes()->type)) {
79  $obj->$attributes = array('type' => (string) $sxe->attributes()->type);
80  }
81 
82  $isUser = null;
83  if (SoapObject::endsWith((string) $sxe->attributes()->type, ':user')) {
84  $isUser = true;
85  } elseif (SoapObject::endsWith((string) $sxe->attributes()->type, ':job')) {
86  $isUser = false;
87  }
88 
89  // Tipologia especifica
90  if ($isUser) {
91  // Caso especifico: User
92  if (isset($sxe->name)) {
93  $obj->name = (string) $sxe->name;
94  }
95  if (isset($sxe->surname1)) {
96  $obj->surname1 = (string) $sxe->surname1;
97  }
98  if (isset($sxe->surname2)) {
99  $obj->surname2 = (string) $sxe->surname2;
100  }
101  } else {
102  // Caso especifico: Job
103  if (isset($sxe->description)) {
104  $obj->description = (string) $sxe->description;
105  }
106  }
107 
108  return $obj;
109  }
110 
111 
112 
120  private function objectTree2array($obj)
121  {
122  if (is_array($obj) || is_object($obj))
123  {
124  $result = array();
125  foreach ($obj as $key => $value)
126  {
127  $result[$key] = $this->objectTree2array($value);
128  }
129  return $result;
130  }
131  return $obj;
132  }
133 
134 
142  protected function __construct(){}
143 
144 
150  public function updateRequestStatus($oData)
151  {
152  // Comprobaciones previas
153  if (!isset($oData->authentication)) {
154  throw new \Exception('tag authentication required');
155  }
156  if (!isset($oData->request)) {
157  throw new \Exception('tag request required');
158  }
159 
160  // Transformacion a array
161  $vAuth = $this->objectTree2array($oData->authentication);
162  $vReq = $this->objectTree2array($oData->request);
163  $vSign = null;
164  if (isset($oData->signature)) {
165  $vSign = $this->objectTree2array($oData->signature);
166  }
167 
168  // Interpretacion en clases del composer
169  $oAuth = authentication::fromSoap($vAuth);
170  $oReq = request::fromSoap($vReq);
171  $oSign = null;
172  if (!is_null($vSign)) {
173  $oSign = signature::fromSoap($vSign);
174  }
175 
176  // Invocación de la funcion de Callback
177  $return = call_user_func($this->fnCallback, $oAuth, $oReq, $oSign);
178  if (is_bool($return)) {
179  return array('result' => $return);
180  }
181  if ($return instanceof \SoapFault) {
182  return $return;
183  }
184  }
185 
186 
196  static public function createExceptionInfo($identifier, $description) {
197  $encIden = utf8_encode($identifier);
198  $encDesc = utf8_encode($description);
199 
200  $array_details = array(
201  'identifier' => $encIden,
202  'description' => $encDesc
203  );
204  return new \SoapFault("Server", "[$encIden] $encDesc", null, $array_details, 'pfirmaException');
205  }
206 }
207 
208 ?>
static createExceptionInfo($identifier, $description)
static createSoapServer($fnCallback, $serviceURL=true)