[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/RPC/ -> xmlrpc.php (source)

   1  <?php
   2  /**
   3   * The Horde_RPC_xmlrpc class provides an XMLRPC implementation of the
   4   * Horde RPC system.
   5   *
   6   * $Horde: framework/RPC/RPC/xmlrpc.php,v 1.9.10.8 2006/07/01 04:54:16 chuck Exp $
   7   *
   8   * Copyright 2002-2006 Jan Schneider <jan@horde.org>
   9   *
  10   * See the enclosed file COPYING for license information (LGPL). If you
  11   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12   *
  13   * @author  Jan Schneider <jan@horde.org>
  14   * @since   Horde 3.0
  15   * @package Horde_RPC
  16   */
  17  class Horde_RPC_xmlrpc extends Horde_RPC {
  18  
  19      /**
  20       * Resource handler for the XMLRPC server.
  21       *
  22       * @var resource
  23       */
  24      var $_server;
  25  
  26      /**
  27       * XMLRPC server constructor
  28       *
  29       * @access private
  30       */
  31      function Horde_RPC_xmlrpc()
  32      {
  33          parent::Horde_RPC();
  34  
  35          $this->_server = xmlrpc_server_create();
  36  
  37          foreach ($GLOBALS['registry']->listMethods() as $method) {
  38              xmlrpc_server_register_method($this->_server, str_replace('/', '.', $method), array('Horde_RPC_xmlrpc', '_dispatcher'));
  39          }
  40      }
  41  
  42      /**
  43       * Cleans up the RPC server.
  44       */
  45      function shutdown()
  46      {
  47          xmlrpc_server_destroy($this->_server);
  48      }
  49  
  50      /**
  51       * Sends an RPC request to the server and returns the result.
  52       *
  53       * @param string  The raw request string.
  54       *
  55       * @return string  The XML encoded response from the server.
  56       */
  57      function getResponse($request)
  58      {
  59          $response = null;
  60          return xmlrpc_server_call_method($this->_server, $request, $response);
  61      }
  62  
  63      /**
  64       * Will be registered as the handler for all available methods
  65       * and will call the appropriate function through the registry.
  66       *
  67       * @access private
  68       *
  69       * @param string $method  The name of the method called by the RPC request.
  70       * @param array $params   The passed parameters.
  71       * @param mixed $data     Unknown.
  72       *
  73       * @return mixed  The result of the called registry method.
  74       */
  75      function _dispatcher($method, $params, $data)
  76      {
  77          global $registry;
  78  
  79          $method = str_replace('.', '/', $method);
  80          if (!$registry->hasMethod($method)) {
  81              return sprintf(_("Method \"%s\" is not defined"), $method);
  82          }
  83  
  84          $result = $registry->call($method, $params);
  85          if (is_a($result, 'PEAR_Error')) {
  86              $result = array('faultCode' => (int)$result->getCode(),
  87                              'faultString' => $result->getMessage());
  88          }
  89  
  90          return $result;
  91      }
  92  
  93      /**
  94       * Builds an XMLRPC request and sends it to the XMLRPC server.
  95       *
  96       * This statically called method is actually the XMLRPC client.
  97       *
  98       * @param string $url     The path to the XMLRPC server on the called host.
  99       * @param string $method  The method to call.
 100       * @param array $params   A hash containing any necessary parameters for
 101       *                        the method call.
 102       * @param $options  Optional associative array of parameters which can be:
 103       *                  user           - Basic Auth username
 104       *                  pass           - Basic Auth password
 105       *                  proxy_host     - Proxy server host
 106       *                  proxy_port     - Proxy server port
 107       *                  proxy_user     - Proxy auth username
 108       *                  proxy_pass     - Proxy auth password
 109       *                  timeout        - Connection timeout in seconds.
 110       *                  allowRedirects - Whether to follow redirects or not
 111       *                  maxRedirects   - Max number of redirects to follow
 112       *
 113       * @return mixed            The returned result from the method or a PEAR
 114       *                          error object on failure.
 115       */
 116      function request($url, $method, $params = null, $options = array())
 117      {
 118          $options['method'] = 'POST';
 119          $language = isset($GLOBALS['language']) ? $GLOBALS['language'] :
 120                      (isset($_SERVER['LANG']) ? $_SERVER['LANG'] : '');
 121  
 122          if (!isset($options['timeout'])) {
 123              $options['timeout'] = 5;
 124          }
 125          if (!isset($options['allowRedirects'])) {
 126              $options['allowRedirects'] = true;
 127              $options['maxRedirects'] = 3;
 128          }
 129  
 130          require_once 'HTTP/Request.php';
 131  
 132          $http = &new HTTP_Request($url, $options);
 133          if (!empty($language)) {
 134              $http->addHeader('Accept-Language', $language);
 135          }
 136          $http->addHeader('User-Agent', 'Horde RPC client');
 137          $http->addHeader('Content-Type', 'text/xml');
 138          $http->addRawPostData(xmlrpc_encode_request($method, $params));
 139  
 140          $result = $http->sendRequest();
 141          if (is_a($result, 'PEAR_Error')) {
 142              return $result;
 143          } elseif ($http->getResponseCode() != 200) {
 144              return PEAR::raiseError(_("Request couldn't be answered. Returned errorcode: ") . $http->getResponseCode(), 'horde.error');
 145          } elseif (strpos($http->getResponseBody(), '<?xml') === false) {
 146              return PEAR::raiseError(_("No valid XML data returned"), 'horde.error', null, null, $http->getResponseBody());
 147          } else {
 148              $response = @xmlrpc_decode(substr($http->getResponseBody(), strpos($http->getResponseBody(), '<?xml')));
 149              if (is_array($response) && isset($response['faultString'])) {
 150                  return PEAR::raiseError($response['faultString'], 'horde.error');
 151              } elseif (is_array($response) && isset($response[0]) &&
 152                        is_array($response[0]) && isset($response[0]['faultString'])) {
 153                  return PEAR::raiseError($response[0]['faultString'], 'horde.error');
 154              }
 155              return $response;
 156          }
 157      }
 158  
 159  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7