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