import_backup.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Import a backup.
  5. *
  6. * @author Bart Mollet <bart.mollet@hogent.be>
  7. * @package chamilo.backup
  8. */
  9. /**
  10. * Code
  11. */
  12. /* INIT SECTION */
  13. // Language files that need to be included
  14. $language_file = array('exercice', 'coursebackup', 'admin');
  15. // Including the global initialization file
  16. require '../inc/global.inc.php';
  17. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  18. api_protect_course_script(true);
  19. // Check access rights (only teachers are allowed here)
  20. if (!api_is_allowed_to_edit()) {
  21. api_not_allowed(true);
  22. }
  23. // Remove memory and time limits as much as possible as this might be a long process...
  24. if (function_exists('ini_set')) {
  25. ini_set('memory_limit', '256M');
  26. ini_set('max_execution_time', 1800);
  27. }
  28. // Section for the tabs
  29. $this_section = SECTION_COURSES;
  30. // Breadcrumbs
  31. $interbreadcrumb[] = array('url' => '../course_info/maintenance.php', 'name' => get_lang('Maintenance'));
  32. // Displaying the header
  33. $nameTools = get_lang('ImportBackup');
  34. Display::display_header($nameTools);
  35. // Include additional libraries
  36. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  37. require_once 'classes/CourseBuilder.class.php';
  38. require_once 'classes/CourseArchiver.class.php';
  39. require_once 'classes/CourseRestorer.class.php';
  40. require_once 'classes/CourseSelectForm.class.php';
  41. // Display the tool title
  42. echo Display::page_header($nameTools);
  43. /* MAIN CODE */
  44. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['import_option']) && $_POST['import_option'] == 'full_backup' )) {
  45. $error = false;
  46. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  47. // Partial backup here we recover the documents posted
  48. $course = CourseSelectForm::get_posted_course();
  49. } else {
  50. if ($_POST['backup_type'] == 'server') {
  51. $filename = $_POST['backup_server'];
  52. $delete_file = false;
  53. } else {
  54. if ($_FILES['backup']['error'] == 0) {
  55. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  56. if ($filename === false) {
  57. $error = true;
  58. } else {
  59. $delete_file = true;
  60. }
  61. } else {
  62. $error = true;
  63. }
  64. }
  65. if (!$error) {
  66. // Full backup
  67. $course = CourseArchiver::read_course($filename,$delete_file);
  68. }
  69. }
  70. if (!$error && $course->has_resources()) {
  71. $cr = new CourseRestorer($course);
  72. $cr->set_file_option($_POST['same_file_name_option']);
  73. $cr->restore();
  74. Display::display_normal_message(get_lang('ImportFinished'));
  75. echo '<a class="btn" href="'.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php">'.get_lang('CourseHomepage').'</a>';
  76. } else {
  77. if (!$error) {
  78. Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
  79. echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  80. } elseif ($filename === false) {
  81. Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
  82. echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  83. } else {
  84. Display::display_error_message(api_ucfirst(get_lang('UploadError')));
  85. echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  86. }
  87. }
  88. CourseArchiver::clean_backup_dir();
  89. } elseif (isset($_POST['import_option']) && $_POST['import_option'] == 'select_items') {
  90. if ($_POST['backup_type'] == 'server') {
  91. $filename = $_POST['backup_server'];
  92. $delete_file = false;
  93. } else {
  94. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  95. $delete_file = true;
  96. }
  97. $course = CourseArchiver::read_course($filename,$delete_file);
  98. if ($course->has_resources() && ($filename !== false)) {
  99. CourseSelectForm::display_form($course, array('same_file_name_option' => $_POST['same_file_name_option']));
  100. } elseif ($filename === false) {
  101. Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
  102. echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  103. } else {
  104. Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
  105. echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  106. }
  107. } else {
  108. $user = api_get_user_info();
  109. $backups = CourseArchiver::get_available_backups($is_platformAdmin?null:$user['user_id']);
  110. $backups_available = count($backups) > 0;
  111. $form = new FormValidator('import_backup_form', 'post', 'import_backup.php', '', 'multipart/form-data');
  112. $form->addElement('header', get_lang('SelectBackupFile'));
  113. $renderer = $form->defaultRenderer();
  114. $renderer->setElementTemplate('<div>{element}</div> ');
  115. $form->addElement('hidden', 'action', 'restore_backup');
  116. $form->addElement('radio', 'backup_type', '', get_lang('LocalFile'), 'local', 'id="bt_local" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=true;document.import_backup_form.backup.disabled=false;"');
  117. $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
  118. $form->addElement('html', '<br />');
  119. if ($backups_available ) {
  120. $form->addElement('radio', 'backup_type', '', get_lang('ServerFile'), 'server', 'id="bt_server" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=false;document.import_backup_form.backup.disabled=true;"');
  121. $options['null'] = '-';
  122. foreach ($backups as $index => $backup) {
  123. $options[$backup['file']] = $backup['course_code'].' ('.$backup['date'].')';
  124. }
  125. $form->addElement('select', 'backup_server', '', $options, 'style="margin-left: 50px;"');
  126. $form->addElement('html', '<script type="text/javascript">document.import_backup_form.backup_server.disabled=true;</script>');
  127. } else {
  128. $form->addElement('radio', '', '', '<i>'.get_lang('NoBackupsAvailable').'</i>', '', 'disabled="true"');
  129. }
  130. $form->addElement('html', '<br /><br />');
  131. $form->addElement('radio', 'import_option', '', get_lang('ImportFullBackup'), 'full_backup', 'id="import_option_1" class="checkbox"');
  132. $form->addElement('radio', 'import_option', '', get_lang('LetMeSelectItems'), 'select_items', 'id="import_option_2" class="checkbox"');
  133. $form->addElement('html', '<br /><br />');
  134. $form->addElement('html', get_lang('SameFilename'));
  135. $form->addElement('html', '<br /><br />');
  136. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameSkip'), FILE_SKIP, 'id="same_file_name_option_1" class="checkbox"');
  137. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameRename'), FILE_RENAME, 'id="same_file_name_option_2" class="checkbox"');
  138. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameOverwrite'), FILE_OVERWRITE, 'id="same_file_name_option_3" class="checkbox"');
  139. $form->addElement('html', '<br />');
  140. $form->addElement('style_submit_button', null, get_lang('ImportBackup'), 'class="save"');
  141. $values['backup_type'] = 'local';
  142. $values['import_option'] = 'full_backup';
  143. $values['same_file_name_option'] = FILE_OVERWRITE;
  144. $form->setDefaults($values);
  145. $form->add_progress_bar();
  146. // When progress bar appears we have to hide the title "Select backup file".
  147. $form->updateAttributes(array('onsubmit' => str_replace('javascript: ', 'javascript: page_title = getElementById(\'page_title\'); if (page_title) { setTimeout(\'page_title.style.display = \\\'none\\\';\', 2000); } ', $form->getAttribute('onsubmit'))));
  148. $form->display();
  149. }
  150. /* FOOTER */
  151. Display::display_footer();