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->libdir
. '/completionlib.php');
14 //Sets a name/value pair in config_plugin table
15 function backup_set_config($name, $value) {
16 return set_config($name, $value, 'backup');
19 //Gets all the information from config_plugin table
20 function backup_get_config() {
21 $backup_config = get_config('backup');
22 return (object)$backup_config;
25 //Delete old data in backup tables (if exists)
26 //Four hours seem to be appropiate now that backup is stable
27 function backup_delete_old_data() {
30 //Change this if you want !!
33 $seconds = $hours * 60 * 60;
34 $delete_from = time()-$seconds;
35 //Now delete from tables
36 $status = $DB->execute("DELETE FROM {backup_ids}
37 WHERE backup_code < ?", array($delete_from));
39 $status = $DB->execute("DELETE FROM {backup_files}
40 WHERE backup_code < ?", array($delete_from));
42 //Now, delete old directory (if exists)
44 $status = backup_delete_old_dirs($delete_from);
49 //Function to delete dirs/files into temp/backup directory
50 //older than $delete_from
51 function backup_delete_old_dirs($delete_from) {
56 //Get files and directories in the temp backup dir witout descend
57 $list = get_directory_list($CFG->tempdir
."/backup", "", false, true, true);
58 foreach ($list as $file) {
59 $file_path = $CFG->tempdir
."/backup/".$file;
60 $moddate = filemtime($file_path);
61 if ($status && $moddate < $delete_from) {
62 //If directory, recurse
63 if (is_dir($file_path)) {
64 $status = delete_dir_contents($file_path);
65 //There is nothing, delete the directory itself
67 $status = rmdir($file_path);
79 //Function to check and create the needed dir to
81 function check_and_create_backup_dir($backup_unique_code) {
84 $status = check_dir_exists($CFG->tempdir
."",true);
86 $status = check_dir_exists($CFG->tempdir
."/backup",true);
89 $status = check_dir_exists($CFG->tempdir
."/backup/".$backup_unique_code,true);
95 //Function to delete all the directory contents recursively
96 //it supports a excluded dit too
97 //Copied from the web !!
98 function delete_dir_contents ($dir,$excludeddir="") {
102 // if we've been given a directory that doesn't exist yet, return true.
103 // this happens when we're trying to clear out a course that has only just
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, $CFG->directorypermissions
);
116 if ((($handle = opendir($dir))) == FALSE) {
117 // The directory could not be opened
121 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
122 while (false !== ($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 $countdirfiles = count($dir_files);
133 for ($i=0; $i<$countdirfiles; $i++
) {
134 chmod($dir_files[$i], $CFG->directorypermissions
);
135 if (((unlink($dir_files[$i]))) == FALSE) {
140 // Empty sub directories and then remove the directory
141 $countdirsubdirs = count($dir_subdirs);
142 for($i=0; $i<$countdirsubdirs; $i++
) {
143 chmod($dir_subdirs[$i], $CFG->directorypermissions
);
144 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
148 if (remove_dir($dir_subdirs[$i]) == FALSE) {
157 // Success, every thing is gone return true
161 //Function to clear (empty) the contents of the backup_dir
162 function clear_backup_dir($backup_unique_code) {
165 $rootdir = $CFG->tempdir
."/backup/".$backup_unique_code;
168 $status = delete_dir_contents($rootdir);
173 //Returns the module type of a course_module's id in a course
174 function get_module_type ($courseid,$moduleid) {
177 $results = $DB->get_records_sql("SELECT cm.id, m.name
178 FROM {course_modules} cm, {modules} m
179 WHERE cm.course = ? AND cm.id = ? AND
180 m.id = cm.module", array($courseid, $moduleid));
183 $name = $results[$moduleid]->name
;
190 //This function return the names of all directories under a give directory
192 function list_directories ($rootdir) {
196 $dir = opendir($rootdir);
197 while (false !== ($file=readdir($dir))) {
198 if ($file=="." ||
$file=="..") {
201 if (is_dir($rootdir."/".$file)) {
202 $results[$file] = $file;
209 //This function return the names of all directories and files under a give directory
211 function list_directories_and_files ($rootdir) {
215 $dir = opendir($rootdir);
216 while (false !== ($file=readdir($dir))) {
217 if ($file=="." ||
$file=="..") {
220 $results[$file] = $file;
226 //This function clean data from backup tables and
227 //delete all temp files used
228 function clean_temp_data ($preferences) {
233 //true->do it, false->don't do it. To debug if necessary.
235 //Now delete from tables
236 $status = $DB->delete_records('backup_ids', array('backup_code'=>$preferences->backup_unique_code
))
237 && $DB->delete_records('backup_files', array('backup_code'=>$preferences->backup_unique_code
));
239 //Now, delete temp directory (if exists)
240 $file_path = $CFG->tempdir
."/backup/".$preferences->backup_unique_code
;
241 if (is_dir($file_path)) {
242 $status = delete_dir_contents($file_path);
243 //There is nothing, delete the directory itself
245 $status = rmdir($file_path);
252 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
253 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
254 //This functions are used to copy any file or directory ($from_file)
255 //to a new file or directory ($to_file). It works recursively and
256 //mantains file perms.
257 //I've copied it from: http://www.php.net/manual/en/function.copy.php
258 //Little modifications done
260 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
263 if (is_file($from_file)) {
264 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
265 //$perms=fileperms($from_file);
266 //return copy($from_file,$to_file) && chmod($to_file,$perms);
268 if (copy($from_file,$to_file)) {
269 chmod($to_file,$CFG->directorypermissions
);
270 if (!empty($log_clam)) {
271 //clam_log_upload($to_file,null,true);
277 else if (is_dir($from_file)) {
278 return backup_copy_dir($from_file,$to_file);
281 //echo "<br />Error: not file or dir ".$from_file; //Debug
286 function backup_copy_dir($from_file,$to_file) {
289 $status = true; // Initialize this, next code will change its value if needed
291 if (!is_dir($to_file)) {
292 //echo "<br />Creating ".$to_file; //Debug
294 $status = mkdir($to_file,$CFG->directorypermissions
);
296 $dir = opendir($from_file);
297 while (false !== ($file=readdir($dir))) {
298 if ($file=="." ||
$file=="..") {
301 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
306 ///Ends copy file/dirs functions
307 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
308 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
311 * Are we restoring a backup that was made on the same site that we are restoring to?
312 * This relies on some information that was only added to backup files in January 2009.
313 * For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains
314 * when this guess could give the wrong answer.
315 * @return boolean true if the backup was made on the same site we are restoring to.
317 function backup_is_same_site(&$restore) {
319 static $hashedsiteid = null;
320 if (is_null($hashedsiteid)) {
321 $hashedsiteid = md5(get_site_identifier());
323 if (!empty($restore->original_siteidentifier
)) {
324 return $restore->original_siteidentifier
== $hashedsiteid;
326 return $restore->original_wwwroot
== $CFG->wwwroot
;
330 //This function is used to insert records in the backup_ids table
331 //If the info field is greater than max_db_storage, then its info
332 //is saved to filesystem
333 function backup_putid($backup_unique_code, $table, $old_id, $new_id, $info="") {
336 $max_db_storage = 128; //Max bytes to save to db, else save to file
340 //First delete to avoid PK duplicates
341 $status = backup_delid($backup_unique_code, $table, $old_id);
343 //Now, serialize info
344 $info_ser = serialize($info);
346 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
347 //insert a "infile" in the info field
349 if (strlen($info_ser) > $max_db_storage) {
350 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
351 $filename = $CFG->tempdir
."/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
353 $status = backup_data2file($filename,$info_ser);
355 $info_to_save = "infile";
358 $info_to_save = $info_ser;
361 //Now, insert the record
364 $rec = new stdClass();
365 $rec->backup_code
= $backup_unique_code;
366 $rec->table_name
= $table;
367 $rec->old_id
= $old_id;
368 $rec->new_id
= ($new_id === null?
0 : $new_id);
369 $rec->info
= $info_to_save;
371 $DB->insert_record('backup_ids', $rec, false);
376 //This function is used to delete recods from the backup_ids table
377 //If the info field is "infile" then the file is deleted too
378 function backup_delid ($backup_unique_code, $table, $old_id) {
380 return $DB->delete_records('backup_ids', array('backup_code'=>$backup_unique_code, 'table_name'=>$table, 'old_id'=>$old_id));
383 //This function is used to get a record from the backup_ids table
384 //If the info field is "infile" then its info
385 //is read from filesystem
386 function backup_getid ($backup_unique_code, $table, $old_id) {
392 $status = $DB->get_record("backup_ids", array("backup_code"=>$backup_unique_code,
393 "table_name"=>$table, "old_id"=>$old_id));
395 //If info field = "infile", get file contents
396 if (!empty($status->info
) && $status->info
== "infile") {
397 $filename = $CFG->tempdir
."/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
398 //Read data from file
399 $status2 = backup_file2data($filename,$info);
402 $status->info
= unserialize($info);
407 //Only if status (record exists)
408 if (!empty($status->info
)) {
409 if ($status->info
=== 'needed') {
410 // TODO: ugly hack - fix before 1.9.1
411 debugging('Incorrect string "needed" in $status->info, please fix the code (table:'.$table.'; old_id:'.$old_id.').', DEBUG_DEVELOPER
);
413 ////First strip slashes
414 $temp = $status->info
;
416 $status->info
= unserialize($temp);
424 //This function is used to add slashes (and decode from UTF-8 if needed)
425 //It's used intensivelly when restoring modules and saving them in db
426 function backup_todb ($data) {
428 if ($data === '$@NULL@$') {
431 return restore_decode_absolute_links($data);
435 //This function is used to check that every necessary function to
436 //backup/restore exists in the current php installation. Thanks to
437 //gregb@crowncollege.edu by the idea.
438 function backup_required_functions($justcheck=false) {
440 if(!function_exists('utf8_encode')) {
441 if (empty($justcheck)) {
442 print_error('needphpext', '', '', 'XML');
451 //This function send n white characters to the browser and flush the
452 //output buffer. Used to avoid browser timeouts and to show the progress.
453 function backup_flush($n=0,$time=false) {
454 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
458 $ti = strftime("%X",time());
462 echo str_repeat(" ", $n) . $ti . "\n";
466 //This function creates the filename and write data to it
467 //returning status as result
468 function backup_data2file ($file,&$data) {
473 $f = fopen($file,"w");
474 $status = fwrite($f,$data);
475 $status2 = fclose($f);
477 return ($status && $status2);
480 //This function read the filename and read data from it
481 function backup_file2data ($file,&$data) {
486 $f = fopen($file,"r");
487 $data = fread ($f,filesize($file));
488 $status2 = fclose($f);
490 return ($status && $status2);