Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Server.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Server/Interface.php';
26 
28 #require_once 'Zend/Xml/Security.php';
29 
31 #require_once 'Zend/Xml/Exception.php';
32 
45 {
50  protected $_actor;
51 
56  protected $_class;
57 
62  protected $_classArgs = array();
63 
67  protected $_object;
68 
73  protected $_classmap;
74 
79  protected $_encoding;
80 
86  protected $_features;
87 
93  protected $_wsdlCache;
94 
100  protected $_wsiCompliant;
101 
106  protected $_faultExceptions = array();
107 
113  protected $_functions = array();
114 
119  protected $_persistence;
120 
125  protected $_request;
126 
131  protected $_response;
132 
138  protected $_returnResponse = false;
139 
144  protected $_soapVersion = SOAP_1_2;
145 
150  protected $_wsdl;
151 
156  protected $_uri;
157 
172  public function __construct($wsdl = null, array $options = null)
173  {
174  if (!extension_loaded('soap')) {
175  #require_once 'Zend/Soap/Server/Exception.php';
176  throw new Zend_Soap_Server_Exception('SOAP extension is not loaded.');
177  }
178 
179  if (null !== $wsdl) {
180  $this->setWsdl($wsdl);
181  }
182 
183  if (null !== $options) {
184  $this->setOptions($options);
185  }
186  }
187 
196  public function setOptions($options)
197  {
198  if($options instanceof Zend_Config) {
199  $options = $options->toArray();
200  }
201 
202  foreach ($options as $key => $value) {
203  switch ($key) {
204  case 'actor':
205  $this->setActor($value);
206  break;
207  case 'classmap':
208  case 'classMap':
209  $this->setClassmap($value);
210  break;
211  case 'encoding':
212  $this->setEncoding($value);
213  break;
214  case 'soapVersion':
215  case 'soap_version':
216  $this->setSoapVersion($value);
217  break;
218  case 'uri':
219  $this->setUri($value);
220  break;
221  case 'wsdl':
222  $this->setWsdl($value);
223  break;
224  case 'featues':
225  trigger_error(__METHOD__ . ': the option "featues" is deprecated as of 1.10.x and will be removed with 2.0.0; use "features" instead', E_USER_NOTICE);
226  case 'features':
227  $this->setSoapFeatures($value);
228  break;
229  case 'cache_wsdl':
230  $this->setWsdlCache($value);
231  break;
232  case 'wsi_compliant':
233  $this->setWsiCompliant($value);
234  break;
235  default:
236  break;
237  }
238  }
239 
240  return $this;
241  }
242 
248  public function getOptions()
249  {
250  $options = array();
251  if (null !== $this->_actor) {
252  $options['actor'] = $this->_actor;
253  }
254 
255  if (null !== $this->_classmap) {
256  $options['classmap'] = $this->_classmap;
257  }
258 
259  if (null !== $this->_encoding) {
260  $options['encoding'] = $this->_encoding;
261  }
262 
263  if (null !== $this->_soapVersion) {
264  $options['soap_version'] = $this->_soapVersion;
265  }
266 
267  if (null !== $this->_uri) {
268  $options['uri'] = $this->_uri;
269  }
270 
271  if (null !== $this->_features) {
272  $options['features'] = $this->_features;
273  }
274 
275  if (null !== $this->_wsdlCache) {
276  $options['cache_wsdl'] = $this->_wsdlCache;
277  }
278 
279  if (null !== $this->_wsiCompliant) {
280  $options['wsi_compliant'] = $this->_wsiCompliant;
281  }
282 
283  return $options;
284  }
291  public function setWsiCompliant($value)
292  {
293  if (is_bool($value)) {
294  $this->_wsiCompliant = $value;
295  }
296  return $this;
297  }
303  public function getWsiCompliant()
304  {
305  return $this->_wsiCompliant;
306  }
314  public function setEncoding($encoding)
315  {
316  if (!is_string($encoding)) {
317  #require_once 'Zend/Soap/Server/Exception.php';
318  throw new Zend_Soap_Server_Exception('Invalid encoding specified');
319  }
320 
321  $this->_encoding = $encoding;
322  return $this;
323  }
324 
330  public function getEncoding()
331  {
332  return $this->_encoding;
333  }
334 
342  public function setSoapVersion($version)
343  {
344  if (!in_array($version, array(SOAP_1_1, SOAP_1_2))) {
345  #require_once 'Zend/Soap/Server/Exception.php';
346  throw new Zend_Soap_Server_Exception('Invalid soap version specified');
347  }
348 
349  $this->_soapVersion = $version;
350  return $this;
351  }
352 
358  public function getSoapVersion()
359  {
360  return $this->_soapVersion;
361  }
362 
370  public function validateUrn($urn)
371  {
372  $scheme = parse_url($urn, PHP_URL_SCHEME);
373  if ($scheme === false || $scheme === null) {
374  #require_once 'Zend/Soap/Server/Exception.php';
375  throw new Zend_Soap_Server_Exception('Invalid URN');
376  }
377 
378  return true;
379  }
380 
389  public function setActor($actor)
390  {
391  $this->validateUrn($actor);
392  $this->_actor = $actor;
393  return $this;
394  }
395 
401  public function getActor()
402  {
403  return $this->_actor;
404  }
405 
415  public function setUri($uri)
416  {
417  $this->validateUrn($uri);
418  $this->_uri = $uri;
419  return $this;
420  }
421 
427  public function getUri()
428  {
429  return $this->_uri;
430  }
431 
439  public function setClassmap($classmap)
440  {
441  if (!is_array($classmap)) {
445  #require_once 'Zend/Soap/Server/Exception.php';
446  throw new Zend_Soap_Server_Exception('Classmap must be an array');
447  }
448  foreach ($classmap as $type => $class) {
449  if (!class_exists($class)) {
453  #require_once 'Zend/Soap/Server/Exception.php';
454  throw new Zend_Soap_Server_Exception('Invalid class in class map');
455  }
456  }
457 
458  $this->_classmap = $classmap;
459  return $this;
460  }
461 
467  public function getClassmap()
468  {
469  return $this->_classmap;
470  }
471 
478  public function setWsdl($wsdl)
479  {
480  $this->_wsdl = $wsdl;
481  return $this;
482  }
483 
489  public function getWsdl()
490  {
491  return $this->_wsdl;
492  }
493 
500  public function setSoapFeatures($feature)
501  {
502  $this->_features = $feature;
503  return $this;
504  }
505 
511  public function getSoapFeatures()
512  {
513  return $this->_features;
514  }
515 
522  public function setWsdlCache($options)
523  {
524  $this->_wsdlCache = $options;
525  return $this;
526  }
527 
531  public function getWsdlCache()
532  {
533  return $this->_wsdlCache;
534  }
535 
545  public function addFunction($function, $namespace = '')
546  {
547  // Bail early if set to SOAP_FUNCTIONS_ALL
548  if ($this->_functions == SOAP_FUNCTIONS_ALL) {
549  return $this;
550  }
551 
552  if (is_array($function)) {
553  foreach ($function as $func) {
554  if (is_string($func) && function_exists($func)) {
555  $this->_functions[] = $func;
556  } else {
557  #require_once 'Zend/Soap/Server/Exception.php';
558  throw new Zend_Soap_Server_Exception('One or more invalid functions specified in array');
559  }
560  }
561  $this->_functions = array_merge($this->_functions, $function);
562  } elseif (is_string($function) && function_exists($function)) {
563  $this->_functions[] = $function;
564  } elseif ($function == SOAP_FUNCTIONS_ALL) {
565  $this->_functions = SOAP_FUNCTIONS_ALL;
566  } else {
567  #require_once 'Zend/Soap/Server/Exception.php';
568  throw new Zend_Soap_Server_Exception('Invalid function specified');
569  }
570 
571  if (is_array($this->_functions)) {
572  $this->_functions = array_unique($this->_functions);
573  }
574 
575  return $this;
576  }
577 
591  public function setClass($class, $namespace = '', $argv = null)
592  {
593  if (isset($this->_class)) {
594  #require_once 'Zend/Soap/Server/Exception.php';
595  throw new Zend_Soap_Server_Exception('A class has already been registered with this soap server instance');
596  }
597 
598  if (!is_string($class)) {
599  #require_once 'Zend/Soap/Server/Exception.php';
600  throw new Zend_Soap_Server_Exception('Invalid class argument (' . gettype($class) . ')');
601  }
602 
603  if (!class_exists($class)) {
604  #require_once 'Zend/Soap/Server/Exception.php';
605  throw new Zend_Soap_Server_Exception('Class "' . $class . '" does not exist');
606  }
607 
608  $this->_class = $class;
609  if (1 < func_num_args()) {
610  $argv = func_get_args();
611  array_shift($argv);
612  $this->_classArgs = $argv;
613  }
614 
615  return $this;
616  }
617 
626  public function setObject($object)
627  {
628  if(!is_object($object)) {
629  #require_once 'Zend/Soap/Server/Exception.php';
630  throw new Zend_Soap_Server_Exception('Invalid object argument ('.gettype($object).')');
631  }
632 
633  if(isset($this->_object)) {
634  #require_once 'Zend/Soap/Server/Exception.php';
635  throw new Zend_Soap_Server_Exception('An object has already been registered with this soap server instance');
636  }
637 
638  if ($this->_wsiCompliant) {
639  #require_once 'Zend/Soap/Server/Proxy.php';
640  $this->_object = new Zend_Soap_Server_Proxy($object);
641  } else {
642  $this->_object = $object;
643  }
644 
645  return $this;
646  }
647 
658  public function getFunctions()
659  {
660  $functions = array();
661  if (null !== $this->_class) {
662  $functions = get_class_methods($this->_class);
663  } elseif (null !== $this->_object) {
664  $functions = get_class_methods($this->_object);
665  }
666 
667  return array_merge((array) $this->_functions, $functions);
668  }
669 
677  public function loadFunctions($definition)
678  {
679  #require_once 'Zend/Soap/Server/Exception.php';
680  throw new Zend_Soap_Server_Exception('Unimplemented');
681  }
682 
689  public function setPersistence($mode)
690  {
691  if (!in_array($mode, array(SOAP_PERSISTENCE_SESSION, SOAP_PERSISTENCE_REQUEST))) {
692  #require_once 'Zend/Soap/Server/Exception.php';
693  throw new Zend_Soap_Server_Exception('Invalid persistence mode specified');
694  }
695 
696  $this->_persistence = $mode;
697  return $this;
698  }
699 
705  public function getPersistence()
706  {
707  return $this->_persistence;
708  }
709 
723  protected function _setRequest($request)
724  {
725  if ($request instanceof DOMDocument) {
726  $xml = $request->saveXML();
727  } elseif ($request instanceof DOMNode) {
728  $xml = $request->ownerDocument->saveXML();
729  } elseif ($request instanceof SimpleXMLElement) {
730  $xml = $request->asXML();
731  } elseif (is_object($request) || is_string($request)) {
732  if (is_object($request)) {
733  $xml = $request->__toString();
734  } else {
735  $xml = $request;
736  }
737 
738  $dom = new DOMDocument();
739  try {
740  if(strlen($xml) == 0 || (!$dom = Zend_Xml_Security::scan($xml, $dom))) {
741  #require_once 'Zend/Soap/Server/Exception.php';
742  throw new Zend_Soap_Server_Exception('Invalid XML');
743  }
744  } catch (Zend_Xml_Exception $e) {
745  #require_once 'Zend/Soap/Server/Exception.php';
746  throw new Zend_Soap_Server_Exception(
747  $e->getMessage()
748  );
749  }
750  }
751  $this->_request = $xml;
752  return $this;
753  }
754 
760  public function getLastRequest()
761  {
762  return $this->_request;
763  }
764 
776  public function setReturnResponse($flag)
777  {
778  $this->_returnResponse = ($flag) ? true : false;
779  return $this;
780  }
781 
787  public function getReturnResponse()
788  {
789  return $this->_returnResponse;
790  }
791 
797  public function getLastResponse()
798  {
799  return $this->_response;
800  }
801 
811  protected function _getSoap()
812  {
813  $options = $this->getOptions();
814  $server = new SoapServer($this->_wsdl, $options);
815 
816  if (!empty($this->_functions)) {
817  $server->addFunction($this->_functions);
818  }
819 
820  if (!empty($this->_class)) {
821  $args = $this->_classArgs;
822  array_unshift($args, $this->_class);
823  if ($this->_wsiCompliant) {
824  #require_once 'Zend/Soap/Server/Proxy.php';
825  array_unshift($args, 'Zend_Soap_Server_Proxy');
826  }
827  call_user_func_array(array($server, 'setClass'), $args);
828  }
829 
830  if (!empty($this->_object)) {
831  $server->setObject($this->_object);
832  }
833 
834  if (null !== $this->_persistence) {
835  $server->setPersistence($this->_persistence);
836  }
837 
838  return $server;
839  }
840 
860  public function handle($request = null)
861  {
862  if (null === $request) {
863  $request = file_get_contents('php://input');
864  }
865 
866  // Set Zend_Soap_Server error handler
867  $displayErrorsOriginalState = $this->_initializeSoapErrorContext();
868 
869  $setRequestException = null;
873  #require_once 'Zend/Soap/Server/Exception.php';
874  try {
875  $this->_setRequest($request);
876  } catch (Zend_Soap_Server_Exception $e) {
877  $setRequestException = $e;
878  }
879 
880  $soap = $this->_getSoap();
881 
882  $fault = false;
883  ob_start();
884  if ($setRequestException instanceof Exception) {
885  // Create SOAP fault message if we've caught a request exception
886  $fault = $this->fault($setRequestException->getMessage(), 'Sender');
887  } else {
888  try {
889  $soap->handle($this->_request);
890  } catch (Exception $e) {
891  $fault = $this->fault($e);
892  }
893  }
894  $this->_response = ob_get_clean();
895 
896  // Restore original error handler
897  restore_error_handler();
898  ini_set('display_errors', $displayErrorsOriginalState);
899 
900  // Send a fault, if we have one
901  if ($fault) {
902  $soap->fault($fault->faultcode, $fault->faultstring);
903  }
904 
905  if (!$this->_returnResponse) {
906  echo $this->_response;
907  return;
908  }
909 
910  return $this->_response;
911  }
912 
918  protected function _initializeSoapErrorContext()
919  {
920  $displayErrorsOriginalState = ini_get('display_errors');
921  ini_set('display_errors', false);
922  set_error_handler(array($this, 'handlePhpErrors'), E_USER_ERROR);
923  return $displayErrorsOriginalState;
924  }
925 
933  {
934  $this->_faultExceptions = array_merge($this->_faultExceptions, (array) $class);
935  return $this;
936  }
937 
945  {
946  if (in_array($class, $this->_faultExceptions, true)) {
947  $index = array_search($class, $this->_faultExceptions);
948  unset($this->_faultExceptions[$index]);
949  return true;
950  }
951 
952  return false;
953  }
954 
960  public function getFaultExceptions()
961  {
963  }
964 
979  public function fault($fault = null, $code = "Receiver")
980  {
981  if ($fault instanceof Exception) {
982  $class = get_class($fault);
983  if (in_array($class, $this->_faultExceptions)) {
984  $message = $fault->getMessage();
985  $eCode = $fault->getCode();
986  $code = empty($eCode) ? $code : $eCode;
987  } else {
988  $message = 'Unknown error';
989  }
990  } elseif(is_string($fault)) {
991  $message = $fault;
992  } else {
993  $message = 'Unknown error';
994  }
995 
996  $allowedFaultModes = array(
997  'VersionMismatch', 'MustUnderstand', 'DataEncodingUnknown',
998  'Sender', 'Receiver', 'Server'
999  );
1000  if(!in_array($code, $allowedFaultModes)) {
1001  $code = "Receiver";
1002  }
1003 
1004  return new SoapFault($code, $message);
1005  }
1006 
1018  public function handlePhpErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
1019  {
1020  throw $this->fault($errstr, "Receiver");
1021  }
1022 }
__construct($wsdl=null, array $options=null)
Definition: Server.php:172
setObject($object)
Definition: Server.php:626
ini_set($varName, $newValue)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
registerFaultException($class)
Definition: Server.php:932
static scan($xml, DOMDocument $dom=null)
Definition: Security.php:71
setEncoding($encoding)
Definition: Server.php:314
handlePhpErrors($errno, $errstr, $errfile=null, $errline=null, array $errcontext=null)
Definition: Server.php:1018
$message
handle($request=null)
Definition: Server.php:860
deregisterFaultException($class)
Definition: Server.php:944
setClassmap($classmap)
Definition: Server.php:439
_setRequest($request)
Definition: Server.php:723
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
setWsdl($wsdl)
Definition: Server.php:478
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
loadFunctions($definition)
Definition: Server.php:677
validateUrn($urn)
Definition: Server.php:370
setReturnResponse($flag)
Definition: Server.php:776
setSoapVersion($version)
Definition: Server.php:342
fault($fault=null, $code="Receiver")
Definition: Server.php:979
setSoapFeatures($feature)
Definition: Server.php:500
addFunction($function, $namespace='')
Definition: Server.php:545
setClass($class, $namespace='', $argv=null)
Definition: Server.php:591
setActor($actor)
Definition: Server.php:389
setWsdlCache($options)
Definition: Server.php:522
setOptions($options)
Definition: Server.php:196
$index
Definition: list.phtml:44
setWsiCompliant($value)
Definition: Server.php:291
setPersistence($mode)
Definition: Server.php:689
$code
Definition: info.phtml:12
_initializeSoapErrorContext()
Definition: Server.php:918