Magento file: lib/Zend/XmlRpc/Response.php

Please LogIn to Reply!

Post By: admin | 26-01-2012 06:35 Reply

Magento folder: lib/Zend/XmlRpc/
Magento file: lib/Zend/XmlRpc/Response.php

  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_Controller
  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. */
  20. /**
  21. * Zend_XmlRpc_Value
  22. */
  23. #require_once 'Zend/XmlRpc/Value.php';
  24. /**
  25. * Zend_XmlRpc_Fault
  26. */
  27. #require_once 'Zend/XmlRpc/Fault.php';
  28. /**
  29. * XmlRpc Response
  30. *
  31. * Container for accessing an XMLRPC return value and creating the XML response.
  32. *
  33. * @category Zend
  34. * @package Zend_XmlRpc
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @version $Id: Response.php 21359 2010-03-07 00:54:02Z lars $
  38. */
  39. class Zend_XmlRpc_Response
  40. {
  41. /**
  42. * Return value
  43. * @var mixed
  44. */
  45. protected $_return;
  46. /**
  47. * Return type
  48. * @var string
  49. */
  50. protected $_type;
  51. /**
  52. * Response character encoding
  53. * @var string
  54. */
  55. protected $_encoding = 'UTF-8';
  56. /**
  57. * Fault, if response is a fault response
  58. * @var null|Zend_XmlRpc_Fault
  59. */
  60. protected $_fault = null;
  61. /**
  62. * Constructor
  63. *
  64. * Can optionally pass in the return value and type hinting; otherwise, the
  65. * return value can be set via {@link setReturnValue()}.
  66. *
  67. * @param mixed $return
  68. * @param string $type
  69. * @return void
  70. */
  71. public function __construct($return = null, $type = null)
  72. {
  73. $this->setReturnValue($return, $type);
  74. }
  75. /**
  76. * Set encoding to use in response
  77. *
  78. * @param string $encoding
  79. * @return Zend_XmlRpc_Response
  80. */
  81. public function setEncoding($encoding)
  82. {
  83. $this->_encoding = $encoding;
  84. Zend_XmlRpc_Value::setEncoding($encoding);
  85. return $this;
  86. }
  87. /**
  88. * Retrieve current response encoding
  89. *
  90. * @return string
  91. */
  92. public function getEncoding()
  93. {
  94. return $this->_encoding;
  95. }
  96. /**
  97. * Set the return value
  98. *
  99. * Sets the return value, with optional type hinting if provided.
  100. *
  101. * @param mixed $value
  102. * @param string $type
  103. * @return void
  104. */
  105. public function setReturnValue($value, $type = null)
  106. {
  107. $this->_return = $value;
  108. $this->_type = (string) $type;
  109. }
  110. /**
  111. * Retrieve the return value
  112. *
  113. * @return mixed
  114. */
  115. public function getReturnValue()
  116. {
  117. return $this->_return;
  118. }
  119. /**
  120. * Retrieve the XMLRPC value for the return value
  121. *
  122. * @return Zend_XmlRpc_Value
  123. */
  124. protected function _getXmlRpcReturn()
  125. {
  126. return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
  127. }
  128. /**
  129. * Is the response a fault response?
  130. *
  131. * @return boolean
  132. */
  133. public function isFault()
  134. {
  135. return $this->_fault instanceof Zend_XmlRpc_Fault;
  136. }
  137. /**
  138. * Returns the fault, if any.
  139. *
  140. * @return null|Zend_XmlRpc_Fault
  141. */
  142. public function getFault()
  143. {
  144. return $this->_fault;
  145. }
  146. /**
  147. * Load a response from an XML response
  148. *
  149. * Attempts to load a response from an XMLRPC response, autodetecting if it
  150. * is a fault response.
  151. *
  152. * @param string $response
  153. * @return boolean True if a valid XMLRPC response, false if a fault
  154. * response or invalid input
  155. */
  156. public function loadXml($response)
  157. {
  158. if (!is_string($response)) {
  159. $this->_fault = new Zend_XmlRpc_Fault(650);
  160. $this->_fault->setEncoding($this->getEncoding());
  161. return false;
  162. }
  163. try {
  164. $useInternalXmlErrors = libxml_use_internal_errors(true);
  165. $xml = new SimpleXMLElement($response);
  166. libxml_use_internal_errors($useInternalXmlErrors);
  167. } catch (Exception $e) {
  168. libxml_use_internal_errors($useInternalXmlErrors);
  169. // Not valid XML
  170. $this->_fault = new Zend_XmlRpc_Fault(651);
  171. $this->_fault->setEncoding($this->getEncoding());
  172. return false;
  173. }
  174. if (!empty($xml->fault)) {
  175. // fault response
  176. $this->_fault = new Zend_XmlRpc_Fault();
  177. $this->_fault->setEncoding($this->getEncoding());
  178. $this->_fault->loadXml($response);
  179. return false;
  180. }
  181. if (empty($xml->params)) {
  182. // Invalid response
  183. $this->_fault = new Zend_XmlRpc_Fault(652);
  184. $this->_fault->setEncoding($this->getEncoding());
  185. return false;
  186. }
  187. try {
  188. if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
  189. throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
  190. }
  191. $valueXml = $xml->params->param->value->asXML();
  192. $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
  193. } catch (Zend_XmlRpc_Value_Exception $e) {
  194. $this->_fault = new Zend_XmlRpc_Fault(653);
  195. $this->_fault->setEncoding($this->getEncoding());
  196. return false;
  197. }
  198. $this->setReturnValue($value->getValue());
  199. return true;
  200. }
  201. /**
  202. * Return response as XML
  203. *
  204. * @return string
  205. */
  206. public function saveXml()
  207. {
  208. $value = $this->_getXmlRpcReturn();
  209. $generator = Zend_XmlRpc_Value::getGenerator();
  210. $generator->openElement('methodResponse')
  211. ->openElement('params')
  212. ->openElement('param');
  213. $value->generateXml();
  214. $generator->closeElement('param')
  215. ->closeElement('params')
  216. ->closeElement('methodResponse');
  217. return $generator->flush();
  218. }
  219. /**
  220. * Return XML response
  221. *
  222. * @return string
  223. */
  224. public function __toString()
  225. {
  226. return $this->saveXML();
  227. }
  228. }
`Reply Or Comment!
Please LogIn to Reply!