SCORM MDL-22168 fix accidental revert - set start time of SCO correctly
[moodle.git] / file.php
blob8c29f48fe4e4603eb2e87d5eab33a83a814584d3
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This script fetches legacy course files in dataroot directory, it is enabled
20 * only if course->legacyfiles == 2.
22 * You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
23 * This ensures proper formatting and offers useful options.
24 * Syntax: file.php/courseid/dir/dir/dir/filename.ext
25 * file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
26 * file.php/courseid/dir (returns index.html from dir)
27 * Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
29 * @package moodlecore
30 * @subpackage file
31 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 // disable moodle specific debug messages and any errors in output
36 define('NO_DEBUG_DISPLAY', true);
38 require_once('config.php');
39 require_once('lib/filelib.php');
41 if (!isset($CFG->filelifetime)) {
42 $lifetime = 86400; // Seconds for files to remain in caches
43 } else {
44 $lifetime = $CFG->filelifetime;
47 $relativepath = get_file_argument();
48 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
50 // relative path must start with '/', because of backup/restore!!!
51 if (!$relativepath) {
52 print_error('invalidargorconf');
53 } else if ($relativepath{0} != '/') {
54 print_error('pathdoesnotstartslash');
57 // extract relative path components
58 $args = explode('/', ltrim($relativepath, '/'));
60 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
61 print_error('invalidarguments');
64 $courseid = (int)array_shift($args);
65 $relativepath = '/'.implode('/', $args);
67 // security: limit access to existing course subdirectories
68 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
70 if ($course->legacyfiles != 2) {
71 // course files disabled
72 send_file_not_found();
75 if ($course->id != SITEID) {
76 require_login($course->id, true, null, false);
78 } else if ($CFG->forcelogin) {
79 if (!empty($CFG->sitepolicy)
80 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath
81 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) {
82 //do not require login for policy file
83 } else {
84 require_login(0, true, null, false);
88 $context = get_context_instance(CONTEXT_COURSE, $course->id);
90 $fs = get_file_storage();
92 $fullpath = $context->id.'course_content0'.$relativepath;
94 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
95 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
96 $fullpath .= '/';
98 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
99 send_file_not_found();
102 // do not serve dirs
103 if ($file->get_filename() == '.') {
104 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
105 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
106 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
107 send_file_not_found();
113 // ========================================
114 // finally send the file
115 // ========================================
116 session_get_instance()->write_close(); // unlock session during fileserving
117 send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);