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 / Request.php
blobf1a1db8a004f096fff5e796e70401c36d3254a03
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 use DOMDocument;
13 use SimpleXMLElement;
14 use Zend\Stdlib\ErrorHandler;
16 /**
17 * XmlRpc Request object
19 * Encapsulates an XmlRpc request, holding the method call and all parameters.
20 * Provides accessors for these, as well as the ability to load from XML and to
21 * create the XML request string.
23 * Additionally, if errors occur setting the method or parsing XML, a fault is
24 * generated and stored in {@link $fault}; developers may check for it using
25 * {@link isFault()} and {@link getFault()}.
27 class Request
29 /**
30 * Request character encoding
31 * @var string
33 protected $encoding = 'UTF-8';
35 /**
36 * Method to call
37 * @var string
39 protected $method;
41 /**
42 * XML request
43 * @var string
45 protected $xml;
47 /**
48 * Method parameters
49 * @var array
51 protected $params = array();
53 /**
54 * Fault object, if any
55 * @var \Zend\XmlRpc\Fault
57 protected $fault = null;
59 /**
60 * XML-RPC type for each param
61 * @var array
63 protected $types = array();
65 /**
66 * XML-RPC request params
67 * @var array
69 protected $xmlRpcParams = array();
71 /**
72 * Create a new XML-RPC request
74 * @param string $method (optional)
75 * @param array $params (optional)
77 public function __construct($method = null, $params = null)
79 if ($method !== null) {
80 $this->setMethod($method);
83 if ($params !== null) {
84 $this->setParams($params);
89 /**
90 * Set encoding to use in request
92 * @param string $encoding
93 * @return \Zend\XmlRpc\Request
95 public function setEncoding($encoding)
97 $this->encoding = $encoding;
98 AbstractValue::setEncoding($encoding);
99 return $this;
103 * Retrieve current request encoding
105 * @return string
107 public function getEncoding()
109 return $this->encoding;
113 * Set method to call
115 * @param string $method
116 * @return bool Returns true on success, false if method name is invalid
118 public function setMethod($method)
120 if (!is_string($method) || !preg_match('/^[a-z0-9_.:\\\\\/]+$/i', $method)) {
121 $this->fault = new Fault(634, 'Invalid method name ("' . $method . '")');
122 $this->fault->setEncoding($this->getEncoding());
123 return false;
126 $this->method = $method;
127 return true;
131 * Retrieve call method
133 * @return string
135 public function getMethod()
137 return $this->method;
141 * Add a parameter to the parameter stack
143 * Adds a parameter to the parameter stack, associating it with the type
144 * $type if provided
146 * @param mixed $value
147 * @param string $type Optional; type hinting
148 * @return void
150 public function addParam($value, $type = null)
152 $this->params[] = $value;
153 if (null === $type) {
154 // Detect type if not provided explicitly
155 if ($value instanceof AbstractValue) {
156 $type = $value->getType();
157 } else {
158 $xmlRpcValue = AbstractValue::getXmlRpcValue($value);
159 $type = $xmlRpcValue->getType();
162 $this->types[] = $type;
163 $this->xmlRpcParams[] = array('value' => $value, 'type' => $type);
167 * Set the parameters array
169 * If called with a single, array value, that array is used to set the
170 * parameters stack. If called with multiple values or a single non-array
171 * value, the arguments are used to set the parameters stack.
173 * Best is to call with array of the format, in order to allow type hinting
174 * when creating the XMLRPC values for each parameter:
175 * <code>
176 * $array = array(
177 * array(
178 * 'value' => $value,
179 * 'type' => $type
180 * )[, ... ]
181 * );
182 * </code>
184 * @access public
185 * @return void
187 public function setParams()
189 $argc = func_num_args();
190 $argv = func_get_args();
191 if (0 == $argc) {
192 return;
195 if ((1 == $argc) && is_array($argv[0])) {
196 $params = array();
197 $types = array();
198 $wellFormed = true;
199 foreach ($argv[0] as $arg) {
200 if (!is_array($arg) || !isset($arg['value'])) {
201 $wellFormed = false;
202 break;
204 $params[] = $arg['value'];
206 if (!isset($arg['type'])) {
207 $xmlRpcValue = AbstractValue::getXmlRpcValue($arg['value']);
208 $arg['type'] = $xmlRpcValue->getType();
210 $types[] = $arg['type'];
212 if ($wellFormed) {
213 $this->xmlRpcParams = $argv[0];
214 $this->params = $params;
215 $this->types = $types;
216 } else {
217 $this->params = $argv[0];
218 $this->types = array();
219 $xmlRpcParams = array();
220 foreach ($argv[0] as $arg) {
221 if ($arg instanceof AbstractValue) {
222 $type = $arg->getType();
223 } else {
224 $xmlRpcValue = AbstractValue::getXmlRpcValue($arg);
225 $type = $xmlRpcValue->getType();
227 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
228 $this->types[] = $type;
230 $this->xmlRpcParams = $xmlRpcParams;
232 return;
235 $this->params = $argv;
236 $this->types = array();
237 $xmlRpcParams = array();
238 foreach ($argv as $arg) {
239 if ($arg instanceof AbstractValue) {
240 $type = $arg->getType();
241 } else {
242 $xmlRpcValue = AbstractValue::getXmlRpcValue($arg);
243 $type = $xmlRpcValue->getType();
245 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
246 $this->types[] = $type;
248 $this->xmlRpcParams = $xmlRpcParams;
252 * Retrieve the array of parameters
254 * @return array
256 public function getParams()
258 return $this->params;
262 * Return parameter types
264 * @return array
266 public function getTypes()
268 return $this->types;
272 * Load XML and parse into request components
274 * @param string $request
275 * @throws Exception\ValueException if invalid XML
276 * @return bool True on success, false if an error occurred.
278 public function loadXml($request)
280 if (!is_string($request)) {
281 $this->fault = new Fault(635);
282 $this->fault->setEncoding($this->getEncoding());
283 return false;
286 // @see ZF-12293 - disable external entities for security purposes
287 $loadEntities = libxml_disable_entity_loader(true);
288 $xmlErrorsFlag = libxml_use_internal_errors(true);
289 try {
290 $dom = new DOMDocument;
291 $dom->loadXML($request);
292 foreach ($dom->childNodes as $child) {
293 if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
294 throw new Exception\ValueException(
295 'Invalid XML: Detected use of illegal DOCTYPE'
299 ErrorHandler::start();
300 $xml = simplexml_import_dom($dom);
301 $error = ErrorHandler::stop();
302 libxml_disable_entity_loader($loadEntities);
303 libxml_use_internal_errors($xmlErrorsFlag);
304 } catch (\Exception $e) {
305 // Not valid XML
306 $this->fault = new Fault(631);
307 $this->fault->setEncoding($this->getEncoding());
308 libxml_disable_entity_loader($loadEntities);
309 libxml_use_internal_errors($xmlErrorsFlag);
310 return false;
312 if (!$xml instanceof SimpleXMLElement || $error) {
313 // Not valid XML
314 $this->fault = new Fault(631);
315 $this->fault->setEncoding($this->getEncoding());
316 libxml_use_internal_errors($xmlErrorsFlag);
317 return false;
320 // Check for method name
321 if (empty($xml->methodName)) {
322 // Missing method name
323 $this->fault = new Fault(632);
324 $this->fault->setEncoding($this->getEncoding());
325 return false;
328 $this->method = (string) $xml->methodName;
330 // Check for parameters
331 if (!empty($xml->params)) {
332 $types = array();
333 $argv = array();
334 foreach ($xml->params->children() as $param) {
335 if (!isset($param->value)) {
336 $this->fault = new Fault(633);
337 $this->fault->setEncoding($this->getEncoding());
338 return false;
341 try {
342 $param = AbstractValue::getXmlRpcValue($param->value, AbstractValue::XML_STRING);
343 $types[] = $param->getType();
344 $argv[] = $param->getValue();
345 } catch (\Exception $e) {
346 $this->fault = new Fault(636);
347 $this->fault->setEncoding($this->getEncoding());
348 return false;
352 $this->types = $types;
353 $this->params = $argv;
356 $this->xml = $request;
358 return true;
362 * Does the current request contain errors and should it return a fault
363 * response?
365 * @return bool
367 public function isFault()
369 return $this->fault instanceof Fault;
373 * Retrieve the fault response, if any
375 * @return null|\Zend\XmlRpc\Fault
377 public function getFault()
379 return $this->fault;
383 * Retrieve method parameters as XMLRPC values
385 * @return array
387 protected function _getXmlRpcParams()
389 $params = array();
390 if (is_array($this->xmlRpcParams)) {
391 foreach ($this->xmlRpcParams as $param) {
392 $value = $param['value'];
393 $type = $param['type'] ?: AbstractValue::AUTO_DETECT_TYPE;
395 if (!$value instanceof AbstractValue) {
396 $value = AbstractValue::getXmlRpcValue($value, $type);
398 $params[] = $value;
402 return $params;
406 * Create XML request
408 * @return string
410 public function saveXml()
412 $args = $this->_getXmlRpcParams();
413 $method = $this->getMethod();
415 $generator = AbstractValue::getGenerator();
416 $generator->openElement('methodCall')
417 ->openElement('methodName', $method)
418 ->closeElement('methodName');
420 if (is_array($args) && count($args)) {
421 $generator->openElement('params');
423 foreach ($args as $arg) {
424 $generator->openElement('param');
425 $arg->generateXml();
426 $generator->closeElement('param');
428 $generator->closeElement('params');
430 $generator->closeElement('methodCall');
432 return $generator->flush();
436 * Return XML request
438 * @return string
440 public function __toString()
442 return $this->saveXML();