MDL-14589 improved param name
[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
PS
19 * This script fetches legacy course files in dataroot directory, it is enabled
20 * only if course->legacyfiles == 2.
8265a51d 21 *
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
28 *
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
33 */
34
2e9b772f
PS
35// disable moodle specific debug messages and any errors in output
36define('NO_DEBUG_DISPLAY', true);
37
8265a51d 38require_once('config.php');
39require_once('lib/filelib.php');
40
41if (!isset($CFG->filelifetime)) {
42 $lifetime = 86400; // Seconds for files to remain in caches
43} else {
44 $lifetime = $CFG->filelifetime;
45}
46
8265a51d 47$relativepath = get_file_argument();
48$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
49
50// relative path must start with '/', because of backup/restore!!!
51if (!$relativepath) {
52 print_error('invalidargorconf');
53} else if ($relativepath{0} != '/') {
54 print_error('pathdoesnotstartslash');
55}
56
57// extract relative path components
58$args = explode('/', ltrim($relativepath, '/'));
59
60if (count($args) == 0) { // always at least courseid, may search for index.html in course root
61 print_error('invalidarguments');
62}
63
64$courseid = (int)array_shift($args);
64f93798 65$relativepath = implode('/', $args);
8265a51d 66
67// security: limit access to existing course subdirectories
bf34822b
PS
68$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
69
70if ($course->legacyfiles != 2) {
71 // course files disabled
72 send_file_not_found();
8265a51d 73}
74
75if ($course->id != SITEID) {
76 require_login($course->id, true, null, false);
77
78} else if ($CFG->forcelogin) {
79 if (!empty($CFG->sitepolicy)
64f93798
PS
80 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
81 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
8265a51d 82 //do not require login for policy file
6ed3da1d 83 } else {
8265a51d 84 require_login(0, true, null, false);
2464c592 85 }
8265a51d 86}
2464c592 87
8265a51d 88$context = get_context_instance(CONTEXT_COURSE, $course->id);
3f8247c2 89
8265a51d 90$fs = get_file_storage();
172dd12c 91
64f93798 92$fullpath = "/$context->id/course/legacy/0/$relativepath";
a4557331 93
8265a51d 94if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
95 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
96 $fullpath .= '/';
76112421 97 }
8265a51d 98 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
99 send_file_not_found();
fd05dffe 100 }
8265a51d 101}
102// do not serve dirs
103if ($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();
172dd12c 108 }
109 }
e7f927a0 110 }
8265a51d 111}
e5890e71 112
8265a51d 113// ========================================
114// finally send the file
115// ========================================
116session_get_instance()->write_close(); // unlock session during fileserving
117send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
e7f927a0 118
172dd12c 119