Projet

Général

Profil

Paste
Télécharger (11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / includes / xmlrpcs.inc @ 76597ebf

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Provides API for defining and handling XML-RPC requests.
6
 */
7
8
/**
9
 * Invokes XML-RPC methods on this server.
10
 *
11
 * @param array $callbacks
12 e33d3026 Julien Enselme
 *   Either an associative array of external XML-RPC method names as keys with
13
 *   the callbacks they map to as values, or a more complex structure
14
 *   describing XML-RPC callbacks as returned from hook_xmlrpc().
15 85ad3d82 Assos Assos
 */
16
function xmlrpc_server($callbacks) {
17
  $xmlrpc_server = new stdClass();
18
  // Define built-in XML-RPC method names
19
  $defaults = array(
20
    'system.multicall' => 'xmlrpc_server_multicall',
21
    array(
22
      'system.methodSignature',
23
      'xmlrpc_server_method_signature',
24
      array('array', 'string'),
25
      'Returns an array describing the return type and required parameters of a method.',
26
    ),
27
    array(
28
      'system.getCapabilities',
29
      'xmlrpc_server_get_capabilities',
30
      array('struct'),
31
      'Returns a struct describing the XML-RPC specifications supported by this server.',
32
    ),
33
    array(
34
      'system.listMethods',
35
      'xmlrpc_server_list_methods',
36
      array('array'),
37
      'Returns an array of available methods on this server.',
38
    ),
39
    array(
40
      'system.methodHelp',
41
      'xmlrpc_server_method_help',
42
      array('string', 'string'),
43
      'Returns a documentation string for the specified method.',
44
    ),
45
  );
46
  // We build an array of all method names by combining the built-ins
47
  // with those defined by modules implementing the _xmlrpc hook.
48
  // Built-in methods are overridable.
49
  $callbacks = array_merge($defaults, (array) $callbacks);
50
  drupal_alter('xmlrpc', $callbacks);
51
  foreach ($callbacks as $key => $callback) {
52
    // we could check for is_array($callback)
53
    if (is_int($key)) {
54
      $method = $callback[0];
55
      $xmlrpc_server->callbacks[$method] = $callback[1];
56
      $xmlrpc_server->signatures[$method] = $callback[2];
57
      $xmlrpc_server->help[$method] = $callback[3];
58
    }
59
    else {
60
      $xmlrpc_server->callbacks[$key] = $callback;
61
      $xmlrpc_server->signatures[$key] = '';
62
      $xmlrpc_server->help[$key] = '';
63
    }
64
  }
65
66
  $data = file_get_contents('php://input');
67
  if (!$data) {
68
    print 'XML-RPC server accepts POST requests only.';
69
    drupal_exit();
70
  }
71
  $xmlrpc_server->message = xmlrpc_message($data);
72
  if (!xmlrpc_message_parse($xmlrpc_server->message)) {
73
    xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
74
  }
75
  if ($xmlrpc_server->message->messagetype != 'methodCall') {
76
    xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
77
  }
78
  if (!isset($xmlrpc_server->message->params)) {
79
    $xmlrpc_server->message->params = array();
80
  }
81
  xmlrpc_server_set($xmlrpc_server);
82
  $result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
83
84
  if (is_object($result) && !empty($result->is_error)) {
85
    xmlrpc_server_error($result);
86
  }
87
  // Encode the result
88
  $r = xmlrpc_value($result);
89
  // Create the XML
90
  $xml = '
91
<methodResponse>
92
  <params>
93
  <param>
94
    <value>' . xmlrpc_value_get_xml($r) . '</value>
95
  </param>
96
  </params>
97
</methodResponse>
98
99
';
100
  // Send it
101
  xmlrpc_server_output($xml);
102
}
103
104
/**
105
 * Throws an XML-RPC error.
106
 *
107
 * @param $error
108
 *   An error object or integer error code.
109
 * @param $message
110
 *   (optional) The description of the error. Used only if an integer error
111
 *   code was passed in.
112
 */
113
function xmlrpc_server_error($error, $message = FALSE) {
114
  if ($message && !is_object($error)) {
115
    $error = xmlrpc_error($error, $message);
116
  }
117
  xmlrpc_server_output(xmlrpc_error_get_xml($error));
118
}
119
120
/**
121
 * Sends XML-RPC output to the browser.
122
 *
123
 * @param string $xml
124
 *   XML to send to the browser.
125
 */
126
function xmlrpc_server_output($xml) {
127
  $xml = '<?xml version="1.0"?>' . "\n" . $xml;
128
  drupal_add_http_header('Content-Length', strlen($xml));
129
  drupal_add_http_header('Content-Type', 'text/xml');
130
  echo $xml;
131
  drupal_exit();
132
}
133
134
/**
135
 * Stores a copy of an XML-RPC request temporarily.
136
 *
137
 * @param object $xmlrpc_server
138
 *   (optional) Request object created by xmlrpc_server(). Omit to leave the
139
 *   previous server object saved.
140
 *
141
 * @return
142
 *   The latest stored request.
143
 *
144
 * @see xmlrpc_server_get()
145
 */
146
function xmlrpc_server_set($xmlrpc_server = NULL) {
147
  static $server;
148
  if (!isset($server)) {
149
    $server = $xmlrpc_server;
150
  }
151
  return $server;
152
}
153
154
/**
155
 * Retrieves the latest stored XML-RPC request.
156
 *
157
 * @return object
158
 *   The stored request.
159
 *
160
 * @see xmlrpc_server_set()
161
 */
162
function xmlrpc_server_get() {
163
  return xmlrpc_server_set();
164
}
165
166
/**
167
 * Dispatches an XML-RPC request and any parameters to the appropriate handler.
168
 *
169
 * @param object $xmlrpc_server
170
 *   Object containing information about this XML-RPC server, the methods it
171
 *   provides, their signatures, etc.
172
 * @param string $methodname
173
 *   The external XML-RPC method name; e.g., 'system.methodHelp'.
174
 * @param array $args
175
 *   Array containing any parameters that are to be sent along with the request.
176
 *
177
 * @return
178
 *   The results of the call.
179
 */
180
function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
181
  // Make sure parameters are in an array
182
  if ($args && !is_array($args)) {
183
    $args = array($args);
184
  }
185
  // Has this method been mapped to a Drupal function by us or by modules?
186
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
187
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $xmlrpc_server->message->methodname)));
188
  }
189
  $method = $xmlrpc_server->callbacks[$methodname];
190
  $signature = $xmlrpc_server->signatures[$methodname];
191
192
  // If the method has a signature, validate the request against the signature
193
  if (is_array($signature)) {
194
    $ok = TRUE;
195
    $return_type = array_shift($signature);
196
    // Check the number of arguments
197
    if (count($args) != count($signature)) {
198
      return xmlrpc_error(-32602, t('Server error. Wrong number of method parameters.'));
199
    }
200
    // Check the argument types
201
    foreach ($signature as $key => $type) {
202
      $arg = $args[$key];
203
      switch ($type) {
204
        case 'int':
205
        case 'i4':
206
          if (is_array($arg) || !is_int($arg)) {
207
            $ok = FALSE;
208
          }
209
          break;
210
211
        case 'base64':
212
        case 'string':
213
          if (!is_string($arg)) {
214
            $ok = FALSE;
215
          }
216
          break;
217
218
        case 'boolean':
219
          if ($arg !== FALSE && $arg !== TRUE) {
220
            $ok = FALSE;
221
          }
222
          break;
223
224
        case 'float':
225
        case 'double':
226
          if (!is_float($arg)) {
227
            $ok = FALSE;
228
          }
229
          break;
230
231
        case 'date':
232
        case 'dateTime.iso8601':
233
          if (!$arg->is_date) {
234
            $ok = FALSE;
235
          }
236
          break;
237
      }
238
      if (!$ok) {
239
        return xmlrpc_error(-32602, t('Server error. Invalid method parameters.'));
240
      }
241
    }
242
  }
243
244
  if (!function_exists($method)) {
245
    return xmlrpc_error(-32601, t('Server error. Requested function @method does not exist.', array("@method" => $method)));
246
  }
247
  // Call the mapped function
248
  return call_user_func_array($method, $args);
249
}
250
251
/**
252
 * Dispatches multiple XML-RPC requests.
253
 *
254
 * @param array $methodcalls
255
 *   An array of XML-RPC requests to make. Each request is an array with the
256
 *   following elements:
257
 *   - methodName: Name of the method to invoke.
258
 *   - params: Parameters to pass to the method.
259
 *
260
 * @return
261
 *   An array of the results of each request.
262
 *
263
 * @see xmlrpc_server_call()
264
 */
265
function xmlrpc_server_multicall($methodcalls) {
266
  // See http://www.xmlrpc.com/discuss/msgReader$1208
267
  $return = array();
268
  $xmlrpc_server = xmlrpc_server_get();
269
  foreach ($methodcalls as $call) {
270
    $ok = TRUE;
271
    if (!isset($call['methodName']) || !isset($call['params'])) {
272
      $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
273
      $ok = FALSE;
274
    }
275
    $method = $call['methodName'];
276
    $params = $call['params'];
277
    if ($method == 'system.multicall') {
278
      $result = xmlrpc_error(-32600, t('Recursive calls to system.multicall are forbidden.'));
279
    }
280
    elseif ($ok) {
281
      $result = xmlrpc_server_call($xmlrpc_server, $method, $params);
282
    }
283
    if (is_object($result) && !empty($result->is_error)) {
284
      $return[] = array(
285
        'faultCode' => $result->code,
286
        'faultString' => $result->message,
287
      );
288
    }
289
    else {
290
      $return[] = array($result);
291
    }
292
  }
293
  return $return;
294
}
295
296
/**
297
 * Lists the methods available on this XML-RPC server.
298
 *
299
 * XML-RPC method system.listMethods maps to this function.
300
 *
301
 * @return array
302
 *   Array of the names of methods available on this server.
303
 */
304
function xmlrpc_server_list_methods() {
305
  $xmlrpc_server = xmlrpc_server_get();
306
  return array_keys($xmlrpc_server->callbacks);
307
}
308
309
/**
310
 * Returns a list of the capabilities of this server.
311
 *
312
 * XML-RPC method system.getCapabilities maps to this function.
313
 *
314
 * @return array
315
 *   Array of server capabilities.
316
 *
317
 * @see http://groups.yahoo.com/group/xml-rpc/message/2897
318
 */
319
function xmlrpc_server_get_capabilities() {
320
  return array(
321
    'xmlrpc' => array(
322
      'specUrl' => 'http://www.xmlrpc.com/spec',
323
      'specVersion' => 1,
324
    ),
325
    'faults_interop' => array(
326
      'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
327
      'specVersion' => 20010516,
328
    ),
329
    'system.multicall' => array(
330
      'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
331
      'specVersion' => 1,
332
    ),
333
    'introspection' => array(
334
      'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
335
      'specVersion' => 1,
336
    ),
337
  );
338
}
339
340
/**
341
 * Returns one method signature for a function.
342
 *
343
 * This is the function mapped to the XML-RPC method system.methodSignature.
344
 *
345
 * A method signature is an array of the input and output types of a method. For
346
 * instance, the method signature of this function is array('array', 'string'),
347
 * because it takes an array and returns a string.
348
 *
349
 * @param string $methodname
350
 *   Name of method to return a method signature for.
351
 *
352
 * @return array
353
 *   An array of arrays of types, each of the arrays representing one method
354
 *   signature of the function that $methodname maps to.
355
 */
356
function xmlrpc_server_method_signature($methodname) {
357
  $xmlrpc_server = xmlrpc_server_get();
358
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
359
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $methodname)));
360
  }
361
  if (!is_array($xmlrpc_server->signatures[$methodname])) {
362
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname signature not specified.', array("@methodname" => $methodname)));
363
  }
364
  // We array of types
365
  $return = array();
366
  foreach ($xmlrpc_server->signatures[$methodname] as $type) {
367
    $return[] = $type;
368
  }
369
  return array($return);
370
}
371
372
/**
373
 * Returns the help for an XML-RPC method.
374
 *
375
 * XML-RPC method system.methodHelp maps to this function.
376
 *
377
 * @param string $method
378
 *   Name of method for which we return a help string.
379
 *
380
 * @return string
381
 *   Help text for $method.
382
 */
383
function xmlrpc_server_method_help($method) {
384
  $xmlrpc_server = xmlrpc_server_get();
385
  return $xmlrpc_server->help[$method];
386
}