Code coverage for /20081101/includes/xmlrpcs.inc

Line #Times calledCode
1
<?php
2
// $Id: xmlrpcs.inc,v 1.27 2008/05/06 12:18:45 dries Exp $
3
4
/**
5
 * The main entry point for XML-RPC requests.
6
 *
7
 * @param $callbacks
8
 *   Array of external XML-RPC method names with the callbacks they map to.
9
 */
1020
function xmlrpc_server($callbacks) {
1120
  $xmlrpc_server = new stdClass();
12
  // Define built-in XML-RPC method names
13
  $defaults = array(
1420
      'system.multicall' => 'xmlrpc_server_multicall',
15
    array(
1620
      'system.methodSignature',
1720
      'xmlrpc_server_method_signature',
1820
      array('array', 'string'),
19
      'Returns an array describing the return type and required parameters
of a method.'
2020
    ),
21
    array(
2220
      'system.getCapabilities',
2320
      'xmlrpc_server_get_capabilities',
2420
      array('struct'),
25
      'Returns a struct describing the XML-RPC specifications supported by
this server.'
2620
    ),
27
    array(
2820
      'system.listMethods',
2920
      'xmlrpc_server_list_methods',
3020
      array('array'),
3120
      'Returns an array of available methods on this server.'),
32
    array(
3320
      'system.methodHelp',
3420
      'xmlrpc_server_method_help',
3520
      array('string', 'string'),
3620
      'Returns a documentation string for the specified method.')
3720
  );
38
  // We build an array of all method names by combining the built-ins
39
  // with those defined by modules implementing the _xmlrpc hook.
40
  // Built-in methods are overridable.
4120
  foreach (array_merge($defaults, (array)$callbacks) as $key => $callback)
{
42
    // we could check for is_array($callback)
4320
    if (is_int($key)) {
4420
      $method = $callback[0];
4520
      $xmlrpc_server->callbacks[$method] = $callback[1];
4620
      $xmlrpc_server->signatures[$method] = $callback[2];
4720
      $xmlrpc_server->help[$method] = $callback[3];
4820
    }
49
    else {
5020
      $xmlrpc_server->callbacks[$key] = $callback;
5120
      $xmlrpc_server->signatures[$key] = '';
5220
      $xmlrpc_server->help[$key] = '';
53
    }
5420
  }
55
5620
  $data = file_get_contents('php://input');
5720
  if (!$data) {
580
    die('XML-RPC server accepts POST requests only.');
590
  }
6020
  $xmlrpc_server->message = xmlrpc_message($data);
6120
  if (!xmlrpc_message_parse($xmlrpc_server->message)) {
620
    xmlrpc_server_error(-32700, t('Parse error. Request not well
formed.'));
630
  }
6420
  if ($xmlrpc_server->message->messagetype != 'methodCall') {
650
    xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request
must be a methodCall.'));
660
  }
6720
  xmlrpc_server_set($xmlrpc_server);
6820
  $result = xmlrpc_server_call($xmlrpc_server,
$xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
69
7020
  if ($result->is_error) {
710
    xmlrpc_server_error($result);
720
  }
73
  // Encode the result
7420
  $r = xmlrpc_value($result);
75
  // Create the XML
76
  $xml = '
77
<methodResponse>
78
  <params>
79
  <param>
80
    <value>' .
8120
    xmlrpc_value_get_xml($r)
8220
    . '</value>
83
  </param>
84
  </params>
85
</methodResponse>
86
8720
';
88
  // Send it
8920
  xmlrpc_server_output($xml);
900
}
91
92
/**
93
 * Throw an XML-RPC error.
94
 *
95
 * @param $error
96
 *   an error object OR integer error code
97
 * @param $message
98
 *   description of error, used only if integer error code was passed
99
 */
10020
function xmlrpc_server_error($error, $message = FALSE) {
1010
  if ($message && !is_object($error)) {
1020
    $error = xmlrpc_error($error, $message);
1030
  }
1040
  xmlrpc_server_output(xmlrpc_error_get_xml($error));
1050
}
106
10720
function xmlrpc_server_output($xml) {
10820
  $xml = '<?xml version="1.0"?>' . "\n" . $xml;
10920
  header('Connection: close');
11020
  header('Content-Length: ' . strlen($xml));
11120
  header('Content-Type: text/xml');
11220
  header('Date: ' . date('r'));
11320
  echo $xml;
11420
  exit;
1150
}
116
117
/**
118
 * Store a copy of the request temporarily.
119
 *
120
 * @param $xmlrpc_server
121
 *   Request object created by xmlrpc_server().
122
 */
12320
function xmlrpc_server_set($xmlrpc_server = NULL) {
12420
  static $server;
12520
  if (!isset($server)) {
12620
    $server = $xmlrpc_server;
12720
  }
12820
  return $server;
1290
}
130
131
// Retrieve the stored request.
13220
function xmlrpc_server_get() {
1331
  return xmlrpc_server_set();
1340
}
135
136
/**
137
 * Dispatch the request and any parameters to the appropriate handler.
138
 *
139
 * @param $xmlrpc_server
140
 * @param $methodname
141
 *   The external XML-RPC method name, e.g. 'system.methodHelp'
142
 * @param $args
143
 *   Array containing any parameters that were sent along with the request.
144
 */
14520
function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
146
  // Make sure parameters are in an array
14720
  if ($args && !is_array($args)) {
1480
    $args = array($args);
1490
  }
150
  // Has this method been mapped to a Drupal function by us or by modules?
15120
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
1520
    return xmlrpc_error(-32601, t('Server error. Requested method
@methodname not specified.', array("@methodname" =>
$xmlrpc_server->message->methodname)));
1530
  }
15420
  $method = $xmlrpc_server->callbacks[$methodname];
15520
  $signature = $xmlrpc_server->signatures[$methodname];
156
157
  // If the method has a signature, validate the request against the
signature
15820
  if (is_array($signature)) {
1598
    $ok = TRUE;
1608
    $return_type = array_shift($signature);
161
    // Check the number of arguments
1628
    if (count($args) != count($signature)) {
1630
      return xmlrpc_error(-32602, t('Server error. Wrong number of method
parameters.'));
1640
    }
165
    // Check the argument types
1668
    foreach ($signature as $key => $type) {
1678
      $arg = $args[$key];
168
      switch ($type) {
1698
        case 'int':
1708
        case 'i4':
1711
          if (is_array($arg) || !is_int($arg)) {
1720
            $ok = FALSE;
1730
          }
1741
          break;
1758
        case 'base64':
1768
        case 'string':
1778
          if (!is_string($arg)) {
1780
            $ok = FALSE;
1790
          }
1808
          break;
1815
        case 'boolean':
1823
          if ($arg !== FALSE && $arg !== TRUE) {
1830
            $ok = FALSE;
1840
          }
1853
          break;
1862
        case 'float':
1872
        case 'double':
1880
          if (!is_float($arg)) {
1890
            $ok = FALSE;
1900
          }
1910
          break;
1922
        case 'date':
1932
        case 'dateTime.iso8601':
1940
          if (!$arg->is_date) {
1950
            $ok = FALSE;
1960
          }
1970
          break;
1980
      }
1998
      if (!$ok) {
2000
        return xmlrpc_error(-32602, t('Server error. Invalid method
parameters.'));
2010
      }
2028
    }
2038
  }
204
20520
  if (!drupal_function_exists($method)) {
2060
    return xmlrpc_error(-32601, t('Server error. Requested function @method
does not exist.', array("@method" => $method)));
2070
  }
208
  // Call the mapped function
20920
  return call_user_func_array($method, $args);
2100
}
211
21220
function xmlrpc_server_multicall($methodcalls) {
213
  // See http://www.xmlrpc.com/discuss/msgReader$1208
2141
  $return = array();
2151
  $xmlrpc_server = xmlrpc_server_get();
2161
  foreach ($methodcalls as $call) {
2171
    $ok = TRUE;
2181
    if (!isset($call['methodName']) || !isset($call['params'])) {
2190
      $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
2200
      $ok = FALSE;
2210
    }
2221
    $method = $call['methodName'];
2231
    $params = $call['params'];
2241
    if ($method == 'system.multicall') {
2250
      $result = xmlrpc_error(-32600, t('Recursive calls to system.multicall
are forbidden.'));
2260
    }
2271
    elseif ($ok) {
2281
      $result = xmlrpc_server_call($xmlrpc_server, $method, $params);
2291
    }
2301
    if ($result->is_error) {
2310
      $return[] = array(
2320
        'faultCode' => $result->code,
2330
        'faultString' => $result->message
2340
      );
2350
    }
236
    else {
2371
      $return[] = $result;
238
    }
2391
  }
2401
  return $return;
2410
}
242
243
244
/**
245
 * XML-RPC method system.listMethods maps to this function.
246
 */
24720
function xmlrpc_server_list_methods() {
2480
  $xmlrpc_server = xmlrpc_server_get();
2490
  return array_keys($xmlrpc_server->callbacks);
2500
}
251
252
/**
253
 * XML-RPC method system.getCapabilities maps to this function.
254
 * See http://groups.yahoo.com/group/xml-rpc/message/2897
255
 */
25620
function xmlrpc_server_get_capabilities() {
257
  return array(
258
    'xmlrpc' => array(
2590
      'specUrl' => 'http://www.xmlrpc.com/spec',
260
      'specVersion' => 1
2610
    ),
262
    'faults_interop' => array(
2630
      'specUrl' =>
'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
264
      'specVersion' => 20010516
2650
    ),
266
    'system.multicall' => array(
2670
      'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
268
      'specVersion' => 1
2690
    ),
270
    'introspection' => array(
2710
    'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
272
    'specVersion' => 1
2730
    )
2740
  );
2750
}
276
277
/**
278
 * XML-RPC method system.methodSignature maps to this function.
279
 *
280
 * @param $methodname
281
 *   Name of method for which we return a method signature.
282
 * @return array
283
 *   An array of types representing the method signature of the
284
 *   function that the methodname maps to. The methodSignature of
285
 *   this function is 'array', 'string' because it takes an array
286
 *   and returns a string.
287
 */
28820
function xmlrpc_server_method_signature($methodname) {
2890
  $xmlrpc_server = xmlrpc_server_get();
2900
  if (!isset($xmlrpc_server->callbacks[$methodname])) {
2910
    return xmlrpc_error(-32601, t('Server error. Requested method
@methodname not specified.', array("@methodname" => $methodname)));
2920
  }
2930
  if (!is_array($xmlrpc_server->signatures[$methodname])) {
2940
    return xmlrpc_error(-32601, t('Server error. Requested method
@methodname signature not specified.', array("@methodname" =>
$methodname)));
2950
  }
296
  // We array of types
2970
  $return = array();
2980
  foreach ($xmlrpc_server->signatures[$methodname] as $type) {
2990
    $return[] = $type;
3000
  }
3010
  return $return;
3020
}
303
304
/**
305
 * XML-RPC method system.methodHelp maps to this function.
306
 *
307
 * @param $method
308
 *   Name of method for which we return a help string.
309
 */
31020
function xmlrpc_server_method_help($method) {
3110
  $xmlrpc_server = xmlrpc_server_get();
3120
  return $xmlrpc_server->help[$method];
3130
}
31420