[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Soap/ -> Server.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Soap
  17   * @subpackage Server
  18   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   */
  21  
  22  /**
  23   * @see Zend_Server_Interface
  24   */
  25  require_once 'Zend/Server/Interface.php';
  26  
  27  /**
  28   * Zend_Soap_Server
  29   *
  30   * @category   Zend
  31   * @package    Zend_Soap
  32   * @subpackage Server
  33   * @uses       Zend_Server_Interface
  34   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  36   * @version    $Id$
  37   */
  38  class Zend_Soap_Server implements Zend_Server_Interface
  39  {
  40      /**
  41       * Actor URI
  42       * @var string URI
  43       */
  44      protected $_actor;
  45  
  46      /**
  47       * Class registered with this server
  48       * @var string
  49       */
  50      protected $_class;
  51  
  52      /**
  53       * Arguments to pass to {@link $_class} constructor
  54       * @var array
  55       */
  56      protected $_classArgs = array();
  57  
  58      /**
  59       * Object registered with this server
  60       */
  61      protected $_object;
  62  
  63      /**
  64       * Array of SOAP type => PHP class pairings for handling return/incoming values
  65       * @var array
  66       */
  67      protected $_classmap;
  68  
  69      /**
  70       * Encoding
  71       * @var string
  72       */
  73      protected $_encoding;
  74  
  75      /**
  76       * SOAP Server Features
  77       *
  78       * @var int
  79       */
  80      protected $_features;
  81  
  82      /**
  83       * WSDL Caching Options of SOAP Server
  84       *
  85       * @var mixed
  86       */
  87      protected $_wsdlCache;
  88  
  89  
  90      /**
  91       * Registered fault exceptions
  92       * @var array
  93       */
  94      protected $_faultExceptions = array();
  95  
  96      /**
  97       * Functions registered with this server; may be either an array or the SOAP_FUNCTIONS_ALL
  98       * constant
  99       * @var array|int
 100       */
 101      protected $_functions = array();
 102  
 103      /**
 104       * Persistence mode; should be one of the SOAP persistence constants
 105       * @var int
 106       */
 107      protected $_persistence;
 108  
 109      /**
 110       * Request XML
 111       * @var string
 112       */
 113      protected $_request;
 114  
 115      /**
 116       * Response XML
 117       * @var string
 118       */
 119      protected $_response;
 120  
 121      /**
 122       * Flag: whether or not {@link handle()} should return a response instead
 123       * of automatically emitting it.
 124       * @var boolean
 125       */
 126      protected $_returnResponse = false;
 127  
 128      /**
 129       * SOAP version to use; SOAP_1_2 by default, to allow processing of headers
 130       * @var int
 131       */
 132      protected $_soapVersion = SOAP_1_2;
 133  
 134      /**
 135       * URI or path to WSDL
 136       * @var string
 137       */
 138      protected $_wsdl;
 139  
 140      /**
 141       * URI namespace for SOAP server
 142       * @var string URI
 143       */
 144      protected $_uri;
 145  
 146      /**
 147       * Constructor
 148       *
 149       * Sets display_errors INI setting to off (prevent client errors due to bad
 150       * XML in response). Registers {@link handlePhpErrors()} as error handler
 151       * for E_USER_ERROR.
 152       *
 153       * If $wsdl is provided, it is passed on to {@link setWsdl()}; if any
 154       * options are specified, they are passed on to {@link setOptions()}.
 155       *
 156       * @param string $wsdl
 157       * @param array $options
 158       * @return void
 159       */
 160      public function __construct($wsdl = null, array $options = null)
 161      {
 162          if (!extension_loaded('soap')) {
 163              require_once 'Zend/Soap/Server/Exception.php';
 164              throw new Zend_Soap_Server_Exception('SOAP extension is not loaded.');
 165          }
 166  
 167          if (null !== $wsdl) {
 168              $this->setWsdl($wsdl);
 169          }
 170  
 171          if (null !== $options) {
 172              $this->setOptions($options);
 173          }
 174      }
 175  
 176      /**
 177       * Set Options
 178       *
 179       * Allows setting options as an associative array of option => value pairs.
 180       *
 181       * @param  array|Zend_Config $options
 182       * @return Zend_Soap_Server
 183       */
 184      public function setOptions($options)
 185      {
 186          if($options instanceof Zend_Config) {
 187              $options = $options->toArray();
 188          }
 189  
 190          foreach ($options as $key => $value) {
 191              switch ($key) {
 192                  case 'actor':
 193                      $this->setActor($value);
 194                      break;
 195                  case 'classmap':
 196                  case 'classMap':
 197                      $this->setClassmap($value);
 198                      break;
 199                  case 'encoding':
 200                      $this->setEncoding($value);
 201                      break;
 202                  case 'soapVersion':
 203                  case 'soap_version':
 204                      $this->setSoapVersion($value);
 205                      break;
 206                  case 'uri':
 207                      $this->setUri($value);
 208                      break;
 209                  case 'wsdl':
 210                      $this->setWsdl($value);
 211                      break;
 212                  case 'featues':
 213                      $this->setSoapFeatures($value);
 214                      break;
 215                  case 'cache_wsdl':
 216                      $this->setWsdlCache($value);
 217                      break;
 218                  default:
 219                      break;
 220              }
 221          }
 222  
 223          return $this;
 224      }
 225  
 226      /**
 227       * Return array of options suitable for using with SoapServer constructor
 228       *
 229       * @return array
 230       */
 231      public function getOptions()
 232      {
 233          $options = array();
 234          if (null !== $this->_actor) {
 235              $options['actor'] = $this->_actor;
 236          }
 237  
 238          if (null !== $this->_classmap) {
 239              $options['classmap'] = $this->_classmap;
 240          }
 241  
 242          if (null !== $this->_encoding) {
 243              $options['encoding'] = $this->_encoding;
 244          }
 245  
 246          if (null !== $this->_soapVersion) {
 247              $options['soap_version'] = $this->_soapVersion;
 248          }
 249  
 250          if (null !== $this->_uri) {
 251              $options['uri'] = $this->_uri;
 252          }
 253  
 254          if(null !== $this->_features) {
 255              $options['features'] = $this->_features;
 256          }
 257  
 258          if(null !== $this->_wsdlCache) {
 259              $options['cache_wsdl'] = $this->_wsdlCache;
 260          }
 261  
 262          return $options;
 263      }
 264  
 265      /**
 266       * Set encoding
 267       *
 268       * @param  string $encoding
 269       * @return Zend_Soap_Server
 270       * @throws Zend_Soap_Server_Exception with invalid encoding argument
 271       */
 272      public function setEncoding($encoding)
 273      {
 274          if (!is_string($encoding)) {
 275              require_once 'Zend/Soap/Server/Exception.php';
 276              throw new Zend_Soap_Server_Exception('Invalid encoding specified');
 277          }
 278  
 279          $this->_encoding = $encoding;
 280          return $this;
 281      }
 282  
 283      /**
 284       * Get encoding
 285       *
 286       * @return string
 287       */
 288      public function getEncoding()
 289      {
 290          return $this->_encoding;
 291      }
 292  
 293      /**
 294       * Set SOAP version
 295       *
 296       * @param  int $version One of the SOAP_1_1 or SOAP_1_2 constants
 297       * @return Zend_Soap_Server
 298       * @throws Zend_Soap_Server_Exception with invalid soap version argument
 299       */
 300      public function setSoapVersion($version)
 301      {
 302          if (!in_array($version, array(SOAP_1_1, SOAP_1_2))) {
 303              require_once 'Zend/Soap/Server/Exception.php';
 304              throw new Zend_Soap_Server_Exception('Invalid soap version specified');
 305          }
 306  
 307          $this->_soapVersion = $version;
 308          return $this;
 309      }
 310  
 311      /**
 312       * Get SOAP version
 313       *
 314       * @return int
 315       */
 316      public function getSoapVersion()
 317      {
 318          return $this->_soapVersion;
 319      }
 320  
 321      /**
 322       * Check for valid URN
 323       *
 324       * @param  string $urn
 325       * @return true
 326       * @throws Zend_Soap_Server_Exception on invalid URN
 327       */
 328      public function validateUrn($urn)
 329      {
 330          $scheme = parse_url($urn, PHP_URL_SCHEME);
 331          if ($scheme === false || $scheme === null) {
 332              require_once 'Zend/Soap/Server/Exception.php';
 333              throw new Zend_Soap_Server_Exception('Invalid URN');
 334          }
 335  
 336          return true;
 337      }
 338  
 339      /**
 340       * Set actor
 341       *
 342       * Actor is the actor URI for the server.
 343       *
 344       * @param  string $actor
 345       * @return Zend_Soap_Server
 346       */
 347      public function setActor($actor)
 348      {
 349          $this->validateUrn($actor);
 350          $this->_actor = $actor;
 351          return $this;
 352      }
 353  
 354      /**
 355       * Retrieve actor
 356       *
 357       * @return string
 358       */
 359      public function getActor()
 360      {
 361          return $this->_actor;
 362      }
 363  
 364      /**
 365       * Set URI
 366       *
 367       * URI in SoapServer is actually the target namespace, not a URI; $uri must begin with 'urn:'.
 368       *
 369       * @param  string $uri
 370       * @return Zend_Soap_Server
 371       * @throws Zend_Soap_Server_Exception with invalid uri argument
 372       */
 373      public function setUri($uri)
 374      {
 375          $this->validateUrn($uri);
 376          $this->_uri = $uri;
 377          return $this;
 378      }
 379  
 380      /**
 381       * Retrieve URI
 382       *
 383       * @return string
 384       */
 385      public function getUri()
 386      {
 387          return $this->_uri;
 388      }
 389  
 390      /**
 391       * Set classmap
 392       *
 393       * @param  array $classmap
 394       * @return Zend_Soap_Server
 395       * @throws Zend_Soap_Server_Exception for any invalid class in the class map
 396       */
 397      public function setClassmap($classmap)
 398      {
 399          if (!is_array($classmap)) {
 400              /**
 401               * @see Zend_Soap_Server_Exception
 402               */
 403              require_once 'Zend/Soap/Server/Exception.php';
 404              throw new Zend_Soap_Server_Exception('Classmap must be an array');
 405          }
 406          foreach ($classmap as $type => $class) {
 407              if (!class_exists($class)) {
 408                  /**
 409                   * @see Zend_Soap_Server_Exception
 410                   */
 411                  require_once 'Zend/Soap/Server/Exception.php';
 412                  throw new Zend_Soap_Server_Exception('Invalid class in class map');
 413              }
 414          }
 415  
 416          $this->_classmap = $classmap;
 417          return $this;
 418      }
 419  
 420      /**
 421       * Retrieve classmap
 422       *
 423       * @return mixed
 424       */
 425      public function getClassmap()
 426      {
 427          return $this->_classmap;
 428      }
 429  
 430      /**
 431       * Set wsdl
 432       *
 433       * @param string $wsdl  URI or path to a WSDL
 434       * @return Zend_Soap_Server
 435       */
 436      public function setWsdl($wsdl)
 437      {
 438          $this->_wsdl = $wsdl;
 439          return $this;
 440      }
 441  
 442      /**
 443       * Retrieve wsdl
 444       *
 445       * @return string
 446       */
 447      public function getWsdl()
 448      {
 449          return $this->_wsdl;
 450      }
 451  
 452      /**
 453       * Set the SOAP Feature options.
 454       *
 455       * @param  string|int $feature
 456       * @return Zend_Soap_Server
 457       */
 458      public function setSoapFeatures($feature)
 459      {
 460          $this->_features = $feature;
 461          return $this;
 462      }
 463  
 464      /**
 465       * Return current SOAP Features options
 466       *
 467       * @return int
 468       */
 469      public function getSoapFeatures()
 470      {
 471          return $this->_features;
 472      }
 473  
 474      /**
 475       * Set the SOAP Wsdl Caching Options
 476       *
 477       * @param string|int|boolean $caching
 478       * @return Zend_Soap_Server
 479       */
 480      public function setWsdlCache($options)
 481      {
 482          $this->_wsdlCache = $options;
 483          return $this;
 484      }
 485  
 486      /**
 487       * Get current SOAP Wsdl Caching option
 488       */
 489      public function getWsdlCache()
 490      {
 491          return $this->_wsdlCache;
 492      }
 493  
 494      /**
 495       * Attach a function as a server method
 496       *
 497       * @param array|string $function Function name, array of function names to attach,
 498       * or SOAP_FUNCTIONS_ALL to attach all functions
 499       * @param  string $namespace Ignored
 500       * @return Zend_Soap_Server
 501       * @throws Zend_Soap_Server_Exception on invalid functions
 502       */
 503      public function addFunction($function, $namespace = '')
 504      {
 505          // Bail early if set to SOAP_FUNCTIONS_ALL
 506          if ($this->_functions == SOAP_FUNCTIONS_ALL) {
 507              return $this;
 508          }
 509  
 510          if (is_array($function)) {
 511              foreach ($function as $func) {
 512                  if (is_string($func) && function_exists($func)) {
 513                      $this->_functions[] = $func;
 514                  } else {
 515                      require_once 'Zend/Soap/Server/Exception.php';
 516                      throw new Zend_Soap_Server_Exception('One or more invalid functions specified in array');
 517                  }
 518              }
 519              $this->_functions = array_merge($this->_functions, $function);
 520          } elseif (is_string($function) && function_exists($function)) {
 521              $this->_functions[] = $function;
 522          } elseif ($function == SOAP_FUNCTIONS_ALL) {
 523              $this->_functions = SOAP_FUNCTIONS_ALL;
 524          } else {
 525              require_once 'Zend/Soap/Server/Exception.php';
 526              throw new Zend_Soap_Server_Exception('Invalid function specified');
 527          }
 528  
 529          if (is_array($this->_functions)) {
 530              $this->_functions = array_unique($this->_functions);
 531          }
 532  
 533          return $this;
 534      }
 535  
 536      /**
 537       * Attach a class to a server
 538       *
 539       * Accepts a class name to use when handling requests. Any additional
 540       * arguments will be passed to that class' constructor when instantiated.
 541       *
 542       * See {@link setObject()} to set preconfigured object instances as request handlers.
 543       *
 544       * @param string $class Class Name which executes SOAP Requests at endpoint.
 545       * @return Zend_Soap_Server
 546       * @throws Zend_Soap_Server_Exception if called more than once, or if class
 547       * does not exist
 548       */
 549      public function setClass($class, $namespace = '', $argv = null)
 550      {
 551          if (isset($this->_class)) {
 552              require_once 'Zend/Soap/Server/Exception.php';
 553              throw new Zend_Soap_Server_Exception('A class has already been registered with this soap server instance');
 554          }
 555  
 556          if (!is_string($class)) {
 557              require_once 'Zend/Soap/Server/Exception.php';
 558              throw new Zend_Soap_Server_Exception('Invalid class argument (' . gettype($class) . ')');
 559          }
 560  
 561          if (!class_exists($class)) {
 562              require_once 'Zend/Soap/Server/Exception.php';
 563              throw new Zend_Soap_Server_Exception('Class "' . $class . '" does not exist');
 564          }
 565  
 566          $this->_class = $class;
 567          if (1 < func_num_args()) {
 568              $argv = func_get_args();
 569              array_shift($argv);
 570              $this->_classArgs = $argv;
 571          }
 572  
 573          return $this;
 574      }
 575  
 576      /**
 577       * Attach an object to a server
 578       *
 579       * Accepts an instanciated object to use when handling requests.
 580       *
 581       * @param object $object
 582       * @return Zend_Soap_Server
 583       */
 584      public function setObject($object)
 585      {
 586          if(!is_object($object)) {
 587              require_once 'Zend/Soap/Server/Exception.php';
 588              throw new Zend_Soap_Server_Exception('Invalid object argument ('.gettype($object).')');
 589          }
 590  
 591          if(isset($this->_object)) {
 592              require_once 'Zend/Soap/Server/Exception.php';
 593              throw new Zend_Soap_Server_Exception('An object has already been registered with this soap server instance');
 594          }
 595  
 596          $this->_object = $object;
 597  
 598          return $this;
 599      }
 600  
 601      /**
 602       * Return a server definition array
 603       *
 604       * Returns a list of all functions registered with {@link addFunction()},
 605       * merged with all public methods of the class set with {@link setClass()}
 606       * (if any).
 607       *
 608       * @access public
 609       * @return array
 610       */
 611      public function getFunctions()
 612      {
 613          $functions = array();
 614          if (null !== $this->_class) {
 615              $functions = get_class_methods($this->_class);
 616          } elseif (null !== $this->_object) {
 617              $functions = get_class_methods($this->_object);
 618          }
 619  
 620          return array_merge((array) $this->_functions, $functions);
 621      }
 622  
 623      /**
 624       * Unimplemented: Load server definition
 625       *
 626       * @param array $array
 627       * @return void
 628       * @throws Zend_Soap_Server_Exception Unimplemented
 629       */
 630      public function loadFunctions($definition)
 631      {
 632          require_once 'Zend/Soap/Server/Exception.php';
 633          throw new Zend_Soap_Server_Exception('Unimplemented');
 634      }
 635  
 636      /**
 637       * Set server persistence
 638       *
 639       * @param int $mode
 640       * @return Zend_Soap_Server
 641       */
 642      public function setPersistence($mode)
 643      {
 644          if (!in_array($mode, array(SOAP_PERSISTENCE_SESSION, SOAP_PERSISTENCE_REQUEST))) {
 645              require_once 'Zend/Soap/Server/Exception.php';
 646              throw new Zend_Soap_Server_Exception('Invalid persistence mode specified');
 647          }
 648  
 649          $this->_persistence = $mode;
 650          return $this;
 651      }
 652  
 653      /**
 654       * Get server persistence
 655       *
 656       * @return Zend_Soap_Server
 657       */
 658      public function getPersistence()
 659      {
 660          return $this->_persistence;
 661      }
 662  
 663      /**
 664       * Set request
 665       *
 666       * $request may be any of:
 667       * - DOMDocument; if so, then cast to XML
 668       * - DOMNode; if so, then grab owner document and cast to XML
 669       * - SimpleXMLElement; if so, then cast to XML
 670       * - stdClass; if so, calls __toString() and verifies XML
 671       * - string; if so, verifies XML
 672       *
 673       * @param DOMDocument|DOMNode|SimpleXMLElement|stdClass|string $request
 674       * @return Zend_Soap_Server
 675       */
 676      protected function _setRequest($request)
 677      {
 678          if ($request instanceof DOMDocument) {
 679              $xml = $request->saveXML();
 680          } elseif ($request instanceof DOMNode) {
 681              $xml = $request->ownerDocument->saveXML();
 682          } elseif ($request instanceof SimpleXMLElement) {
 683              $xml = $request->asXML();
 684          } elseif (is_object($request) || is_string($request)) {
 685              if (is_object($request)) {
 686                  $xml = $request->__toString();
 687              } else {
 688                  $xml = $request;
 689              }
 690  
 691              $dom = new DOMDocument();
 692              if(strlen($xml) == 0 || !$dom->loadXML($xml)) {
 693                  require_once 'Zend/Soap/Server/Exception.php';
 694                  throw new Zend_Soap_Server_Exception('Invalid XML');
 695              }
 696          }
 697          $this->_request = $xml;
 698          return $this;
 699      }
 700  
 701      /**
 702       * Retrieve request XML
 703       *
 704       * @return string
 705       */
 706      public function getLastRequest()
 707      {
 708          return $this->_request;
 709      }
 710  
 711      /**
 712       * Set return response flag
 713       *
 714       * If true, {@link handle()} will return the response instead of
 715       * automatically sending it back to the requesting client.
 716       *
 717       * The response is always available via {@link getResponse()}.
 718       *
 719       * @param boolean $flag
 720       * @return Zend_Soap_Server
 721       */
 722      public function setReturnResponse($flag)
 723      {
 724          $this->_returnResponse = ($flag) ? true : false;
 725          return $this;
 726      }
 727  
 728      /**
 729       * Retrieve return response flag
 730       *
 731       * @return boolean
 732       */
 733      public function getReturnResponse()
 734      {
 735          return $this->_returnResponse;
 736      }
 737  
 738      /**
 739       * Get response XML
 740       *
 741       * @return string
 742       */
 743      public function getLastResponse()
 744      {
 745          return $this->_response;
 746      }
 747  
 748      /**
 749       * Get SoapServer object
 750       *
 751       * Uses {@link $_wsdl} and return value of {@link getOptions()} to instantiate
 752       * SoapServer object, and then registers any functions or class with it, as
 753       * well as peristence.
 754       *
 755       * @return SoapServer
 756       */
 757      protected function _getSoap()
 758      {
 759          $options = $this->getOptions();
 760          $server  = new SoapServer($this->_wsdl, $options);
 761  
 762          if (!empty($this->_functions)) {
 763              $server->addFunction($this->_functions);
 764          }
 765  
 766          if (!empty($this->_class)) {
 767              $args = $this->_classArgs;
 768              array_unshift($args, $this->_class);
 769              call_user_func_array(array($server, 'setClass'), $args);
 770          }
 771  
 772          if (!empty($this->_object)) {
 773              $server->setObject($this->_object);
 774          }
 775  
 776          if (null !== $this->_persistence) {
 777              $server->setPersistence($this->_persistence);
 778          }
 779  
 780          return $server;
 781      }
 782  
 783      /**
 784       * Handle a request
 785       *
 786       * Instantiates SoapServer object with options set in object, and
 787       * dispatches its handle() method.
 788       *
 789       * $request may be any of:
 790       * - DOMDocument; if so, then cast to XML
 791       * - DOMNode; if so, then grab owner document and cast to XML
 792       * - SimpleXMLElement; if so, then cast to XML
 793       * - stdClass; if so, calls __toString() and verifies XML
 794       * - string; if so, verifies XML
 795       *
 796       * If no request is passed, pulls request using php:://input (for
 797       * cross-platform compatability purposes).
 798       *
 799       * @param DOMDocument|DOMNode|SimpleXMLElement|stdClass|string $request Optional request
 800       * @return void|string
 801       */
 802      public function handle($request = null)
 803      {
 804          if (null === $request) {
 805              $request = file_get_contents('php://input');
 806          }
 807  
 808          // Set Zend_Soap_Server error handler
 809          $displayErrorsOriginalState = $this->_initializeSoapErrorContext();
 810  
 811          $setRequestException = null;
 812          /**
 813           * @see Zend_Soap_Server_Exception
 814           */
 815          require_once 'Zend/Soap/Server/Exception.php';
 816          try {
 817              $this->_setRequest($request);
 818          } catch (Zend_Soap_Server_Exception $e) {
 819              $setRequestException = $e;
 820          }
 821  
 822          $soap = $this->_getSoap();
 823  
 824          ob_start();
 825          if($setRequestException instanceof Exception) {
 826              // Send SOAP fault message if we've catched exception
 827              $soap->fault("Sender", $setRequestException->getMessage());
 828          } else {
 829              try {
 830                  $soap->handle($request);
 831              } catch (Exception $e) {
 832                  $fault = $this->fault($e);
 833                  $soap->fault($fault->faultcode, $fault->faultstring);
 834              }
 835          }
 836          $this->_response = ob_get_clean();
 837  
 838          // Restore original error handler
 839          restore_error_handler();
 840          ini_set('display_errors', $displayErrorsOriginalState);
 841  
 842          if (!$this->_returnResponse) {
 843              echo $this->_response;
 844              return;
 845          }
 846  
 847          return $this->_response;
 848      }
 849  
 850      /**
 851       * Method initalizes the error context that the SOAPServer enviroment will run in.
 852       *
 853       * @return boolean display_errors original value
 854       */
 855      protected function _initializeSoapErrorContext()
 856      {
 857          $displayErrorsOriginalState = ini_get('display_errors');
 858          ini_set('display_errors', false);
 859          set_error_handler(array($this, 'handlePhpErrors'), E_USER_ERROR);
 860          return $displayErrorsOriginalState;
 861      }
 862  
 863      /**
 864       * Register a valid fault exception
 865       *
 866       * @param  string|array $class Exception class or array of exception classes
 867       * @return Zend_Soap_Server
 868       */
 869      public function registerFaultException($class)
 870      {
 871          $this->_faultExceptions = array_merge($this->_faultExceptions, (array) $class);
 872          return $this;
 873      }
 874  
 875      /**
 876       * Deregister a fault exception from the fault exception stack
 877       *
 878       * @param  string $class
 879       * @return boolean
 880       */
 881      public function deregisterFaultException($class)
 882      {
 883          if (in_array($class, $this->_faultExceptions, true)) {
 884              $index = array_search($class, $this->_faultExceptions);
 885              unset($this->_faultExceptions[$index]);
 886              return true;
 887          }
 888  
 889          return false;
 890      }
 891  
 892      /**
 893       * Return fault exceptions list
 894       *
 895       * @return array
 896       */
 897      public function getFaultExceptions()
 898      {
 899          return $this->_faultExceptions;
 900      }
 901  
 902      /**
 903       * Generate a server fault
 904       *
 905       * Note that the arguments are reverse to those of SoapFault.
 906       *
 907       * If an exception is passed as the first argument, its message and code
 908       * will be used to create the fault object if it has been registered via
 909       * {@Link registerFaultException()}.
 910       *
 911       * @link   http://www.w3.org/TR/soap12-part1/#faultcodes
 912       * @param  string|Exception $fault
 913       * @param  string $code SOAP Fault Codes
 914       * @return SoapFault
 915       */
 916      public function fault($fault = null, $code = "Receiver")
 917      {
 918          if ($fault instanceof Exception) {
 919              $class = get_class($fault);
 920              if (in_array($class, $this->_faultExceptions)) {
 921                  $message = $fault->getMessage();
 922                  $eCode   = $fault->getCode();
 923                  $code    = empty($eCode) ? $code : $eCode;
 924              } else {
 925                  $message = 'Unknown error';
 926              }
 927          } elseif(is_string($fault)) {
 928              $message = $fault;
 929          } else {
 930              $message = 'Unknown error';
 931          }
 932  
 933          $allowedFaultModes = array(
 934              'VersionMismatch', 'MustUnderstand', 'DataEncodingUnknown',
 935              'Sender', 'Receiver', 'Server'
 936          );
 937          if(!in_array($code, $allowedFaultModes)) {
 938              $code = "Receiver";
 939          }
 940  
 941          return new SoapFault($code, $message);
 942      }
 943  
 944      /**
 945       * Throw PHP errors as SoapFaults
 946       *
 947       * @param int $errno
 948       * @param string $errstr
 949       * @param string $errfile
 950       * @param int $errline
 951       * @param array $errcontext
 952       * @return void
 953       * @throws SoapFault
 954       */
 955      public function handlePhpErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
 956      {
 957          throw $this->fault($errstr, "Receiver");
 958      }
 959  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1