MDL-58518 block_myoverview: use user's midnight for sort by courses
[moodle.git] / file.php
CommitLineData
8265a51d 1<?php
2
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.
14//
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/>.
17
18/**
bf34822b 19 * This script fetches legacy course files in dataroot directory, it is enabled
edfd6a5e 20 * only if course->legacyfiles == 2. DO not link to this file in new code.
8265a51d 21 *
8265a51d 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
26 *
d078f6d3 27 * @package core
8265a51d 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
31 */
32
2e9b772f
PS
33// disable moodle specific debug messages and any errors in output
34define('NO_DEBUG_DISPLAY', true);
35
8265a51d 36require_once('config.php');
37require_once('lib/filelib.php');
38
8265a51d 39$relativepath = get_file_argument();
40$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
41
42// relative path must start with '/', because of backup/restore!!!
43if (!$relativepath) {
44 print_error('invalidargorconf');
45} else if ($relativepath{0} != '/') {
46 print_error('pathdoesnotstartslash');
47}
48
49// extract relative path components
50$args = explode('/', ltrim($relativepath, '/'));
51
52if (count($args) == 0) { // always at least courseid, may search for index.html in course root
53 print_error('invalidarguments');
54}
55
56$courseid = (int)array_shift($args);
64f93798 57$relativepath = implode('/', $args);
8265a51d 58
59// security: limit access to existing course subdirectories
74df2951 60$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
bf34822b 61
57ebd15e
PS
62if ($course->legacyfiles != 2) {
63 // course files disabled
64 send_file_not_found();
65}
66
8265a51d 67if ($course->id != SITEID) {
cdbea7ee 68 require_login($course, true, null, false);
8265a51d 69
70} else if ($CFG->forcelogin) {
71 if (!empty($CFG->sitepolicy)
64f93798
PS
72 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
73 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
8265a51d 74 //do not require login for policy file
6ed3da1d 75 } else {
8265a51d 76 require_login(0, true, null, false);
2464c592 77 }
8265a51d 78}
2464c592 79
bf0f06b1 80$context = context_course::instance($course->id);
3f8247c2 81
8265a51d 82$fs = get_file_storage();
172dd12c 83
64f93798 84$fullpath = "/$context->id/course/legacy/0/$relativepath";
a4557331 85
8265a51d 86if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
87 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
88 $fullpath .= '/';
76112421 89 }
6c6147bb
MS
90 // Try to fallback to the directory named as the supposed file.
91 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'.'))) {
8265a51d 92 send_file_not_found();
fd05dffe 93 }
8265a51d 94}
95// do not serve dirs
96if ($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();
172dd12c 101 }
102 }
e7f927a0 103 }
8265a51d 104}
e5890e71 105
8265a51d 106// ========================================
107// finally send the file
108// ========================================
d79d5ac2 109\core\session\manager::write_close(); // Unlock session during file serving.
0c431257 110send_stored_file($file, null, $CFG->filteruploadedfiles, $forcedownload);
e7f927a0 111
172dd12c 112