import_backup.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php // $Id: import_backup.php 13315 2007-09-27 08:17:12Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  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, 44 rue des palais, 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('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. {
  70. $error=false;
  71. if(isset($_POST['action']) && $_POST['action'] == 'course_select_form' )
  72. {
  73. $course = CourseSelectForm::get_posted_course();
  74. }
  75. else
  76. {
  77. if( $_POST['backup_type'] == 'server')
  78. {
  79. $filename = $_POST['backup_server'];
  80. $delete_file = false;
  81. }
  82. else
  83. {
  84. if($_FILES['backup']['error']==0){
  85. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  86. $delete_file = true;
  87. }
  88. else{
  89. $error=true;
  90. }
  91. }
  92. $course = CourseArchiver::read_course($filename,$delete_file);
  93. }
  94. if( $course->has_resources())
  95. {
  96. $cr = new CourseRestorer($course);
  97. $cr->set_file_option($_POST['same_file_name_option']);
  98. $cr->restore();
  99. echo get_lang('ImportFinished');
  100. }
  101. else
  102. {
  103. if(!$error){
  104. echo get_lang('NoResourcesInBackupFile');
  105. }
  106. else{
  107. echo get_lang('UploadError');
  108. }
  109. }
  110. CourseArchiver::clean_backup_dir();
  111. echo '<p><a href="../course_home/course_home.php">&lt;&lt; '.get_lang('CourseHomepage').'</a></p>';
  112. }
  113. elseif( isset($_POST['import_option']) && $_POST['import_option'] == 'select_items')
  114. {
  115. if( $_POST['backup_type'] == 'server')
  116. {
  117. $filename = $_POST['backup_server'];
  118. $delete_file = false;
  119. }
  120. else
  121. {
  122. $filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
  123. $delete_file = true;
  124. }
  125. $course = CourseArchiver::read_course($filename,$delete_file);
  126. if( $course->has_resources())
  127. {
  128. CourseSelectForm::display_form($course,array('same_file_name_option'=>$_POST['same_file_name_option']));
  129. }
  130. else
  131. {
  132. echo get_lang('NoResourcesInBackupFile');
  133. echo '<p><a href="../course_home/course_home.php">&lt;&lt; '.get_lang('CourseHomepage').'</a></p>';
  134. }
  135. }
  136. else
  137. {
  138. $user = api_get_user_info();
  139. $backups = CourseArchiver::get_available_backups($is_platformAdmin?null:$user['user_id']);
  140. $backups_available = (count($backups)>0);
  141. echo get_lang('SelectBackupFile').'<br/><br/>';
  142. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  143. $form = new FormValidator('import_backup_form','POST','import_backup.php', '','multipart/form-data');
  144. $renderer = $form->defaultRenderer();
  145. $renderer->setElementTemplate('<div>{element}</div> ');
  146. $form->addElement('hidden','action', 'restore_backup');
  147. $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;"');
  148. $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
  149. $form->addElement('html', '<br/>');
  150. if( $backups_available ){
  151. $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;"');
  152. $options['null'] = '-';
  153. foreach($backups as $index => $backup)
  154. {
  155. $options[$backup['file']]= $backup['course_code'].' ('.$backup['date'];
  156. }
  157. $form->addElement('select', 'backup_server', '', $options, 'style="margin-left: 50px;"');
  158. $form->addElement('html','<script type="text/javascript">document.import_backup_form.backup_server.disabled=true;</script>');
  159. }
  160. else
  161. {
  162. $form->addElement('radio', '', '', '<i>'.get_lang('NoBackupsAvailable').'</i>', '', 'disabled="true"');
  163. }
  164. $form->addElement('html', '<br/><br/>');
  165. $form->addElement('radio', 'import_option', '', get_lang('ImportFullBackup'), 'full_backup', 'id="import_option_1" class="checkbox"');
  166. $form->addElement('radio', 'import_option', '', get_lang('LetMeSelectItems'), 'select_items', 'id="import_option_2" class="checkbox"');
  167. $form->addElement('html', '<br/><br/>');
  168. $form->addElement('html', get_lang('SameFilename'));
  169. $form->addElement('html', '<br/><br/>');
  170. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameSkip'), FILE_SKIP, 'id="same_file_name_option_1" class="checkbox"');
  171. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameRename'), FILE_RENAME, 'id="same_file_name_option_2" class="checkbox"');
  172. $form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameOverwrite'), FILE_OVERWRITE, 'id="same_file_name_option_3" class="checkbox"');
  173. $form->addElement('html', '<br/>');
  174. $form->addElement('submit', null, get_lang('ImportBackup'));
  175. $values['backup_type'] = 'local';
  176. $values['import_option'] = 'full_backup';
  177. $values['same_file_name_option'] = FILE_OVERWRITE;
  178. $form->setDefaults($values);
  179. $form->add_progress_bar();
  180. $form->display();
  181. }
  182. /*
  183. ==============================================================================
  184. FOOTER
  185. ==============================================================================
  186. */
  187. Display::display_footer();
  188. ?>