Response.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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-2009 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-2009 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. return $this;
  85. }
  86. /**
  87. * Retrieve current response encoding
  88. *
  89. * @return string
  90. */
  91. public function getEncoding()
  92. {
  93. return $this->_encoding;
  94. }
  95. /**
  96. * Set the return value
  97. *
  98. * Sets the return value, with optional type hinting if provided.
  99. *
  100. * @param mixed $value
  101. * @param string $type
  102. * @return void
  103. */
  104. public function setReturnValue($value, $type = null)
  105. {
  106. $this->_return = $value;
  107. $this->_type = (string) $type;
  108. }
  109. /**
  110. * Retrieve the return value
  111. *
  112. * @return mixed
  113. */
  114. public function getReturnValue()
  115. {
  116. return $this->_return;
  117. }
  118. /**
  119. * Retrieve the XMLRPC value for the return value
  120. *
  121. * @return Zend_XmlRpc_Value
  122. */
  123. protected function _getXmlRpcReturn()
  124. {
  125. return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
  126. }
  127. /**
  128. * Is the response a fault response?
  129. *
  130. * @return boolean
  131. */
  132. public function isFault()
  133. {
  134. return $this->_fault instanceof Zend_XmlRpc_Fault;
  135. }
  136. /**
  137. * Returns the fault, if any.
  138. *
  139. * @return null|Zend_XmlRpc_Fault
  140. */
  141. public function getFault()
  142. {
  143. return $this->_fault;
  144. }
  145. /**
  146. * Load a response from an XML response
  147. *
  148. * Attempts to load a response from an XMLRPC response, autodetecting if it
  149. * is a fault response.
  150. *
  151. * @param string $response
  152. * @return boolean True if a valid XMLRPC response, false if a fault
  153. * response or invalid input
  154. */
  155. public function loadXml($response)
  156. {
  157. if (!is_string($response)) {
  158. $this->_fault = new Zend_XmlRpc_Fault(650);
  159. $this->_fault->setEncoding($this->getEncoding());
  160. return false;
  161. }
  162. try {
  163. $xml = @new SimpleXMLElement($response);
  164. } catch (Exception $e) {
  165. // Not valid XML
  166. $this->_fault = new Zend_XmlRpc_Fault(651);
  167. $this->_fault->setEncoding($this->getEncoding());
  168. return false;
  169. }
  170. if (!empty($xml->fault)) {
  171. // fault response
  172. $this->_fault = new Zend_XmlRpc_Fault();
  173. $this->_fault->setEncoding($this->getEncoding());
  174. $this->_fault->loadXml($response);
  175. return false;
  176. }
  177. if (empty($xml->params)) {
  178. // Invalid response
  179. $this->_fault = new Zend_XmlRpc_Fault(652);
  180. $this->_fault->setEncoding($this->getEncoding());
  181. return false;
  182. }
  183. try {
  184. if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
  185. throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
  186. }
  187. $valueXml = $xml->params->param->value->asXML();
  188. $valueXml = preg_replace('/<\?xml version=.*?\?>/i', '', $valueXml);
  189. $value = Zend_XmlRpc_Value::getXmlRpcValue(trim($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. $valueDOM = new DOMDocument('1.0', $this->getEncoding());
  207. $valueDOM->loadXML($value->saveXML());
  208. $dom = new DOMDocument('1.0', $this->getEncoding());
  209. $response = $dom->appendChild($dom->createElement('methodResponse'));
  210. $params = $response->appendChild($dom->createElement('params'));
  211. $param = $params->appendChild($dom->createElement('param'));
  212. $param->appendChild($dom->importNode($valueDOM->documentElement, true));
  213. return $dom->saveXML();
  214. }
  215. /**
  216. * Return XML response
  217. *
  218. * @return string
  219. */
  220. public function __toString()
  221. {
  222. return $this->saveXML();
  223. }
  224. }