PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/restore.php

https://github.com/nadavkav/MoodleTAO
PHP | 181 lines | 151 code | 16 blank | 14 comment | 27 complexity | e23b651cae3b0a3506eb81d43201df06 MD5 | raw file
  1. <?php //$Id$
  2. //This script is used to configure and execute the restore proccess.
  3. //Define some globals for all the script
  4. //Units used
  5. require_once("../config.php");
  6. require_once("../lib/xmlize.php");
  7. require_once("../course/lib.php");
  8. require_once("lib.php");
  9. require_once("restorelib.php");
  10. require_once("bb/restore_bb.php");
  11. require_once("$CFG->libdir/blocklib.php");
  12. require_once("$CFG->libdir/wiki_to_markdown.php" );
  13. require_once("$CFG->libdir/adminlib.php");
  14. //Optional
  15. $id = optional_param( 'id' );
  16. $file = optional_param( 'file' );
  17. $cancel = optional_param( 'cancel' );
  18. $launch = optional_param( 'launch' );
  19. $to = optional_param( 'to' );
  20. $method = optional_param( 'method' );
  21. $backup_unique_code = optional_param('backup_unique_code',0,PARAM_INT);
  22. //Check login
  23. require_login();
  24. /// With method=manual, we come from the FileManager so we delete all the backup/restore/import session structures
  25. if ($method == 'manual') {
  26. if (isset($SESSION->course_header)) {
  27. unset ($SESSION->course_header);
  28. }
  29. if (isset($SESSION->info)) {
  30. unset ($SESSION->info);
  31. }
  32. if (isset($SESSION->backupprefs)) {
  33. unset ($SESSION->backupprefs);
  34. }
  35. if (isset($SESSION->restore)) {
  36. unset ($SESSION->restore);
  37. }
  38. if (isset($SESSION->import_preferences)) {
  39. unset ($SESSION->import_preferences);
  40. }
  41. }
  42. if (!$to && isset($SESSION->restore->restoreto) && isset($SESSION->restore->importing) && isset($SESSION->restore->course_id)) {
  43. $to = $SESSION->restore->course_id;
  44. }
  45. if (!empty($id)) {
  46. require_login($id);
  47. if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
  48. if (empty($to)) {
  49. error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
  50. } else {
  51. if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $to))
  52. && !has_capability('moodle/site:import', get_context_instance(CONTEXT_COURSE, $to))) {
  53. error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
  54. }
  55. }
  56. }
  57. } else {
  58. if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
  59. error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
  60. }
  61. }
  62. //Check site
  63. if (!$site = get_site()) {
  64. error("Site not found!");
  65. }
  66. //Check necessary functions exists. Thanks to gregb@crowncollege.edu
  67. backup_required_functions();
  68. //Check backup_version
  69. if ($file) {
  70. $linkto = "restore.php?id=".$id."&amp;file=".$file;
  71. } else {
  72. $linkto = "restore.php";
  73. }
  74. upgrade_backup_db($linkto);
  75. //Get strings
  76. if (empty($to)) {
  77. $strcourserestore = get_string("courserestore");
  78. } else {
  79. $strcourserestore = get_string("importdata");
  80. }
  81. $stradministration = get_string("administration");
  82. //If no file has been selected from the FileManager, inform and end
  83. $navlinks = array();
  84. $navlinks[] = array('name' => $stradministration, 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc');
  85. $navlinks[] = array('name' => $strcourserestore, 'link' => null, 'type' => 'misc');
  86. $navigation = build_navigation($navlinks);
  87. if (!$file) {
  88. print_header("$site->shortname: $strcourserestore", $site->fullname, $navigation);
  89. print_heading(get_string("nofilesselected"));
  90. print_continue("$CFG->wwwroot/$CFG->admin/index.php");
  91. print_footer();
  92. exit;
  93. }
  94. //If cancel has been selected, inform and end
  95. if ($cancel) {
  96. print_header("$site->shortname: $strcourserestore", $site->fullname, $navigation);
  97. print_heading(get_string("restorecancelled"));
  98. print_continue("$CFG->wwwroot/course/view.php?id=".$id);
  99. print_footer();
  100. exit;
  101. }
  102. //We are here, so we have a file.
  103. //Get and check course
  104. if (! $course = get_record("course", "id", $id)) {
  105. error("Course ID was incorrect (can't find it)");
  106. }
  107. //Print header
  108. if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
  109. $navlinks[] = array('name' => basename($file), 'link' => null, 'type' => 'misc');
  110. $navigation = build_navigation($navlinks);
  111. print_header("$site->shortname: $strcourserestore", $site->fullname, $navigation);
  112. } else {
  113. $navlinks = array();
  114. $navlinks[] = array('name' => $course->shortname, 'link' => "$CFG->wwwroot/course/view.php?id=$course->id", 'type' => 'misc');
  115. $navlinks[] = array('name' => $strcourserestore, 'link' => null, 'type' => 'misc');
  116. $navigation = build_navigation($navlinks);
  117. print_header("$course->shortname: $strcourserestore", $course->fullname, $navigation);
  118. }
  119. //Print form
  120. print_heading("$strcourserestore".((empty($to) ? ': '.basename($file) : '')));
  121. print_simple_box_start('center');
  122. //Adjust some php variables to the execution of this script
  123. @ini_set("max_execution_time","3000");
  124. if (empty($CFG->extramemorylimit)) {
  125. raise_memory_limit('128M');
  126. } else {
  127. raise_memory_limit($CFG->extramemorylimit);
  128. }
  129. //Call the form, depending the step we are
  130. if (!$launch) {
  131. include_once("restore_precheck.html");
  132. } else if ($launch == "form") {
  133. if (!empty($SESSION->restore->importing)) {
  134. // set up all the config stuff and skip asking the user about it.
  135. restore_setup_for_check($SESSION->restore,$backup_unique_code);
  136. require_sesskey();
  137. include_once("restore_execute.html");
  138. } else {
  139. include_once("restore_form.html");
  140. }
  141. } else if ($launch == "check") {
  142. include_once("restore_check.html");
  143. //To avoid multiple restore executions...
  144. $SESSION->cancontinue = true;
  145. } else if ($launch == "execute") {
  146. //Prevent multiple restore executions...
  147. if (empty($SESSION->cancontinue)) {
  148. error("Multiple restore execution not allowed!");
  149. }
  150. //Unset this for the future
  151. unset($SESSION->cancontinue);
  152. require_sesskey();
  153. include_once("restore_execute.html");
  154. }
  155. print_simple_box_end();
  156. //Print footer
  157. print_footer();
  158. ?>