2004-12-14 18:57:51 +00:00
|
|
|
<?php // $Id$
|
|
|
|
// This script fetches files from the dataroot directory
|
2008-07-10 09:55:11 +00:00
|
|
|
//
|
|
|
|
// You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
|
|
|
|
// This ensures proper formatting and offers useful options.
|
|
|
|
//
|
2004-12-14 18:57:51 +00:00
|
|
|
// Syntax: file.php/courseid/dir/dir/dir/filename.ext
|
2005-07-12 08:05:42 +00:00
|
|
|
// file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
|
2004-12-14 18:57:51 +00:00
|
|
|
// file.php/courseid/dir (returns index.html from dir)
|
|
|
|
// Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
|
2005-02-26 20:43:30 +00:00
|
|
|
// Test: file.php/testslasharguments
|
2004-12-14 18:57:51 +00:00
|
|
|
|
2006-12-28 21:21:44 +00:00
|
|
|
|
|
|
|
//TODO: Blog attachments do not have access control implemented - anybody can read them!
|
|
|
|
// It might be better to move the code to separate file because the access
|
2008-07-31 22:15:30 +00:00
|
|
|
// control is quite complex - see bolg/index.php
|
2006-12-28 21:21:44 +00:00
|
|
|
|
2004-09-29 18:19:39 +00:00
|
|
|
require_once('config.php');
|
2005-03-07 11:34:16 +00:00
|
|
|
require_once('lib/filelib.php');
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2007-09-25 09:37:58 +00:00
|
|
|
if (!isset($CFG->filelifetime)) {
|
2004-12-14 18:57:51 +00:00
|
|
|
$lifetime = 86400; // Seconds for files to remain in caches
|
2003-01-12 06:53:25 +00:00
|
|
|
} else {
|
2004-12-14 18:57:51 +00:00
|
|
|
$lifetime = $CFG->filelifetime;
|
2006-09-21 06:38:27 +00:00
|
|
|
}
|
|
|
|
|
2006-09-23 09:38:39 +00:00
|
|
|
// disable moodle specific debug messages
|
|
|
|
disable_debugging();
|
2002-08-26 09:48:00 +00:00
|
|
|
|
2004-12-14 18:57:51 +00:00
|
|
|
$relativepath = get_file_argument('file.php');
|
2005-07-12 08:05:42 +00:00
|
|
|
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
|
2008-07-31 22:15:30 +00:00
|
|
|
|
2004-12-14 18:57:51 +00:00
|
|
|
// relative path must start with '/', because of backup/restore!!!
|
|
|
|
if (!$relativepath) {
|
2008-04-30 04:09:29 +00:00
|
|
|
print_error('invalidargorconf');
|
2004-12-14 18:57:51 +00:00
|
|
|
} else if ($relativepath{0} != '/') {
|
2008-04-30 04:09:29 +00:00
|
|
|
print_error('pathdoesnotstartslash');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2004-12-14 18:57:51 +00:00
|
|
|
// extract relative path components
|
2008-07-31 22:15:30 +00:00
|
|
|
$args = explode('/', ltrim($relativepath, '/'));
|
|
|
|
|
2004-12-14 18:57:51 +00:00
|
|
|
if (count($args) == 0) { // always at least courseid, may search for index.html in course root
|
2008-04-30 04:09:29 +00:00
|
|
|
print_error('invalidarguments');
|
2002-09-05 11:56:20 +00:00
|
|
|
}
|
2004-03-16 07:20:00 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
$courseid = (int)array_shift($args);
|
|
|
|
$relativepath = '/'.implode('/', $args);
|
|
|
|
|
|
|
|
// security: limit access to existing course subdirectories
|
|
|
|
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
|
2008-04-30 04:09:29 +00:00
|
|
|
print_error('invalidcourseid');
|
2004-09-07 14:13:19 +00:00
|
|
|
}
|
2001-11-25 15:48:24 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
if ($course->id != SITEID) {
|
2008-04-25 18:55:36 +00:00
|
|
|
require_login($course->id, true, null, false);
|
2008-07-31 22:15:30 +00:00
|
|
|
|
2004-05-28 01:17:37 +00:00
|
|
|
} else if ($CFG->forcelogin) {
|
2007-04-02 16:13:41 +00:00
|
|
|
if (!empty($CFG->sitepolicy)
|
|
|
|
and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath
|
|
|
|
or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) {
|
|
|
|
//do not require login for policy file
|
|
|
|
} else {
|
2008-04-25 18:55:36 +00:00
|
|
|
require_login(0, true, null, false);
|
2007-04-02 16:13:41 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
$fs = get_file_storage();
|
2005-02-22 23:36:20 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
$fullpath = $context->id.'course_content0'.$relativepath;
|
2005-02-22 23:36:20 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
|
|
|
|
if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
|
|
|
|
$fullpath .= '/';
|
|
|
|
}
|
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
|
2008-08-16 17:23:19 +00:00
|
|
|
send_file_not_found();
|
2005-02-22 23:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-31 22:15:30 +00:00
|
|
|
// do not serve dirs
|
|
|
|
if ($file->get_filename() == '.') {
|
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
|
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
|
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
|
2008-08-16 17:23:19 +00:00
|
|
|
send_file_not_found();
|
2008-07-31 22:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-14 18:57:51 +00:00
|
|
|
}
|
2004-04-26 03:41:45 +00:00
|
|
|
|
2004-12-14 18:57:51 +00:00
|
|
|
// ========================================
|
|
|
|
// finally send the file
|
|
|
|
// ========================================
|
2005-05-17 01:00:48 +00:00
|
|
|
session_write_close(); // unlock session during fileserving
|
2008-07-31 22:15:30 +00:00
|
|
|
send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
|
2004-12-14 18:57:51 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
|