MDL-21695 adding help and link strings
[moodle.git] / file.php
blobddbdfd24226b2a66150642b2f3352795bd7ebd89
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 files from the course files in dataroot directory
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
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
34 // disable moodle specific debug messages and any errors in output
35 define('NO_DEBUG_DISPLAY', true);
37 require_once('config.php');
38 require_once('lib/filelib.php');
40 if (!isset($CFG->filelifetime)) {
41 $lifetime = 86400; // Seconds for files to remain in caches
42 } else {
43 $lifetime = $CFG->filelifetime;
46 $relativepath = get_file_argument();
47 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
49 // relative path must start with '/', because of backup/restore!!!
50 if (!$relativepath) {
51 print_error('invalidargorconf');
52 } else if ($relativepath{0} != '/') {
53 print_error('pathdoesnotstartslash');
56 // extract relative path components
57 $args = explode('/', ltrim($relativepath, '/'));
59 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
60 print_error('invalidarguments');
63 $courseid = (int)array_shift($args);
64 $relativepath = '/'.implode('/', $args);
66 // security: limit access to existing course subdirectories
67 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
68 print_error('invalidcourseid');
71 if ($course->id != SITEID) {
72 require_login($course->id, true, null, false);
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
79 } else {
80 require_login(0, true, null, false);
84 $context = get_context_instance(CONTEXT_COURSE, $course->id);
86 $fs = get_file_storage();
88 $fullpath = $context->id.'course_content0'.$relativepath;
90 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
91 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
92 $fullpath .= '/';
94 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
95 send_file_not_found();
98 // do not serve dirs
99 if ($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();
109 // ========================================
110 // finally send the file
111 // ========================================
112 session_get_instance()->write_close(); // unlock session during fileserving
113 send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);