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");
10 if (isset($file)) { // workaround for situations where / syntax doesn't work
13 $pathinfo = get_slash_arguments("file.php");
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
48 header("Content-disposition: inline; filename=$filename");
49 header("Content-length: ".filesize($pathname));
52 if (empty($CFG->filteruploadedfiles
)) {
53 header("Content-type: $mimetype");
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;
65 echo format_text(implode('', file($pathname)), FORMAT_MOODLE
, $options, $courseid);
68 } else { /// Just send it out raw
69 header("Content-type: $mimetype");
74 error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");