MDL-23974 dom and xml extensions are now required - backported from HAED
[moodle.git] / backup / lib.php
blob92987472f9a6b64b7fbbc7abbf127704bb7117ce
1 <?php //$Id$
2 //This file contains all the general function needed (file manipulation...)
3 //not directly part of the backup/restore utility plus some constants
5 // Define "restoreto" options
6 define('RESTORETO_CURRENT_DELETING', 0);
7 define('RESTORETO_CURRENT_ADDING', 1);
8 define('RESTORETO_NEW_COURSE', 2);
9 define('RESTORETO_EXISTING_DELETING', 3);
10 define('RESTORETO_EXISTING_ADDING', 4);
12 require_once($CFG->dirroot.'/lib/uploadlib.php');
14 //Sets a name/value pair in backup_config table
15 function backup_set_config($name, $value) {
16 if (get_field("backup_config", "name", "name", $name)) {
17 return set_field("backup_config", "value", addslashes($value), "name", $name);
18 } else {
19 $config = new object();
20 $config->name = $name;
21 $config->value = addslashes($value);
22 return insert_record("backup_config", $config);
26 //Gets all the information from backup_config table
27 function backup_get_config() {
28 $backup_config = null;
29 if ($configs = get_records("backup_config")) {
30 foreach ($configs as $config) {
31 $backup_config[$config->name] = $config->value;
34 return (object)$backup_config;
37 //Delete old data in backup tables (if exists)
38 //Four hours seem to be appropiate now that backup is stable
39 function backup_delete_old_data() {
41 global $CFG;
43 //Change this if you want !!
44 $hours = 4;
45 //End change this
46 $seconds = $hours * 60 * 60;
47 $delete_from = time()-$seconds;
48 //Now delete from tables
49 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
50 WHERE backup_code < '$delete_from'",false);
51 if ($status) {
52 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
53 WHERE backup_code < '$delete_from'",false);
55 //Now, delete old directory (if exists)
56 if ($status) {
57 $status = backup_delete_old_dirs($delete_from);
59 return($status);
62 //Function to delete dirs/files into temp/backup directory
63 //older than $delete_from
64 function backup_delete_old_dirs($delete_from) {
66 global $CFG;
68 $status = true;
69 //Get files and directories in the temp backup dir witout descend
70 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false, true, true);
71 foreach ($list as $file) {
72 $file_path = $CFG->dataroot."/temp/backup/".$file;
73 $moddate = filemtime($file_path);
74 if ($status && $moddate < $delete_from) {
75 //If directory, recurse
76 if (is_dir($file_path)) {
77 $status = delete_dir_contents($file_path);
78 //There is nothing, delete the directory itself
79 if ($status) {
80 $status = rmdir($file_path);
82 //If file
83 } else {
84 unlink("$file_path");
89 return $status;
92 //Function to check and create the needed dir to
93 //save all the backup
94 function check_and_create_backup_dir($backup_unique_code) {
96 global $CFG;
98 $status = check_dir_exists($CFG->dataroot."/temp",true);
99 if ($status) {
100 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
102 if ($status) {
103 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
106 return $status;
109 //Function to delete all the directory contents recursively
110 //it supports a excluded dit too
111 //Copied from the web !!
112 function delete_dir_contents ($dir,$excludeddir="") {
114 if (!is_dir($dir)) {
115 // if we've been given a directory that doesn't exist yet, return true.
116 // this happens when we're trying to clear out a course that has only just
117 // been created.
118 return true;
120 $slash = "/";
122 // Create arrays to store files and directories
123 $dir_files = array();
124 $dir_subdirs = array();
126 // Make sure we can delete it
127 chmod($dir, 0777);
129 if ((($handle = opendir($dir))) == FALSE) {
130 // The directory could not be opened
131 return false;
134 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
135 while(false !== ($entry = readdir($handle))) {
136 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
137 $dir_subdirs[] = $dir. $slash .$entry;
139 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
140 $dir_files[] = $dir. $slash .$entry;
144 // Delete all files in the curent directory return false and halt if a file cannot be removed
145 for($i=0; $i<count($dir_files); $i++) {
146 chmod($dir_files[$i], 0777);
147 if (((unlink($dir_files[$i]))) == FALSE) {
148 return false;
152 // Empty sub directories and then remove the directory
153 for($i=0; $i<count($dir_subdirs); $i++) {
154 chmod($dir_subdirs[$i], 0777);
155 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
156 return false;
158 else {
159 if (remove_dir($dir_subdirs[$i]) == FALSE) {
160 return false;
165 // Close directory
166 closedir($handle);
168 // Success, every thing is gone return true
169 return true;
172 //Function to clear (empty) the contents of the backup_dir
173 function clear_backup_dir($backup_unique_code) {
175 global $CFG;
177 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
179 //Delete recursively
180 $status = delete_dir_contents($rootdir);
182 return $status;
185 //Returns the module type of a course_module's id in a course
186 function get_module_type ($courseid,$moduleid) {
188 global $CFG;
190 $results = get_records_sql ("SELECT cm.id, m.name
191 FROM {$CFG->prefix}course_modules cm,
192 {$CFG->prefix}modules m
193 WHERE cm.course = '$courseid' AND
194 cm.id = '$moduleid' AND
195 m.id = cm.module");
197 if ($results) {
198 $name = $results[$moduleid]->name;
199 } else {
200 $name = false;
202 return $name;
205 //This function return the names of all directories under a give directory
206 //Not recursive
207 function list_directories ($rootdir) {
209 $results = null;
211 $dir = opendir($rootdir);
212 while (false !== ($file=readdir($dir))) {
213 if ($file=="." || $file=="..") {
214 continue;
216 if (is_dir($rootdir."/".$file)) {
217 $results[$file] = $file;
220 closedir($dir);
221 return $results;
224 //This function return the names of all directories and files under a give directory
225 //Not recursive
226 function list_directories_and_files ($rootdir) {
228 $results = "";
230 $dir = opendir($rootdir);
231 while (false !== ($file=readdir($dir))) {
232 if ($file=="." || $file=="..") {
233 continue;
235 $results[$file] = $file;
237 closedir($dir);
238 return $results;
241 //This function clean data from backup tables and
242 //delete all temp files used
243 function clean_temp_data ($preferences) {
245 global $CFG;
247 $status = true;
249 //true->do it, false->don't do it. To debug if necessary.
250 if (true) {
251 //Now delete from tables
252 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
253 WHERE backup_code = '$preferences->backup_unique_code'",false);
254 if ($status) {
255 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
256 WHERE backup_code = '$preferences->backup_unique_code'",false);
258 //Now, delete temp directory (if exists)
259 $file_path = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
260 if (is_dir($file_path)) {
261 $status = delete_dir_contents($file_path);
262 //There is nothing, delete the directory itself
263 if ($status) {
264 $status = rmdir($file_path);
268 return $status;
271 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
272 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
273 //This functions are used to copy any file or directory ($from_file)
274 //to a new file or directory ($to_file). It works recursively and
275 //mantains file perms.
276 //I've copied it from: http://www.php.net/manual/en/function.copy.php
277 //Little modifications done
279 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
281 global $CFG;
283 if (is_file($from_file)) {
284 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
285 //$perms=fileperms($from_file);
286 //return copy($from_file,$to_file) && chmod($to_file,$perms);
287 umask(0000);
288 if (copy($from_file,$to_file)) {
289 chmod($to_file,$CFG->directorypermissions);
290 if (!empty($log_clam)) {
291 clam_log_upload($to_file,null,true);
293 return true;
295 return false;
297 else if (is_dir($from_file)) {
298 return backup_copy_dir($from_file,$to_file);
300 else{
301 //echo "<br />Error: not file or dir ".$from_file; //Debug
302 return false;
306 function backup_copy_dir($from_file,$to_file) {
308 global $CFG;
310 $status = true; // Initialize this, next code will change its value if needed
312 if (!is_dir($to_file)) {
313 //echo "<br />Creating ".$to_file; //Debug
314 umask(0000);
315 $status = mkdir($to_file,$CFG->directorypermissions);
317 $dir = opendir($from_file);
318 while (false !== ($file=readdir($dir))) {
319 if ($file=="." || $file=="..") {
320 continue;
322 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
324 closedir($dir);
325 return $status;
327 ///Ends copy file/dirs functions
328 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
329 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
332 function upgrade_backup_db($continueto) {
333 /// This function upgrades the backup tables, if necessary
334 /// It's called from admin/index.php, also backup.php and restore.php
336 global $CFG, $db;
338 require_once ("$CFG->dirroot/backup/version.php"); // Get code versions
340 if (empty($CFG->backup_version)) { // Backup has never been installed.
341 $strdatabaseupgrades = get_string("databaseupgrades");
342 $navlinks = array();
343 $navlinks[] = array('name' => $strdatabaseupgrades, 'link' => null, 'type' => 'misc');
344 $navigation = build_navigation($navlinks);
346 print_header($strdatabaseupgrades, $strdatabaseupgrades, $navigation, "",
347 upgrade_get_javascript(), false, "&nbsp;", "&nbsp;");
349 upgrade_log_start();
350 print_heading('backup');
351 $db->debug=true;
353 /// Both old .sql files and new install.xml are supported
354 /// but we priorize install.xml (XMLDB) if present
355 $status = false;
356 if (file_exists($CFG->dirroot . '/backup/db/install.xml')) {
357 $status = install_from_xmldb_file($CFG->dirroot . '/backup/db/install.xml'); //New method
358 } else if (file_exists($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.sql')) {
359 $status = modify_database($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.sql'); //Old method
362 $db->debug = false;
363 if ($status) {
364 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
365 notify(get_string("databasesuccess"), "green");
366 notify(get_string("databaseupgradebackups", "", $backup_version), "green");
367 print_continue($continueto);
368 print_footer('none');
369 exit;
370 } else {
371 error("Upgrade of backup system failed! (Could not update version in config table)");
373 } else {
374 error("Backup tables could NOT be set up successfully!");
378 /// Upgrading code starts here
379 $oldupgrade = false;
380 $newupgrade = false;
381 if (is_readable($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.php')) {
382 include_once($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
383 $oldupgrade = true;
385 if (is_readable($CFG->dirroot . '/backup/db/upgrade.php')) {
386 include_once($CFG->dirroot . '/backup/db/upgrade.php'); // defines new upgrading function
387 $newupgrade = true;
390 if ($backup_version > $CFG->backup_version) { // Upgrade tables
391 $strdatabaseupgrades = get_string("databaseupgrades");
392 $navigation = array(array('name' => $strdatabaseupgrades, 'link' => null, 'type' => 'misc'));
393 print_header($strdatabaseupgrades, $strdatabaseupgrades, build_navigation($navigation), '', upgrade_get_javascript());
395 upgrade_log_start();
396 print_heading('backup');
398 /// Run de old and new upgrade functions for the module
399 $oldupgrade_function = 'backup_upgrade';
400 $newupgrade_function = 'xmldb_backup_upgrade';
402 /// First, the old function if exists
403 $oldupgrade_status = true;
404 if ($oldupgrade && function_exists($oldupgrade_function)) {
405 $db->debug = true;
406 $oldupgrade_status = $oldupgrade_function($CFG->backup_version);
407 } else if ($oldupgrade) {
408 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
409 '/backup/db/' . $CFG->dbtype . '.php');
412 /// Then, the new function if exists and the old one was ok
413 $newupgrade_status = true;
414 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
415 $db->debug = true;
416 $newupgrade_status = $newupgrade_function($CFG->backup_version);
417 } else if ($newupgrade) {
418 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
419 '/backup/db/upgrade.php');
422 $db->debug=false;
423 /// Now analyze upgrade results
424 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
425 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
426 notify(get_string("databasesuccess"), "green");
427 notify(get_string("databaseupgradebackups", "", $backup_version), "green");
428 print_continue($continueto);
429 print_footer('none');
430 exit;
431 } else {
432 error("Upgrade of backup system failed! (Could not update version in config table)");
434 } else {
435 error("Upgrade failed! See backup/version.php");
438 } else if ($backup_version < $CFG->backup_version) {
439 upgrade_log_start();
440 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
442 upgrade_log_finish();
446 * Are we restoring a backup that was made on the same site that we are restoring to?
447 * This relies on some information that was only added to backup files in January 2009.
448 * For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains
449 * when this guess could give the wrong answer.
450 * @return boolean true if the backup was made on the same site we are restoring to.
452 function backup_is_same_site(&$restore) {
453 global $CFG;
454 static $hashedsiteid = null;
455 if (is_null($hashedsiteid)) {
456 $hashedsiteid = md5(get_site_identifier());
458 if (!empty($restore->original_siteidentifier)) {
459 return $restore->original_siteidentifier == $hashedsiteid;
460 } else {
461 return $restore->original_wwwroot == $CFG->wwwroot;
465 //This function is used to insert records in the backup_ids table
466 //If the info field is greater than max_db_storage, then its info
467 //is saved to filesystem
468 function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
470 global $CFG;
472 $max_db_storage = 128; //Max bytes to save to db, else save to file
474 $status = true;
476 //First delete to avoid PK duplicates
477 $status = backup_delid($backup_unique_code, $table, $old_id);
479 //Now, serialize info
480 $info_ser = serialize($info);
482 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
483 //insert a "infile" in the info field
485 if (strlen($info_ser) > $max_db_storage) {
486 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
487 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
488 //Save data to file
489 $status = backup_data2file($filename,$info_ser);
490 //Set info_to save
491 $info_to_save = "infile";
492 } else {
493 //Saving to db, addslashes
494 $info_to_save = addslashes($info_ser);
497 //Now, insert the record
498 if ($status) {
499 //Build the record
500 $rec = new object();
501 $rec->backup_code = $backup_unique_code;
502 $rec->table_name = $table;
503 $rec->old_id = $old_id;
504 $rec->new_id = ($new_id === null? 0 : $new_id);
505 $rec->info = $info_to_save;
507 if (!insert_record('backup_ids', $rec, false)) {
508 $status = false;
511 return $status;
514 //This function is used to delete recods from the backup_ids table
515 //If the info field is "infile" then the file is deleted too
516 function backup_delid ($backup_unique_code, $table, $old_id) {
518 global $CFG;
520 $status = true;
522 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
523 WHERE backup_code = $backup_unique_code AND
524 table_name = '$table' AND
525 old_id = '$old_id'",false);
526 return $status;
529 //This function is used to get a record from the backup_ids table
530 //If the info field is "infile" then its info
531 //is read from filesystem
532 function backup_getid ($backup_unique_code, $table, $old_id) {
534 global $CFG;
536 $status = true;
537 $status2 = true;
539 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
540 "table_name",$table,
541 "old_id", $old_id);
543 //If info field = "infile", get file contents
544 if (!empty($status->info) && $status->info == "infile") {
545 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
546 //Read data from file
547 $status2 = backup_file2data($filename,$info);
548 if ($status2) {
549 //unserialize data
550 $status->info = unserialize($info);
551 } else {
552 $status = false;
554 } else {
555 //Only if status (record exists)
556 if (!empty($status->info)) {
557 if ($status->info === 'needed') {
558 // TODO: ugly hack - fix before 1.9.1
559 debugging('Incorrect string "needed" in $status->info, please fix the code (table:'.$table.'; old_id:'.$old_id.').', DEBUG_DEVELOPER);
560 } else {
561 ////First strip slashes
562 $temp = stripslashes($status->info);
563 //Now unserialize
564 $status->info = unserialize($temp);
569 return $status;
572 //This function is used to add slashes (and decode from UTF-8 if needed)
573 //It's used intensivelly when restoring modules and saving them in db
574 function backup_todb ($data, $addslashes=true) {
575 // MDL-10770
576 if ($data === '$@NULL@$') {
577 return null;
578 } else {
579 if ($addslashes) {
580 $data = addslashes($data);
582 return restore_decode_absolute_links($data);
586 //This function is used to check that every necessary function to
587 //backup/restore exists in the current php installation. Thanks to
588 //gregb@crowncollege.edu by the idea.
589 function backup_required_functions($justcheck=false) {
591 if(!function_exists('utf8_encode')) {
592 if (empty($justcheck)) {
593 error('You need to add XML support to your PHP installation');
594 } else {
595 return false;
599 return true;
602 //This function send n white characters to the browser and flush the
603 //output buffer. Used to avoid browser timeouts and to show the progress.
604 function backup_flush($n=0,$time=false) {
605 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
606 return;
608 if ($time) {
609 $ti = strftime("%X",time());
610 } else {
611 $ti = "";
613 echo str_repeat(" ", $n) . $ti . "\n";
614 flush();
617 //This function creates the filename and write data to it
618 //returning status as result
619 function backup_data2file ($file,&$data) {
621 $status = true;
622 $status2 = true;
624 $f = fopen($file,"w");
625 $status = fwrite($f,$data);
626 $status2 = fclose($f);
628 return ($status && $status2);
631 //This function read the filename and read data from it
632 function backup_file2data ($file,&$data) {
634 $status = true;
635 $status2 = true;
637 $f = fopen($file,"r");
638 $data = fread ($f,filesize($file));
639 $status2 = fclose($f);
641 return ($status && $status2);
644 /** this function will restore an entire backup.zip into the specified course
645 * using standard moodle backup/restore functions, but silently.
646 * @param string $pathtofile the absolute path to the backup file.
647 * @param int $destinationcourse the course id to restore to.
648 * @param boolean $emptyfirst whether to delete all coursedata first.
649 * @param boolean $userdata whether to include any userdata that may be in the backup file.
650 * @param array $preferences optional, 0 will be used. Can contain:
651 * metacourse
652 * logs
653 * course_files
654 * messages
656 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
657 global $CFG,$SESSION,$USER; // is there such a thing on cron? I guess so..
659 if (!defined('RESTORE_SILENTLY')) {
660 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
663 $debuginfo = 'import_backup_file_silently: ';
664 $cleanupafter = false;
665 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
667 $course = null;
668 if ($destinationcourse && !$course = get_record('course','id',$destinationcourse)) {
669 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
670 return false;
673 // first check we have a valid file.
674 if (!file_exists($pathtofile) || !is_readable($pathtofile)) {
675 mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
676 return false;
679 // now make sure it's a zip file
680 require_once($CFG->dirroot.'/lib/filelib.php');
681 $filename = substr($pathtofile,strrpos($pathtofile,'/')+1);
682 $mimetype = mimeinfo("type", $filename);
683 if ($mimetype != 'application/zip') {
684 mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
685 return false;
688 // restore_precheck wants this within dataroot, so lets put it there if it's not already..
689 if (strstr($pathtofile,$CFG->dataroot) === false) {
690 // first try and actually move it..
691 if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) {
692 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
693 return false;
695 if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) {
696 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
697 return false;
698 } else {
699 $pathtofile = 'temp/backup/'.$filename;
700 $cleanupafter = true;
702 } else {
703 // it is within dataroot, so take it off the path for restore_precheck.
704 $pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/'));
707 if (!backup_required_functions()) {
708 mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
709 return false;
711 @ini_set("max_execution_time","3000");
712 if (empty($CFG->extramemorylimit)) {
713 raise_memory_limit('128M');
714 } else {
715 raise_memory_limit($CFG->extramemorylimit);
718 if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
719 mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
720 return false;
723 global $restore; // ick
724 $restore = new StdClass;
725 // copy back over the stuff that gets set in restore_precheck
726 $restore->course_header = $SESSION->course_header;
727 $restore->info = $SESSION->info;
729 $xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml";
730 $info = restore_read_xml_roles($xmlfile);
731 $restore->rolesmapping = array();
732 if (isset($info->roles) && is_array($info->roles)) {
733 foreach ($info->roles as $id => $info) {
734 if ($newroleid = get_field('role', 'id', 'shortname', $info->shortname)) {
735 $restore->rolesmapping[$id] = $newroleid;
740 // add on some extra stuff we need...
741 $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
742 $restore->course_id = $destinationcourse;
743 if ($destinationcourse) {
744 $restore->restoreto = RESTORETO_CURRENT_ADDING;
745 $restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate;
746 } else {
747 $restore->restoreto = RESTORETO_NEW_COURSE;
748 $restore->restore_restorecatto = 0; // let this be handled by the headers
749 $restore->course_startdateoffset = 0;
753 $restore->users = $userdata;
754 $restore->user_files = $userdata;
755 $restore->deleting = $emptyfirst;
757 $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
758 $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
759 $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
760 $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
761 $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
762 $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
764 $restore->backup_version = $restore->info->backup_backup_version;
765 $restore->original_wwwroot = $restore->info->original_wwwroot;
767 // now copy what we have over to the session
768 // this needs to happen before restore_setup_for_check
769 // which for some reason reads the session
770 $SESSION->restore =& $restore;
771 // rename the things that are called differently
772 $SESSION->restore->restore_course_files = $restore->course_files;
773 $SESSION->restore->restore_site_files = $restore->site_files;
774 $SESSION->restore->backup_version = $restore->info->backup_backup_version;
776 restore_setup_for_check($restore, $backup_unique_code);
778 // maybe we need users (defaults to 2 (none) in restore_setup_for_check)
779 // so set this again here
780 if (!empty($userdata)) {
781 $restore->users = 1;
784 // we also need modules...
785 if ($allmods = get_records("modules")) {
786 foreach ($allmods as $mod) {
787 $modname = $mod->name;
788 //Now check that we have that module info in the backup file
789 if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") {
790 $restore->mods[$modname]->restore = true;
791 $restore->mods[$modname]->userinfo = $userdata;
793 else {
794 // avoid warnings
795 $restore->mods[$modname]->restore = false;
796 $restore->mods[$modname]->userinfo = false;
800 if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) {
801 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
802 return false;
804 // now get out the new courseid and return that
805 if ($restore->restoreto = RESTORETO_NEW_COURSE) {
806 if (!empty($SESSION->restore->course_id)) {
807 return $SESSION->restore->course_id;
809 return false;
811 return true;
815 * Function to backup an entire course silently and create a zipfile.
817 * @param int $courseid the id of the course
818 * @param array $prefs see {@link backup_generate_preferences_artificially}
820 function backup_course_silently($courseid, $prefs, &$errorstring) {
821 global $CFG, $preferences; // global preferences here because something else wants it :(
822 if (!defined('BACKUP_SILENTLY')) {
823 define('BACKUP_SILENTLY', 1);
825 if (!$course = get_record('course', 'id', $courseid)) {
826 debugging("Couldn't find course with id $courseid in backup_course_silently");
827 return false;
829 $preferences = backup_generate_preferences_artificially($course, $prefs);
830 $preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0;
831 if (backup_execute($preferences, $errorstring)) {
832 return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name;
834 else {
835 return false;
840 * Function to generate the $preferences variable that
841 * backup uses. This will back up all modules and instances in a course.
843 * @param object $course course object
844 * @param array $prefs can contain:
845 backup_metacourse
846 backup_users
847 backup_logs
848 backup_user_files
849 backup_course_files
850 backup_site_files
851 backup_messages
852 userdata
853 * and if not provided, they will not be included.
856 function backup_generate_preferences_artificially($course, $prefs) {
857 global $CFG;
858 $preferences = new StdClass;
859 $preferences->backup_unique_code = time();
860 $preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0);
861 $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
862 $preferences->mods = array();
863 $count = 0;
865 if ($allmods = get_records("modules") ) {
866 foreach ($allmods as $mod) {
867 $modname = $mod->name;
868 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
869 $modbackup = $modname."_backup_mods";
870 $modbackupone = $modname."_backup_one_mod";
871 $modcheckbackup = $modname."_check_backup_mods";
872 if (!file_exists($modfile)) {
873 continue;
875 include_once($modfile);
876 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
877 continue;
879 $var = "exists_".$modname;
880 $preferences->$var = true;
881 $count++;
882 // check that there are instances and we can back them up individually
883 if (!count_records('course_modules','course',$course->id,'module',$mod->id) || !function_exists($modbackupone)) {
884 continue;
886 $var = 'exists_one_'.$modname;
887 $preferences->$var = true;
888 $varname = $modname.'_instances';
889 $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true);
890 foreach ($preferences->$varname as $instance) {
891 $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
892 $var = 'backup_'.$modname.'_instance_'.$instance->id;
893 $preferences->$var = true;
894 $preferences->mods[$modname]->instances[$instance->id]->backup = true;
895 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
896 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
897 $preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var;
898 $var = 'backup_'.$modname.'_instances';
899 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
902 //Check data
903 //Check module info
904 $preferences->mods[$modname]->name = $modname;
906 $var = "backup_".$modname;
907 $preferences->$var = true;
908 $preferences->mods[$modname]->backup = true;
910 //Check include user info
911 $var = "backup_user_info_".$modname;
912 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
913 $preferences->mods[$modname]->userinfo = $preferences->$var;
915 //Call the check function to show more info
916 $modcheckbackup = $modname."_check_backup_mods";
917 $var = $modname.'_instances';
918 $instancestopass = array();
919 if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) {
920 $table->data = array();
921 $countinstances = 0;
922 foreach ($preferences->$var as $instance) {
923 $var1 = 'backup_'.$modname.'_instance_'.$instance->id;
924 $var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
925 if (!empty($preferences->$var1)) {
926 $obj = new StdClass;
927 $obj->name = $instance->name;
928 $obj->userdata = $preferences->$var2;
929 $obj->id = $instance->id;
930 $instancestopass[$instance->id]= $obj;
931 $countinstances++;
936 $modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass);
940 //Check other parameters
941 $preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0);
942 $preferences->backup_logs = (isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0);
943 $preferences->backup_user_files = (isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0);
944 $preferences->backup_course_files = (isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0);
945 $preferences->backup_site_files = (isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0);
946 $preferences->backup_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0);
947 $preferences->backup_gradebook_history = (isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0);
948 $preferences->backup_blogs = (isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0);
949 $preferences->backup_course = $course->id;
951 //Check users
952 user_check_backup($course->id,$preferences->backup_unique_code,$preferences->backup_users,$preferences->backup_messages, $preferences->backup_blogs);
954 //Check logs
955 log_check_backup($course->id);
957 //Check user files
958 user_files_check_backup($course->id,$preferences->backup_unique_code);
960 //Check course files
961 course_files_check_backup($course->id,$preferences->backup_unique_code);
963 //Check site files
964 site_files_check_backup($course->id,$preferences->backup_unique_code);
966 //Role assignments
967 $roles = get_records('role', '', '', 'sortorder');
968 foreach ($roles as $role) {
969 $preferences->backuproleassignments[$role->id] = $role;
972 backup_add_static_preferences($preferences);
973 return $preferences;