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 serves user's private files
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('config.php');
28 require_once('lib/filelib.php');
30 // disable moodle specific debug messages
33 $relativepath = get_file_argument();
34 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
36 // relative path must start with '/'
38 print_error('invalidargorconf');
39 } else if ($relativepath{0} != '/') {
40 print_error('pathdoesnotstartslash');
43 // extract relative path components
44 $args = explode('/', ltrim($relativepath, '/'));
46 if (count($args) == 0) { // always at least user id
47 print_error('invalidarguments');
50 $contextid = (int)array_shift($args);
51 $filearea = array_shift($args);
53 $context = get_context_instance_by_id($contextid);
54 if ($context->contextlevel != CONTEXT_USER) {
55 print_error('invalidarguments');
58 $userid = $context->instanceid;
64 print_error('noguest');
67 // access controll here must match user edit forms
68 if ($userid == $USER->id) {
69 if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
70 send_file_not_found();
73 if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
74 send_file_not_found();
78 $forcedownload = true;
84 send_file_not_found();
86 if ($USER->id != $userid) {
87 send_file_not_found();
90 $forcedownload = true;
94 send_file_not_found();
97 $relativepath = '/'.implode('/', $args);
99 $fs = get_file_storage();
101 $fullpath = $context->id.$filearea.$itemid.$relativepath;
103 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
104 send_file_not_found();
107 // ========================================
108 // finally send the file
109 // ========================================
110 session_get_instance()->write_close(); // unlock session during fileserving
111 send_stored_file($file, 0, false, $forcedownload);