]> git.mjollnir.org Git - moodle.git/commitdiff
Copy, expand and do preliminary checks from backup file
authorstronk7 <stronk7>
Fri, 16 May 2003 21:52:49 +0000 (21:52 +0000)
committerstronk7 <stronk7>
Fri, 16 May 2003 21:52:49 +0000 (21:52 +0000)
backup/restore.php
backup/restore_precheck.html [new file with mode: 0644]
backup/restorelib.php

index 595d5f254aec5f4b24ac864f808873727b79aa6f..a3e32033ae54be674b9972cc9cf3cb924bdcea0b 100644 (file)
@@ -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();
 
     //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,
                      "<A HREF=\"$moodle_home/$CFG->admin/index.php\">$stradministration</A> -> $strcourserestore");
-
-        if ($courses = get_courses()) {
-            print_heading(get_string("choosecourse"));
-            print_simple_box_start("CENTER");
-            foreach ($courses as $course) {
-            echo "<A HREF=\"backup.php?id=$course->id\">$course->fullname ($course->shortname)</A><BR>";
-            }
-            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,
                  "<A HREF=\"$moodle_home/$CFG->admin/index.php\">$stradministration</A> ->
-                  <A HREF=\"backup.php\">$strcoursebackup</A> -> $course->fullname ($course->shortname)");
+                  <A HREF=\"backup.php\">$strcourserestore</A> -> ".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 (file)
index 0000000..1f72111
--- /dev/null
@@ -0,0 +1,76 @@
+<?PHP  // $Id$
+    //This page copies th zip to the temp directory,
+    //unzip it, check that it is a valid backup file
+    //inform about its contents and fill all the necesary
+    //variables to continue with the restore.
+
+    //Checks we have the file variable
+    if (!isset($file)) {         
+        error ("File not specified");
+    }
+
+    //Prepend dataroot to variable to have the absolute path
+    $file = $CFG->dataroot."/".$file;
+
+    //Start the main table
+    echo "<table cellpadding=5>";
+    echo "<tr><td>";
+    
+    //Start the mail ul
+    echo "<ul>";
+
+    //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 "<li>Creating temp dir";
+        $status = clear_backup_dir($backup_unique_code);
+    }
+
+    //Now delete old data and directories under dataroot/temp/backup
+    if ($status) {   
+        echo "<li>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 "<li>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 "<li>Checking backup file";
+        $status = restore_check_moodle_file ($CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml");
+    }
+
+    //End the main ul
+    echo "</ul>";
+
+    //End the main table
+    echo "</tr></td>";
+    echo "</table>";
+
+    if (!$status) {
+        error ("An error has ocurred");
+    }
+?>
index 6b811290dd2ff77a27bf3016d4e65c7011a6fd26..96272f6df2960abfac18570e7b491934f593fe9c 100644 (file)
@@ -1,2 +1,49 @@
 <?PHP //$Id$
+    //Functions used in restore
+   
+    //This function unzips a zip file in the samen directory that it is
+    //It automatically uses pclzip or command line unzip
+    function restore_unzip ($file,$moodle_home) {
+        
+        global $CFG;
+
+        $status = true;
+
+        if (empty($CFG->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,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+                if ($status !== false) {
+                    $status = strpos($first_chars,"<MOODLE_BACKUP>");
+                }
+            }
+        }   
+
+        return $status;  
+    }   
 ?>