import_backup.php 8.9 KB

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