Project

General

Profile

Bug #5515 » import_backup.php

Yoselyn Castillo, 20/09/2012 21:20

 
1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * Import a backup.
5
 *
6
 * @author Bart Mollet <bart.mollet@hogent.be>
7
 * @package chamilo.backup
8
 */
9
/**
10
 * Code
11
 */
12
/*	INIT SECTION */
13

    
14
// Language files that need to be included
15
$language_file = array('exercice', 'coursebackup', 'admin');
16

    
17
// Including the global initialization file
18
require '../inc/global.inc.php';
19
$current_course_tool  = TOOL_COURSE_MAINTENANCE;
20
api_protect_course_script(true);
21

    
22
// Check access rights (only teachers are allowed here)
23
if (!api_is_allowed_to_edit()) {
24
	api_not_allowed(true);
25
}
26

    
27
// Remove memory and time limits as much as possible as this might be a long process...
28
if (function_exists('ini_set')) {
29
	ini_set('memory_limit', '256M');
30
	ini_set('max_execution_time', 1800);
31
}
32

    
33
// Section for the tabs
34
$this_section = SECTION_COURSES;
35

    
36
// Breadcrumbs
37
$interbreadcrumb[] = array('url' => '../course_info/maintenance.php', 'name' => get_lang('Maintenance'));
38

    
39
// Displaying the header
40
$nameTools = get_lang('ImportBackup');
41
Display::display_header($nameTools);
42

    
43
// Include additional libraries
44
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
45
require_once 'classes/CourseBuilder.class.php';
46
require_once 'classes/CourseArchiver.class.php';
47
require_once 'classes/CourseRestorer.class.php';
48
require_once 'classes/CourseSelectForm.class.php';
49

    
50
// Display the tool title
51
echo Display::page_header($nameTools);
52

    
53
/*	MAIN CODE */
54

    
55
if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form' ) || (isset($_POST['import_option']) && $_POST['import_option'] == 'full_backup' )) {
56
	$error = false;
57
	if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
58
		// Partial backup here we recover the documents posted
59
		$course = CourseSelectForm::get_posted_course();
60
        
61
	} else {
62
		if ($_POST['backup_type'] == 'server') {
63
			$filename = $_POST['backup_server'];
64
			$delete_file = false;
65
		} else {
66
			if ($_FILES['backup']['error'] == 0) {
67
				$filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
68
				if ($filename === false) {
69
                	$error = true;
70
                } else                {
71
                    $delete_file = true;
72
                }
73
			} else {
74
				$error = true;
75
			}
76
		}
77
    
78
        if (!$error) {
79
		  // Full backup
80
		  $course = CourseArchiver::read_course($filename, $delete_file);
81
        }
82
	}
83

    
84
	if (!$error && $course->has_resources()) {
85
		$cr = new CourseRestorer($course);
86
		$cr->set_file_option($_POST['same_file_name_option']);
87
		$cr->restore();
88
		Display::display_normal_message(get_lang('ImportFinished'));
89
        echo '<a class="btn" href="'.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php">'.get_lang('CourseHomepage').'</a>';
90
	} else {
91
		if (!$error) {
92
			Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
93
            echo  '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
94
		} elseif ($filename === false) {
95
            Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
96
            echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
97
        } else {
98
            if ($filename == '') {
99
               Display::display_error_message(api_ucfirst(get_lang('SelectBackupFile'))); 
100
               echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
101
            } else {
102
    			Display::display_error_message(api_ucfirst(get_lang('UploadError')));
103
                echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
104
            }
105
		}
106
	}
107
	CourseArchiver::clean_backup_dir();
108
} elseif (isset($_POST['import_option']) && $_POST['import_option'] == 'select_items') {
109
	if ($_POST['backup_type'] == 'server') {
110
		$filename = $_POST['backup_server'];
111
		$delete_file = false;
112
	} else {
113
		$filename = CourseArchiver::import_uploaded_file($_FILES['backup']['tmp_name']);
114
		$delete_file = true;
115
	}
116
	$course = CourseArchiver::read_course($filename, $delete_file);
117
  
118
	if ($course->has_resources() && ($filename !== false)) {
119
		CourseSelectForm::display_form($course, array('same_file_name_option' => $_POST['same_file_name_option']));
120
	} elseif ($filename === false) {
121
    	Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
122
        echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
123
    } else {
124
		Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
125
        echo '<a class="btn" href="import_backup.php?'.api_get_cidreq().'">'.get_lang('TryAgain').'</a>';
126
	}
127
} else {
128
	$user = api_get_user_info();
129
	$backups = CourseArchiver::get_available_backups($is_platformAdmin ?null:$user['user_id']);
130
	$backups_available = count($backups) > 0;
131

    
132
	$form = new FormValidator('import_backup_form', 'post', 'import_backup.php', '', 'multipart/form-data');
133
    $form->addElement('header', get_lang('SelectBackupFile'));
134
	$renderer = $form->defaultRenderer();
135
	$renderer->setElementTemplate('<div>{element}</div> ');
136

    
137
	$form->addElement('hidden', 'action', 'restore_backup');
138

    
139
	$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;"');
140
	$form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
141
	$form->addElement('html', '<br />');
142

    
143
	if ($backups_available) {
144
		$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;"');
145
		$options['null'] = '-';
146
		foreach ($backups as $index => $backup) {
147
			$options[$backup['file']] = $backup['course_code'].' ('.$backup['date'].')';
148
		}
149
		$form->addElement('select', 'backup_server', '', $options, 'style="margin-left: 50px;"');
150
		$form->addElement('html', '<script type="text/javascript">document.import_backup_form.backup_server.disabled=true;</script>');
151
	} else {
152
		$form->addElement('radio', '', '', '<i>'.get_lang('NoBackupsAvailable').'</i>', '', 'disabled="true"');
153
	}
154

    
155
	$form->addElement('html', '<br /><br />');
156

    
157
	$form->addElement('radio', 'import_option', '', get_lang('ImportFullBackup'), 'full_backup', 'id="import_option_1" class="checkbox"');
158
	$form->addElement('radio', 'import_option', '', get_lang('LetMeSelectItems'), 'select_items', 'id="import_option_2" class="checkbox"');
159

    
160
	$form->addElement('html', '<br /><br />');
161

    
162
	$form->addElement('html', get_lang('SameFilename'));
163
	$form->addElement('html', '<br /><br />');
164
	$form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameSkip'), FILE_SKIP, 'id="same_file_name_option_1" class="checkbox"');
165
	$form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameRename'), FILE_RENAME, 'id="same_file_name_option_2" class="checkbox"');
166
	$form->addElement('radio', 'same_file_name_option', '', get_lang('SameFilenameOverwrite'), FILE_OVERWRITE, 'id="same_file_name_option_3" class="checkbox"');
167

    
168
	$form->addElement('html', '<br />');
169
	$form->addElement('style_submit_button', null, get_lang('ImportBackup'), 'class="save"');
170

    
171
	$values['backup_type'] = 'local';
172
	$values['import_option'] = 'full_backup';
173
	$values['same_file_name_option'] = FILE_OVERWRITE;
174
	$form->setDefaults($values);
175

    
176
	$form->add_progress_bar();
177
	// When progress bar appears we have to hide the title "Select backup file".
178
	$form->updateAttributes(array('onsubmit' => str_replace('javascript: ', 'javascript: page_title = getElementById(\'page_title\'); if (page_title) { setTimeout(\'page_title.style.display = \\\'none\\\';\', 2000); } ', $form->getAttribute('onsubmit'))));
179

    
180
	$form->display();
181
}
182

    
183
/*	FOOTER */
184
Display::display_footer();
(2-2/3)