Merge branch 'w04_MDL-26045_20_onlineblock' of git://github.com/skodak/moodle
[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 *
64f93798 21 * @package core
d92e7c4d 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 27// disable moodle specific debug messages and any errors in output
64f93798
PS
28//define('NO_DEBUG_DISPLAY', true);
29//TODO: uncomment this once the file api stabilises a bit more
2e9b772f 30
d92e7c4d 31require_once('config.php');
32require_once('lib/filelib.php');
33
d92e7c4d 34$relativepath = get_file_argument();
35$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
36
37// relative path must start with '/'
38if (!$relativepath) {
39 print_error('invalidargorconf');
29afd52b 40} else if ($relativepath[0] != '/') {
d92e7c4d 41 print_error('pathdoesnotstartslash');
42}
43
44// extract relative path components
45$args = explode('/', ltrim($relativepath, '/'));
46
64f93798 47if (count($args) < 3) { // always at least context, component and filearea
d92e7c4d 48 print_error('invalidarguments');
49}
50
51$contextid = (int)array_shift($args);
64f93798
PS
52$component = clean_param(array_shift($args), PARAM_SAFEDIR);
53$filearea = clean_param(array_shift($args), PARAM_SAFEDIR);
54
55list($context, $course, $cm) = get_context_info_array($contextid);
d92e7c4d 56
d92e7c4d 57$fs = get_file_storage();
58
50f95991 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]);
61if (!empty($CFG->excludeoldflashclients) && $mimetype == 'application/x-shockwave-flash'&& !empty($SESSION->flashversion)) {
62 $userplayerversion = explode('.', $SESSION->flashversion);
63 $requiredplayerversion = explode('.', $CFG->excludeoldflashclients);
64f93798 64 if (($userplayerversion[0] < $requiredplayerversion[0]) ||
50f95991 65 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] < $requiredplayerversion[1]) ||
66 ($userplayerversion[0] == $requiredplayerversion[0] && $userplayerversion[1] == $requiredplayerversion[1]
64f93798 67 && $userplayerversion[2] < $requiredplayerversion[2])) {
50f95991 68 $path = $CFG->dirroot."/lib/flashdetect/flashupgrade.swf"; // Alternate content asking user to upgrade Flash
69 $filename = "flashupgrade.swf";
64f93798
PS
70 send_file($path, $filename, O, 0, false, false, 'application/x-shockwave-flash'); // Do not cache
71 }
72}
d92e7c4d 73
64f93798
PS
74// ========================================================================================================================
75if ($component === 'blog') {
76 // Blog file serving
77 if ($context->contextlevel != CONTEXT_SYSTEM) {
78 send_file_not_found();
79 }
80 if ($filearea !== 'attachment' and $filearea !== 'post') {
81 send_file_not_found();
82 }
d92e7c4d 83
64f93798
PS
84 if (empty($CFG->bloglevel)) {
85 print_error('siteblogdisable', 'blog');
86 }
172dd12c 87
64f93798
PS
88 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
89 require_login();
90 if (isguestuser()) {
91 print_error('noguest');
92 }
93 if ($CFG->bloglevel == BLOG_USER_LEVEL) {
d92e7c4d 94 if ($USER->id != $entry->userid) {
95 send_file_not_found();
96 }
97 }
64f93798
PS
98 }
99 $entryid = (int)array_shift($args);
100 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
101 send_file_not_found();
102 }
172dd12c 103
64f93798
PS
104 if ('publishstate' === 'public') {
105 if ($CFG->forcelogin) {
106 require_login();
107 }
172dd12c 108
64f93798
PS
109 } else if ('publishstate' === 'site') {
110 require_login();
111 //ok
112 } else if ('publishstate' === 'draft') {
113 require_login();
114 if ($USER->id != $entry->userid) {
d92e7c4d 115 send_file_not_found();
116 }
64f93798
PS
117 }
118
119 $filename = array_pop($args);
d85ab705 120 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
121
122 if (!$file = $fs->get_file($context->id, $component, $filearea, $entryid, $filepath, $filename) or $file->is_directory()) {
123 send_file_not_found();
124 }
172dd12c 125
64f93798
PS
126 send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!
127
128// ========================================================================================================================
129} else if ($component === 'grade') {
130 if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) {
131 // Global gradebook files
8bdc9cac
SH
132 if ($CFG->forcelogin) {
133 require_login();
134 }
135
64f93798 136 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
8bdc9cac
SH
137
138 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
139 send_file_not_found();
140 }
141
142 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 143 send_stored_file($file, 60*60, 0, $forcedownload);
8bdc9cac 144
64f93798
PS
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();
8bdc9cac 148
7b2e259c 149 if ($CFG->forcelogin || $course->id != SITEID) {
64f93798 150 require_login($course);
8bdc9cac
SH
151 }
152
64f93798 153 $fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
8bdc9cac
SH
154
155 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
156 send_file_not_found();
157 }
158
159 session_get_instance()->write_close(); // unlock session during fileserving
64f93798
PS
160 send_stored_file($file, 60*60, 0, $forcedownload);
161 } else {
162 send_file_not_found();
163 }
8bdc9cac 164
64f93798
PS
165// ========================================================================================================================
166} else if ($component === 'tag') {
167 if ($filearea === 'description' and $context->contextlevel == CONTEXT_SYSTEM) {
76d9df3f 168
64f93798 169 // All tag descriptions are going to be public but we still need to respect forcelogin
76d9df3f
SH
170 if ($CFG->forcelogin) {
171 require_login();
172 }
173
6cf93b6b 174 $fullpath = "/$context->id/tag/description/".implode('/', $args);
76d9df3f
SH
175
176 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
177 send_file_not_found();
178 }
179
180 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 181 send_stored_file($file, 60*60, 0, true);
172dd12c 182
d92e7c4d 183 } else {
184 send_file_not_found();
172dd12c 185 }
186
64f93798
PS
187// ========================================================================================================================
188} else if ($component === 'calendar') {
189 if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_SYSTEM) {
76d9df3f 190
64f93798
PS
191 // All events here are public the one requirement is that we respect forcelogin
192 if ($CFG->forcelogin) {
193 require_login();
194 }
195
196 // Get the event if from the args array
197 $eventid = array_shift($args);
198
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();
202 }
203 // Check that we got an event and that it's userid is that of the user
204
205 // Get the file and serve if successful
206 $filename = array_pop($args);
d85ab705 207 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
208 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
209 send_file_not_found();
210 }
211
212 session_get_instance()->write_close(); // unlock session during fileserving
213 send_stored_file($file, 60*60, 0, $forcedownload);
214
215 } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_USER) {
76d9df3f
SH
216
217 // Must be logged in, if they are not then they obviously can't be this user
218 require_login();
03221650 219
76d9df3f
SH
220 // Don't want guests here, potentially saves a DB call
221 if (isguestuser()) {
222 send_file_not_found();
223 }
224
225 // Get the event if from the args array
226 $eventid = array_shift($args);
76d9df3f 227
64f93798
PS
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'))) {
76d9df3f
SH
230 send_file_not_found();
231 }
232
64f93798
PS
233 // Get the file and serve if successful
234 $filename = array_pop($args);
d85ab705 235 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 236 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
76d9df3f
SH
237 send_file_not_found();
238 }
239
240 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 241 send_stored_file($file, 60*60, 0, $forcedownload);
8bdc9cac 242
64f93798
PS
243 } else if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_COURSE) {
244
245 // Respect forcelogin and require login unless this is the site.... it probably
246 // should NEVER be the site
7b2e259c 247 if ($CFG->forcelogin || $course->id != SITEID) {
64f93798 248 require_login($course);
8bdc9cac
SH
249 }
250
64f93798
PS
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?
8bdc9cac
SH
254 send_file_not_found();
255 }
256
64f93798
PS
257 // Get the event id
258 $eventid = array_shift($args);
8bdc9cac 259
64f93798
PS
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();
266 }
8bdc9cac 267
64f93798
PS
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();
8bdc9cac 272 }
64f93798
PS
273 } else if ($event->eventtype === 'course') {
274 //ok
275 } else {
276 // some other type
277 send_file_not_found();
8bdc9cac
SH
278 }
279
64f93798
PS
280 // If we get this far we can serve the file
281 $filename = array_pop($args);
d85ab705 282 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 283 if (!$file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename) or $file->is_directory()) {
8bdc9cac
SH
284 send_file_not_found();
285 }
286
287 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 288 send_stored_file($file, 60*60, 0, $forcedownload);
76d9df3f 289
64f93798
PS
290 } else {
291 send_file_not_found();
292 }
172dd12c 293
64f93798
PS
294// ========================================================================================================================
295} else if ($component === 'user') {
edfd6a5e
PS
296 if ($filearea === 'icon' and $context->contextlevel == CONTEXT_USER) {
297 if (!empty($CFG->forcelogin) and !isloggedin()) {
298 // protect images if login required and not logged in;
299 // do not use require_login() because it is expensive and not suitable here anyway
300 redirect($OUTPUT->pix_url('u/f1'));
301 }
302 $filename = array_pop($args);
303 if ($filename !== 'f1' and $filename !== 'f2') {
304 redirect($OUTPUT->pix_url('u/f1'));
305 }
2cc895aa
PS
306 if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
307 if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
308 redirect($OUTPUT->pix_url('u/f1'));
309 }
edfd6a5e 310 }
2cc895aa 311
edfd6a5e
PS
312 send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page
313
314 } else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) {
64f93798 315 require_login();
172dd12c 316
64f93798
PS
317 if (isguestuser()) {
318 send_file_not_found();
214a81b8 319 }
172dd12c 320
64f93798
PS
321 if ($USER->id !== $context->instanceid) {
322 send_file_not_found();
323 }
172dd12c 324
64f93798 325 $filename = array_pop($args);
d85ab705 326 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 327 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
214a81b8
DC
328 send_file_not_found();
329 }
172dd12c 330
214a81b8 331 session_get_instance()->write_close(); // unlock session during fileserving
64f93798
PS
332 send_stored_file($file, 0, 0, true); // must force download - security!
333
334 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_USER) {
335
214a81b8 336 if ($CFG->forcelogin) {
214a81b8
DC
337 require_login();
338 }
214a81b8 339
64f93798 340 $userid = $context->instanceid;
214a81b8 341
64f93798
PS
342 if ($USER->id == $userid) {
343 // always can access own
214a81b8 344
64f93798
PS
345 } else if (!empty($CFG->forceloginforprofiles)) {
346 require_login();
172dd12c 347
64f93798
PS
348 if (isguestuser()) {
349 send_file_not_found();
350 }
172dd12c 351
64f93798
PS
352 // we allow access to site profile of all course contacts (usually teachers)
353 if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) {
354 send_file_not_found();
355 }
172dd12c 356
64f93798
PS
357 $canview = false;
358 if (has_capability('moodle/user:viewdetails', $context)) {
359 $canview = true;
360 } else {
361 $courses = enrol_get_my_courses();
362 }
172dd12c 363
64f93798
PS
364 while (!$canview && count($courses) > 0) {
365 $course = array_shift($courses);
366 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) {
367 $canview = true;
368 }
369 }
370 }
172dd12c 371
64f93798 372 $filename = array_pop($args);
d85ab705 373 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 374 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
9e5fa330 375 send_file_not_found();
172dd12c 376 }
377
d92e7c4d 378 session_get_instance()->write_close(); // unlock session during fileserving
64f93798
PS
379 send_stored_file($file, 0, 0, true); // must force download - security!
380
381 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_COURSE) {
382 $userid = (int)array_shift($args);
383 $usercontext = get_context_instance(CONTEXT_USER, $userid);
d92e7c4d 384
8bdc9cac
SH
385 if ($CFG->forcelogin) {
386 require_login();
387 }
388
64f93798
PS
389 if (!empty($CFG->forceloginforprofiles)) {
390 require_login();
391 if (isguestuser()) {
392 print_error('noguest');
393 }
03221650 394
64f93798
PS
395 //TODO: review this logic of user profile access prevention
396 if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) {
397 print_error('usernotavailable');
398 }
399 if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) {
400 print_error('cannotviewprofile');
401 }
402 if (!is_enrolled($context, $userid)) {
403 print_error('notenrolledprofile');
404 }
405 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
406 print_error('groupnotamember');
407 }
408 }
409
410 $filename = array_pop($args);
d85ab705 411 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
f9817fc9 412 if (!$file = $fs->get_file($usercontext->id, 'user', 'profile', 0, $filepath, $filename) or $file->is_directory()) {
8bdc9cac
SH
413 send_file_not_found();
414 }
415
416 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 417 send_stored_file($file, 0, 0, true); // must force download - security!
8bdc9cac 418
64f93798
PS
419 } else if ($filearea === 'backup' and $context->contextlevel == CONTEXT_USER) {
420 require_login();
8bdc9cac 421
64f93798
PS
422 if (isguestuser()) {
423 send_file_not_found();
424 }
7054b01d
DC
425 $userid = $context->instanceid;
426
64f93798
PS
427 if ($USER->id != $userid) {
428 send_file_not_found();
8bdc9cac
SH
429 }
430
64f93798 431 $filename = array_pop($args);
d85ab705 432 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 433 if (!$file = $fs->get_file($context->id, 'user', 'backup', 0, $filepath, $filename) or $file->is_directory()) {
8bdc9cac
SH
434 send_file_not_found();
435 }
436
437 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 438 send_stored_file($file, 0, 0, true); // must force download - security!
8bdc9cac 439
64f93798
PS
440 } else {
441 send_file_not_found();
442 }
76d9df3f 443
64f93798
PS
444// ========================================================================================================================
445} else if ($component === 'coursecat') {
446 if ($context->contextlevel != CONTEXT_COURSECAT) {
447 send_file_not_found();
448 }
76d9df3f 449
64f93798
PS
450 if ($filearea === 'description') {
451 if ($CFG->forcelogin) {
452 // no login necessary - unless login forced everywhere
453 require_login();
76d9df3f
SH
454 }
455
64f93798 456 $filename = array_pop($args);
d85ab705 457 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 458 if (!$file = $fs->get_file($context->id, 'coursecat', 'description', 0, $filepath, $filename) or $file->is_directory()) {
76d9df3f
SH
459 send_file_not_found();
460 }
461
64f93798
PS
462 session_get_instance()->write_close(); // unlock session during fileserving
463 send_stored_file($file, 60*60, 0, $forcedownload);
464 } else {
465 send_file_not_found();
466 }
76d9df3f 467
64f93798
PS
468// ========================================================================================================================
469} else if ($component === 'course') {
470 if ($context->contextlevel != CONTEXT_COURSE) {
471 send_file_not_found();
472 }
76d9df3f 473
64f93798
PS
474 if ($filearea === 'summary') {
475 if ($CFG->forcelogin) {
476 require_login();
76d9df3f
SH
477 }
478
64f93798 479 $filename = array_pop($args);
d85ab705 480 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 481 if (!$file = $fs->get_file($context->id, 'course', 'summary', 0, $filepath, $filename) or $file->is_directory()) {
76d9df3f
SH
482 send_file_not_found();
483 }
484
485 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 486 send_stored_file($file, 60*60, 0, $forcedownload);
76d9df3f 487
64f93798 488 } else if ($filearea === 'section') {
d92e7c4d 489 if ($CFG->forcelogin) {
172dd12c 490 require_login($course);
7b2e259c 491 } else if ($course->id != SITEID) {
d92e7c4d 492 require_login($course);
493 }
106f3b67 494
d92e7c4d 495 $sectionid = (int)array_shift($args);
106f3b67 496
172ab484
PS
497 if (!$section = $DB->get_record('course_sections', array('id'=>$sectionid, 'course'=>$course->id))) {
498 send_file_not_found();
499 }
500
501 if ($course->numsections < $section->section) {
d92e7c4d 502 if (!has_capability('moodle/course:update', $context)) {
2a62743c 503 // block access to unavailable sections if can not edit course
106f3b67 504 send_file_not_found();
505 }
d92e7c4d 506 }
106f3b67 507
64f93798 508 $filename = array_pop($args);
d85ab705 509 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 510 if (!$file = $fs->get_file($context->id, 'course', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
d92e7c4d 511 send_file_not_found();
512 }
172dd12c 513
d92e7c4d 514 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 515 send_stored_file($file, 60*60, 0, $forcedownload);
172dd12c 516
64f93798
PS
517 } else {
518 send_file_not_found();
519 }
91b890ab 520
64f93798
PS
521} else if ($component === 'group') {
522 if ($context->contextlevel != CONTEXT_COURSE) {
523 send_file_not_found();
524 }
91b890ab 525
a02357df 526 require_course_login($course, true, null, false);
91b890ab 527
64f93798
PS
528 $groupid = (int)array_shift($args);
529
530 $group = $DB->get_record('groups', array('id'=>$groupid, 'courseid'=>$course->id), '*', MUST_EXIST);
01ebadfb
PS
531 if (($course->groupmodeforce and $course->groupmode == SEPARATEGROUPS) and !has_capability('moodle/site:accessallgroups', $context) and !groups_is_member($group->id, $USER->id)) {
532 // do not allow access to separate group info if not member or teacher
64f93798
PS
533 send_file_not_found();
534 }
535
e88dd876 536 if ($filearea === 'description') {
cd6cf6b6
SH
537
538 require_login($course);
539
64f93798 540 $filename = array_pop($args);
d85ab705 541 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 542 if (!$file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename) or $file->is_directory()) {
91b890ab
DC
543 send_file_not_found();
544 }
545
64f93798
PS
546 session_get_instance()->write_close(); // unlock session during fileserving
547 send_stored_file($file, 60*60, 0, $forcedownload);
214a81b8 548
e88dd876
PS
549 } else if ($filearea === 'icon') {
550 $filename = array_pop($args);
551
552 if ($filename !== 'f1' and $filename !== 'f2') {
553 send_file_not_found();
554 }
2cc895aa
PS
555 if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.png')) {
556 if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.jpg')) {
557 send_file_not_found();
558 }
e88dd876
PS
559 }
560
561 session_get_instance()->write_close(); // unlock session during fileserving
562 send_stored_file($file, 60*60);
563
64f93798
PS
564 } else {
565 send_file_not_found();
566 }
214a81b8 567
64f93798
PS
568} else if ($component === 'grouping') {
569 if ($context->contextlevel != CONTEXT_COURSE) {
570 send_file_not_found();
571 }
3156b8ca 572
64f93798 573 require_login($course);
3156b8ca 574
64f93798 575 $groupingid = (int)array_shift($args);
106f3b67 576
64f93798
PS
577 // note: everybody has access to grouping desc images for now
578 if ($filearea === 'description') {
106f3b67 579
64f93798 580 $filename = array_pop($args);
d85ab705 581 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
d9eb6265 582 if (!$file = $fs->get_file($context->id, 'grouping', 'description', $groupingid, $filepath, $filename) or $file->is_directory()) {
106f3b67 583 send_file_not_found();
584 }
172dd12c 585
d92e7c4d 586 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 587 send_stored_file($file, 60*60, 0, $forcedownload);
106f3b67 588
d92e7c4d 589 } else {
590 send_file_not_found();
591 }
592
64f93798
PS
593// ========================================================================================================================
594} else if ($component === 'backup') {
595 if ($filearea === 'course' and $context->contextlevel == CONTEXT_COURSE) {
596 require_login($course);
597 require_capability('moodle/backup:downloadfile', $context);
d92e7c4d 598
64f93798 599 $filename = array_pop($args);
d85ab705 600 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
601 if (!$file = $fs->get_file($context->id, 'backup', 'course', 0, $filepath, $filename) or $file->is_directory()) {
602 send_file_not_found();
603 }
d92e7c4d 604
64f93798
PS
605 session_get_instance()->write_close(); // unlock session during fileserving
606 send_stored_file($file, 0, 0, $forcedownload);
607
608 } else if ($filearea === 'section' and $context->contextlevel == CONTEXT_COURSE) {
609 require_login($course);
610 require_capability('moodle/backup:downloadfile', $context);
611
612 $sectionid = (int)array_shift($args);
613
614 $filename = array_pop($args);
d85ab705 615 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
616 if (!$file = $fs->get_file($context->id, 'backup', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
617 send_file_not_found();
618 }
619
620 session_get_instance()->write_close();
621 send_stored_file($file, 60*60, 0, $forcedownload);
622
623 } else if ($filearea === 'activity' and $context->contextlevel == CONTEXT_MODULE) {
624 require_login($course, false, $cm);
625 require_capability('moodle/backup:downloadfile', $context);
626
627 $filename = array_pop($args);
d85ab705 628 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
629 if (!$file = $fs->get_file($context->id, 'backup', 'activity', 0, $filepath, $filename) or $file->is_directory()) {
630 send_file_not_found();
631 }
632
633 session_get_instance()->write_close();
634 send_stored_file($file, 60*60, 0, $forcedownload);
635
bac233d3
SH
636 } else if ($filearea === 'automated' and $context->contextlevel == CONTEXT_COURSE) {
637 // Backup files that were generated by the automated backup systems.
638
639 require_login($course);
640 require_capability('moodle/site:config', $context);
641
642 $filename = array_pop($args);
643 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
644 if (!$file = $fs->get_file($context->id, 'backup', 'automated', 0, $filepath, $filename) or $file->is_directory()) {
645 send_file_not_found();
646 }
647
648 session_get_instance()->write_close(); // unlock session during fileserving
649 send_stored_file($file, 0, 0, $forcedownload);
650
64f93798 651 } else {
d92e7c4d 652 send_file_not_found();
653 }
654
fe6ce234
DC
655// ========================================================================================================================
656} else if ($component === 'question') {
657 require_once($CFG->libdir . '/questionlib.php');
658 question_pluginfile($course, $context, 'question', $filearea, $args, $forcedownload);
659 send_file_not_found();
660
64f93798
PS
661// ========================================================================================================================
662} else if (strpos($component, 'mod_') === 0) {
663 $modname = substr($component, 4);
664 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
d92e7c4d 665 send_file_not_found();
666 }
64f93798 667 require_once("$CFG->dirroot/mod/$modname/lib.php");
172dd12c 668
64f93798
PS
669 if ($context->contextlevel == CONTEXT_MODULE) {
670 if ($cm->modname !== $modname) {
671 // somebody tries to gain illegal access, cm type must match the component!
9e5fa330 672 send_file_not_found();
172dd12c 673 }
64f93798
PS
674 }
675
676 if ($filearea === 'intro') {
677 if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) {
57956cc3
PS
678 send_file_not_found();
679 }
680 require_course_login($course, true, $cm);
681
d92e7c4d 682 // all users may access it
64f93798 683 $filename = array_pop($args);
d85ab705 684 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 685 if (!$file = $fs->get_file($context->id, 'mod_'.$modname, 'intro', 0, $filepath, $filename) or $file->is_directory()) {
dc5c2bd9 686 send_file_not_found();
687 }
688
d92e7c4d 689 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
dc5c2bd9 690
d92e7c4d 691 // finally send the file
692 send_stored_file($file, $lifetime, 0);
693 }
694
64f93798
PS
695 $filefunction = $component.'_pluginfile';
696 $filefunctionold = $modname.'_pluginfile';
d92e7c4d 697 if (function_exists($filefunction)) {
520199ea 698 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
64f93798
PS
699 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
700 } else if (function_exists($filefunctionold)) {
701 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
702 $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload);
b287ea7a 703 }
520199ea 704
705 send_file_not_found();
172dd12c 706
64f93798
PS
707// ========================================================================================================================
708} else if (strpos($component, 'block_') === 0) {
709 $blockname = substr($component, 6);
710 // note: no more class methods in blocks please, that is ....
711 if (!file_exists("$CFG->dirroot/blocks/$blockname/lib.php")) {
f1cbbbeb
SH
712 send_file_not_found();
713 }
64f93798 714 require_once("$CFG->dirroot/blocks/$blockname/lib.php");
f1cbbbeb 715
64f93798
PS
716 if ($context->contextlevel == CONTEXT_BLOCK) {
717 $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST);
718 if ($birecord->blockname !== $blockname) {
719 // somebody tries to gain illegal access, cm type must match the component!
720 send_file_not_found();
721 }
722 } else {
723 $birecord = null;
f1cbbbeb
SH
724 }
725
64f93798
PS
726 $filefunction = $component.'_pluginfile';
727 if (function_exists($filefunction)) {
728 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
729 $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload);
f1cbbbeb
SH
730 }
731
732 send_file_not_found();
172dd12c 733
64f93798
PS
734} else if (strpos($component, '_') === false) {
735 // all core subsystems have to be specified above, no more guessing here!
736 send_file_not_found();
737
d92e7c4d 738} else {
64f93798
PS
739 // try to serve general plugin file in arbitrary context
740 $dir = get_component_directory($component);
741 if (!file_exists("$dir/lib.php")) {
742 send_file_not_found();
743 }
77dd3034 744 include_once("$dir/lib.php");
64f93798
PS
745
746 $filefunction = $component.'_pluginfile';
747 if (function_exists($filefunction)) {
748 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
749 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
750 }
751
d92e7c4d 752 send_file_not_found();
753}