OK, this is IT.
[moodle.git] / file.php
blob3d236cb7645a1f22ff4afa6e12f2526c84f71851
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 if ($numargs < 2 or empty($args[1])) {
28 error("No valid arguments supplied");
31 $courseid = (integer)$args[0];
33 $course = get_record("course", "id", $courseid);
35 if ($course->category) {
36 require_login($courseid);
39 // it's OK to get here if no course was specified
41 $pathname = "$CFG->dataroot$pathinfo";
42 $filename = $args[$numargs-1];
44 if (file_exists($pathname)) {
45 $lastmodified = filemtime($pathname);
46 $mimetype = mimeinfo("type", $filename);
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: ");
52 header("Content-disposition: inline; filename=$filename");
53 header("Content-length: ".filesize($pathname));
56 if (empty($CFG->filteruploadedfiles)) {
57 header("Content-type: $mimetype");
58 readfile($pathname);
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);
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>";
72 } else { /// Just send it out raw
73 header("Content-type: $mimetype");
74 readfile($pathname);
77 } else {
78 error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
81 exit;