mnet is not going to bork my sites with disabled mnet, sorry - preventing loading...
[moodle.git] / files / index.php
CommitLineData
be19e367 1<?php
2
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.
14//
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/>.
17
18/**
19 * Temporary file manager for all moodle files. To be replaced by something much better.
20 *
21 * @package moodlecore
22 * @subpackage file
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require('../config.php');
28require_once($CFG->libdir.'/filelib.php');
29require_once($CFG->libdir.'/adminlib.php');
30
31$courseid = optional_param('id', 0, PARAM_INT);
32
33$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
34$filearea = optional_param('filearea', '', PARAM_ALPHAEXT);
35$itemid = optional_param('itemid', -1, PARAM_INT);
36$filepath = optional_param('filepath', '', PARAM_PATH);
37$filename = optional_param('filename', '', PARAM_FILE);
38
39$newdirname = optional_param('newdirname', '', PARAM_FILE);
40$delete = optional_param('delete', 0, PARAM_BOOL);
41
42if ($courseid) {
1904e9b3
SH
43 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
44 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
49c8c8d2 45 redirect(new moodle_url('index.php', array('contextid' => $context->id, 'itemid'=> 0, 'filearea' => 'course_content')));
be19e367 46}
47
1904e9b3
SH
48$context = get_context_instance_by_id($contextid, MUST_EXIST);
49
50$course = null;
51$cm = null;
52if ($context->contextlevel == CONTEXT_MODULE) {
53 $cm = get_coursemodule_from_id(null, $context->instanceid, 0, false, MUST_EXIST);
54 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
55} else if ($context->contextlevel == CONTEXT_COURSE) {
56 $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
be19e367 57}
f9903ed0 58
1904e9b3 59require_login($course, false, $cm);
be19e367 60require_capability('moodle/course:managefiles', $context);
f9903ed0 61
be19e367 62if ($filearea === '') {
63 $filearea = null;
64}
ad4e0fcc 65
be19e367 66if ($itemid < 0) {
67 $itemid = null;
68}
f9903ed0 69
be19e367 70if ($filepath === '') {
71 $filepath = null;
72}
fdfc6269 73
be19e367 74if ($filename === '') {
75 $filename = null;
76}
f9903ed0 77
be19e367 78$error = '';
f9903ed0 79
be19e367 80$browser = get_file_browser();
1a11dfd1 81
be19e367 82$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename);
1a11dfd1 83
172dd12c 84/// process actions
be19e367 85if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) {
86 if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) {
5e991e37 87 $params = $newdir_info->get_params();
49c8c8d2 88 redirect(new moodle_url('index.php', $params));
be19e367 89 } else {
90 $error = "Could not create new dir"; // TODO: localise
172dd12c 91 }
be19e367 92}
1a11dfd1 93
be19e367 94if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
95 $file = $_FILES['newfile'];
96 $newfilename = clean_param($file['name'], PARAM_FILE);
97 if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
98 try {
99 if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) {
5e991e37 100 $params = $file_info->get_params();
49c8c8d2 101 redirect(new moodle_url('index.php', $params));
70cedbfd 102
be19e367 103 } else {
104 $error = "Could not create upload file"; // TODO: localise
f9903ed0 105 }
be19e367 106 } catch (file_exception $e) {
107 $error = "Exception: Could not create upload file"; // TODO: localise
172dd12c 108 }
109 }
be19e367 110}
e64984a0 111
be19e367 112if ($file_info and $delete) {
113 if (!data_submitted() or !confirm_sesskey()) {
390679e7
DC
114 $params = $file_info->get_params();
115 $params = array_merge($params, array('delete'=>1));
116 $PAGE->set_url("/files/index.php", $params);
a8206cc4
RW
117 $PAGE->set_title("$SITE->shortname: " .get_string('files'));
118 $PAGE->set_heading($SITE->fullname);
534f203d 119 echo $OUTPUT->header();
aa9a6867 120 echo $OUTPUT->notification(get_string('deletecheckwarning').': '.$file_info->get_visible_name());
be19e367 121 $parent_info = $file_info->get_parent();
e64984a0 122
be19e367 123 $optionsno = $parent_info->get_params();
124 $optionsyes = $file_info->get_params();
125 $optionsyes['delete'] = 1;
126 $optionsyes['sesskey'] = sesskey();
e64984a0 127
642816a6 128 echo $OUTPUT->confirm(get_string('deletecheckfiles'), new moodle_url( 'index.php', $optionsyes), new moodle_url('index.php', $optionsno));
7e0d6675 129 echo $OUTPUT->footer();
be19e367 130 die;
131 }
e64984a0 132
be19e367 133 if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) {
134 if (!$file_info->delete()) {
135 $error = "Could not delete file!"; // TODO: localise
172dd12c 136 }
5e991e37 137 $params = $parent_info->get_params();
49c8c8d2 138 redirect(new moodle_url('index.php', $params));
172dd12c 139 }
be19e367 140}
f9903ed0 141
f9903ed0 142
172dd12c 143/// print dir listing
be19e367 144html_header($context, $file_info);
f9903ed0 145
be19e367 146if ($error !== '') {
aa9a6867 147 echo $OUTPUT->notification($error);
be19e367 148}
f9903ed0 149
be19e367 150displaydir($file_info);
151
152if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {
153 echo '<br />';
154
155 echo '<form action="index.php" method="post"><div>';
156 echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
157 echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
158 echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
159 echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
160 echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
161 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
162 echo '<input type="text" name="newdirname" value="" />';
163 echo '<input type="submit" value="'.get_string('makeafolder').'" />';
164 echo '</div></form>';
165
166 echo '<br />';
167
168 echo '<form enctype="multipart/form-data" method="post" action="index.php"><div>';
169 echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
170 echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
171 echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
172 echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
173 echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
174 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
175 echo '<input name="newfile" type="file" />';
176 echo '<input type="submit" value="'.get_string('uploadafile').'" />';
177 echo '</div></form>';
178}
f9903ed0 179
be19e367 180html_footer();
f9903ed0 181
172dd12c 182/// UI functions /////////////////////////
183
184function html_footer() {
7e0d6675 185 global $OUTPUT;
172dd12c 186 echo '</td></tr></table>';
7e0d6675 187 echo $OUTPUT->footer();
f9903ed0 188}
189
172dd12c 190function html_header($context, $file_info){
534f203d 191 global $CFG, $SITE, $PAGE, $OUTPUT;
f9903ed0 192
172dd12c 193 $strfiles = get_string("files");
1904e9b3
SH
194 if ($context->contextlevel == CONTEXT_MODULE) {
195 $PAGE->set_pagelayout('incourse');
196 } else if ($context->contextlevel == CONTEXT_COURSE) {
197 $PAGE->set_pagelayout('course');
198 } else {
199 $PAGE->set_pagelayout('admin');
200 }
201 $PAGE->navbar->add($strfiles);
5e991e37 202 $PAGE->set_url("/files/index.php", $file_info->get_params());
534f203d 203 $PAGE->set_title("$SITE->shortname: $strfiles");
a8206cc4 204 $PAGE->set_heading($SITE->fullname);
534f203d 205 echo $OUTPUT->header();
f9903ed0 206
172dd12c 207 echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"740\">";
208 echo "<tr>";
209 echo "<td colspan=\"2\">";
f9903ed0 210}
211
172dd12c 212/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
f9903ed0 213
6b091c94 214function print_cell($alignment='center', $text='&nbsp;', $class='') {
215 if ($class) {
216 $class = ' class="'.$class.'"';
217 }
60a9a6ea 218 echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
f9903ed0 219}
220
172dd12c 221function displaydir($file_info) {
42d5737a 222 global $CFG, $OUTPUT;
172dd12c 223
224 $children = $file_info->get_children();
225 $parent_info = $file_info->get_parent();
226
227 $strname = get_string('name');
228 $strsize = get_string('size');
229 $strmodified = get_string('modified');
230 $strfolder = get_string('folder');
231 $strfile = get_string('file');
232 $strdownload = get_string('download');
233 $strdelete = get_string('delete');
234 $straction = get_string('action');
235
236 $path = array();
237 $params = $file_info->get_params_rawencoded();
238 $params = implode('&amp;', $params);
239 $path[] = $file_info->get_visible_name();
240
241 $level = $parent_info;
242 while ($level) {
243 $params = $level->get_params_rawencoded();
244 $params = implode('&amp;', $params);
245 $path[] = '<a href="index.php?'.$params.'">'.$level->get_visible_name().'</a>';
246 $level = $level->get_parent();
247 }
f9903ed0 248
172dd12c 249 $path = array_reverse($path);
f9903ed0 250
172dd12c 251 $path = implode (' / ', $path);
252 echo $path. ' /';
f9903ed0 253
60a9a6ea 254 echo "<div>";
60a9a6ea 255 echo "<hr/>";
172dd12c 256 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"740\" class=\"files\">";
9a58f7cb 257 echo "<tr>";
2eb467b3 258 echo "<th class=\"header\" scope=\"col\"></th>";
60a9a6ea 259 echo "<th class=\"header name\" scope=\"col\">$strname</th>";
260 echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
261 echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
262 echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
9a58f7cb 263 echo "</tr>\n";
f9903ed0 264
172dd12c 265 $parentwritable = $file_info->is_writable();
f9903ed0 266
172dd12c 267 if ($parent_info) {
268 $params = $parent_info->get_params_rawencoded();
269 $params = implode('&amp;', $params);
6b091c94 270
172dd12c 271 echo "<tr class=\"folder\">";
272 print_cell();
b5d0cafc 273 print_cell('left', '<a href="index.php?'.$params.'"><img src="'.$OUTPUT->pix_url('f/parent') . '" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>', 'name');
172dd12c 274 print_cell();
275 print_cell();
276 print_cell();
70cedbfd 277
172dd12c 278 echo "</tr>";
f9903ed0 279 }
280
172dd12c 281 if ($children) {
282 foreach ($children as $child_info) {
283 $filename = $child_info->get_visible_name();
284 $filesize = $child_info->get_filesize();
285 $filesize = $filesize ? display_size($filesize) : '';
286 $filedate = $child_info->get_timemodified();
287 $filedate = $filedate ? userdate($filedate) : '';
f9903ed0 288
172dd12c 289 $mimetype = $child_info->get_mimetype();
f9903ed0 290
172dd12c 291 $params = $child_info->get_params_rawencoded();
292 $params = implode('&amp;', $params);
f9903ed0 293
172dd12c 294 if ($child_info->is_directory()) {
8e73aa97 295
172dd12c 296 echo "<tr class=\"folder\">";
297 print_cell();
b5d0cafc 298 print_cell("left", "<a href=\"index.php?$params\"><img src=\"" . $OUTPUT->pix_url('f/folder') . "\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($filename)."</a>", 'name');
172dd12c 299 print_cell("right", $filesize, 'size');
300 print_cell("right", $filedate, 'date');
301 if ($parentwritable) {
b5d0cafc 302 print_cell("right", "<a href=\"index.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
172dd12c 303 } else {
304 print_cell();
305 }
306 echo "</tr>";
f9903ed0 307
172dd12c 308 } else {
5a254a29 309
172dd12c 310 if ($downloadurl = $child_info->get_url(true)) {
b5d0cafc 311 $downloadurl = "&nbsp;<a href=\"$downloadurl\" title=\"" . get_string('downloadfile') . "\"><img src=\"" . $OUTPUT->pix_url('t/down') . "\" class=\"iconsmall\" alt=\"$strdownload\" /></a>";
172dd12c 312 } else {
313 $downloadurl = '';
314 }
f9903ed0 315
172dd12c 316 if ($viewurl = $child_info->get_url()) {
75015e5f
PS
317 $viewurl = "&nbsp;".$OUTPUT->action_link($viewurl, "<img src=\"" . $OUTPUT->pix_url('t/preview') . "\" class=\"iconsmall\" alt=\"$strfile\" />&nbsp;",
318 new popup_action('click', $viewurl, 'display', array('height' => 480, 'width' => 640)));
172dd12c 319 } else {
320 $viewurl = '';
321 }
8e73aa97 322
8e73aa97 323
324
172dd12c 325 echo "<tr class=\"file\">";
326 print_cell();
b5d0cafc 327 print_cell("left", "<img src=\"" . $OUTPUT->pix_url(file_mimetype_icon($mimetype)) . "\" class=\"icon\" alt=\"$strfile\" />&nbsp;".s($filename).$downloadurl.$viewurl, 'name');
172dd12c 328 print_cell("right", $filesize, 'size');
329 print_cell("right", $filedate, 'date');
330 if ($parentwritable) {
b5d0cafc 331 print_cell("right", "<a href=\"index.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
172dd12c 332 } else {
333 print_cell();
476fd9c7 334 }
172dd12c 335 echo "</tr>";
f9903ed0 336 }
f9903ed0 337 }
338 }
f98e18e6 339
9a58f7cb 340 echo "</table>";
172dd12c 341 echo "</div>";
60a9a6ea 342 echo "<hr/>";
f9903ed0 343
390679e7 344}