2 //This file contains all the general function needed (file manipulation...)
3 //not directly part of the backup/restore utility
5 require_once($CFG->dirroot
.'/lib/uploadlib.php');
7 //Sets a name/value pair in backup_config table
8 function backup_set_config($name, $value) {
9 if (get_field("backup_config", "name", "name", $name)) {
10 return set_field("backup_config", "value", $value, "name", $name);
12 $config->name
= $name;
13 $config->value
= $value;
14 return insert_record("backup_config", $config);
18 //Gets all the information from backup_config table
19 function backup_get_config() {
20 $backup_config = null;
21 if ($configs = get_records("backup_config")) {
22 foreach ($configs as $config) {
23 $backup_config[$config->name
] = $config->value
;
26 return (object)$backup_config;
29 //Delete old data in backup tables (if exists)
30 //Four hours seem to be appropiate now that backup is stable
31 function backup_delete_old_data() {
35 //Change this if you want !!
38 $seconds = $hours * 60 * 60;
39 $delete_from = time()-$seconds;
40 //Now delete from tables
41 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
42 WHERE backup_code < '$delete_from'",false);
44 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
45 WHERE backup_code < '$delete_from'",false);
47 //Now, delete old directory (if exists)
49 $status = backup_delete_old_dirs($delete_from);
54 //Function to delete dirs/files into temp/backup directory
55 //older than $delete_from
56 function backup_delete_old_dirs($delete_from) {
61 //Get files and directories in the temp backup dir witout descend
62 $list = get_directory_list($CFG->dataroot
."/temp/backup", "", false, true, true);
63 foreach ($list as $file) {
64 $file_path = $CFG->dataroot
."/temp/backup/".$file;
65 $moddate = filemtime($file_path);
66 if ($status && $moddate < $delete_from) {
67 //If directory, recurse
68 if (is_dir($file_path)) {
69 $status = delete_dir_contents($file_path);
70 //There is nothing, delete the directory itself
72 $status = rmdir($file_path);
84 //Function to check and create the needed dir to
86 function check_and_create_backup_dir($backup_unique_code) {
90 $status = check_dir_exists($CFG->dataroot
."/temp",true);
92 $status = check_dir_exists($CFG->dataroot
."/temp/backup",true);
95 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code,true);
101 //Function to delete all the directory contents recursively
102 //it supports a excluded dit too
103 //Copied from the web !!
104 function delete_dir_contents ($dir,$excludeddir="") {
108 // Create arrays to store files and directories
109 $dir_files = array();
110 $dir_subdirs = array();
112 // Make sure we can delete it
115 if ((($handle = opendir($dir))) == FALSE) {
116 // The directory could not be opened
120 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
121 while($entry = readdir($handle)) {
122 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
123 $dir_subdirs[] = $dir. $slash .$entry;
125 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
126 $dir_files[] = $dir. $slash .$entry;
130 // Delete all files in the curent directory return false and halt if a file cannot be removed
131 for($i=0; $i<count($dir_files); $i++
) {
132 chmod($dir_files[$i], 0777);
133 if (((unlink($dir_files[$i]))) == FALSE) {
138 // Empty sub directories and then remove the directory
139 for($i=0; $i<count($dir_subdirs); $i++
) {
140 chmod($dir_subdirs[$i], 0777);
141 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
145 if (rmdir($dir_subdirs[$i]) == FALSE) {
154 // Success, every thing is gone return true
158 //Function to clear (empty) the contents of the backup_dir
159 function clear_backup_dir($backup_unique_code) {
163 $rootdir = $CFG->dataroot
."/temp/backup/".$backup_unique_code;
166 $status = delete_dir_contents($rootdir);
171 //Returns the module type of a course_module's id in a course
172 function get_module_type ($courseid,$moduleid) {
176 $results = get_records_sql ("SELECT cm.id, m.name
177 FROM {$CFG->prefix}course_modules cm,
178 {$CFG->prefix}modules m
179 WHERE cm.course = '$courseid' AND
180 cm.id = '$moduleid' AND
184 $name = $results[$moduleid]->name
;
191 //This function return the names of all directories under a give directory
193 function list_directories ($rootdir) {
197 $dir = opendir($rootdir);
198 while ($file=readdir($dir)) {
199 if ($file=="." ||
$file=="..") {
202 if (is_dir($rootdir."/".$file)) {
203 $results[$file] = $file;
210 //This function return the names of all directories and files under a give directory
212 function list_directories_and_files ($rootdir) {
216 $dir = opendir($rootdir);
217 while ($file=readdir($dir)) {
218 if ($file=="." ||
$file=="..") {
221 $results[$file] = $file;
227 //This function clean data from backup tables and
228 //delete all temp files used
229 function clean_temp_data ($preferences) {
235 //true->do it, false->don't do it. To debug if necessary.
237 //Now delete from tables
238 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
239 WHERE backup_code = '$preferences->backup_unique_code'",false);
241 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
242 WHERE backup_code = '$preferences->backup_unique_code'",false);
244 //Now, delete temp directory (if exists)
245 $file_path = $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
;
246 if (is_dir($file_path)) {
247 $status = delete_dir_contents($file_path);
248 //There is nothing, delete the directory itself
250 $status = rmdir($file_path);
257 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
258 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
259 //This functions are used to copy any file or directory ($from_file)
260 //to a new file or directory ($to_file). It works recursively and
261 //mantains file perms.
262 //I've copied it from: http://www.php.net/manual/en/function.copy.php
263 //Little modifications done
265 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
269 if (is_file($from_file)) {
270 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
271 //$perms=fileperms($from_file);
272 //return copy($from_file,$to_file) && chmod($to_file,$perms);
274 if (copy($from_file,$to_file)) {
275 chmod($to_file,$CFG->directorypermissions
);
276 if (!empty($log_clam)) {
277 clam_log_upload($to_file,null,true);
283 else if (is_dir($from_file)) {
284 return backup_copy_dir($from_file,$to_file);
287 //echo "<br />Error: not file or dir ".$from_file; //Debug
292 function backup_copy_dir($from_file,$to_file) {
296 $status = true; // Initialize this, next code will change its value if needed
298 if (!is_dir($to_file)) {
299 //echo "<br />Creating ".$to_file; //Debug
301 $status = mkdir($to_file,$CFG->directorypermissions
);
303 $dir = opendir($from_file);
304 while ($file=readdir($dir)) {
305 if ($file=="." ||
$file=="..") {
308 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
313 ///Ends copy file/dirs functions
314 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
315 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
318 function upgrade_backup_db($continueto) {
319 /// This function upgrades the backup tables, if necessary
320 /// It's called from admin/index.php, also backup.php and restore.php
324 require_once ("$CFG->dirroot/backup/version.php"); // Get code versions
326 if (empty($CFG->backup_version
)) { // Backup has never been installed.
327 $strdatabaseupgrades = get_string("databaseupgrades");
328 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, "",
329 '<script type="text/javascript" src="' . $CFG->wwwroot
. '/lib/scroll_to_errors.js"></script>',
330 false, " ", " ");
333 print_heading('backup');
336 /// Both old .sql files and new install.xml are supported
337 /// but we priorize install.xml (XMLDB) if present
339 if (file_exists($CFG->dirroot
. '/backup/db/install.xml')) {
340 $status = install_from_xmldb_file($CFG->dirroot
. '/backup/db/install.xml'); //New method
341 } else if (file_exists($CFG->dirroot
. '/backup/db/' . $CFG->dbtype
. '.sql')) {
342 $status = modify_database($CFG->dirroot
. '/backup/db/' . $CFG->dbtype
. '.sql'); //Old method
347 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
348 //initialize default backup settings now
349 $adminroot = admin_get_root();
350 apply_default_settings($adminroot->locate('backups'));
351 notify(get_string("databasesuccess"), "green");
352 notify(get_string("databaseupgradebackups", "", $backup_version), "green");
353 print_continue($continueto);
356 error("Upgrade of backup system failed! (Could not update version in config table)");
359 error("Backup tables could NOT be set up successfully!");
363 /// Upgrading code starts here
366 if (is_readable($CFG->dirroot
. '/backup/db/' . $CFG->dbtype
. '.php')) {
367 include_once($CFG->dirroot
. '/backup/db/' . $CFG->dbtype
. '.php'); // defines old upgrading function
370 if (is_readable($CFG->dirroot
. '/backup/db/upgrade.php')) {
371 include_once($CFG->dirroot
. '/backup/db/upgrade.php'); // defines new upgrading function
375 if ($backup_version > $CFG->backup_version
) { // Upgrade tables
376 $strdatabaseupgrades = get_string("databaseupgrades");
377 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '',
378 '<script type="text/javascript" src="' . $CFG->wwwroot
. '/lib/scroll_to_errors.js"></script>');
381 print_heading('backup');
383 /// Run de old and new upgrade functions for the module
384 $oldupgrade_function = 'backup_upgrade';
385 $newupgrade_function = 'xmldb_backup_upgrade';
387 /// First, the old function if exists
388 $oldupgrade_status = true;
389 if ($oldupgrade && function_exists($oldupgrade_function)) {
391 $oldupgrade_status = $oldupgrade_function($CFG->backup_version
);
392 } else if ($oldupgrade) {
393 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
394 '/backup/db/' . $CFG->dbtype
. '.php');
397 /// Then, the new function if exists and the old one was ok
398 $newupgrade_status = true;
399 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
401 $newupgrade_status = $newupgrade_function($CFG->backup_version
);
402 } else if ($newupgrade) {
403 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
404 '/backup/db/upgrade.php');
408 /// Now analyze upgrade results
409 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
410 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
411 notify(get_string("databasesuccess"), "green");
412 notify(get_string("databaseupgradebackups", "", $backup_version), "green");
413 print_continue($continueto);
416 error("Upgrade of backup system failed! (Could not update version in config table)");
419 error("Upgrade failed! See backup/version.php");
422 } else if ($backup_version < $CFG->backup_version
) {
424 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
426 upgrade_log_finish();
430 //This function is used to insert records in the backup_ids table
431 //If the info field is greater than max_db_storage, then its info
432 //is saved to filesystem
433 function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
437 $max_db_storage = 128; //Max bytes to save to db, else save to file
441 //First delete to avoid PK duplicates
442 $status = backup_delid($backup_unique_code, $table, $old_id);
444 //Now, serialize info
445 $info_ser = serialize($info);
447 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
448 //insert a "infile" in the info field
450 if (strlen($info_ser) > $max_db_storage) {
451 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
452 $filename = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
454 $status = backup_data2file($filename,$info_ser);
456 $info_to_save = "infile";
458 //Saving to db, addslashes
459 $info_to_save = addslashes($info_ser);
462 //Now, insert the record
465 $rec->backup_code
= $backup_unique_code;
466 $rec->table_name
= $table;
467 $rec->old_id
= $old_id;
468 $rec->new_id
= ($new_id === null?
0 : $new_id);
469 $rec->info
= $info_to_save;
471 if (!insert_record('backup_ids', $rec, false)) {
478 //This function is used to delete recods from the backup_ids table
479 //If the info field is "infile" then the file is deleted too
480 function backup_delid ($backup_unique_code, $table, $old_id) {
486 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
487 WHERE backup_code = $backup_unique_code AND
488 table_name = '$table' AND
489 old_id = '$old_id'",false);
493 //This function is used to get a record from the backup_ids table
494 //If the info field is "infile" then its info
495 //is read from filesystem
496 function backup_getid ($backup_unique_code, $table, $old_id) {
503 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
507 //If info field = "infile", get file contents
508 if (!empty($status->info
) && $status->info
== "infile") {
509 $filename = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
510 //Read data from file
511 $status2 = backup_file2data($filename,$info);
514 $status->info
= unserialize($info);
519 //Only if status (record exists)
521 ////First strip slashes
522 $temp = stripslashes($status->info
);
524 $status->info
= unserialize($temp);
531 //This function is used to add slashes (and decode from UTF-8 if needed)
532 //It's used intensivelly when restoring modules and saving them in db
533 function backup_todb ($data) {
536 if (empty($CFG->unicodedb
)) {
537 return restore_decode_absolute_links(addslashes(utf8_decode($data)));
539 return restore_decode_absolute_links(addslashes($data));
543 //This function is used to check that every necessary function to
544 //backup/restore exists in the current php installation. Thanks to
545 //gregb@crowncollege.edu by the idea.
546 function backup_required_functions($justcheck=false) {
548 if(!function_exists('utf8_encode')) {
549 if (empty($justcheck)) {
550 error('You need to add XML support to your PHP installation');
559 //This function send n white characters to the browser and flush the
560 //output buffer. Used to avoid browser timeouts and to show the progress.
561 function backup_flush($n=0,$time=false) {
562 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
566 $ti = strftime("%X",time());
570 echo str_repeat(" ", $n) . $ti . "\n";
574 //This function creates the filename and write data to it
575 //returning status as result
576 function backup_data2file ($file,&$data) {
581 $f = fopen($file,"w");
582 $status = fwrite($f,$data);
583 $status2 = fclose($f);
585 return ($status && $status2);
588 //This function read the filename and read data from it
589 function backup_file2data ($file,&$data) {
594 $f = fopen($file,"r");
595 $data = fread ($f,filesize($file));
596 $status2 = fclose($f);
598 return ($status && $status2);
601 /** this function will restore an entire backup.zip into the specified course
602 * using standard moodle backup/restore functions, but silently.
603 * @param string $pathtofile the absolute path to the backup file.
604 * @param int $destinationcourse the course id to restore to.
605 * @param boolean $emptyfirst whether to delete all coursedata first.
606 * @param boolean $userdata whether to include any userdata that may be in the backup file.
608 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false) {
609 global $CFG,$SESSION,$USER; // is there such a thing on cron? I guess so..
613 $USER->admin
= 1; // not sure why, but this doesn't get set
616 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
618 $debuginfo = 'import_backup_file_silently: ';
619 $cleanupafter = false;
620 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
622 if (!$course = get_record('course','id',$destinationcourse)) {
623 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
627 // first check we have a valid file.
628 if (!file_exists($pathtofile) ||
!is_readable($pathtofile)) {
629 mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
633 // now make sure it's a zip file
634 require_once($CFG->dirroot
.'/lib/filelib.php');
635 $filename = substr($pathtofile,strrpos($pathtofile,'/')+
1);
636 $mimetype = mimeinfo("type", $filename);
637 if ($mimetype != 'application/zip') {
638 mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
642 // restore_precheck wants this within dataroot, so lets put it there if it's not already..
643 if (strstr($pathtofile,$CFG->dataroot
) === false) {
644 // first try and actually move it..
645 if (!check_dir_exists($CFG->dataroot
.'/temp/backup/',true)) {
646 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
649 if (!copy($pathtofile,$CFG->dataroot
.'/temp/backup/'.$filename)) {
650 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
653 $pathtofile = 'temp/backup/'.$filename;
654 $cleanupafter = true;
657 // it is within dataroot, so take it off the path for restore_precheck.
658 $pathtofile = substr($pathtofile,strlen($CFG->dataroot
.'/'));
661 if (!backup_required_functions()) {
662 mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
666 @ini_set
("max_execution_time","3000");
667 raise_memory_limit("128M");
669 if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
670 mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
674 restore_setup_for_check($SESSION->restore
,$backup_unique_code);
676 // add on some extra stuff we need...
677 $SESSION->restore
->restoreto
= 1;
678 $SESSION->restore
->course_id
= $destinationcourse;
679 $SESSION->restore
->deleting
= $emptyfirst;
681 // maybe we need users (defaults to 2 in restore_setup_for_check)
682 if (!empty($userdata)) {
683 $SESSION->restore
->users
= 1;
686 // we also need modules...
687 if ($allmods = get_records("modules")) {
688 foreach ($allmods as $mod) {
689 $modname = $mod->name
;
690 //Now check that we have that module info in the backup file
691 if (isset($SESSION->info
->mods
[$modname]) && $SESSION->info
->mods
[$modname]->backup
== "true") {
692 $SESSION->restore
->mods
[$modname]->restore
= true;
693 $SESSION->restore
->mods
[$modname]->userinfo
= $userdata;
697 if (!restore_execute($SESSION->restore
,$SESSION->info
,$SESSION->course_header
,$errorstr)) {
698 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');