Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/XmlRpc/Server.php
Go to the documentation of this file.
00001 <?php
00026 require_once 'Zend/Server/Abstract.php';
00027 
00031 require_once 'Zend/XmlRpc/Request.php';
00032 
00036 require_once 'Zend/XmlRpc/Response.php';
00037 
00041 require_once 'Zend/XmlRpc/Response/Http.php';
00042 
00046 require_once 'Zend/XmlRpc/Server/Fault.php';
00047 
00051 require_once 'Zend/XmlRpc/Server/System.php';
00052 
00056 require_once 'Zend/XmlRpc/Value.php';
00057 
00061 require_once 'Zend/Server/Reflection.php';
00062 
00066 require_once 'Zend/Server/Reflection/Function/Abstract.php';
00067 
00072 require_once 'Zend/Server/Reflection/Method.php';
00073 
00117 class Zend_XmlRpc_Server extends Zend_Server_Abstract
00118 {
00123     protected $_encoding = 'UTF-8';
00124 
00129     protected $_request = null;
00130 
00135     protected $_responseClass = 'Zend_XmlRpc_Response_Http';
00136 
00141     protected $_table;
00142 
00147     protected $_typeMap = array(
00148         'i4'                         => 'i4',
00149         'int'                        => 'int',
00150         'integer'                    => 'int',
00151         'Zend_Crypt_Math_BigInteger' => 'i8',
00152         'i8'                         => 'i8',
00153         'ex:i8'                      => 'i8',
00154         'double'                     => 'double',
00155         'float'                      => 'double',
00156         'real'                       => 'double',
00157         'boolean'                    => 'boolean',
00158         'bool'                       => 'boolean',
00159         'true'                       => 'boolean',
00160         'false'                      => 'boolean',
00161         'string'                     => 'string',
00162         'str'                        => 'string',
00163         'base64'                     => 'base64',
00164         'dateTime.iso8601'           => 'dateTime.iso8601',
00165         'date'                       => 'dateTime.iso8601',
00166         'time'                       => 'dateTime.iso8601',
00167         'time'                       => 'dateTime.iso8601',
00168         'Zend_Date'                  => 'dateTime.iso8601',
00169         'DateTime'                   => 'dateTime.iso8601',
00170         'array'                      => 'array',
00171         'struct'                     => 'struct',
00172         'null'                       => 'nil',
00173         'nil'                        => 'nil',
00174         'ex:nil'                     => 'nil',
00175         'void'                       => 'void',
00176         'mixed'                      => 'struct',
00177     );
00178 
00184     protected $_sendArgumentsToAllMethods = true;
00185 
00193     public function __construct()
00194     {
00195         $this->_table = new Zend_Server_Definition();
00196         $this->_registerSystemMethods();
00197     }
00198 
00207     public function __call($method, $params)
00208     {
00209         $system = $this->getSystem();
00210         if (!method_exists($system, $method)) {
00211             require_once 'Zend/XmlRpc/Server/Exception.php';
00212             throw new Zend_XmlRpc_Server_Exception('Unknown instance method called on server: ' . $method);
00213         }
00214         return call_user_func_array(array($system, $method), $params);
00215     }
00216 
00233     public function addFunction($function, $namespace = '')
00234     {
00235         if (!is_string($function) && !is_array($function)) {
00236             require_once 'Zend/XmlRpc/Server/Exception.php';
00237             throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
00238         }
00239 
00240         $argv = null;
00241         if (2 < func_num_args()) {
00242             $argv = func_get_args();
00243             $argv = array_slice($argv, 2);
00244         }
00245 
00246         $function = (array) $function;
00247         foreach ($function as $func) {
00248             if (!is_string($func) || !function_exists($func)) {
00249                 require_once 'Zend/XmlRpc/Server/Exception.php';
00250                 throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
00251             }
00252             $reflection = Zend_Server_Reflection::reflectFunction($func, $argv, $namespace);
00253             $this->_buildSignature($reflection);
00254         }
00255     }
00256 
00274     public function setClass($class, $namespace = '', $argv = null)
00275     {
00276         if (is_string($class) && !class_exists($class)) {
00277             require_once 'Zend/XmlRpc/Server/Exception.php';
00278             throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
00279         }
00280 
00281         $argv = null;
00282         if (2 < func_num_args()) {
00283             $argv = func_get_args();
00284             $argv = array_slice($argv, 2);
00285         }
00286 
00287         $dispatchable = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
00288         foreach ($dispatchable->getMethods() as $reflection) {
00289             $this->_buildSignature($reflection, $class);
00290         }
00291     }
00292 
00300     public function fault($fault = null, $code = 404)
00301     {
00302         if (!$fault instanceof Exception) {
00303             $fault = (string) $fault;
00304             if (empty($fault)) {
00305                 $fault = 'Unknown Error';
00306             }
00307             require_once 'Zend/XmlRpc/Server/Exception.php';
00308             $fault = new Zend_XmlRpc_Server_Exception($fault, $code);
00309         }
00310 
00311         return Zend_XmlRpc_Server_Fault::getInstance($fault);
00312     }
00313 
00320     public function handle($request = false)
00321     {
00322         // Get request
00323         if ((!$request || !$request instanceof Zend_XmlRpc_Request)
00324             && (null === ($request = $this->getRequest()))
00325         ) {
00326             require_once 'Zend/XmlRpc/Request/Http.php';
00327             $request = new Zend_XmlRpc_Request_Http();
00328             $request->setEncoding($this->getEncoding());
00329         }
00330 
00331         $this->setRequest($request);
00332 
00333         if ($request->isFault()) {
00334             $response = $request->getFault();
00335         } else {
00336             try {
00337                 $response = $this->_handle($request);
00338             } catch (Exception $e) {
00339                 $response = $this->fault($e);
00340             }
00341         }
00342 
00343         // Set output encoding
00344         $response->setEncoding($this->getEncoding());
00345 
00346         return $response;
00347     }
00348 
00359     public function loadFunctions($definition)
00360     {
00361         if (!is_array($definition) && (!$definition instanceof Zend_Server_Definition)) {
00362             if (is_object($definition)) {
00363                 $type = get_class($definition);
00364             } else {
00365                 $type = gettype($definition);
00366             }
00367             require_once 'Zend/XmlRpc/Server/Exception.php';
00368             throw new Zend_XmlRpc_Server_Exception('Unable to load server definition; must be an array or Zend_Server_Definition, received ' . $type, 612);
00369         }
00370 
00371         $this->_table->clearMethods();
00372         $this->_registerSystemMethods();
00373 
00374         if ($definition instanceof Zend_Server_Definition) {
00375             $definition = $definition->getMethods();
00376         }
00377 
00378         foreach ($definition as $key => $method) {
00379             if ('system.' == substr($key, 0, 7)) {
00380                 continue;
00381             }
00382             $this->_table->addMethod($method, $key);
00383         }
00384     }
00385 
00392     public function setEncoding($encoding)
00393     {
00394         $this->_encoding = $encoding;
00395         Zend_XmlRpc_Value::setEncoding($encoding);
00396         return $this;
00397     }
00398 
00404     public function getEncoding()
00405     {
00406         return $this->_encoding;
00407     }
00408 
00415     public function setPersistence($mode)
00416     {
00417     }
00418 
00426     public function setRequest($request)
00427     {
00428         if (is_string($request) && class_exists($request)) {
00429             $request = new $request();
00430             if (!$request instanceof Zend_XmlRpc_Request) {
00431                 require_once 'Zend/XmlRpc/Server/Exception.php';
00432                 throw new Zend_XmlRpc_Server_Exception('Invalid request class');
00433             }
00434             $request->setEncoding($this->getEncoding());
00435         } elseif (!$request instanceof Zend_XmlRpc_Request) {
00436             require_once 'Zend/XmlRpc/Server/Exception.php';
00437             throw new Zend_XmlRpc_Server_Exception('Invalid request object');
00438         }
00439 
00440         $this->_request = $request;
00441         return $this;
00442     }
00443 
00449     public function getRequest()
00450     {
00451         return $this->_request;
00452     }
00453 
00460     public function setResponseClass($class)
00461     {
00462         if (!class_exists($class) or
00463             ($c = new ReflectionClass($class) and !$c->isSubclassOf('Zend_XmlRpc_Response'))) {
00464 
00465             require_once 'Zend/XmlRpc/Server/Exception.php';
00466             throw new Zend_XmlRpc_Server_Exception('Invalid response class');
00467         }
00468         $this->_responseClass = $class;
00469         return true;
00470     }
00471 
00477     public function getResponseClass()
00478     {
00479         return $this->_responseClass;
00480     }
00481 
00487     public function getDispatchTable()
00488     {
00489         return $this->_table;
00490     }
00491 
00500     public function getFunctions()
00501     {
00502         return $this->_table->toArray();
00503     }
00504 
00510     public function getSystem()
00511     {
00512         return $this->_system;
00513     }
00514 
00523     public function sendArgumentsToAllMethods($flag = null)
00524     {
00525         if ($flag === null) {
00526             return $this->_sendArgumentsToAllMethods;
00527         }
00528 
00529         $this->_sendArgumentsToAllMethods = (bool)$flag;
00530         return $this;
00531     }
00532 
00539     protected function _fixType($type)
00540     {
00541         if (isset($this->_typeMap[$type])) {
00542             return $this->_typeMap[$type];
00543         }
00544         return 'void';
00545     }
00546 
00556     protected function _handle(Zend_XmlRpc_Request $request)
00557     {
00558         $method = $request->getMethod();
00559 
00560         // Check for valid method
00561         if (!$this->_table->hasMethod($method)) {
00562             require_once 'Zend/XmlRpc/Server/Exception.php';
00563             throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 620);
00564         }
00565 
00566         $info     = $this->_table->getMethod($method);
00567         $params   = $request->getParams();
00568         $argv     = $info->getInvokeArguments();
00569         if (0 < count($argv) and $this->sendArgumentsToAllMethods()) {
00570             $params = array_merge($params, $argv);
00571         }
00572 
00573         // Check calling parameters against signatures
00574         $matched    = false;
00575         $sigCalled  = $request->getTypes();
00576 
00577         $sigLength  = count($sigCalled);
00578         $paramsLen  = count($params);
00579         if ($sigLength < $paramsLen) {
00580             for ($i = $sigLength; $i < $paramsLen; ++$i) {
00581                 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($params[$i]);
00582                 $sigCalled[] = $xmlRpcValue->getType();
00583             }
00584         }
00585 
00586         $signatures = $info->getPrototypes();
00587         foreach ($signatures as $signature) {
00588             $sigParams = $signature->getParameters();
00589             if ($sigCalled === $sigParams) {
00590                 $matched = true;
00591                 break;
00592             }
00593         }
00594         if (!$matched) {
00595             require_once 'Zend/XmlRpc/Server/Exception.php';
00596             throw new Zend_XmlRpc_Server_Exception('Calling parameters do not match signature', 623);
00597         }
00598 
00599         $return        = $this->_dispatch($info, $params);
00600         $responseClass = $this->getResponseClass();
00601         return new $responseClass($return);
00602     }
00603 
00609     protected function _registerSystemMethods()
00610     {
00611         $system = new Zend_XmlRpc_Server_System($this);
00612         $this->_system = $system;
00613         $this->setClass($system, 'system');
00614     }
00615 }
 All Data Structures Namespaces Files Functions Variables Enumerations