import_backup.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  6. use ChamiloSession as Session;
  7. /**
  8. * Import a backup.
  9. *
  10. * @author Bart Mollet <bart.mollet@hogent.be>
  11. *
  12. * @package chamilo.backup
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  16. api_protect_course_script(true);
  17. // Check access rights (only teachers are allowed here)
  18. if (!api_is_allowed_to_edit()) {
  19. api_not_allowed(true);
  20. }
  21. api_set_more_memory_and_time_limits();
  22. // Section for the tabs
  23. $this_section = SECTION_COURSES;
  24. // Breadcrumbs
  25. $interbreadcrumb[] = [
  26. 'url' => '../course_info/maintenance.php',
  27. 'name' => get_lang('Maintenance'),
  28. ];
  29. // Displaying the header
  30. $nameTools = get_lang('ImportBackup');
  31. Display::display_header($nameTools);
  32. // Display the tool title
  33. echo Display::page_header($nameTools);
  34. $action = isset($_POST['action']) ? $_POST['action'] : '';
  35. $importOption = isset($_POST['import_option']) ? $_POST['import_option'] : '';
  36. /* MAIN CODE */
  37. $filename = '';
  38. if (Security::check_token('post') && ($action === 'course_select_form' || $importOption === 'full_backup')) {
  39. // Clear token
  40. Security::clear_token();
  41. $error = false;
  42. if ($action === 'course_select_form') {
  43. // Partial backup here we recover the documents posted
  44. $filename = Session::read('backup_file');
  45. $course = CourseArchiver::readCourse($filename, false);
  46. $course = CourseSelectForm::get_posted_course(null, null, null, $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::importUploadedFile($_FILES['backup']['tmp_name']);
  54. if ($filename === false) {
  55. $error = true;
  56. } else {
  57. $delete_file = false;
  58. }
  59. Session::write('backup_file', $filename);
  60. } else {
  61. $error = true;
  62. }
  63. }
  64. if (!$error) {
  65. // Full backup
  66. $course = CourseArchiver::readCourse($filename, $delete_file);
  67. }
  68. }
  69. if (!$error && is_object($course) && $course->has_resources()) {
  70. $cr = new CourseRestorer($course);
  71. $cr->set_file_option($_POST['same_file_name_option']);
  72. $cr->restore();
  73. echo Display::return_message(get_lang('ImportFinished'));
  74. echo '<a class="btn btn-default" href="'.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php">'.
  75. get_lang('CourseHomepage').'</a>';
  76. } else {
  77. if (!$error) {
  78. echo Display::return_message(get_lang('NoResourcesInBackupFile'), 'warning');
  79. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  80. } elseif ($filename === false) {
  81. echo Display::return_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'), 'error');
  82. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  83. } else {
  84. if ($filename == '') {
  85. echo Display::return_message(get_lang('SelectBackupFile'), 'error');
  86. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  87. } else {
  88. echo Display::return_message(get_lang('UploadError'), 'error');
  89. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  90. }
  91. }
  92. }
  93. CourseArchiver::cleanBackupDir();
  94. } elseif (Security::check_token('post') && $importOption === 'select_items') {
  95. // Clear token
  96. Security::clear_token();
  97. if ($_POST['backup_type'] === 'server') {
  98. $filename = $_POST['backup_server'];
  99. $delete_file = false;
  100. } else {
  101. $filename = CourseArchiver::importUploadedFile($_FILES['backup']['tmp_name']);
  102. $delete_file = false;
  103. Session::write('backup_file', $filename);
  104. }
  105. $course = CourseArchiver::readCourse($filename, $delete_file);
  106. if ($course->has_resources() && $filename !== false) {
  107. $hiddenFields['same_file_name_option'] = $_POST['same_file_name_option'];
  108. // Add token to Course select form
  109. $hiddenFields['sec_token'] = Security::get_token();
  110. CourseSelectForm::display_form($course, $hiddenFields);
  111. } elseif ($filename === false) {
  112. echo Display::return_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'), 'error');
  113. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  114. } else {
  115. echo Display::return_message(get_lang('NoResourcesInBackupFile'), 'warning');
  116. echo '<a class="btn btn-default" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
  117. }
  118. } else {
  119. $user = api_get_user_info();
  120. $backups = CourseArchiver::getAvailableBackups($is_platformAdmin ? null : $user['user_id']);
  121. $backups_available = count($backups) > 0;
  122. $form = new FormValidator(
  123. 'import_backup_form',
  124. 'post',
  125. api_get_path(WEB_CODE_PATH).'coursecopy/import_backup.php?'.api_get_cidreq(),
  126. '',
  127. ['enctype' => 'multipart/form-data']
  128. );
  129. $form->addElement('header', get_lang('SelectBackupFile'));
  130. $renderer = $form->defaultRenderer();
  131. $renderer->setCustomElementTemplate('<div>{element}</div> ');
  132. $form->addElement('hidden', 'action', 'restore_backup');
  133. $form->addElement(
  134. 'radio',
  135. 'backup_type',
  136. '',
  137. get_lang('LocalFile'),
  138. 'local',
  139. 'id="bt_local" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=true;document.import_backup_form.backup.disabled=false;"'
  140. );
  141. $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
  142. $form->addElement('html', '<br />');
  143. if ($backups_available) {
  144. $form->addElement(
  145. 'radio',
  146. 'backup_type',
  147. '',
  148. get_lang('ServerFile'),
  149. 'server',
  150. 'id="bt_server" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=false;document.import_backup_form.backup.disabled=true;"'
  151. );
  152. $options['null'] = '-';
  153. foreach ($backups as $index => $backup) {
  154. $options[$backup['file']] = $backup['course_code'].' ('.$backup['date'].')';
  155. }
  156. $form->addElement(
  157. 'select',
  158. 'backup_server',
  159. '',
  160. $options,
  161. 'style="margin-left: 50px;"'
  162. );
  163. $form->addElement(
  164. 'html',
  165. '<script type="text/javascript">document.import_backup_form.backup_server.disabled=true;</script>'
  166. );
  167. } else {
  168. $form->addElement(
  169. 'radio',
  170. '',
  171. '',
  172. '<i>'.get_lang('NoBackupsAvailable').'</i>',
  173. '',
  174. 'disabled="true"'
  175. );
  176. }
  177. $form->addElement('html', '<br /><br />');
  178. $form->addElement(
  179. 'radio',
  180. 'import_option',
  181. '',
  182. get_lang('ImportFullBackup'),
  183. 'full_backup',
  184. 'id="import_option_1" class="checkbox"'
  185. );
  186. $form->addElement(
  187. 'radio',
  188. 'import_option',
  189. '',
  190. get_lang('LetMeSelectItems'),
  191. 'select_items',
  192. 'id="import_option_2" class="checkbox"'
  193. );
  194. $form->addElement('html', '<br /><br />');
  195. $form->addElement('html', get_lang('SameFilename'));
  196. $form->addElement('html', '<br /><br />');
  197. $form->addElement(
  198. 'radio',
  199. 'same_file_name_option',
  200. '',
  201. get_lang('SameFilenameSkip'),
  202. FILE_SKIP,
  203. 'id="same_file_name_option_1" class="checkbox"'
  204. );
  205. $form->addElement(
  206. 'radio',
  207. 'same_file_name_option',
  208. '',
  209. get_lang('SameFilenameRename'),
  210. FILE_RENAME,
  211. 'id="same_file_name_option_2" class="checkbox"'
  212. );
  213. $form->addElement(
  214. 'radio',
  215. 'same_file_name_option',
  216. '',
  217. get_lang('SameFilenameOverwrite'),
  218. FILE_OVERWRITE,
  219. 'id="same_file_name_option_3" class="checkbox"'
  220. );
  221. $form->addElement('html', '<br />');
  222. $form->addButtonImport(get_lang('ImportBackup'));
  223. $values['backup_type'] = 'local';
  224. $values['import_option'] = 'full_backup';
  225. $values['same_file_name_option'] = FILE_OVERWRITE;
  226. $form->setDefaults($values);
  227. $form->addProgress();
  228. // When progress bar appears we have to hide the title "Select backup file".
  229. $form->updateAttributes([
  230. 'onsubmit' => str_replace(
  231. 'javascript: ',
  232. 'javascript: page_title = getElementById(\'page_title\'); if (page_title) { setTimeout(\'page_title.style.display = \\\'none\\\';\', 2000); } ',
  233. $form->getAttribute('onsubmit')
  234. ),
  235. ]);
  236. // Add Security token
  237. $token = Security::get_token();
  238. $form->addElement('hidden', 'sec_token');
  239. $form->setConstants(['sec_token' => $token]);
  240. $form->display();
  241. }
  242. if (!isset($_POST['action'])) {
  243. Session::erase('backup_file');
  244. }
  245. Display::display_footer();