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 if a directory exists
85 //and, optionally, create it
86 function check_dir_exists($dir,$create=false) {
96 $status = mkdir ($dir,$CFG->directorypermissions
);
102 //Function to check and create the needed dir to
103 //save all the backup
104 function check_and_create_backup_dir($backup_unique_code) {
108 $status = check_dir_exists($CFG->dataroot
."/temp",true);
110 $status = check_dir_exists($CFG->dataroot
."/temp/backup",true);
113 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code,true);
119 //Function to delete all the directory contents recursively
120 //it supports a excluded dit too
121 //Copied from the web !!
122 function delete_dir_contents ($dir,$excludeddir="") {
126 // Create arrays to store files and directories
127 $dir_files = array();
128 $dir_subdirs = array();
130 // Make sure we can delete it
133 if ((($handle = opendir($dir))) == FALSE) {
134 // The directory could not be opened
138 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
139 while($entry = readdir($handle)) {
140 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
141 $dir_subdirs[] = $dir. $slash .$entry;
143 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
144 $dir_files[] = $dir. $slash .$entry;
148 // Delete all files in the curent directory return false and halt if a file cannot be removed
149 for($i=0; $i<count($dir_files); $i++
) {
150 chmod($dir_files[$i], 0777);
151 if (((unlink($dir_files[$i]))) == FALSE) {
156 // Empty sub directories and then remove the directory
157 for($i=0; $i<count($dir_subdirs); $i++
) {
158 chmod($dir_subdirs[$i], 0777);
159 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
163 if (rmdir($dir_subdirs[$i]) == FALSE) {
172 // Success, every thing is gone return true
176 //Function to clear (empty) the contents of the backup_dir
177 function clear_backup_dir($backup_unique_code) {
181 $rootdir = $CFG->dataroot
."/temp/backup/".$backup_unique_code;
184 $status = delete_dir_contents($rootdir);
189 //Returns the module type of a course_module's id in a course
190 function get_module_type ($courseid,$moduleid) {
194 $results = get_records_sql ("SELECT cm.id, m.name
195 FROM {$CFG->prefix}course_modules cm,
196 {$CFG->prefix}modules m
197 WHERE cm.course = '$courseid' AND
198 cm.id = '$moduleid' AND
202 $name = $results[$moduleid]->name
;
209 //This function return the names of all directories under a give directory
211 function list_directories ($rootdir) {
215 $dir = opendir($rootdir);
216 while ($file=readdir($dir)) {
217 if ($file=="." ||
$file=="..") {
220 if (is_dir($rootdir."/".$file)) {
221 $results[$file] = $file;
228 //This function return the names of all directories and files under a give directory
230 function list_directories_and_files ($rootdir) {
234 $dir = opendir($rootdir);
235 while ($file=readdir($dir)) {
236 if ($file=="." ||
$file=="..") {
239 $results[$file] = $file;
245 //This function clean data from backup tables and
246 //delete all temp files used
247 function clean_temp_data ($preferences) {
253 //true->do it, false->don't do it. To debug if necessary.
255 //Now delete from tables
256 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
257 WHERE backup_code = '$preferences->backup_unique_code'",false);
259 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
260 WHERE backup_code = '$preferences->backup_unique_code'",false);
262 //Now, delete temp directory (if exists)
263 $file_path = $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
;
264 if (is_dir($file_path)) {
265 $status = delete_dir_contents($file_path);
266 //There is nothing, delete the directory itself
268 $status = rmdir($file_path);
275 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
276 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
277 //This functions are used to copy any file or directory ($from_file)
278 //to a new file or directory ($to_file). It works recursively and
279 //mantains file perms.
280 //I've copied it from: http://www.php.net/manual/en/function.copy.php
281 //Little modifications done
283 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
287 if (is_file($from_file)) {
288 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
289 //$perms=fileperms($from_file);
290 //return copy($from_file,$to_file) && chmod($to_file,$perms);
292 if (copy($from_file,$to_file) && chmod($to_file,$CFG->directorypermissions
)) {
293 if (!empty($log_clam)) {
294 clam_log_upload($to_file,null,true);
300 else if (is_dir($from_file)) {
301 return backup_copy_dir($from_file,$to_file);
304 //echo "<br />Error: not file or dir ".$from_file; //Debug
309 function backup_copy_dir($from_file,$to_file) {
313 if (!is_dir($to_file)) {
314 //echo "<br />Creating ".$to_file; //Debug
316 $status = mkdir($to_file,$CFG->directorypermissions
);
318 $dir = opendir($from_file);
319 while ($file=readdir($dir)) {
320 if ($file=="." ||
$file=="..") {
323 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
328 ///Ends copy file/dirs functions
329 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
330 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
333 function upgrade_backup_db($continueto) {
334 /// This function upgrades the backup tables, if necessary
335 /// It's called from admin/index.php, also backup.php and restore.php
339 require_once ("$CFG->dirroot/backup/version.php"); // Get code versions
341 if (empty($CFG->backup_version
)) { // Backup has never been installed.
342 $strdatabaseupgrades = get_string("databaseupgrades");
343 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades,
344 "", "", false, " ", " ");
347 if (modify_database("$CFG->dirroot/backup/db/$CFG->dbtype.sql")) {
349 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
350 notify(get_string("databasesuccess"), "green");
351 notify(get_string("databaseupgradebackups", "", $backup_version));
352 print_continue($continueto);
355 error("Upgrade of backup system failed! (Could not update version in config table)");
358 error("Backup tables could NOT be set up successfully!");
363 if ($backup_version > $CFG->backup_version
) { // Upgrade tables
364 $strdatabaseupgrades = get_string("databaseupgrades");
365 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
367 require_once ("$CFG->dirroot/backup/db/$CFG->dbtype.php");
370 if (backup_upgrade($CFG->backup_version
)) {
372 if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
373 notify(get_string("databasesuccess"), "green");
374 notify(get_string("databaseupgradebackups", "", $backup_version));
375 print_continue($continueto);
378 error("Upgrade of backup system failed! (Could not update version in config table)");
382 error("Upgrade failed! See backup/version.php");
385 } else if ($backup_version < $CFG->backup_version
) {
386 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
391 //This function is used to insert records in the backup_ids table
392 //If the info field is greater than max_db_storage, then its info
393 //is saved to filesystem
394 function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
398 $max_db_storage = 128; //Max bytes to save to db, else save to file
402 //First delete to avoid PK duplicates
403 $status = backup_delid($backup_unique_code, $table, $old_id);
405 //Now, serialize info
406 $info_ser = serialize($info);
408 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
409 //insert a "infile" in the info field
411 if (strlen($info_ser) > $max_db_storage) {
412 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
413 $filename = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
415 $status = backup_data2file($filename,$info_ser);
417 $info_to_save = "infile";
419 //Saving to db, addslashes
420 $info_to_save = addslashes($info_ser);
423 //Now, insert the record
426 $rec->backup_code
= $backup_unique_code;
427 $rec->table_name
= $table;
428 $rec->old_id
= $old_id;
429 $rec->new_id
=$new_id;
430 $rec->info
= $info_to_save;
432 $status = insert_record ("backup_ids", $rec, false,"backup_code");
437 //This function is used to delete recods from the backup_ids table
438 //If the info field is "infile" then the file is deleted too
439 function backup_delid ($backup_unique_code, $table, $old_id) {
445 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
446 WHERE backup_code = $backup_unique_code AND
447 table_name = '$table' AND
448 old_id = '$old_id'",false);
452 //This function is used to get a record from the backup_ids table
453 //If the info field is "infile" then its info
454 //is read from filesystem
455 function backup_getid ($backup_unique_code, $table, $old_id) {
462 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
466 //If info field = "infile", get file contents
467 if ($status->info
== "infile") {
468 $filename = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
469 //Read data from file
470 $status2 = backup_file2data($filename,$info);
473 $status->info
= unserialize($info);
478 //Only if status (record exists)
480 ////First strip slashes
481 $temp = stripslashes($status->info
);
483 $status->info
= unserialize($temp);
490 //This function is used to add slashes and decode from UTF-8
491 //It's used intensivelly when restoring modules and saving them in db
492 function backup_todb ($data) {
493 return restore_decode_absolute_links(addslashes(utf8_decode($data)));
496 //This function is used to check that every necessary function to
497 //backup/restore exists in the current php installation. Thanks to
498 //gregb@crowncollege.edu by the idea.
499 function backup_required_functions() {
501 if(!function_exists('utf8_encode')) {
502 error('You need to add XML support to your PHP installation');
507 //This function send n white characters to the browser and flush the
508 //output buffer. Used to avoid browser timeouts and to show the progress.
509 function backup_flush($n=0,$time=false) {
511 $ti = strftime("%X",time());
515 echo str_repeat(" ", $n) . $ti . "\n";
519 //This function creates the filename and write data to it
520 //returning status as result
521 function backup_data2file ($file,&$data) {
526 $f = fopen($file,"w");
527 $status = fwrite($f,$data);
528 $status2 = fclose($f);
530 return ($status && $status2);
533 //This function read the filename and read data from it
534 function backup_file2data ($file,&$data) {
539 $f = fopen($file,"r");
540 $data = fread ($f,filesize($file));
541 $status2 = fclose($f);
543 return ($status && $status2);