xmlrpc.server.inc 11.7 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Page callback file for the xmlrpc module.
6 7
 */

8 9 10
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

11 12
/**
 * Process an XML-RPC request.
13 14 15
 *
 * @return \Symfony\Component\HttpFoundation\Response
 *   A Response object.
16
 *
17 18
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal\xmlrpc\Controller\XmlrpcController::php().
19 20 21
 */
function xmlrpc_server_page() {
  module_load_include('inc', 'xmlrpc');
22
  return xmlrpc_server(\Drupal::moduleHandler()->invokeAll('xmlrpc'));
23 24
}

25
/**
26
 * Invokes XML-RPC methods on this server.
27
 *
28
 * @param array $callbacks
29
 *   Array of external XML-RPC method names with the callbacks they map to.
30 31 32
 *
 * @return \Symfony\Component\HttpFoundation\Response
 *   A Response object.
33
 */
34 35
function xmlrpc_server($callbacks) {
  $xmlrpc_server = new stdClass();
36
  // Define built-in XML-RPC method names
37
  $defaults = array(
38
    'system.multicall' => 'xmlrpc_server_multicall',
39 40 41 42
    array(
      'system.methodSignature',
      'xmlrpc_server_method_signature',
      array('array', 'string'),
43
      'Returns an array describing the return type and required parameters of a method.',
44 45 46 47 48
    ),
    array(
      'system.getCapabilities',
      'xmlrpc_server_get_capabilities',
      array('struct'),
49
      'Returns a struct describing the XML-RPC specifications supported by this server.',
50 51 52 53 54
    ),
    array(
      'system.listMethods',
      'xmlrpc_server_list_methods',
      array('array'),
55 56
      'Returns an array of available methods on this server.',
    ),
57 58 59 60
    array(
      'system.methodHelp',
      'xmlrpc_server_method_help',
      array('string', 'string'),
61 62
      'Returns a documentation string for the specified method.',
    ),
63
  );
64 65 66
  // 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.
67
  $callbacks = array_merge($defaults, (array) $callbacks);
68
  \Drupal::moduleHandler()->alter('xmlrpc', $callbacks);
69
  foreach ($callbacks as $key => $callback) {
70 71 72 73 74 75 76 77 78 79 80 81 82
    // 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] = '';
    }
  }
83

84
  $data = file_get_contents('php://input');
85
  if (!$data) {
86
    throw new BadRequestHttpException('XML-RPC server accepts POST requests only.');
87
  }
88 89
  $xmlrpc_server->message = xmlrpc_message($data);
  if (!xmlrpc_message_parse($xmlrpc_server->message)) {
90
    return xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
91
  }
92
  if ($xmlrpc_server->message->messagetype != 'methodCall') {
93
    return xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
94
  }
95 96 97
  if (!isset($xmlrpc_server->message->params)) {
    $xmlrpc_server->message->params = array();
  }
98
  xmlrpc_server_set($xmlrpc_server);
99
  $result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
100

101
  if (is_object($result) && !empty($result->is_error)) {
102
    return xmlrpc_server_error($result);
103
  }
104 105 106 107 108 109 110
  // Encode the result
  $r = xmlrpc_value($result);
  // Create the XML
  $xml = '
<methodResponse>
  <params>
  <param>
111
    <value>' . xmlrpc_value_get_xml($r) . '</value>
112 113 114
  </param>
  </params>
</methodResponse>
115

116 117
';
  // Send it
118
  return xmlrpc_server_output($xml);
119
}
120

121
/**
122
 * Throws an XML-RPC error.
123 124
 *
 * @param $error
125
 *   An error object or integer error code.
126
 * @param $message
127 128
 *   (optional) The description of the error. Used only if an integer error
 *   code was passed in.
129 130 131
 *
 * @return \Symfony\Component\HttpFoundation\Response
 *   A Response object.
132
 */
133
function xmlrpc_server_error($error, $message = FALSE) {
134 135
  if ($message && !is_object($error)) {
    $error = xmlrpc_error($error, $message);
136
  }
137
  return xmlrpc_server_output(xmlrpc_error_get_xml($error));
138 139
}

140 141 142 143 144
/**
 * Sends XML-RPC output to the browser.
 *
 * @param string $xml
 *   XML to send to the browser.
145 146 147
 *
 * @return \Symfony\Component\HttpFoundation\Response
 *   A Response object.
148
 */
149
function xmlrpc_server_output($xml) {
150
  $xml = '<?xml version="1.0"?>' . "\n" . $xml;
151 152 153 154 155
  $headers = array(
    'Content-Length' => strlen($xml),
    'Content-Type' => 'text/xml'
  );
  return new Response($xml, 200, $headers);
156 157
}

158
/**
159 160 161 162 163
 * 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.
164
 *
165 166 167 168
 * @return
 *   The latest stored request.
 *
 * @see xmlrpc_server_get()
169
 */
170 171 172 173
function xmlrpc_server_set($xmlrpc_server = NULL) {
  static $server;
  if (!isset($server)) {
    $server = $xmlrpc_server;
174
  }
175 176
  return $server;
}
177

178 179 180 181 182 183 184 185
/**
 * Retrieves the latest stored XML-RPC request.
 *
 * @return object
 *   The stored request.
 *
 * @see xmlrpc_server_set()
 */
186 187 188
function xmlrpc_server_get() {
  return xmlrpc_server_set();
}
189

190
/**
191 192 193 194 195 196 197 198 199
 * 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.
200
 *
201 202
 * @return
 *   The results of the call.
203
 */
204
function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
205
  // Make sure parameters are in an array
206 207
  if ($args && !is_array($args)) {
    $args = array($args);
208
  }
209
  // Has this method been mapped to a Drupal function by us or by modules?
210
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
211
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $xmlrpc_server->message->methodname)));
212 213 214
  }
  $method = $xmlrpc_server->callbacks[$methodname];
  $signature = $xmlrpc_server->signatures[$methodname];
215

216
  // If the method has a signature, validate the request against the signature
217
  if (is_array($signature)) {
218
    $ok = TRUE;
219 220
    // Remove first element of $signature which is the unused 'return type'.
    array_shift($signature);
221 222
    // Check the number of arguments
    if (count($args) != count($signature)) {
223
      return xmlrpc_error(-32602, t('Server error. Wrong number of method parameters.'));
224
    }
225 226 227 228 229 230 231
    // 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)) {
232
            $ok = FALSE;
233 234
          }
          break;
235

236 237 238
        case 'base64':
        case 'string':
          if (!is_string($arg)) {
239
            $ok = FALSE;
240 241
          }
          break;
242

243
        case 'boolean':
244 245
          if ($arg !== FALSE && $arg !== TRUE) {
            $ok = FALSE;
246 247
          }
          break;
248

249 250 251
        case 'float':
        case 'double':
          if (!is_float($arg)) {
252
            $ok = FALSE;
253 254
          }
          break;
255

256 257 258
        case 'date':
        case 'dateTime.iso8601':
          if (!$arg->is_date) {
259
            $ok = FALSE;
260 261
          }
          break;
262
      }
263
      if (!$ok) {
264
        return xmlrpc_error(-32602, t('Server error. Invalid method parameters.'));
265 266 267
      }
    }
  }
268

269
  if (!function_exists($method)) {
270
    return xmlrpc_error(-32601, t('Server error. Requested function @method does not exist.', array("@method" => $method)));
271
  }
272
  // Call the mapped function
273 274
  return call_user_func_array($method, $args);
}
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289
/**
 * 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()
 */
290 291 292 293 294
function xmlrpc_server_multicall($methodcalls) {
  // See http://www.xmlrpc.com/discuss/msgReader$1208
  $return = array();
  $xmlrpc_server = xmlrpc_server_get();
  foreach ($methodcalls as $call) {
295
    $ok = TRUE;
296 297
    if (!isset($call['methodName']) || !isset($call['params'])) {
      $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
298
      $ok = FALSE;
299 300 301 302 303 304 305 306 307
    }
    $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);
    }
308
    if (is_object($result) && !empty($result->is_error)) {
309 310
      $return[] = array(
        'faultCode' => $result->code,
311
        'faultString' => $result->message,
312 313 314
      );
    }
    else {
315
      $return[] = array($result);
316 317 318 319 320 321
    }
  }
  return $return;
}

/**
322 323
 * Lists the methods available on this XML-RPC server.
 *
324
 * XML-RPC method system.listMethods maps to this function.
325 326 327
 *
 * @return array
 *   Array of the names of methods available on this server.
328 329 330 331 332 333 334
 */
function xmlrpc_server_list_methods() {
  $xmlrpc_server = xmlrpc_server_get();
  return array_keys($xmlrpc_server->callbacks);
}

/**
335 336
 * Returns a list of the capabilities of this server.
 *
337
 * XML-RPC method system.getCapabilities maps to this function.
338
 *
339 340 341
 * @return array
 *   Array of server capabilities.
 *
342
 * @see http://groups.yahoo.com/group/xml-rpc/message/2897
343 344 345 346 347
 */
function xmlrpc_server_get_capabilities() {
  return array(
    'xmlrpc' => array(
      'specUrl' => 'http://www.xmlrpc.com/spec',
348
      'specVersion' => 1,
349 350 351
    ),
    'faults_interop' => array(
      'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
352
      'specVersion' => 20010516,
353 354 355
    ),
    'system.multicall' => array(
      'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
356
      'specVersion' => 1,
357 358
    ),
    'introspection' => array(
359 360 361
      'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
      'specVersion' => 1,
    ),
362 363 364 365
  );
}

/**
366
 * Returns one method signature for a function.
367
 *
368
 * This is the function mapped to the XML-RPC method system.methodSignature.
369
 *
370 371 372 373 374 375 376 377
 * 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
378 379
 *   An array of arrays of types, each of the arrays representing one method
 *   signature of the function that $methodname maps to.
380 381 382
 */
function xmlrpc_server_method_signature($methodname) {
  $xmlrpc_server = xmlrpc_server_get();
383
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
384
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $methodname)));
385
  }
386
  if (!is_array($xmlrpc_server->signatures[$methodname])) {
387
    return xmlrpc_error(-32601, t('Server error. Requested method @methodname signature not specified.', array("@methodname" => $methodname)));
388
  }
389
  // We array of types
390 391
  $return = array();
  foreach ($xmlrpc_server->signatures[$methodname] as $type) {
392
    $return[] = $type;
393
  }
394
  return array($return);
395 396
}

397
/**
398 399
 * Returns the help for an XML-RPC method.
 *
400 401
 * XML-RPC method system.methodHelp maps to this function.
 *
402
 * @param string $method
403
 *   Name of method for which we return a help string.
404 405 406
 *
 * @return string
 *   Help text for $method.
407
 */
408 409
function xmlrpc_server_method_help($method) {
  $xmlrpc_server = xmlrpc_server_get();
410
  return $xmlrpc_server->help[$method];
411
}