Fixes for Bug MDL-8617 "Implement groupings & course modules..."
[moodle-pu.git] / backup / lib.php
blob25944442e6635ce425bfac9dab4f53a9af880e22
1 <?php //$Id$
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", addslashes($value), "name", $name);
11 } else {
12 $config = new object();
13 $config->name = $name;
14 $config->value = addslashes($value);
15 return insert_record("backup_config", $config);
19 //Gets all the information from backup_config table
20 function backup_get_config() {
21 $backup_config = null;
22 if ($configs = get_records("backup_config")) {
23 foreach ($configs as $config) {
24 $backup_config[$config->name] = $config->value;
27 return (object)$backup_config;
30 //Delete old data in backup tables (if exists)
31 //Four hours seem to be appropiate now that backup is stable
32 function backup_delete_old_data() {
34 global $CFG;
36 //Change this if you want !!
37 $hours = 4;
38 //End change this
39 $seconds = $hours * 60 * 60;
40 $delete_from = time()-$seconds;
41 //Now delete from tables
42 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
43 WHERE backup_code < '$delete_from'",false);
44 if ($status) {
45 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
46 WHERE backup_code < '$delete_from'",false);
48 //Now, delete old directory (if exists)
49 if ($status) {
50 $status = backup_delete_old_dirs($delete_from);
52 return($status);
55 //Function to delete dirs/files into temp/backup directory
56 //older than $delete_from
57 function backup_delete_old_dirs($delete_from) {
59 global $CFG;
61 $status = true;
62 //Get files and directories in the temp backup dir witout descend
63 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false, true, true);
64 foreach ($list as $file) {
65 $file_path = $CFG->dataroot."/temp/backup/".$file;
66 $moddate = filemtime($file_path);
67 if ($status && $moddate < $delete_from) {
68 //If directory, recurse
69 if (is_dir($file_path)) {
70 $status = delete_dir_contents($file_path);
71 //There is nothing, delete the directory itself
72 if ($status) {
73 $status = rmdir($file_path);
75 //If file
76 } else {
77 unlink("$file_path");
82 return $status;
85 //Function to check and create the needed dir to
86 //save all the backup
87 function check_and_create_backup_dir($backup_unique_code) {
89 global $CFG;
91 $status = check_dir_exists($CFG->dataroot."/temp",true);
92 if ($status) {
93 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
95 if ($status) {
96 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
99 return $status;
102 //Function to delete all the directory contents recursively
103 //it supports a excluded dit too
104 //Copied from the web !!
105 function delete_dir_contents ($dir,$excludeddir="") {
107 $slash = "/";
109 // Create arrays to store files and directories
110 $dir_files = array();
111 $dir_subdirs = array();
113 // Make sure we can delete it
114 chmod($dir, 0777);
116 if ((($handle = opendir($dir))) == FALSE) {
117 // The directory could not be opened
118 return false;
121 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
122 while($entry = readdir($handle)) {
123 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
124 $dir_subdirs[] = $dir. $slash .$entry;
126 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
127 $dir_files[] = $dir. $slash .$entry;
131 // Delete all files in the curent directory return false and halt if a file cannot be removed
132 for($i=0; $i<count($dir_files); $i++) {
133 chmod($dir_files[$i], 0777);
134 if (((unlink($dir_files[$i]))) == FALSE) {
135 return false;
139 // Empty sub directories and then remove the directory
140 for($i=0; $i<count($dir_subdirs); $i++) {
141 chmod($dir_subdirs[$i], 0777);
142 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
143 return false;
145 else {
146 if (rmdir($dir_subdirs[$i]) == FALSE) {
147 return false;
152 // Close directory
153 closedir($handle);
155 // Success, every thing is gone return true
156 return true;
159 //Function to clear (empty) the contents of the backup_dir
160 function clear_backup_dir($backup_unique_code) {
162 global $CFG;
164 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
166 //Delete recursively
167 $status = delete_dir_contents($rootdir);
169 return $status;
172 //Returns the module type of a course_module's id in a course
173 function get_module_type ($courseid,$moduleid) {
175 global $CFG;
177 $results = get_records_sql ("SELECT cm.id, m.name
178 FROM {$CFG->prefix}course_modules cm,
179 {$CFG->prefix}modules m
180 WHERE cm.course = '$courseid' AND
181 cm.id = '$moduleid' AND
182 m.id = cm.module");
184 if ($results) {
185 $name = $results[$moduleid]->name;
186 } else {
187 $name = false;
189 return $name;
192 //This function return the names of all directories under a give directory
193 //Not recursive
194 function list_directories ($rootdir) {
196 $results = null;
198 $dir = opendir($rootdir);
199 while ($file=readdir($dir)) {
200 if ($file=="." || $file=="..") {
201 continue;
203 if (is_dir($rootdir."/".$file)) {
204 $results[$file] = $file;
207 closedir($dir);
208 return $results;
211 //This function return the names of all directories and files under a give directory
212 //Not recursive
213 function list_directories_and_files ($rootdir) {
215 $results = "";
217 $dir = opendir($rootdir);
218 while ($file=readdir($dir)) {
219 if ($file=="." || $file=="..") {
220 continue;
222 $results[$file] = $file;
224 closedir($dir);
225 return $results;
228 //This function clean data from backup tables and
229 //delete all temp files used
230 function clean_temp_data ($preferences) {
232 global $CFG;
234 $status = true;
236 //true->do it, false->don't do it. To debug if necessary.
237 if (true) {
238 //Now delete from tables
239 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
240 WHERE backup_code = '$preferences->backup_unique_code'",false);
241 if ($status) {
242 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
243 WHERE backup_code = '$preferences->backup_unique_code'",false);
245 //Now, delete temp directory (if exists)
246 $file_path = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
247 if (is_dir($file_path)) {
248 $status = delete_dir_contents($file_path);
249 //There is nothing, delete the directory itself
250 if ($status) {
251 $status = rmdir($file_path);
255 return $status;
258 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
259 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
260 //This functions are used to copy any file or directory ($from_file)
261 //to a new file or directory ($to_file). It works recursively and
262 //mantains file perms.
263 //I've copied it from: http://www.php.net/manual/en/function.copy.php
264 //Little modifications done
266 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
268 global $CFG;
270 if (is_file($from_file)) {
271 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
272 //$perms=fileperms($from_file);
273 //return copy($from_file,$to_file) && chmod($to_file,$perms);
274 umask(0000);
275 if (copy($from_file,$to_file)) {
276 chmod($to_file,$CFG->directorypermissions);
277 if (!empty($log_clam)) {
278 clam_log_upload($to_file,null,true);
280 return true;
282 return false;
284 else if (is_dir($from_file)) {
285 return backup_copy_dir($from_file,$to_file);
287 else{
288 //echo "<br />Error: not file or dir ".$from_file; //Debug
289 return false;
293 function backup_copy_dir($from_file,$to_file) {
295 global $CFG;
297 $status = true; // Initialize this, next code will change its value if needed
299 if (!is_dir($to_file)) {
300 //echo "<br />Creating ".$to_file; //Debug
301 umask(0000);
302 $status = mkdir($to_file,$CFG->directorypermissions);
304 $dir = opendir($from_file);
305 while ($file=readdir($dir)) {
306 if ($file=="." || $file=="..") {
307 continue;
309 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
311 closedir($dir);
312 return $status;
314 ///Ends copy file/dirs functions
315 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
316 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
319 function upgrade_backup_db($continueto) {
320 /// This function upgrades the backup tables, if necessary
321 /// It's called from admin/index.php, also backup.php and restore.php
323 global $CFG, $db;
325 require_once ("$CFG->dirroot/backup/version.php"); // Get code versions
327 if (empty($CFG->backup_version)) { // Backup has never been installed.
328 $strdatabaseupgrades = get_string("databaseupgrades");
329 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, "",
330 upgrade_get_javascript(), false, "&nbsp;", "&nbsp;");
332 upgrade_log_start();
333 print_heading('backup');
334 $db->debug=true;
336 /// Both old .sql files and new install.xml are supported
337 /// but we priorize install.xml (XMLDB) if present
338 $status = false;
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
345 $db->debug = false;
346 if ($status) {
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);
354 print_footer('none');
355 exit;
356 } else {
357 error("Upgrade of backup system failed! (Could not update version in config table)");
359 } else {
360 error("Backup tables could NOT be set up successfully!");
364 /// Upgrading code starts here
365 $oldupgrade = false;
366 $newupgrade = false;
367 if (is_readable($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.php')) {
368 include_once($CFG->dirroot . '/backup/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
369 $oldupgrade = true;
371 if (is_readable($CFG->dirroot . '/backup/db/upgrade.php')) {
372 include_once($CFG->dirroot . '/backup/db/upgrade.php'); // defines new upgrading function
373 $newupgrade = true;
376 if ($backup_version > $CFG->backup_version) { // Upgrade tables
377 $strdatabaseupgrades = get_string("databaseupgrades");
378 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '', upgrade_get_javascript());
380 upgrade_log_start();
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)) {
390 $db->debug = true;
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) {
400 $db->debug = true;
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');
407 $db->debug=false;
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);
414 print_footer('none');
415 exit;
416 } else {
417 error("Upgrade of backup system failed! (Could not update version in config table)");
419 } else {
420 error("Upgrade failed! See backup/version.php");
423 } else if ($backup_version < $CFG->backup_version) {
424 upgrade_log_start();
425 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
427 upgrade_log_finish();
431 //This function is used to insert records in the backup_ids table
432 //If the info field is greater than max_db_storage, then its info
433 //is saved to filesystem
434 function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
436 global $CFG;
438 $max_db_storage = 128; //Max bytes to save to db, else save to file
440 $status = true;
442 //First delete to avoid PK duplicates
443 $status = backup_delid($backup_unique_code, $table, $old_id);
445 //Now, serialize info
446 $info_ser = serialize($info);
448 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
449 //insert a "infile" in the info field
451 if (strlen($info_ser) > $max_db_storage) {
452 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
453 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
454 //Save data to file
455 $status = backup_data2file($filename,$info_ser);
456 //Set info_to save
457 $info_to_save = "infile";
458 } else {
459 //Saving to db, addslashes
460 $info_to_save = addslashes($info_ser);
463 //Now, insert the record
464 if ($status) {
465 //Build the record
466 $rec->backup_code = $backup_unique_code;
467 $rec->table_name = $table;
468 $rec->old_id = $old_id;
469 $rec->new_id = ($new_id === null? 0 : $new_id);
470 $rec->info = $info_to_save;
472 if (!insert_record('backup_ids', $rec, false)) {
473 $status = false;
476 return $status;
479 //This function is used to delete recods from the backup_ids table
480 //If the info field is "infile" then the file is deleted too
481 function backup_delid ($backup_unique_code, $table, $old_id) {
483 global $CFG;
485 $status = true;
487 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
488 WHERE backup_code = $backup_unique_code AND
489 table_name = '$table' AND
490 old_id = '$old_id'",false);
491 return $status;
494 //This function is used to get a record from the backup_ids table
495 //If the info field is "infile" then its info
496 //is read from filesystem
497 function backup_getid ($backup_unique_code, $table, $old_id) {
499 global $CFG;
501 $status = true;
502 $status2 = true;
504 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
505 "table_name",$table,
506 "old_id", $old_id);
508 //If info field = "infile", get file contents
509 if (!empty($status->info) && $status->info == "infile") {
510 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
511 //Read data from file
512 $status2 = backup_file2data($filename,$info);
513 if ($status2) {
514 //unserialize data
515 $status->info = unserialize($info);
516 } else {
517 $status = false;
519 } else {
520 //Only if status (record exists)
521 if ($status) {
522 ////First strip slashes
523 $temp = stripslashes($status->info);
524 //Now unserialize
525 $status->info = unserialize($temp);
529 return $status;
532 //This function is used to add slashes (and decode from UTF-8 if needed)
533 //It's used intensivelly when restoring modules and saving them in db
534 function backup_todb ($data) {
535 return restore_decode_absolute_links(addslashes($data));
538 //This function is used to check that every necessary function to
539 //backup/restore exists in the current php installation. Thanks to
540 //gregb@crowncollege.edu by the idea.
541 function backup_required_functions($justcheck=false) {
543 if(!function_exists('utf8_encode')) {
544 if (empty($justcheck)) {
545 error('You need to add XML support to your PHP installation');
546 } else {
547 return false;
551 return true;
554 //This function send n white characters to the browser and flush the
555 //output buffer. Used to avoid browser timeouts and to show the progress.
556 function backup_flush($n=0,$time=false) {
557 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
558 return;
560 if ($time) {
561 $ti = strftime("%X",time());
562 } else {
563 $ti = "";
565 echo str_repeat(" ", $n) . $ti . "\n";
566 flush();
569 //This function creates the filename and write data to it
570 //returning status as result
571 function backup_data2file ($file,&$data) {
573 $status = true;
574 $status2 = true;
576 $f = fopen($file,"w");
577 $status = fwrite($f,$data);
578 $status2 = fclose($f);
580 return ($status && $status2);
583 //This function read the filename and read data from it
584 function backup_file2data ($file,&$data) {
586 $status = true;
587 $status2 = true;
589 $f = fopen($file,"r");
590 $data = fread ($f,filesize($file));
591 $status2 = fclose($f);
593 return ($status && $status2);
596 /** this function will restore an entire backup.zip into the specified course
597 * using standard moodle backup/restore functions, but silently.
598 * @param string $pathtofile the absolute path to the backup file.
599 * @param int $destinationcourse the course id to restore to.
600 * @param boolean $emptyfirst whether to delete all coursedata first.
601 * @param boolean $userdata whether to include any userdata that may be in the backup file.
603 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false) {
604 global $CFG,$SESSION,$USER; // is there such a thing on cron? I guess so..
606 if (empty($USER)) {
607 $USER = get_admin();
608 $USER->admin = 1; // not sure why, but this doesn't get set
611 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
613 $debuginfo = 'import_backup_file_silently: ';
614 $cleanupafter = false;
615 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
617 if (!$course = get_record('course','id',$destinationcourse)) {
618 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
619 return false;
622 // first check we have a valid file.
623 if (!file_exists($pathtofile) || !is_readable($pathtofile)) {
624 mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
625 return false;
628 // now make sure it's a zip file
629 require_once($CFG->dirroot.'/lib/filelib.php');
630 $filename = substr($pathtofile,strrpos($pathtofile,'/')+1);
631 $mimetype = mimeinfo("type", $filename);
632 if ($mimetype != 'application/zip') {
633 mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
634 return false;
637 // restore_precheck wants this within dataroot, so lets put it there if it's not already..
638 if (strstr($pathtofile,$CFG->dataroot) === false) {
639 // first try and actually move it..
640 if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) {
641 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
642 return false;
644 if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) {
645 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
646 return false;
647 } else {
648 $pathtofile = 'temp/backup/'.$filename;
649 $cleanupafter = true;
651 } else {
652 // it is within dataroot, so take it off the path for restore_precheck.
653 $pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/'));
656 if (!backup_required_functions()) {
657 mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
658 return false;
661 @ini_set("max_execution_time","3000");
662 raise_memory_limit("192M");
664 if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
665 mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
666 return false;
669 restore_setup_for_check($SESSION->restore,$backup_unique_code);
671 // add on some extra stuff we need...
672 $SESSION->restore->restoreto = 1;
673 $SESSION->restore->course_id = $destinationcourse;
674 $SESSION->restore->deleting = $emptyfirst;
676 // maybe we need users (defaults to 2 in restore_setup_for_check)
677 if (!empty($userdata)) {
678 $SESSION->restore->users = 1;
681 // we also need modules...
682 if ($allmods = get_records("modules")) {
683 foreach ($allmods as $mod) {
684 $modname = $mod->name;
685 //Now check that we have that module info in the backup file
686 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
687 $SESSION->restore->mods[$modname]->restore = true;
688 $SESSION->restore->mods[$modname]->userinfo = $userdata;
692 if (!restore_execute($SESSION->restore,$SESSION->info,$SESSION->course_header,$errorstr)) {
693 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
694 return false;
696 return true;