create_backup.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 __DIR__.'/../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. // Display the tool title
  36. echo Display::page_header($nameTools);
  37. /* MAIN CODE */
  38. if (Security::check_token('post') && (
  39. (
  40. isset($_POST['action']) &&
  41. $_POST['action'] == 'course_select_form'
  42. ) || (
  43. isset($_POST['backup_option']) &&
  44. $_POST['backup_option'] == 'full_backup'
  45. )
  46. )
  47. ) {
  48. // Clear token
  49. Security::clear_token();
  50. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  51. $course = CourseSelectForm::get_posted_course();
  52. } else {
  53. $cb = new CourseBuilder();
  54. $course = $cb->build();
  55. }
  56. $zip_file = CourseArchiver::createBackup($course);
  57. echo Display::return_message(get_lang('BackupCreated'), 'confirm');
  58. 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().'">
  59. ' . get_lang('Download').'</a>';
  60. } elseif (Security::check_token('post') && (
  61. isset($_POST['backup_option']) &&
  62. $_POST['backup_option'] == 'select_items'
  63. )
  64. ) {
  65. // Clear token
  66. Security::clear_token();
  67. $cb = new CourseBuilder('partial');
  68. $course = $cb->build();
  69. // Add token to Course select form
  70. $hiddenFields['sec_token'] = Security::get_token();
  71. CourseSelectForm::display_form($course, $hiddenFields);
  72. } else {
  73. $cb = new CourseBuilder();
  74. $course = $cb->build();
  75. if (!$course->has_resources()) {
  76. echo get_lang('NoResourcesToBackup');
  77. } else {
  78. $form = new FormValidator(
  79. 'create_backup_form',
  80. 'post',
  81. api_get_self().'?'.api_get_cidreq()
  82. );
  83. $form->addElement('header', get_lang('SelectOptionForBackup'));
  84. $form->addElement('radio', 'backup_option', '', get_lang('CreateFullBackup'), 'full_backup');
  85. $form->addElement('radio', 'backup_option', '', get_lang('LetMeSelectItems'), 'select_items');
  86. $form->addButtonSave(get_lang('CreateBackup'));
  87. $form->addProgress();
  88. // When progress bar appears we have to hide the title "Please select a backup-option".
  89. $form->updateAttributes(
  90. array(
  91. 'onsubmit' => str_replace(
  92. 'javascript: ',
  93. 'javascript: page_title = getElementById(\'page_title\'); if (page_title) { setTimeout(\'page_title.style.display = \\\'none\\\';\', 2000); } ',
  94. $form->getAttribute('onsubmit')
  95. )
  96. )
  97. );
  98. $values['backup_option'] = 'full_backup';
  99. $form->setDefaults($values);
  100. // Add Security token
  101. $token = Security::get_token();
  102. $form->addElement('hidden', 'sec_token');
  103. $form->setConstants(array('sec_token' => $token));
  104. echo '<div class="row">';
  105. echo '<div class="col-md-12">';
  106. echo '<div class="tool-backup">';
  107. $form->display();
  108. echo '</div>';
  109. echo '</div>';
  110. echo '</div>';
  111. }
  112. }
  113. Display::display_footer();