MDL-14589 remove old user icons after upgrade
[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
64f93798
PS
149 if ($CFG->forcelogin || $course->id !== SITEID) {
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
64f93798 174 $fullpath = "/$context->id/tage/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
247 if ($CFG->forcelogin || $course->id !== SITEID) {
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 }
306 if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
307 redirect($OUTPUT->pix_url('u/f1'));
308 }
309 send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page
310
311 } else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) {
64f93798 312 require_login();
172dd12c 313
64f93798
PS
314 if (isguestuser()) {
315 send_file_not_found();
214a81b8 316 }
172dd12c 317
64f93798
PS
318 if ($USER->id !== $context->instanceid) {
319 send_file_not_found();
320 }
172dd12c 321
64f93798 322 $filename = array_pop($args);
d85ab705 323 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 324 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
214a81b8
DC
325 send_file_not_found();
326 }
172dd12c 327
214a81b8 328 session_get_instance()->write_close(); // unlock session during fileserving
64f93798
PS
329 send_stored_file($file, 0, 0, true); // must force download - security!
330
331 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_USER) {
332
214a81b8 333 if ($CFG->forcelogin) {
214a81b8
DC
334 require_login();
335 }
214a81b8 336
64f93798 337 $userid = $context->instanceid;
214a81b8 338
64f93798
PS
339 if ($USER->id == $userid) {
340 // always can access own
214a81b8 341
64f93798
PS
342 } else if (!empty($CFG->forceloginforprofiles)) {
343 require_login();
172dd12c 344
64f93798
PS
345 if (isguestuser()) {
346 send_file_not_found();
347 }
172dd12c 348
64f93798
PS
349 // we allow access to site profile of all course contacts (usually teachers)
350 if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) {
351 send_file_not_found();
352 }
172dd12c 353
64f93798
PS
354 $canview = false;
355 if (has_capability('moodle/user:viewdetails', $context)) {
356 $canview = true;
357 } else {
358 $courses = enrol_get_my_courses();
359 }
172dd12c 360
64f93798
PS
361 while (!$canview && count($courses) > 0) {
362 $course = array_shift($courses);
363 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_COURSE, $course->id))) {
364 $canview = true;
365 }
366 }
367 }
172dd12c 368
64f93798 369 $filename = array_pop($args);
d85ab705 370 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 371 if (!$file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename) or $file->is_directory()) {
9e5fa330 372 send_file_not_found();
172dd12c 373 }
374
d92e7c4d 375 session_get_instance()->write_close(); // unlock session during fileserving
64f93798
PS
376 send_stored_file($file, 0, 0, true); // must force download - security!
377
378 } else if ($filearea === 'profile' and $context->contextlevel == CONTEXT_COURSE) {
379 $userid = (int)array_shift($args);
380 $usercontext = get_context_instance(CONTEXT_USER, $userid);
d92e7c4d 381
8bdc9cac
SH
382 if ($CFG->forcelogin) {
383 require_login();
384 }
385
64f93798
PS
386 if (!empty($CFG->forceloginforprofiles)) {
387 require_login();
388 if (isguestuser()) {
389 print_error('noguest');
390 }
03221650 391
64f93798
PS
392 //TODO: review this logic of user profile access prevention
393 if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) {
394 print_error('usernotavailable');
395 }
396 if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) {
397 print_error('cannotviewprofile');
398 }
399 if (!is_enrolled($context, $userid)) {
400 print_error('notenrolledprofile');
401 }
402 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
403 print_error('groupnotamember');
404 }
405 }
406
407 $filename = array_pop($args);
d85ab705 408 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 409 if (!$file = $fs->get_file($context->id, 'user', 'profile', 0, $filepath, $filename) or $file->is_directory()) {
8bdc9cac
SH
410 send_file_not_found();
411 }
412
413 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 414 send_stored_file($file, 0, 0, true); // must force download - security!
8bdc9cac 415
64f93798
PS
416 } else if ($filearea === 'backup' and $context->contextlevel == CONTEXT_USER) {
417 require_login();
8bdc9cac 418
64f93798
PS
419 if (isguestuser()) {
420 send_file_not_found();
421 }
422 if ($USER->id != $userid) {
423 send_file_not_found();
8bdc9cac
SH
424 }
425
64f93798 426 $filename = array_pop($args);
d85ab705 427 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 428 if (!$file = $fs->get_file($context->id, 'user', 'backup', 0, $filepath, $filename) or $file->is_directory()) {
8bdc9cac
SH
429 send_file_not_found();
430 }
431
432 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 433 send_stored_file($file, 0, 0, true); // must force download - security!
8bdc9cac 434
64f93798
PS
435 } else {
436 send_file_not_found();
437 }
76d9df3f 438
64f93798
PS
439// ========================================================================================================================
440} else if ($component === 'coursecat') {
441 if ($context->contextlevel != CONTEXT_COURSECAT) {
442 send_file_not_found();
443 }
76d9df3f 444
64f93798
PS
445 if ($filearea === 'description') {
446 if ($CFG->forcelogin) {
447 // no login necessary - unless login forced everywhere
448 require_login();
76d9df3f
SH
449 }
450
64f93798 451 $filename = array_pop($args);
d85ab705 452 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 453 if (!$file = $fs->get_file($context->id, 'coursecat', 'description', 0, $filepath, $filename) or $file->is_directory()) {
76d9df3f
SH
454 send_file_not_found();
455 }
456
64f93798
PS
457 session_get_instance()->write_close(); // unlock session during fileserving
458 send_stored_file($file, 60*60, 0, $forcedownload);
459 } else {
460 send_file_not_found();
461 }
76d9df3f 462
64f93798
PS
463// ========================================================================================================================
464} else if ($component === 'course') {
465 if ($context->contextlevel != CONTEXT_COURSE) {
466 send_file_not_found();
467 }
76d9df3f 468
64f93798
PS
469 if ($filearea === 'summary') {
470 if ($CFG->forcelogin) {
471 require_login();
76d9df3f
SH
472 }
473
64f93798 474 $filename = array_pop($args);
d85ab705 475 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 476 if (!$file = $fs->get_file($context->id, 'course', 'summary', 0, $filepath, $filename) or $file->is_directory()) {
76d9df3f
SH
477 send_file_not_found();
478 }
479
480 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 481 send_stored_file($file, 60*60, 0, $forcedownload);
76d9df3f 482
64f93798 483 } else if ($filearea === 'section') {
d92e7c4d 484 if ($CFG->forcelogin) {
172dd12c 485 require_login($course);
d92e7c4d 486 } else if ($course->id !== SITEID) {
487 require_login($course);
488 }
106f3b67 489
d92e7c4d 490 $sectionid = (int)array_shift($args);
106f3b67 491
d92e7c4d 492 if ($course->numsections < $sectionid) {
493 if (!has_capability('moodle/course:update', $context)) {
494 // disable access to invisible sections if can not edit course
495 // this is going to break some ugly hacks, but is necessary
106f3b67 496 send_file_not_found();
497 }
d92e7c4d 498 }
106f3b67 499
64f93798 500 $filename = array_pop($args);
d85ab705 501 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 502 if (!$file = $fs->get_file($context->id, 'course', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
d92e7c4d 503 send_file_not_found();
504 }
172dd12c 505
d92e7c4d 506 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 507 send_stored_file($file, 60*60, 0, $forcedownload);
172dd12c 508
64f93798
PS
509 } else {
510 send_file_not_found();
511 }
91b890ab 512
64f93798
PS
513} else if ($component === 'group') {
514 if ($context->contextlevel != CONTEXT_COURSE) {
515 send_file_not_found();
516 }
91b890ab 517
64f93798 518 require_login($course);
91b890ab 519
64f93798
PS
520 $groupid = (int)array_shift($args);
521
522 $group = $DB->get_record('groups', array('id'=>$groupid, 'courseid'=>$course->id), '*', MUST_EXIST);
523 if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($group->id, $USER->id)) {
524 send_file_not_found();
525 }
526
527 if ($filearea === 'description' or $filearea === 'icon') {
528
529 //TODO: implement group image storage in file pool
530
531 $filename = array_pop($args);
d85ab705 532 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 533 if (!$file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename) or $file->is_directory()) {
91b890ab
DC
534 send_file_not_found();
535 }
536
64f93798
PS
537 session_get_instance()->write_close(); // unlock session during fileserving
538 send_stored_file($file, 60*60, 0, $forcedownload);
214a81b8 539
64f93798
PS
540 } else {
541 send_file_not_found();
542 }
214a81b8 543
64f93798
PS
544} else if ($component === 'grouping') {
545 if ($context->contextlevel != CONTEXT_COURSE) {
546 send_file_not_found();
547 }
3156b8ca 548
64f93798 549 require_login($course);
3156b8ca 550
64f93798 551 $groupingid = (int)array_shift($args);
106f3b67 552
64f93798
PS
553 // note: everybody has access to grouping desc images for now
554 if ($filearea === 'description') {
106f3b67 555
64f93798 556 $filename = array_pop($args);
d85ab705 557 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 558 if (!$file = $fs->get_file($context->id, 'grouping', 'description', $groupingid->id, $filepath, $filename) or $file->is_directory()) {
106f3b67 559 send_file_not_found();
560 }
172dd12c 561
d92e7c4d 562 session_get_instance()->write_close(); // unlock session during fileserving
64f93798 563 send_stored_file($file, 60*60, 0, $forcedownload);
106f3b67 564
d92e7c4d 565 } else {
566 send_file_not_found();
567 }
568
64f93798
PS
569// ========================================================================================================================
570} else if ($component === 'backup') {
571 if ($filearea === 'course' and $context->contextlevel == CONTEXT_COURSE) {
572 require_login($course);
573 require_capability('moodle/backup:downloadfile', $context);
d92e7c4d 574
64f93798 575 $filename = array_pop($args);
d85ab705 576 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
577 if (!$file = $fs->get_file($context->id, 'backup', 'course', 0, $filepath, $filename) or $file->is_directory()) {
578 send_file_not_found();
579 }
d92e7c4d 580
64f93798
PS
581 session_get_instance()->write_close(); // unlock session during fileserving
582 send_stored_file($file, 0, 0, $forcedownload);
583
584 } else if ($filearea === 'section' and $context->contextlevel == CONTEXT_COURSE) {
585 require_login($course);
586 require_capability('moodle/backup:downloadfile', $context);
587
588 $sectionid = (int)array_shift($args);
589
590 $filename = array_pop($args);
d85ab705 591 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
592 if (!$file = $fs->get_file($context->id, 'backup', 'section', $sectionid, $filepath, $filename) or $file->is_directory()) {
593 send_file_not_found();
594 }
595
596 session_get_instance()->write_close();
597 send_stored_file($file, 60*60, 0, $forcedownload);
598
599 } else if ($filearea === 'activity' and $context->contextlevel == CONTEXT_MODULE) {
600 require_login($course, false, $cm);
601 require_capability('moodle/backup:downloadfile', $context);
602
603 $filename = array_pop($args);
d85ab705 604 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
605 if (!$file = $fs->get_file($context->id, 'backup', 'activity', 0, $filepath, $filename) or $file->is_directory()) {
606 send_file_not_found();
607 }
608
609 session_get_instance()->write_close();
610 send_stored_file($file, 60*60, 0, $forcedownload);
611
612 } else {
d92e7c4d 613 send_file_not_found();
614 }
615
64f93798
PS
616// ========================================================================================================================
617} else if (strpos($component, 'mod_') === 0) {
618 $modname = substr($component, 4);
619 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
d92e7c4d 620 send_file_not_found();
621 }
64f93798 622 require_once("$CFG->dirroot/mod/$modname/lib.php");
172dd12c 623
64f93798
PS
624 if ($context->contextlevel == CONTEXT_MODULE) {
625 if ($cm->modname !== $modname) {
626 // somebody tries to gain illegal access, cm type must match the component!
9e5fa330 627 send_file_not_found();
172dd12c 628 }
64f93798
PS
629 }
630
631 if ($filearea === 'intro') {
632 if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) {
57956cc3
PS
633 send_file_not_found();
634 }
635 require_course_login($course, true, $cm);
636
d92e7c4d 637 // all users may access it
64f93798 638 $filename = array_pop($args);
d85ab705 639 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798 640 if (!$file = $fs->get_file($context->id, 'mod_'.$modname, 'intro', 0, $filepath, $filename) or $file->is_directory()) {
dc5c2bd9 641 send_file_not_found();
642 }
643
d92e7c4d 644 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
dc5c2bd9 645
d92e7c4d 646 // finally send the file
647 send_stored_file($file, $lifetime, 0);
648 }
649
64f93798
PS
650 $filefunction = $component.'_pluginfile';
651 $filefunctionold = $modname.'_pluginfile';
d92e7c4d 652 if (function_exists($filefunction)) {
520199ea 653 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
64f93798
PS
654 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
655 } else if (function_exists($filefunctionold)) {
656 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
657 $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload);
b287ea7a 658 }
520199ea 659
660 send_file_not_found();
172dd12c 661
64f93798
PS
662// ========================================================================================================================
663} else if (strpos($component, 'block_') === 0) {
664 $blockname = substr($component, 6);
665 // note: no more class methods in blocks please, that is ....
666 if (!file_exists("$CFG->dirroot/blocks/$blockname/lib.php")) {
f1cbbbeb
SH
667 send_file_not_found();
668 }
64f93798 669 require_once("$CFG->dirroot/blocks/$blockname/lib.php");
f1cbbbeb 670
64f93798
PS
671 if ($context->contextlevel == CONTEXT_BLOCK) {
672 $birecord = $DB->get_record('block_instances', array('id'=>$context->instanceid), '*',MUST_EXIST);
673 if ($birecord->blockname !== $blockname) {
674 // somebody tries to gain illegal access, cm type must match the component!
675 send_file_not_found();
676 }
677 } else {
678 $birecord = null;
f1cbbbeb
SH
679 }
680
64f93798
PS
681 $filefunction = $component.'_pluginfile';
682 if (function_exists($filefunction)) {
683 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
684 $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload);
f1cbbbeb
SH
685 }
686
687 send_file_not_found();
172dd12c 688
64f93798
PS
689} else if (strpos($component, '_') === false) {
690 // all core subsystems have to be specified above, no more guessing here!
691 send_file_not_found();
692
d92e7c4d 693} else {
64f93798
PS
694 // try to serve general plugin file in arbitrary context
695 $dir = get_component_directory($component);
696 if (!file_exists("$dir/lib.php")) {
697 send_file_not_found();
698 }
77dd3034 699 include_once("$dir/lib.php");
64f93798
PS
700
701 $filefunction = $component.'_pluginfile';
702 if (function_exists($filefunction)) {
703 // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
704 $filefunction($course, $cm, $context, $filearea, $args, $forcedownload);
705 }
706
d92e7c4d 707 send_file_not_found();
708}