Mnet: Stylefix: Array was returned with unusual numeric index
[moodle.git] / mnet / xmlrpc / server.php
CommitLineData
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:
13ini_set('display_errors',0);
14require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
15ini_set('display_errors',0);
16
17// Include MNET stuff:
18require_once $CFG->dirroot.'/mnet/lib.php';
19require_once $CFG->dirroot.'/mnet/remote_client.php';
20
21// Content type for output is not html:
22header('Content-type: text/xml');
23
24if (!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
36if (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
60if (!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 */
117function 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/**
151a9872 211 * Return the proper XML-RPC content to report an error in the local language.
71558f85 212 *
213 * @param int $code The ID code of the error message
151a9872 214 * @param string $text The array-key of the error message in the lang file
215 * @param string $param The $a param for the error message in the lang file
71558f85 216 * @return string $text The text of the error message
217 */
218function mnet_server_fault($code, $text, $param = null) {
151a9872 219 global $MNET_REMOTE_CLIENT;
71558f85 220 if (!is_numeric($code)) {
221 $code = 0;
222 }
223 $code = intval($code);
224
225 $text = get_string($text, 'mnet', $param);
151a9872 226 return mnet_server_fault_xml($code, $text);
227}
71558f85 228
151a9872 229/**
230 * Return the proper XML-RPC content to report an error.
231 *
232 * @param int $code The ID code of the error message
233 * @param string $text The error message
234 * @return string $text The XML text of the error message
235 */
236function mnet_server_fault_xml($code, $text) {
237 global $MNET_REMOTE_CLIENT;
71558f85 238 // Replace illegal XML chars - is this already in a lib somewhere?
239 $text = str_replace(array('<','>','&','"',"'"), array('&lt;','&gt;','&amp;','&quot;','&apos;'), $text);
240
241 $return = mnet_server_prepare_response('<?xml version="1.0"?>
242<methodResponse>
243 <fault>
244 <value>
245 <struct>
246 <member>
247 <name>faultCode</name>
248 <value><int>'.$code.'</int></value>
249 </member>
250 <member>
251 <name>faultString</name>
252 <value><string>'.$text.'</string></value>
253 </member>
254 </struct>
255 </value>
256 </fault>
257 </methodResponse>');
258 return $return;
259}
260
261/**
262 * Dummy function for the XML-RPC dispatcher - use to call a method on an object
263 * or to call a function
264 *
265 * Translate XML-RPC's strange function call syntax into a more straightforward
266 * PHP-friendly alternative. This dummy function will be called by the
267 * dispatcher, and can be used to call a method on an object, or just a function
268 *
269 * The methodName argument (eg. mnet/testlib/mnet_concatenate_strings)
270 * is ignored.
271 *
272 * @param string $methodname We discard this - see 'functionname'
273 * @param array $argsarray Each element is an argument to the real
274 * function
275 * @param string $functionname The name of the PHP function you want to call
276 * @return mixed The return value will be that of the real
277 * function, whateber it may be.
278 */
279function mnet_server_dummy_method($methodname, $argsarray, $functionname) {
280 global $MNET_REMOTE_CLIENT;
281
5c2e5bf2 282 if (!is_object($MNET_REMOTE_CLIENT->object_to_call)) {
71558f85 283 return @call_user_func_array($functionname, $argsarray);
284 } else {
285 return @call_user_method_array($functionname, $MNET_REMOTE_CLIENT->object_to_call, $argsarray);
286 }
287}
288
289/**
290 * Package a response in any required envelope, and return it to the client
291 *
292 * @param string $response The XMLRPC response string
293 * @return string The encoded response string
294 */
295function mnet_server_prepare_response($response) {
296 global $MNET_REMOTE_CLIENT;
297
298 if ($MNET_REMOTE_CLIENT->request_was_signed) {
299 $response = mnet_sign_message($response);
300 }
301
302 if ($MNET_REMOTE_CLIENT->request_was_encrypted) {
303 $response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key);
304 }
305
306 return $response;
307}
308
309/**
310 * If security checks are passed, dispatch the request to the function/method
311 *
312 * The config variable 'mnet_dispatcher_mode' can be:
313 * strict: Only execute functions that are in specific files
314 * off: The default - don't execute anything
315 *
316 * @param string $payload The XML-RPC request
317 * @return No return val - just echo the response
318 */
319function mnet_server_dispatch($payload) {
320 global $CFG, $MNET_REMOTE_CLIENT;
321 // xmlrpc_decode_request returns an array of parameters, and the $method
322 // variable (which is passed by reference) is instantiated with the value from
323 // the methodName tag in the xml payload
324 // xmlrpc_decode_request($xml, &$method)
325 $params = xmlrpc_decode_request($payload, $method);
326
327 // $method is something like: "mod/forum/lib/forum_add_instance"
328 // $params is an array of parameters. A parameter might itself be an array.
329
330 // Whitelist characters that are permitted in a method name
331 // The method name must not begin with a / - avoid absolute paths
332 // A dot character . is only allowed in the filename, i.e. something.php
333 if (0 == preg_match("@^[A-Za-z0-9]+/[A-Za-z0-9/_-]+(\.php/)?[A-Za-z0-9_-]+$@",$method)) {
334 exit(mnet_server_fault(713, 'nosuchfunction'));
335 }
336
337 $callstack = explode('/', $method);
338 // callstack will look like array('mod', 'forum', 'lib', 'forum_add_instance');
339
340 /**
341 * What has the site administrator chosen as his dispatcher setting?
342 * strict: Only execute functions that are in specific files
343 * off: The default - don't execute anything
344 */
345 ////////////////////////////////////// OFF
346 if (!isset($CFG->mnet_dispatcher_mode) ) {
347 set_config('mnet_dispatcher_mode', 'off');
348 exit(mnet_server_fault(704, 'nosuchservice'));
349 } elseif ('off' == $CFG->mnet_dispatcher_mode) {
350 exit(mnet_server_fault(704, 'nosuchservice'));
351
352 ////////////////////////////////////// SYSTEM METHODS
353 } elseif ($callstack[0] == 'system') {
354 $functionname = $callstack[1];
355 $xmlrpcserver = xmlrpc_server_create();
356
357 // I'm adding the canonical xmlrpc references here, however we've
358 // already forbidden that the period (.) should be allowed in the call
359 // stack, so if someone tries to access our XMLRPC in the normal way,
360 // they'll already have received a RPC server fault message.
361
362 // Maybe we should allow an easement so that regular XMLRPC clients can
363 // call our system methods, and find out what we have to offer?
364
365 xmlrpc_server_register_method($xmlrpcserver, 'system.listMethods', 'mnet_system');
366 xmlrpc_server_register_method($xmlrpcserver, 'system/listMethods', 'mnet_system');
367
368 xmlrpc_server_register_method($xmlrpcserver, 'system.methodSignature', 'mnet_system');
369 xmlrpc_server_register_method($xmlrpcserver, 'system/methodSignature', 'mnet_system');
370
371 xmlrpc_server_register_method($xmlrpcserver, 'system.methodHelp', 'mnet_system');
372 xmlrpc_server_register_method($xmlrpcserver, 'system/methodHelp', 'mnet_system');
373
374 xmlrpc_server_register_method($xmlrpcserver, 'system.listServices', 'mnet_system');
375 xmlrpc_server_register_method($xmlrpcserver, 'system/listServices', 'mnet_system');
376
377 xmlrpc_server_register_method($xmlrpcserver, 'system.keyswap', 'mnet_keyswap');
378 xmlrpc_server_register_method($xmlrpcserver, 'system/keyswap', 'mnet_keyswap');
379
380 if ($method == 'system.listMethods' ||
381 $method == 'system/listMethods' ||
382 $method == 'system.methodSignature' ||
383 $method == 'system/methodSignature' ||
384 $method == 'system.methodHelp' ||
385 $method == 'system/methodHelp' ||
386 $method == 'system.listServices' ||
387 $method == 'system/listServices' ||
388 $method == 'system.keyswap' ||
389 $method == 'system/keyswap') {
390
391 $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT);
392 $response = mnet_server_prepare_response($response);
393 } else {
394 exit(mnet_server_fault(7018, 'nosuchfunction'));
395 }
396
397 xmlrpc_server_destroy($xmlrpcserver);
398 echo $response;
399 ////////////////////////////////////// STRICT AUTH
400 } elseif ($callstack[0] == 'auth') {
401
402 // Break out the callstack into its elements
403 list($base, $plugin, $filename, $methodname) = $callstack;
404
405 // We refuse to include anything that is not auth.php
406 if ($filename == 'auth.php' && is_enabled_auth($plugin)) {
407 $authclass = 'auth_plugin_'.$plugin;
408 $includefile = '/auth/'.$plugin.'/auth.php';
409 $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $authclass);
410 $response = mnet_server_prepare_response($response);
411 echo $response;
412 } else {
413 // Generate error response - unable to locate function
414 exit(mnet_server_fault(702, 'nosuchfunction'));
415 }
416
417 ////////////////////////////////////// STRICT ENROL
418 } elseif ($callstack[0] == 'enrol') {
419
420 // Break out the callstack into its elements
421 list($base, $plugin, $filename, $methodname) = $callstack;
422
423 if ($filename == 'enrol.php' && is_enabled_enrol($plugin)) {
424 $enrolclass = 'enrolment_plugin_'.$plugin;
425 $includefile = '/enrol/'.$plugin.'/enrol.php';
426 $response = mnet_server_invoke_method($includefile, $methodname, $method, $payload, $enrolclass);
427 $response = mnet_server_prepare_response($response);
428 echo $response;
429 } else {
430 // Generate error response - unable to locate function
431 exit(mnet_server_fault(703, 'nosuchfunction'));
432 }
433
434 ////////////////////////////////////// STRICT MOD/*
435 } elseif ($callstack[0] == 'mod' || 'promiscuous' == $CFG->mnet_dispatcher_mode) {
436 list($base, $module, $filename, $functionname) = $callstack;
437
438 ////////////////////////////////////// STRICT MOD/*
439 if ($base == 'mod' && $filename == 'rpclib.php') {
440 $includefile = '/mod/'.$module.'/rpclib.php';
441 $response = mnet_server_invoke_method($includefile, $functionname, $method, $payload);
442 $response = mnet_server_prepare_response($response);
443 echo $response;
444
445 ////////////////////////////////////// PROMISCUOUS
446 } elseif ('promiscuous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) {
447
448 $functionname = array_pop($callstack);
449 $filename = array_pop($callstack);
450
451 if ($MNET_REMOTE_CLIENT->plaintext_is_ok()) {
452
453 // The call stack holds the path to any include file
454 $includefile = $CFG->dirroot.'/'.implode('/',$callstack).'/'.$filename.'.php';
455
456 $response = mnet_server_invoke_function($includefile, $functionname, $method, $payload);
457 echo $response;
458 }
459
460 } else {
461 // Generate error response - unable to locate function
462 exit(mnet_server_fault(7012, 'nosuchfunction'));
463 }
464
465 } else {
466 // Generate error response - unable to locate function
467 exit(mnet_server_fault(7012, 'nosuchfunction'));
468 }
469}
470
471/**
472 * Execute the system functions - mostly for introspection
473 *
474 * @param string $method XMLRPC method name, e.g. system.listMethods
475 * @param array $params Array of parameters from the XMLRPC request
476 * @param string $hostinfo Hostinfo object from the mnet_host table
477 * @return mixed Response data - any kind of PHP variable
478 */
479function mnet_system($method, $params, $hostinfo) {
480 global $CFG;
481
482 if (empty($hostinfo)) return array();
483
484 $id_list = $hostinfo->id;
485 if (!empty($CFG->mnet_all_hosts_id)) {
486 $id_list .= ', '.$CFG->mnet_all_hosts_id;
487 }
488
489 if ('system.listMethods' == $method || 'system/listMethods' == $method) {
490 if (count($params) == 0) {
491 $query = '
492 SELECT DISTINCT
493 rpc.function_name,
494 rpc.xmlrpc_path,
495 rpc.enabled,
496 rpc.help,
497 rpc.profile
498 FROM
499 '.$CFG->prefix.'mnet_host2service h2s,
500 '.$CFG->prefix.'mnet_service2rpc s2r,
501 '.$CFG->prefix.'mnet_rpc rpc
502 WHERE
503 s2r.rpcid = rpc.id AND
504 h2s.serviceid = s2r.serviceid AND
505 h2s.hostid in ('.$id_list .')
506 ORDER BY
507 rpc.xmlrpc_path ASC';
508
509 } else {
510 $query = '
511 SELECT DISTINCT
512 rpc.function_name,
513 rpc.xmlrpc_path,
514 rpc.enabled,
515 rpc.help,
516 rpc.profile
517 FROM
518 '.$CFG->prefix.'mnet_host2service h2s,
519 '.$CFG->prefix.'mnet_service2rpc s2r,
520 '.$CFG->prefix.'mnet_service svc,
521 '.$CFG->prefix.'mnet_rpc rpc
522 WHERE
523 s2r.rpcid = rpc.id AND
524 h2s.serviceid = s2r.serviceid AND
525 h2s.hostid in ('.$id_list .') AND
526 svc.id = h2s.serviceid AND
527 svc.name = \''.$params[0].'\'
528 ORDER BY
529 rpc.xmlrpc_path ASC';
530
531 }
532 $resultset = array_values(get_records_sql($query));
533 $methods = array();
534 foreach($resultset as $result) {
535 $methods[] = $result->xmlrpc_path;
536 }
537 return $methods;
538 } elseif ('system.methodSignature' == $method || 'system/methodSignature' == $method) {
539 $query = '
540 SELECT DISTINCT
541 rpc.function_name,
542 rpc.xmlrpc_path,
543 rpc.enabled,
544 rpc.help,
545 rpc.profile
546 FROM
547 '.$CFG->prefix.'mnet_host2service h2s,
548 '.$CFG->prefix.'mnet_service2rpc s2r,
549 '.$CFG->prefix.'mnet_rpc rpc
550 WHERE
551 rpc.xmlrpc_path = \''.$params[0].'\' AND
552 s2r.rpcid = rpc.id AND
553 h2s.serviceid = s2r.serviceid AND
554 h2s.hostid in ('.$id_list .')';
555
556 $result = get_records_sql($query);
557 $methodsigs = array();
558
559 if (is_array($result)) {
560 foreach($result as $method) {
561 $methodsigs[] = unserialize($method->profile);
562 }
563 }
564
565 return $methodsigs;
566 } elseif ('system.methodHelp' == $method || 'system/methodHelp' == $method) {
567 $query = '
568 SELECT DISTINCT
569 rpc.function_name,
570 rpc.xmlrpc_path,
571 rpc.enabled,
572 rpc.help,
573 rpc.profile
574 FROM
575 '.$CFG->prefix.'mnet_host2service h2s,
576 '.$CFG->prefix.'mnet_service2rpc s2r,
577 '.$CFG->prefix.'mnet_rpc rpc
578 WHERE
579 rpc.xmlrpc_path = \''.$params[0].'\' AND
580 s2r.rpcid = rpc.id AND
581 h2s.serviceid = s2r.serviceid AND
582 h2s.hostid in ('.$id_list .')';
583
584 $result = get_record_sql($query);
585
586 if (is_object($result)) {
587 return $result->help;
588 }
589 } elseif ('system.listServices' == $method || 'system/listServices' == $method) {
590 $query = '
591 SELECT DISTINCT
592 s.id,
593 s.name,
594 s.apiversion,
595 h2s.publish,
596 h2s.subscribe
597 FROM
598 '.$CFG->prefix.'mnet_host2service h2s,
599 '.$CFG->prefix.'mnet_service s
600 WHERE
601 h2s.serviceid = s.id AND
602 h2s.hostid in ('.$id_list .')
603 ORDER BY
604 s.name ASC';
605
606 $result = get_records_sql($query);
607 $services = array();
608
609 if (is_array($result)) {
610 foreach($result as $service) {
611 $services[] = array('name' => $service->name,
612 'apiversion' => $service->apiversion,
613 'publish' => $service->publish,
614 'subscribe' => $service->subscribe);
615 }
616 }
617
618 return $services;
619 }
620 exit(mnet_server_fault(7019, 'nosuchfunction'));
621}
622
623/**
624 * Initialize the object (if necessary), execute the method or function, and
625 * return the response
626 *
627 * @param string $includefile The file that contains the object definition
628 * @param string $methodname The name of the method to execute
629 * @param string $method The full path to the method
630 * @param string $payload The XML-RPC request payload
631 * @param string $class The name of the class to instantiate (or false)
632 * @return string The XML-RPC response
633 */
634function mnet_server_invoke_method($includefile, $methodname, $method, $payload, $class=false) {
635
636 $permission = mnet_permit_rpc_call($includefile, $methodname, $class);
637
638 if (RPC_NOSUCHFILE == $permission) {
639 // Generate error response - unable to locate function
640 exit(mnet_server_fault(705, 'nosuchfile', $includefile));
641 }
642
643 if (RPC_NOSUCHFUNCTION == $permission) {
644 // Generate error response - unable to locate function
645 exit(mnet_server_fault(706, 'nosuchfunction'));
646 }
647
648 if (RPC_FORBIDDENFUNCTION == $permission) {
649 // Generate error response - unable to locate function
650 exit(mnet_server_fault(707, 'forbidden-function'));
651 }
652
653 if (RPC_NOSUCHCLASS == $permission) {
654 // Generate error response - unable to locate function
655 exit(mnet_server_fault(7013, 'nosuchfunction'));
656 }
657
658 if (RPC_NOSUCHMETHOD == $permission) {
659 // Generate error response - unable to locate function
660 exit(mnet_server_fault(7014, 'nosuchmethod'));
661 }
662
663 if (RPC_NOSUCHFUNCTION == $permission) {
664 // Generate error response - unable to locate function
665 exit(mnet_server_fault(7014, 'nosuchmethod'));
666 }
667
668 if (RPC_FORBIDDENMETHOD == $permission) {
669 // Generate error response - unable to locate function
670 exit(mnet_server_fault(7015, 'nosuchfunction'));
671 }
672
673 if (0 < $permission) {
674 // Generate error response - unable to locate function
675 exit(mnet_server_fault(7019, 'unknownerror'));
676 }
677
678 if (RPC_OK == $permission) {
679 $xmlrpcserver = xmlrpc_server_create();
680 $bool = xmlrpc_server_register_method($xmlrpcserver, $method, 'mnet_server_dummy_method');
681 $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $methodname);
682 $bool = xmlrpc_server_destroy($xmlrpcserver);
683 return $response;
684 }
685}
686
85d2d959 687/**
688 * Accepts a public key from a new remote host and returns the public key for
689 * this host. If 'register all hosts' is turned on, it will bootstrap a record
690 * for the remote host in the mnet_host table (if it's not already there)
691 *
692 * @param string $function XML-RPC requires this but we don't... discard!
693 * @param array $params Array of parameters
694 * $params[0] is the remote wwwroot
695 * $params[1] is the remote public key
696 * @return string The XML-RPC response
697 */
71558f85 698function mnet_keyswap($function, $params) {
735c7beb 699 global $CFG, $MNET;
71558f85 700 $return = array();
701
702 if (!empty($CFG->mnet_register_allhosts)) {
703 $mnet_peer = new mnet_peer();
85d2d959 704 $keyok = $mnet_peer->bootstrap($params[0], $params[1]);
71558f85 705 if ($keyok) {
706 $mnet_peer->commit();
707 }
708 }
735c7beb 709 return $MNET->public_key;
71558f85 710}
85d2d959 711
71558f85 712?>