You are here

function xmlrpc_server in xmlrpc 8

Invokes XML-RPC methods on this server.

Parameters

array $callbacks: Either an associative array of external XML-RPC method names as keys with the callbacks they map to as values, or a more complex structure describing XML-RPC callbacks as returned from hook_xmlrpc().

Return value

\Symfony\Component\HttpFoundation\Response A Response object.

1 call to xmlrpc_server()
XmlrpcController::php in src/Controller/XmlrpcController.php
Process an XML-RPC request.

File

./xmlrpc.server.inc, line 22
Page callback file for the xmlrpc module.

Code

function xmlrpc_server(array $callbacks) {
  $xmlrpc_server = new stdClass();

  // Define built-in XML-RPC method names.
  $defaults = [
    'system.multicall' => 'xmlrpc_server_multicall',
    [
      'system.methodSignature',
      'xmlrpc_server_method_signature',
      [
        'array',
        'string',
      ],
      'Returns an array describing the return type and required parameters of a method.',
    ],
    [
      'system.getCapabilities',
      'xmlrpc_server_get_capabilities',
      [
        'struct',
      ],
      'Returns a struct describing the XML-RPC specifications supported by this server.',
    ],
    [
      'system.listMethods',
      'xmlrpc_server_list_methods',
      [
        'array',
      ],
      'Returns an array of available methods on this server.',
    ],
    [
      'system.methodHelp',
      'xmlrpc_server_method_help',
      [
        'string',
        'string',
      ],
      'Returns a documentation string for the specified method.',
    ],
  ];

  // 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.
  $callbacks = array_merge($defaults, (array) $callbacks);
  \Drupal::moduleHandler()
    ->alter('xmlrpc', $callbacks);
  foreach ($callbacks as $key => $callback) {

    // 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] = '';
    }
  }
  $data = file_get_contents('php://input');
  if (!$data) {
    throw new BadRequestHttpException('XML-RPC server accepts POST requests only.');
  }
  $xmlrpc_server->message = xmlrpc_message($data);
  if (!xmlrpc_message_parse($xmlrpc_server->message)) {
    return xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
  }
  if ($xmlrpc_server->message->messagetype != 'methodCall') {
    return xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
  }
  if (!isset($xmlrpc_server->message->params)) {
    $xmlrpc_server->message->params = [];
  }
  xmlrpc_server_set($xmlrpc_server);
  $result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
  if (is_object($result) && !empty($result->is_error)) {
    return xmlrpc_server_error($result);
  }

  // Encode the result.
  $r = xmlrpc_value($result);

  // Create the XML.
  $xml = '
<methodResponse>
  <params>
  <param>
    <value>' . xmlrpc_value_get_xml($r) . '</value>
  </param>
  </params>
</methodResponse>

';

  // Send it.
  return xmlrpc_server_output($xml);
}