create_backup.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  6. /**
  7. * Create a backup.
  8. *
  9. * @author Bart Mollet <bart.mollet@hogent.be>
  10. *
  11. * @package chamilo.backup
  12. */
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  15. api_protect_course_script(true);
  16. // Check access rights (only teachers are allowed here)
  17. if (!api_is_allowed_to_edit()) {
  18. api_not_allowed(true);
  19. }
  20. api_check_archive_dir();
  21. api_set_more_memory_and_time_limits();
  22. // Section for the tabs
  23. $this_section = SECTION_COURSES;
  24. // Breadcrumbs
  25. $interbreadcrumb[] = [
  26. 'url' => api_get_path(WEB_CODE_PATH).'course_info/maintenance.php',
  27. 'name' => get_lang('Maintenance'),
  28. ];
  29. // Displaying the header
  30. $nameTools = get_lang('CreateBackup');
  31. Display::display_header($nameTools);
  32. // Display the tool title
  33. echo Display::page_header($nameTools);
  34. $action = isset($_POST['action']) ? $_POST['action'] : '';
  35. $backupOption = isset($_POST['backup_option']) ? $_POST['backup_option'] : '';
  36. if (Security::check_token('post') &&
  37. ($action === 'course_select_form' || $backupOption === 'full_backup')
  38. ) {
  39. // Clear token
  40. Security::clear_token();
  41. if ($action === 'course_select_form') {
  42. $cb = new CourseBuilder('partial');
  43. $course = $cb->build(0, null, false, array_keys($_POST['resource']), $_POST['resource']);
  44. $course = CourseSelectForm::get_posted_course(null, 0, '', $course);
  45. } else {
  46. $cb = new CourseBuilder('complete');
  47. $course = $cb->build();
  48. }
  49. $zipFile = CourseArchiver::createBackup($course);
  50. echo Display::return_message(get_lang('BackupCreated'), 'confirm');
  51. echo '<br />';
  52. echo Display::url(
  53. get_lang('Download'),
  54. api_get_path(WEB_CODE_PATH).'course_info/download.php?archive='.$zipFile.'&'.api_get_cidreq(),
  55. ['class' => 'btn btn-primary btn-large']
  56. );
  57. } elseif (Security::check_token('post') && $backupOption === 'select_items') {
  58. // Clear token
  59. Security::clear_token();
  60. $cb = new CourseBuilder('partial');
  61. $course = $cb->build();
  62. if ($course->has_resources()) {
  63. // Add token to Course select form
  64. $hiddenFields['sec_token'] = Security::get_token();
  65. CourseSelectForm::display_form($course, $hiddenFields, false, true);
  66. } else {
  67. echo Display::return_message(get_lang('NoResourcesToBackup'), 'warning');
  68. }
  69. } else {
  70. $form = new FormValidator(
  71. 'create_backup_form',
  72. 'post',
  73. api_get_self().'?'.api_get_cidreq()
  74. );
  75. $form->addElement('header', get_lang('SelectOptionForBackup'));
  76. $form->addElement('radio', 'backup_option', '', get_lang('CreateFullBackup'), 'full_backup');
  77. $form->addElement('radio', 'backup_option', '', get_lang('LetMeSelectItems'), 'select_items');
  78. $form->addButtonSave(get_lang('CreateBackup'));
  79. $form->addProgress();
  80. // When progress bar appears we have to hide the title "Please select a backup-option".
  81. $form->updateAttributes(
  82. [
  83. 'onsubmit' => str_replace(
  84. 'javascript: ',
  85. 'javascript: page_title = getElementById(\'page_title\'); if (page_title) { setTimeout(\'page_title.style.display = \\\'none\\\';\', 2000); } ',
  86. $form->getAttribute('onsubmit')
  87. ),
  88. ]
  89. );
  90. $values['backup_option'] = 'full_backup';
  91. $form->setDefaults($values);
  92. // Add Security token
  93. $token = Security::get_token();
  94. $form->addElement('hidden', 'sec_token');
  95. $form->setConstants(['sec_token' => $token]);
  96. echo '<div class="row">';
  97. echo '<div class="col-md-12">';
  98. echo '<div class="tool-backup">';
  99. $form->display();
  100. echo '</div>';
  101. echo '</div>';
  102. echo '</div>';
  103. }
  104. Display::display_footer();