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