Automatic installer.php lang files by installer_builder (20070315)
[moodle.git] / file.php
blob501243d51b5748718892f99ed2d32149c9148d8c
1 <?php // $Id$
2 // This script fetches files from the dataroot directory
3 // Syntax: file.php/courseid/dir/dir/dir/filename.ext
4 // file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
5 // file.php/courseid/dir (returns index.html from dir)
6 // Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
7 // Test: file.php/testslasharguments
9 require_once('config.php');
10 require_once('lib/filelib.php');
12 if (empty($CFG->filelifetime)) {
13 $lifetime = 86400; // Seconds for files to remain in caches
14 } else {
15 $lifetime = $CFG->filelifetime;
19 $relativepath = get_file_argument('file.php');
20 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
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!');
29 $pathname = $CFG->dataroot.$relativepath;
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
34 error('No valid arguments supplied');
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');
44 // security: prevent access to "000" or "1 something" directories
45 if ($args[0] != $course->id) {
46 error('Invalid course ID');
49 // security: login to course if necessary
50 if ($course->id != SITEID) {
51 require_login($course->id);
52 } else if ($CFG->forcelogin) {
53 require_login();
56 // security: only editing teachers can access backups
57 if ((count($args) >= 2) and (strtolower($args[1]) == 'backupdata')) {
58 if (!isteacheredit($course->id)) {
59 error('Access not allowed');
60 } else {
61 $lifetime = 0; //disable browser caching for backups
65 if (is_dir($pathname)) {
66 if (file_exists($pathname.'/index.html')) {
67 $pathname = rtrim($pathname, '/').'/index.html';
68 $args[] = 'index.html';
69 } else if (file_exists($pathname.'/index.htm')) {
70 $pathname = rtrim($pathname, '/').'/index.htm';
71 $args[] = 'index.htm';
72 } else if (file_exists($pathname.'/Default.htm')) {
73 $pathname = rtrim($pathname, '/').'/Default.htm';
74 $args[] = 'Default.htm';
75 } else {
76 // security: do not return directory node!
77 not_found($course->id);
81 // security: teachers can view all assignments, students only their own
82 if ((count($args) >= 3)
83 and (strtolower($args[1]) == 'moddata')
84 and (strtolower($args[2]) == 'assignment')) {
86 $lifetime = 0; // do not cache assignments, students may reupload them
87 if ((!isteacher($course->id)) && (count($args) != 6 || $args[4] != $USER->id)) {
88 error('Access not allowed');
92 // security: force download of all attachments submitted by students
93 if ((count($args) >= 3)
94 and (strtolower($args[1]) == 'moddata')
95 and ((strtolower($args[2]) == 'forum')
96 or (strtolower($args[2]) == 'assignment')
97 or (strtolower($args[2]) == 'data')
98 or (strtolower($args[2]) == 'glossary')
99 or (strtolower($args[2]) == 'wiki')
100 or (strtolower($args[2]) == 'exercise')
101 or (strtolower($args[2]) == 'workshop')
102 )) {
103 $forcedownload = 1; // force download of all attachments
106 // security: some protection of hidden resource files
107 // warning: it may break backwards compatibility
108 if ((!empty($CFG->preventaccesstohiddenfiles))
109 and (count($args) >= 2)
110 and (!isteacher($course->id))) {
112 $reference = ltrim($relativepath, "/{$args[0]}/");
114 $sql = "SELECT COUNT(r.id) " .
115 "FROM {$CFG->prefix}resource r, " .
116 "{$CFG->prefix}course_modules cm, " .
117 "{$CFG->prefix}modules m " .
118 "WHERE r.course = '{$course->id}' " .
119 "AND m.name = 'resource' " .
120 "AND cm.module = m.id " .
121 "AND cm.instance = r.id " .
122 "AND cm.visible = 0 " .
123 "AND r.type = 'file' " .
124 "AND r.reference = '{$reference}'";
125 if (count_records_sql($sql)) {
126 error('Access not allowed');
130 // check that file exists
131 if (!file_exists($pathname)) {
132 not_found($course->id);
135 // extra security: keep symbolic links inside dataroot/courseid if required
136 /*if (!empty($CFG->checksymlinks)) {
137 $realpath = realpath($pathname);
138 $realdataroot = realpath($CFG->dataroot.'/'.$course->id);
139 if (strpos($realpath, $realdataroot) !== 0) {
140 not_found($course->id);
144 // ========================================
145 // finally send the file
146 // ========================================
147 session_write_close(); // unlock session during fileserving
148 $filename = $args[count($args)-1];
149 send_file($pathname, $filename, $lifetime, $CFG->filteruploadedfiles, false, $forcedownload);
151 function not_found($courseid) {
152 global $CFG;
153 header('HTTP/1.0 404 not found');
154 error(get_string('filenotfound', 'error'), $CFG->wwwroot.'/course/view.php?id='.$courseid); //this is not displayed on IIS??