| 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 | | */ |
| 10 | 20 | function xmlrpc_server($callbacks) { |
| 11 | 20 | $xmlrpc_server = new stdClass(); |
| 12 | | // Define built-in XML-RPC method names |
| 13 | | $defaults = array( |
| 14 | 20 | 'system.multicall' => 'xmlrpc_server_multicall', |
| 15 | | array( |
| 16 | 20 | 'system.methodSignature', |
| 17 | 20 | 'xmlrpc_server_method_signature', |
| 18 | 20 | array('array', 'string'), |
| 19 | | 'Returns an array describing the return type and required parameters
of a method.' |
| 20 | 20 | ), |
| 21 | | array( |
| 22 | 20 | 'system.getCapabilities', |
| 23 | 20 | 'xmlrpc_server_get_capabilities', |
| 24 | 20 | array('struct'), |
| 25 | | 'Returns a struct describing the XML-RPC specifications supported by
this server.' |
| 26 | 20 | ), |
| 27 | | array( |
| 28 | 20 | 'system.listMethods', |
| 29 | 20 | 'xmlrpc_server_list_methods', |
| 30 | 20 | array('array'), |
| 31 | 20 | 'Returns an array of available methods on this server.'), |
| 32 | | array( |
| 33 | 20 | 'system.methodHelp', |
| 34 | 20 | 'xmlrpc_server_method_help', |
| 35 | 20 | array('string', 'string'), |
| 36 | 20 | 'Returns a documentation string for the specified method.') |
| 37 | 20 | ); |
| 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. |
| 41 | 20 | foreach (array_merge($defaults, (array)$callbacks) as $key => $callback)
{ |
| 42 | | // we could check for is_array($callback) |
| 43 | 20 | if (is_int($key)) { |
| 44 | 20 | $method = $callback[0]; |
| 45 | 20 | $xmlrpc_server->callbacks[$method] = $callback[1]; |
| 46 | 20 | $xmlrpc_server->signatures[$method] = $callback[2]; |
| 47 | 20 | $xmlrpc_server->help[$method] = $callback[3]; |
| 48 | 20 | } |
| 49 | | else { |
| 50 | 20 | $xmlrpc_server->callbacks[$key] = $callback; |
| 51 | 20 | $xmlrpc_server->signatures[$key] = ''; |
| 52 | 20 | $xmlrpc_server->help[$key] = ''; |
| 53 | | } |
| 54 | 20 | } |
| 55 | | |
| 56 | 20 | $data = file_get_contents('php://input'); |
| 57 | 20 | if (!$data) { |
| 58 | 0 | die('XML-RPC server accepts POST requests only.'); |
| 59 | 0 | } |
| 60 | 20 | $xmlrpc_server->message = xmlrpc_message($data); |
| 61 | 20 | if (!xmlrpc_message_parse($xmlrpc_server->message)) { |
| 62 | 0 | xmlrpc_server_error(-32700, t('Parse error. Request not well
formed.')); |
| 63 | 0 | } |
| 64 | 20 | if ($xmlrpc_server->message->messagetype != 'methodCall') { |
| 65 | 0 | xmlrpc_server_error(-32600, t('Server error. Invalid XML-RPC. Request
must be a methodCall.')); |
| 66 | 0 | } |
| 67 | 20 | xmlrpc_server_set($xmlrpc_server); |
| 68 | 20 | $result = xmlrpc_server_call($xmlrpc_server,
$xmlrpc_server->message->methodname, $xmlrpc_server->message->params); |
| 69 | | |
| 70 | 20 | if ($result->is_error) { |
| 71 | 0 | xmlrpc_server_error($result); |
| 72 | 0 | } |
| 73 | | // Encode the result |
| 74 | 20 | $r = xmlrpc_value($result); |
| 75 | | // Create the XML |
| 76 | | $xml = ' |
| 77 | | <methodResponse> |
| 78 | | <params> |
| 79 | | <param> |
| 80 | | <value>' . |
| 81 | 20 | xmlrpc_value_get_xml($r) |
| 82 | 20 | . '</value> |
| 83 | | </param> |
| 84 | | </params> |
| 85 | | </methodResponse> |
| 86 | | |
| 87 | 20 | '; |
| 88 | | // Send it |
| 89 | 20 | xmlrpc_server_output($xml); |
| 90 | 0 | } |
| 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 | | */ |
| 100 | 20 | function xmlrpc_server_error($error, $message = FALSE) { |
| 101 | 0 | if ($message && !is_object($error)) { |
| 102 | 0 | $error = xmlrpc_error($error, $message); |
| 103 | 0 | } |
| 104 | 0 | xmlrpc_server_output(xmlrpc_error_get_xml($error)); |
| 105 | 0 | } |
| 106 | | |
| 107 | 20 | function xmlrpc_server_output($xml) { |
| 108 | 20 | $xml = '<?xml version="1.0"?>' . "\n" . $xml; |
| 109 | 20 | header('Connection: close'); |
| 110 | 20 | header('Content-Length: ' . strlen($xml)); |
| 111 | 20 | header('Content-Type: text/xml'); |
| 112 | 20 | header('Date: ' . date('r')); |
| 113 | 20 | echo $xml; |
| 114 | 20 | exit; |
| 115 | 0 | } |
| 116 | | |
| 117 | | /** |
| 118 | | * Store a copy of the request temporarily. |
| 119 | | * |
| 120 | | * @param $xmlrpc_server |
| 121 | | * Request object created by xmlrpc_server(). |
| 122 | | */ |
| 123 | 20 | function xmlrpc_server_set($xmlrpc_server = NULL) { |
| 124 | 20 | static $server; |
| 125 | 20 | if (!isset($server)) { |
| 126 | 20 | $server = $xmlrpc_server; |
| 127 | 20 | } |
| 128 | 20 | return $server; |
| 129 | 0 | } |
| 130 | | |
| 131 | | // Retrieve the stored request. |
| 132 | 20 | function xmlrpc_server_get() { |
| 133 | 1 | return xmlrpc_server_set(); |
| 134 | 0 | } |
| 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 | | */ |
| 145 | 20 | function xmlrpc_server_call($xmlrpc_server, $methodname, $args) { |
| 146 | | // Make sure parameters are in an array |
| 147 | 20 | if ($args && !is_array($args)) { |
| 148 | 0 | $args = array($args); |
| 149 | 0 | } |
| 150 | | // Has this method been mapped to a Drupal function by us or by modules? |
| 151 | 20 | if (!isset($xmlrpc_server->callbacks[$methodname])) { |
| 152 | 0 | return xmlrpc_error(-32601, t('Server error. Requested method
@methodname not specified.', array("@methodname" =>
$xmlrpc_server->message->methodname))); |
| 153 | 0 | } |
| 154 | 20 | $method = $xmlrpc_server->callbacks[$methodname]; |
| 155 | 20 | $signature = $xmlrpc_server->signatures[$methodname]; |
| 156 | | |
| 157 | | // If the method has a signature, validate the request against the
signature |
| 158 | 20 | if (is_array($signature)) { |
| 159 | 8 | $ok = TRUE; |
| 160 | 8 | $return_type = array_shift($signature); |
| 161 | | // Check the number of arguments |
| 162 | 8 | if (count($args) != count($signature)) { |
| 163 | 0 | return xmlrpc_error(-32602, t('Server error. Wrong number of method
parameters.')); |
| 164 | 0 | } |
| 165 | | // Check the argument types |
| 166 | 8 | foreach ($signature as $key => $type) { |
| 167 | 8 | $arg = $args[$key]; |
| 168 | | switch ($type) { |
| 169 | 8 | case 'int': |
| 170 | 8 | case 'i4': |
| 171 | 1 | if (is_array($arg) || !is_int($arg)) { |
| 172 | 0 | $ok = FALSE; |
| 173 | 0 | } |
| 174 | 1 | break; |
| 175 | 8 | case 'base64': |
| 176 | 8 | case 'string': |
| 177 | 8 | if (!is_string($arg)) { |
| 178 | 0 | $ok = FALSE; |
| 179 | 0 | } |
| 180 | 8 | break; |
| 181 | 5 | case 'boolean': |
| 182 | 3 | if ($arg !== FALSE && $arg !== TRUE) { |
| 183 | 0 | $ok = FALSE; |
| 184 | 0 | } |
| 185 | 3 | break; |
| 186 | 2 | case 'float': |
| 187 | 2 | case 'double': |
| 188 | 0 | if (!is_float($arg)) { |
| 189 | 0 | $ok = FALSE; |
| 190 | 0 | } |
| 191 | 0 | break; |
| 192 | 2 | case 'date': |
| 193 | 2 | case 'dateTime.iso8601': |
| 194 | 0 | if (!$arg->is_date) { |
| 195 | 0 | $ok = FALSE; |
| 196 | 0 | } |
| 197 | 0 | break; |
| 198 | 0 | } |
| 199 | 8 | if (!$ok) { |
| 200 | 0 | return xmlrpc_error(-32602, t('Server error. Invalid method
parameters.')); |
| 201 | 0 | } |
| 202 | 8 | } |
| 203 | 8 | } |
| 204 | | |
| 205 | 20 | if (!drupal_function_exists($method)) { |
| 206 | 0 | return xmlrpc_error(-32601, t('Server error. Requested function @method
does not exist.', array("@method" => $method))); |
| 207 | 0 | } |
| 208 | | // Call the mapped function |
| 209 | 20 | return call_user_func_array($method, $args); |
| 210 | 0 | } |
| 211 | | |
| 212 | 20 | function xmlrpc_server_multicall($methodcalls) { |
| 213 | | // See http://www.xmlrpc.com/discuss/msgReader$1208 |
| 214 | 1 | $return = array(); |
| 215 | 1 | $xmlrpc_server = xmlrpc_server_get(); |
| 216 | 1 | foreach ($methodcalls as $call) { |
| 217 | 1 | $ok = TRUE; |
| 218 | 1 | if (!isset($call['methodName']) || !isset($call['params'])) { |
| 219 | 0 | $result = xmlrpc_error(3, t('Invalid syntax for system.multicall.')); |
| 220 | 0 | $ok = FALSE; |
| 221 | 0 | } |
| 222 | 1 | $method = $call['methodName']; |
| 223 | 1 | $params = $call['params']; |
| 224 | 1 | if ($method == 'system.multicall') { |
| 225 | 0 | $result = xmlrpc_error(-32600, t('Recursive calls to system.multicall
are forbidden.')); |
| 226 | 0 | } |
| 227 | 1 | elseif ($ok) { |
| 228 | 1 | $result = xmlrpc_server_call($xmlrpc_server, $method, $params); |
| 229 | 1 | } |
| 230 | 1 | if ($result->is_error) { |
| 231 | 0 | $return[] = array( |
| 232 | 0 | 'faultCode' => $result->code, |
| 233 | 0 | 'faultString' => $result->message |
| 234 | 0 | ); |
| 235 | 0 | } |
| 236 | | else { |
| 237 | 1 | $return[] = $result; |
| 238 | | } |
| 239 | 1 | } |
| 240 | 1 | return $return; |
| 241 | 0 | } |
| 242 | | |
| 243 | | |
| 244 | | /** |
| 245 | | * XML-RPC method system.listMethods maps to this function. |
| 246 | | */ |
| 247 | 20 | function xmlrpc_server_list_methods() { |
| 248 | 0 | $xmlrpc_server = xmlrpc_server_get(); |
| 249 | 0 | return array_keys($xmlrpc_server->callbacks); |
| 250 | 0 | } |
| 251 | | |
| 252 | | /** |
| 253 | | * XML-RPC method system.getCapabilities maps to this function. |
| 254 | | * See http://groups.yahoo.com/group/xml-rpc/message/2897 |
| 255 | | */ |
| 256 | 20 | function xmlrpc_server_get_capabilities() { |
| 257 | | return array( |
| 258 | | 'xmlrpc' => array( |
| 259 | 0 | 'specUrl' => 'http://www.xmlrpc.com/spec', |
| 260 | | 'specVersion' => 1 |
| 261 | 0 | ), |
| 262 | | 'faults_interop' => array( |
| 263 | 0 | 'specUrl' =>
'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php', |
| 264 | | 'specVersion' => 20010516 |
| 265 | 0 | ), |
| 266 | | 'system.multicall' => array( |
| 267 | 0 | 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', |
| 268 | | 'specVersion' => 1 |
| 269 | 0 | ), |
| 270 | | 'introspection' => array( |
| 271 | 0 | 'specUrl' => 'http://scripts.incutio.com/xmlrpc/introspection.html', |
| 272 | | 'specVersion' => 1 |
| 273 | 0 | ) |
| 274 | 0 | ); |
| 275 | 0 | } |
| 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 | | */ |
| 288 | 20 | function xmlrpc_server_method_signature($methodname) { |
| 289 | 0 | $xmlrpc_server = xmlrpc_server_get(); |
| 290 | 0 | if (!isset($xmlrpc_server->callbacks[$methodname])) { |
| 291 | 0 | return xmlrpc_error(-32601, t('Server error. Requested method
@methodname not specified.', array("@methodname" => $methodname))); |
| 292 | 0 | } |
| 293 | 0 | if (!is_array($xmlrpc_server->signatures[$methodname])) { |
| 294 | 0 | return xmlrpc_error(-32601, t('Server error. Requested method
@methodname signature not specified.', array("@methodname" =>
$methodname))); |
| 295 | 0 | } |
| 296 | | // We array of types |
| 297 | 0 | $return = array(); |
| 298 | 0 | foreach ($xmlrpc_server->signatures[$methodname] as $type) { |
| 299 | 0 | $return[] = $type; |
| 300 | 0 | } |
| 301 | 0 | return $return; |
| 302 | 0 | } |
| 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 | | */ |
| 310 | 20 | function xmlrpc_server_method_help($method) { |
| 311 | 0 | $xmlrpc_server = xmlrpc_server_get(); |
| 312 | 0 | return $xmlrpc_server->help[$method]; |
| 313 | 0 | } |
| 314 | 20 | |