Merge branch 'MDL-71425-311-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_31...
[moodle.git] / file.php
blobab0a80e04522ddc82a7a7a4b9ed01f151421a042
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. DO not link to this file in new code.
22 * Syntax: file.php/courseid/dir/dir/dir/filename.ext
23 * file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
24 * file.php/courseid/dir (returns index.html from dir)
25 * Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
27 * @package core
28 * @subpackage file
29 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 // disable moodle specific debug messages and any errors in output
34 define('NO_DEBUG_DISPLAY', true);
36 require_once('config.php');
37 require_once('lib/filelib.php');
39 $relativepath = get_file_argument();
40 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
42 // relative path must start with '/', because of backup/restore!!!
43 if (!$relativepath) {
44 print_error('invalidargorconf');
45 } else if ($relativepath[0] != '/') {
46 print_error('pathdoesnotstartslash');
49 // extract relative path components
50 $args = explode('/', ltrim($relativepath, '/'));
52 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
53 print_error('invalidarguments');
56 $courseid = (int)array_shift($args);
57 $relativepath = implode('/', $args);
59 // security: limit access to existing course subdirectories
60 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
62 if ($course->legacyfiles != 2) {
63 // course files disabled
64 send_file_not_found();
67 if ($course->id != SITEID) {
68 require_login($course, true, null, false);
70 } else if ($CFG->forcelogin) {
71 if (empty($CFG->sitepolicyhandler) and !empty($CFG->sitepolicy)
72 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
73 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
74 //do not require login for policy file
75 } else {
76 require_login(0, true, null, false);
80 $context = context_course::instance($course->id);
82 $fs = get_file_storage();
84 $fullpath = "/$context->id/course/legacy/0/$relativepath";
86 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
87 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
88 $fullpath .= '/';
90 // Try to fallback to the directory named as the supposed file.
91 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'.'))) {
92 send_file_not_found();
95 // do not serve dirs
96 if ($file->get_filename() == '.') {
97 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
98 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
99 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
100 send_file_not_found();
106 // ========================================
107 // finally send the file
108 // ========================================
109 \core\session\manager::write_close(); // Unlock session during file serving.
110 send_stored_file($file, null, $CFG->filteruploadedfiles, $forcedownload);