MDL-21915 fixing remaining chmod and mkdir to use moodle file permissions
[moodle.git] / backup / lib.php
CommitLineData
795b6945 1<?php
52376d94 2 //This file contains all the general function needed (file manipulation...)
64acc5a5 3 //not directly part of the backup/restore utility plus some constants
4
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);
afbe3de8 11
4e1132a8 12 require_once($CFG->libdir . '/completionlib.php');
8930f900 13
eb6a973c 14 //Sets a name/value pair in config_plugin table
de4746a8 15 function backup_set_config($name, $value) {
eb6a973c 16 return set_config($name, $value, 'backup');
de4746a8 17 }
18
eb6a973c 19 //Gets all the information from config_plugin table
de4746a8 20 function backup_get_config() {
eb6a973c 21 $backup_config = get_config('backup');
49b95c30 22 return (object)$backup_config;
de4746a8 23 }
24
b0778a76 25 //Delete old data in backup tables (if exists)
dfd02290 26 //Four hours seem to be appropiate now that backup is stable
b0778a76 27 function backup_delete_old_data() {
ad4375ac 28 global $CFG, $DB;
674b30f5 29
b0778a76 30 //Change this if you want !!
dfd02290 31 $hours = 4;
b0778a76 32 //End change this
dfd02290 33 $seconds = $hours * 60 * 60;
b0778a76 34 $delete_from = time()-$seconds;
35 //Now delete from tables
ad4375ac 36 $status = $DB->execute("DELETE FROM {backup_ids}
37 WHERE backup_code < ?", array($delete_from));
674b30f5 38 if ($status) {
ad4375ac 39 $status = $DB->execute("DELETE FROM {backup_files}
40 WHERE backup_code < ?", array($delete_from));
674b30f5 41 }
3b8bad6f 42 //Now, delete old directory (if exists)
43 if ($status) {
44 $status = backup_delete_old_dirs($delete_from);
45 }
b0778a76 46 return($status);
47 }
674b30f5 48
3b8bad6f 49 //Function to delete dirs/files into temp/backup directory
50 //older than $delete_from
51 function backup_delete_old_dirs($delete_from) {
52
53 global $CFG;
54
55 $status = true;
41bac9d0 56 //Get files and directories in the temp backup dir witout descend
57 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false, true, true);
3b8bad6f 58 foreach ($list as $file) {
59 $file_path = $CFG->dataroot."/temp/backup/".$file;
60 $moddate = filemtime($file_path);
af9cd955 61 if ($status && $moddate < $delete_from) {
cfb9c525 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
66 if ($status) {
67 $status = rmdir($file_path);
68 }
69 //If file
70 } else {
71 unlink("$file_path");
3b8bad6f 72 }
73 }
74 }
75
76 return $status;
77 }
78
3bee1ead 79 //Function to check and create the needed dir to
674b30f5 80 //save all the backup
81 function check_and_create_backup_dir($backup_unique_code) {
3bee1ead 82 global $CFG;
674b30f5 83
84 $status = check_dir_exists($CFG->dataroot."/temp",true);
85 if ($status) {
86 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
87 }
88 if ($status) {
89 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
90 }
3bee1ead 91
674b30f5 92 return $status;
93 }
94
95 //Function to delete all the directory contents recursively
35ea1594 96 //it supports a excluded dit too
af9cd955 97 //Copied from the web !!
35ea1594 98 function delete_dir_contents ($dir,$excludeddir="") {
4756e9c9 99 global $CFG;
674b30f5 100
3bee1ead 101 if (!is_dir($dir)) {
9e3775db 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
104 // been created.
105 return true;
106 }
af9cd955 107 $slash = "/";
674b30f5 108
af9cd955 109 // Create arrays to store files and directories
110 $dir_files = array();
111 $dir_subdirs = array();
674b30f5 112
af9cd955 113 // Make sure we can delete it
4756e9c9 114 chmod($dir, $CFG->directorypermissions);
af9cd955 115
116 if ((($handle = opendir($dir))) == FALSE) {
117 // The directory could not be opened
118 return false;
119 }
120
121 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
ed818bbd 122 while(false !== ($entry = readdir($handle))) {
35ea1594 123 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
af9cd955 124 $dir_subdirs[] = $dir. $slash .$entry;
125 }
35ea1594 126 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
af9cd955 127 $dir_files[] = $dir. $slash .$entry;
128 }
129 }
130
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++) {
4756e9c9 133 chmod($dir_files[$i], $CFG->directorypermissions);
af9cd955 134 if (((unlink($dir_files[$i]))) == FALSE) {
135 return false;
136 }
137 }
138
139 // Empty sub directories and then remove the directory
140 for($i=0; $i<count($dir_subdirs); $i++) {
4756e9c9 141 chmod($dir_subdirs[$i], $CFG->directorypermissions);
af9cd955 142 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
143 return false;
144 }
145 else {
907ef7f8 146 if (remove_dir($dir_subdirs[$i]) == FALSE) {
af9cd955 147 return false;
674b30f5 148 }
149 }
150 }
674b30f5 151
af9cd955 152 // Close directory
153 closedir($handle);
154
155 // Success, every thing is gone return true
156 return true;
674b30f5 157 }
158
159 //Function to clear (empty) the contents of the backup_dir
674b30f5 160 function clear_backup_dir($backup_unique_code) {
3bee1ead 161 global $CFG;
674b30f5 162
163 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
3bee1ead 164
674b30f5 165 //Delete recursively
166 $status = delete_dir_contents($rootdir);
167
168 return $status;
169 }
3b8bad6f 170
3bee1ead 171 //Returns the module type of a course_module's id in a course
cfb9c525 172 function get_module_type ($courseid,$moduleid) {
ad4375ac 173 global $DB;
cfb9c525 174
ad4375ac 175 $results = $DB->get_records_sql("SELECT cm.id, m.name
176 FROM {course_modules} cm, {modules} m
177 WHERE cm.course = ? AND cm.id = ? AND
178 m.id = cm.module", array($courseid, $moduleid));
cfb9c525 179
180 if ($results) {
181 $name = $results[$moduleid]->name;
182 } else {
183 $name = false;
184 }
52376d94 185 return $name;
667d2f2a 186 }
187
188 //This function return the names of all directories under a give directory
189 //Not recursive
190 function list_directories ($rootdir) {
97a49e63 191
192 $results = null;
3bee1ead 193
667d2f2a 194 $dir = opendir($rootdir);
ed818bbd 195 while (false !== ($file=readdir($dir))) {
667d2f2a 196 if ($file=="." || $file=="..") {
197 continue;
198 }
199 if (is_dir($rootdir."/".$file)) {
200 $results[$file] = $file;
201 }
202 }
3bee1ead 203 closedir($dir);
204 return $results;
667d2f2a 205 }
206
207 //This function return the names of all directories and files under a give directory
208 //Not recursive
209 function list_directories_and_files ($rootdir) {
3a4b33c3 210
211 $results = "";
3bee1ead 212
667d2f2a 213 $dir = opendir($rootdir);
ed818bbd 214 while (false !== ($file=readdir($dir))) {
667d2f2a 215 if ($file=="." || $file=="..") {
216 continue;
217 }
218 $results[$file] = $file;
219 }
3bee1ead 220 closedir($dir);
667d2f2a 221 return $results;
222 }
223
47846965 224 //This function clean data from backup tables and
225 //delete all temp files used
226 function clean_temp_data ($preferences) {
ad4375ac 227 global $CFG, $DB;
47846965 228
229 $status = true;
230
231 //true->do it, false->don't do it. To debug if necessary.
232 if (true) {
233 //Now delete from tables
ad4375ac 234 $status = $DB->delete_records('backup_ids', array('backup_code'=>$preferences->backup_unique_code))
235 && $DB->delete_records('backup_files', array('backup_code'=>$preferences->backup_unique_code));
236
47846965 237 //Now, delete temp directory (if exists)
238 $file_path = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
239 if (is_dir($file_path)) {
240 $status = delete_dir_contents($file_path);
241 //There is nothing, delete the directory itself
242 if ($status) {
243 $status = rmdir($file_path);
244 }
245 }
246 }
247 return $status;
248 }
249
efead3b9 250 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
252 //This functions are used to copy any file or directory ($from_file)
253 //to a new file or directory ($to_file). It works recursively and
254 //mantains file perms.
255 //I've copied it from: http://www.php.net/manual/en/function.copy.php
256 //Little modifications done
3bee1ead 257
d27920b9 258 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
f654deea 259 global $CFG;
260
efead3b9 261 if (is_file($from_file)) {
7ef0797d 262 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
f654deea 263 //$perms=fileperms($from_file);
264 //return copy($from_file,$to_file) && chmod($to_file,$perms);
265 umask(0000);
9704c866 266 if (copy($from_file,$to_file)) {
267 chmod($to_file,$CFG->directorypermissions);
d27920b9 268 if (!empty($log_clam)) {
f728d49b 269 //clam_log_upload($to_file,null,true);
d27920b9 270 }
8930f900 271 return true;
272 }
273 return false;
efead3b9 274 }
275 else if (is_dir($from_file)) {
276 return backup_copy_dir($from_file,$to_file);
277 }
278 else{
7ef0797d 279 //echo "<br />Error: not file or dir ".$from_file; //Debug
efead3b9 280 return false;
281 }
cfb9c525 282 }
efead3b9 283
284 function backup_copy_dir($from_file,$to_file) {
93808667 285 global $CFG;
286
615b4a13 287 $status = true; // Initialize this, next code will change its value if needed
288
efead3b9 289 if (!is_dir($to_file)) {
7ef0797d 290 //echo "<br />Creating ".$to_file; //Debug
f654deea 291 umask(0000);
795b6945 292 $status = mkdir($to_file,$CFG->directorypermissions);
efead3b9 293 }
294 $dir = opendir($from_file);
ed818bbd 295 while (false !== ($file=readdir($dir))) {
efead3b9 296 if ($file=="." || $file=="..") {
297 continue;
298 }
4f6ae69e 299 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
efead3b9 300 }
4f6ae69e 301 closedir($dir);
302 return $status;
efead3b9 303 }
304 ///Ends copy file/dirs functions
305 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
306 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7641819d 307
d61f7b76 308 /**
309 * Are we restoring a backup that was made on the same site that we are restoring to?
310 * This relies on some information that was only added to backup files in January 2009.
311 * For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains
312 * when this guess could give the wrong answer.
313 * @return boolean true if the backup was made on the same site we are restoring to.
314 */
315 function backup_is_same_site(&$restore) {
316 global $CFG;
317 static $hashedsiteid = null;
318 if (is_null($hashedsiteid)) {
319 $hashedsiteid = md5(get_site_identifier());
320 }
321 if (!empty($restore->original_siteidentifier)) {
322 return $restore->original_siteidentifier == $hashedsiteid;
323 } else {
324 return $restore->original_wwwroot == $CFG->wwwroot;
325 }
326 }
10dcde40 327
7ba74615 328 //This function is used to insert records in the backup_ids table
af9cd955 329 //If the info field is greater than max_db_storage, then its info
330 //is saved to filesystem
ad4375ac 331 function backup_putid($backup_unique_code, $table, $old_id, $new_id, $info="") {
332 global $CFG, $DB;
af9cd955 333
334 $max_db_storage = 128; //Max bytes to save to db, else save to file
3bee1ead 335
7ba74615 336 $status = true;
3bee1ead 337
7ba74615 338 //First delete to avoid PK duplicates
339 $status = backup_delid($backup_unique_code, $table, $old_id);
340
af9cd955 341 //Now, serialize info
342 $info_ser = serialize($info);
343
3bee1ead 344 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
af9cd955 345 //insert a "infile" in the info field
346
347 if (strlen($info_ser) > $max_db_storage) {
348 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
349 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
350 //Save data to file
3bee1ead 351 $status = backup_data2file($filename,$info_ser);
af9cd955 352 //Set info_to save
353 $info_to_save = "infile";
354 } else {
294ce987 355 //Saving to db
ad4375ac 356 $info_to_save = $info_ser;
af9cd955 357 }
358
359 //Now, insert the record
360 if ($status) {
d8e24b78 361 //Build the record
e894efc3 362 $rec = new stdClass();
d8e24b78 363 $rec->backup_code = $backup_unique_code;
364 $rec->table_name = $table;
365 $rec->old_id = $old_id;
062a84a6 366 $rec->new_id = ($new_id === null? 0 : $new_id);
d8e24b78 367 $rec->info = $info_to_save;
15a6cf33 368
fc29e51b 369 $DB->insert_record('backup_ids', $rec, false);
af9cd955 370 }
7ba74615 371 return $status;
372 }
373
374 //This function is used to delete recods from the backup_ids table
af9cd955 375 //If the info field is "infile" then the file is deleted too
7ba74615 376 function backup_delid ($backup_unique_code, $table, $old_id) {
ad4375ac 377 global $DB;
a4e315c6 378 return $DB->delete_records('backup_ids', array('backup_code'=>$backup_unique_code, 'table_name'=>$table, 'old_id'=>$old_id));
7ba74615 379 }
a2c7397c 380
381 //This function is used to get a record from the backup_ids table
af9cd955 382 //If the info field is "infile" then its info
383 //is read from filesystem
a2c7397c 384 function backup_getid ($backup_unique_code, $table, $old_id) {
ad4375ac 385 global $CFG, $DB;
a2c7397c 386
387 $status = true;
af9cd955 388 $status2 = true;
a2c7397c 389
795b6945 390 $status = $DB->get_record("backup_ids", array("backup_code"=>$backup_unique_code,
ad4375ac 391 "table_name"=>$table, "old_id"=>$old_id));
a2c7397c 392
af9cd955 393 //If info field = "infile", get file contents
9a7538a3 394 if (!empty($status->info) && $status->info == "infile") {
af9cd955 395 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
396 //Read data from file
397 $status2 = backup_file2data($filename,$info);
398 if ($status2) {
399 //unserialize data
400 $status->info = unserialize($info);
401 } else {
402 $status = false;
403 }
404 } else {
783ab2b0 405 //Only if status (record exists)
7f9bd149 406 if (!empty($status->info)) {
795b6945
PS
407 if ($status->info === 'needed') {
408 // TODO: ugly hack - fix before 1.9.1
7f9bd149 409 debugging('Incorrect string "needed" in $status->info, please fix the code (table:'.$table.'; old_id:'.$old_id.').', DEBUG_DEVELOPER);
410 } else {
411 ////First strip slashes
ad4375ac 412 $temp = $status->info;
7f9bd149 413 //Now unserialize
414 $status->info = unserialize($temp);
415 }
783ab2b0 416 }
af9cd955 417 }
418
a2c7397c 419 return $status;
420 }
6d18c5a2 421
fd5ca378 422 //This function is used to add slashes (and decode from UTF-8 if needed)
6d18c5a2 423 //It's used intensivelly when restoring modules and saving them in db
9db7dab2 424 function backup_todb ($data) {
e814e7d7 425 // MDL-10770
f23dea66 426 if ($data === '$@NULL@$') {
e295df44 427 return null;
e814e7d7 428 } else {
7f9bd149 429 return restore_decode_absolute_links($data);
e814e7d7 430 }
6d18c5a2 431 }
432
3bee1ead 433 //This function is used to check that every necessary function to
fe3c84ab 434 //backup/restore exists in the current php installation. Thanks to
435 //gregb@crowncollege.edu by the idea.
f90666aa 436 function backup_required_functions($justcheck=false) {
fe3c84ab 437
d2c94f4b 438 if(!function_exists('utf8_encode')) {
f90666aa 439 if (empty($justcheck)) {
5832a6f3 440 print_error('needphpext', '', '', 'XML');
f90666aa 441 } else {
442 return false;
443 }
fe3c84ab 444 }
445
f90666aa 446 return true;
fe3c84ab 447 }
60b420af 448
449 //This function send n white characters to the browser and flush the
450 //output buffer. Used to avoid browser timeouts and to show the progress.
451 function backup_flush($n=0,$time=false) {
f90666aa 452 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
453 return;
454 }
60b420af 455 if ($time) {
456 $ti = strftime("%X",time());
457 } else {
458 $ti = "";
459 }
460 echo str_repeat(" ", $n) . $ti . "\n";
461 flush();
462 }
3bee1ead 463
af9cd955 464 //This function creates the filename and write data to it
465 //returning status as result
466 function backup_data2file ($file,&$data) {
467
468 $status = true;
469 $status2 = true;
3bee1ead 470
af9cd955 471 $f = fopen($file,"w");
472 $status = fwrite($f,$data);
473 $status2 = fclose($f);
474
475 return ($status && $status2);
476 }
fe3c84ab 477
af9cd955 478 //This function read the filename and read data from it
479 function backup_file2data ($file,&$data) {
480
481 $status = true;
482 $status2 = true;
483
484 $f = fopen($file,"r");
485 $data = fread ($f,filesize($file));
486 $status2 = fclose($f);
487
488 return ($status && $status2);
489 }
f90666aa 490
491 /** this function will restore an entire backup.zip into the specified course
492 * using standard moodle backup/restore functions, but silently.
493 * @param string $pathtofile the absolute path to the backup file.
494 * @param int $destinationcourse the course id to restore to.
495 * @param boolean $emptyfirst whether to delete all coursedata first.
496 * @param boolean $userdata whether to include any userdata that may be in the backup file.
9e3775db 497 * @param array $preferences optional, 0 will be used. Can contain:
498 * metacourse
499 * logs
500 * course_files
501 * messages
f90666aa 502 */
9e3775db 503 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
ad4375ac 504 global $CFG,$SESSION,$USER, $DB; // is there such a thing on cron? I guess so..
f90666aa 505
062173b0 506 if (!defined('RESTORE_SILENTLY')) {
507 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
508 }
3bee1ead 509
f90666aa 510 $debuginfo = 'import_backup_file_silently: ';
511 $cleanupafter = false;
512 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
513
70fbfb2c
PL
514 $course = null;
515 if ($destinationcourse && !$course = $DB->get_record('course', array('id'=>$destinationcourse))) {
f90666aa 516 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
517 return false;
518 }
519
520 // first check we have a valid file.
521 if (!file_exists($pathtofile) || !is_readable($pathtofile)) {
522 mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
523 return false;
524 }
525
526 // now make sure it's a zip file
527 require_once($CFG->dirroot.'/lib/filelib.php');
528 $filename = substr($pathtofile,strrpos($pathtofile,'/')+1);
529 $mimetype = mimeinfo("type", $filename);
530 if ($mimetype != 'application/zip') {
531 mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
532 return false;
533 }
534
535 // restore_precheck wants this within dataroot, so lets put it there if it's not already..
536 if (strstr($pathtofile,$CFG->dataroot) === false) {
537 // first try and actually move it..
538 if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) {
539 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
540 return false;
541 }
542 if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) {
543 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
544 return false;
545 } else {
546 $pathtofile = 'temp/backup/'.$filename;
547 $cleanupafter = true;
548 }
549 } else {
550 // it is within dataroot, so take it off the path for restore_precheck.
551 $pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/'));
552 }
553
554 if (!backup_required_functions()) {
555 mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
556 return false;
557 }
f90666aa 558 @ini_set("max_execution_time","3000");
1ed8e660 559 if (empty($CFG->extramemorylimit)) {
7022dd39 560 raise_memory_limit('128M');
561 } else {
1ed8e660 562 raise_memory_limit($CFG->extramemorylimit);
7022dd39 563 }
3bee1ead 564
f90666aa 565 if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
566 mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
567 return false;
568 }
3bee1ead 569
70fbfb2c
PL
570 global $restore; // ick
571 $restore = new StdClass;
572 // copy back over the stuff that gets set in restore_precheck
573 $restore->course_header = $SESSION->course_header;
574 $restore->info = $SESSION->info;
575
576 $xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml";
577 $info = restore_read_xml_roles($xmlfile);
578 $restore->rolesmapping = array();
579 if (isset($info->roles) && is_array($info->roles)) {
580 foreach ($info->roles as $id => $info) {
8d3950ea 581 if ($newroleid = $DB->get_field('role', 'id', array('shortname' => $info->shortname))) {
70fbfb2c
PL
582 $restore->rolesmapping[$id] = $newroleid;
583 }
584 }
585 }
f90666aa 586
587 // add on some extra stuff we need...
70fbfb2c
PL
588 $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
589 $restore->course_id = $destinationcourse;
590 if ($destinationcourse) {
591 $restore->restoreto = RESTORETO_CURRENT_ADDING;
592 $restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate;
593 } else {
594 $restore->restoreto = RESTORETO_NEW_COURSE;
595 $restore->restore_restorecatto = 0; // let this be handled by the headers
596 $restore->course_startdateoffset = 0;
597
598 }
599
600 $restore->users = $userdata;
601 $restore->user_files = $userdata;
602 $restore->deleting = $emptyfirst;
603
604 $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
605 $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
606 $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
607 $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
608 $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
609 $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
610
611 $restore->backup_version = $restore->info->backup_backup_version;
612 $restore->original_wwwroot = $restore->info->original_wwwroot;
613
614 // now copy what we have over to the session
615 // this needs to happen before restore_setup_for_check
616 // which for some reason reads the session
617 $SESSION->restore =& $restore;
2f67a9b3 618 // rename the things that are called differently
70fbfb2c
PL
619 $SESSION->restore->restore_course_files = $restore->course_files;
620 $SESSION->restore->restore_site_files = $restore->site_files;
621 $SESSION->restore->backup_version = $restore->info->backup_backup_version;
622
623 restore_setup_for_check($restore, $backup_unique_code);
624
625 // maybe we need users (defaults to 2 (none) in restore_setup_for_check)
626 // so set this again here
f90666aa 627 if (!empty($userdata)) {
70fbfb2c 628 $restore->users = 1;
f90666aa 629 }
630
631 // we also need modules...
ad4375ac 632 if ($allmods = $DB->get_records("modules")) {
f90666aa 633 foreach ($allmods as $mod) {
634 $modname = $mod->name;
635 //Now check that we have that module info in the backup file
70fbfb2c
PL
636 if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") {
637 $restore->mods[$modname]->restore = true;
638 $restore->mods[$modname]->userinfo = $userdata;
f90666aa 639 }
9e3775db 640 else {
641 // avoid warnings
70fbfb2c
PL
642 $restore->mods[$modname]->restore = false;
643 $restore->mods[$modname]->userinfo = false;
9e3775db 644 }
f90666aa 645 }
646 }
70fbfb2c 647 if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) {
f90666aa 648 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
649 return false;
650 }
70fbfb2c
PL
651 // now get out the new courseid and return that
652 if ($restore->restoreto = RESTORETO_NEW_COURSE) {
653 if (!empty($SESSION->restore->course_id)) {
654 return $SESSION->restore->course_id;
655 }
656 return false;
657 }
f90666aa 658 return true;
659 }
660
3bee1ead 661 /**
9e3775db 662 * Function to backup an entire course silently and create a zipfile.
3bee1ead 663 *
664 * @param int $courseid the id of the course
9e3775db 665 * @param array $prefs see {@link backup_generate_preferences_artificially}
666 */
667 function backup_course_silently($courseid, $prefs, &$errorstring) {
ad4375ac 668 global $CFG, $preferences, $DB; // global preferences here because something else wants it :(
062173b0 669 if (!defined('BACKUP_SILENTLY')) {
670 define('BACKUP_SILENTLY', 1);
671 }
ad4375ac 672 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
9e3775db 673 debugging("Couldn't find course with id $courseid in backup_course_silently");
674 return false;
675 }
3bee1ead 676 $preferences = backup_generate_preferences_artificially($course, $prefs);
70fbfb2c 677 $preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0;
9e3775db 678 if (backup_execute($preferences, $errorstring)) {
679 return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name;
680 }
681 else {
682 return false;
683 }
684 }
685
686 /**
3bee1ead 687 * Function to generate the $preferences variable that
9e3775db 688 * backup uses. This will back up all modules and instances in a course.
3bee1ead 689 *
9e3775db 690 * @param object $course course object
691 * @param array $prefs can contain:
692 backup_metacourse
693 backup_users
694 backup_logs
695 backup_user_files
696 backup_course_files
3bee1ead 697 backup_site_files
9e3775db 698 backup_messages
70fbfb2c 699 userdata
9e3775db 700 * and if not provided, they will not be included.
701 */
702
703 function backup_generate_preferences_artificially($course, $prefs) {
ad4375ac 704 global $CFG, $DB;
9e3775db 705 $preferences = new StdClass;
706 $preferences->backup_unique_code = time();
73ac87a4 707 $preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0);
9e3775db 708 $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
70fbfb2c 709 $preferences->mods = array();
9e3775db 710 $count = 0;
711
ad4375ac 712 if ($allmods = $DB->get_records("modules") ) {
9e3775db 713 foreach ($allmods as $mod) {
714 $modname = $mod->name;
715 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
716 $modbackup = $modname."_backup_mods";
717 $modbackupone = $modname."_backup_one_mod";
718 $modcheckbackup = $modname."_check_backup_mods";
719 if (!file_exists($modfile)) {
720 continue;
721 }
722 include_once($modfile);
723 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
724 continue;
725 }
726 $var = "exists_".$modname;
727 $preferences->$var = true;
728 $count++;
729 // check that there are instances and we can back them up individually
ad4375ac 730 if (!$DB->count_records('course_modules', array('course'=>$course->id), array('module'=>$mod->id)) || !function_exists($modbackupone)) {
9e3775db 731 continue;
732 }
733 $var = 'exists_one_'.$modname;
734 $preferences->$var = true;
735 $varname = $modname.'_instances';
f8f95b45 736 $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true);
9e3775db 737 foreach ($preferences->$varname as $instance) {
738 $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
739 $var = 'backup_'.$modname.'_instance_'.$instance->id;
740 $preferences->$var = true;
741 $preferences->mods[$modname]->instances[$instance->id]->backup = true;
742 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
70fbfb2c
PL
743 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
744 $preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var;
9e3775db 745 $var = 'backup_'.$modname.'_instances';
746 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
747 }
748
749 //Check data
750 //Check module info
751 $preferences->mods[$modname]->name = $modname;
752
753 $var = "backup_".$modname;
754 $preferences->$var = true;
755 $preferences->mods[$modname]->backup = true;
756
757 //Check include user info
758 $var = "backup_user_info_".$modname;
70fbfb2c
PL
759 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
760 $preferences->mods[$modname]->userinfo = $preferences->$var;
9e3775db 761
70fbfb2c
PL
762 //Call the check function to show more info
763 $modcheckbackup = $modname."_check_backup_mods";
764 $var = $modname.'_instances';
765 $instancestopass = array();
766 if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) {
767 $table->data = array();
768 $countinstances = 0;
769 foreach ($preferences->$var as $instance) {
770 $var1 = 'backup_'.$modname.'_instance_'.$instance->id;
771 $var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
772 if (!empty($preferences->$var1)) {
773 $obj = new StdClass;
774 $obj->name = $instance->name;
775 $obj->userdata = $preferences->$var2;
776 $obj->id = $instance->id;
777 $instancestopass[$instance->id]= $obj;
778 $countinstances++;
779
780 }
781 }
782 }
783 $modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass);
9e3775db 784 }
785 }
3bee1ead 786
9e3775db 787 //Check other parameters
788 $preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0);
9e3775db 789 $preferences->backup_logs = (isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0);
790 $preferences->backup_user_files = (isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0);
791 $preferences->backup_course_files = (isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0);
3bee1ead 792 $preferences->backup_site_files = (isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0);
9e3775db 793 $preferences->backup_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0);
68e3ffbb 794 $preferences->backup_gradebook_history = (isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0);
eb7687ea 795 $preferences->backup_blogs = (isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0);
9e3775db 796 $preferences->backup_course = $course->id;
73ac87a4 797
798 //Check users
799 user_check_backup($course->id,$preferences->backup_unique_code,$preferences->backup_users,$preferences->backup_messages, $preferences->backup_blogs);
800
801 //Check logs
802 log_check_backup($course->id);
803
804 //Check user files
805 user_files_check_backup($course->id,$preferences->backup_unique_code);
806
807 //Check course files
808 course_files_check_backup($course->id,$preferences->backup_unique_code);
809
810 //Check site files
811 site_files_check_backup($course->id,$preferences->backup_unique_code);
812
813 //Role assignments
0990f7aa 814 $roles = $DB->get_records('role', null, 'sortorder');
73ac87a4 815 foreach ($roles as $role) {
816 $preferences->backuproleassignments[$role->id] = $role;
817 }
818
9e3775db 819 backup_add_static_preferences($preferences);
820 return $preferences;
821 }
795b6945 822 function add_to_backup_log($starttime,$courseid,$message, $backuptype) {
bc7ec91a 823 global $DB;
e894efc3 824 $log = new stdClass();
bc7ec91a 825 $log->courseid = $courseid;
826 $log->time = time();
827 $log->laststarttime = $starttime;
828 $log->info = $message;
829 $log->backuptype = $backuptype;
830 $DB->insert_record('backup_log', $log);
831 }
9e3775db 832