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_Phpgw class provides an XMLRPC implementation of the
  4:  * Horde RPC system compatible with phpgw. It is based on the
  5:  * xmlrpc.php implementation by Jan Schneider.
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Michael Braun <mi.braun@onlinehome.de>
 11:  * @category Horde
 12:  * @package  Rpc
 13:  */
 14: class Horde_Rpc_Phpgw extends Horde_Rpc
 15: {
 16:     /**
 17:      * Resource handler for the XML-RPC server.
 18:      *
 19:      * @var resource
 20:      */
 21:     var $_server;
 22: 
 23:     /**
 24:      * XMLRPC server constructor.
 25:      */
 26:     function __construct($request, $params = array())
 27:     {
 28:         parent::__construct($request, $params);
 29: 
 30:         $this->_server = xmlrpc_server_create();
 31: 
 32:         // Register only phpgw services.
 33:         foreach ($GLOBALS['registry']->listMethods('phpgw') as $method) {
 34:             $methods = explode('/', $method);
 35:             array_shift($methods);
 36:             $method = implode('.', $methods);
 37:             xmlrpc_server_register_method($this->_server, $method, array('Horde_Rpc_Phpgw', '_dispatcher'));
 38:         }
 39:     }
 40: 
 41:     /**
 42:      * Authorization is done by xmlrpc method system.login.
 43:      */
 44:     function authorize()
 45:     {
 46:         return true;
 47:     }
 48: 
 49:     /**
 50:      * Sends an RPC request to the server and returns the result.
 51:      *
 52:      * @param string  The raw request string.
 53:      *
 54:      * @return string  The XML encoded response from the server.
 55:      */
 56:     function getResponse($request)
 57:     {
 58:         $response = null;
 59:         return xmlrpc_server_call_method($this->_server, $request, $response);
 60:     }
 61: 
 62:     /**
 63:      * Will be registered as the handler for all available methods
 64:      * and will call the appropriate function through the registry.
 65:      *
 66:      * @access private
 67:      *
 68:      * @param string $method  The name of the method called by the RPC request.
 69:      * @param array $params   The passed parameters.
 70:      * @param mixed $data     Unknown.
 71:      *
 72:      * @return mixed  The result of the called registry method.
 73:      */
 74:     function _dispatcher($method, $params, $data)
 75:     {
 76:         global $registry;
 77:         $method = str_replace('.', '/', 'phpgw.' . $method);
 78: 
 79:         if (!$registry->hasMethod($method)) {
 80:             Horde::logMessage(sprintf(Horde_Rpc_Translation::t("Method \"%s\" is not defined"), $method), 'NOTICE');
 81:             return sprintf(Horde_Rpc_Translation::t("Method \"%s\" is not defined"), $method);
 82:         }
 83: 
 84:         // Try to resume a session
 85:         if (isset($params[0]['kp3']) && $params[0]["kp3"] == session_name() && session_id() != $params[0]["sessionid"]) {
 86:             Horde::logMessage("manually reload session ".$params[0]["sessionid"], 'NOTICE');
 87:             session_regenerate_id();
 88:             session_unset();
 89:             session_id($params[0]["sessionid"]);
 90:         }
 91: 
 92:         // Be authenticated or call system.login.
 93:         $authenticated = $registry->isAuthenticated() || $method== "phpgw/system/login";
 94: 
 95:         if ($authenticated) {
 96:             Horde::logMessage("rpc call $method allowed", 'NOTICE');
 97:             return $registry->call($method, $params);
 98:         } else {
 99:             return PEAR::raiseError(Horde_Rpc_Translation::t("You did not authenticate."), 'horde.error');
100:             // return parent::authorize();
101:             // error 9 "access denied"
102:         }
103:     }
104: 
105:     /**
106:      * Builds an XMLRPC request and sends it to the XMLRPC server.
107:      *
108:      * This statically called method is actually the XMLRPC client.
109:      *
110:      * @param string|Horde_Url $url     The path to the XMLRPC server on the
111:      *                                  called host.
112:      * @param string $method             The method to call.
113:      * @param Horde_Http_Client $client  The transport client
114:      * @param array $params              A hash containing any necessary
115:      *                                   parameters for the method call.
116:      *
117:      * @return mixed  The returned result from the method.
118:      * @throws Horde_Rpc_Exception
119:      */
120:     public static function request($url, $method, $client, $params = null)
121:     {
122:         $options['method'] = 'POST';
123:         $headers = array(
124:             'User-Agent' => 'Horde RPC client',
125:             'Content-Type', 'text/xml');
126:         try {
127:             $result = $client->post($url, xmlrpc_encode_request($method, $params), $headers);
128:         } catch (Horde_Http_Client_Exception $e) {
129:             throw new Horde_Rpc_Exception($result);
130:         }
131:         if ($result->code != 200) {
132:             throw new Horde_Rpc_Exception(Horde_Rpc_Translation::t("Request couldn't be answered. Returned errorcode: ") . $result->code);
133:         } elseif (strpos($result->getBody(), '<?xml') === false) {
134:             throw new Horde_Rpc_Exception(Horde_Rpc_Translation::t("No valid XML data returned:") . "\n" . $result->getBody());
135:         } else {
136:             $response = @xmlrpc_decode(substr($result->getBody(), strpos($result->getBody(), '<?xml')));
137:             if (is_array($response) && isset($response['faultString'])) {
138:                 throw new Horde_Rpc_Exception($response['faultString']);
139:             } elseif (is_array($response) && isset($response[0]) &&
140:                       is_array($response[0]) && isset($response[0]['faultString'])) {
141:                 throw new Horde_Rpc_Exception($response[0]['faultString']);
142:             }
143:             return $response;
144:         }
145:     }
146: 
147: }
148: 
API documentation generated by ApiGen