Updated to 0.6.1 alpha (tons of changes)
[moodle.git] / backup / lib.php
CommitLineData
afbe3de8 1<?PHP //$Id$
52376d94 2 //This file contains all the general function needed (file manipulation...)
3 //not directly part of the backup/restore utility
afbe3de8 4
b0778a76 5 //Delete old data in backup tables (if exists)
6 //Two days seems to be apropiate
7 function backup_delete_old_data() {
674b30f5 8
9 global $CFG;
10
b0778a76 11 //Change this if you want !!
32ad5774 12 $days = 2;
b0778a76 13 //End change this
674b30f5 14 $seconds = $days * 24 * 60 * 60;
b0778a76 15 $delete_from = time()-$seconds;
16 //Now delete from tables
674b30f5 17 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
18 WHERE backup_code < '$delete_from'",false);
19 if ($status) {
20 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
21 WHERE backup_code < '$delete_from'",false);
22 }
3b8bad6f 23 //Now, delete old directory (if exists)
24 if ($status) {
25 $status = backup_delete_old_dirs($delete_from);
26 }
b0778a76 27 return($status);
28 }
674b30f5 29
3b8bad6f 30 //Function to delete dirs/files into temp/backup directory
31 //older than $delete_from
32 function backup_delete_old_dirs($delete_from) {
33
34 global $CFG;
35
36 $status = true;
37 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false);
38 foreach ($list as $file) {
39 $file_path = $CFG->dataroot."/temp/backup/".$file;
40 $moddate = filemtime($file_path);
41 if ($status and $moddate < $delete_from) {
cfb9c525 42 //If directory, recurse
43 if (is_dir($file_path)) {
44 $status = delete_dir_contents($file_path);
45 //There is nothing, delete the directory itself
46 if ($status) {
47 $status = rmdir($file_path);
48 }
49 //If file
50 } else {
51 unlink("$file_path");
3b8bad6f 52 }
53 }
54 }
55
56 return $status;
57 }
58
674b30f5 59 //Function to check if a directory exists
60 //and, optionally, create it
61 function check_dir_exists($dir,$create=false) {
62
63 global $CFG;
64
65 $status = true;
66 if(!is_dir($dir)) {
67 if (!$create) {
68 $status = false;
69 } else {
1bdeb2ca 70 umask(0000);
674b30f5 71 $status = mkdir ($dir,$CFG->directorypermissions);
72 }
73 }
74 return $status;
75 }
76
77 //Function to check and create the needed dir to
78 //save all the backup
79 function check_and_create_backup_dir($backup_unique_code) {
80
81 global $CFG;
82
83 $status = check_dir_exists($CFG->dataroot."/temp",true);
84 if ($status) {
85 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
86 }
87 if ($status) {
88 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
89 }
90
91 return $status;
92 }
93
94 //Function to delete all the directory contents recursively
95 //Copied from admin/delete.php
96 function delete_dir_contents ($rootdir) {
97
98 $dir = opendir($rootdir);
99
100 $status = true;
101
102 while ($file = readdir($dir)) {
103 if ($file != "." and $file != "..") {
104 $fullfile = "$rootdir/$file";
105 if (filetype($fullfile) == "dir") {
106 delete_dir_contents($fullfile);
107 if (!rmdir($fullfile)) {
3b8bad6f 108 $status = false;
674b30f5 109 }
110 } else {
111 if (!unlink("$fullfile")) {
3b8bad6f 112 $status = false;
674b30f5 113 }
114 }
115 }
116 }
117 closedir($dir);
118
119 return $status;
120
121 }
122
123 //Function to clear (empty) the contents of the backup_dir
124 //Copied from admin/delete.php
125 function clear_backup_dir($backup_unique_code) {
126
127 global $CFG;
128
129 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
130
131 //Delete recursively
132 $status = delete_dir_contents($rootdir);
133
134 return $status;
135 }
3b8bad6f 136
cfb9c525 137 //Returns the module type of a course_module's id in a course
138 function get_module_type ($courseid,$moduleid) {
139
140 global $CFG;
141
142 $results = get_records_sql ("SELECT cm.id, m.name
143 FROM {$CFG->prefix}course_modules cm,
144 {$CFG->prefix}modules m
145 WHERE cm.course = '$courseid' AND
146 cm.id = '$moduleid' AND
147 m.id = cm.module");
148
149 if ($results) {
150 $name = $results[$moduleid]->name;
151 } else {
152 $name = false;
153 }
52376d94 154 return $name;
667d2f2a 155 }
156
157 //This function return the names of all directories under a give directory
158 //Not recursive
159 function list_directories ($rootdir) {
160
161 $dir = opendir($rootdir);
162 while ($file=readdir($dir)) {
163 if ($file=="." || $file=="..") {
164 continue;
165 }
166 if (is_dir($rootdir."/".$file)) {
167 $results[$file] = $file;
168 }
169 }
170 closedir($dir);
171 return $results;
172 }
173
174 //This function return the names of all directories and files under a give directory
175 //Not recursive
176 function list_directories_and_files ($rootdir) {
177
178 $dir = opendir($rootdir);
179 while ($file=readdir($dir)) {
180 if ($file=="." || $file=="..") {
181 continue;
182 }
183 $results[$file] = $file;
184 }
185 closedir($dir);
186 return $results;
187 }
188
47846965 189 //This function clean data from backup tables and
190 //delete all temp files used
191 function clean_temp_data ($preferences) {
192
47846965 193 global $CFG;
194
195 $status = true;
196
197 //true->do it, false->don't do it. To debug if necessary.
198 if (true) {
199 //Now delete from tables
200 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
af478c0b 201 WHERE backup_code = '$preferences->backup_unique_code'",false);
47846965 202 if ($status) {
203 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_files
af478c0b 204 WHERE backup_code = '$preferences->backup_unique_code'",false);
47846965 205 }
206 //Now, delete temp directory (if exists)
207 $file_path = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
208 if (is_dir($file_path)) {
209 $status = delete_dir_contents($file_path);
210 //There is nothing, delete the directory itself
211 if ($status) {
212 $status = rmdir($file_path);
213 }
214 }
215 }
216 return $status;
217 }
218
efead3b9 219 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
220 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
221 //This functions are used to copy any file or directory ($from_file)
222 //to a new file or directory ($to_file). It works recursively and
223 //mantains file perms.
224 //I've copied it from: http://www.php.net/manual/en/function.copy.php
225 //Little modifications done
226
227 function backup_copy_file ($from_file,$to_file) {
228 if (is_file($from_file)) {
229 $perms=fileperms($from_file);
230 return copy($from_file,$to_file) && chmod($to_file,$perms);
231 }
232 else if (is_dir($from_file)) {
233 return backup_copy_dir($from_file,$to_file);
234 }
235 else{
236 return false;
237 }
cfb9c525 238 }
efead3b9 239
240 function backup_copy_dir($from_file,$to_file) {
241 if (!is_dir($to_file)) {
242 mkdir($to_file);
243 chmod("$to_file",0777);
244 }
245 $dir = opendir($from_file);
246 while ($file=readdir($dir)) {
247 if ($file=="." || $file=="..") {
248 continue;
249 }
4f6ae69e 250 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
efead3b9 251 }
4f6ae69e 252 closedir($dir);
253 return $status;
efead3b9 254 }
255 ///Ends copy file/dirs functions
256 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
257 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7641819d 258
259 //This function upgrades, if necesary, the backup-restore tables
260 //It's called from backup.php and restore.php
261 function upgrade_backup_db($updgradeto,$backup_release,$continueto) {
262
263 global $CFG,$db;
264
265 //Check backup_version
266 if ($CFG->backup_version) {
267 if ($updgradeto > $CFG->backup_version) { // upgrade
268 $a->oldversion = $CFG->backup_version;
269 $a->newversion = $updgradeto;
270 $strdatabasechecking = get_string("databasechecking", "", $a);
271 $strdatabasesuccess = get_string("databasesuccess");
272 print_header($strdatabasechecking, $strdatabasechecking, $strdatabasechecking);
273 print_heading($strdatabasechecking);
274 $db->debug=true;
275 if (backup_upgrade($a->oldversion)) {
276 $db->debug=false;
277 if (set_config("backup_version", $a->newversion)) {
278 notify($strdatabasesuccess, "green");
279 notify("You are running Backup/Recovery version ".$backup_release,"black");
280 print_continue($continueto);
281 die;
282 } else {
283 notify("Upgrade failed! (Could not update version in config table)");
284 die;
285 }
286 } else {
287 $db->debug=false;
288 notify("Upgrade failed! See backup_version.php");
289 die;
290 }
291 } else if ($updgradeto < $CFG->backup_version) {
292 notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
293 }
294 //Not exists. Starting installation
295 } else {
296 $strdatabaseupgrades = get_string("databaseupgrades");
297 print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades);
298
299 if (set_config("backup_version", "2003010100")) {
300 print_heading("You are currently going to install the needed structures to Backup/Recover");
301 print_continue($continue_to);
302 die;
303 }
304 }
305 }
7ba74615 306
307 //This function is used to insert records in the backup_ids table
308 function backup_putid ($backup_unique_code, $table, $old_id, $new_id, $info="") {
309
310 global $CFG;
311
312 $status = true;
313
314 //First delete to avoid PK duplicates
315 $status = backup_delid($backup_unique_code, $table, $old_id);
316
317 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids
318 (backup_code, table_name, old_id, new_id, info)
319 VALUES
320 ($backup_unique_code, '$table', '$old_id', '$new_id', '$info')",false);
321 return $status;
322 }
323
324 //This function is used to delete recods from the backup_ids table
325 function backup_delid ($backup_unique_code, $table, $old_id) {
326
327 global $CFG;
328
329 $status = true;
330
331 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
332 WHERE backup_code = $backup_unique_code AND
333 table_name = '$table' AND
334 old_id = '$old_id'",false);
335 return $status;
336 }
a2c7397c 337
338 //This function is used to get a record from the backup_ids table
339 function backup_getid ($backup_unique_code, $table, $old_id) {
340
341 global $CFG;
342
343 $status = true;
344
345 $status = get_record ("backup_ids","backup_code",$backup_unique_code,
346 "table_name",$table,
347 "old_id", $old_id);
348
349 return $status;
350 }
6d18c5a2 351
352 //This function is used to add slashes and decode from UTF-8
353 //It's used intensivelly when restoring modules and saving them in db
354 function backup_todb ($data) {
355 return addslashes(utf8_decode($data));
356 }
357
fe3c84ab 358 //This function is used to check that every necessary function to
359 //backup/restore exists in the current php installation. Thanks to
360 //gregb@crowncollege.edu by the idea.
361 function backup_required_functions() {
362
d2c94f4b 363 if(!function_exists('utf8_encode')) {
fe3c84ab 364 error('You need to add utf8 support to your PHP installation');
365 }
366
367 }
60b420af 368
369 //This function send n white characters to the browser and flush the
370 //output buffer. Used to avoid browser timeouts and to show the progress.
371 function backup_flush($n=0,$time=false) {
372 if ($time) {
373 $ti = strftime("%X",time());
374 } else {
375 $ti = "";
376 }
377 echo str_repeat(" ", $n) . $ti . "\n";
378 flush();
379 }
fe3c84ab 380
381
afbe3de8 382?>