create_backup.php 4.1 KB

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