Slight fix for logic of USER
[moodle.git] / file.php
blob9443edb52d30cc44dfaa12f2df61b29fc40f83cd
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 if (empty($CFG->filelifetime)) {
9 $CFG->filelifetime = 86400; /// Seconds for files to remain in caches
12 if (isset($file)) { // workaround for situations where / syntax doesn't work
13 $pathinfo = $file;
14 } else {
15 $pathinfo = get_slash_arguments("file.php");
18 if (!$pathinfo) {
19 error("No file parameters!");
22 $pathinfo = urldecode($pathinfo);
24 if (! $args = parse_slash_arguments($pathinfo)) {
25 error("No valid arguments supplied");
28 $numargs = count($args);
29 if ($numargs < 2 or empty($args[1])) {
30 error("No valid arguments supplied");
33 $courseid = (integer)$args[0];
35 if (!$course = get_record("course", "id", $courseid)) { // Course ID must be specified
36 error("Invalid course ID");
39 if ($course->category) {
40 require_login($courseid);
41 } else if ($CFG->forcelogin) {
42 require_login();
45 $pathname = "$CFG->dataroot$pathinfo";
46 if ($pathargs = explode("?",$pathname)) {
47 $pathname = $pathargs[0]; // Only keep what's before the '?'
49 $filename = $args[$numargs-1];
50 if ($fileargs = explode("?",$filename)) {
51 $filename = $fileargs[0]; // Only keep what's before the '?'
54 if (file_exists($pathname)) {
55 $lastmodified = filemtime($pathname);
56 $mimetype = mimeinfo("type", $filename);
58 header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
59 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . " GMT");
60 header("Cache-control: max_age = $CFG->filelifetime");
61 header("Pragma: ");
62 header("Content-disposition: inline; filename=$filename");
65 if (empty($CFG->filteruploadedfiles)) {
66 header("Content-length: ".filesize($pathname));
67 header("Content-type: $mimetype");
68 readfile($pathname);
70 } else { /// Try and put the file through filters
71 if ($mimetype == "text/html") {
72 $options->noclean = true;
73 $output = format_text(implode('', file($pathname)), FORMAT_HTML, $options, $courseid);
75 header("Content-length: ".strlen($output));
76 header("Content-type: text/html");
77 echo $output;
79 } else if ($mimetype == "text/plain") {
80 $options->newlines = false;
81 $options->noclean = true;
82 $output = '<pre>'.format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid).'</pre>';
83 header("Content-length: ".strlen($output));
84 header("Content-type: text/html");
85 echo $output;
87 } else { /// Just send it out raw
88 header("Content-length: ".filesize($pathname));
89 header("Content-type: $mimetype");
90 readfile($pathname);
93 } else {
94 header("HTTP/1.0 404 not found");
95 error(get_string("filenotfound", "error"), "$CFG->wwwroot/course/view.php?id=$courseid");
98 exit;