2001-09-09 16:47:10 +00:00
|
|
|
<?php
|
2005-08-11 12:57:41 +00:00
|
|
|
// $Id$
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2009-12-31 13:43:36 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Provides API for defining and handling XML-RPC requests.
|
|
|
|
*/
|
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
/**
|
|
|
|
* The main entry point for XML-RPC requests.
|
|
|
|
*
|
|
|
|
* @param $callbacks
|
|
|
|
* Array of external XML-RPC method names with the callbacks they map to.
|
|
|
|
*/
|
2005-07-13 18:46:15 +00:00
|
|
|
function xmlrpc_server($callbacks) {
|
|
|
|
$xmlrpc_server = new stdClass();
|
2006-06-08 21:15:02 +00:00
|
|
|
// Define built-in XML-RPC method names
|
2005-07-13 18:46:15 +00:00
|
|
|
$defaults = array(
|
2010-07-22 16:20:15 +00:00
|
|
|
'system.multicall' => 'xmlrpc_server_multicall',
|
2005-07-13 18:46:15 +00:00
|
|
|
array(
|
|
|
|
'system.methodSignature',
|
|
|
|
'xmlrpc_server_method_signature',
|
|
|
|
array('array', 'string'),
|
2010-07-22 16:20:15 +00:00
|
|
|
'Returns an array describing the return type and required parameters of a method.',
|
2005-07-13 18:46:15 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'system.getCapabilities',
|
|
|
|
'xmlrpc_server_get_capabilities',
|
|
|
|
array('struct'),
|
2010-07-22 16:20:15 +00:00
|
|
|
'Returns a struct describing the XML-RPC specifications supported by this server.',
|
2005-07-13 18:46:15 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'system.listMethods',
|
|
|
|
'xmlrpc_server_list_methods',
|
|
|
|
array('array'),
|
2010-07-22 16:20:15 +00:00
|
|
|
'Returns an array of available methods on this server.',
|
|
|
|
),
|
2005-07-13 18:46:15 +00:00
|
|
|
array(
|
|
|
|
'system.methodHelp',
|
|
|
|
'xmlrpc_server_method_help',
|
|
|
|
array('string', 'string'),
|
2010-07-22 16:20:15 +00:00
|
|
|
'Returns a documentation string for the specified method.',
|
|
|
|
),
|
2005-07-13 18:46:15 +00:00
|
|
|
);
|
2006-06-08 21:15:02 +00:00
|
|
|
// 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.
|
2009-12-31 13:43:36 +00:00
|
|
|
foreach (array_merge($defaults, (array) $callbacks) as $key => $callback) {
|
2005-07-13 18:46:15 +00:00
|
|
|
// 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] = '';
|
|
|
|
}
|
|
|
|
}
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
$data = file_get_contents('php://input');
|
2005-08-14 22:27:40 +00:00
|
|
|
if (!$data) {
|
2010-04-29 05:35:21 +00:00
|
|
|
print 'XML-RPC server accepts POST requests only.';
|
|
|
|
drupal_exit();
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
$xmlrpc_server->message = xmlrpc_message($data);
|
|
|
|
if (!xmlrpc_message_parse($xmlrpc_server->message)) {
|
2006-06-08 21:15:02 +00:00
|
|
|
xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
if ($xmlrpc_server->message->messagetype != 'methodCall') {
|
2006-06-08 21:15:02 +00:00
|
|
|
xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2009-12-02 00:31:47 +00:00
|
|
|
if (!isset($xmlrpc_server->message->params)) {
|
|
|
|
$xmlrpc_server->message->params = array();
|
|
|
|
}
|
2005-08-14 09:53:40 +00:00
|
|
|
xmlrpc_server_set($xmlrpc_server);
|
2005-07-13 18:46:15 +00:00
|
|
|
$result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
|
2006-06-08 21:15:02 +00:00
|
|
|
|
2010-03-31 15:33:08 +00:00
|
|
|
if (is_object($result) && !empty($result->is_error)) {
|
2005-07-13 18:46:15 +00:00
|
|
|
xmlrpc_server_error($result);
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
// Encode the result
|
|
|
|
$r = xmlrpc_value($result);
|
|
|
|
// Create the XML
|
|
|
|
$xml = '
|
|
|
|
<methodResponse>
|
|
|
|
<params>
|
|
|
|
<param>
|
2010-07-22 16:20:15 +00:00
|
|
|
<value>' . xmlrpc_value_get_xml($r) . '</value>
|
2005-07-13 18:46:15 +00:00
|
|
|
</param>
|
|
|
|
</params>
|
|
|
|
</methodResponse>
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
';
|
|
|
|
// Send it
|
|
|
|
xmlrpc_server_output($xml);
|
|
|
|
}
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
/**
|
|
|
|
* Throw an XML-RPC error.
|
|
|
|
*
|
|
|
|
* @param $error
|
|
|
|
* an error object OR integer error code
|
|
|
|
* @param $message
|
|
|
|
* description of error, used only if integer error code was passed
|
|
|
|
*/
|
2006-07-05 11:45:51 +00:00
|
|
|
function xmlrpc_server_error($error, $message = FALSE) {
|
2005-07-13 18:46:15 +00:00
|
|
|
if ($message && !is_object($error)) {
|
|
|
|
$error = xmlrpc_error($error, $message);
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
xmlrpc_server_output(xmlrpc_error_get_xml($error));
|
2001-09-09 16:47:10 +00:00
|
|
|
}
|
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
function xmlrpc_server_output($xml) {
|
2008-04-14 17:48:46 +00:00
|
|
|
$xml = '<?xml version="1.0"?>' . "\n" . $xml;
|
2009-09-30 18:36:02 +00:00
|
|
|
drupal_add_http_header('Content-Length', strlen($xml));
|
|
|
|
drupal_add_http_header('Content-Type', 'text/xml');
|
2005-07-13 18:46:15 +00:00
|
|
|
echo $xml;
|
2009-10-13 21:16:44 +00:00
|
|
|
drupal_exit();
|
2001-09-09 16:47:10 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
/**
|
|
|
|
* Store a copy of the request temporarily.
|
|
|
|
*
|
|
|
|
* @param $xmlrpc_server
|
|
|
|
* Request object created by xmlrpc_server().
|
|
|
|
*/
|
2005-07-13 18:46:15 +00:00
|
|
|
function xmlrpc_server_set($xmlrpc_server = NULL) {
|
|
|
|
static $server;
|
|
|
|
if (!isset($server)) {
|
|
|
|
$server = $xmlrpc_server;
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
return $server;
|
|
|
|
}
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
// Retrieve the stored request.
|
2005-07-13 18:46:15 +00:00
|
|
|
function xmlrpc_server_get() {
|
|
|
|
return xmlrpc_server_set();
|
|
|
|
}
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
/**
|
|
|
|
* Dispatch the request and any parameters to the appropriate handler.
|
|
|
|
*
|
|
|
|
* @param $xmlrpc_server
|
2009-12-31 13:43:36 +00:00
|
|
|
* Contains information about this XML-RPC server, the methods it provides,
|
|
|
|
* their signatures, etc.
|
2006-06-08 21:15:02 +00:00
|
|
|
* @param $methodname
|
|
|
|
* The external XML-RPC method name, e.g. 'system.methodHelp'
|
|
|
|
* @param $args
|
|
|
|
* Array containing any parameters that were sent along with the request.
|
|
|
|
*/
|
2005-07-13 18:46:15 +00:00
|
|
|
function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
|
2006-06-08 21:15:02 +00:00
|
|
|
// Make sure parameters are in an array
|
2005-07-13 18:46:15 +00:00
|
|
|
if ($args && !is_array($args)) {
|
|
|
|
$args = array($args);
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2006-06-08 21:15:02 +00:00
|
|
|
// Has this method been mapped to a Drupal function by us or by modules?
|
2005-07-13 18:46:15 +00:00
|
|
|
if (!isset($xmlrpc_server->callbacks[$methodname])) {
|
2008-04-28 10:04:05 +00:00
|
|
|
return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $xmlrpc_server->message->methodname)));
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
$method = $xmlrpc_server->callbacks[$methodname];
|
|
|
|
$signature = $xmlrpc_server->signatures[$methodname];
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
// If the method has a signature, validate the request against the signature
|
2005-07-13 18:46:15 +00:00
|
|
|
if (is_array($signature)) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = TRUE;
|
2005-07-13 18:46:15 +00:00
|
|
|
$return_type = array_shift($signature);
|
|
|
|
// Check the number of arguments
|
|
|
|
if (count($args) != count($signature)) {
|
2006-06-08 21:15:02 +00:00
|
|
|
return xmlrpc_error(-32602, t('Server error. Wrong number of method parameters.'));
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
// 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)) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = FALSE;
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-07-22 16:20:15 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
case 'base64':
|
|
|
|
case 'string':
|
|
|
|
if (!is_string($arg)) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = FALSE;
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-07-22 16:20:15 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
case 'boolean':
|
2006-07-05 11:45:51 +00:00
|
|
|
if ($arg !== FALSE && $arg !== TRUE) {
|
|
|
|
$ok = FALSE;
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-07-22 16:20:15 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
if (!is_float($arg)) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = FALSE;
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-07-22 16:20:15 +00:00
|
|
|
|
2005-07-13 18:46:15 +00:00
|
|
|
case 'date':
|
|
|
|
case 'dateTime.iso8601':
|
|
|
|
if (!$arg->is_date) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = FALSE;
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
if (!$ok) {
|
2006-06-08 21:15:02 +00:00
|
|
|
return xmlrpc_error(-32602, t('Server error. Invalid method parameters.'));
|
2001-09-27 20:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-31 08:54:37 +00:00
|
|
|
|
2009-08-24 00:14:23 +00:00
|
|
|
if (!function_exists($method)) {
|
2008-04-28 10:04:05 +00:00
|
|
|
return xmlrpc_error(-32601, t('Server error. Requested function @method does not exist.', array("@method" => $method)));
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
2006-06-08 21:15:02 +00:00
|
|
|
// Call the mapped function
|
2005-07-13 18:46:15 +00:00
|
|
|
return call_user_func_array($method, $args);
|
|
|
|
}
|
2001-09-09 16:47:10 +00:00
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
function xmlrpc_server_multicall($methodcalls) {
|
|
|
|
// See http://www.xmlrpc.com/discuss/msgReader$1208
|
|
|
|
$return = array();
|
|
|
|
$xmlrpc_server = xmlrpc_server_get();
|
|
|
|
foreach ($methodcalls as $call) {
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = TRUE;
|
2006-06-08 21:15:02 +00:00
|
|
|
if (!isset($call['methodName']) || !isset($call['params'])) {
|
|
|
|
$result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
|
2006-07-05 11:45:51 +00:00
|
|
|
$ok = FALSE;
|
2006-06-08 21:15:02 +00:00
|
|
|
}
|
|
|
|
$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);
|
|
|
|
}
|
2010-03-31 15:33:08 +00:00
|
|
|
if (is_object($result) && !empty($result->is_error)) {
|
2006-06-08 21:15:02 +00:00
|
|
|
$return[] = array(
|
|
|
|
'faultCode' => $result->code,
|
2010-07-22 16:20:15 +00:00
|
|
|
'faultString' => $result->message,
|
2006-06-08 21:15:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2009-12-29 19:33:36 +00:00
|
|
|
$return[] = array($result);
|
2006-06-08 21:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML-RPC method system.listMethods maps to this function.
|
|
|
|
*/
|
|
|
|
function xmlrpc_server_list_methods() {
|
|
|
|
$xmlrpc_server = xmlrpc_server_get();
|
|
|
|
return array_keys($xmlrpc_server->callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML-RPC method system.getCapabilities maps to this function.
|
2009-12-31 13:43:36 +00:00
|
|
|
*
|
|
|
|
* @see http://groups.yahoo.com/group/xml-rpc/message/2897
|
2006-06-08 21:15:02 +00:00
|
|
|
*/
|
|
|
|
function xmlrpc_server_get_capabilities() {
|
|
|
|
return array(
|
|
|
|
'xmlrpc' => array(
|
|
|
|
'specUrl' => 'http://www.xmlrpc.com/spec',
|
2010-07-22 16:20:15 +00:00
|
|
|
'specVersion' => 1,
|
2006-06-08 21:15:02 +00:00
|
|
|
),
|
|
|
|
'faults_interop' => array(
|
|
|
|
'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
|
2010-07-22 16:20:15 +00:00
|
|
|
'specVersion' => 20010516,
|
2006-06-08 21:15:02 +00:00
|
|
|
),
|
|
|
|
'system.multicall' => array(
|
|
|
|
'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
|
2010-07-22 16:20:15 +00:00
|
|
|
'specVersion' => 1,
|
2006-06-08 21:15:02 +00:00
|
|
|
),
|
|
|
|
'introspection' => array(
|
2010-07-22 16:20:15 +00:00
|
|
|
'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
|
|
|
|
'specVersion' => 1,
|
|
|
|
),
|
2006-06-08 21:15:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML-RPC method system.methodSignature maps to this function.
|
|
|
|
*
|
|
|
|
* @param $methodname
|
|
|
|
* Name of method for which we return a method signature.
|
2010-07-22 16:20:15 +00:00
|
|
|
*
|
2009-12-31 13:43:36 +00:00
|
|
|
* @return
|
2006-06-08 21:15:02 +00:00
|
|
|
* An array of types representing the method signature of the
|
|
|
|
* function that the methodname maps to. The methodSignature of
|
|
|
|
* this function is 'array', 'string' because it takes an array
|
|
|
|
* and returns a string.
|
|
|
|
*/
|
|
|
|
function xmlrpc_server_method_signature($methodname) {
|
|
|
|
$xmlrpc_server = xmlrpc_server_get();
|
2005-07-13 18:46:15 +00:00
|
|
|
if (!isset($xmlrpc_server->callbacks[$methodname])) {
|
2008-04-28 10:04:05 +00:00
|
|
|
return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $methodname)));
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
2006-06-08 21:15:02 +00:00
|
|
|
if (!is_array($xmlrpc_server->signatures[$methodname])) {
|
2008-04-28 10:04:05 +00:00
|
|
|
return xmlrpc_error(-32601, t('Server error. Requested method @methodname signature not specified.', array("@methodname" => $methodname)));
|
2005-07-13 18:46:15 +00:00
|
|
|
}
|
2006-06-08 21:15:02 +00:00
|
|
|
// We array of types
|
2005-07-13 18:46:15 +00:00
|
|
|
$return = array();
|
|
|
|
foreach ($xmlrpc_server->signatures[$methodname] as $type) {
|
2006-06-08 21:15:02 +00:00
|
|
|
$return[] = $type;
|
2001-09-09 16:47:10 +00:00
|
|
|
}
|
2005-07-13 18:46:15 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2006-06-08 21:15:02 +00:00
|
|
|
/**
|
|
|
|
* XML-RPC method system.methodHelp maps to this function.
|
|
|
|
*
|
|
|
|
* @param $method
|
|
|
|
* Name of method for which we return a help string.
|
|
|
|
*/
|
2005-11-13 08:18:12 +00:00
|
|
|
function xmlrpc_server_method_help($method) {
|
|
|
|
$xmlrpc_server = xmlrpc_server_get();
|
2005-07-13 18:46:15 +00:00
|
|
|
return $xmlrpc_server->help[$method];
|
2007-09-04 21:10:45 +00:00
|
|
|
}
|
2010-07-22 16:20:15 +00:00
|
|
|
|