PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Soap/Server.php

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