Response.php 6.3 KB

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