From: stronk7 Date: Fri, 16 May 2003 21:52:49 +0000 (+0000) Subject: Copy, expand and do preliminary checks from backup file X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b9846a9b9e75f01ec009c76204c88cc8b7cb1e32;p=moodle.git Copy, expand and do preliminary checks from backup file --- diff --git a/backup/restore.php b/backup/restore.php index 595d5f254a..a3e32033ae 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -19,8 +19,8 @@ require_once ("lib.php"); require_once ("restorelib.php"); - //Optional variables - optional_variable($id); // course id + //Optional + optional_variable($file); //Check login require_login(); @@ -32,89 +32,39 @@ //Check site if (!$site = get_site()) { - error("Site noto found!"); + error("Site not found!"); } //Check backup_version - if ($CFG->backup_version) { - if ($backup_version > $CFG->backup_version) { // upgrade - $a->oldversion = $CFG->backup_version; - $a->newversion = $backup_version; - $strdatabasechecking = get_string("databasechecking", "", $a); - $strdatabasesuccess = get_string("databasesuccess"); - print_header($strdatabasechecking, $strdatabasechecking, $strdatabasechecking); - print_heading($strdatabasechecking); - $db->debug=true; - if (backup_upgrade($a->oldversion)) { - $db->debug=false; - if (set_config("backup_version", $a->newversion)) { - notify($strdatabasesuccess, "green"); - notify("You are running Backup/Recovery version ".$backup_release,"black"); - print_continue("backup.php"); - die; - } else { - notify("Upgrade failed! (Could not update version in config table)"); - die; - } - } else { - $db->debug=false; - notify("Upgrade failed! See backup_version.php"); - die; - } - } else if ($backup_version < $CFG->backup_version) { - notify("WARNING!!! The code you are using is OLDER than the version that made these databases!"); - } - //Not exists. Starting installation - } else { - $strdatabaseupgrades = get_string("databaseupgrades"); - print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades); - - if (set_config("backup_version", "2003010100")) { - print_heading("You are currently going to install the needed structures to Backup/Recover"); - print_continue("backup.php"); - die; - } - } + upgrade_backup_db($backup_version,$backup_release,"restore.php"); //Get strings $strcourserestore = get_string("courserestore"); $stradministration = get_string("administration"); - //If no course has been selected or cancel button pressed - if (!$id or $cancel) { + //If no file has been selected from the FileManager, inform and end + if (!$file) { print_header("$site->shortname: $strcourserestore", $site->fullname, "admin/index.php\">$stradministration -> $strcourserestore"); - - if ($courses = get_courses()) { - print_heading(get_string("choosecourse")); - print_simple_box_start("CENTER"); - foreach ($courses as $course) { - echo "id\">$course->fullname ($course->shortname)
"; - } - print_simple_box_end(); - } else { - print_heading(get_string("nocoursesyet")); - print_continue("../$CFG->admin/index.php"); - } + print_heading(get_string("nofilesselected")); + print_continue("$moodle_home/$CFG->admin/index.php"); print_footer(); exit; } - //Get and check course - if (! $course = get_record("course", "id", $id)) { - error("Course ID was incorrect (can't find it)"); - } + //We are here, so me have a file. //Print header - print_header("$site->shortname: $strcoursebackup", $site->fullname, + print_header("$site->shortname: $strcourserestore", $site->fullname, "admin/index.php\">$stradministration -> - $strcoursebackup -> $course->fullname ($course->shortname)"); + $strcourserestore -> ".basename($file)); - //Print form - print_heading("$strcoursebackup: $course->fullname ($course->shortname)"); + //Print form + print_heading("$strcourserestore: ".basename($file)); print_simple_box_start("center", "", "$THEME->cellheading"); + //Call the form, depending the step we are if (!$launch) { - include_once("backup_form.html"); + include_once("restore_precheck.html"); } else if ($launch == "check") { include_once("backup_check.html"); } else if ($launch == "execute") { diff --git a/backup/restore_precheck.html b/backup/restore_precheck.html new file mode 100644 index 0000000000..1f72111546 --- /dev/null +++ b/backup/restore_precheck.html @@ -0,0 +1,76 @@ +dataroot."/".$file; + + //Start the main table + echo ""; + echo ""; + echo "
"; + + //Start the mail ul + echo "
    "; + + //Check the file exists + if (!is_file($file)) { + error ("File not exists ($file)"); + } + + //Check the file name ends with .zip + if (!substr($file,-4) == ".zip") { + error ("File has an incorrect extension"); + } + + //Now calculate the unique_code for this restore + $backup_unique_code = time(); + + //Now check and create the backup dir (if it doesn't exist) + $status = check_and_create_backup_dir($backup_unique_code); + //Empty dir + if ($status) { + echo "
  • Creating temp dir"; + $status = clear_backup_dir($backup_unique_code); + } + + //Now delete old data and directories under dataroot/temp/backup + if ($status) { + echo "
  • Cleaning old data"; + $status = backup_delete_old_data(); + } + + //Now copy he zip file to dataroot/temp/backup/backup_unique_code + if ($status) { + $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file)); + } + + //Now unzip the file + if ($status) { + echo "
  • Retrieving backup file"; + $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file),$moodle_home); + } + + //Now check for the moodle.xml file + if ($status) { + echo "
  • Checking backup file"; + $status = restore_check_moodle_file ($CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml"); + } + + //End the main ul + echo "
"; + + //End the main table + echo "
"; + + if (!$status) { + error ("An error has ocurred"); + } +?> diff --git a/backup/restorelib.php b/backup/restorelib.php index 6b811290dd..96272f6df2 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -1,2 +1,49 @@ unzip)) { // Use built-in php-based unzip function + include_once($moodle_home."/lib/pclzip/pclzip.lib.php"); + $archive = new PclZip($file); + if (!$list = $archive->extract(dirname($file))) { + $status = false; + } + } else { // Use external unzip program + $command = "cd ".dirname($file)."; $CFG->unzip -o ".basename($file); + Exec($command); + } + + return $status; + } + + //This function checks if moodle.xml seems to be a valid xml file + //(exists, has an xml header and a course main tag + function restore_check_moodle_file ($file) { + + $status = true; + + //Check if it exists + if ($status = is_file($file)) { + //Open it and read the first 200 bytes (chars) + $handle = fopen ($file, "r"); + $first_chars = fread($handle,200); + $status = fclose ($handle); + //Chek if it has the requires strings + if ($status) { + $status = strpos($first_chars,""); + if ($status !== false) { + $status = strpos($first_chars,""); + } + } + } + + return $status; + } ?>