Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / file.php
blobf2a4b966b8d159c3fff35502cedbfb9b81a4ce95
1 <?php // $Id$
2 // This script fetches files from the dataroot directory
3 //
4 // You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
5 // This ensures proper formatting and offers useful options.
6 //
7 // Syntax: file.php/courseid/dir/dir/dir/filename.ext
8 // file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
9 // file.php/courseid/dir (returns index.html from dir)
10 // Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
11 // Test: file.php/testslasharguments
14 //TODO: Blog attachments do not have access control implemented - anybody can read them!
15 // It might be better to move the code to separate file because the access
16 // control is quite complex - see bolg/index.php
18 require_once('config.php');
19 require_once('lib/filelib.php');
21 if (!isset($CFG->filelifetime)) {
22 $lifetime = 86400; // Seconds for files to remain in caches
23 } else {
24 $lifetime = $CFG->filelifetime;
27 // disable moodle specific debug messages
28 disable_debugging();
30 $relativepath = get_file_argument('file.php');
31 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
33 // relative path must start with '/', because of backup/restore!!!
34 if (!$relativepath) {
35 error('No valid arguments supplied or incorrect server configuration');
36 } else if ($relativepath{0} != '/') {
37 error('No valid arguments supplied, path does not start with slash!');
40 $pathname = $CFG->dataroot.$relativepath;
42 // extract relative path components
43 $args = explode('/', trim($relativepath, '/'));
44 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
45 error('No valid arguments supplied');
48 // security: limit access to existing course subdirectories
49 if (($args[0]!='blog') and (!$course = get_record_sql("SELECT * FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'"))) {
50 error('Invalid course ID');
53 // security: prevent access to "000" or "1 something" directories
54 // hack for blogs, needs proper security check too
55 if (($args[0] != 'blog') and ($args[0] != $course->id)) {
56 error('Invalid course ID');
59 // security: login to course if necessary
60 // Note: file.php always calls require_login() with $setwantsurltome=false
61 // in order to avoid messing redirects. MDL-14495
62 if ($args[0] == 'blog') {
63 if (empty($CFG->bloglevel)) {
64 error('Blogging is disabled!');
65 } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
66 require_login(0, true, null, false);
67 } else if ($CFG->forcelogin) {
68 require_login(0, true, null, false);
70 } else if ($course->id != SITEID) {
71 require_login($course->id, true, null, false);
72 } else if ($CFG->forcelogin) {
73 if (!empty($CFG->sitepolicy)
74 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath
75 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) {
76 //do not require login for policy file
77 } else {
78 require_login(0, true, null, false);
82 // security: only editing teachers can access backups
83 if ((count($args) >= 2) and (strtolower($args[1]) == 'backupdata')) {
84 if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $course->id))) {
85 error('Access not allowed');
86 } else {
87 $lifetime = 0; //disable browser caching for backups
91 if (is_dir($pathname)) {
92 if (file_exists($pathname.'/index.html')) {
93 $pathname = rtrim($pathname, '/').'/index.html';
94 $args[] = 'index.html';
95 } else if (file_exists($pathname.'/index.htm')) {
96 $pathname = rtrim($pathname, '/').'/index.htm';
97 $args[] = 'index.htm';
98 } else if (file_exists($pathname.'/Default.htm')) {
99 $pathname = rtrim($pathname, '/').'/Default.htm';
100 $args[] = 'Default.htm';
101 } else {
102 // security: do not return directory node!
103 not_found($course->id);
107 // security: teachers can view all assignments, students only their own
108 if ((count($args) >= 3)
109 and (strtolower($args[1]) == 'moddata')
110 and (strtolower($args[2]) == 'assignment')) {
112 $lifetime = 0; // do not cache assignments, students may reupload them
113 if ($args[4] == $USER->id) {
114 //can view own assignemnt submissions
115 } else {
116 $instance = (int)$args[3];
117 if (!$cm = get_coursemodule_from_instance('assignment', $instance, $course->id)) {
118 not_found($course->id);
120 if (!has_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $cm->id))) {
121 error('Access not allowed');
126 // security: force download of all attachments submitted by students
127 if (count($args) >= 3 and strtolower($args[1]) === 'moddata') {
128 $mod = clean_param($args[2], PARAM_SAFEDIR);
129 if (file_exists("$CFG->dirroot/mod/$mod/lib.php")) {
130 if (!$forcedownload) {
131 require_once("$CFG->dirroot/mod/$mod/lib.php");
132 $trustedfunction = $mod.'_is_moddata_trusted';
133 if (function_exists($trustedfunction)) {
134 // force download of all attachments that are not trusted
135 $forcedownload = !$trustedfunction();
136 } else {
137 $forcedownload = 1;
140 } else {
141 // module is not installed - better not serve file at all
142 not_found($course->id);
145 if ($args[0] == 'blog') {
146 $forcedownload = 1; // force download of all attachments
149 // security: some protection of hidden resource files
150 // warning: it may break backwards compatibility
151 if ((!empty($CFG->preventaccesstohiddenfiles))
152 and (count($args) >= 2)
153 and (!(strtolower($args[1]) == 'moddata' and strtolower($args[2]) != 'resource')) // do not block files from other modules!
154 and (!has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $course->id)))) {
156 $rargs = $args;
157 array_shift($rargs);
158 $reference = implode('/', $rargs);
160 $sql = "SELECT COUNT(r.id) " .
161 "FROM {$CFG->prefix}resource r, " .
162 "{$CFG->prefix}course_modules cm, " .
163 "{$CFG->prefix}modules m " .
164 "WHERE r.course = '{$course->id}' " .
165 "AND m.name = 'resource' " .
166 "AND cm.module = m.id " .
167 "AND cm.instance = r.id " .
168 "AND cm.visible = 0 " .
169 "AND r.type = 'file' " .
170 "AND r.reference = '{$reference}'";
171 if (count_records_sql($sql)) {
172 error('Access not allowed');
176 // check that file exists
177 if (!file_exists($pathname)) {
178 not_found($course->id);
181 // ========================================
182 // finally send the file
183 // ========================================
184 session_write_close(); // unlock session during fileserving
185 $filename = $args[count($args)-1];
186 send_file($pathname, $filename, $lifetime, $CFG->filteruploadedfiles, false, $forcedownload);
188 function not_found($courseid) {
189 global $CFG;
190 header('HTTP/1.0 404 not found');
191 print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$courseid); //this is not displayed on IIS??