import_backup.php 9.7 KB

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