Updated by Bobo
[moodle.git] / file.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2 // This function fetches files from the data directory
3 // Syntax: file.php/courseid/dir/.../dir/filename.ext
4
b0e3a925 5 require_once("config.php");
6 require_once("files/mimetypes.php");
f9903ed0 7
66e5f959 8 if (empty($CFG->filelifetime)) {
9 $CFG->filelifetime = 86400; /// Seconds for files to remain in caches
10 }
f9903ed0 11
3f8247c2 12 if (isset($file)) { // workaround for situations where / syntax doesn't work
6ed3da1d 13 $pathinfo = $file;
14 } else {
15 $pathinfo = get_slash_arguments("file.php");
3f8247c2 16 }
17
6ed3da1d 18 if (!$pathinfo) {
19 error("No file parameters!");
f9903ed0 20 }
21
ae67d9cd 22 $pathinfo = urldecode($pathinfo);
23
6ed3da1d 24 if (! $args = parse_slash_arguments($pathinfo)) {
55b8ac31 25 error("No valid arguments supplied");
26 }
27
f9903ed0 28 $numargs = count($args);
a4557331 29 if ($numargs < 2 or empty($args[1])) {
30 error("No valid arguments supplied");
31 }
32
f9903ed0 33 $courseid = (integer)$args[0];
34
21ddaf60 35 $course = get_record("course", "id", $courseid);
36
37 if ($course->category) {
f9903ed0 38 require_login($courseid);
39 }
40
21ddaf60 41 // it's OK to get here if no course was specified
42
9277dbd4 43 $pathname = "$CFG->dataroot$pathinfo";
f9903ed0 44 $filename = $args[$numargs-1];
45
f9903ed0 46 if (file_exists($pathname)) {
47 $lastmodified = filemtime($pathname);
b7a3d3b2 48 $mimetype = mimeinfo("type", $filename);
f9903ed0 49
50 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
66e5f959 51 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . " GMT");
52 header("Cache-control: max_age = $CFG->filelifetime");
f9903ed0 53 header("Pragma: ");
5be2f47c 54 header("Content-disposition: inline; filename=$filename");
b7a3d3b2 55
f0507011 56
3cf4ab97 57 if (empty($CFG->filteruploadedfiles)) {
e5890e71 58 header("Content-length: ".filesize($pathname));
b7a3d3b2 59 header("Content-type: $mimetype");
60 readfile($pathname);
3cf4ab97 61
62 } else { /// Try and put the file through filters
63 if ($mimetype == "text/html") {
e5890e71 64 $output = format_text(implode('', file($pathname)), FORMAT_HTML, NULL, $courseid);
65
66 header("Content-length: ".strlen($output));
3cf4ab97 67 header("Content-type: text/html");
e5890e71 68 echo $output;
3cf4ab97 69
70 } else if ($mimetype == "text/plain") {
3cf4ab97 71 $options->newlines = false;
e5890e71 72 $output = '<pre>'.format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid).'</pre>';
73 header("Content-length: ".strlen($output));
74 header("Content-type: text/html");
75 echo $output;
3cf4ab97 76
77 } else { /// Just send it out raw
e5890e71 78 header("Content-length: ".filesize($pathname));
3cf4ab97 79 header("Content-type: $mimetype");
80 readfile($pathname);
81 }
f0507011 82 }
f9903ed0 83 } else {
dcde9f02 84 error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
f9903ed0 85 }
86
87 exit;
88?>