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