Response.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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$
  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. $xml = new SimpleXMLElement($response);
  165. } catch (Exception $e) {
  166. // Not valid XML
  167. $this->_fault = new Zend_XmlRpc_Fault(651);
  168. $this->_fault->setEncoding($this->getEncoding());
  169. return false;
  170. }
  171. if (!empty($xml->fault)) {
  172. // fault response
  173. $this->_fault = new Zend_XmlRpc_Fault();
  174. $this->_fault->setEncoding($this->getEncoding());
  175. $this->_fault->loadXml($response);
  176. return false;
  177. }
  178. if (empty($xml->params)) {
  179. // Invalid response
  180. $this->_fault = new Zend_XmlRpc_Fault(652);
  181. $this->_fault->setEncoding($this->getEncoding());
  182. return false;
  183. }
  184. try {
  185. if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
  186. throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
  187. }
  188. $valueXml = $xml->params->param->value->asXML();
  189. $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
  190. } catch (Zend_XmlRpc_Value_Exception $e) {
  191. $this->_fault = new Zend_XmlRpc_Fault(653);
  192. $this->_fault->setEncoding($this->getEncoding());
  193. return false;
  194. }
  195. $this->setReturnValue($value->getValue());
  196. return true;
  197. }
  198. /**
  199. * Return response as XML
  200. *
  201. * @return string
  202. */
  203. public function saveXml()
  204. {
  205. $value = $this->_getXmlRpcReturn();
  206. $generator = Zend_XmlRpc_Value::getGenerator();
  207. $generator->openElement('methodResponse')
  208. ->openElement('params')
  209. ->openElement('param');
  210. $value->generateXml();
  211. $generator->closeElement('param')
  212. ->closeElement('params')
  213. ->closeElement('methodResponse');
  214. return $generator->flush();
  215. }
  216. /**
  217. * Return XML response
  218. *
  219. * @return string
  220. */
  221. public function __toString()
  222. {
  223. return $this->saveXML();
  224. }
  225. }