Skip to content

Commit

Permalink
mnet MDL-21276 get rid of all the $MNET* globals
Browse files Browse the repository at this point in the history
  • Loading branch information
Penny Leach committed Feb 2, 2010
1 parent 0d4b9d0 commit 287efec
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 179 deletions.
17 changes: 9 additions & 8 deletions admin/mnet/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -60,21 +61,21 @@
$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.
print_error ('deleteoutoftime', 'mnet', 'index.php');
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;
}
Expand All @@ -94,11 +95,11 @@
</tr>
<tr valign="top">
<td align="right"><?php print_string('publickey', 'mnet'); ?>:</td>
<td><pre><?php echo $MNET->public_key; ?></pre></td>
<td><pre><?php echo $mnet->public_key; ?></pre></td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('expires', 'mnet'); ?>:</td>
<td><?php echo userdate($MNET->public_key_expires); ?></td>
<td><?php echo userdate($mnet->public_key_expires); ?></td>
</tr>
</table>
</td>
Expand Down
66 changes: 36 additions & 30 deletions auth/mnet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -52,7 +53,8 @@ function user_login($username, $password) {
* @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));
Expand Down Expand Up @@ -87,7 +89,7 @@ function user_authorise($token, $useragent) {
$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)) {
Expand Down Expand Up @@ -121,7 +123,7 @@ function user_authorise($token, $useragent) {
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);
}
Expand All @@ -146,7 +148,7 @@ function generate_token() {
* 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
Expand Down Expand Up @@ -199,7 +201,7 @@ function start_jump_session($mnethostid, $wantsurl, $wantsurlbackhere=false) {
// 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';
}
Expand Down Expand Up @@ -228,7 +230,7 @@ function update_session_id() {
* @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
Expand Down Expand Up @@ -462,20 +464,21 @@ function confirm_mnet_session($token, $remotewwwroot) {
* @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;
}
// 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;
}

Expand Down Expand Up @@ -510,13 +513,13 @@ function update_enrolments($username, $courses) {
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?
Expand Down Expand Up @@ -560,7 +563,7 @@ function update_enrolments($username, $courses) {
// 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);
Expand All @@ -570,7 +573,7 @@ function update_enrolments($username, $courses) {
// 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() {
Expand Down Expand Up @@ -704,7 +707,7 @@ function process_config($config) {
* @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

Expand Down Expand Up @@ -849,7 +852,8 @@ function keepalive_client() {
* @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();
Expand All @@ -860,7 +864,7 @@ function refresh_log($array) {

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];
Expand All @@ -878,12 +882,12 @@ function refresh_log($array) {
$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();
Expand All @@ -900,7 +904,8 @@ function refresh_log($array) {
* @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;

Expand Down Expand Up @@ -931,8 +936,8 @@ function keepalive_server($array) {

$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);
}

/**
Expand Down Expand Up @@ -963,15 +968,14 @@ function cron() {
* @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:
Expand Down Expand Up @@ -1036,7 +1040,8 @@ function kill_parent($username, $useragent) {
* @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));
Expand All @@ -1054,7 +1059,7 @@ function kill_children($username, $useragent) {
// 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";
Expand All @@ -1077,7 +1082,7 @@ function kill_children($username, $useragent) {
$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;
Expand All @@ -1093,8 +1098,9 @@ function kill_children($username, $useragent) {
* @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;
Expand Down
Loading

0 comments on commit 287efec

Please sign in to comment.