Too many bugs, not enough time!
[moodle.git] / file.php
blob3c4e0da045a4d259b63caeff735007d40abe2d61
1 <?PHP // $Id$
2 // This function fetches files from the data directory
3 // Syntax: file.php/courseid/dir/.../dir/filename.ext
5 require_once("config.php");
6 require_once("files/mimetypes.php");
8 $lifetime = 86400;
10 if (isset($file)) { // workaround for situations where / syntax doesn't work
11 $pathinfo = $file;
12 } else {
13 $pathinfo = get_slash_arguments("file.php");
16 if (!$pathinfo) {
17 error("No file parameters!");
20 $pathinfo = urldecode($pathinfo);
22 if (! $args = parse_slash_arguments($pathinfo)) {
23 error("No valid arguments supplied");
26 $numargs = count($args);
27 $courseid = (integer)$args[0];
29 $course = get_record("course", "id", $courseid);
31 if ($course->category) {
32 require_login($courseid);
35 // it's OK to get here if no course was specified
37 $pathname = "$CFG->dataroot$pathinfo";
38 $filename = $args[$numargs-1];
40 if (file_exists($pathname)) {
41 $lastmodified = filemtime($pathname);
42 $mimetype = mimeinfo("type", $filename);
44 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
45 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
46 header("Cache-control: max_age = $lifetime"); // a day
47 header("Pragma: ");
48 header("Content-disposition: inline; filename=$filename");
49 header("Content-length: ".filesize($pathname));
52 if (empty($CFG->filteruploadedfiles)) {
53 header("Content-type: $mimetype");
54 readfile($pathname);
56 } else { /// Try and put the file through filters
57 if ($mimetype == "text/html") {
58 header("Content-type: text/html");
59 echo format_text(implode('', file($pathname)), FORMAT_HTML, NULL, $courseid);
61 } else if ($mimetype == "text/plain") {
62 header("Content-type: text/html");
63 $options->newlines = false;
64 echo "<pre>";
65 echo format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid);
66 echo "</pre>";
68 } else { /// Just send it out raw
69 header("Content-type: $mimetype");
70 readfile($pathname);
73 } else {
74 error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
77 exit;