Merge branch 'install_21_STABLE' of git://github.com/amosbot/moodle into MOODLE_21_STABLE
[moodle.git] / pluginfile.php
blob244f8c8447b73b4359cb24087b47613a918b2b6b
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 * This script delegates file serving to individual plugins
21 * @package core
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
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 '/'
38 if (!$relativepath) {
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_SAFEDIR);
53 $filearea = clean_param(array_shift($args), PARAM_SAFEDIR);
55 list($context, $course, $cm) = get_context_info_array($contextid);
57 $fs = get_file_storage();
59 // If the file is a Flash file and that the user flash player is outdated return a flash upgrader MDL-20841
60 $mimetype = mimeinfo('type', $args[count($args)-1]);
61 if (!empty($CFG->excludeoldflashclients) && $mimetype == 'application/x-shockwave-flash'&& !empty($SESSION->flashversion)) {
62 $userplayerversion = explode('.', $SESSION->flashversion);
63 $requiredplayerversion = explode('.', $CFG->excludeoldflashclients);
64 if (($userplayerversion[0] < $requiredplayerversion[0]) ||
65 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] < $requiredplayerversion[1]) ||
66 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] == $requiredplayerversion[1]
67 && $userplayerversion[2] < $requiredplayerversion[2])) {
68 $path = $CFG->dirroot."/lib/flashdetect/flashupgrade.swf"; // Alternate content asking user to upgrade Flash
69 $filename = "flashupgrade.swf";
70 send_file($path, $filename, O, 0, false, false, 'application/x-shockwave-flash'); // Do not cache
74 // ========================================================================================================================
75 if ($component === 'blog') {
76 // Blog file serving
77 if ($context->contextlevel != CONTEXT_SYSTEM) {
78 send_file_not_found();
80 if ($filearea !== 'attachment' and $filearea !== 'post') {
81 send_file_not_found();
84 if (empty($CFG->bloglevel)) {
85 print_error('siteblogdisable', 'blog');
88 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
89 require_login();
90 if (isguestuser()) {
91 print_error('noguest');
93 if ($CFG->bloglevel == BLOG_USER_LEVEL) {
94 if ($USER->id != $entry->userid) {
95 send_file_not_found();
99 $entryid = (int)array_shift($args);
100 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
101 send_file_not_found();
104 if ('publishstate' === 'public') {
105 if ($CFG->forcelogin) {
106 require_login();
109 } else if ('publishstate' === 'site') {
110 require_login();
111 //ok
112 } else if ('publishstate' === 'draft') {
113 require_login();
114 if ($USER->id != $entry->userid) {
115 send_file_not_found();
119 $filename = array_pop($args);
120 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
122 if (!$file = $fs->get_file($context->id, $component, $filearea, $entryid, $filepath, $filename) or $file->is_directory()) {
123 send_file_not_found();
126 send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!
128 // ========================================================================================================================
129 } else if ($component === 'grade') {
130 if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) {
131 // Global gradebook files
132 if ($CFG->forcelogin) {
133 require_login();
136 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
138 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
139 send_file_not_found();
142 session_get_instance()->write_close(); // unlock session during fileserving
143 send_stored_file($file, 60*60, 0, $forcedownload);
145 } else if ($filearea === 'feedback' and $context->contextlevel == CONTEXT_COURSE) {
146 //TODO: nobody implemented this yet in grade edit form!!
147 send_file_not_found();
149 if ($CFG->forcelogin || $course->id != SITEID) {
150 require_login($course);
153 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
155 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
156 send_file_not_found();
159 session_get_instance()->write_close(); // unlock session during fileserving
160 send_stored_file($file, 60*60, 0, $forcedownload);
161 } else {
162 send_file_not_found();
165 // ========================================================================================================================
166 } else if ($component === 'tag') {
167 if ($filearea === 'description' and $context->contextlevel == CONTEXT_SYSTEM) {
169 // All tag descriptions are going to be public but we still need to respect forcelogin
170 if ($CFG->forcelogin) {
171 require_login();
174 $fullpath = "/$context->id/tag/description/".implode('/', $args);
176 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
177 send_file_not_found();
180 session_get_instance()->write_close(); // unlock session during fileserving
181 send_stored_file($file, 60*60, 0, true);
183 } else {
184 send_file_not_found();
187 // ========================================================================================================================
188 } else if ($component === 'calendar') {
189 if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_SYSTEM) {
191 // All events here are public the one requirement is that we respect forcelogin
192 if ($CFG->forcelogin) {
193 require_login();
196 // Get the event if from the args array
197 $eventid = array_shift($args);
199 // Load the event from the database
200 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'eventtype'=>'site'))) {
201 send_file_not_found();
203 // Check that we got an event and that it's userid is that of the user
205 // Get the file and serve if successful
206 $filename = array_pop($args);
207 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
208 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
209 send_file_not_found();
212 session_get_instance()->write_close(); // unlock session during fileserving
213 send_stored_file($file, 60*60, 0, $forcedownload);
215 } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_USER) {
217 // Must be logged in, if they are not then they obviously can't be this user
218 require_login();
220 // Don't want guests here, potentially saves a DB call
221 if (isguestuser()) {
222 send_file_not_found();
225 // Get the event if from the args array
226 $eventid = array_shift($args);
228 // Load the event from the database - user id must match
229 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'userid'=>$USER->id, 'eventtype'=>'user'))) {
230 send_file_not_found();
233 // Get the file and serve if successful
234 $filename = array_pop($args);
235 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
236 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
237 send_file_not_found();
240 session_get_instance()->write_close(); // unlock session during fileserving
241 send_stored_file($file, 60*60, 0, $forcedownload);
243 } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_COURSE) {
245 // Respect forcelogin and require login unless this is the site.... it probably
246 // should NEVER be the site
247 if ($CFG->forcelogin || $course->id != SITEID) {
248 require_login($course);
251 // Must be able to at least view the course
252 if (!is_enrolled($context) and !is_viewing($context)) {
253 //TODO: hmm, do we really want to block guests here?
254 send_file_not_found();
257 // Get the event id
258 $eventid = array_shift($args);
260 // Load the event from the database we need to check whether it is
261 // a) valid course event
262 // b) a group event
263 // Group events use the course context (there is no group context)
264 if (!$event = $DB->get_record('event', array('id'=>(int)$eventid, 'courseid'=>$course->id))) {
265 send_file_not_found();
268 // If its a group event require either membership of view all groups capability
269 if ($event->eventtype === 'group') {
270 if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($event->groupid, $USER->id)) {
271 send_file_not_found();
273 } else if ($event->eventtype === 'course') {
274 //ok
275 } else {
276 // some other type
277 send_file_not_found();
280 // If we get this far we can serve the file
281 $filename = array_pop($args);
282 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
283 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
284 send_file_not_found();
287 session_get_instance()->write_close(); // unlock session during fileserving
288 send_stored_file($file, 60*60, 0, $forcedownload);
290 } else {
291 send_file_not_found();
294 // ========================================================================================================================
295 } else if ($component === 'user') {
296 if ($filearea === 'icon' and $context->contextlevel == CONTEXT_USER) {
297 $redirect = false;
298 if (count($args) == 1) {
299 $themename = theme_config::DEFAULT_THEME;
300 $filename = array_shift($args);
301 } else {
302 $themename = array_shift($args);
303 $filename = array_shift($args);
305 if ((!empty($CFG->forcelogin) and !isloggedin())) {
306 // protect images if login required and not logged in;
307 // do not use require_login() because it is expensive and not suitable here anyway
308 $redirect = true;
310 if (!$redirect and ($filename !== 'f1' and $filename !== 'f2')) {
311 $filename = 'f1';
312 $redirect = true;
314 if (!$redirect && !$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
315 if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
316 $redirect = true;
319 if ($redirect) {
320 $theme = theme_config::load($themename);
321 redirect($theme->pix_url('u/'.$filename, 'moodle'));
323 send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page
325 } else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) {
326 require_login();
328 if (isguestuser()) {
329 send_file_not_found();
332 if ($USER->id !== $context->instanceid) {
333 send_file_not_found();
336 $filename = array_pop($args);
337 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
338 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
339 send_file_not_found();
342 session_get_instance()->write_close(); // unlock session during fileserving
343 send_stored_file($file, 0, 0, true); // must force download - security!
345 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_USER) {
347 if ($CFG->forcelogin) {
348 require_login();
351 $userid = $context->instanceid;
353 if ($USER->id == $userid) {
354 // always can access own
356 } else if (!empty($CFG->forceloginforprofiles)) {
357 require_login();
359 if (isguestuser()) {
360 send_file_not_found();
363 // we allow access to site profile of all course contacts (usually teachers)
364 if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) {
365 send_file_not_found();
368 $canview = false;
369 if (has_capability('moodle/user:viewdetails', $context)) {
370 $canview = true;
371 } else {
372 $courses = enrol_get_my_courses();
375 while (!$canview && count($courses) > 0) {
376 $course = array_shift($courses);
377 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) {
378 $canview = true;
383 $filename = array_pop($args);
384 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
385 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
386 send_file_not_found();
389 session_get_instance()->write_close(); // unlock session during fileserving
390 send_stored_file($file, 0, 0, true); // must force download - security!
392 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_COURSE) {
393 $userid = (int)array_shift($args);
394 $usercontext = get_context_instance(CONTEXT_USER, $userid);
396 if ($CFG->forcelogin) {
397 require_login();
400 if (!empty($CFG->forceloginforprofiles)) {
401 require_login();
402 if (isguestuser()) {
403 print_error('noguest');
406 //TODO: review this logic of user profile access prevention
407 if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) {
408 print_error('usernotavailable');
410 if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) {
411 print_error('cannotviewprofile');
413 if (!is_enrolled($context, $userid)) {
414 print_error('notenrolledprofile');
416 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
417 print_error('groupnotamember');
421 $filename = array_pop($args);
422 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
423 if (!$file = $fs->get_file($usercontext->id, 'user', 'profile', 0, $filepath, $filename) or $file->is_directory()) {
424 send_file_not_found();
427 session_get_instance()->write_close(); // unlock session during fileserving
428 send_stored_file($file, 0, 0, true); // must force download - security!
430 } else if ($filearea === 'backup' and $context->contextlevel == CONTEXT_USER) {
431 require_login();
433 if (isguestuser()) {
434 send_file_not_found();
436 $userid = $context->instanceid;
438 if ($USER->id != $userid) {
439 send_file_not_found();
442 $filename = array_pop($args);
443 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
444 if (!$file = $fs->get_file($context->id, 'user', 'backup', 0, $filepath, $filename) or $file->is_directory()) {
445 send_file_not_found();
448 session_get_instance()->write_close(); // unlock session during fileserving
449 send_stored_file($file, 0, 0, true); // must force download - security!
451 } else {
452 send_file_not_found();
455 // ========================================================================================================================
456 } else if ($component === 'coursecat') {
457 if ($context->contextlevel != CONTEXT_COURSECAT) {
458 send_file_not_found();
461 if ($filearea === 'description') {
462 if ($CFG->forcelogin) {
463 // no login necessary - unless login forced everywhere
464 require_login();
467 $filename = array_pop($args);
468 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
469 if (!$file = $fs->get_file($context->id, 'coursecat', 'description', 0, $filepath, $filename) or $file->is_directory()) {
470 send_file_not_found();
473 session_get_instance()->write_close(); // unlock session during fileserving
474 send_stored_file($file, 60*60, 0, $forcedownload);
475 } else {
476 send_file_not_found();
479 // ========================================================================================================================
480 } else if ($component === 'course') {
481 if ($context->contextlevel != CONTEXT_COURSE) {
482 send_file_not_found();
485 if ($filearea === 'summary') {
486 if ($CFG->forcelogin) {
487 require_login();
490 $filename = array_pop($args);
491 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
492 if (!$file = $fs->get_file($context->id, 'course', 'summary', 0, $filepath, $filename) or $file->is_directory()) {
493 send_file_not_found();
496 session_get_instance()->write_close(); // unlock session during fileserving
497 send_stored_file($file, 60*60, 0, $forcedownload);
499 } else if ($filearea === 'section') {
500 if ($CFG->forcelogin) {
501 require_login($course);
502 } else if ($course->id != SITEID) {
503 require_login($course);
506 $sectionid = (int)array_shift($args);
508 if (!$section = $DB->get_record('course_sections', array('id'=>$sectionid, 'course'=>$course->id))) {
509 send_file_not_found();
512 if ($course->numsections < $section->section) {
513 if (!has_capability('moodle/course:update', $context)) {
514 // block access to unavailable sections if can not edit course
515 send_file_not_found();
519 $filename = array_pop($args);
520 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
521 if (!$file = $fs->get_file($context->id, 'course', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
522 send_file_not_found();
525 session_get_instance()->write_close(); // unlock session during fileserving
526 send_stored_file($file, 60*60, 0, $forcedownload);
528 } else {
529 send_file_not_found();
532 } else if ($component === 'group') {
533 if ($context->contextlevel != CONTEXT_COURSE) {
534 send_file_not_found();
537 require_course_login($course, true, null, false);
539 $groupid = (int)array_shift($args);
541 $group = $DB->get_record('groups', array('id'=>$groupid, 'courseid'=>$course->id), '*', MUST_EXIST);
542 if (($course->groupmodeforce and $course->groupmode == SEPARATEGROUPS) and !has_capability('moodle/site:accessallgroups', $context) and !groups_is_member($group->id, $USER->id)) {
543 // do not allow access to separate group info if not member or teacher
544 send_file_not_found();
547 if ($filearea === 'description') {
549 require_login($course);
551 $filename = array_pop($args);
552 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
553 if (!$file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename) or $file->is_directory()) {
554 send_file_not_found();
557 session_get_instance()->write_close(); // unlock session during fileserving
558 send_stored_file($file, 60*60, 0, $forcedownload);
560 } else if ($filearea === 'icon') {
561 $filename = array_pop($args);
563 if ($filename !== 'f1' and $filename !== 'f2') {
564 send_file_not_found();
566 if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.png')) {
567 if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.jpg')) {
568 send_file_not_found();
572 session_get_instance()->write_close(); // unlock session during fileserving
573 send_stored_file($file, 60*60);
575 } else {
576 send_file_not_found();
579 } else if ($component === 'grouping') {
580 if ($context->contextlevel != CONTEXT_COURSE) {
581 send_file_not_found();
584 require_login($course);
586 $groupingid = (int)array_shift($args);
588 // note: everybody has access to grouping desc images for now
589 if ($filearea === 'description') {
591 $filename = array_pop($args);
592 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
593 if (!$file = $fs->get_file($context->id, 'grouping', 'description', $groupingid, $filepath, $filename) or $file->is_directory()) {
594 send_file_not_found();
597 session_get_instance()->write_close(); // unlock session during fileserving
598 send_stored_file($file, 60*60, 0, $forcedownload);
600 } else {
601 send_file_not_found();
604 // ========================================================================================================================
605 } else if ($component === 'backup') {
606 if ($filearea === 'course' and $context->contextlevel == CONTEXT_COURSE) {
607 require_login($course);
608 require_capability('moodle/backup:downloadfile', $context);
610 $filename = array_pop($args);
611 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
612 if (!$file = $fs->get_file($context->id, 'backup', 'course', 0, $filepath, $filename) or $file->is_directory()) {
613 send_file_not_found();
616 session_get_instance()->write_close(); // unlock session during fileserving
617 send_stored_file($file, 0, 0, $forcedownload);
619 } else if ($filearea === 'section' and $context->contextlevel == CONTEXT_COURSE) {
620 require_login($course);
621 require_capability('moodle/backup:downloadfile', $context);
623 $sectionid = (int)array_shift($args);
625 $filename = array_pop($args);
626 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
627 if (!$file = $fs->get_file($context->id, 'backup', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
628 send_file_not_found();
631 session_get_instance()->write_close();
632 send_stored_file($file, 60*60, 0, $forcedownload);
634 } else if ($filearea === 'activity' and $context->contextlevel == CONTEXT_MODULE) {
635 require_login($course, false, $cm);
636 require_capability('moodle/backup:downloadfile', $context);
638 $filename = array_pop($args);
639 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
640 if (!$file = $fs->get_file($context->id, 'backup', 'activity', 0, $filepath, $filename) or $file->is_directory()) {
641 send_file_not_found();
644 session_get_instance()->write_close();
645 send_stored_file($file, 60*60, 0, $forcedownload);
647 } else if ($filearea === 'automated' and $context->contextlevel == CONTEXT_COURSE) {
648 // Backup files that were generated by the automated backup systems.
650 require_login($course);
651 require_capability('moodle/site:config', $context);
653 $filename = array_pop($args);
654 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
655 if (!$file = $fs->get_file($context->id, 'backup', 'automated', 0, $filepath, $filename) or $file->is_directory()) {
656 send_file_not_found();
659 session_get_instance()->write_close(); // unlock session during fileserving
660 send_stored_file($file, 0, 0, $forcedownload);
662 } else {
663 send_file_not_found();
666 // ========================================================================================================================
667 } else if ($component === 'question') {
668 require_once($CFG->libdir . '/questionlib.php');
669 question_pluginfile($course, $context, 'question', $filearea, $args, $forcedownload);
670 send_file_not_found();
672 // ========================================================================================================================
673 } else if (strpos($component, 'mod_') === 0) {
674 $modname = substr($component, 4);
675 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
676 send_file_not_found();
678 require_once("$CFG->dirroot/mod/$modname/lib.php");
680 if ($context->contextlevel == CONTEXT_MODULE) {
681 if ($cm->modname !== $modname) {
682 // somebody tries to gain illegal access, cm type must match the component!
683 send_file_not_found();
687 if ($filearea === 'intro') {
688 if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) {
689 send_file_not_found();
691 require_course_login($course, true, $cm);
693 // all users may access it
694 $filename = array_pop($args);
695 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
696 if (!$file = $fs->get_file($context->id, 'mod_'.$modname, 'intro', 0, $filepath, $filename) or $file->is_directory()) {
697 send_file_not_found();
700 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
702 // finally send the file
703 send_stored_file($file, $lifetime, 0);
706 $filefunction = $component.'_pluginfile';
707 $filefunctionold = $modname.'_pluginfile';
708 if (function_exists($filefunction)) {
709 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
710 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
711 } else if (function_exists($filefunctionold)) {
712 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
713 $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload);
716 send_file_not_found();
718 // ========================================================================================================================
719 } else if (strpos($component, 'block_') === 0) {
720 $blockname = substr($component, 6);
721 // note: no more class methods in blocks please, that is ....
722 if (!file_exists("$CFG->dirroot/blocks/$blockname/lib.php")) {
723 send_file_not_found();
725 require_once("$CFG->dirroot/blocks/$blockname/lib.php");
727 if ($context->contextlevel == CONTEXT_BLOCK) {
728 $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST);
729 if ($birecord->blockname !== $blockname) {
730 // somebody tries to gain illegal access, cm type must match the component!
731 send_file_not_found();
733 } else {
734 $birecord = null;
737 $filefunction = $component.'_pluginfile';
738 if (function_exists($filefunction)) {
739 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
740 $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload);
743 send_file_not_found();
745 } else if (strpos($component, '_') === false) {
746 // all core subsystems have to be specified above, no more guessing here!
747 send_file_not_found();
749 } else {
750 // try to serve general plugin file in arbitrary context
751 $dir = get_component_directory($component);
752 if (!file_exists("$dir/lib.php")) {
753 send_file_not_found();
755 include_once("$dir/lib.php");
757 $filefunction = $component.'_pluginfile';
758 if (function_exists($filefunction)) {
759 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
760 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
763 send_file_not_found();