Server.php 25 KB

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