import_backup.php 9.7 KB

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