Overview

Packages

  • None
  • Rpc

Classes

  • Horde_Rpc
  • Horde_Rpc_ActiveSync
  • Horde_Rpc_Exception
  • Horde_Rpc_Phpgw
  • Horde_Rpc_Soap
  • Horde_Rpc_Syncml
  • Horde_Rpc_Syncml_Wbxml
  • Horde_Rpc_Translation
  • Horde_Rpc_Webdav
  • Horde_Rpc_Webdav2
  • Horde_Rpc_Xmlrpc
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_Rpc_xmlrpc class provides an XMLRPC implementation of the
  4:  * Horde RPC system.
  5:  *
  6:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (LGPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 10:  *
 11:  * @author  Jan Schneider <jan@horde.org>
 12:  * @package Rpc
 13:  */
 14: class Horde_Rpc_Xmlrpc extends Horde_Rpc
 15: {
 16:     /**
 17:      * Resource handler for the XMLRPC server.
 18:      *
 19:      * @var resource
 20:      */
 21:     var $_server;
 22: 
 23:     /**
 24:      * XMLRPC server constructor
 25:      *
 26:      * @access private
 27:      */
 28:     public function __construct($request, $params = array())
 29:     {
 30:         parent::__construct($request, $params);
 31: 
 32:         $this->_server = xmlrpc_server_create();
 33: 
 34:         foreach ($GLOBALS['registry']->listMethods() as $method) {
 35:             xmlrpc_server_register_method($this->_server, str_replace('/', '.', $method), array('Horde_Rpc_Xmlrpc', '_dispatcher'));
 36:         }
 37:     }
 38: 
 39:     /**
 40:      * Sends an RPC request to the server and returns the result.
 41:      *
 42:      * @param string  The raw request string.
 43:      *
 44:      * @return string  The XML encoded response from the server.
 45:      */
 46:     function getResponse($request)
 47:     {
 48:         $response = null;
 49:         return xmlrpc_server_call_method($this->_server, $request, $response);
 50:     }
 51: 
 52:     /**
 53:      * Will be registered as the handler for all available methods
 54:      * and will call the appropriate function through the registry.
 55:      *
 56:      * @access private
 57:      *
 58:      * @param string $method  The name of the method called by the RPC request.
 59:      * @param array $params   The passed parameters.
 60:      * @param mixed $data     Unknown.
 61:      *
 62:      * @return mixed  The result of the called registry method.
 63:      */
 64:     function _dispatcher($method, $params, $data)
 65:     {
 66:         global $registry;
 67: 
 68:         $method = str_replace('.', '/', $method);
 69:         if (!$registry->hasMethod($method)) {
 70:             return 'Method "' . $method . '" is not defined';
 71:         }
 72: 
 73:         try {
 74:             $result = $registry->call($method, $params);
 75:         } catch (Horde_Exception $e) {
 76:             $result = array('faultCode' => (int)$e->getCode(),
 77:                             'faultString' => $e->getMessage());
 78:         }
 79: 
 80:         return $result;
 81:     }
 82: 
 83:     /**
 84:      * Builds an XMLRPC request and sends it to the XMLRPC server.
 85:      *
 86:      * This statically called method is actually the XMLRPC client.
 87:      *
 88:      * @param string|Horde_Url $url      The path to the XMLRPC server on the
 89:      *                                   called host.
 90:      * @param string $method             The method to call.
 91:      * @param Horde_Http_Client $client  The transport client
 92:      * @param array $params              A hash containing any necessary
 93:      *                                   parameters for the method call.
 94:      *
 95:      * @return mixed  The returned result from the method.
 96:      * @throws Horde_Rpc_Exception
 97:      */
 98:     public static function request($url, $method, $client, $params = null)
 99:     {
100:         $headers = array(
101:             'User-Agent' => 'Horde RPC client',
102:             'Content-Type' => 'text/xml');
103:         try {
104:             $result = $client->post($url, xmlrpc_encode_request($method, $params), $headers);
105:         } catch (Horde_Http_Client_Exception $e) {
106:             throw new Horde_Rpc_Exception($result);
107:         }
108:         if ($result->code != 200) {
109:             throw new Horde_Rpc_Exception('Request couldn\'t be answered. Returned errorcode: "' . $result->code);
110:         } elseif (strpos($result->getBody(), '<?xml') === false) {
111:             throw new Horde_Rpc_Exception("No valid XML data returned:\n" . $result->getBody());
112:         } else {
113:             $response = @xmlrpc_decode(substr($result->getBody(), strpos($result->getBody(), '<?xml')));
114:             if (is_array($response) && isset($response['faultString'])) {
115:                 throw new Horde_Rpc_Exception($response['faultString']);
116:             } elseif (is_array($response) && isset($response[0]) &&
117:                       is_array($response[0]) && isset($response[0]['faultString'])) {
118:                 throw new Horde_Rpc_Exception($response[0]['faultString']);
119:             }
120: 
121:             return $response;
122:         }
123:     }
124: 
125: }
126: 
API documentation generated by ApiGen