file courseidnumber.html was added on branch MOODLE_15_STABLE on 2005-07-07 17:12...
[moodle.git] / backup / lib.php
blob6658490c52cffce62fa2d62261f88206b201821f
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", $value, "name", $name);
11 } else {
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() {
33 global $CFG;
35 //Change this if you want !!
36 $hours = 4;
37 //End change this
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);
43 if ($status) {
44 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
45 WHERE backup_code < '$delete_from'",false);
47 //Now, delete old directory (if exists)
48 if ($status) {
49 $status = backup_delete_old_dirs($delete_from);
51 return($status);
54 //Function to delete dirs/files into temp/backup directory
55 //older than $delete_from
56 function backup_delete_old_dirs($delete_from) {
58 global $CFG;
60 $status = true;
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
71 if ($status) {
72 $status = rmdir($file_path);
74 //If file
75 } else {
76 unlink("$file_path");
81 return $status;
84 //Function to check if a directory exists
85 //and, optionally, create it
86 function check_dir_exists($dir,$create=false) {
88 global $CFG;
90 $status = true;
91 if(!is_dir($dir)) {
92 if (!$create) {
93 $status = false;
94 } else {
95 umask(0000);
96 $status = mkdir ($dir,$CFG->directorypermissions);
99 return $status;
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) {
106 global $CFG;
108 $status = check_dir_exists($CFG->dataroot."/temp",true);
109 if ($status) {
110 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
112 if ($status) {
113 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
116 return $status;
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="") {
124 $slash = "/";
126 // Create arrays to store files and directories
127 $dir_files = array();
128 $dir_subdirs = array();
130 // Make sure we can delete it
131 chmod($dir, 0777);
133 if ((($handle = opendir($dir))) == FALSE) {
134 // The directory could not be opened
135 return false;
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) {
152 return 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) {
160 return false;
162 else {
163 if (rmdir($dir_subdirs[$i]) == FALSE) {
164 return false;
169 // Close directory
170 closedir($handle);
172 // Success, every thing is gone return true
173 return true;
176 //Function to clear (empty) the contents of the backup_dir
177 function clear_backup_dir($backup_unique_code) {
179 global $CFG;
181 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
183 //Delete recursively
184 $status = delete_dir_contents($rootdir);
186 return $status;
189 //Returns the module type of a course_module's id in a course
190 function get_module_type ($courseid,$moduleid) {
192 global $CFG;
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
199 m.id = cm.module");
201 if ($results) {
202 $name = $results[$moduleid]->name;
203 } else {
204 $name = false;
206 return $name;
209 //This function return the names of all directories under a give directory
210 //Not recursive
211 function list_directories ($rootdir) {
213 $results = null;
215 $dir = opendir($rootdir);
216 while ($file=readdir($dir)) {
217 if ($file=="." || $file=="..") {
218 continue;
220 if (is_dir($rootdir."/".$file)) {
221 $results[$file] = $file;
224 closedir($dir);
225 return $results;
228 //This function return the names of all directories and files under a give directory
229 //Not recursive
230 function list_directories_and_files ($rootdir) {
232 $results = "";
234 $dir = opendir($rootdir);
235 while ($file=readdir($dir)) {
236 if ($file=="." || $file=="..") {
237 continue;
239 $results[$file] = $file;
241 closedir($dir);
242 return $results;
245 //This function clean data from backup tables and
246 //delete all temp files used
247 function clean_temp_data ($preferences) {
249 global $CFG;
251 $status = true;
253 //true->do it, false->don't do it. To debug if necessary.
254 if (true) {
255 //Now delete from tables
256 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
257 WHERE backup_code = '$preferences->backup_unique_code'",false);
258 if ($status) {
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
267 if ($status) {
268 $status = rmdir($file_path);
272 return $status;
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) {
285 global $CFG;
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);
291 umask(0000);
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);
296 return true;
298 return false;
300 else if (is_dir($from_file)) {
301 return backup_copy_dir($from_file,$to_file);
303 else{
304 //echo "<br />Error: not file or dir ".$from_file; //Debug
305 return false;
309 function backup_copy_dir($from_file,$to_file) {
311 global $CFG;
313 if (!is_dir($to_file)) {
314 //echo "<br />Creating ".$to_file; //Debug
315 umask(0000);
316 $status = mkdir($to_file,$CFG->directorypermissions);
318 $dir = opendir($from_file);
319 while ($file=readdir($dir)) {
320 if ($file=="." || $file=="..") {
321 continue;
323 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
325 closedir($dir);
326 return $status;
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
337 global $CFG, $db;
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, "&nbsp;", "&nbsp;");
346 $db->debug=true;
347 if (modify_database("$CFG->dirroot/backup/db/$CFG->dbtype.sql")) {
348 $db->debug = false;
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);
353 exit;
354 } else {
355 error("Upgrade of backup system failed! (Could not update version in config table)");
357 } else {
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");
369 $db->debug=true;
370 if (backup_upgrade($CFG->backup_version)) {
371 $db->debug=false;
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);
376 exit;
377 } else {
378 error("Upgrade of backup system failed! (Could not update version in config table)");
380 } else {
381 $db->debug=false;
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="") {
396 global $CFG;
398 $max_db_storage = 128; //Max bytes to save to db, else save to file
400 $status = true;
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";
414 //Save data to file
415 $status = backup_data2file($filename,$info_ser);
416 //Set info_to save
417 $info_to_save = "infile";
418 } else {
419 //Saving to db, addslashes
420 $info_to_save = addslashes($info_ser);
423 //Now, insert the record
424 if ($status) {
425 //Build 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");
434 return $status;
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) {
441 global $CFG;
443 $status = true;
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);
449 return $status;
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) {
457 global $CFG;
459 $status = true;
460 $status2 = true;
462 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
463 "table_name",$table,
464 "old_id", $old_id);
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);
471 if ($status2) {
472 //unserialize data
473 $status->info = unserialize($info);
474 } else {
475 $status = false;
477 } else {
478 //Only if status (record exists)
479 if ($status) {
480 ////First strip slashes
481 $temp = stripslashes($status->info);
482 //Now unserialize
483 $status->info = unserialize($temp);
487 return $status;
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) {
510 if ($time) {
511 $ti = strftime("%X",time());
512 } else {
513 $ti = "";
515 echo str_repeat(" ", $n) . $ti . "\n";
516 flush();
519 //This function creates the filename and write data to it
520 //returning status as result
521 function backup_data2file ($file,&$data) {
523 $status = true;
524 $status2 = true;
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) {
536 $status = true;
537 $status2 = true;
539 $f = fopen($file,"r");
540 $data = fread ($f,filesize($file));
541 $status2 = fclose($f);
543 return ($status && $status2);