PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/XmlRpc/Fault.php

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