From 9499230085ea1ad11a4d0b6675e58f44633377e3 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sat, 9 May 2009 01:32:41 +0000 Subject: [PATCH] MDL-16879 - fix mnet hosts. Tentative function. Not applied to upgrade yet. Please review. --- lib/db/upgradelib.php | 105 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 71bee5dfb6e30..1ebab19cee0ed 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -586,4 +586,109 @@ function upgrade_fix_category_depths() { $db->debug = true; } +/** + * This function will fix the status of the localhost/all records in the mnet_host table + * checking they exist and adding them if missing + redefine CFG->mnet_localhost_id and + * CFG->mnet_all_hosts_id if needed + update all the users having non-existent mnhostid + * to correct CFG->mnet_localhost_id + * + * Implemented because, at some point, specially in old installations upgraded along + * multiple versions, sometimes the stuff above has ended being inconsistent, causing + * problems here and there (noticeablely in backup/restore). MDL-16879 + */ +function upgrade_fix_incorrect_mnethostids() { + + global $CFG; + +/// Get current $CFG/mnet_host records + $old_mnet_localhost_id = !empty($CFG->mnet_localhost_id) ? $CFG->mnet_localhost_id : 0; + $old_mnet_all_hosts_id = !empty($CFG->mnet_all_hosts_id) ? $CFG->mnet_all_hosts_id : 0; + + $current_mnet_localhost_host = get_record('mnet_host', 'wwwroot', addslashes($CFG->wwwroot)); /// By wwwroot + $current_mnet_all_hosts_host = get_record_select('mnet_host', sql_isempty('mnet_host', 'wwwroot', false, false)); /// By empty wwwroot + +/// Create localhost_host if necessary (pretty improbable but better to be 100% in the safe side) +/// Code stolen from mnet_environment->init + if (!$current_mnet_localhost_host) { + $current_mnet_localhost_host = new stdClass(); + $current_mnet_localhost_host->wwwroot = $CFG->wwwroot; + $current_mnet_localhost_host->ip_address = ''; + $current_mnet_localhost_host->public_key = ''; + $current_mnet_localhost_host->public_key_expires = 0; + $current_mnet_localhost_host->last_connect_time = 0; + $current_mnet_localhost_host->last_log_id = 0; + $current_mnet_localhost_host->deleted = 0; + $current_mnet_localhost_host->name = ''; + /// Get the ip of the server + if (empty($_SERVER['SERVER_ADDR'])) { + /// SERVER_ADDR is only returned by Apache-like webservers + $count = preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $$current_mnet_localhost_host->wwwroot, $matches); + $my_hostname = $count > 0 ? $matches[1] : false; + $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH! + if ($my_ip == $my_hostname) { + $current_mnet_localhost_host->ip_address = 'UNKNOWN'; + } else { + $current_mnet_localhost_host->ip_address = $my_ip; + } + } else { + $current_mnet_localhost_host->ip_address = $_SERVER['SERVER_ADDR']; + } + $current_mnet_localhost_host->id = insert_record('mnet_host', $current_mnet_localhost_host, true); + } + +/// Create all_hosts_host if necessary (pretty improbable but better to be 100% in the safe side) +/// Code stolen from mnet_environment->init + if (!$current_mnet_all_hosts_host) { + $current_mnet_all_hosts_host = new stdClass(); + $current_mnet_all_hosts_host->wwwroot = ''; + $current_mnet_all_hosts_host->ip_address = ''; + $current_mnet_all_hosts_host->public_key = ''; + $current_mnet_all_hosts_host->public_key_expires = 0; + $current_mnet_all_hosts_host->last_connect_time = 0; + $current_mnet_all_hosts_host->last_log_id = 0; + $current_mnet_all_hosts_host->deleted = 0; + $current_mnet_all_hosts_host->name = 'All Hosts'; + $current_mnet_all_hosts_host->id = insert_record('mnet_host', $current_mnet_all_hosts_host, true); + } + +/// Compare old_mnet_localhost_id and current_mnet_localhost_host + + if ($old_mnet_localhost_id != $current_mnet_localhost_host->id) { /// Different = problems + /// Update $CFG->mnet_localhost_id to correct value + set_config('mnet_localhost_id', $current_mnet_localhost_host->id); + + /// Delete $old_mnet_localhost_id if exists (users will be assigned to new one below) + delete_records('mnet_host', 'id', $old_mnet_localhost_id); + } + +/// Compare old_mnet_all_hosts_id and current_mnet_all_hosts_host + + if ($old_mnet_all_hosts_id != $current_mnet_all_hosts_host->id) { /// Different = problems + /// Update $CFG->mnet_localhost_id to correct value + set_config('mnet_all_hosts_id', $current_mnet_all_hosts_host->id); + + /// Delete $old_mnet_all_hosts_id if exists + delete_records('mnet_host', 'id', $old_mnet_all_hosts_id); + } + +/// Finally, update all the incorrect user->mnethostid to the correct CFG->mnet_localhost_id, preventing UIX dupes + $hosts = get_records_menu('mnet_host', '', '', '', 'id, id AS id2'); + $hosts_str = implode(', ', $hosts); + + $sql = "SELECT id + FROM {$CFG->prefix}user u1 + WHERE u1.mnethostid NOT IN ($hosts_str) + AND NOT EXISTS ( + SELECT 'x' + FROM {$CFG->prefix}user u2 + WHERE u2.username = u1.username + AND u2.mnethostid = $current_mnet_localhost_host->id)"; + + $rs = get_recordset_sql($sql); + while ($rec = rs_fetch_next_record($rs)) { + set_field('user', 'mnethostid', $current_mnet_localhost_host->id, 'id', $rec->id); + } + rs_close($rs); +} + ?>