import_backup.php 9.4 KB

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