admin/webservices MDL-12886 Fixed some strings
[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:
af0e9032
PS
13// disable moodle specific debug messages and any errors in output
14define('NO_DEBUG_DISPLAY', true);
15// cookies are not used, makes sure there is empty global $USER
16define('NO_MOODLE_COOKIES', true);
17
71558f85 18require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
71558f85 19
20// Include MNET stuff:
21require_once $CFG->dirroot.'/mnet/lib.php';
22require_once $CFG->dirroot.'/mnet/remote_client.php';
de260e0f
PL
23require_once $CFG->dirroot.'/mnet/xmlrpc/serverlib.php';
24
71558f85 25
600be062 26if ($CFG->mnet_dispatcher_mode === 'off') {
27 print_error('mnetdisabled', 'mnet');
28}
29
71558f85 30// Content type for output is not html:
1008dad6 31header('Content-type: text/xml; charset=utf-8');
71558f85 32
25202581 33// PHP 5.2.2: $HTTP_RAW_POST_DATA not populated bug:
34// http://bugs.php.net/bug.php?id=41293
35if (empty($HTTP_RAW_POST_DATA)) {
36 $HTTP_RAW_POST_DATA = file_get_contents('php://input');
37}
38
71558f85 39if (!empty($CFG->mnet_rpcdebug)) {
de260e0f
PL
40 error_log("HTTP_RAW_POST_DATA");
41 error_log($HTTP_RAW_POST_DATA);
71558f85 42}
43
8d60e942 44if (!isset($_SERVER)) {
45 exit(mnet_server_fault(712, "phperror"));
46}
47
48
71558f85 49// New global variable which ONLY gets set in this server page, so you know that
50// if you've been called by a remote Moodle, this should be set:
51$MNET_REMOTE_CLIENT = new mnet_remote_client();
52
939ea0bc
PL
53try {
54 $plaintextmessage = mnet_server_strip_encryption($HTTP_RAW_POST_DATA);
55 $xmlrpcrequest = mnet_server_strip_signature($plaintextmessage);
c0b22a3f 56} catch (Exception $e) {
939ea0bc
PL
57 exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
58}
59
de260e0f
PL
60if (!empty($CFG->mnet_rpcdebug)) {
61 error_log("XMLRPC Payload");
62 error_log(print_r($xmlrpcrequest,1));
63}
8d60e942 64
334e66c1 65if($MNET_REMOTE_CLIENT->pushkey == true) {
8d60e942 66 // The peer used one of our older public keys, we will return a
67 // signed/encrypted error message containing our new public key
68 // Sign message with our old key, and encrypt to the peer's private key.
334e66c1 69 exit(mnet_server_fault_xml(7025, $MNET->public_key, $MNET_REMOTE_CLIENT->useprivatekey));
8d60e942 70}
71// Have a peek at what the request would be if we were to process it
72$params = xmlrpc_decode_request($xmlrpcrequest, $method);
73
74// One of three conditions need to be met before we continue processing this request:
75// 1. Request is properly encrypted and signed
76// 2. Request is for a keyswap (we don't mind enencrypted or unsigned requests for a public key)
77// 3. Request is properly signed and we're happy with it being unencrypted
78if ((($MNET_REMOTE_CLIENT->request_was_encrypted == true) && ($MNET_REMOTE_CLIENT->signatureok == true))
79 || (($method == 'system.keyswap') || ($method == 'system/keyswap'))
80 || (($MNET_REMOTE_CLIENT->signatureok == true) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == true))) {
939ea0bc 81 try {
de260e0f
PL
82 // main dispatch call. will echo the response directly
83 mnet_server_dispatch($xmlrpcrequest);
84 exit;
c0b22a3f 85 } catch (Exception $e) {
939ea0bc
PL
86 exit(mnet_server_fault($e->getCode(), $e->getMessage(), $e->a));
87 }
8d60e942 88}
de260e0f
PL
89// if we get to here, something is wrong
90// so detect a few common cases and send appropriate errors
91if (($MNET_REMOTE_CLIENT->request_was_encrypted == false) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false)) {
92 exit(mnet_server_fault(7021, 'forbidden-transport'));
71558f85 93}
94
de260e0f
PL
95if ($MNET_REMOTE_CLIENT->request_was_signed == false) {
96 // Request was not signed
97 exit(mnet_server_fault(711, 'verifysignature-error'));
5f6b28fa 98}
71558f85 99
de260e0f
PL
100if ($MNET_REMOTE_CLIENT->signatureok == false) {
101 // We were unable to verify the signature
102 exit(mnet_server_fault(710, 'verifysignature-invalid'));
71558f85 103}
de260e0f 104exit(mnet_server_fault(7000, 'unknownerror'));