import_backup.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php // $Id: import_backup.php 20448 2009-05-10 10:17:22Z ivantcholakov $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * ==============================================================================
  22. * Import a backup.
  23. *
  24. * @author Bart Mollet <bart.mollet@hogent.be>
  25. * @package dokeos.backup
  26. * ==============================================================================
  27. */
  28. /*
  29. ==============================================================================
  30. INIT SECTION
  31. ==============================================================================
  32. */
  33. // name of the language file that needs to be included
  34. $language_file = array('exercice', 'coursebackup', 'admin');
  35. // including the global file
  36. include ('../inc/global.inc.php');
  37. // Check access rights (only teachers are allowed here)
  38. if( ! api_is_allowed_to_edit())
  39. {
  40. api_not_allowed(true);
  41. }
  42. //remove memory and time limits as much as possible as this might be a long process...
  43. if(function_exists('ini_set'))
  44. {
  45. ini_set('memory_limit','256M');
  46. ini_set('max_execution_time',1800);
  47. }
  48. // section for the tabs
  49. $this_section=SECTION_COURSES;
  50. // breadcrumbs
  51. $interbreadcrumb[] = array ("url" => "../course_info/maintenance.php", "name" => get_lang('Maintenance'));
  52. // Displaying the header
  53. $nameTools = get_lang('ImportBackup');
  54. Display::display_header($nameTools);
  55. // include additional libraries
  56. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  57. require_once('classes/CourseBuilder.class.php');
  58. require_once('classes/CourseArchiver.class.php');
  59. require_once('classes/CourseRestorer.class.php');
  60. require_once('classes/CourseSelectForm.class.php');
  61. // Display the tool title
  62. api_display_tool_title($nameTools);
  63. /*
  64. ==============================================================================
  65. MAIN CODE
  66. ==============================================================================
  67. */
  68. if((isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['import_option']) && $_POST['import_option'] == 'full_backup' )) {
  69. $error=false;
  70. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  71. // partial backup here we recover the documents posted
  72. $course = CourseSelectForm::get_posted_course();
  73. } else {
  74. if( $_POST['backup_type'] == 'server') {
  75. $filename = $_POST['backup_server'];
  76. $delete_file = false;
  77. } else {
  78. if($_FILES['backup']['error']==0){
  79. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  80. if ($filename === false) {
  81. $error = true;
  82. } else {
  83. $delete_file = true;
  84. }
  85. } else {
  86. $error=true;
  87. }
  88. }
  89. if(!$error) {
  90. // full backup
  91. $course = CourseArchiver::read_course($filename,$delete_file);
  92. }
  93. }
  94. if(!$error && $course->has_resources()) {
  95. $cr = new CourseRestorer($course);
  96. $cr->set_file_option($_POST['same_file_name_option']);
  97. $cr->restore();
  98. Display::display_normal_message(get_lang('ImportFinished').
  99. //'<a class="bottom-link" href="../course_home/course_home.php?'.api_get_cidreq().'">&gt;&gt; '.get_lang('CourseHomepage').'</a>',false); // This is not the preferable way to go to the course homepage.
  100. '<a class="bottom-link" href="'.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php">&lt;&lt; '.get_lang('CourseHomepage').'</a>',false);
  101. } else {
  102. if(!$error){
  103. Display::display_warning_message(get_lang('NoResourcesInBackupFile').
  104. '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
  105. } elseif ($filename === false) {
  106. Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin').
  107. '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
  108. } else {
  109. Display::display_error_message(api_ucfirst(get_lang('UploadError')).
  110. '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
  111. }
  112. }
  113. CourseArchiver::clean_backup_dir();
  114. } elseif (isset($_POST['import_option']) && $_POST['import_option'] == 'select_items') {
  115. if( $_POST['backup_type'] == 'server') {
  116. $filename = $_POST['backup_server'];
  117. $delete_file = false;
  118. } else {
  119. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  120. $delete_file = true;
  121. }
  122. $course = CourseArchiver::read_course($filename,$delete_file);
  123. if ($course->has_resources() && ($filename !== false)) {
  124. CourseSelectForm::display_form($course,array('same_file_name_option'=>$_POST['same_file_name_option']));
  125. } elseif ($filename === false) {
  126. Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin').
  127. '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
  128. } else {
  129. Display::display_warning_message(get_lang('NoResourcesInBackupFile').
  130. '<a class="bottom-link" href="import_backup.php?'.api_get_cidreq().'">&lt;&lt; '.get_lang('TryAgain').'</a>',false);
  131. }
  132. } else {
  133. $user = api_get_user_info();
  134. $backups = CourseArchiver::get_available_backups($is_platformAdmin?null:$user['user_id']);
  135. $backups_available = (count($backups)>0);
  136. echo get_lang('SelectBackupFile').'<br /><br />';
  137. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  138. $form = new FormValidator('import_backup_form','POST','import_backup.php', '','multipart/form-data');
  139. $renderer = $form->defaultRenderer();
  140. $renderer->setElementTemplate('<div>{element}</div> ');
  141. $form->addElement('hidden','action', 'restore_backup');
  142. $form->addElement('radio', 'backup_type', '', get_lang('LocalFile'), 'local', 'id="bt_local" class="checkbox" onclick="javascript:document.import_backup_form.backup_server.disabled=true;document.import_backup_form.backup.disabled=false;"');
  143. $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
  144. $form->addElement('html', '<br />');
  145. if( $backups_available ){
  146. $form->addElement('radio', 'backup_type', '', get_lang('ServerFile'), 'server', 'id="bt_server" class="checkbox" onclick="javascript:document.import_backup_form.backup_server.disabled=false;document.import_backup_form.backup.disabled=true;"');
  147. $options['null'] = '-';
  148. foreach($backups as $index => $backup)
  149. {
  150. $options[$backup['file']]= $backup['course_code'].' ('.$backup['date'];
  151. }
  152. $form->addElement('select', 'backup_server', '', $options, 'style="margin-left: 50px;"');
  153. $form->addElement('html','<script type="text/javascript">document.import_backup_form.backup_server.disabled=true;</script>');
  154. }
  155. else
  156. {
  157. $form->addElement('radio', '', '', '<i>'.get_lang('NoBackupsAvailable').'</i>', '', 'disabled="true"');
  158. }
  159. $form->addElement('html', '<br /><br />');
  160. $form->addElement('radio', 'import_option', '', get_lang('ImportFullBackup'), 'full_backup', 'id="import_option_1" class="checkbox"');
  161. $form->addElement('radio', 'import_option', '', get_lang('LetMeSelectItems'), 'select_items', 'id="import_option_2" class="checkbox"');
  162. $form->addElement('html', '<br /><br />');
  163. $form->addElement('html', get_lang('SameFilename'));
  164. $form->addElement('html', '<br /><br />');
  165. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameSkip'), FILE_SKIP, 'id="same_file_name_option_1" class="checkbox"');
  166. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameRename'), FILE_RENAME, 'id="same_file_name_option_2" class="checkbox"');
  167. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameOverwrite'), FILE_OVERWRITE, 'id="same_file_name_option_3" class="checkbox"');
  168. $form->addElement('html', '<br />');
  169. $form->addElement('style_submit_button', null, get_lang('ImportBackup'), 'class="save"');
  170. $values['backup_type'] = 'local';
  171. $values['import_option'] = 'full_backup';
  172. $values['same_file_name_option'] = FILE_OVERWRITE;
  173. $form->setDefaults($values);
  174. $form->add_progress_bar();
  175. $form->display();
  176. }
  177. /*
  178. ==============================================================================
  179. FOOTER
  180. ==============================================================================
  181. */
  182. Display::display_footer();
  183. ?>