MDL-52217 repository: prepare_file should use per-request directory
[moodle.git] / backup / restorefile.php
blob411aeb607d7e755c1bdb979396be5fb3972f23f3
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Import backup file or select existing backup file from moodle
20 * @package moodlecore
21 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once(dirname(__FILE__) . '/restorefile_form.php');
27 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
29 // current context
30 $contextid = required_param('contextid', PARAM_INT);
31 $filecontextid = optional_param('filecontextid', 0, PARAM_INT);
32 // action
33 $action = optional_param('action', '', PARAM_ALPHA);
34 // file parameters
35 // non js interface may require these parameters
36 $component = optional_param('component', null, PARAM_COMPONENT);
37 $filearea = optional_param('filearea', null, PARAM_AREA);
38 $itemid = optional_param('itemid', null, PARAM_INT);
39 $filepath = optional_param('filepath', null, PARAM_PATH);
40 $filename = optional_param('filename', null, PARAM_FILE);
42 list($context, $course, $cm) = get_context_info_array($contextid);
44 // will be used when restore
45 if (!empty($filecontextid)) {
46 $filecontext = context::instance_by_id($filecontextid);
49 $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid));
51 switch ($context->contextlevel) {
52 case CONTEXT_MODULE:
53 $heading = get_string('restoreactivity', 'backup');
54 break;
55 case CONTEXT_COURSE:
56 default:
57 $heading = get_string('restorecourse', 'backup');
61 require_login($course, false, $cm);
62 require_capability('moodle/restore:restorecourse', $context);
64 if (is_null($course)) {
65 $courseid = 0;
66 $coursefullname = $SITE->fullname;
67 } else {
68 $courseid = $course->id;
69 $coursefullname = $course->fullname;
72 $browser = get_file_browser();
74 // check if tmp dir exists
75 $tmpdir = $CFG->tempdir . '/backup';
76 if (!check_dir_exists($tmpdir, true, true)) {
77 throw new restore_controller_exception('cannot_create_backup_temp_dir');
80 // choose the backup file from backup files tree
81 if ($action == 'choosebackupfile') {
82 if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) {
83 if (is_a($fileinfo, 'file_info_stored')) {
84 // Use the contenthash rather than copying the file where possible,
85 // to improve performance and avoid timeouts with large files.
86 $fs = get_file_storage();
87 $params = $fileinfo->get_params();
88 $file = $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
89 $params['itemid'], $params['filepath'], $params['filename']);
90 $restore_url = new moodle_url('/backup/restore.php', array('contextid' => $contextid,
91 'pathnamehash' => $file->get_pathnamehash(), 'contenthash' => $file->get_contenthash()));
92 } else {
93 // If it's some weird other kind of file then use old code.
94 $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
95 $pathname = $tmpdir . '/' . $filename;
96 $fileinfo->copy_to_pathname($pathname);
97 $restore_url = new moodle_url('/backup/restore.php', array(
98 'contextid' => $contextid, 'filename' => $filename));
100 redirect($restore_url);
101 } else {
102 redirect($url, get_string('filenotfound', 'error'));
104 die;
107 $PAGE->set_url($url);
108 $PAGE->set_context($context);
109 $PAGE->set_title(get_string('course') . ': ' . $coursefullname);
110 $PAGE->set_heading($heading);
111 $PAGE->set_pagelayout('admin');
113 $form = new course_restore_form(null, array('contextid'=>$contextid));
114 $data = $form->get_data();
115 if ($data && has_capability('moodle/restore:uploadfile', $context)) {
116 $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
117 $pathname = $tmpdir . '/' . $filename;
118 $form->save_file('backupfile', $pathname);
119 $restore_url = new moodle_url('/backup/restore.php', array('contextid'=>$contextid, 'filename'=>$filename));
120 redirect($restore_url);
121 die;
126 echo $OUTPUT->header();
128 // require uploadfile cap to use file picker
129 if (has_capability('moodle/restore:uploadfile', $context)) {
130 echo $OUTPUT->heading(get_string('importfile', 'backup'));
131 echo $OUTPUT->container_start();
132 $form->display();
133 echo $OUTPUT->container_end();
136 if ($context->contextlevel == CONTEXT_MODULE) {
137 echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup');
138 echo $OUTPUT->container_start();
139 $treeview_options = array();
140 $user_context = context_user::instance($USER->id);
141 $treeview_options['filecontext'] = $context;
142 $treeview_options['currentcontext'] = $context;
143 $treeview_options['component'] = 'backup';
144 $treeview_options['context'] = $context;
145 $treeview_options['filearea'] = 'activity';
146 $renderer = $PAGE->get_renderer('core', 'backup');
147 echo $renderer->backup_files_viewer($treeview_options);
148 echo $OUTPUT->container_end();
151 echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup');
152 echo $OUTPUT->container_start();
153 $treeview_options = array();
154 $treeview_options['filecontext'] = $context;
155 $treeview_options['currentcontext'] = $context;
156 $treeview_options['component'] = 'backup';
157 $treeview_options['context'] = $context;
158 $treeview_options['filearea'] = 'course';
159 $renderer = $PAGE->get_renderer('core', 'backup');
160 echo $renderer->backup_files_viewer($treeview_options);
161 echo $OUTPUT->container_end();
163 echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup');
164 echo $OUTPUT->container_start();
165 $treeview_options = array();
166 $user_context = context_user::instance($USER->id);
167 $treeview_options['filecontext'] = $user_context;
168 $treeview_options['currentcontext'] = $context;
169 $treeview_options['component'] = 'user';
170 $treeview_options['context'] = 'backup';
171 $treeview_options['filearea'] = 'backup';
172 $renderer = $PAGE->get_renderer('core', 'backup');
173 echo $renderer->backup_files_viewer($treeview_options);
174 echo $OUTPUT->container_end();
176 $automatedbackups = get_config('backup', 'backup_auto_active');
177 if (!empty($automatedbackups)) {
178 echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup');
179 echo $OUTPUT->container_start();
180 $treeview_options = array();
181 $user_context = context_user::instance($USER->id);
182 $treeview_options['filecontext'] = $context;
183 $treeview_options['currentcontext'] = $context;
184 $treeview_options['component'] = 'backup';
185 $treeview_options['context'] = $context;
186 $treeview_options['filearea'] = 'automated';
187 $renderer = $PAGE->get_renderer('core', 'backup');
188 echo $renderer->backup_files_viewer($treeview_options);
189 echo $OUTPUT->container_end();
192 echo $OUTPUT->footer();