xmlrpcs.inc 10.9 KB
Newer Older
Dries's avatar
 
Dries committed
1
2
<?php

3
4
5
6
7
/**
 * @file
 * Provides API for defining and handling XML-RPC requests.
 */

8
/**
9
 * Invokes XML-RPC methods on this server.
10
 *
11
 * @param array $callbacks
12
13
 *   Array of external XML-RPC method names with the callbacks they map to.
 */
14
15
function xmlrpc_server($callbacks) {
  $xmlrpc_server = new stdClass();
16
  // Define built-in XML-RPC method names
17
  $defaults = array(
18
    'system.multicall' => 'xmlrpc_server_multicall',
19
20
21
22
    array(
      'system.methodSignature',
      'xmlrpc_server_method_signature',
      array('array', 'string'),
23
      'Returns an array describing the return type and required parameters of a method.',
24
25
26
27
28
    ),
    array(
      'system.getCapabilities',
      'xmlrpc_server_get_capabilities',
      array('struct'),
29
      'Returns a struct describing the XML-RPC specifications supported by this server.',
30
31
32
33
34
    ),
    array(
      'system.listMethods',
      'xmlrpc_server_list_methods',
      array('array'),
35
36
      'Returns an array of available methods on this server.',
    ),
37
38
39
40
    array(
      'system.methodHelp',
      'xmlrpc_server_method_help',
      array('string', 'string'),
41
42
      'Returns a documentation string for the specified method.',
    ),
43
  );
44
45
46
  // We build an array of all method names by combining the built-ins
  // with those defined by modules implementing the _xmlrpc hook.
  // Built-in methods are overridable.
47
48
49
  $callbacks = array_merge($defaults, (array) $callbacks);
  drupal_alter('xmlrpc', $callbacks);
  foreach ($callbacks as $key => $callback) {
50
51
52
53
54
55
56
57
58
59
60
61
62
    // we could check for is_array($callback)
    if (is_int($key)) {
      $method = $callback[0];
      $xmlrpc_server->callbacks[$method] = $callback[1];
      $xmlrpc_server->signatures[$method] = $callback[2];
      $xmlrpc_server->help[$method] = $callback[3];
    }
    else {
      $xmlrpc_server->callbacks[$key] = $callback;
      $xmlrpc_server->signatures[$key] = '';
      $xmlrpc_server->help[$key] = '';
    }
  }
Dries's avatar
 
Dries committed
63

64
  $data = file_get_contents('php://input');
65
  if (!$data) {
66
67
    print 'XML-RPC server accepts POST requests only.';
    drupal_exit();
Dries's avatar
   
Dries committed
68
  }
69
70
  $xmlrpc_server->message = xmlrpc_message($data);
  if (!xmlrpc_message_parse($xmlrpc_server->message)) {
71
    xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
Dries's avatar
   
Dries committed
72
  }
73
  if ($xmlrpc_server->message->messagetype != 'methodCall') {
74
    xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
Dries's avatar
   
Dries committed
75
  }
76
77
78
  if (!isset($xmlrpc_server->message->params)) {
    $xmlrpc_server->message->params = array();
  }
79
  xmlrpc_server_set($xmlrpc_server);
80
  $result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
81

82
  if (is_object($result) && !empty($result->is_error)) {
83
    xmlrpc_server_error($result);
Dries's avatar
   
Dries committed
84
  }
85
86
87
88
89
90
91
  // Encode the result
  $r = xmlrpc_value($result);
  // Create the XML
  $xml = '
<methodResponse>
  <params>
  <param>
92
    <value>' . xmlrpc_value_get_xml($r) . '</value>
93
94
95
  </param>
  </params>
</methodResponse>
Dries's avatar
 
Dries committed
96

97
98
99
100
';
  // Send it
  xmlrpc_server_output($xml);
}
Dries's avatar
 
Dries committed
101

102
/**
103
 * Throws an XML-RPC error.
104
105
 *
 * @param $error
106
 *   An error object or integer error code.
107
 * @param $message
108
109
 *   (optional) The description of the error. Used only if an integer error
 *   code was passed in.
110
 */
111
function xmlrpc_server_error($error, $message = FALSE) {
112
113
  if ($message && !is_object($error)) {
    $error = xmlrpc_error($error, $message);
Dries's avatar
   
Dries committed
114
  }
115
  xmlrpc_server_output(xmlrpc_error_get_xml($error));
Dries's avatar
 
Dries committed
116
117
}

118
119
120
121
122
123
/**
 * Sends XML-RPC output to the browser.
 *
 * @param string $xml
 *   XML to send to the browser.
 */
124
function xmlrpc_server_output($xml) {
125
  $xml = '<?xml version="1.0"?>' . "\n" . $xml;
126
127
  drupal_add_http_header('Content-Length', strlen($xml));
  drupal_add_http_header('Content-Type', 'text/xml');
128
  echo $xml;
129
  drupal_exit();
Dries's avatar
 
Dries committed
130
131
}

132
/**
133
134
135
136
137
 * Stores a copy of an XML-RPC request temporarily.
 *
 * @param object $xmlrpc_server
 *   (optional) Request object created by xmlrpc_server(). Omit to leave the
 *   previous server object saved.
138
 *
139
140
141
142
 * @return
 *   The latest stored request.
 *
 * @see xmlrpc_server_get()
143
 */
144
145
146
147
function xmlrpc_server_set($xmlrpc_server = NULL) {
  static $server;
  if (!isset($server)) {
    $server = $xmlrpc_server;
Dries's avatar
   
Dries committed
148
  }
149
150
  return $server;
}
Dries's avatar
 
Dries committed
151

152
153
154
155
156
157
158
159
/**
 * Retrieves the latest stored XML-RPC request.
 *
 * @return object
 *   The stored request.
 *
 * @see xmlrpc_server_set()
 */
160
161
162
function xmlrpc_server_get() {
  return xmlrpc_server_set();
}
Dries's avatar
 
Dries committed
163

164
/**
165
166
167
168
169
170
171
172
173
 * Dispatches an XML-RPC request and any parameters to the appropriate handler.
 *
 * @param object $xmlrpc_server
 *   Object containing information about this XML-RPC server, the methods it
 *   provides, their signatures, etc.
 * @param string $methodname
 *   The external XML-RPC method name; e.g., 'system.methodHelp'.
 * @param array $args
 *   Array containing any parameters that are to be sent along with the request.
174
 *
175
176
 * @return
 *   The results of the call.
177
 */
178
function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
179
  // Make sure parameters are in an array
180
181
  if ($args && !is_array($args)) {
    $args = array($args);
Dries's avatar
   
Dries committed
182
  }
183
  // Has this method been mapped to a Drupal function by us or by modules?
184
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
185
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $xmlrpc_server->message->methodname)));
186
187
188
  }
  $method = $xmlrpc_server->callbacks[$methodname];
  $signature = $xmlrpc_server->signatures[$methodname];
Dries's avatar
 
Dries committed
189

190
  // If the method has a signature, validate the request against the signature
191
  if (is_array($signature)) {
192
    $ok = TRUE;
193
194
195
    $return_type = array_shift($signature);
    // Check the number of arguments
    if (count($args) != count($signature)) {
196
      return xmlrpc_error(-32602, t('Server error. Wrong number of method parameters.'));
Dries's avatar
   
Dries committed
197
    }
198
199
200
201
202
203
204
    // Check the argument types
    foreach ($signature as $key => $type) {
      $arg = $args[$key];
      switch ($type) {
        case 'int':
        case 'i4':
          if (is_array($arg) || !is_int($arg)) {
205
            $ok = FALSE;
206
207
          }
          break;
208

209
210
211
        case 'base64':
        case 'string':
          if (!is_string($arg)) {
212
            $ok = FALSE;
213
214
          }
          break;
215

216
        case 'boolean':
217
218
          if ($arg !== FALSE && $arg !== TRUE) {
            $ok = FALSE;
219
220
          }
          break;
221

222
223
224
        case 'float':
        case 'double':
          if (!is_float($arg)) {
225
            $ok = FALSE;
226
227
          }
          break;
228

229
230
231
        case 'date':
        case 'dateTime.iso8601':
          if (!$arg->is_date) {
232
            $ok = FALSE;
233
234
          }
          break;
Dries's avatar
   
Dries committed
235
      }
236
      if (!$ok) {
237
        return xmlrpc_error(-32602, t('Server error. Invalid method parameters.'));
Dries's avatar
   
Dries committed
238
239
240
      }
    }
  }
241

242
  if (!function_exists($method)) {
243
    return xmlrpc_error(-32601, t('Server error. Requested function @method does not exist.', array("@method" => $method)));
244
  }
245
  // Call the mapped function
246
247
  return call_user_func_array($method, $args);
}
Dries's avatar
 
Dries committed
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * Dispatches multiple XML-RPC requests.
 *
 * @param array $methodcalls
 *   An array of XML-RPC requests to make. Each request is an array with the
 *   following elements:
 *   - methodName: Name of the method to invoke.
 *   - params: Parameters to pass to the method.
 *
 * @return
 *   An array of the results of each request.
 *
 * @see xmlrpc_server_call()
 */
263
264
265
266
267
function xmlrpc_server_multicall($methodcalls) {
  // See http://www.xmlrpc.com/discuss/msgReader$1208
  $return = array();
  $xmlrpc_server = xmlrpc_server_get();
  foreach ($methodcalls as $call) {
268
    $ok = TRUE;
269
270
    if (!isset($call['methodName']) || !isset($call['params'])) {
      $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
271
      $ok = FALSE;
272
273
274
275
276
277
278
279
280
    }
    $method = $call['methodName'];
    $params = $call['params'];
    if ($method == 'system.multicall') {
      $result = xmlrpc_error(-32600, t('Recursive calls to system.multicall are forbidden.'));
    }
    elseif ($ok) {
      $result = xmlrpc_server_call($xmlrpc_server, $method, $params);
    }
281
    if (is_object($result) && !empty($result->is_error)) {
282
283
      $return[] = array(
        'faultCode' => $result->code,
284
        'faultString' => $result->message,
285
286
287
      );
    }
    else {
288
      $return[] = array($result);
289
290
291
292
293
294
    }
  }
  return $return;
}

/**
295
296
 * Lists the methods available on this XML-RPC server.
 *
297
 * XML-RPC method system.listMethods maps to this function.
298
299
300
 *
 * @return array
 *   Array of the names of methods available on this server.
301
302
303
304
305
306
307
 */
function xmlrpc_server_list_methods() {
  $xmlrpc_server = xmlrpc_server_get();
  return array_keys($xmlrpc_server->callbacks);
}

/**
308
309
 * Returns a list of the capabilities of this server.
 *
310
 * XML-RPC method system.getCapabilities maps to this function.
311
 *
312
313
314
 * @return array
 *   Array of server capabilities.
 *
315
 * @see http://groups.yahoo.com/group/xml-rpc/message/2897
316
317
318
319
320
 */
function xmlrpc_server_get_capabilities() {
  return array(
    'xmlrpc' => array(
      'specUrl' => 'http://www.xmlrpc.com/spec',
321
      'specVersion' => 1,
322
323
324
    ),
    'faults_interop' => array(
      'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
325
      'specVersion' => 20010516,
326
327
328
    ),
    'system.multicall' => array(
      'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
329
      'specVersion' => 1,
330
331
    ),
    'introspection' => array(
332
333
334
      'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
      'specVersion' => 1,
    ),
335
336
337
338
  );
}

/**
339
 * Returns one method signature for a function.
340
 *
341
 * This is the function mapped to the XML-RPC method system.methodSignature.
342
 *
343
344
345
346
347
348
349
350
 * A method signature is an array of the input and output types of a method. For
 * instance, the method signature of this function is array('array', 'string'),
 * because it takes an array and returns a string.
 *
 * @param string $methodname
 *   Name of method to return a method signature for.
 *
 * @return array
351
352
 *   An array of arrays of types, each of the arrays representing one method
 *   signature of the function that $methodname maps to.
353
354
355
 */
function xmlrpc_server_method_signature($methodname) {
  $xmlrpc_server = xmlrpc_server_get();
356
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
357
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $methodname)));
358
  }
359
  if (!is_array($xmlrpc_server->signatures[$methodname])) {
360
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname signature not specified.', array("@methodname" => $methodname)));
361
  }
362
  // We array of types
363
364
  $return = array();
  foreach ($xmlrpc_server->signatures[$methodname] as $type) {
365
    $return[] = $type;
Dries's avatar
 
Dries committed
366
  }
367
  return array($return);
368
369
}

370
/**
371
372
 * Returns the help for an XML-RPC method.
 *
373
374
 * XML-RPC method system.methodHelp maps to this function.
 *
375
 * @param string $method
376
 *   Name of method for which we return a help string.
377
378
379
 *
 * @return string
 *   Help text for $method.
380
 */
381
382
function xmlrpc_server_method_help($method) {
  $xmlrpc_server = xmlrpc_server_get();
383
  return $xmlrpc_server->help[$method];
384
}