course publication MDL-19315 split /lib/hublib.php into admin/registration/lib.php...
[moodle.git] / pluginfile.php
CommitLineData
d92e7c4d 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 * This script delegates file serving to individual plugins
20 *
21 * @package moodlecore
22 * @subpackage file
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
2e9b772f
PS
27// disable moodle specific debug messages and any errors in output
28define('NO_DEBUG_DISPLAY', true);
29
d92e7c4d 30require_once('config.php');
31require_once('lib/filelib.php');
32
d92e7c4d 33$relativepath = get_file_argument();
34$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
35
36// relative path must start with '/'
37if (!$relativepath) {
38 print_error('invalidargorconf');
29afd52b 39} else if ($relativepath[0] != '/') {
d92e7c4d 40 print_error('pathdoesnotstartslash');
41}
42
43// extract relative path components
44$args = explode('/', ltrim($relativepath, '/'));
45
46if (count($args) == 0) { // always at least user id
47 print_error('invalidarguments');
48}
49
50$contextid = (int)array_shift($args);
51$filearea = array_shift($args);
52
49375088 53if (!$context = get_context_instance_by_id($contextid)) {
54 send_file_not_found();
55}
d92e7c4d 56$fs = get_file_storage();
57
50f95991 58// If the file is a Flash file and that the user flash player is outdated return a flash upgrader MDL-20841
59$mimetype = mimeinfo('type', $args[count($args)-1]);
60if (!empty($CFG->excludeoldflashclients) && $mimetype == 'application/x-shockwave-flash'&& !empty($SESSION->flashversion)) {
61 $userplayerversion = explode('.', $SESSION->flashversion);
62 $requiredplayerversion = explode('.', $CFG->excludeoldflashclients);
63 $sendflashupgrader = true;
64}
65if (!empty($sendflashupgrader) && (($userplayerversion[0] < $requiredplayerversion[0]) ||
66 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] < $requiredplayerversion[1]) ||
67 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] == $requiredplayerversion[1]
68 && $userplayerversion[2] < $requiredplayerversion[2]))) {
69 $path = $CFG->dirroot."/lib/flashdetect/flashupgrade.swf"; // Alternate content asking user to upgrade Flash
70 $filename = "flashupgrade.swf";
71 $lifetime = 0; // Do not cache
72 send_file($path, $filename, $lifetime, 0, false, false, $mimetype);
d92e7c4d 73
50f95991 74} else if ($context->contextlevel == CONTEXT_SYSTEM) {
1c7b8b93 75 if ($filearea === 'blog_attachment' || $filearea === 'blog_post') {
d92e7c4d 76
77 if (empty($CFG->bloglevel)) {
78 print_error('siteblogdisable', 'blog');
79 }
80 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
81 require_login();
82 if (isguestuser()) {
83 print_error('noguest');
84 }
85 if ($CFG->bloglevel == BLOG_USER_LEVEL) {
86 if ($USER->id != $entry->userid) {
87 send_file_not_found();
88 }
89 }
90 }
91 $entryid = (int)array_shift($args);
92 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
93 send_file_not_found();
94 }
95 if ('publishstate' === 'public') {
96 if ($CFG->forcelogin) {
97 require_login();
98 }
172dd12c 99
d92e7c4d 100 } else if ('publishstate' === 'site') {
101 require_login();
102 //ok
103 } else if ('publishstate' === 'draft') {
104 require_login();
105 if ($USER->id != $entry->userid) {
106 send_file_not_found();
107 }
108 }
172dd12c 109
d92e7c4d 110 //TODO: implement shared course and shared group access
172dd12c 111
d92e7c4d 112 $relativepath = '/'.implode('/', $args);
1c7b8b93 113 $fullpath = $context->id.$filearea.$entryid.$relativepath;
172dd12c 114
d92e7c4d 115 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
116 send_file_not_found();
117 }
172dd12c 118
d92e7c4d 119 send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!
8bdc9cac
SH
120 } else if ($filearea === 'grade_outcome' || $filearea === 'grade_scale') { // CONTEXT_SYSTEM
121 if ($CFG->forcelogin) {
122 require_login();
123 }
124
125 $fullpath = $context->id.$filearea.implode('/', $args);
126
127 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
128 send_file_not_found();
129 }
130
131 session_get_instance()->write_close(); // unlock session during fileserving
132 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
133
134 } else if ($filearea === 'tag_description') { // CONTEXT_SYSTEM
135
136 // All tag descriptions are going to be public but we still need to respect forcelogin
137 if ($CFG->forcelogin) {
138 require_login();
139 }
140
141 $fullpath = $context->id.$filearea.implode('/', $args);
142
143 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
144 send_file_not_found();
145 }
146
147 session_get_instance()->write_close(); // unlock session during fileserving
148 send_stored_file($file, 60*60, 0, true); // TODO: change timeout?
149
76d9df3f
SH
150 } else if ($filearea === 'calendar_event_description') { // CONTEXT_SYSTEM
151
152 // All events here are public the one requirement is that we respect forcelogin
153 if ($CFG->forcelogin) {
154 require_login();
155 }
156
157 $fullpath = $context->id.$filearea.implode('/', $args);
158
159 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
160 send_file_not_found();
161 }
162
163 session_get_instance()->write_close(); // unlock session during fileserving
164 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
172dd12c 165
d92e7c4d 166 } else {
167 send_file_not_found();
172dd12c 168 }
169
d92e7c4d 170} else if ($context->contextlevel == CONTEXT_USER) {
76d9df3f
SH
171
172 if ($filearea === 'calendar_event_description') { // CONTEXT_USER
173
174 // Must be logged in, if they are not then they obviously can't be this user
175 require_login();
03221650 176
76d9df3f
SH
177 // Don't want guests here, potentially saves a DB call
178 if (isguestuser()) {
179 send_file_not_found();
180 }
181
182 // Get the event if from the args array
183 $eventid = array_shift($args);
184 if ((int)$eventid <= 0) {
185 send_file_not_found();
186 }
187
188 // Load the event from the database
189 $event = $DB->get_record('event', array('id'=>(int)$eventid));
190 // Check that we got an event and that it's userid is that of the user
191 if (!$event || $event->userid !== $USER->id) {
192 send_file_not_found();
193 }
194
195 // Get the file and serve if succesfull
196 $fullpath = $context->id.$filearea.$eventid.'/'.implode('/', $args);
197 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
198 send_file_not_found();
199 }
200
201 session_get_instance()->write_close(); // unlock session during fileserving
202 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
8bdc9cac
SH
203 } else if ($filearea === 'user_profile') { // CONTEXT_USER
204
205 if ($CFG->forcelogin) {
206 require_login();
207 }
208
209 $userid = array_shift($args);
210 if ((int)$userid <= 0) {
211 send_file_not_found();
212 }
213
214 if (!empty($CFG->forceloginforprofiles)) {
215 require_login();
216 if (isguestuser()) {
217 send_file_not_found();
218 }
219
220 if ($USER->id !== $userid) {
221 $usercontext = get_context_instance(CONTEXT_USER, $userid);
222 // The browsing user is not the current user
4f0c2d00 223 if (!has_coursemanager_role($userid) && !has_capability('moodle/user:viewdetails', $usercontext)) {
8bdc9cac
SH
224 send_file_not_found();
225 }
226
227 $canview = false;
228 if (has_capability('moodle/user:viewdetails', $usercontext)) {
229 $canview = true;
230 } else {
231 $courses = get_my_courses($USER->id);
232 }
233
234 while (!$canview && count($courses) > 0) {
235 $course = array_shift($courses);
236 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) {
237 $canview = true;
238 }
239 }
240 }
241 }
242
243 $fullpath = $context->id.$filearea.$userid.'/'.implode('/', $args);
244
245 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
246 send_file_not_found();
247 }
248
249 session_get_instance()->write_close(); // unlock session during fileserving
250 send_stored_file($file, 60*60, 0, true);
76d9df3f
SH
251 }
252
d92e7c4d 253 send_file_not_found();
172dd12c 254
255
d92e7c4d 256} else if ($context->contextlevel == CONTEXT_COURSECAT) {
257 if ($filearea !== 'coursecat_intro') {
258 send_file_not_found();
259 }
172dd12c 260
d92e7c4d 261 if ($CFG->forcelogin) {
262 // no login necessary - unless login forced everywhere
263 require_login();
264 }
172dd12c 265
d92e7c4d 266 $relativepath = '/'.implode('/', $args);
267 $fullpath = $context->id.'coursecat_intro0'.$relativepath;
172dd12c 268
d92e7c4d 269 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
270 send_file_not_found();
271 }
172dd12c 272
d92e7c4d 273 session_get_instance()->write_close(); // unlock session during fileserving
274 send_stored_file($file, 60*60, 0, $forcedownload);
172dd12c 275
172dd12c 276
d92e7c4d 277} else if ($context->contextlevel == CONTEXT_COURSE) {
278 if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
279 print_error('invalidcourseid');
280 }
172dd12c 281
d92e7c4d 282 if ($filearea === 'course_backup') {
283 require_login($course);
2fdde5d1 284 require_capability('moodle/backup:downloadfile', $context);
172dd12c 285
d92e7c4d 286 $relativepath = '/'.implode('/', $args);
287 $fullpath = $context->id.'course_backup0'.$relativepath;
172dd12c 288
d92e7c4d 289 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
9e5fa330 290 send_file_not_found();
172dd12c 291 }
292
d92e7c4d 293 session_get_instance()->write_close(); // unlock session during fileserving
294 send_stored_file($file, 0, 0, true);
295
296 } else if ($filearea === 'course_intro') {
172dd12c 297 if ($CFG->forcelogin) {
172dd12c 298 require_login();
299 }
300
301 $relativepath = '/'.implode('/', $args);
d92e7c4d 302 $fullpath = $context->id.'course_intro0'.$relativepath;
172dd12c 303
d92e7c4d 304 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
9e5fa330 305 send_file_not_found();
172dd12c 306 }
307
56949c17 308 session_get_instance()->write_close(); // unlock session during fileserving
d92e7c4d 309 send_stored_file($file, 60*60, 0, false); // TODO: change timeout?
172dd12c 310
8bdc9cac
SH
311 } else if ($filearea === 'course_summary') {
312
313 if ($CFG->forcelogin) {
314 require_login();
315 }
316
317 $fullpath = $context->id.$filearea.implode('/', $args);
03221650 318
8bdc9cac
SH
319 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
320 send_file_not_found();
321 }
322
323 session_get_instance()->write_close(); // unlock session during fileserving
324 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
325
326 } else if ($filearea === 'course_grade_tree_feedback') {
327
328 if ($CFG->forcelogin || $course->id !== SITEID) {
329 require_login($course);
330 }
331
332 $fullpath = $context->id.$filearea.implode('/', $args);
333
334 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
335 send_file_not_found();
336 }
337
338 session_get_instance()->write_close(); // unlock session during fileserving
339 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
340
76d9df3f
SH
341 } else if ($filearea === 'calendar_event_description') { // CONTEXT_COURSE
342
343 // This is for content used in course and group events
344
345 // Respect forcelogin and require login unless this is the site.... it probably
346 // should NEVER be the site
347 if ($CFG->forcelogin || $course->id !== SITEID) {
348 require_login($course);
349 }
350
351 // Must be able to at least view the course
4f0c2d00 352 if (!is_enrolled($context) and !is_viewing($context)) {
76d9df3f
SH
353 send_file_not_found();
354 }
355
356 // Get the event id
357 $eventid = array_shift($args);
358 if ((int)$eventid <= 0) {
359 send_file_not_found();
360 }
361
362 // Load the event from the database we need to check whether it is
363 // a) valid
364 // b) a group event
365 // Group events use the course context (there is no group context)
366 $event = $DB->get_record('event', array('id'=>(int)$eventid));
367 if (!$event || $event->userid !== $USER->id) {
368 send_file_not_found();
369 }
370
371 // If its a group event require either membership of manage groups capability
372 if ($event->eventtype === 'group' && !has_capability('moodle/course:managegroups', $context) && !groups_is_member($event->groupid, $USER->id)) {
373 send_file_not_found();
374 }
375
376 // If we get this far we can serve the file
377 $fullpath = $context->id.$filearea.$eventid.'/'.implode('/', $args);
378 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
379 send_file_not_found();
380 }
381
382 session_get_instance()->write_close(); // unlock session during fileserving
383 send_stored_file($file, 60*60, 0, $forcedownload); // TODO: change timeout?
384
d92e7c4d 385 } else if ($filearea === 'course_section') {
386 if ($CFG->forcelogin) {
172dd12c 387 require_login($course);
d92e7c4d 388 } else if ($course->id !== SITEID) {
389 require_login($course);
390 }
106f3b67 391
d92e7c4d 392 $sectionid = (int)array_shift($args);
106f3b67 393
d92e7c4d 394 if ($course->numsections < $sectionid) {
395 if (!has_capability('moodle/course:update', $context)) {
396 // disable access to invisible sections if can not edit course
397 // this is going to break some ugly hacks, but is necessary
106f3b67 398 send_file_not_found();
399 }
d92e7c4d 400 }
106f3b67 401
d92e7c4d 402 $relativepath = '/'.implode('/', $args);
403 $fullpath = $context->id.'course_section'.$sectionid.$relativepath;
172dd12c 404
d92e7c4d 405 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
406 send_file_not_found();
407 }
172dd12c 408
d92e7c4d 409 session_get_instance()->write_close(); // unlock session during fileserving
410 send_stored_file($file, 60*60, 0, false); // TODO: change timeout?
172dd12c 411
91b890ab
DC
412 } else if ($filearea === 'section_backup') {
413 require_login($course);
414 require_capability('moodle/backup:downloadfile', $context);
415
416 $sectionid = (int)array_shift($args);
417
418 $relativepath = '/'.implode('/', $args);
419 $fullpath = $context->id.'section_backup'.$sectionid.$relativepath;
420
421 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
422 send_file_not_found();
423 }
424
425 session_get_instance()->write_close();
426 send_stored_file($file, 60*60, 0, false);
427
d92e7c4d 428 } else if ($filearea === 'user_profile') {
429 $userid = (int)array_shift($args);
430 $usercontext = get_context_instance(CONTEXT_USER, $userid);
4f0c2d00
PS
431
432 if ($CFG->forcelogin) {
433 require_login();
434 }
3156b8ca 435
d92e7c4d 436 if (!empty($CFG->forceloginforprofiles)) {
437 require_login();
438 if (isguestuser()) {
439 print_error('noguest');
3156b8ca 440 }
441
4f0c2d00 442 if (!has_coursemanager_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) {
d92e7c4d 443 print_error('usernotavailable');
3156b8ca 444 }
d92e7c4d 445 if (!has_capability('moodle/user:viewdetails', $context) &&
446 !has_capability('moodle/user:viewdetails', $usercontext)) {
447 print_error('cannotviewprofile');
3156b8ca 448 }
4f0c2d00 449 if (!is_enrolled($context, $userid)) {
d92e7c4d 450 print_error('notenrolledprofile');
106f3b67 451 }
d92e7c4d 452 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
453 print_error('groupnotamember');
106f3b67 454 }
d92e7c4d 455 }
106f3b67 456
d92e7c4d 457 $relativepath = '/'.implode('/', $args);
458 $fullpath = $usercontext->id.'user_profile0'.$relativepath;
106f3b67 459
d92e7c4d 460 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
106f3b67 461 send_file_not_found();
462 }
172dd12c 463
d92e7c4d 464 session_get_instance()->write_close(); // unlock session during fileserving
465 send_stored_file($file, 0, 0, true); // must force download - security!
106f3b67 466
d92e7c4d 467 } else {
468 send_file_not_found();
469 }
470
471} else if ($context->contextlevel == CONTEXT_MODULE) {
472
473 if (!$coursecontext = get_context_instance_by_id(get_parent_contextid($context))) {
474 send_file_not_found();
475 }
476
477 if (!$course = $DB->get_record('course', array('id'=>$coursecontext->instanceid))) {
478 send_file_not_found();
479 }
480 $modinfo = get_fast_modinfo($course);
481 if (empty($modinfo->cms[$context->instanceid])) {
482 send_file_not_found();
483 }
484
485 $cminfo = $modinfo->cms[$context->instanceid];
486 $modname = $cminfo->modname;
487 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
488 if (!file_exists($libfile)) {
489 send_file_not_found();
490 }
172dd12c 491
d92e7c4d 492 require_once($libfile);
493 if ($filearea === $modname.'_intro') {
494 if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) {
9e5fa330 495 send_file_not_found();
172dd12c 496 }
57956cc3
PS
497 if (!$cm = get_coursemodule_from_instance($modname, $cminfo->instance, $course->id)) {
498 send_file_not_found();
499 }
500 require_course_login($course, true, $cm);
501
d92e7c4d 502 if (!$cminfo->uservisible) {
9e5fa330 503 send_file_not_found();
172dd12c 504 }
d92e7c4d 505 // all users may access it
506 $relativepath = '/'.implode('/', $args);
507 $fullpath = $context->id.$filearea.'0'.$relativepath;
172dd12c 508
d92e7c4d 509 $fs = get_file_storage();
510 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
dc5c2bd9 511 send_file_not_found();
512 }
513
d92e7c4d 514 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
dc5c2bd9 515
d92e7c4d 516 // finally send the file
517 send_stored_file($file, $lifetime, 0);
91b890ab
DC
518 } else if ($filearea === 'activity_backup') {
519 require_login($course);
520 require_capability('moodle/backup:downloadfile', $context);
521
522 $relativepath = '/'.implode('/', $args);
523 $fullpath = $context->id.'activity_backup0'.$relativepath;
524
525 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
526 send_file_not_found();
527 }
528
529 session_get_instance()->write_close();
530 send_stored_file($file, 60*60, 0, false);
d92e7c4d 531 }
532
533 $filefunction = $modname.'_pluginfile';
534 if (function_exists($filefunction)) {
520199ea 535 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
536 $filefunction($course, $cminfo, $context, $filearea, $args, $forcedownload);
b287ea7a 537 }
520199ea 538
539 send_file_not_found();
172dd12c 540
d92e7c4d 541} else if ($context->contextlevel == CONTEXT_BLOCK) {
172dd12c 542
f1cbbbeb
SH
543 if (!$context = get_context_instance_by_id($contextid)) {
544 send_file_not_found();
545 }
546 $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST);
547 $blockinstance = block_instance($birecord->blockname, $birecord);
548
549 if (strpos(get_class($blockinstance), $filearea) !== 0) {
550 send_file_not_found();
551 }
552
553 $itemid = array_shift($args);
554 $filename = array_pop($args);
555 $filepath = '/'.join('/', $args);
556
557 if (method_exists($blockinstance, 'send_file')) {
558 $blockinstance->send_file($context, $filearea, $itemid, $filepath, $filename);
559 }
560
561 send_file_not_found();
172dd12c 562
d92e7c4d 563} else {
564 send_file_not_found();
565}