File indexing completed on 2024-06-16 05:30:35

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_XmlRpc
0017  * @subpackage Server
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * XML-RPC system.* methods
0025  *
0026  * @category   Zend
0027  * @package    Zend_XmlRpc
0028  * @subpackage Server
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_XmlRpc_Server_System
0033 {
0034     /**
0035      * @var Zend_XmlRpc_Server
0036      */
0037     protected $_server;
0038 
0039     /**
0040      * Constructor
0041      *
0042      * @param  Zend_XmlRpc_Server $server
0043      * @return void
0044      */
0045     public function __construct(Zend_XmlRpc_Server $server)
0046     {
0047         $this->_server = $server;
0048     }
0049 
0050     /**
0051      * List all available XMLRPC methods
0052      *
0053      * Returns an array of methods.
0054      *
0055      * @return array
0056      */
0057     public function listMethods()
0058     {
0059         $table = $this->_server->getDispatchTable()->getMethods();
0060         return array_keys($table);
0061     }
0062 
0063     /**
0064      * Display help message for an XMLRPC method
0065      *
0066      * @param string $method
0067      * @return string
0068      */
0069     public function methodHelp($method)
0070     {
0071         $table = $this->_server->getDispatchTable();
0072         if (!$table->hasMethod($method)) {
0073             // require_once 'Zend/XmlRpc/Server/Exception.php';
0074             throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 640);
0075         }
0076 
0077         return $table->getMethod($method)->getMethodHelp();
0078     }
0079 
0080     /**
0081      * Return a method signature
0082      *
0083      * @param string $method
0084      * @return array
0085      */
0086     public function methodSignature($method)
0087     {
0088         $table = $this->_server->getDispatchTable();
0089         if (!$table->hasMethod($method)) {
0090             // require_once 'Zend/XmlRpc/Server/Exception.php';
0091             throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 640);
0092         }
0093         $method = $table->getMethod($method)->toArray();
0094         return $method['prototypes'];
0095     }
0096 
0097     /**
0098      * Multicall - boxcar feature of XML-RPC for calling multiple methods
0099      * in a single request.
0100      *
0101      * Expects a an array of structs representing method calls, each element
0102      * having the keys:
0103      * - methodName
0104      * - params
0105      *
0106      * Returns an array of responses, one for each method called, with the value
0107      * returned by the method. If an error occurs for a given method, returns a
0108      * struct with a fault response.
0109      *
0110      * @see http://www.xmlrpc.com/discuss/msgReader$1208
0111      * @param  array $methods
0112      * @return array
0113      */
0114     public function multicall($methods)
0115     {
0116         $responses = array();
0117         foreach ($methods as $method) {
0118             $fault = false;
0119             if (!is_array($method)) {
0120                 $fault = $this->_server->fault('system.multicall expects each method to be a struct', 601);
0121             } elseif (!isset($method['methodName'])) {
0122                 $fault = $this->_server->fault('Missing methodName: ' . var_export($methods, 1), 602);
0123             } elseif (!isset($method['params'])) {
0124                 $fault = $this->_server->fault('Missing params', 603);
0125             } elseif (!is_array($method['params'])) {
0126                 $fault = $this->_server->fault('Params must be an array', 604);
0127             } else {
0128                 if ('system.multicall' == $method['methodName']) {
0129                     // don't allow recursive calls to multicall
0130                     $fault = $this->_server->fault('Recursive system.multicall forbidden', 605);
0131                 }
0132             }
0133 
0134             if (!$fault) {
0135                 try {
0136                     $request = new Zend_XmlRpc_Request();
0137                     $request->setMethod($method['methodName']);
0138                     $request->setParams($method['params']);
0139                     $response = $this->_server->handle($request);
0140                     if ($response instanceof Zend_XmlRpc_Fault
0141                         || $response->isFault()
0142                     ) {
0143                         $fault = $response;
0144                     } else {
0145                         $responses[] = $response->getReturnValue();
0146                     }
0147                 } catch (Exception $e) {
0148                     $fault = $this->_server->fault($e);
0149                 }
0150             }
0151 
0152             if ($fault) {
0153                 $responses[] = array(
0154                     'faultCode'   => $fault->getCode(),
0155                     'faultString' => $fault->getMessage()
0156                 );
0157             }
0158         }
0159 
0160         return $responses;
0161     }
0162 }