import_backup.php 9.4 KB

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