import_backup.php 7.6 KB

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