2004-09-29 18:19:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* file.php - Used to fetch file from the data directory
|
|
|
|
*
|
|
|
|
* This script file fetches files from the data directory (dataroot)<br>
|
|
|
|
* Syntax: file.php/courseid/dir/.../dir/filename.ext
|
|
|
|
*
|
|
|
|
* @uses $CFG
|
|
|
|
* @uses FORMAT_HTML
|
|
|
|
* @uses FORMAT_MOODLE
|
|
|
|
* @author Martin Dougiamas
|
|
|
|
* @version $Id$
|
|
|
|
* @package moodlecore
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 {
|
2004-09-29 18:19:39 +00:00
|
|
|
$pathinfo = get_slash_arguments('file.php');
|
2002-08-26 09:48:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-12 06:53:25 +00:00
|
|
|
if (!$pathinfo) {
|
2004-09-29 18:19:39 +00:00
|
|
|
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)) {
|
2004-09-29 18:19:39 +00:00
|
|
|
error('No valid arguments supplied');
|
2002-09-05 11:56:20 +00:00
|
|
|
}
|
|
|
|
|
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])) {
|
2004-09-29 18:19:39 +00:00
|
|
|
error('No valid arguments supplied');
|
2004-03-16 07:20:00 +00:00
|
|
|
}
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
$courseid = (integer)$args[0];
|
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
if (!$course = get_record('course', 'id', $courseid)) { // Course ID must be specified
|
|
|
|
error('Invalid course ID');
|
2004-09-07 14:13:19 +00:00
|
|
|
}
|
2001-11-25 15:48:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
$pathname = $CFG->dataroot . $pathinfo;
|
|
|
|
if ($pathargs = explode('?', $pathname)) {
|
2004-05-01 03:33:10 +00:00
|
|
|
$pathname = $pathargs[0]; // Only keep what's before the '?'
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
$filename = $args[$numargs-1];
|
2004-09-29 18:19:39 +00:00
|
|
|
if ($fileargs = explode('?', $filename)) {
|
2004-05-01 03:33:10 +00:00
|
|
|
$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-09-29 18:19:39 +00:00
|
|
|
$mimetype = mimeinfo('type', $filename);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastmodified) . ' GMT');
|
|
|
|
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $CFG->filelifetime) . ' GMT');
|
|
|
|
header('Cache-control: max_age = '. $CFG->filelifetime);
|
|
|
|
header('Pragma: ');
|
|
|
|
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-09-29 18:19:39 +00:00
|
|
|
header('Content-length: '. filesize($pathname));
|
|
|
|
header('Content-type: '. $mimetype);
|
2004-03-09 06:44:27 +00:00
|
|
|
readfile($pathname);
|
2004-03-11 02:14:15 +00:00
|
|
|
|
|
|
|
} else { /// Try and put the file through filters
|
2004-09-29 18:19:39 +00:00
|
|
|
if ($mimetype == 'text/html') {
|
2004-08-10 10:44:29 +00:00
|
|
|
$options->noclean = true;
|
|
|
|
$output = format_text(implode('', file($pathname)), FORMAT_HTML, $options, $courseid);
|
2004-04-26 03:41:45 +00:00
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
header('Content-length: '. strlen($output));
|
|
|
|
header('Content-type: text/html');
|
2004-04-26 03:41:45 +00:00
|
|
|
echo $output;
|
2004-03-11 02:14:15 +00:00
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
} else if ($mimetype == 'text/plain') {
|
2004-03-11 02:14:15 +00:00
|
|
|
$options->newlines = false;
|
2004-08-10 10:44:29 +00:00
|
|
|
$options->noclean = true;
|
2004-09-29 18:19:39 +00:00
|
|
|
$output = '<pre>'. format_text(implode('', file($pathname)), FORMAT_MOODLE, $options, $courseid) .'</pre>';
|
|
|
|
header('Content-length: '. strlen($output));
|
|
|
|
header('Content-type: text/html');
|
2004-04-26 03:41:45 +00:00
|
|
|
echo $output;
|
2004-03-11 02:14:15 +00:00
|
|
|
|
|
|
|
} else { /// Just send it out raw
|
2004-09-29 18:19:39 +00:00
|
|
|
header('Content-length: '. filesize($pathname));
|
|
|
|
header('Content-type: '. $mimetype);
|
2004-03-11 02:14:15 +00:00
|
|
|
readfile($pathname);
|
|
|
|
}
|
2004-03-09 06:28:01 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2004-09-29 18:19:39 +00:00
|
|
|
header('HTTP/1.0 404 not found');
|
|
|
|
error(get_string('filenotfound', 'error'), $CFG->wwwroot .'/course/view.php?id='. $courseid);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
2004-09-29 18:19:39 +00:00
|
|
|
?>
|