2
0

Fault.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * @package Zend_XmlRpc
  16. * @subpackage Server
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_XmlRpc_Value
  22. */
  23. require_once 'Zend/XmlRpc/Value.php';
  24. /**
  25. * XMLRPC Faults
  26. *
  27. * Container for XMLRPC faults, containing both a code and a message;
  28. * additionally, has methods for determining if an XML response is an XMLRPC
  29. * fault, as well as generating the XML for an XMLRPC fault response.
  30. *
  31. * To allow method chaining, you may only use the {@link getInstance()} factory
  32. * to instantiate a Zend_XmlRpc_Server_Fault.
  33. *
  34. * @category Zend
  35. * @package Zend_XmlRpc
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_XmlRpc_Fault
  40. {
  41. /**
  42. * Fault code
  43. * @var int
  44. */
  45. protected $_code;
  46. /**
  47. * Fault character encoding
  48. * @var string
  49. */
  50. protected $_encoding = 'UTF-8';
  51. /**
  52. * Fault message
  53. * @var string
  54. */
  55. protected $_message;
  56. /**
  57. * Internal fault codes => messages
  58. * @var array
  59. */
  60. protected $_internal = array(
  61. 404 => 'Unknown Error',
  62. // 610 - 619 reflection errors
  63. 610 => 'Invalid method class',
  64. 611 => 'Unable to attach function or callback; not callable',
  65. 612 => 'Unable to load array; not an array',
  66. 613 => 'One or more method records are corrupt or otherwise unusable',
  67. // 620 - 629 dispatch errors
  68. 620 => 'Method does not exist',
  69. 621 => 'Error instantiating class to invoke method',
  70. 622 => 'Method missing implementation',
  71. 623 => 'Calling parameters do not match signature',
  72. // 630 - 639 request errors
  73. 630 => 'Unable to read request',
  74. 631 => 'Failed to parse request',
  75. 632 => 'Invalid request, no method passed; request must contain a \'methodName\' tag',
  76. 633 => 'Param must contain a value',
  77. 634 => 'Invalid method name',
  78. 635 => 'Invalid XML provided to request',
  79. 636 => 'Error creating xmlrpc value',
  80. // 640 - 649 system.* errors
  81. 640 => 'Method does not exist',
  82. // 650 - 659 response errors
  83. 650 => 'Invalid XML provided for response',
  84. 651 => 'Failed to parse response',
  85. 652 => 'Invalid response',
  86. 653 => 'Invalid XMLRPC value in response',
  87. );
  88. /**
  89. * Constructor
  90. *
  91. * @return Zend_XmlRpc_Fault
  92. */
  93. public function __construct($code = 404, $message = '')
  94. {
  95. $this->setCode($code);
  96. $code = $this->getCode();
  97. if (empty($message) && isset($this->_internal[$code])) {
  98. $message = $this->_internal[$code];
  99. } elseif (empty($message)) {
  100. $message = 'Unknown error';
  101. }
  102. $this->setMessage($message);
  103. }
  104. /**
  105. * Set the fault code
  106. *
  107. * @param int $code
  108. * @return Zend_XmlRpc_Fault
  109. */
  110. public function setCode($code)
  111. {
  112. $this->_code = (int) $code;
  113. return $this;
  114. }
  115. /**
  116. * Return fault code
  117. *
  118. * @return int
  119. */
  120. public function getCode()
  121. {
  122. return $this->_code;
  123. }
  124. /**
  125. * Retrieve fault message
  126. *
  127. * @param string
  128. * @return Zend_XmlRpc_Fault
  129. */
  130. public function setMessage($message)
  131. {
  132. $this->_message = (string) $message;
  133. return $this;
  134. }
  135. /**
  136. * Retrieve fault message
  137. *
  138. * @return string
  139. */
  140. public function getMessage()
  141. {
  142. return $this->_message;
  143. }
  144. /**
  145. * Set encoding to use in fault response
  146. *
  147. * @param string $encoding
  148. * @return Zend_XmlRpc_Fault
  149. */
  150. public function setEncoding($encoding)
  151. {
  152. $this->_encoding = $encoding;
  153. return $this;
  154. }
  155. /**
  156. * Retrieve current fault encoding
  157. *
  158. * @return string
  159. */
  160. public function getEncoding()
  161. {
  162. return $this->_encoding;
  163. }
  164. /**
  165. * Load an XMLRPC fault from XML
  166. *
  167. * @param string $fault
  168. * @return boolean Returns true if successfully loaded fault response, false
  169. * if response was not a fault response
  170. * @throws Zend_XmlRpc_Exception if no or faulty XML provided, or if fault
  171. * response does not contain either code or message
  172. */
  173. public function loadXml($fault)
  174. {
  175. if (!is_string($fault)) {
  176. require_once 'Zend/XmlRpc/Exception.php';
  177. throw new Zend_XmlRpc_Exception('Invalid XML provided to fault');
  178. }
  179. try {
  180. $xml = @new SimpleXMLElement($fault);
  181. } catch (Exception $e) {
  182. // Not valid XML
  183. require_once 'Zend/XmlRpc/Exception.php';
  184. throw new Zend_XmlRpc_Exception('Failed to parse XML fault: ' . $e->getMessage(), 500);
  185. }
  186. // Check for fault
  187. if (!$xml->fault) {
  188. // Not a fault
  189. return false;
  190. }
  191. if (!$xml->fault->value->struct) {
  192. // not a proper fault
  193. require_once 'Zend/XmlRpc/Exception.php';
  194. throw new Zend_XmlRpc_Exception('Invalid fault structure', 500);
  195. }
  196. $structXml = $xml->fault->value->asXML();
  197. $structXml = preg_replace('/<\?xml version=.*?\?>/i', '', $structXml);
  198. $struct = Zend_XmlRpc_Value::getXmlRpcValue(trim($structXml), Zend_XmlRpc_Value::XML_STRING);
  199. $struct = $struct->getValue();
  200. if (isset($struct['faultCode'])) {
  201. $code = $struct['faultCode'];
  202. }
  203. if (isset($struct['faultString'])) {
  204. $message = $struct['faultString'];
  205. }
  206. if (empty($code) && empty($message)) {
  207. require_once 'Zend/XmlRpc/Exception.php';
  208. throw new Zend_XmlRpc_Exception('Fault code and string required');
  209. }
  210. if (empty($code)) {
  211. $code = '404';
  212. }
  213. if (empty($message)) {
  214. if (isset($this->_internal[$code])) {
  215. $message = $this->_internal[$code];
  216. } else {
  217. $message = 'Unknown Error';
  218. }
  219. }
  220. $this->setCode($code);
  221. $this->setMessage($message);
  222. return true;
  223. }
  224. /**
  225. * Determine if an XML response is an XMLRPC fault
  226. *
  227. * @param string $xml
  228. * @return boolean
  229. */
  230. public static function isFault($xml)
  231. {
  232. $fault = new self();
  233. require_once 'Zend/XmlRpc/Exception.php';
  234. try {
  235. $isFault = $fault->loadXml($xml);
  236. } catch (Zend_XmlRpc_Exception $e) {
  237. $isFault = false;
  238. }
  239. return $isFault;
  240. }
  241. /**
  242. * Serialize fault to XML
  243. *
  244. * @return string
  245. */
  246. public function saveXML()
  247. {
  248. // Create fault value
  249. $faultStruct = array(
  250. 'faultCode' => $this->getCode(),
  251. 'faultString' => $this->getMessage()
  252. );
  253. $value = Zend_XmlRpc_Value::getXmlRpcValue($faultStruct);
  254. $valueDOM = new DOMDocument('1.0', $this->getEncoding());
  255. $valueDOM->loadXML($value->saveXML());
  256. // Build response XML
  257. $dom = new DOMDocument('1.0', $this->getEncoding());
  258. $r = $dom->appendChild($dom->createElement('methodResponse'));
  259. $f = $r->appendChild($dom->createElement('fault'));
  260. $f->appendChild($dom->importNode($valueDOM->documentElement, 1));
  261. return $dom->saveXML();
  262. }
  263. /**
  264. * Return XML fault response
  265. *
  266. * @return string
  267. */
  268. public function __toString()
  269. {
  270. return $this->saveXML();
  271. }
  272. }