2001-11-22 06:23:56 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
// This function fetches files from the data directory
|
|
|
|
// Syntax: file.php/courseid/dir/.../dir/filename.ext
|
|
|
|
|
2003-01-05 14:19:20 +00:00
|
|
|
require_once("config.php");
|
|
|
|
require_once("files/mimetypes.php");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-04-01 02:32:04 +00:00
|
|
|
if (empty($CFG->filelifetime)) {
|
|
|
|
$CFG->filelifetime = 86400; /// Seconds for files to remain in caches
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-08-26 09:48:00 +00:00
|
|
|
if (isset($file)) { // workaround for situations where / syntax doesn't work
|
2003-01-12 06:53:25 +00:00
|
|
|
$pathinfo = $file;
|
|
|
|
} else {
|
|
|
|
$pathinfo = get_slash_arguments("file.php");
|
2002-08-26 09:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-12 06:53:25 +00:00
|
|
|
if (!$pathinfo) {
|
|
|
|
error("No file parameters!");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2003-11-27 17:23:36 +00:00
|
|
|
$pathinfo = urldecode($pathinfo);
|
|
|
|
|
2003-01-12 06:53:25 +00:00
|
|
|
if (! $args = parse_slash_arguments($pathinfo)) {
|
2002-09-05 11:56:20 +00:00
|
|
|
error("No valid arguments supplied");
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
$numargs = count($args);
|
2004-03-16 07:20:00 +00:00
|
|
|
if ($numargs < 2 or empty($args[1])) {
|
|
|
|
error("No valid arguments supplied");
|
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
$courseid = (integer)$args[0];
|
|
|
|
|
2001-11-25 15:48:24 +00:00
|
|
|
$course = get_record("course", "id", $courseid);
|
|
|
|
|
|
|
|
if ($course->category) {
|
2001-11-22 06:23:56 +00:00
|
|
|
require_login($courseid);
|
2004-05-28 01:17:37 +00:00
|
|
|
} else if ($CFG->forcelogin) {
|
|
|
|
require_login();
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2001-11-25 15:48:24 +00:00
|
|
|
// it's OK to get here if no course was specified
|
|
|
|
|
2003-01-15 05:32:14 +00:00
|
|
|
$pathname = "$CFG->dataroot$pathinfo";
|
2004-05-01 03:33:10 +00:00
|
|
|
if ($pathargs = explode("?",$pathname)) {
|
|
|
|
$pathname = $pathargs[0]; // Only keep what's before the '?'
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
$filename = $args[$numargs-1];
|
2004-05-01 03:33:10 +00:00
|
|
|
if ($fileargs = explode("?",$filename)) {
|
|
|
|
$filename = $fileargs[0]; // Only keep what's before the '?'
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
if (file_exists($pathname)) {
|
|
|
|
$lastmodified = filemtime($pathname);
|
2004-03-09 06:44:27 +00:00
|
|
|
$mimetype = mimeinfo("type", $filename);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
|
2004-04-01 02:32:04 +00:00
|
|
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . " GMT");
|
|
|
|
header("Cache-control: max_age = $CFG->filelifetime");
|
2001-11-22 06:23:56 +00:00
|
|
|
header("Pragma: ");
|
2002-09-02 09:06:43 +00:00
|
|
|
header("Content-disposition: inline; filename=$filename");
|
2004-03-09 06:44:27 +00:00
|
|
|
|
2004-03-09 06:28:01 +00:00
|
|
|
|
2004-03-11 02:14:15 +00:00
|
|
|
if (empty($CFG->filteruploadedfiles)) {
|
2004-04-26 03:41:45 +00:00
|
|
|
header("Content-length: ".filesize($pathname));
|
2004-03-09 06:44:27 +00:00
|
|
|
header("Content-type: $mimetype");
|
|
|
|
readfile($pathname);
|
2004-03-11 02:14:15 +00:00
|
|
|
|
|
|
|
} else { /// Try and put the file through filters
|
|
|
|
if ($mimetype == "text/html") {
|
2004-04-26 03:41:45 +00:00
|
|
|
$output = format_text(implode('', file($pathname)), FORMAT_HTML, NULL, $courseid);
|
|
|
|
|
|
|
|
header("Content-length: ".strlen($output));
|
2004-03-11 02:14:15 +00:00
|
|
|
header("Content-type: text/html");
|
2004-04-26 03:41:45 +00:00
|
|
|
echo $output;
|
2004-03-11 02:14:15 +00:00
|
|
|
|
|
|
|
} else if ($mimetype == "text/plain") {
|
|
|
|
$options->newlines = false;
|
2004-04-26 03:41:45 +00:00
|
|
|
$output = '<pre>'.format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid).'</pre>';
|
|
|
|
header("Content-length: ".strlen($output));
|
|
|
|
header("Content-type: text/html");
|
|
|
|
echo $output;
|
2004-03-11 02:14:15 +00:00
|
|
|
|
|
|
|
} else { /// Just send it out raw
|
2004-04-26 03:41:45 +00:00
|
|
|
header("Content-length: ".filesize($pathname));
|
2004-03-11 02:14:15 +00:00
|
|
|
header("Content-type: $mimetype");
|
|
|
|
readfile($pathname);
|
|
|
|
}
|
2004-03-09 06:28:01 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2003-01-15 10:55:54 +00:00
|
|
|
error("Sorry, but the file you are looking for was not found ($pathname)", "course/view.php?id=$courseid");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
?>
|