00001 <?php
00002
00003
00010 function xmlrpc_server($callbacks) {
00011 $xmlrpc_server = new stdClass();
00012
00013 $defaults = array(
00014 'system.multicall' => 'xmlrpc_server_multicall',
00015 array(
00016 'system.methodSignature',
00017 'xmlrpc_server_method_signature',
00018 array('array', 'string'),
00019 'Returns an array describing the return type and required parameters of a method.'
00020 ),
00021 array(
00022 'system.getCapabilities',
00023 'xmlrpc_server_get_capabilities',
00024 array('struct'),
00025 'Returns a struct describing the XML-RPC specifications supported by this server.'
00026 ),
00027 array(
00028 'system.listMethods',
00029 'xmlrpc_server_list_methods',
00030 array('array'),
00031 'Returns an array of available methods on this server.'),
00032 array(
00033 'system.methodHelp',
00034 'xmlrpc_server_method_help',
00035 array('string', 'string'),
00036 'Returns a documentation string for the specified method.')
00037 );
00038
00039
00040
00041 foreach (array_merge($defaults, (array)$callbacks) as $key => $callback) {
00042
00043 if (is_int($key)) {
00044 $method = $callback[0];
00045 $xmlrpc_server->callbacks[$method] = $callback[1];
00046 $xmlrpc_server->signatures[$method] = $callback[2];
00047 $xmlrpc_server->help[$method] = $callback[3];
00048 }
00049 else {
00050 $xmlrpc_server->callbacks[$key] = $callback;
00051 $xmlrpc_server->signatures[$key] = '';
00052 $xmlrpc_server->help[$key] = '';
00053 }
00054 }
00055
00056 $data = file_get_contents('php://input');
00057 if (!$data) {
00058 die('XML-RPC server accepts POST requests only.');
00059 }
00060 $xmlrpc_server->message = xmlrpc_message($data);
00061 if (!xmlrpc_message_parse($xmlrpc_server->message)) {
00062 xmlrpc_server_error(-32700, t('Parse error. Request not well formed.'));
00063 }
00064 if ($xmlrpc_server->message->messagetype != 'methodCall') {
00065 xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request must be a methodCall.'));
00066 }
00067 xmlrpc_server_set($xmlrpc_server);
00068 $result = xmlrpc_server_call($xmlrpc_server, $xmlrpc_server->message->methodname, $xmlrpc_server->message->params);
00069
00070 if ($result->is_error) {
00071 xmlrpc_server_error($result);
00072 }
00073
00074 $r = xmlrpc_value($result);
00075
00076 $xml = '
00077 <methodResponse>
00078 <params>
00079 <param>
00080 <value>' .
00081 xmlrpc_value_get_xml($r)
00082 . '</value>
00083 </param>
00084 </params>
00085 </methodResponse>
00086
00087 ';
00088
00089 xmlrpc_server_output($xml);
00090 }
00091
00100 function xmlrpc_server_error($error, $message = FALSE) {
00101 if ($message && !is_object($error)) {
00102 $error = xmlrpc_error($error, $message);
00103 }
00104 xmlrpc_server_output(xmlrpc_error_get_xml($error));
00105 }
00106
00107 function xmlrpc_server_output($xml) {
00108 $xml = '<?xml version="1.0"?>' . "\n" . $xml;
00109 header('Connection: close');
00110 header('Content-Length: ' . strlen($xml));
00111 header('Content-Type: text/xml');
00112 header('Date: ' . date('r'));
00113 echo $xml;
00114 exit;
00115 }
00116
00123 function xmlrpc_server_set($xmlrpc_server = NULL) {
00124 static $server;
00125 if (!isset($server)) {
00126 $server = $xmlrpc_server;
00127 }
00128 return $server;
00129 }
00130
00131
00132 function xmlrpc_server_get() {
00133 return xmlrpc_server_set();
00134 }
00135
00145 function xmlrpc_server_call($xmlrpc_server, $methodname, $args) {
00146
00147 if ($args && !is_array($args)) {
00148 $args = array($args);
00149 }
00150
00151 if (!isset($xmlrpc_server->callbacks[$methodname])) {
00152 return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $xmlrpc_server->message->methodname)));
00153 }
00154 $method = $xmlrpc_server->callbacks[$methodname];
00155 $signature = $xmlrpc_server->signatures[$methodname];
00156
00157
00158 if (is_array($signature)) {
00159 $ok = TRUE;
00160 $return_type = array_shift($signature);
00161
00162 if (count($args) != count($signature)) {
00163 return xmlrpc_error(-32602, t('Server error. Wrong number of method parameters.'));
00164 }
00165
00166 foreach ($signature as $key => $type) {
00167 $arg = $args[$key];
00168 switch ($type) {
00169 case 'int':
00170 case 'i4':
00171 if (is_array($arg) || !is_int($arg)) {
00172 $ok = FALSE;
00173 }
00174 break;
00175 case 'base64':
00176 case 'string':
00177 if (!is_string($arg)) {
00178 $ok = FALSE;
00179 }
00180 break;
00181 case 'boolean':
00182 if ($arg !== FALSE && $arg !== TRUE) {
00183 $ok = FALSE;
00184 }
00185 break;
00186 case 'float':
00187 case 'double':
00188 if (!is_float($arg)) {
00189 $ok = FALSE;
00190 }
00191 break;
00192 case 'date':
00193 case 'dateTime.iso8601':
00194 if (!$arg->is_date) {
00195 $ok = FALSE;
00196 }
00197 break;
00198 }
00199 if (!$ok) {
00200 return xmlrpc_error(-32602, t('Server error. Invalid method parameters.'));
00201 }
00202 }
00203 }
00204
00205 if (!drupal_function_exists($method)) {
00206 return xmlrpc_error(-32601, t('Server error. Requested function @method does not exist.', array("@method" => $method)));
00207 }
00208
00209 return call_user_func_array($method, $args);
00210 }
00211
00212 function xmlrpc_server_multicall($methodcalls) {
00213
00214 $return = array();
00215 $xmlrpc_server = xmlrpc_server_get();
00216 foreach ($methodcalls as $call) {
00217 $ok = TRUE;
00218 if (!isset($call['methodName']) || !isset($call['params'])) {
00219 $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.'));
00220 $ok = FALSE;
00221 }
00222 $method = $call['methodName'];
00223 $params = $call['params'];
00224 if ($method == 'system.multicall') {
00225 $result = xmlrpc_error(-32600, t('Recursive calls to system.multicall are forbidden.'));
00226 }
00227 elseif ($ok) {
00228 $result = xmlrpc_server_call($xmlrpc_server, $method, $params);
00229 }
00230 if ($result->is_error) {
00231 $return[] = array(
00232 'faultCode' => $result->code,
00233 'faultString' => $result->message
00234 );
00235 }
00236 else {
00237 $return[] = $result;
00238 }
00239 }
00240 return $return;
00241 }
00242
00243
00247 function xmlrpc_server_list_methods() {
00248 $xmlrpc_server = xmlrpc_server_get();
00249 return array_keys($xmlrpc_server->callbacks);
00250 }
00251
00256 function xmlrpc_server_get_capabilities() {
00257 return array(
00258 'xmlrpc' => array(
00259 'specUrl' => 'http://www.xmlrpc.com/spec',
00260 'specVersion' => 1
00261 ),
00262 'faults_interop' => array(
00263 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
00264 'specVersion' => 20010516
00265 ),
00266 'system.multicall' => array(
00267 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
00268 'specVersion' => 1
00269 ),
00270 'introspection' => array(
00271 'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html',
00272 'specVersion' => 1
00273 )
00274 );
00275 }
00276
00288 function xmlrpc_server_method_signature($methodname) {
00289 $xmlrpc_server = xmlrpc_server_get();
00290 if (!isset($xmlrpc_server->callbacks[$methodname])) {
00291 return xmlrpc_error(-32601, t('Server error. Requested method @methodname not specified.', array("@methodname" => $methodname)));
00292 }
00293 if (!is_array($xmlrpc_server->signatures[$methodname])) {
00294 return xmlrpc_error(-32601, t('Server error. Requested method @methodname signature not specified.', array("@methodname" => $methodname)));
00295 }
00296
00297 $return = array();
00298 foreach ($xmlrpc_server->signatures[$methodname] as $type) {
00299 $return[] = $type;
00300 }
00301 return $return;
00302 }
00303
00310 function xmlrpc_server_method_help($method) {
00311 $xmlrpc_server = xmlrpc_server_get();
00312 return $xmlrpc_server->help[$method];
00313 }