fix git cvs drift
[moodle.git] / userfile.php
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.
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/>.
18 /**
19  * This script serves user's private files
20  *
21  * @package    moodlecore
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  */
27 require_once('config.php');
28 require_once('lib/filelib.php');
30 // disable moodle specific debug messages
31 disable_debugging();
33 $relativepath = get_file_argument();
34 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
36 // relative path must start with '/'
37 if (!$relativepath) {
38     print_error('invalidargorconf');
39 } else if ($relativepath{0} != '/') {
40     print_error('pathdoesnotstartslash');
41 }
43 // extract relative path components
44 $args = explode('/', ltrim($relativepath, '/'));
46 if (count($args) == 0) { // always at least user id
47     print_error('invalidarguments');
48 }
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');
56 }
58 $userid = $context->instanceid;
60 switch ($filearea) {
61     case 'user_profile':
62         require_login();
63         if (isguestuser()) {
64             print_error('noguest');
65         }
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();
71              }
72         } else {
73             if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
74                 send_file_not_found();
75             }
76         }
77         $itemid = 0;
78         $forcedownload = true;
79         break;
81     case 'user_private':
82         require_login();
83         if (isguestuser()) {
84             send_file_not_found();
85         }
86         if ($USER->id != $userid) {
87             send_file_not_found();
88         }
89         $itemid = 0;
90         $forcedownload = true;
91         break;
93     default:
94         send_file_not_found();
95 }
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);