3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\XmlRpc
;
15 * Container for accessing an XMLRPC return value and creating the XML response.
32 * Response character encoding
35 protected $encoding = 'UTF-8';
38 * Fault, if response is a fault response
39 * @var null|\Zend\XmlRpc\Fault
41 protected $fault = null;
46 * Can optionally pass in the return value and type hinting; otherwise, the
47 * return value can be set via {@link setReturnValue()}.
49 * @param mixed $return
52 public function __construct($return = null, $type = null)
54 $this->setReturnValue($return, $type);
58 * Set encoding to use in response
60 * @param string $encoding
61 * @return \Zend\XmlRpc\Response
63 public function setEncoding($encoding)
65 $this->encoding
= $encoding;
66 AbstractValue
::setEncoding($encoding);
71 * Retrieve current response encoding
75 public function getEncoding()
77 return $this->encoding
;
81 * Set the return value
83 * Sets the return value, with optional type hinting if provided.
89 public function setReturnValue($value, $type = null)
91 $this->return = $value;
92 $this->type
= (string) $type;
96 * Retrieve the return value
100 public function getReturnValue()
102 return $this->return;
106 * Retrieve the XMLRPC value for the return value
108 * @return \Zend\XmlRpc\AbstractValue
110 protected function _getXmlRpcReturn()
112 return AbstractValue
::getXmlRpcValue($this->return);
116 * Is the response a fault response?
120 public function isFault()
122 return $this->fault
instanceof Fault
;
126 * Returns the fault, if any.
128 * @return null|\Zend\XmlRpc\Fault
130 public function getFault()
136 * Load a response from an XML response
138 * Attempts to load a response from an XMLRPC response, autodetecting if it
139 * is a fault response.
141 * @param string $response
142 * @throws Exception\ValueException if invalid XML
143 * @return bool True if a valid XMLRPC response, false if a fault
144 * response or invalid input
146 public function loadXml($response)
148 if (!is_string($response)) {
149 $this->fault
= new Fault(650);
150 $this->fault
->setEncoding($this->getEncoding());
154 // @see ZF-12293 - disable external entities for security purposes
155 $loadEntities = libxml_disable_entity_loader(true);
156 $useInternalXmlErrors = libxml_use_internal_errors(true);
158 $dom = new \DOMDocument
;
159 $dom->loadXML($response);
160 foreach ($dom->childNodes
as $child) {
161 if ($child->nodeType
=== XML_DOCUMENT_TYPE_NODE
) {
162 throw new Exception\
ValueException(
163 'Invalid XML: Detected use of illegal DOCTYPE'
167 // TODO: Locate why this passes tests but a simplexml import doesn't
168 //$xml = simplexml_import_dom($dom);
169 $xml = new \
SimpleXMLElement($response);
170 libxml_disable_entity_loader($loadEntities);
171 libxml_use_internal_errors($useInternalXmlErrors);
172 } catch (\Exception
$e) {
173 libxml_disable_entity_loader($loadEntities);
174 libxml_use_internal_errors($useInternalXmlErrors);
176 $this->fault
= new Fault(651);
177 $this->fault
->setEncoding($this->getEncoding());
181 if (!empty($xml->fault
)) {
183 $this->fault
= new Fault();
184 $this->fault
->setEncoding($this->getEncoding());
185 $this->fault
->loadXml($response);
189 if (empty($xml->params
)) {
191 $this->fault
= new Fault(652);
192 $this->fault
->setEncoding($this->getEncoding());
197 if (!isset($xml->params
) ||
!isset($xml->params
->param
) ||
!isset($xml->params
->param
->value
)) {
198 throw new Exception\
ValueException('Missing XML-RPC value in XML');
200 $valueXml = $xml->params
->param
->value
->asXML();
201 $value = AbstractValue
::getXmlRpcValue($valueXml, AbstractValue
::XML_STRING
);
202 } catch (Exception\ValueException
$e) {
203 $this->fault
= new Fault(653);
204 $this->fault
->setEncoding($this->getEncoding());
208 $this->setReturnValue($value->getValue());
213 * Return response as XML
217 public function saveXml()
219 $value = $this->_getXmlRpcReturn();
220 $generator = AbstractValue
::getGenerator();
221 $generator->openElement('methodResponse')
222 ->openElement('params')
223 ->openElement('param');
224 $value->generateXml();
225 $generator->closeElement('param')
226 ->closeElement('params')
227 ->closeElement('methodResponse');
229 return $generator->flush();
233 * Return XML response
237 public function __toString()
239 return $this->saveXML();