Neue Felder eingearbeitet und Verbesserungen
[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);
f9903ed0 27 $courseid = (integer)$args[0];
28
21ddaf60 29 $course = get_record("course", "id", $courseid);
30
31 if ($course->category) {
f9903ed0 32 require_login($courseid);
33 }
34
21ddaf60 35 // it's OK to get here if no course was specified
36
9277dbd4 37 $pathname = "$CFG->dataroot$pathinfo";
f9903ed0 38 $filename = $args[$numargs-1];
39
f9903ed0 40 if (file_exists($pathname)) {
41 $lastmodified = filemtime($pathname);
b7a3d3b2 42 $mimetype = mimeinfo("type", $filename);
f9903ed0 43
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: ");
5be2f47c 48 header("Content-disposition: inline; filename=$filename");
49 header("Content-length: ".filesize($pathname));
b7a3d3b2 50
f0507011 51
52 if ($mimetype == "text/html") {
b7a3d3b2 53 header("Content-type: text/html");
54 echo format_text(implode('', file($pathname)), FORMAT_HTML, NULL, $courseid); // Filter HTML files
55
56 } else if ($mimetype == "text/plain") {
57 header("Content-type: text/html");
58 $options->newlines = false;
59 echo "<pre>";
60 echo format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid); // Filter TEXT files
61 echo "</pre>";
62
f0507011 63 } else {
b7a3d3b2 64 header("Content-type: $mimetype");
65 readfile($pathname);
f0507011 66 }
f9903ed0 67 } else {
dcde9f02 68 error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
f9903ed0 69 }
70
71 exit;
72?>