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
;
14 use Zend\Stdlib\ErrorHandler
;
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()}.
30 * Request character encoding
33 protected $encoding = 'UTF-8';
51 protected $params = array();
54 * Fault object, if any
55 * @var \Zend\XmlRpc\Fault
57 protected $fault = null;
60 * XML-RPC type for each param
63 protected $types = array();
66 * XML-RPC request params
69 protected $xmlRpcParams = array();
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);
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);
103 * Retrieve current request encoding
107 public function getEncoding()
109 return $this->encoding
;
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());
126 $this->method
= $method;
131 * Retrieve call method
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
146 * @param mixed $value
147 * @param string $type Optional; type hinting
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();
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:
187 public function setParams()
189 $argc = func_num_args();
190 $argv = func_get_args();
195 if ((1 == $argc) && is_array($argv[0])) {
199 foreach ($argv[0] as $arg) {
200 if (!is_array($arg) ||
!isset($arg['value'])) {
204 $params[] = $arg['value'];
206 if (!isset($arg['type'])) {
207 $xmlRpcValue = AbstractValue
::getXmlRpcValue($arg['value']);
208 $arg['type'] = $xmlRpcValue->getType();
210 $types[] = $arg['type'];
213 $this->xmlRpcParams
= $argv[0];
214 $this->params
= $params;
215 $this->types
= $types;
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();
224 $xmlRpcValue = AbstractValue
::getXmlRpcValue($arg);
225 $type = $xmlRpcValue->getType();
227 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
228 $this->types
[] = $type;
230 $this->xmlRpcParams
= $xmlRpcParams;
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();
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
256 public function getParams()
258 return $this->params
;
262 * Return parameter types
266 public function getTypes()
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());
286 // @see ZF-12293 - disable external entities for security purposes
287 $loadEntities = libxml_disable_entity_loader(true);
288 $xmlErrorsFlag = libxml_use_internal_errors(true);
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) {
306 $this->fault
= new Fault(631);
307 $this->fault
->setEncoding($this->getEncoding());
308 libxml_disable_entity_loader($loadEntities);
309 libxml_use_internal_errors($xmlErrorsFlag);
312 if (!$xml instanceof SimpleXMLElement ||
$error) {
314 $this->fault
= new Fault(631);
315 $this->fault
->setEncoding($this->getEncoding());
316 libxml_use_internal_errors($xmlErrorsFlag);
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());
328 $this->method
= (string) $xml->methodName
;
330 // Check for parameters
331 if (!empty($xml->params
)) {
334 foreach ($xml->params
->children() as $param) {
335 if (!isset($param->value
)) {
336 $this->fault
= new Fault(633);
337 $this->fault
->setEncoding($this->getEncoding());
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());
352 $this->types
= $types;
353 $this->params
= $argv;
356 $this->xml
= $request;
362 * Does the current request contain errors and should it return a fault
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()
383 * Retrieve method parameters as XMLRPC values
387 protected function _getXmlRpcParams()
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);
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');
426 $generator->closeElement('param');
428 $generator->closeElement('params');
430 $generator->closeElement('methodCall');
432 return $generator->flush();
440 public function __toString()
442 return $this->saveXML();