Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/XmlRpc/Client.php
Go to the documentation of this file.
00001 <?php
00028 require_once 'Zend/Http/Client.php';
00029 
00034 require_once 'Zend/XmlRpc/Client/ServerProxy.php';
00035 
00040 require_once 'Zend/XmlRpc/Client/ServerIntrospection.php';
00041 
00047 require_once 'Zend/XmlRpc/Value.php';
00048 
00053 require_once 'Zend/XmlRpc/Request.php';
00054 
00059 require_once 'Zend/XmlRpc/Response.php';
00060 
00065 require_once 'Zend/XmlRpc/Fault.php';
00066 
00067 
00077 class Zend_XmlRpc_Client
00078 {
00084     protected $_serverAddress;
00085 
00090     protected $_httpClient = null;
00091 
00096     protected $_introspector = null;
00097 
00102     protected $_lastRequest = null;
00103 
00108     protected $_lastResponse = null;
00109 
00114     protected $_proxyCache = array();
00115 
00120     protected $_skipSystemLookup = false;
00121 
00130     public function __construct($server, Zend_Http_Client $httpClient = null)
00131     {
00132         if ($httpClient === null) {
00133             $this->_httpClient = new Zend_Http_Client();
00134         } else {
00135             $this->_httpClient = $httpClient;
00136         }
00137 
00138         $this->_introspector  = new Zend_XmlRpc_Client_ServerIntrospection($this);
00139         $this->_serverAddress = $server;
00140     }
00141 
00142 
00149     public function setHttpClient(Zend_Http_Client $httpClient)
00150     {
00151         return $this->_httpClient = $httpClient;
00152     }
00153 
00154 
00160     public function getHttpClient()
00161     {
00162         return $this->_httpClient;
00163     }
00164 
00165 
00172     public function setIntrospector(Zend_XmlRpc_Client_ServerIntrospection $introspector)
00173     {
00174         return $this->_introspector = $introspector;
00175     }
00176 
00177 
00183     public function getIntrospector()
00184     {
00185         return $this->_introspector;
00186     }
00187 
00188 
00194     public function getLastRequest()
00195     {
00196         return $this->_lastRequest;
00197     }
00198 
00199 
00205     public function getLastResponse()
00206     {
00207         return $this->_lastResponse;
00208     }
00209 
00210 
00217     public function getProxy($namespace = '')
00218     {
00219         if (empty($this->_proxyCache[$namespace])) {
00220             $proxy = new Zend_XmlRpc_Client_ServerProxy($this, $namespace);
00221             $this->_proxyCache[$namespace] = $proxy;
00222         }
00223         return $this->_proxyCache[$namespace];
00224     }
00225 
00232     public function setSkipSystemLookup($flag = true)
00233     {
00234         $this->_skipSystemLookup = (bool) $flag;
00235         return $this;
00236     }
00237 
00243     public function skipSystemLookup()
00244     {
00245         return $this->_skipSystemLookup;
00246     }
00247 
00256     public function doRequest($request, $response = null)
00257     {
00258         $this->_lastRequest = $request;
00259 
00260         iconv_set_encoding('input_encoding', 'UTF-8');
00261         iconv_set_encoding('output_encoding', 'UTF-8');
00262         iconv_set_encoding('internal_encoding', 'UTF-8');
00263 
00264         $http = $this->getHttpClient();
00265         if($http->getUri() === null) {
00266             $http->setUri($this->_serverAddress);
00267         }
00268 
00269         $http->setHeaders(array(
00270             'Content-Type: text/xml; charset=utf-8',
00271             'Accept: text/xml',
00272         ));
00273 
00274         if ($http->getHeader('user-agent') === null) {
00275             $http->setHeaders(array('User-Agent: Zend_XmlRpc_Client'));
00276         }
00277 
00278         $xml = $this->_lastRequest->__toString();
00279         $http->setRawData($xml);
00280         $httpResponse = $http->request(Zend_Http_Client::POST);
00281 
00282         if (! $httpResponse->isSuccessful()) {
00287             require_once 'Zend/XmlRpc/Client/HttpException.php';
00288             throw new Zend_XmlRpc_Client_HttpException(
00289                                     $httpResponse->getMessage(),
00290                                     $httpResponse->getStatus());
00291         }
00292 
00293         if ($response === null) {
00294             $response = new Zend_XmlRpc_Response();
00295         }
00296         $this->_lastResponse = $response;
00297         $this->_lastResponse->loadXml($httpResponse->getBody());
00298     }
00299 
00308     public function call($method, $params=array())
00309     {
00310         if (!$this->skipSystemLookup() && ('system.' != substr($method, 0, 7))) {
00311             // Ensure empty array/struct params are cast correctly
00312             // If system.* methods are not available, bypass. (ZF-2978)
00313             $success = true;
00314             try {
00315                 $signatures = $this->getIntrospector()->getMethodSignature($method);
00316             } catch (Zend_XmlRpc_Exception $e) {
00317                 $success = false;
00318             }
00319             if ($success) {
00320                 $validTypes = array(
00321                     Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY,
00322                     Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64,
00323                     Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN,
00324                     Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME,
00325                     Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE,
00326                     Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
00327                     Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER,
00328                     Zend_XmlRpc_Value::XMLRPC_TYPE_NIL,
00329                     Zend_XmlRpc_Value::XMLRPC_TYPE_STRING,
00330                     Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT,
00331                 );
00332 
00333                 if (!is_array($params)) {
00334                     $params = array($params);
00335                 }
00336                 foreach ($params as $key => $param) {
00337 
00338                     if ($param instanceof Zend_XmlRpc_Value) {
00339                         continue;
00340                     }
00341 
00342                     $type = Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
00343                     foreach ($signatures as $signature) {
00344                         if (!is_array($signature)) {
00345                             continue;
00346                         }
00347 
00348                         if (isset($signature['parameters'][$key])) {
00349                             $type = $signature['parameters'][$key];
00350                             $type = in_array($type, $validTypes) ? $type : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
00351                         }
00352                     }
00353 
00354                     $params[$key] = Zend_XmlRpc_Value::getXmlRpcValue($param, $type);
00355                 }
00356             }
00357         }
00358 
00359         $request = $this->_createRequest($method, $params);
00360 
00361         $this->doRequest($request);
00362 
00363         if ($this->_lastResponse->isFault()) {
00364             $fault = $this->_lastResponse->getFault();
00369             require_once 'Zend/XmlRpc/Client/FaultException.php';
00370             throw new Zend_XmlRpc_Client_FaultException($fault->getMessage(),
00371                                                         $fault->getCode());
00372         }
00373 
00374         return $this->_lastResponse->getReturnValue();
00375     }
00376 
00382     protected function _createRequest($method, $params)
00383     {
00384         return new Zend_XmlRpc_Request($method, $params);
00385     }
00386 }
 All Data Structures Namespaces Files Functions Variables Enumerations