Response.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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-2012 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-2012 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. // @see ZF-12293 - disable external entities for security purposes
  164. $loadEntities = libxml_disable_entity_loader(true);
  165. $useInternalXmlErrors = libxml_use_internal_errors(true);
  166. try {
  167. $dom = new DOMDocument;
  168. $dom->loadXML($response);
  169. foreach ($dom->childNodes as $child) {
  170. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  171. require_once 'Zend/XmlRpc/Exception.php';
  172. throw new Zend_XmlRpc_Exception(
  173. 'Invalid XML: Detected use of illegal DOCTYPE'
  174. );
  175. }
  176. }
  177. // TODO: Locate why this passes tests but a simplexml import doesn't
  178. // $xml = simplexml_import_dom($dom);
  179. $xml = new SimpleXMLElement($response);
  180. libxml_disable_entity_loader($loadEntities);
  181. libxml_use_internal_errors($useInternalXmlErrors);
  182. } catch (Exception $e) {
  183. libxml_disable_entity_loader($loadEntities);
  184. libxml_use_internal_errors($useInternalXmlErrors);
  185. // Not valid XML
  186. $this->_fault = new Zend_XmlRpc_Fault(651);
  187. $this->_fault->setEncoding($this->getEncoding());
  188. return false;
  189. }
  190. if (!empty($xml->fault)) {
  191. // fault response
  192. $this->_fault = new Zend_XmlRpc_Fault();
  193. $this->_fault->setEncoding($this->getEncoding());
  194. $this->_fault->loadXml($response);
  195. return false;
  196. }
  197. if (empty($xml->params)) {
  198. // Invalid response
  199. $this->_fault = new Zend_XmlRpc_Fault(652);
  200. $this->_fault->setEncoding($this->getEncoding());
  201. return false;
  202. }
  203. try {
  204. if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
  205. require_once 'Zend/XmlRpc/Value/Exception.php';
  206. throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
  207. }
  208. $valueXml = $xml->params->param->value->asXML();
  209. $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
  210. } catch (Zend_XmlRpc_Value_Exception $e) {
  211. $this->_fault = new Zend_XmlRpc_Fault(653);
  212. $this->_fault->setEncoding($this->getEncoding());
  213. return false;
  214. }
  215. $this->setReturnValue($value->getValue());
  216. return true;
  217. }
  218. /**
  219. * Return response as XML
  220. *
  221. * @return string
  222. */
  223. public function saveXml()
  224. {
  225. $value = $this->_getXmlRpcReturn();
  226. $generator = Zend_XmlRpc_Value::getGenerator();
  227. $generator->openElement('methodResponse')
  228. ->openElement('params')
  229. ->openElement('param');
  230. $value->generateXml();
  231. $generator->closeElement('param')
  232. ->closeElement('params')
  233. ->closeElement('methodResponse');
  234. return $generator->flush();
  235. }
  236. /**
  237. * Return XML response
  238. *
  239. * @return string
  240. */
  241. public function __toString()
  242. {
  243. return $this->saveXML();
  244. }
  245. }