MDL-20296 some parameters were undefined
[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/**
19 * This script fetches legacy files from the course files in dataroot directory
20 *
21 * You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
22 * This ensures proper formatting and offers useful options.
23 * Syntax: file.php/courseid/dir/dir/dir/filename.ext
24 * file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
25 * file.php/courseid/dir (returns index.html from dir)
26 * Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
27 *
28 * @package moodlecore
29 * @subpackage file
30 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33
2e9b772f
PS
34// disable moodle specific debug messages and any errors in output
35define('NO_DEBUG_DISPLAY', true);
36
8265a51d 37require_once('config.php');
38require_once('lib/filelib.php');
39
40if (!isset($CFG->filelifetime)) {
41 $lifetime = 86400; // Seconds for files to remain in caches
42} else {
43 $lifetime = $CFG->filelifetime;
44}
45
8265a51d 46$relativepath = get_file_argument();
47$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
48
49// relative path must start with '/', because of backup/restore!!!
50if (!$relativepath) {
51 print_error('invalidargorconf');
52} else if ($relativepath{0} != '/') {
53 print_error('pathdoesnotstartslash');
54}
55
56// extract relative path components
57$args = explode('/', ltrim($relativepath, '/'));
58
59if (count($args) == 0) { // always at least courseid, may search for index.html in course root
60 print_error('invalidarguments');
61}
62
63$courseid = (int)array_shift($args);
64$relativepath = '/'.implode('/', $args);
65
66// security: limit access to existing course subdirectories
67if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
68 print_error('invalidcourseid');
69}
70
71if ($course->id != SITEID) {
72 require_login($course->id, true, null, false);
73
74} else if ($CFG->forcelogin) {
75 if (!empty($CFG->sitepolicy)
76 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath
77 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) {
78 //do not require login for policy file
6ed3da1d 79 } else {
8265a51d 80 require_login(0, true, null, false);
2464c592 81 }
8265a51d 82}
2464c592 83
8265a51d 84$context = get_context_instance(CONTEXT_COURSE, $course->id);
3f8247c2 85
8265a51d 86$fs = get_file_storage();
172dd12c 87
8265a51d 88$fullpath = $context->id.'course_content0'.$relativepath;
a4557331 89
8265a51d 90if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
91 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
92 $fullpath .= '/';
76112421 93 }
8265a51d 94 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
95 send_file_not_found();
fd05dffe 96 }
8265a51d 97}
98// do not serve dirs
99if ($file->get_filename() == '.') {
100 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
101 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
102 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
103 send_file_not_found();
172dd12c 104 }
105 }
e7f927a0 106 }
8265a51d 107}
e5890e71 108
8265a51d 109// ========================================
110// finally send the file
111// ========================================
112session_get_instance()->write_close(); // unlock session during fileserving
113send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
e7f927a0 114
172dd12c 115