* Small correction
[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);
f57cd38b 39 } else if ($CFG->forcelogin) {
40 require_login();
f9903ed0 41 }
42
21ddaf60 43 // it's OK to get here if no course was specified
44
9277dbd4 45 $pathname = "$CFG->dataroot$pathinfo";
d45bd055 46 if ($pathargs = explode("?",$pathname)) {
47 $pathname = $pathargs[0]; // Only keep what's before the '?'
48 }
f9903ed0 49 $filename = $args[$numargs-1];
d45bd055 50 if ($fileargs = explode("?",$filename)) {
51 $filename = $fileargs[0]; // Only keep what's before the '?'
52 }
f9903ed0 53
f9903ed0 54 if (file_exists($pathname)) {
55 $lastmodified = filemtime($pathname);
b7a3d3b2 56 $mimetype = mimeinfo("type", $filename);
f9903ed0 57
58 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
66e5f959 59 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . " GMT");
60 header("Cache-control: max_age = $CFG->filelifetime");
f9903ed0 61 header("Pragma: ");
5be2f47c 62 header("Content-disposition: inline; filename=$filename");
b7a3d3b2 63
f0507011 64
3cf4ab97 65 if (empty($CFG->filteruploadedfiles)) {
e5890e71 66 header("Content-length: ".filesize($pathname));
b7a3d3b2 67 header("Content-type: $mimetype");
68 readfile($pathname);
3cf4ab97 69
70 } else { /// Try and put the file through filters
71 if ($mimetype == "text/html") {
e5890e71 72 $output = format_text(implode('', file($pathname)), FORMAT_HTML, NULL, $courseid);
73
74 header("Content-length: ".strlen($output));
3cf4ab97 75 header("Content-type: text/html");
e5890e71 76 echo $output;
3cf4ab97 77
78 } else if ($mimetype == "text/plain") {
3cf4ab97 79 $options->newlines = false;
e5890e71 80 $output = '<pre>'.format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid).'</pre>';
81 header("Content-length: ".strlen($output));
82 header("Content-type: text/html");
83 echo $output;
3cf4ab97 84
85 } else { /// Just send it out raw
e5890e71 86 header("Content-length: ".filesize($pathname));
3cf4ab97 87 header("Content-type: $mimetype");
88 readfile($pathname);
89 }
f0507011 90 }
f9903ed0 91 } else {
6093cbfc 92 error("Sorry, but the file you are looking for was not found ($pathname)",
93 "$CFG->wwwroot/course/view.php?id=$courseid");
f9903ed0 94 }
95
96 exit;
97?>