removed $SESSION->encoding now replaced by current_charset() and $CFG->unicodedb...
[moodle.git] / file.php
CommitLineData
e7f927a0 1<?php // $Id$
2 // This script fetches files from the dataroot directory
3 // Syntax: file.php/courseid/dir/dir/dir/filename.ext
69faecce 4 // file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
e7f927a0 5 // file.php/courseid/dir (returns index.html from dir)
6 // Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
48283ff6 7 // Test: file.php/testslasharguments
e7f927a0 8
822a1063 9 require_once('config.php');
4d68dff4 10 require_once('lib/filelib.php');
f9903ed0 11
66e5f959 12 if (empty($CFG->filelifetime)) {
e7f927a0 13 $lifetime = 86400; // Seconds for files to remain in caches
6ed3da1d 14 } else {
e7f927a0 15 $lifetime = $CFG->filelifetime;
3f8247c2 16 }
e7f927a0 17
3f8247c2 18
e7f927a0 19 $relativepath = get_file_argument('file.php');
69faecce 20 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
e7f927a0 21
22 // relative path must start with '/', because of backup/restore!!!
23 if (!$relativepath) {
24 error('No valid arguments supplied or incorrect server configuration');
25 } else if ($relativepath{0} != '/') {
26 error('No valid arguments supplied, path does not start with slash!');
f9903ed0 27 }
28
e7f927a0 29 $pathname = $CFG->dataroot.$relativepath;
ae67d9cd 30
e7f927a0 31 // extract relative path components
32 $args = explode('/', trim($relativepath, '/'));
33 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
822a1063 34 error('No valid arguments supplied');
55b8ac31 35 }
36
e7f927a0 37 // security: limit access to existing course subdirectories
38 // note: course ID must be specified
39 // note: the lang field is needed for the course language switching hack in weblib.php
40 if (!$course = get_record_sql("SELECT id, lang FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'")) {
41 error('Invalid course ID');
a4557331 42 }
43
e7f927a0 44 // security: prevent access to "000" or "1 something" directories
45 if ($args[0] != $course->id) {
822a1063 46 error('Invalid course ID');
76112421 47 }
21ddaf60 48
e7f927a0 49 // security: login to course if necessary
50 if ($course->id != SITEID) {
51 require_login($course->id);
f57cd38b 52 } else if ($CFG->forcelogin) {
53 require_login();
f9903ed0 54 }
55
e7f927a0 56 // security: only editing teachers can access backups
57 if ((!isteacheredit($course->id))
58 and (count($args) >= 2)
59 and (strtolower($args[1]) == 'backupdata')) {
f9903ed0 60
e7f927a0 61 error('Access not allowed');
62 }
f9903ed0 63
e7f927a0 64 if (is_dir($pathname)) {
65 if (file_exists($pathname.'/index.html')) {
66 $pathname = rtrim($pathname, '/').'/index.html';
67 $args[] = 'index.html';
68 } else if (file_exists($pathname.'/index.htm')) {
69 $pathname = rtrim($pathname, '/').'/index.htm';
70 $args[] = 'index.htm';
71 } else if (file_exists($pathname.'/Default.htm')) {
72 $pathname = rtrim($pathname, '/').'/Default.htm';
73 $args[] = 'Default.htm';
74 } else {
75 // security: do not return directory node!
76 not_found($course->id);
77 }
78 }
3cf4ab97 79
fd05dffe 80 // security: teachers can view all assignments, students only their own
81 if ((count($args) >= 3)
82 and (strtolower($args[1]) == 'moddata')
83 and (strtolower($args[2]) == 'assignment')) {
84
85 $lifetime = 0; // do not cache assignments, students may reupload them
86 if ((!isteacher($course->id)) && (count($args) != 6 || $args[4] != $USER->id)) {
87 error('Access not allowed');
88 }
89 }
90
69faecce 91 // security: force download of all attachments submitted by students
92 if ((count($args) >= 3)
93 and (strtolower($args[1]) == 'moddata')
94 and ((strtolower($args[2]) == 'forum')
95 or (strtolower($args[2]) == 'assignment')
96 or (strtolower($args[2]) == 'glossary')
97 or (strtolower($args[2]) == 'wiki')
98 or (strtolower($args[2]) == 'exercise')
99 or (strtolower($args[2]) == 'workshop')
100 )) {
101 $forcedownload = 1; // force download of all attachments
102 }
103
fd05dffe 104 // security: some protection of hidden resource files
105 // warning: it may break backwards compatibility
fd05dffe 106 if ((!empty($CFG->preventaccesstohiddenfiles))
107 and (count($args) >= 2)
108 and (!isteacher($course->id))) {
109
110 $reference = ltrim($relativepath, "/{$args[0]}/");
111
112 $sql = "SELECT COUNT(r.id) " .
113 "FROM {$CFG->prefix}resource r, " .
114 "{$CFG->prefix}course_modules cm, " .
115 "{$CFG->prefix}modules m " .
116 "WHERE r.course = '{$course->id}' " .
117 "AND m.name = 'resource' " .
118 "AND cm.module = m.id " .
119 "AND cm.instance = r.id " .
120 "AND cm.visible = 0 " .
121 "AND r.type = 'file' " .
122 "AND r.reference = '{$reference}'";
123 if (count_records_sql($sql)) {
124 error('Access not allowed');
125 }
126 }
127
e7f927a0 128 // check that file exists
129 if (!file_exists($pathname)) {
130 not_found($course->id);
131 }
e5890e71 132
e7f927a0 133 // extra security: keep symbolic links inside dataroot/courseid if required
134 /*if (!empty($CFG->checksymlinks)) {
135 $realpath = realpath($pathname);
136 $realdataroot = realpath($CFG->dataroot.'/'.$course->id);
137 if (strpos($realpath, $realdataroot) !== 0) {
138 not_found($course->id);
f0507011 139 }
e7f927a0 140 }*/
141
142 // ========================================
143 // finally send the file
144 // ========================================
2ea55bc0 145 session_write_close(); // unlock session during fileserving
e7f927a0 146 $filename = $args[count($args)-1];
b9709b76 147 send_file($pathname, $filename, $lifetime, $CFG->filteruploadedfiles, false, $forcedownload);
e7f927a0 148
149 function not_found($courseid) {
150 global $CFG;
822a1063 151 header('HTTP/1.0 404 not found');
e7f927a0 152 error(get_string('filenotfound', 'error'), $CFG->wwwroot.'/course/view.php?id='.$courseid); //this is not displayed on IIS??
f9903ed0 153 }
e7f927a0 154?>