71558f85 |
1 | <?php |
2 | /** |
3 | * An XML-RPC server |
4 | * |
5 | * @author Donal McMullan donal@catalyst.net.nz |
6 | * @version 0.0.1 |
7 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
8 | * @package mnet |
9 | */ |
10 | |
11 | // Make certain that config.php doesn't display any errors, and that it doesn't |
12 | // override our do-not-display-errors setting: |
13 | ini_set('display_errors',0); |
14 | require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); |
15 | ini_set('display_errors',0); |
16 | |
17 | // Include MNET stuff: |
18 | require_once $CFG->dirroot.'/mnet/lib.php'; |
19 | require_once $CFG->dirroot.'/mnet/remote_client.php'; |
20 | |
21 | // Content type for output is not html: |
22 | header('Content-type: text/xml'); |
23 | |
24 | if (!empty($CFG->mnet_rpcdebug)) { |
25 | trigger_error("HTTP_RAW_POST_DATA"); |
26 | trigger_error($HTTP_RAW_POST_DATA); |
27 | } |
28 | |
29 | // New global variable which ONLY gets set in this server page, so you know that |
30 | // if you've been called by a remote Moodle, this should be set: |
31 | $MNET_REMOTE_CLIENT = new mnet_remote_client(); |
32 | |
33 | // Peek at the message to see if it's an XML-ENC document. If it is, note that |
34 | // the client connection was encrypted, and strip the xml-encryption and |
35 | // xml-signature wrappers from the XML-RPC payload |
36 | if (strpos(substr($HTTP_RAW_POST_DATA, 0, 100), '<encryptedMessage>')) { |
37 | $MNET_REMOTE_CLIENT->was_encrypted(); |
38 | // Extract the XML-RPC payload from the XML-ENC and XML-SIG wrappers. |
39 | $payload = mnet_server_strip_wrappers($HTTP_RAW_POST_DATA); |
40 | } else { |
41 | $params = xmlrpc_decode_request($HTTP_RAW_POST_DATA, $method); |
42 | if ($method == 'system.keyswap' || |
43 | $method == 'system/keyswap') { |
44 | |
45 | // OK |
46 | |
47 | } elseif ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false) { |
48 | exit(mnet_server_fault(7021, 'forbidden-transport')); |
49 | } |
50 | // Looks like plaintext is ok. It is assumed that a plaintext call: |
51 | // 1. Came from a trusted host on your local network |
52 | // 2. Is *not* from a Moodle - otherwise why skip encryption/signing? |
53 | // 3. Is free to execute ANY function in Moodle |
54 | // 4. Cannot execute any methods (as it can't instantiate a class first) |
55 | // To execute a method, you'll need to create a wrapper function that first |
56 | // instantiates the class, and then calls the method. |
57 | $payload = $HTTP_RAW_POST_DATA; |
58 | } |
59 | |
60 | if (!empty($CFG->mnet_rpcdebug)) { |
61 | trigger_error("XMLRPC Payload"); |
62 | trigger_error(print_r($payload,1)); |
63 | } |
64 | |
65 | // Parse and action the XML-RPC payload |
66 | $response = mnet_server_dispatch($payload); |
67 | |
68 | /** |
69 | * Strip the encryption (XML-ENC) and signature (XML-SIG) wrappers and return the XML-RPC payload |
70 | * |
71 | * IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP: |
72 | * The payload will have been encrypted with a symmetric key. This key will |
73 | * itself have been encrypted using your public key. The key is decrypted using |
74 | * your private key, and then used to decrypt the XML payload. |
75 | * |
76 | * IF COMMUNICATION TAKES PLACE OVER UNENCRYPTED HTTP *OR* ENCRYPTED HTTPS: |
77 | * In either case, there will be an XML wrapper which contains your XML-RPC doc |
78 | * as an object element, a signature for that doc, and various standards- |
79 | * compliant info to aid in verifying the signature. |
80 | * |
81 | * This function parses the encryption wrapper, decrypts the contents, parses |
82 | * the signature wrapper, and if the signature matches the payload, it returns |
83 | * the payload, which should be an XML-RPC request. |
84 | * If there is an error, or the signatures don't match, it echoes an XML-RPC |
85 | * error and exits. |
86 | * |
87 | * See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing} |
88 | * and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing} |
89 | * guidelines for more detail on the XML. |
90 | * |
91 | * -----XML-Envelope--------------------------------- |
92 | * | | |
93 | * | Encrypted-Symmetric-key---------------- | |
94 | * | |_____________________________________| | |
95 | * | | |
96 | * | Encrypted data------------------------- | |
97 | * | | | | |
98 | * | | -XML-Envelope------------------ | | |
99 | * | | | | | | |
100 | * | | | --Signature------------- | | | |
101 | * | | | |______________________| | | | |
102 | * | | | | | | |
103 | * | | | --Signed-Payload-------- | | | |
104 | * | | | | | | | | |
105 | * | | | | XML-RPC Request | | | | |
106 | * | | | |______________________| | | | |
107 | * | | | | | | |
108 | * | | |_____________________________| | | |
109 | * | |_____________________________________| | |
110 | * | | |
111 | * |________________________________________________| |
112 | * |
113 | * @uses $db |
114 | * @param string $HTTP_RAW_POST_DATA The XML that the client sent |
115 | * @return string The XMLRPC payload. |
116 | */ |
117 | function mnet_server_strip_wrappers($HTTP_RAW_POST_DATA) { |
118 | global $MNET, $MNET_REMOTE_CLIENT; |
119 | if (isset($_SERVER)) { |
120 | |
121 | $crypt_parser = new mnet_encxml_parser(); |
122 | $crypt_parser->parse($HTTP_RAW_POST_DATA); |
123 | |
124 | if ($crypt_parser->payload_encrypted) { |
125 | |
735c7beb |
126 | $key = array_pop($crypt_parser->cipher); // This key is Symmetric |
71558f85 |
127 | $data = array_pop($crypt_parser->cipher); |
128 | |
129 | $crypt_parser->free_resource(); |
130 | |
131 | // Initialize payload var |
132 | $payload = ''; |
133 | |
134 | // &$payload |
135 | $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $MNET->get_private_key()); |
136 | |
735c7beb |
137 | if (!$isOpen) { |
138 | // Decryption failed... let's try our archived keys |
139 | $result = get_config('mnet', 'openssl_history'); |
140 | if(empty($result)) { |
141 | set_config('openssl_history', serialize(array()), 'mnet'); |
142 | $result = get_config('mnet', 'openssl_history'); |
143 | } |
144 | $openssl_history = unserialize($result->value); |
145 | foreach($openssl_history as $keyset) { |
146 | $keyresource = openssl_pkey_get_private($keyset['keypair_PEM']); |
147 | $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $keyresource); |
148 | if ($isOpen) { |
149 | // It's an older code, sir, but it checks out |
150 | exit(mnet_server_fault(7025, $MNET->public_key)); |
151 | } |
152 | } |
153 | } |
154 | |
71558f85 |
155 | if (!$isOpen) { |
156 | exit(mnet_server_fault(7023, 'encryption-invalid')); |
157 | } |
158 | |
159 | if (strpos(substr($payload, 0, 100), '<signedMessage>')) { |
160 | $MNET_REMOTE_CLIENT->was_signed(); |
161 | $sig_parser = new mnet_encxml_parser(); |
162 | $sig_parser->parse($payload); |
163 | } else { |
164 | exit(mnet_server_fault(7022, 'verifysignature-error')); |
165 | } |
166 | |
167 | } else { |
168 | exit(mnet_server_fault(7024, 'payload-not-encrypted')); |
169 | } |
170 | |
171 | unset($payload); |
172 | |
173 | $host_record_exists = $MNET_REMOTE_CLIENT->set_wwwroot($sig_parser->remote_wwwroot); |
174 | |
175 | if (false == $host_record_exists) { |
176 | exit(mnet_server_fault(7020, 'wrong-wwwroot', $sig_parser->remote_wwwroot)); |
177 | } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != $MNET_REMOTE_CLIENT->ip_address) { |
178 | exit(mnet_server_fault(7017, 'wrong-ip')); |
179 | } |
180 | |
181 | /** |
182 | * Get the certificate (i.e. public key) from the remote server. |
183 | */ |
184 | $certificate = $MNET_REMOTE_CLIENT->public_key; |
185 | |
186 | if ($certificate == false) { |
187 | exit(mnet_server_fault(709, 'nosuchpublickey')); |
188 | } |
189 | |
190 | $payload = base64_decode($sig_parser->data_object); |
191 | |
192 | // Does the signature match the data and the public cert? |
193 | $signature_verified = openssl_verify($payload, base64_decode($sig_parser->signature), $certificate); |
194 | if ($signature_verified == 1) { |
195 | // Parse the XML |
196 | } elseif ($signature_verified == 0) { |
197 | exit(mnet_server_fault(710, 'verifysignature-invalid')); |
198 | } else { |
199 | exit(mnet_server_fault(711, 'verifysignature-error')); |
200 | } |
201 | |
202 | $sig_parser->free_resource(); |
203 | |
204 | return $payload; |
205 | } else { |
206 | exit(mnet_server_fault(712, "phperror")); |
207 | } |
208 | } |
209 | |
210 | /** |
211 | * Return the proper XML-RPC content to report an error. |
212 | * |
213 | * @param int $code The ID code of the error message |
214 | * @return string $text The text of the error message |
215 | */ |
216 | function mnet_server_fault($code, $text, $param = null) { |
217 | if (!is_numeric($code)) { |
218 | $code = 0; |
219 | } |
220 | $code = intval($code); |
221 | |
222 | $text = get_string($text, 'mnet', $param); |
223 | |
224 | // Replace illegal XML chars - is this already in a lib somewhere? |
225 | $text = str_replace(array('<','>','&','"',"'"), array('<','>','&','"','''), $text); |
226 | |
227 | $return = mnet_server_prepare_response('<?xml version="1.0"?> |
228 | <methodResponse> |
229 | <fault> |
230 | <value> |
231 | <struct> |
232 | <member> |
233 | <name>faultCode</name> |
234 | <value><int>'.$code.'</int></value> |
235 | </member> |
236 | <member> |
237 | <name>faultString</name> |
238 | <value><string>'.$text.'</string></value> |
239 | </member> |
240 | </struct> |
241 | </value> |
242 | </fault> |
243 | </methodResponse>'); |
244 | return $return; |
245 | } |
246 | |
247 | /** |
248 | * Dummy function for the XML-RPC dispatcher - use to call a method on an object |
249 | * or to call a function |
250 | * |
251 | * Translate XML-RPC's strange function call syntax into a more straightforward |
252 | * PHP-friendly alternative. This dummy function will be called by the |
253 | * dispatcher, and can be used to call a method on an object, or just a function |
254 | * |
255 | * The methodName argument (eg. mnet/testlib/mnet_concatenate_strings) |
256 | * is ignored. |
257 | * |
258 | * @param string $methodname We discard this - see 'functionname' |
259 | * @param array $argsarray Each element is an argument to the real |
260 | * function |
261 | * @param string $functionname The name of the PHP function you want to call |
262 | * @return mixed The return value will be that of the real |
263 | * function, whateber it may be. |
264 | */ |
265 | function mnet_server_dummy_method($methodname, $argsarray, $functionname) { |
266 | global $MNET_REMOTE_CLIENT; |
267 | |
5c2e5bf2 |
268 | if (!is_object($MNET_REMOTE_CLIENT->object_to_call)) { |
71558f85 |
269 | return @call_user_func_array($functionname, $argsarray); |
270 | } else { |
271 | return @call_user_method_array($functionname, $MNET_REMOTE_CLIENT->object_to_call, $argsarray); |
272 | } |
273 | } |
274 | |
275 | /** |
276 | * Package a response in any required envelope, and return it to the client |
277 | * |
278 | * @param string $response The XMLRPC response string |
279 | * @return string The encoded response string |
280 | */ |
281 | function mnet_server_prepare_response($response) { |
282 | global $MNET_REMOTE_CLIENT; |
283 | |
284 | if ($MNET_REMOTE_CLIENT->request_was_signed) { |
285 | $response = mnet_sign_message($response); |
286 | } |
287 | |
288 | if ($MNET_REMOTE_CLIENT->request_was_encrypted) { |
289 | $response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key); |
290 | } |
291 | |
292 | return $response; |
293 | } |
294 | |
295 | /** |
296 | * If security checks are passed, dispatch the request to the function/method |
297 | * |
298 | * The config variable 'mnet_dispatcher_mode' can be: |
299 | * strict: Only execute functions that are in specific files |
300 | * off: The default - don't execute anything |
301 | * |
302 | * @param string $payload The XML-RPC request |
303 | * @return No return val - just echo the response |
304 | */ |
305 | function mnet_server_dispatch($payload) { |
306 | global $CFG, $MNET_REMOTE_CLIENT; |
307 | // xmlrpc_decode_request returns an array of parameters, and the $method |
308 | // variable (which is passed by reference) is instantiated with the value from |
309 | // the methodName tag in the xml payload |
310 | // xmlrpc_decode_request($xml, &$method) |
311 | $params = xmlrpc_decode_request($payload, $method); |
312 | |
313 | // $method is something like: "mod/forum/lib/forum_add_instance" |
314 | // $params is an array of parameters. A parameter might itself be an array. |
315 | |
316 | // Whitelist characters that are permitted in a method name |
317 | // The method name must not begin with a / - avoid absolute paths |
318 | // A dot character . is only allowed in the filename, i.e. something.php |
319 | if (0 == preg_match("@^[A-Za-z0-9]+/[A-Za-z0-9/_-]+(\.php/)?[A-Za-z0-9_-]+$@",$method)) { |
320 | exit(mnet_server_fault(713, 'nosuchfunction')); |
321 | } |
322 | |
323 | $callstack = explode('/', $method); |
324 | // callstack will look like array('mod', 'forum', 'lib', 'forum_add_instance'); |
325 | |
326 | /** |
327 | * What has the site administrator chosen as his dispatcher setting? |
328 | * strict: Only execute functions that are in specific files |
329 | * off: The default - don't execute anything |
330 | */ |
331 | ////////////////////////////////////// OFF |
332 | if (!isset($CFG->mnet_dispatcher_mode) ) { |
333 | set_config('mnet_dispatcher_mode', 'off'); |
334 | exit(mnet_server_fault(704, 'nosuchservice')); |
335 | } elseif ('off' == $CFG->mnet_dispatcher_mode) { |
336 | exit(mnet_server_fault(704, 'nosuchservice')); |
337 | |
338 | ////////////////////////////////////// SYSTEM METHODS |
339 | } elseif ($callstack[0] == 'system') { |
340 | $functionname = $callstack[1]; |
341 | $xmlrpcserver = xmlrpc_server_create(); |
342 | |
343 | // I'm adding the canonical xmlrpc references here, however we've |
344 | // already forbidden that the period (.) should be allowed in the call |
345 | // stack, so if someone tries to access our XMLRPC in the normal way, |
346 | // they'll already have received a RPC server fault message. |
347 | |
348 | // Maybe we should allow an easement so that regular XMLRPC clients can |
349 | // call our system methods, and find out what we have to offer? |
350 | |
351 | xmlrpc_server_register_method($xmlrpcserver, 'system.listMethods', 'mnet_system'); |
352 | xmlrpc_server_register_method($xmlrpcserver, 'system/listMethods', 'mnet_system'); |
353 | |
354 | xmlrpc_server_register_method($xmlrpcserver, 'system.methodSignature', 'mnet_system'); |
355 | xmlrpc_server_register_method($xmlrpcserver, 'system/methodSignature', 'mnet_system'); |
356 | |
357 | xmlrpc_server_register_method($xmlrpcserver, 'system.methodHelp', 'mnet_system'); |
358 | xmlrpc_server_register_method($xmlrpcserver, 'system/methodHelp', 'mnet_system'); |
359 | |
360 | xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system'); |
361 | xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system'); |
362 | |
363 | xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap'); |
364 | xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap'); |
365 | |
366 | if ($method == 'system.listMethods' || |
367 | $method == 'system/listMethods' || |
368 | $method == 'system.methodSignature' || |
369 | $method == 'system/methodSignature' || |
370 | $method == 'system.methodHelp' || |
371 | $method == 'system/methodHelp' || |
372 | $method == 'system.listServices' || |
373 | $method == 'system/listServices' || |
374 | $method == 'system.keyswap' || |
375 | $method == 'system/keyswap') { |
376 | |
377 | $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT); |
378 | $response = mnet_server_prepare_response($response); |
379 | } else { |
380 | exit(mnet_server_fault(7018, 'nosuchfunction')); |
381 | } |
382 | |
383 | xmlrpc_server_destroy($xmlrpcserver); |
384 | echo $response; |
385 | ////////////////////////////////////// STRICT AUTH |
386 | } elseif ($callstack[0] == 'auth') { |
387 | |
388 | // Break out the callstack into its elements |
389 | list($base, $plugin, $filename, $methodname) = $callstack; |
390 | |
391 | // We refuse to include anything that is not auth.php |
392 | if ($filename == 'auth.php' && is_enabled_auth($plugin)) { |
393 | $authclass = 'auth_plugin_'.$plugin; |
394 | $includefile = '/auth/'.$plugin.'/auth.php'; |
395 | $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $authclass); |
396 | $response = mnet_server_prepare_response($response); |
397 | echo $response; |
398 | } else { |
399 | // Generate error response - unable to locate function |
400 | exit(mnet_server_fault(702, 'nosuchfunction')); |
401 | } |
402 | |
403 | ////////////////////////////////////// STRICT ENROL |
404 | } elseif ($callstack[0] == 'enrol') { |
405 | |
406 | // Break out the callstack into its elements |
407 | list($base, $plugin, $filename, $methodname) = $callstack; |
408 | |
409 | if ($filename == 'enrol.php' && is_enabled_enrol($plugin)) { |
410 | $enrolclass = 'enrolment_plugin_'.$plugin; |
411 | $includefile = '/enrol/'.$plugin.'/enrol.php'; |
412 | $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $enrolclass); |
413 | $response = mnet_server_prepare_response($response); |
414 | echo $response; |
415 | } else { |
416 | // Generate error response - unable to locate function |
417 | exit(mnet_server_fault(703, 'nosuchfunction')); |
418 | } |
419 | |
420 | ////////////////////////////////////// STRICT MOD/* |
421 | } elseif ($callstack[0] == 'mod' || 'promiscuous' == $CFG->mnet_dispatcher_mode) { |
422 | list($base, $module, $filename, $functionname) = $callstack; |
423 | |
424 | ////////////////////////////////////// STRICT MOD/* |
425 | if ($base == 'mod' && $filename == 'rpclib.php') { |
426 | $includefile = '/mod/'.$module.'/rpclib.php'; |
427 | $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload); |
428 | $response = mnet_server_prepare_response($response); |
429 | echo $response; |
430 | |
431 | ////////////////////////////////////// PROMISCUOUS |
432 | } elseif ('promiscuous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) { |
433 | |
434 | $functionname = array_pop($callstack); |
435 | $filename = array_pop($callstack); |
436 | |
437 | if ($MNET_REMOTE_CLIENT->plaintext_is_ok()) { |
438 | |
439 | // The call stack holds the path to any include file |
440 | $includefile = $CFG->dirroot.'/'.implode('/',$callstack).'/'.$filename.'.php'; |
441 | |
442 | $response = mnet_server_invoke_function($includefile, $functionname, $method, $payload); |
443 | echo $response; |
444 | } |
445 | |
446 | } else { |
447 | // Generate error response - unable to locate function |
448 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
449 | } |
450 | |
451 | } else { |
452 | // Generate error response - unable to locate function |
453 | exit(mnet_server_fault(7012, 'nosuchfunction')); |
454 | } |
455 | } |
456 | |
457 | /** |
458 | * Execute the system functions - mostly for introspection |
459 | * |
460 | * @param string $method XMLRPC method name, e.g. system.listMethods |
461 | * @param array $params Array of parameters from the XMLRPC request |
462 | * @param string $hostinfo Hostinfo object from the mnet_host table |
463 | * @return mixed Response data - any kind of PHP variable |
464 | */ |
465 | function mnet_system($method, $params, $hostinfo) { |
466 | global $CFG; |
467 | |
468 | if (empty($hostinfo)) return array(); |
469 | |
470 | $id_list = $hostinfo->id; |
471 | if (!empty($CFG->mnet_all_hosts_id)) { |
472 | $id_list .= ', '.$CFG->mnet_all_hosts_id; |
473 | } |
474 | |
475 | if ('system.listMethods' == $method || 'system/listMethods' == $method) { |
476 | if (count($params) == 0) { |
477 | $query = ' |
478 | SELECT DISTINCT |
479 | rpc.function_name, |
480 | rpc.xmlrpc_path, |
481 | rpc.enabled, |
482 | rpc.help, |
483 | rpc.profile |
484 | FROM |
485 | '.$CFG->prefix.'mnet_host2service h2s, |
486 | '.$CFG->prefix.'mnet_service2rpc s2r, |
487 | '.$CFG->prefix.'mnet_rpc rpc |
488 | WHERE |
489 | s2r.rpcid = rpc.id AND |
490 | h2s.serviceid = s2r.serviceid AND |
491 | h2s.hostid in ('.$id_list .') |
492 | ORDER BY |
493 | rpc.xmlrpc_path ASC'; |
494 | |
495 | } else { |
496 | $query = ' |
497 | SELECT DISTINCT |
498 | rpc.function_name, |
499 | rpc.xmlrpc_path, |
500 | rpc.enabled, |
501 | rpc.help, |
502 | rpc.profile |
503 | FROM |
504 | '.$CFG->prefix.'mnet_host2service h2s, |
505 | '.$CFG->prefix.'mnet_service2rpc s2r, |
506 | '.$CFG->prefix.'mnet_service svc, |
507 | '.$CFG->prefix.'mnet_rpc rpc |
508 | WHERE |
509 | s2r.rpcid = rpc.id AND |
510 | h2s.serviceid = s2r.serviceid AND |
511 | h2s.hostid in ('.$id_list .') AND |
512 | svc.id = h2s.serviceid AND |
513 | svc.name = \''.$params[0].'\' |
514 | ORDER BY |
515 | rpc.xmlrpc_path ASC'; |
516 | |
517 | } |
518 | $resultset = array_values(get_records_sql($query)); |
519 | $methods = array(); |
520 | foreach($resultset as $result) { |
521 | $methods[] = $result->xmlrpc_path; |
522 | } |
523 | return $methods; |
524 | } elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) { |
525 | $query = ' |
526 | SELECT DISTINCT |
527 | rpc.function_name, |
528 | rpc.xmlrpc_path, |
529 | rpc.enabled, |
530 | rpc.help, |
531 | rpc.profile |
532 | FROM |
533 | '.$CFG->prefix.'mnet_host2service h2s, |
534 | '.$CFG->prefix.'mnet_service2rpc s2r, |
535 | '.$CFG->prefix.'mnet_rpc rpc |
536 | WHERE |
537 | rpc.xmlrpc_path = \''.$params[0].'\' AND |
538 | s2r.rpcid = rpc.id AND |
539 | h2s.serviceid = s2r.serviceid AND |
540 | h2s.hostid in ('.$id_list .')'; |
541 | |
542 | $result = get_records_sql($query); |
543 | $methodsigs = array(); |
544 | |
545 | if (is_array($result)) { |
546 | foreach($result as $method) { |
547 | $methodsigs[] = unserialize($method->profile); |
548 | } |
549 | } |
550 | |
551 | return $methodsigs; |
552 | } elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) { |
553 | $query = ' |
554 | SELECT DISTINCT |
555 | rpc.function_name, |
556 | rpc.xmlrpc_path, |
557 | rpc.enabled, |
558 | rpc.help, |
559 | rpc.profile |
560 | FROM |
561 | '.$CFG->prefix.'mnet_host2service h2s, |
562 | '.$CFG->prefix.'mnet_service2rpc s2r, |
563 | '.$CFG->prefix.'mnet_rpc rpc |
564 | WHERE |
565 | rpc.xmlrpc_path = \''.$params[0].'\' AND |
566 | s2r.rpcid = rpc.id AND |
567 | h2s.serviceid = s2r.serviceid AND |
568 | h2s.hostid in ('.$id_list .')'; |
569 | |
570 | $result = get_record_sql($query); |
571 | |
572 | if (is_object($result)) { |
573 | return $result->help; |
574 | } |
575 | } elseif ('system.listServices' == $method || 'system/listServices' == $method) { |
576 | $query = ' |
577 | SELECT DISTINCT |
578 | s.id, |
579 | s.name, |
580 | s.apiversion, |
581 | h2s.publish, |
582 | h2s.subscribe |
583 | FROM |
584 | '.$CFG->prefix.'mnet_host2service h2s, |
585 | '.$CFG->prefix.'mnet_service s |
586 | WHERE |
587 | h2s.serviceid = s.id AND |
588 | h2s.hostid in ('.$id_list .') |
589 | ORDER BY |
590 | s.name ASC'; |
591 | |
592 | $result = get_records_sql($query); |
593 | $services = array(); |
594 | |
595 | if (is_array($result)) { |
596 | foreach($result as $service) { |
597 | $services[] = array('name' => $service->name, |
598 | 'apiversion' => $service->apiversion, |
599 | 'publish' => $service->publish, |
600 | 'subscribe' => $service->subscribe); |
601 | } |
602 | } |
603 | |
604 | return $services; |
605 | } |
606 | exit(mnet_server_fault(7019, 'nosuchfunction')); |
607 | } |
608 | |
609 | /** |
610 | * Initialize the object (if necessary), execute the method or function, and |
611 | * return the response |
612 | * |
613 | * @param string $includefile The file that contains the object definition |
614 | * @param string $methodname The name of the method to execute |
615 | * @param string $method The full path to the method |
616 | * @param string $payload The XML-RPC request payload |
617 | * @param string $class The name of the class to instantiate (or false) |
618 | * @return string The XML-RPC response |
619 | */ |
620 | function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) { |
621 | |
622 | $permission = mnet_permit_rpc_call($includefile, $methodname, $class); |
623 | |
624 | if (RPC_NOSUCHFILE == $permission) { |
625 | // Generate error response - unable to locate function |
626 | exit(mnet_server_fault(705, 'nosuchfile', $includefile)); |
627 | } |
628 | |
629 | if (RPC_NOSUCHFUNCTION == $permission) { |
630 | // Generate error response - unable to locate function |
631 | exit(mnet_server_fault(706, 'nosuchfunction')); |
632 | } |
633 | |
634 | if (RPC_FORBIDDENFUNCTION == $permission) { |
635 | // Generate error response - unable to locate function |
636 | exit(mnet_server_fault(707, 'forbidden-function')); |
637 | } |
638 | |
639 | if (RPC_NOSUCHCLASS == $permission) { |
640 | // Generate error response - unable to locate function |
641 | exit(mnet_server_fault(7013, 'nosuchfunction')); |
642 | } |
643 | |
644 | if (RPC_NOSUCHMETHOD == $permission) { |
645 | // Generate error response - unable to locate function |
646 | exit(mnet_server_fault(7014, 'nosuchmethod')); |
647 | } |
648 | |
649 | if (RPC_NOSUCHFUNCTION == $permission) { |
650 | // Generate error response - unable to locate function |
651 | exit(mnet_server_fault(7014, 'nosuchmethod')); |
652 | } |
653 | |
654 | if (RPC_FORBIDDENMETHOD == $permission) { |
655 | // Generate error response - unable to locate function |
656 | exit(mnet_server_fault(7015, 'nosuchfunction')); |
657 | } |
658 | |
659 | if (0 < $permission) { |
660 | // Generate error response - unable to locate function |
661 | exit(mnet_server_fault(7019, 'unknownerror')); |
662 | } |
663 | |
664 | if (RPC_OK == $permission) { |
665 | $xmlrpcserver = xmlrpc_server_create(); |
666 | $bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method'); |
667 | $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname); |
668 | $bool = xmlrpc_server_destroy($xmlrpcserver); |
669 | return $response; |
670 | } |
671 | } |
672 | |
673 | function mnet_keyswap($function, $params) { |
735c7beb |
674 | global $CFG, $MNET; |
71558f85 |
675 | $return = array(); |
676 | |
677 | if (!empty($CFG->mnet_register_allhosts)) { |
678 | $mnet_peer = new mnet_peer(); |
679 | $keyok = $mnet_peer->bootstrap($params[0]); |
680 | if ($keyok) { |
681 | $mnet_peer->commit(); |
682 | } |
683 | } |
735c7beb |
684 | return $MNET->public_key; |
71558f85 |
685 | } |
686 | ?> |