3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This script delegates file serving to individual plugins
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 // disable moodle specific debug messages and any errors in output
28 //define('NO_DEBUG_DISPLAY', true);
29 //TODO: uncomment this once the file api stabilises a bit more
31 require_once('config.php');
32 require_once('lib/filelib.php');
34 $relativepath = get_file_argument();
35 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL
);
37 // relative path must start with '/'
39 print_error('invalidargorconf');
40 } else if ($relativepath[0] != '/') {
41 print_error('pathdoesnotstartslash');
44 // extract relative path components
45 $args = explode('/', ltrim($relativepath, '/'));
47 if (count($args) < 3) { // always at least context, component and filearea
48 print_error('invalidarguments');
51 $contextid = (int)array_shift($args);
52 $component = clean_param(array_shift($args), PARAM_COMPONENT
);
53 $filearea = clean_param(array_shift($args), PARAM_AREA
);
55 list($context, $course, $cm) = get_context_info_array($contextid);
57 $fs = get_file_storage();
59 // ========================================================================================================================
60 if ($component === 'blog') {
62 if ($context->contextlevel
!= CONTEXT_SYSTEM
) {
63 send_file_not_found();
65 if ($filearea !== 'attachment' and $filearea !== 'post') {
66 send_file_not_found();
69 if (empty($CFG->bloglevel
)) {
70 print_error('siteblogdisable', 'blog');
73 if ($CFG->bloglevel
< BLOG_GLOBAL_LEVEL
) {
76 print_error('noguest');
78 if ($CFG->bloglevel
== BLOG_USER_LEVEL
) {
79 if ($USER->id
!= $entry->userid
) {
80 send_file_not_found();
84 $entryid = (int)array_shift($args);
85 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
86 send_file_not_found();
89 if ('publishstate' === 'public') {
90 if ($CFG->forcelogin
) {
94 } else if ('publishstate' === 'site') {
97 } else if ('publishstate' === 'draft') {
99 if ($USER->id
!= $entry->userid
) {
100 send_file_not_found();
104 $filename = array_pop($args);
105 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
107 if (!$file = $fs->get_file($context->id
, $component, $filearea, $entryid, $filepath, $filename) or $file->is_directory()) {
108 send_file_not_found();
111 send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!
113 // ========================================================================================================================
114 } else if ($component === 'grade') {
115 if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel
== CONTEXT_SYSTEM
) {
116 // Global gradebook files
117 if ($CFG->forcelogin
) {
121 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
123 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
124 send_file_not_found();
127 session_get_instance()->write_close(); // unlock session during fileserving
128 send_stored_file($file, 60*60, 0, $forcedownload);
130 } else if ($filearea === 'feedback' and $context->contextlevel
== CONTEXT_COURSE
) {
131 //TODO: nobody implemented this yet in grade edit form!!
132 send_file_not_found();
134 if ($CFG->forcelogin ||
$course->id
!= SITEID
) {
135 require_login($course);
138 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
140 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
141 send_file_not_found();
144 session_get_instance()->write_close(); // unlock session during fileserving
145 send_stored_file($file, 60*60, 0, $forcedownload);
147 send_file_not_found();
150 // ========================================================================================================================
151 } else if ($component === 'tag') {
152 if ($filearea === 'description' and $context->contextlevel
== CONTEXT_SYSTEM
) {
154 // All tag descriptions are going to be public but we still need to respect forcelogin
155 if ($CFG->forcelogin
) {
159 $fullpath = "/$context->id/tag/description/".implode('/', $args);
161 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
162 send_file_not_found();
165 session_get_instance()->write_close(); // unlock session during fileserving
166 send_stored_file($file, 60*60, 0, true);
169 send_file_not_found();
172 // ========================================================================================================================
173 } else if ($component === 'calendar') {
174 if ($filearea === 'event_description' and $context->contextlevel
== CONTEXT_SYSTEM
) {
176 // All events here are public the one requirement is that we respect forcelogin
177 if ($CFG->forcelogin
) {
181 // Get the event if from the args array
182 $eventid = array_shift($args);
184 // Load the event from the database
185 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'eventtype'=>'site'))) {
186 send_file_not_found();
188 // Check that we got an event and that it's userid is that of the user
190 // Get the file and serve if successful
191 $filename = array_pop($args);
192 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
193 if (!$file = $fs->get_file($context->id
, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
194 send_file_not_found();
197 session_get_instance()->write_close(); // unlock session during fileserving
198 send_stored_file($file, 60*60, 0, $forcedownload);
200 } else if ($filearea === 'event_description' and $context->contextlevel
== CONTEXT_USER
) {
202 // Must be logged in, if they are not then they obviously can't be this user
205 // Don't want guests here, potentially saves a DB call
207 send_file_not_found();
210 // Get the event if from the args array
211 $eventid = array_shift($args);
213 // Load the event from the database - user id must match
214 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'userid'=>$USER->id
, 'eventtype'=>'user'))) {
215 send_file_not_found();
218 // Get the file and serve if successful
219 $filename = array_pop($args);
220 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
221 if (!$file = $fs->get_file($context->id
, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
222 send_file_not_found();
225 session_get_instance()->write_close(); // unlock session during fileserving
226 send_stored_file($file, 60*60, 0, $forcedownload);
228 } else if ($filearea === 'event_description' and $context->contextlevel
== CONTEXT_COURSE
) {
230 // Respect forcelogin and require login unless this is the site.... it probably
231 // should NEVER be the site
232 if ($CFG->forcelogin ||
$course->id
!= SITEID
) {
233 require_login($course);
236 // Must be able to at least view the course
237 if (!is_enrolled($context) and !is_viewing($context)) {
238 //TODO: hmm, do we really want to block guests here?
239 send_file_not_found();
243 $eventid = array_shift($args);
245 // Load the event from the database we need to check whether it is
246 // a) valid course event
248 // Group events use the course context (there is no group context)
249 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'courseid'=>$course->id
))) {
250 send_file_not_found();
253 // If its a group event require either membership of view all groups capability
254 if ($event->eventtype
=== 'group') {
255 if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($event->groupid
, $USER->id
)) {
256 send_file_not_found();
258 } else if ($event->eventtype
=== 'course') {
262 send_file_not_found();
265 // If we get this far we can serve the file
266 $filename = array_pop($args);
267 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
268 if (!$file = $fs->get_file($context->id
, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
269 send_file_not_found();
272 session_get_instance()->write_close(); // unlock session during fileserving
273 send_stored_file($file, 60*60, 0, $forcedownload);
276 send_file_not_found();
279 // ========================================================================================================================
280 } else if ($component === 'user') {
281 if ($filearea === 'icon' and $context->contextlevel
== CONTEXT_USER
) {
283 if (count($args) == 1) {
284 $themename = theme_config
::DEFAULT_THEME
;
285 $filename = array_shift($args);
287 $themename = array_shift($args);
288 $filename = array_shift($args);
290 if ((!empty($CFG->forcelogin
) and !isloggedin())) {
291 // protect images if login required and not logged in;
292 // do not use require_login() because it is expensive and not suitable here anyway
295 if (!$redirect and ($filename !== 'f1' and $filename !== 'f2')) {
299 if (!$redirect && !$file = $fs->get_file($context->id
, 'user', 'icon', 0, '/', $filename.'/.png')) {
300 if (!$file = $fs->get_file($context->id
, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
305 $theme = theme_config
::load($themename);
306 redirect($theme->pix_url('u/'.$filename, 'moodle'));
308 send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page
310 } else if ($filearea === 'private' and $context->contextlevel
== CONTEXT_USER
) {
314 send_file_not_found();
317 if ($USER->id
!== $context->instanceid
) {
318 send_file_not_found();
321 $filename = array_pop($args);
322 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
323 if (!$file = $fs->get_file($context->id
, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
324 send_file_not_found();
327 session_get_instance()->write_close(); // unlock session during fileserving
328 send_stored_file($file, 0, 0, true); // must force download - security!
330 } else if ($filearea === 'profile' and $context->contextlevel
== CONTEXT_USER
) {
332 if ($CFG->forcelogin
) {
336 $userid = $context->instanceid
;
338 if ($USER->id
== $userid) {
339 // always can access own
341 } else if (!empty($CFG->forceloginforprofiles
)) {
345 send_file_not_found();
348 // we allow access to site profile of all course contacts (usually teachers)
349 if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) {
350 send_file_not_found();
354 if (has_capability('moodle/user:viewdetails', $context)) {
357 $courses = enrol_get_my_courses();
360 while (!$canview && count($courses) > 0) {
361 $course = array_shift($courses);
362 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
368 $filename = array_pop($args);
369 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
370 if (!$file = $fs->get_file($context->id
, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
371 send_file_not_found();
374 session_get_instance()->write_close(); // unlock session during fileserving
375 send_stored_file($file, 0, 0, true); // must force download - security!
377 } else if ($filearea === 'profile' and $context->contextlevel
== CONTEXT_COURSE
) {
378 $userid = (int)array_shift($args);
379 $usercontext = get_context_instance(CONTEXT_USER
, $userid);
381 if ($CFG->forcelogin
) {
385 if (!empty($CFG->forceloginforprofiles
)) {
388 print_error('noguest');
391 //TODO: review this logic of user profile access prevention
392 if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) {
393 print_error('usernotavailable');
395 if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) {
396 print_error('cannotviewprofile');
398 if (!is_enrolled($context, $userid)) {
399 print_error('notenrolledprofile');
401 if (groups_get_course_groupmode($course) == SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $context)) {
402 print_error('groupnotamember');
406 $filename = array_pop($args);
407 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
408 if (!$file = $fs->get_file($usercontext->id
, 'user', 'profile', 0, $filepath, $filename) or $file->is_directory()) {
409 send_file_not_found();
412 session_get_instance()->write_close(); // unlock session during fileserving
413 send_stored_file($file, 0, 0, true); // must force download - security!
415 } else if ($filearea === 'backup' and $context->contextlevel
== CONTEXT_USER
) {
419 send_file_not_found();
421 $userid = $context->instanceid
;
423 if ($USER->id
!= $userid) {
424 send_file_not_found();
427 $filename = array_pop($args);
428 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
429 if (!$file = $fs->get_file($context->id
, 'user', 'backup', 0, $filepath, $filename) or $file->is_directory()) {
430 send_file_not_found();
433 session_get_instance()->write_close(); // unlock session during fileserving
434 send_stored_file($file, 0, 0, true); // must force download - security!
437 send_file_not_found();
440 // ========================================================================================================================
441 } else if ($component === 'coursecat') {
442 if ($context->contextlevel
!= CONTEXT_COURSECAT
) {
443 send_file_not_found();
446 if ($filearea === 'description') {
447 if ($CFG->forcelogin
) {
448 // no login necessary - unless login forced everywhere
452 $filename = array_pop($args);
453 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
454 if (!$file = $fs->get_file($context->id
, 'coursecat', 'description', 0, $filepath, $filename) or $file->is_directory()) {
455 send_file_not_found();
458 session_get_instance()->write_close(); // unlock session during fileserving
459 send_stored_file($file, 60*60, 0, $forcedownload);
461 send_file_not_found();
464 // ========================================================================================================================
465 } else if ($component === 'course') {
466 if ($context->contextlevel
!= CONTEXT_COURSE
) {
467 send_file_not_found();
470 if ($filearea === 'summary') {
471 if ($CFG->forcelogin
) {
475 $filename = array_pop($args);
476 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
477 if (!$file = $fs->get_file($context->id
, 'course', 'summary', 0, $filepath, $filename) or $file->is_directory()) {
478 send_file_not_found();
481 session_get_instance()->write_close(); // unlock session during fileserving
482 send_stored_file($file, 60*60, 0, $forcedownload);
484 } else if ($filearea === 'section') {
485 if ($CFG->forcelogin
) {
486 require_login($course);
487 } else if ($course->id
!= SITEID
) {
488 require_login($course);
491 $sectionid = (int)array_shift($args);
493 if (!$section = $DB->get_record('course_sections', array('id'=>$sectionid, 'course'=>$course->id
))) {
494 send_file_not_found();
497 if ($course->numsections
< $section->section
) {
498 if (!has_capability('moodle/course:update', $context)) {
499 // block access to unavailable sections if can not edit course
500 send_file_not_found();
504 $filename = array_pop($args);
505 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
506 if (!$file = $fs->get_file($context->id
, 'course', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
507 send_file_not_found();
510 session_get_instance()->write_close(); // unlock session during fileserving
511 send_stored_file($file, 60*60, 0, $forcedownload);
514 send_file_not_found();
517 } else if ($component === 'group') {
518 if ($context->contextlevel
!= CONTEXT_COURSE
) {
519 send_file_not_found();
522 require_course_login($course, true, null, false);
524 $groupid = (int)array_shift($args);
526 $group = $DB->get_record('groups', array('id'=>$groupid, 'courseid'=>$course->id
), '*', MUST_EXIST
);
527 if (($course->groupmodeforce
and $course->groupmode
== SEPARATEGROUPS
) and !has_capability('moodle/site:accessallgroups', $context) and !groups_is_member($group->id
, $USER->id
)) {
528 // do not allow access to separate group info if not member or teacher
529 send_file_not_found();
532 if ($filearea === 'description') {
534 require_login($course);
536 $filename = array_pop($args);
537 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
538 if (!$file = $fs->get_file($context->id
, 'group', 'description', $group->id
, $filepath, $filename) or $file->is_directory()) {
539 send_file_not_found();
542 session_get_instance()->write_close(); // unlock session during fileserving
543 send_stored_file($file, 60*60, 0, $forcedownload);
545 } else if ($filearea === 'icon') {
546 $filename = array_pop($args);
548 if ($filename !== 'f1' and $filename !== 'f2') {
549 send_file_not_found();
551 if (!$file = $fs->get_file($context->id
, 'group', 'icon', $group->id
, '/', $filename.'.png')) {
552 if (!$file = $fs->get_file($context->id
, 'group', 'icon', $group->id
, '/', $filename.'.jpg')) {
553 send_file_not_found();
557 session_get_instance()->write_close(); // unlock session during fileserving
558 send_stored_file($file, 60*60);
561 send_file_not_found();
564 } else if ($component === 'grouping') {
565 if ($context->contextlevel
!= CONTEXT_COURSE
) {
566 send_file_not_found();
569 require_login($course);
571 $groupingid = (int)array_shift($args);
573 // note: everybody has access to grouping desc images for now
574 if ($filearea === 'description') {
576 $filename = array_pop($args);
577 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
578 if (!$file = $fs->get_file($context->id
, 'grouping', 'description', $groupingid, $filepath, $filename) or $file->is_directory()) {
579 send_file_not_found();
582 session_get_instance()->write_close(); // unlock session during fileserving
583 send_stored_file($file, 60*60, 0, $forcedownload);
586 send_file_not_found();
589 // ========================================================================================================================
590 } else if ($component === 'backup') {
591 if ($filearea === 'course' and $context->contextlevel
== CONTEXT_COURSE
) {
592 require_login($course);
593 require_capability('moodle/backup:downloadfile', $context);
595 $filename = array_pop($args);
596 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
597 if (!$file = $fs->get_file($context->id
, 'backup', 'course', 0, $filepath, $filename) or $file->is_directory()) {
598 send_file_not_found();
601 session_get_instance()->write_close(); // unlock session during fileserving
602 send_stored_file($file, 0, 0, $forcedownload);
604 } else if ($filearea === 'section' and $context->contextlevel
== CONTEXT_COURSE
) {
605 require_login($course);
606 require_capability('moodle/backup:downloadfile', $context);
608 $sectionid = (int)array_shift($args);
610 $filename = array_pop($args);
611 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
612 if (!$file = $fs->get_file($context->id
, 'backup', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
613 send_file_not_found();
616 session_get_instance()->write_close();
617 send_stored_file($file, 60*60, 0, $forcedownload);
619 } else if ($filearea === 'activity' and $context->contextlevel
== CONTEXT_MODULE
) {
620 require_login($course, false, $cm);
621 require_capability('moodle/backup:downloadfile', $context);
623 $filename = array_pop($args);
624 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
625 if (!$file = $fs->get_file($context->id
, 'backup', 'activity', 0, $filepath, $filename) or $file->is_directory()) {
626 send_file_not_found();
629 session_get_instance()->write_close();
630 send_stored_file($file, 60*60, 0, $forcedownload);
632 } else if ($filearea === 'automated' and $context->contextlevel
== CONTEXT_COURSE
) {
633 // Backup files that were generated by the automated backup systems.
635 require_login($course);
636 require_capability('moodle/site:config', $context);
638 $filename = array_pop($args);
639 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
640 if (!$file = $fs->get_file($context->id
, 'backup', 'automated', 0, $filepath, $filename) or $file->is_directory()) {
641 send_file_not_found();
644 session_get_instance()->write_close(); // unlock session during fileserving
645 send_stored_file($file, 0, 0, $forcedownload);
648 send_file_not_found();
651 // ========================================================================================================================
652 } else if ($component === 'question') {
653 require_once($CFG->libdir
. '/questionlib.php');
654 question_pluginfile($course, $context, 'question', $filearea, $args, $forcedownload);
655 send_file_not_found();
657 // ========================================================================================================================
658 } else if (strpos($component, 'mod_') === 0) {
659 $modname = substr($component, 4);
660 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
661 send_file_not_found();
663 require_once("$CFG->dirroot/mod/$modname/lib.php");
665 if ($context->contextlevel
== CONTEXT_MODULE
) {
666 if ($cm->modname
!== $modname) {
667 // somebody tries to gain illegal access, cm type must match the component!
668 send_file_not_found();
672 if ($filearea === 'intro') {
673 if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO
, true)) {
674 send_file_not_found();
676 require_course_login($course, true, $cm);
678 // all users may access it
679 $filename = array_pop($args);
680 $filepath = $args ?
'/'.implode('/', $args).'/' : '/';
681 if (!$file = $fs->get_file($context->id
, 'mod_'.$modname, 'intro', 0, $filepath, $filename) or $file->is_directory()) {
682 send_file_not_found();
685 $lifetime = isset($CFG->filelifetime
) ?
$CFG->filelifetime
: 86400;
687 // finally send the file
688 send_stored_file($file, $lifetime, 0);
691 $filefunction = $component.'_pluginfile';
692 $filefunctionold = $modname.'_pluginfile';
693 if (function_exists($filefunction)) {
694 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
695 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
696 } else if (function_exists($filefunctionold)) {
697 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
698 $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload);
701 send_file_not_found();
703 // ========================================================================================================================
704 } else if (strpos($component, 'block_') === 0) {
705 $blockname = substr($component, 6);
706 // note: no more class methods in blocks please, that is ....
707 if (!file_exists("$CFG->dirroot/blocks/$blockname/lib.php")) {
708 send_file_not_found();
710 require_once("$CFG->dirroot/blocks/$blockname/lib.php");
712 if ($context->contextlevel
== CONTEXT_BLOCK
) {
713 $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid
), '*',MUST_EXIST
);
714 if ($birecord->blockname
!== $blockname) {
715 // somebody tries to gain illegal access, cm type must match the component!
716 send_file_not_found();
722 $filefunction = $component.'_pluginfile';
723 if (function_exists($filefunction)) {
724 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
725 $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload);
728 send_file_not_found();
730 } else if (strpos($component, '_') === false) {
731 // all core subsystems have to be specified above, no more guessing here!
732 send_file_not_found();
735 // try to serve general plugin file in arbitrary context
736 $dir = get_component_directory($component);
737 if (!file_exists("$dir/lib.php")) {
738 send_file_not_found();
740 include_once("$dir/lib.php");
742 $filefunction = $component.'_pluginfile';
743 if (function_exists($filefunction)) {
744 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
745 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
748 send_file_not_found();