Skip to content
Newer
Older
100644 118 lines (96 sloc) 3.85 KB
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
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 /**
bf34822 @skodak MDL-22547 course->legacyfiles switch implemented
skodak authored May 22, 2010
19 * This script fetches legacy course files in dataroot directory, it is enabled
edfd6a5 @skodak MDL-14589 user icons now stored in file storage pool
skodak authored Jul 11, 2010
20 * only if course->legacyfiles == 2. DO not link to this file in new code.
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
21 *
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 *
d078f6d @skodak standardized PHPDocs package info
skodak authored Sep 6, 2010
27 * @package core
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
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
2e9b772 @skodak MDL-20693 new NO_DEBUG_DISPLAY define
skodak authored Oct 31, 2009
33 // disable moodle specific debug messages and any errors in output
34 define('NO_DEBUG_DISPLAY', true);
35
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
36 require_once('config.php');
37 require_once('lib/filelib.php');
38
39 if (!isset($CFG->filelifetime)) {
40 $lifetime = 86400; // Seconds for files to remain in caches
41 } else {
42 $lifetime = $CFG->filelifetime;
43 }
44
45 $relativepath = get_file_argument();
46 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
47
48 // relative path must start with '/', because of backup/restore!!!
49 if (!$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
58 if (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);
64f9379 @skodak MDL-22950 adding new component column to the files table, unfortunate…
skodak authored Jul 3, 2010
63 $relativepath = implode('/', $args);
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
64
65 // security: limit access to existing course subdirectories
bf34822 @skodak MDL-22547 course->legacyfiles switch implemented
skodak authored May 22, 2010
66 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
67
57ebd15 @skodak MDL-23308 reverting previously reverted deprecation of legacy course …
skodak authored Sep 3, 2010
68 if ($course->legacyfiles != 2) {
69 // course files disabled
70 send_file_not_found();
71 }
72
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
73 if ($course->id != SITEID) {
74 require_login($course->id, true, null, false);
75
76 } else if ($CFG->forcelogin) {
77 if (!empty($CFG->sitepolicy)
64f9379 @skodak MDL-22950 adding new component column to the files table, unfortunate…
skodak authored Jul 3, 2010
78 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
79 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
80 //do not require login for policy file
6ed3da1 Cleaned up file argument processing a bit
moodler authored Jan 12, 2003
81 } else {
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
82 require_login(0, true, null, false);
2464c59 removed moodle specific debug messages from file.php
skodak authored Sep 21, 2006
83 }
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
84 }
2464c59 removed moodle specific debug messages from file.php
skodak authored Sep 21, 2006
85
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
86 $context = get_context_instance(CONTEXT_COURSE, $course->id);
3f8247c Changes to use a new configuration variable: CFG->slasharguments
martin authored Aug 26, 2002
87
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
88 $fs = get_file_storage();
172dd12 MDL-14589 initial file storage implementation, temporary file manager…
skodak authored Jul 31, 2008
89
64f9379 @skodak MDL-22950 adding new component column to the files table, unfortunate…
skodak authored Jul 3, 2010
90 $fullpath = "/$context->id/course/legacy/0/$relativepath";
a455733 Some security fixes with directory listing
moodler authored Mar 16, 2004
91
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
92 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
93 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
94 $fullpath .= '/';
7611242 Merged from stable
moodler authored Sep 7, 2004
95 }
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
96 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
97 send_file_not_found();
fd05dff SC#98 protection of uploaded files in resources, please review and test
skodak authored Feb 22, 2005
98 }
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
99 }
100 // do not serve dirs
101 if ($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();
172dd12 MDL-14589 initial file storage implementation, temporary file manager…
skodak authored Jul 31, 2008
106 }
107 }
e7f927a new handling of uploaded files, please TEST, TEST, TEST
skodak authored Dec 14, 2004
108 }
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
109 }
e5890e7 file.php now calculates content-length correctly for filtered text an…
moodler authored Apr 26, 2004
110
8265a51 MDL-18111 improving file api comments and docs, fixing license header
skodak authored May 21, 2009
111 // ========================================
112 // finally send the file
113 // ========================================
114 session_get_instance()->write_close(); // unlock session during fileserving
115 send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
e7f927a new handling of uploaded files, please TEST, TEST, TEST
skodak authored Dec 14, 2004
116
172dd12 MDL-14589 initial file storage implementation, temporary file manager…
skodak authored Jul 31, 2008
117
Something went wrong with that request. Please try again.