From 287efec6f6f0805e47ad9ad59f4a157fcbdda57b Mon Sep 17 00:00:00 2001 From: Penny Leach Date: Tue, 2 Feb 2010 03:13:40 +0000 Subject: [PATCH] mnet MDL-21276 get rid of all the $MNET* globals --- admin/mnet/index.php | 17 ++-- auth/mnet/auth.php | 66 +++++++++------- enrol/mnet/enrol.php | 31 +++++--- lang/en_utf8/mnet.php | 1 + lib/moodlelib.php | 81 ++++++++++++++++--- lib/portfolio/plugin.php | 7 ++ mnet/lib.php | 32 ++++---- mnet/xmlrpc/client.php | 7 +- mnet/xmlrpc/server.php | 20 +++-- mnet/xmlrpc/serverlib.php | 83 ++++++++++---------- mnet/xmlrpc/xmlparser.php | 3 - portfolio/mahara/lib.php | 23 +++--- repository/mahara/repository.class.php | 18 +---- repository/remotemoodle/repository.class.php | 28 ++----- 14 files changed, 238 insertions(+), 179 deletions(-) diff --git a/admin/mnet/index.php b/admin/mnet/index.php index c0bd5415822..83aff87633c 100644 --- a/admin/mnet/index.php +++ b/admin/mnet/index.php @@ -14,6 +14,7 @@ require_capability('moodle/site:config', $context, $USER->id, true, "nopermissions"); $site = get_site(); + $mnet = get_mnet_environment(); if (!extension_loaded('openssl')) { admin_externalpage_print_header(); @@ -42,10 +43,10 @@ } } } elseif (!empty($form->submit) && $form->submit == get_string('delete')) { - $MNET->get_private_key(); - $SESSION->mnet_confirm_delete_key = md5(sha1($MNET->keypair['keypair_PEM'])).':'.time(); + $mnet->get_private_key(); + $SESSION->mnet_confirm_delete_key = md5(sha1($mnet->keypair['keypair_PEM'])).':'.time(); - $formcontinue = new single_button(new moodle_url('index.php', array('confirm' => md5($MNET->public_key))), get_string('yes')); + $formcontinue = new single_button(new moodle_url('index.php', array('confirm' => md5($mnet->public_key))), get_string('yes')); $formcancel = new single_button(new moodle_url('index.php', array()), get_string('no')); echo $OUTPUT->confirm(get_string("deletekeycheck", "mnet"), $formcontinue, $formcancel); exit; @@ -60,7 +61,7 @@ $key = ''; $time = ''; @list($key, $time) = explode(':',$SESSION->mnet_confirm_delete_key); - $MNET->get_private_key(); + $mnet->get_private_key(); if($time < time() - 60) { // fail - you're out of time. @@ -68,13 +69,13 @@ exit; } - if ($key != md5(sha1($MNET->keypair['keypair_PEM']))) { + if ($key != md5(sha1($mnet->keypair['keypair_PEM']))) { // fail - you're being attacked? print_error ('deletewrongkeyvalue', 'mnet', 'index.php'); exit; } - $MNET->replace_keys(); + $mnet->replace_keys(); redirect('index.php', get_string('keydeleted','mnet')); exit; } @@ -94,11 +95,11 @@ : -
public_key; ?>
+
public_key; ?>
: - public_key_expires); ?> + public_key_expires); ?> diff --git a/auth/mnet/auth.php b/auth/mnet/auth.php index ba0b2c9a8eb..b94e92f7900 100644 --- a/auth/mnet/auth.php +++ b/auth/mnet/auth.php @@ -29,6 +29,7 @@ class auth_plugin_mnet extends auth_plugin_base { function auth_plugin_mnet() { $this->authtype = 'mnet'; $this->config = get_config('auth/mnet'); + $this->mnet = get_mnet_environment(); } /** @@ -52,7 +53,8 @@ class auth_plugin_mnet extends auth_plugin_base { * @return array $userdata Array of user info for remote host */ function user_authorise($token, $useragent) { - global $CFG, $MNET, $SITE, $MNET_REMOTE_CLIENT, $DB; + global $CFG, $SITE, $DB; + $remoteclient = get_mnet_remote_client(); require_once $CFG->dirroot . '/mnet/xmlrpc/serverlib.php'; $mnet_session = $DB->get_record('mnet_session', array('token'=>$token, 'useragent'=>$useragent)); @@ -87,7 +89,7 @@ class auth_plugin_mnet extends auth_plugin_base { $userdata['maildigest'] = $user->maildigest; $userdata['maildisplay'] = $user->maildisplay; $userdata['htmleditor'] = $user->htmleditor; - $userdata['wwwroot'] = $MNET->wwwroot; + $userdata['wwwroot'] = $this->mnet->wwwroot; $userdata['session.gc_maxlifetime'] = ini_get('session.gc_maxlifetime'); $userdata['picture'] = $user->picture; if (!empty($user->picture)) { @@ -121,7 +123,7 @@ class auth_plugin_mnet extends auth_plugin_base { h.name, h.id, h.wwwroot"; - if ($courses = $DB->get_records_sql($sql, array($user->id, $MNET_REMOTE_CLIENT->id))) { + if ($courses = $DB->get_records_sql($sql, array($user->id, $remoteclient->id))) { foreach($courses as $course) { $userdata['myhosts'][] = array('name'=> $course->hostname, 'url' => $CFG->wwwroot.'/auth/mnet/jump.php?hostid='.$course->hostid, 'count' => $course->count); } @@ -146,7 +148,7 @@ class auth_plugin_mnet extends auth_plugin_base { * rather than somewhere inside *its* wwwroot */ function start_jump_session($mnethostid, $wantsurl, $wantsurlbackhere=false) { - global $CFG, $USER, $MNET, $DB; + global $CFG, $USER, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; // check remote login permissions @@ -199,7 +201,7 @@ class auth_plugin_mnet extends auth_plugin_base { // construct the redirection URL //$transport = mnet_get_protocol($mnet_peer->transport); $wantsurl = urlencode($wantsurl); - $url = "{$mnet_peer->wwwroot}{$mnet_peer->application->sso_land_url}?token={$mnet_session->token}&idp={$MNET->wwwroot}&wantsurl={$wantsurl}"; + $url = "{$mnet_peer->wwwroot}{$mnet_peer->application->sso_land_url}?token={$mnet_session->token}&idp={$this->mnet->wwwroot}&wantsurl={$wantsurl}"; if ($wantsurlbackhere) { $url .= '&remoteurl=1'; } @@ -228,7 +230,7 @@ class auth_plugin_mnet extends auth_plugin_base { * @return array The local user record. */ function confirm_mnet_session($token, $remotewwwroot) { - global $CFG, $MNET, $DB; + global $CFG, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; // verify the remote host is configured locally before attempting RPC call @@ -462,7 +464,8 @@ class auth_plugin_mnet extends auth_plugin_base { * @return bool */ function update_enrolments($username, $courses) { - global $MNET_REMOTE_CLIENT, $CFG, $DB; + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); if (empty($username) || !is_array($courses)) { return false; @@ -470,12 +473,12 @@ class auth_plugin_mnet extends auth_plugin_base { // make sure it is a user we have an in active session // with that host... if (!$userid = $DB->get_field('mnet_session', 'userid', - array('username'=>$username, 'mnethostid'=>$MNET_REMOTE_CLIENT->id))) { + array('username'=>$username, 'mnethostid'=>$remoteclient->id))) { throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet')); } if (empty($courses)) { // no courses? clear out quickly - $DB->delete_records('mnet_enrol_assignments', array('hostid'=>$MNET_REMOTE_CLIENT->id, 'userid'=>$userid)); + $DB->delete_records('mnet_enrol_assignments', array('hostid'=>$remoteclient->id, 'userid'=>$userid)); return true; } @@ -510,13 +513,13 @@ class auth_plugin_mnet extends auth_plugin_base { WHERE c.hostid = ?'; - $currentcourses = $DB->get_records_sql($sql, array($userid, $MNET_REMOTE_CLIENT->id)); + $currentcourses = $DB->get_records_sql($sql, array($userid, $remoteclient->id)); $local_courseid_array = array(); foreach($courses as $course) { $course['remoteid'] = $course['id']; - $course['hostid'] = (int)$MNET_REMOTE_CLIENT->id; + $course['hostid'] = (int)$remoteclient->id; $userisregd = false; // First up - do we have a record for this course? @@ -560,7 +563,7 @@ class auth_plugin_mnet extends auth_plugin_base { // No - create a record $assignObj = new stdClass(); $assignObj->userid = $userid; - $assignObj->hostid = (int)$MNET_REMOTE_CLIENT->id; + $assignObj->hostid = (int)$remoteclient->id; $assignObj->courseid = $course['id']; $assignObj->rolename = $course['defaultrolename']; $assignObj->id = $DB->insert_record('mnet_enrol_assignments', $assignObj); @@ -570,7 +573,7 @@ class auth_plugin_mnet extends auth_plugin_base { // Clean up courses that the user is no longer enrolled in. $local_courseid_string = implode(', ', $local_courseid_array); $whereclause = " userid = ? AND hostid = ? AND courseid NOT IN ($local_courseid_string)"; - $DB->delete_records_select('mnet_enrol_assignments', $whereclause, array($userid, $MNET_REMOTE_CLIENT->id)); + $DB->delete_records_select('mnet_enrol_assignments', $whereclause, array($userid, $remoteclient->id)); } function prevent_local_passwords() { @@ -704,7 +707,7 @@ class auth_plugin_mnet extends auth_plugin_base { * @return void */ function keepalive_client() { - global $CFG, $MNET, $DB; + global $CFG, $DB; $cutoff = time() - 300; // TODO - find out what the remote server's session // cutoff is, and preempt that @@ -849,7 +852,8 @@ class auth_plugin_mnet extends auth_plugin_base { * @return string "All ok" or an error message */ function refresh_log($array) { - global $CFG, $MNET_REMOTE_CLIENT, $DB; + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); // We don't want to output anything to the client machine $start = ob_start(); @@ -860,7 +864,7 @@ class auth_plugin_mnet extends auth_plugin_base { foreach($array as $logEntry) { $logEntryObj = (object)$logEntry; - $logEntryObj->hostid = $MNET_REMOTE_CLIENT->id; + $logEntryObj->hostid = $remoteclient->id; if (isset($useridarray[$logEntryObj->username])) { $logEntryObj->userid = $useridarray[$logEntryObj->username]; @@ -878,12 +882,12 @@ class auth_plugin_mnet extends auth_plugin_base { $insertok = $DB->insert_record('mnet_log', $logEntryObj, false); if ($insertok) { - $MNET_REMOTE_CLIENT->last_log_id = $logEntryObj->remoteid; + $remoteclient->last_log_id = $logEntryObj->remoteid; } else { $returnString .= 'Record with id '.$logEntryObj->remoteid." failed to insert.\n"; } } - $MNET_REMOTE_CLIENT->commit(); + $remoteclient->commit(); $transaction->allow_commit(); $end = ob_end_clean(); @@ -900,7 +904,8 @@ class auth_plugin_mnet extends auth_plugin_base { * @return string "All ok" or an error message */ function keepalive_server($array) { - global $MNET_REMOTE_CLIENT, $CFG, $DB; + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); $CFG->usesid = true; @@ -931,8 +936,8 @@ class auth_plugin_mnet extends auth_plugin_base { $end = ob_end_clean(); - if (empty($returnString)) return array('code' => 0, 'message' => 'All ok', 'last log id' => $MNET_REMOTE_CLIENT->last_log_id); - return array('code' => 1, 'message' => $returnString, 'last log id' => $MNET_REMOTE_CLIENT->last_log_id); + if (empty($returnString)) return array('code' => 0, 'message' => 'All ok', 'last log id' => $remoteclient->last_log_id); + return array('code' => 1, 'message' => $returnString, 'last log id' => $remoteclient->last_log_id); } /** @@ -963,15 +968,14 @@ class auth_plugin_mnet extends auth_plugin_base { * @return void */ function prelogout_hook() { - global $MNET, $CFG, $USER; + global $CFG, $USER; + if (!is_enabled_auth('mnet')) { return; } - require_once $CFG->dirroot.'/mnet/xmlrpc/client.php'; - // If the user is local to this Moodle: - if ($USER->mnethostid == $MNET->id) { + if ($USER->mnethostid == $this->mnet->id) { $this->kill_children($USER->username, sha1($_SERVER['HTTP_USER_AGENT'])); // Else the user has hit 'logout' at a Service Provider Moodle: @@ -1036,7 +1040,8 @@ class auth_plugin_mnet extends auth_plugin_base { * @return string A plaintext report of what has happened */ function kill_children($username, $useragent) { - global $CFG, $USER, $MNET_REMOTE_CLIENT, $DB; + global $CFG, $USER, $DB; + $remoteclient = get_mnet_remote_client(); require_once $CFG->dirroot.'/mnet/xmlrpc/client.php'; $userid = $DB->get_field('user', 'id', array('mnethostid'=>$CFG->mnet_localhost_id, 'username'=>$username)); @@ -1054,7 +1059,7 @@ class auth_plugin_mnet extends auth_plugin_base { // If this script is being executed by a remote peer, that means the user has clicked // logout on that peer, and the session on that peer can be deleted natively. // Skip over it. - if (isset($MNET_REMOTE_CLIENT->id) && ($mnetsession->mnethostid == $MNET_REMOTE_CLIENT->id)) { + if (isset($remoteclient->id) && ($mnetsession->mnethostid == $remoteclient->id)) { continue; } $returnstring .= "Deleting session\n"; @@ -1077,7 +1082,7 @@ class auth_plugin_mnet extends auth_plugin_base { $ignore = $DB->delete_records('mnet_session', array('useragent'=>$useragent, 'userid'=>$userid)); - if (isset($MNET_REMOTE_CLIENT) && isset($MNET_REMOTE_CLIENT->id)) { + if (isset($remoteclient) && isset($remoteclient->id)) { session_kill_user($userid); } return $returnstring; @@ -1093,8 +1098,9 @@ class auth_plugin_mnet extends auth_plugin_base { * @return bool True on success */ function kill_child($username, $useragent) { - global $CFG, $MNET_REMOTE_CLIENT, $DB; - $session = $DB->get_record('mnet_session', array('username'=>$username, 'mnethostid'=>$MNET_REMOTE_CLIENT->id, 'useragent'=>$useragent)); + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); + $session = $DB->get_record('mnet_session', array('username'=>$username, 'mnethostid'=>$remoteclient->id, 'useragent'=>$useragent)); if (false != $session) { session_kill($session->session_id); return true; diff --git a/enrol/mnet/enrol.php b/enrol/mnet/enrol.php index 7f211d6b7d7..195f1b57e2e 100644 --- a/enrol/mnet/enrol.php +++ b/enrol/mnet/enrol.php @@ -6,6 +6,13 @@ class enrolment_plugin_mnet { + /** * mnet environment - constructor makes sure its set up */ + private $mnet; + + function __construct() { + $this->mnet = get_mnet_environment(); + } + /// Override the base config_form() function function config_form($frm) { global $CFG, $OUTPUT, $PAGE; @@ -229,13 +236,15 @@ class enrolment_plugin_mnet { * client machine */ function course_enrolments($courseid, $roles = '') { - global $MNET_REMOTE_CLIENT, $CFG, $DB; + global $CFG, $DB; if (! $course = $DB->get_record('course', array('id'=>$courseid))) { return 'no course'; //error("That's an invalid course id"); } + $remoteclient = get_mnet_remote_client(); + $context = get_context_instance(CONTEXT_COURSE, $courseid); $sql = " @@ -254,7 +263,7 @@ class enrolment_plugin_mnet { a.contextid = {$context->id} AND a.roleid = r.id AND a.userid = u.id AND - u.mnethostid = '{$MNET_REMOTE_CLIENT->id}' + u.mnethostid = '{$remoteclient->id}' "; if(!empty($roles)) { @@ -284,9 +293,10 @@ class enrolment_plugin_mnet { * @return bool Whether the enrolment has been successful */ function enrol_user($user, $courseid) { - global $MNET, $MNET_REMOTE_CLIENT, $DB; + global $DB; + $remoteclient = get_mnet_remote_client(); - $userrecord = $DB->get_record('user',array('username'=>$user['username'], 'mnethostid'=>$MNET_REMOTE_CLIENT->id)); + $userrecord = $DB->get_record('user',array('username'=>$user['username'], 'mnethostid'=>$remoteclient->id)); if ($userrecord == false) { // We should at least be checking that we allow the remote @@ -297,7 +307,7 @@ class enrolment_plugin_mnet { $userrecord->email = $user['email']; $userrecord->firstname = $user['firstname']; $userrecord->lastname = $user['lastname']; - $userrecord->mnethostid = $MNET_REMOTE_CLIENT->id; + $userrecord->mnethostid = $remoteclient->id; //TODO - username required to use PARAM_USERNAME before inserting into user table (MDL-16919) if ($userrecord->id = $DB->insert_record('user', $userrecord)) { @@ -330,9 +340,10 @@ class enrolment_plugin_mnet { * @return bool Whether the user can login from the remote host */ function unenrol_user($username, $courseid) { - global $MNET_REMOTE_CLIENT, $DB; + global $DB; + $remoteclient = get_mnet_remote_client(); - if (!$userrecord = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$MNET_REMOTE_CLIENT->id))) { + if (!$userrecord = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$remoteclient->id))) { throw new mnet_exception(5014, get_string('usernotfound', 'enrol_mnet')); } @@ -395,7 +406,7 @@ class enrolment_plugin_mnet { * @return array Whether the user can login from the remote host */ function fetch_remote_courses($mnethostid) { - global $CFG, $USER, $MNET, $DB; + global $CFG, $USER, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; // get the Service Provider info @@ -491,7 +502,7 @@ class enrolment_plugin_mnet { * @return array Whether the user can login from the remote host */ function req_enrol_user($userid, $courseid) { - global $CFG, $USER, $MNET, $DB; + global $CFG, $USER, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; // Prepare a basic user record @@ -537,7 +548,7 @@ class enrolment_plugin_mnet { * @return array Whether the user can login from the remote host */ function req_unenrol_user($userid, $courseid) { - global $CFG, $USER, $MNET, $DB; + global $CFG, $USER, $DB; require_once $CFG->dirroot . '/mnet/xmlrpc/client.php'; // in case the remote host doesn't have it diff --git a/lang/en_utf8/mnet.php b/lang/en_utf8/mnet.php index 4197e3a0cc6..854a61b1f93 100644 --- a/lang/en_utf8/mnet.php +++ b/lang/en_utf8/mnet.php @@ -285,4 +285,5 @@ $string['methodhelp'] = 'Method help for $a'; $string['testclient'] = 'Moodle Network Test Client'; $string['unknown'] = 'Unknown'; $string['notmoodleapplication'] = 'WARNING: This is not a moodle application, so some of the inspection methods may not work properly.'; +$string['notinxmlrpcserver'] = 'Attempt to access the MNET remote client, not during XMLRPC server execution'; ?> diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 499256284fb..5103b19029f 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4619,21 +4619,15 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $a // home site (identity provider - idp) before hitting the link itself if (is_mnet_remote_user($user)) { require_once($CFG->dirroot.'/mnet/lib.php'); - // Form the request url to hit the idp's jump.php - if (isset($mnetjumps[$user->mnethostid])) { - $MNETIDPJUMPURL = $mnetjumps[$user->mnethostid]; - } else { - $idp = mnet_get_peer_host($user->mnethostid); - $idpjumppath = mnet_get_app_jumppath($idp->applicationid); - $MNETIDPJUMPURL = $idp->wwwroot . $idpjumppath . '?hostwwwroot=' . $CFG->wwwroot . '&wantsurl='; - $mnetjumps[$user->mnethostid] = $MNETIDPJUMPURL; - } + + $jumpurl = mnet_get_idp_jump_url($user); + $callback = partial('mnet_sso_apply_redirection', $jumpurl); $messagetext = preg_replace_callback("%($CFG->wwwroot[^[:space:]]*)%", - 'mnet_sso_apply_indirection', + $callback, $messagetext); $messagehtml = preg_replace_callback("%href=[\"'`]($CFG->wwwroot[\w_:\?=#&@/;.~-]*)[\"'`]%", - 'mnet_sso_apply_indirection', + $callback, $messagehtml); } $mail =& get_mailer(); @@ -9295,3 +9289,68 @@ function partial() { $p = new partial($func, $args); return array($p, 'method'); } + +/** + * helper function to load up and initialise the mnet environment + * this must be called before you use mnet functions. + * + * @return mnet_environment the equivalent of old $MNET global + */ +function get_mnet_environment() { + global $CFG; + require_once($CFG->dirroot . '/mnet/lib.php'); + static $instance = null; + if (empty($instance)) { + $instance = new mnet_environment(); + $instance->init(); + } + return $instance; +} + +/** + * during xmlrpc server code execution, any code wishing to access + * information about the remote peer must use this to get it. + * + * @return mnet_remote_client the equivalent of old $MNET_REMOTE_CLIENT global + */ +function get_mnet_remote_client() { + if (!defined('MNET_SERVER')) { + debugging(get_string('notinxmlrpcserver', 'mnet')); + return false; + } + global $MNET_REMOTE_CLIENT; + if (isset($MNET_REMOTE_CLIENT)) { + return $MNET_REMOTE_CLIENT; + } + return false; +} + +/** + * during the xmlrpc server code execution, this will be called + * to setup the object returned by {@see get_mnet_remote_client} + * + * @param mnet_remote_client $client the client to set up + */ +function set_mnet_remote_client($client) { + if (!defined('MNET_SERVER')) { + throw new moodle_exception('notinxmlrpcserver', 'mnet'); + } + global $MNET_REMOTE_CLIENT; + $MNET_REMOTE_CLIENT = $client; +} + +/** + * return the jump url for a given remote user + * this is used for rewriting forum post links in emails, etc + * + * @param stdclass $user the user to get the idp url for + */ +function mnet_get_idp_jump_url($user) { + static $mnetjumps = array(); + if (!array_key_exists($user->mnethostid, $mnetjumps)) { + $idp = mnet_get_peer_host($user->mnethostid); + $idpjumppath = mnet_get_app_jumppath($idp->applicationid); + $mnetjumps[$user->mnethostid] = $idp->wwwroot . $idpjumppath . '?hostwwwroot=' . $CFG->wwwroot . '&wantsurl='; + } + return $mnetjumps[$user->mnethostid]; +} diff --git a/lib/portfolio/plugin.php b/lib/portfolio/plugin.php index 46bdf332183..0b2a3260f87 100644 --- a/lib/portfolio/plugin.php +++ b/lib/portfolio/plugin.php @@ -497,9 +497,16 @@ abstract class portfolio_plugin_base { foreach ($DB->get_records('portfolio_instance_config', array('instance' => $instanceid)) as $config) { $this->config->{$config->name} = $config->value; } + $this->init(); return $this; } + /** + * called after __construct - allows plugins to perform initialisation tasks + * without having to override the constructor. + */ + protected function init() { } + /** * a list of fields that can be configured per instance. * this is used for the save handlers of the config form diff --git a/mnet/lib.php b/mnet/lib.php index 5aa0b395b0b..7420d6a2cd0 100644 --- a/mnet/lib.php +++ b/mnet/lib.php @@ -21,9 +21,6 @@ define('RPC_FORBIDDENFUNCTION', 4); define('RPC_NOSUCHMETHOD', 5); define('RPC_FORBIDDENMETHOD', 6); -$MNET = new mnet_environment(); -$MNET->init(); - /** * Strip extraneous detail from a URL or URI and return the hostname * @@ -46,7 +43,8 @@ function mnet_get_hostname_from_uri($uri = null) { * @return string A PEM formatted SSL Certificate. */ function mnet_get_public_key($uri, $application=null) { - global $CFG, $MNET, $DB; + global $CFG, $DB; + $mnet = get_mnet_environment(); // The key may be cached in the mnet_set_public_key function... // check this first $key = mnet_set_public_key($uri); @@ -58,7 +56,7 @@ function mnet_get_public_key($uri, $application=null) { $application = $DB->get_record('mnet_application', array('name'=>'moodle')); } - $rq = xmlrpc_encode_request('system/keyswap', array($CFG->wwwroot, $MNET->public_key, $application->name), array("encoding" => "utf-8")); + $rq = xmlrpc_encode_request('system/keyswap', array($CFG->wwwroot, $mnet->public_key, $application->name), array("encoding" => "utf-8")); $ch = curl_init($uri . $application->xmlrpc_server_url); curl_setopt($ch, CURLOPT_TIMEOUT, 60); @@ -189,13 +187,14 @@ function mnet_set_public_key($uri, $key = null) { * @return string An XML-DSig document */ function mnet_sign_message($message, $privatekey = null) { - global $CFG, $MNET; + global $CFG; $digest = sha1($message); + $mnet = get_mnet_environment(); // If the user hasn't supplied a private key (for example, one of our older, // expired private keys, we get the current default private key and use that. if ($privatekey == null) { - $privatekey = $MNET->get_private_key(); + $privatekey = $mnet->get_private_key(); } // The '$sig' value below is returned by reference. @@ -220,7 +219,7 @@ function mnet_sign_message($message, $privatekey = null) { '.base64_encode($message).' - '.$MNET->wwwroot.' + '.$mnet->wwwroot.' '.time().' '; return $message; @@ -252,7 +251,7 @@ function mnet_sign_message($message, $privatekey = null) { * @return string An XML-ENC document */ function mnet_encrypt_message($message, $remote_certificate) { - global $MNET; + $mnet = get_mnet_environment(); // Generate a key resource from the remote_certificate text string $publickey = openssl_get_publickey($remote_certificate); @@ -296,7 +295,7 @@ function mnet_encrypt_message($message, $remote_certificate) { XMLENC - '.$MNET->wwwroot.' + '.$mnet->wwwroot.' '; return $message; } @@ -467,12 +466,12 @@ function mnet_get_peer_host ($mnethostid) { * log in at their mnet identity provider (if they are not already logged in) * before ultimately being directed to the original url. * - * uses global MNETIDPJUMPURL the url which user should initially be directed to - * MNETIDPJUMPURL is a URL associated with a moodle networking peer when it + * @param string $jumpurl the url which user should initially be directed to. + * This is a URL associated with a moodle networking peer when it * is fulfiling a role as an identity provider (IDP). Different urls for * different peers, the jumpurl is formed partly from the IDP's webroot, and * partly from a predefined local path within that webwroot. - * The result of the user hitting MNETIDPJUMPURL is that they will be asked + * The result of the user hitting this jump url is that they will be asked * to login (at their identity provider (if they aren't already)), mnet * will prepare the necessary authentication information, then redirect * them back to somewhere at the content provider(CP) moodle (this moodle) @@ -481,9 +480,8 @@ function mnet_get_peer_host ($mnethostid) { * 1 - the destination url * @return string the url the remote user should be supplied with. */ -function mnet_sso_apply_indirection ($url) { - global $MNETIDPJUMPURL; - global $CFG; +function mnet_sso_apply_indirection ($jumpurl, $url) { + global $USER, $CFG; $localpart=''; $urlparts = parse_url($url[1]); @@ -505,7 +503,7 @@ function mnet_sso_apply_indirection ($url) { $localpart .= '#'.$urlparts['fragment']; } } - $indirecturl = $MNETIDPJUMPURL . urlencode($localpart); + $indirecturl = $jumpurl . urlencode($localpart); //If we matched on more than just a url (ie an html link), return the url to an href format if ($url[0] != $url[1]) { $indirecturl = 'href="'.$indirecturl.'"'; diff --git a/mnet/xmlrpc/client.php b/mnet/xmlrpc/client.php index 538a6ebab8c..cb5718092d2 100644 --- a/mnet/xmlrpc/client.php +++ b/mnet/xmlrpc/client.php @@ -20,11 +20,14 @@ class mnet_xmlrpc_client { var $timeout = 60; var $error = array(); var $response = ''; + var $mnet = null; /** * Constructor returns true */ function mnet_xmlrpc_client() { + // make sure we've got this set up before we try and do anything else + $this->mnet = get_mnet_environment(); return true; } @@ -122,7 +125,7 @@ class mnet_xmlrpc_client { * remote function */ function send($mnet_peer) { - global $CFG, $MNET, $DB; + global $CFG, $DB; if (!$this->permission_to_call($mnet_peer)) { @@ -186,7 +189,7 @@ class mnet_xmlrpc_client { $decryptedenvelope = ''; // &$decryptedenvelope - $isOpen = openssl_open(base64_decode($data), $decryptedenvelope, base64_decode($key), $MNET->get_private_key()); + $isOpen = openssl_open(base64_decode($data), $decryptedenvelope, base64_decode($key), $this->mnet->get_private_key()); if (!$isOpen) { // Decryption failed... let's try our archived keys diff --git a/mnet/xmlrpc/server.php b/mnet/xmlrpc/server.php index f9cbbf43965..13f269b10d3 100644 --- a/mnet/xmlrpc/server.php +++ b/mnet/xmlrpc/server.php @@ -15,8 +15,11 @@ define('NO_DEBUG_DISPLAY', true); // cookies are not used, makes sure there is empty global $USER define('NO_MOODLE_COOKIES', true); +define('MNET_SERVER', true); + require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); +$mnet = get_mnet_environment(); // Include MNET stuff: require_once $CFG->dirroot.'/mnet/lib.php'; require_once $CFG->dirroot.'/mnet/remote_client.php'; @@ -48,7 +51,8 @@ if (!isset($_SERVER)) { // New global variable which ONLY gets set in this server page, so you know that // if you've been called by a remote Moodle, this should be set: -$MNET_REMOTE_CLIENT = new mnet_remote_client(); +$remoteclient = new mnet_remote_client(); +set_mnet_remote_client($remoteclient); try { $plaintextmessage = mnet_server_strip_encryption($HTTP_RAW_POST_DATA); @@ -62,11 +66,11 @@ if (!empty($CFG->mnet_rpcdebug)) { error_log(print_r($xmlrpcrequest,1)); } -if($MNET_REMOTE_CLIENT->pushkey == true) { +if($remoteclient->pushkey == true) { // The peer used one of our older public keys, we will return a // signed/encrypted error message containing our new public key // Sign message with our old key, and encrypt to the peer's private key. - exit(mnet_server_fault_xml(7025, $MNET->public_key, $MNET_REMOTE_CLIENT->useprivatekey)); + exit(mnet_server_fault_xml(7025, $mnet->public_key, $remoteclient->useprivatekey)); } // Have a peek at what the request would be if we were to process it $params = xmlrpc_decode_request($xmlrpcrequest, $method); @@ -75,9 +79,9 @@ $params = xmlrpc_decode_request($xmlrpcrequest, $method); // 1. Request is properly encrypted and signed // 2. Request is for a keyswap (we don't mind enencrypted or unsigned requests for a public key) // 3. Request is properly signed and we're happy with it being unencrypted -if ((($MNET_REMOTE_CLIENT->request_was_encrypted == true) && ($MNET_REMOTE_CLIENT->signatureok == true)) +if ((($remoteclient->request_was_encrypted == true) && ($remoteclient->signatureok == true)) || (($method == 'system.keyswap') || ($method == 'system/keyswap')) - || (($MNET_REMOTE_CLIENT->signatureok == true) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == true))) { + || (($remoteclient->signatureok == true) && ($remoteclient->plaintext_is_ok() == true))) { try { // main dispatch call. will echo the response directly mnet_server_dispatch($xmlrpcrequest); @@ -88,16 +92,16 @@ if ((($MNET_REMOTE_CLIENT->request_was_encrypted == true) && ($MNET_REMOTE_CLIEN } // if we get to here, something is wrong // so detect a few common cases and send appropriate errors -if (($MNET_REMOTE_CLIENT->request_was_encrypted == false) && ($MNET_REMOTE_CLIENT->plaintext_is_ok() == false)) { +if (($remoteclient->request_was_encrypted == false) && ($remoteclient->plaintext_is_ok() == false)) { exit(mnet_server_fault(7021, 'forbidden-transport')); } -if ($MNET_REMOTE_CLIENT->request_was_signed == false) { +if ($remoteclient->request_was_signed == false) { // Request was not signed exit(mnet_server_fault(711, 'verifysignature-error')); } -if ($MNET_REMOTE_CLIENT->signatureok == false) { +if ($remoteclient->signatureok == false) { // We were unable to verify the signature exit(mnet_server_fault(710, 'verifysignature-invalid')); } diff --git a/mnet/xmlrpc/serverlib.php b/mnet/xmlrpc/serverlib.php index fb9c077f4b4..4406de0489a 100644 --- a/mnet/xmlrpc/serverlib.php +++ b/mnet/xmlrpc/serverlib.php @@ -34,16 +34,17 @@ * @return string XML with any encryption envolope removed */ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) { - global $MNET, $MNET_REMOTE_CLIENT; + $remoteclient = get_mnet_remote_client(); $crypt_parser = new mnet_encxml_parser(); $crypt_parser->parse($HTTP_RAW_POST_DATA); + $mnet = get_mnet_environment(); if (!$crypt_parser->payload_encrypted) { return $HTTP_RAW_POST_DATA; } // Make sure we know who we're talking to - $host_record_exists = $MNET_REMOTE_CLIENT->set_wwwroot($crypt_parser->remote_wwwroot); + $host_record_exists = $remoteclient->set_wwwroot($crypt_parser->remote_wwwroot); if (false == $host_record_exists) { throw new mnet_server_exception(7020, 'wrong-wwwroot', $crypt_parser->remote_wwwroot); @@ -58,9 +59,9 @@ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) { $payload = ''; // Initialize payload var // &$payload - $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $MNET->get_private_key()); + $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $mnet->get_private_key()); if ($isOpen) { - $MNET_REMOTE_CLIENT->was_encrypted(); + $remoteclient->was_encrypted(); return $payload; } @@ -77,10 +78,9 @@ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) { $isOpen = openssl_open(base64_decode($data), $payload, base64_decode($key), $keyresource); if ($isOpen) { // It's an older code, sir, but it checks out - - $MNET_REMOTE_CLIENT->was_encrypted(); - $MNET_REMOTE_CLIENT->encrypted_to($keyresource); - $MNET_REMOTE_CLIENT->set_pushkey(); + $remoteclient->was_encrypted(); + $remoteclient->encrypted_to($keyresource); + $remoteclient->set_pushkey(); return $payload; } } @@ -96,7 +96,7 @@ function mnet_server_strip_encryption($HTTP_RAW_POST_DATA) { * @return string XMLRPC request */ function mnet_server_strip_signature($plaintextmessage) { - global $MNET, $MNET_REMOTE_CLIENT; + $remoteclient = get_mnet_remote_client(); $sig_parser = new mnet_encxml_parser(); $sig_parser->parse($plaintextmessage); @@ -105,14 +105,14 @@ function mnet_server_strip_signature($plaintextmessage) { } // Record that the request was signed in some way - $MNET_REMOTE_CLIENT->was_signed(); + $remoteclient->was_signed(); // Load any information we have about this mnet peer - $MNET_REMOTE_CLIENT->set_wwwroot($sig_parser->remote_wwwroot); + $remoteclient->set_wwwroot($sig_parser->remote_wwwroot); $payload = base64_decode($sig_parser->data_object); $signature = base64_decode($sig_parser->signature); - $certificate = $MNET_REMOTE_CLIENT->public_key; + $certificate = $remoteclient->public_key; // If we don't have any certificate for the host, don't try to check the signature // Just return the parsed request @@ -125,20 +125,20 @@ function mnet_server_strip_signature($plaintextmessage) { if ($signature_verified == 0) { // $signature was not generated for $payload using $certificate // Get the key the remote peer is currently publishing: - $currkey = mnet_get_public_key($MNET_REMOTE_CLIENT->wwwroot, $MNET_REMOTE_CLIENT->application); + $currkey = mnet_get_public_key($remoteclient->wwwroot, $remoteclient->application); // If the key the remote peer is currently publishing is different to $certificate if($currkey != $certificate) { // Try and get the server's new key through trusted means - $MNET_REMOTE_CLIENT->refresh_key(); + $remoteclient->refresh_key(); // If we did manage to re-key, try to verify the signature again using the new public key. - $certificate = $MNET_REMOTE_CLIENT->public_key; + $certificate = $remoteclient->public_key; $signature_verified = openssl_verify($payload, $signature, $certificate); } } if ($signature_verified == 1) { - $MNET_REMOTE_CLIENT->signature_verified(); - $MNET_REMOTE_CLIENT->touch(); + $remoteclient->signature_verified(); + $remoteclient->touch(); } $sig_parser->free_resource(); @@ -156,7 +156,6 @@ function mnet_server_strip_signature($plaintextmessage) { * @return string $text The text of the error message */ function mnet_server_fault($code, $text, $param = null) { - global $MNET_REMOTE_CLIENT; if (!is_numeric($code)) { $code = 0; } @@ -179,7 +178,7 @@ function mnet_server_fault($code, $text, $param = null) { * @return string $text The XML text of the error message */ function mnet_server_fault_xml($code, $text, $privatekey = null) { - global $MNET_REMOTE_CLIENT, $CFG; + global $CFG; // Replace illegal XML chars - is this already in a lib somewhere? $text = str_replace(array('<','>','&','"',"'"), array('<','>','&','"','''), $text); @@ -218,14 +217,13 @@ function mnet_server_fault_xml($code, $text, $privatekey = null) { * @return string The encoded response string */ function mnet_server_prepare_response($response, $privatekey = null) { - global $MNET_REMOTE_CLIENT; - - if ($MNET_REMOTE_CLIENT->request_was_signed) { + $remoteclient = get_mnet_remote_client(); + if ($remoteclient->request_was_signed) { $response = mnet_sign_message($response, $privatekey); } - if ($MNET_REMOTE_CLIENT->request_was_encrypted) { - $response = mnet_encrypt_message($response, $MNET_REMOTE_CLIENT->public_key); + if ($remoteclient->request_was_encrypted) { + $response = mnet_encrypt_message($response, $remoteclient->public_key); } return $response; @@ -245,7 +243,8 @@ function mnet_server_prepare_response($response, $privatekey = null) { * @return No return val - just echo the response */ function mnet_server_dispatch($payload) { - global $CFG, $MNET_REMOTE_CLIENT, $DB; + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); // xmlrpc_decode_request returns an array of parameters, and the $method // variable (which is passed by reference) is instantiated with the value from // the methodName tag in the xml payload @@ -302,7 +301,7 @@ function mnet_server_dispatch($payload) { } if ($method == 'system.' . $m || $method == 'system/' . $m) { xmlrpc_server_register_method($xmlrpcserver, $method, $handler); - $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $MNET_REMOTE_CLIENT, array("encoding" => "utf-8")); + $response = xmlrpc_server_call_method($xmlrpcserver, $payload, $remoteclient, array("encoding" => "utf-8")); $response = mnet_server_prepare_response($response); echo $response; xmlrpc_server_destroy($xmlrpcserver); @@ -321,7 +320,7 @@ function mnet_server_dispatch($payload) { return; // if the rpc record isn't found, check to see if dangerous mode is on ////////////////////////////////////// DANGEROUS - } else if ('dangerous' == $CFG->mnet_dispatcher_mode && $MNET_REMOTE_CLIENT->plaintext_is_ok()) { + } else if ('dangerous' == $CFG->mnet_dispatcher_mode && $remoteclient->plaintext_is_ok()) { $functionname = array_pop($callstack); $filename = clean_param(implode('/',$callstack), PARAM_PATH); @@ -522,8 +521,9 @@ function mnet_server_invoke_dangerous_method($includefile, $methodname, $method, * @return string The XML-RPC response */ function mnet_keyswap($function, $params) { - global $CFG, $MNET; + global $CFG; $return = array(); + $mnet = get_mnet_environment(); if (!empty($CFG->mnet_register_allhosts)) { $mnet_peer = new mnet_peer(); @@ -533,7 +533,7 @@ function mnet_keyswap($function, $params) { $mnet_peer->commit(); } } - return $MNET->public_key; + return $mnet->public_key; } /** @@ -545,9 +545,10 @@ function mnet_keyswap($function, $params) { * @throws mnet_server_exception */ function mnet_verify_permissions($rpcrecord) { - global $CFG, $MNET_REMOTE_CLIENT, $DB; + global $CFG, $DB; + $remoteclient = get_mnet_remote_client(); - $id_list = $MNET_REMOTE_CLIENT->id; + $id_list = $remoteclient->id; if (!empty($CFG->mnet_all_hosts_id)) { $id_list .= ', '.$CFG->mnet_all_hosts_id; } @@ -572,7 +573,7 @@ function mnet_verify_permissions($rpcrecord) { } /** - * Figure out exactly what needs to be called and stashes it in $MNET_REMOTE_CLIENT + * Figure out exactly what needs to be called and stashes it in $remoteclient * Does some further verification that the method is callable * * @param string $method the full xmlrpc method that was called eg auth/mnet/auth.php/user_authorise @@ -582,7 +583,8 @@ function mnet_verify_permissions($rpcrecord) { * @throws mnet_server_exception */ function mnet_setup_dummy_method($method, $callstack, $rpcrecord) { - global $MNET_REMOTE_CLIENT, $CFG; + global $CFG; + $remoteclient = get_mnet_remote_client(); // verify that the callpath in the stack matches our records // callstack will look like array('mod', 'forum', 'lib.php', 'forum_add_instance'); $path = get_plugin_directory($rpcrecord->plugintype, $rpcrecord->pluginname, false); @@ -608,12 +610,12 @@ function mnet_setup_dummy_method($method, $callstack, $rpcrecord) { if (!is_callable(array($object, $rpcrecord->functionname))) { throw new mnet_server_exception(706, "nosuchfunction"); } - $MNET_REMOTE_CLIENT->object_to_call($object); + $remoteclient->object_to_call($object); } else { if (!is_callable(array($rpcrecord->classname, $rpcrecord->functionname))) { throw new mnet_server_exception(706, "nosuchfunction"); } - $MNET_REMOTE_CLIENT->static_location($rpcrecord->classname); + $remoteclient->static_location($rpcrecord->classname); } } } @@ -639,13 +641,12 @@ function mnet_setup_dummy_method($method, $callstack, $rpcrecord) { * function, whatever it may be. */ function mnet_server_dummy_method($methodname, $argsarray, $functionname) { - global $MNET_REMOTE_CLIENT; - + $remoteclient = get_mnet_remote_client(); try { - if (is_object($MNET_REMOTE_CLIENT->object_to_call)) { - return @call_user_func_array(array($MNET_REMOTE_CLIENT->object_to_call,$functionname), $argsarray); - } else if (!empty($MNET_REMOTE_CLIENT->static_location)) { - return @call_user_func_array(array($MNET_REMOTE_CLIENT->static_location, $functionname), $argsarray); + if (is_object($remoteclient->object_to_call)) { + return @call_user_func_array(array($remoteclient->object_to_call,$functionname), $argsarray); + } else if (!empty($remoteclient->static_location)) { + return @call_user_func_array(array($remoteclient->static_location, $functionname), $argsarray); } else { return @call_user_func_array($functionname, $argsarray); } diff --git a/mnet/xmlrpc/xmlparser.php b/mnet/xmlrpc/xmlparser.php index f3ad40988de..95ccd2a6547 100644 --- a/mnet/xmlrpc/xmlparser.php +++ b/mnet/xmlrpc/xmlparser.php @@ -83,13 +83,10 @@ class mnet_encxml_parser { * | | * |________________________________________________| * - * @uses $MNET * @param string $data The XML that you want to parse * @return bool True on success - false on failure */ function parse($data) { - global $MNET, $MNET_REMOTE_CLIENT; - $p = xml_parse($this->parser, $data); if ($p == 0) { diff --git a/portfolio/mahara/lib.php b/portfolio/mahara/lib.php index c21bdcf24bb..8fcf7fc0109 100644 --- a/portfolio/mahara/lib.php +++ b/portfolio/mahara/lib.php @@ -50,6 +50,14 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base { private $totalsize; // total size of all included files added together private $continueurl; // if we've been sent back a specific url to continue to (eg folder id) + protected function init() { + $this->mnet = get_mnet_environment(); + } + + public function __wakeup() { + $this->mnet = get_mnet_environment(); + } + public static function get_name() { return get_string('pluginname', 'portfolio_mahara'); } @@ -181,17 +189,8 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base { $this->set('file', $this->exporter->zip_tempfiles()); // this will throw a file_exception which the exporter catches separately. } - private function ensure_environment() { - global $MNET; - if (empty($MNET)) { - $MNET = new mnet_environment(); - $MNET->init(); - } // no idea why this happens :( - } - public function send_package() { global $CFG; - $this->ensure_environment(); // send the 'content_ready' request to mahara require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); $client = new mnet_xmlrpc_client(); @@ -247,7 +246,6 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base { return preg_replace('/wantsurl=[^&]*&/', 'wantsurl=' . urlencode($remoteurl) . '&', $sessions[$this->get_config('mnethostid')]); } $this->ensure_mnethost(); - $this->ensure_environment(); $mnetauth = get_auth_plugin('mnet'); if (!$url = $mnetauth->start_jump_session($this->get_config('mnethostid'), $remoteurl)) { return false; @@ -337,7 +335,8 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base { * @param string $token the token recieved previously during send_content_intent */ public static function fetch_file($token) { - global $DB, $MNET_REMOTE_CLIENT;; + global $DB; + $remoteclient = get_mnet_remote_client(); try { if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) { throw new mnet_server_exception(8009, get_string('mnet_notoken', 'portfolio_mahara')); @@ -346,7 +345,7 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base { } catch (portfolio_exception $e) { throw new mnet_server_exception(8010, get_string('mnet_noid', 'portfolio_mahara')); } - if ($exporter->get('instance')->get_config('mnethostid') != $MNET_REMOTE_CLIENT->id) { + if ($exporter->get('instance')->get_config('mnethostid') != $remoteclient->id) { throw new mnet_server_exception(8011, get_string('mnet_wronghost', 'portfolio_mahara')); } global $CFG; diff --git a/repository/mahara/repository.class.php b/repository/mahara/repository.class.php index 967721b94b2..03170019568 100644 --- a/repository/mahara/repository.class.php +++ b/repository/mahara/repository.class.php @@ -42,6 +42,7 @@ class repository_mahara extends repository { public function __construct($repositoryid, $context = SITEID, $options = array()) { global $SESSION, $action, $CFG; parent::__construct($repositoryid, $context, $options); + $this->mnet = get_mnet_environment(); } /** @@ -64,8 +65,6 @@ class repository_mahara extends repository { public function print_login($ajax = true) { global $SESSION, $CFG, $DB; //jump to the peer to create a session - require_once($CFG->dirroot . '/mnet/lib.php'); - $this->ensure_environment(); $mnetauth = get_auth_plugin('mnet'); $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //need to retrieve the host url @@ -91,18 +90,6 @@ class repository_mahara extends repository { return $this->get_listing('', '', $search_text); } - /** - * Set the MNET environment - * @global $MNET - */ - private function ensure_environment() { - global $MNET; - if (empty($MNET)) { - $MNET = new mnet_environment(); - $MNET->init(); - } - } - /** * Retrieve the file listing - file picker function * @global $CFG @@ -119,7 +106,6 @@ class repository_mahara extends repository { ///We also check that the "get file list" method has been activated (if it is not ///the method will not be returned by the system method system/listMethods) require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); - $this->ensure_environment(); ///check that the peer has been setup if (!array_key_exists('peer',$this->options)) { @@ -231,8 +217,6 @@ class repository_mahara extends repository { global $CFG, $DB, $USER; ///set mnet environment and set the mnet host - require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); - $this->ensure_environment(); $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url $mnet_peer = new mnet_peer(); $mnet_peer->set_wwwroot($host->wwwroot); diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index b861e0bc4fb..104754863cf 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -44,6 +44,7 @@ class repository_remotemoodle extends repository { public function __construct($repositoryid, $context = SITEID, $options = array()) { global $SESSION, $action, $CFG; parent::__construct($repositoryid, $context, $options); + $this->mnet = get_mnet_environment(); } /** @@ -51,19 +52,20 @@ class repository_remotemoodle extends repository { * The file is encoded in base64 * @global $DB * @global $USER - * @global $MNET_REMOTE_CLIENT * @param $username * @param $source * @return */ public function retrieveFile($username, $source) { - global $DB, $USER, $MNET_REMOTE_CLIENT; + global $DB, $USER; + + $remoteclient = get_mnet_remote_client(); ///check the the user is known ///he has to be previously connected to the server site in order to be in the database //TODO: MDL-21318 this looks problematic, because global $USER would need to be set back after this, // also is the user allowed to roam? - $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id)); + $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id)); if (empty($USER)) { throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username)); } @@ -93,20 +95,20 @@ class repository_remotemoodle extends repository { * Retrieve file list for a user of the Moodle client calling this function * @global $DB * @global $USER - * @global $MNET_REMOTE_CLIENT * @global $CFG * @param $username * @param $search * @return */ public function getFileList($username, $search) { - global $DB, $USER, $MNET_REMOTE_CLIENT, $CFG; + global $DB, $USER, $CFG; + $remoteclient = get_mnet_remote_client(); ///check the the user is known ///he has to be previously connected to the server site in order to be in the database //TODO: MDL-21318 this looks problematic, because global $USER would need to be set back after this, // also is the user allowed to roam? - $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id)); + $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id)); if (empty($USER)) { throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username)); } @@ -139,18 +141,6 @@ class repository_remotemoodle extends repository { return $this->get_listing('', '', $search_text); } - /** - * Set the MNET environment - * @global $MNET - */ - private function ensure_environment() { - global $MNET; - if (empty($MNET)) { - $MNET = new mnet_environment(); - $MNET->init(); - } - } - /** * Retrieve the file listing - file picker function * @global $CFG @@ -168,7 +158,6 @@ class repository_remotemoodle extends repository { ///We also check that this method has been activated (if it is not ///the method will not be returned by the system method system/listMethods) require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); - $this->ensure_environment(); ///check that the peer has been setup if (!array_key_exists('peer',$this->options)) { @@ -235,7 +224,6 @@ class repository_remotemoodle extends repository { ///set mnet environment and set the mnet host require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); - $this->ensure_environment(); $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url $mnet_peer = new mnet_peer(); $mnet_peer->set_wwwroot($host->wwwroot);