backup/restore: MDL-16614 more reliable test for when we are restoring a backup that was made of the same site that we are restoring to.

* Use this to fix a question restore bug.
* Replace older code that does something similar with the new test.
* Refactor initialisation of $CFG->siteidentifier into a function. There were about 4 copies of this code ;-)
This commit is contained in:
tjhunt
2009-01-20 03:16:05 +00:00
parent 6592bac242
commit 1ae3f411b5
10 changed files with 83 additions and 25 deletions
+2 -4
View File
@@ -553,10 +553,8 @@
set_config('adminblocks_initialised', 1);
}
/// Define the unique site ID code if it isn't already
if (empty($CFG->siteidentifier)) { // Unique site identification code
set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']);
}
/// Define the unique site ID code if it isn't already set. This getter does that as a side-effect.
get_site_identifier();
/// Check if the guest user exists. If not, create one.
if (! record_exists("user", "username", "guest")) {
+1 -7
View File
@@ -23,13 +23,7 @@
$admin->country = $CFG->country;
}
if (empty($CFG->siteidentifier)) { // Unique site identification code
set_config('siteidentifier', random_string(32).$_SERVER['HTTP_HOST']);
}
/// Print headings
$stradministration = get_string("administration");
$strregistration = get_string("registration");
$strregistrationinfo = get_string("registrationinfo");
@@ -54,7 +48,7 @@
echo "<!-- Together they form a key. If any of these change between updates then the entry -->\n";
echo "<!-- is flagged as a new entry and will be manually checked by the list maintainer -->\n";
echo "<input type=\"hidden\" name=\"url\" value=\"$CFG->wwwroot\" />\n";
echo "<input type=\"hidden\" name=\"secret\" value=\"$CFG->siteidentifier\" />\n";
echo "<input type=\"hidden\" name=\"secret\" value=\"" . get_site_identifier() . "\" />\n";
echo "<input type=\"hidden\" name=\"host\" value=\"".$_SERVER["HTTP_HOST"]."\" />\n";
echo "<input type=\"hidden\" name=\"lang\" value=\"".current_language()."\" />\n";
+2
View File
@@ -483,6 +483,8 @@
fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
//The original site wwwroot
fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot));
//The original site identifier. MD5 hashed for security.
fwrite ($bf,full_tag("ORIGINAL_SITE_IDENTIFIER_HASH",2,false,md5(get_site_identifier())));
//The zip method used
if (!empty($CFG->zip)) {
$zipmethod = 'external';
+19
View File
@@ -435,6 +435,25 @@
upgrade_log_finish();
}
/**
* Are we restoring a backup that was made on the same site that we are restoring to?
* This relies on some information that was only added to backup files in January 2009.
* For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains
* when this guess could give the wrong answer.
* @return boolean true if the backup was made on the same site we are restoring to.
*/
function backup_is_same_site(&$restore) {
global $CFG;
static $hashedsiteid = null;
if (is_null($hashedsiteid)) {
$hashedsiteid = md5(get_site_identifier());
}
if (!empty($restore->original_siteidentifier)) {
return $restore->original_siteidentifier == $hashedsiteid;
} else {
return $restore->original_wwwroot == $CFG->wwwroot;
}
}
//This function is used to insert records in the backup_ids table
//If the info field is greater than max_db_storage, then its info
+1 -1
View File
@@ -291,7 +291,7 @@
/// If restoring users and backup has mnet remote users and we are restoring to different site, forbid restore to non-admins. MDL-17009
if ($restore->users != 2 && /// If restoring users
!empty($info->mnet_remoteusers) && $info->mnet_remoteusers === 'true' && /// and backup contains remote users
$info->original_wwwroot !== $CFG->wwwroot) { /// and backup is being restored to different site
!backup_is_same_site($info)) { /// and backup is being restored to different site
/// If user is admin (by 'moodle/user:create' cap), warn about conversion to local auth if missing mnet hosts and continue restore
if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM))) {
+4
View File
@@ -13,6 +13,10 @@
//Add info->original_wwwroot to $restore to be able to use it in all the restore process
//(mainly when decoding internal links)
$restore->original_wwwroot = $info->original_wwwroot;
// Copy $info->original_siteidentifier, is present, so backup_is_same_site can work.
if (isset($info->original_siteidentifier)) {
$restore->original_siteidentifier = $info->original_siteidentifier;
}
//Add info->backup_version to $restore to be able to detect versions in the restore process
//(to decide when to convert wiki texts to markdown...)
$restore->backup_version = $info->backup_backup_version;
+17 -5
View File
@@ -486,6 +486,15 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
//The backup date
$tab[3][0] = "<b>".get_string("backupdate").":</b>";
$tab[3][1] = userdate($info->backup_date);
//Is this the same Moodle install?
if (!empty($info->original_siteidentifier)) {
$tab[4][0] = "<b>".get_string("backupfromthissite").":</b>";
if (backup_is_same_site($info)) {
$tab[4][1] = get_string('yes');
} else {
$tab[4][1] = get_string('no');
}
}
//Print title
print_heading(get_string("backup").":");
$table->data = $tab;
@@ -1260,7 +1269,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
$dbmetacourse = false;
//Check if child course exists in destination server
//(by id in the same server or by idnumber and shortname in other server)
if ($restore->original_wwwroot == $CFG->wwwroot) {
if (backup_is_same_site($restore)) {
//Same server, lets see by id
$dbcourse = get_record('course','id',$child->id);
} else {
@@ -1292,7 +1301,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
$dbmetacourse = false;
//Check if parent course exists in destination server
//(by id in the same server or by idnumber and shortname in other server)
if ($restore->original_wwwroot == $CFG->wwwroot) {
if (backup_is_same_site($restore)) {
//Same server, lets see by id
$dbcourse = get_record('course','id',$parent->id);
} else {
@@ -2562,7 +2571,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
// - if the destination site is different (by wwwroot) reset it.
// - if the destination site is the same (by wwwroot), leave it unmodified
if ($restore->original_wwwroot != $CFG->wwwroot) {
if (!backup_is_same_site($restore)) {
$user->policyagreed = 0;
} else {
//Nothing to do, we are in the same server
@@ -2627,7 +2636,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
// - if we are in the same server (by wwwroot), maintain it unmodified.
if (empty($user->roles['teacher']->enrol)) {
$user->roles['teacher']->enrol = $CFG->enrol;
} else if ($restore->original_wwwroot != $CFG->wwwroot) {
} else if (!backup_is_same_site($restore)) {
$user->roles['teacher']->enrol = $CFG->enrol;
} else {
//Nothing to do. Leave it unmodified
@@ -2673,7 +2682,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
// - if we are in the same server (by wwwroot), maintain it unmodified.
if (empty($user->roles['student']->enrol)) {
$user->roles['student']->enrol = $CFG->enrol;
} else if ($restore->original_wwwroot != $CFG->wwwroot) {
} else if (!backup_is_same_site($restore)) {
$user->roles['student']->enrol = $CFG->enrol;
} else {
//Nothing to do. Leave it unmodified
@@ -4966,6 +4975,9 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
case "ORIGINAL_WWWROOT":
$this->info->original_wwwroot = $this->getContents();
break;
case "ORIGINAL_SITE_IDENTIFIER_HASH":
$this->info->original_siteidentifier = $this->getContents();
break;
case "MNET_REMOTEUSERS":
$this->info->mnet_remoteusers = $this->getContents();
break;
+1
View File
@@ -149,6 +149,7 @@ $string['backupexecuteathelp'] = 'Choose what time automated backups should run
$string['backupfailed'] = 'Some of your courses weren\'t saved!!';
$string['backupfilename'] = 'backup';
$string['backupfinished'] = 'Backup completed successfully';
$string['backupfromthissite'] = 'Backup was made on this site?';
$string['backupgradebookhistoryhelp'] = 'If enabled then gradebook history will be included in automated backups. Note that grade history must not be disabled in server settings (disablegradehistory) in order for this to work';
$string['backupincludemoduleshelp'] = 'Choose whether you want to include course modules, with or without user data, in automated backups';
$string['backupincludemoduleuserdatahelp'] = 'Choose whether you want to include module user data in automated backups.';
+14 -6
View File
@@ -3939,15 +3939,10 @@ function reset_course_userdata($data) {
function generate_email_processing_address($modid,$modargs) {
global $CFG;
if (empty($CFG->siteidentifier)) { // Unique site identification code
set_config('siteidentifier', random_string(32));
}
$header = $CFG->mailprefix . substr(base64_encode(pack('C',$modid)),0,2).$modargs;
return $header . substr(md5($header.$CFG->siteidentifier),0,16).'@'.$CFG->maildomain;
return $header . substr(md5($header.get_site_identifier()),0,16).'@'.$CFG->maildomain;
}
function moodle_process_email($modargs,$body) {
// the first char should be an unencoded letter. We'll take this as an action
switch ($modargs{0}) {
@@ -8303,5 +8298,18 @@ function is_primary_admin($userid){
}
}
/**
* @return string $CFG->siteidentifier, first making sure it is properly initialised.
*/
function get_site_identifier() {
global $CFG;
// Check to see if it is missing. If so, initialise it.
if (empty($CFG->siteidentifier)) {
set_config('siteidentifier', random_string(32) . $_SERVER['HTTP_HOST']);
}
// Return it.
return $CFG->siteidentifier;
}
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
?>
+22 -2
View File
@@ -334,8 +334,28 @@
$question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']);
$question->timecreated = backup_todb_optional_field($que_info, 'TIMECREATED', 0);
$question->timemodified = backup_todb_optional_field($que_info, 'TIMEMODIFIED', 0);
$question->createdby = backup_todb_optional_field($que_info, 'CREATEDBY', null);
$question->modifiedby = backup_todb_optional_field($que_info, 'MODIFIEDBY', null);
// Set the createdby field, if the user was in the backup, or if we are on the same site.
$createdby = backup_todb_optional_field($que_info, 'CREATEDBY', null);
if (!empty($createdby)) {
$user = backup_getid($restore->backup_unique_code, 'user', $createdby);
if ($user) {
$question->createdby = $user->new_id;
} else if (backup_is_same_site($restore)) {
$question->createdby = $createdby;
}
}
// Set the modifiedby field, if the user was in the backup, or if we are on the same site.
$modifiedby = backup_todb_optional_field($que_info, 'MODIFIEDBY', null);
if (!empty($createdby)) {
$user = backup_getid($restore->backup_unique_code, 'user', $modifiedby);
if ($user) {
$question->modifiedby = $user->new_id;
} else if (backup_is_same_site($restore)) {
$question->modifiedby = $modifiedby;
}
}
if ($restore->backup_version < 2006032200) {
// The qtype was an integer that now needs to be converted to the name