Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / XmlRpc / Response.php
blobeccd4d3ebbfff7e6a642277cc61d515ed7ad14dc
1 <?php
2 /**
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
8 */
10 namespace Zend\XmlRpc;
12 /**
13 * XmlRpc Response
15 * Container for accessing an XMLRPC return value and creating the XML response.
17 class Response
19 /**
20 * Return value
21 * @var mixed
23 protected $return;
25 /**
26 * Return type
27 * @var string
29 protected $type;
31 /**
32 * Response character encoding
33 * @var string
35 protected $encoding = 'UTF-8';
37 /**
38 * Fault, if response is a fault response
39 * @var null|\Zend\XmlRpc\Fault
41 protected $fault = null;
43 /**
44 * Constructor
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
50 * @param string $type
52 public function __construct($return = null, $type = null)
54 $this->setReturnValue($return, $type);
57 /**
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);
67 return $this;
70 /**
71 * Retrieve current response encoding
73 * @return string
75 public function getEncoding()
77 return $this->encoding;
80 /**
81 * Set the return value
83 * Sets the return value, with optional type hinting if provided.
85 * @param mixed $value
86 * @param string $type
87 * @return void
89 public function setReturnValue($value, $type = null)
91 $this->return = $value;
92 $this->type = (string) $type;
95 /**
96 * Retrieve the return value
98 * @return mixed
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?
118 * @return bool
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()
132 return $this->fault;
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());
151 return false;
154 // @see ZF-12293 - disable external entities for security purposes
155 $loadEntities = libxml_disable_entity_loader(true);
156 $useInternalXmlErrors = libxml_use_internal_errors(true);
157 try {
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);
175 // Not valid XML
176 $this->fault = new Fault(651);
177 $this->fault->setEncoding($this->getEncoding());
178 return false;
181 if (!empty($xml->fault)) {
182 // fault response
183 $this->fault = new Fault();
184 $this->fault->setEncoding($this->getEncoding());
185 $this->fault->loadXml($response);
186 return false;
189 if (empty($xml->params)) {
190 // Invalid response
191 $this->fault = new Fault(652);
192 $this->fault->setEncoding($this->getEncoding());
193 return false;
196 try {
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());
205 return false;
208 $this->setReturnValue($value->getValue());
209 return true;
213 * Return response as XML
215 * @return string
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
235 * @return string
237 public function __toString()
239 return $this->saveXML();