1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Horde_Rpc_Xmlrpc extends Horde_Rpc
15: {
16: 17: 18: 19: 20:
21: var $_server;
22:
23: 24: 25: 26: 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: 41: 42: 43: 44: 45:
46: function getResponse($request)
47: {
48: $response = null;
49: return xmlrpc_server_call_method($this->_server, $request, $response);
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 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: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 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: