MDL-18111 improving file api comments and docs
[moodle.git] / file.php
CommitLineData
e7f927a0 1<?php // $Id$
2 // This script fetches files from the dataroot directory
5a254a29 3 //
4 // You should use the get_file_url() function, available in lib/filelib.php, to link to file.php.
5 // This ensures proper formatting and offers useful options.
6 //
e7f927a0 7 // Syntax: file.php/courseid/dir/dir/dir/filename.ext
69faecce 8 // file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
e7f927a0 9 // file.php/courseid/dir (returns index.html from dir)
10 // Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
feaf5d06 11
822a1063 12 require_once('config.php');
4d68dff4 13 require_once('lib/filelib.php');
f9903ed0 14
f44762bd 15 if (!isset($CFG->filelifetime)) {
e7f927a0 16 $lifetime = 86400; // Seconds for files to remain in caches
6ed3da1d 17 } else {
e7f927a0 18 $lifetime = $CFG->filelifetime;
2464c592 19 }
20
7eb0b60a 21 // disable moodle specific debug messages
22 disable_debugging();
3f8247c2 23
11e7b506 24 $relativepath = get_file_argument();
69faecce 25 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
172dd12c 26
e7f927a0 27 // relative path must start with '/', because of backup/restore!!!
28 if (!$relativepath) {
33aa5723 29 print_error('invalidargorconf');
e7f927a0 30 } else if ($relativepath{0} != '/') {
33aa5723 31 print_error('pathdoesnotstartslash');
f9903ed0 32 }
33
e7f927a0 34 // extract relative path components
172dd12c 35 $args = explode('/', ltrim($relativepath, '/'));
36
e7f927a0 37 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
33aa5723 38 print_error('invalidarguments');
55b8ac31 39 }
a4557331 40
172dd12c 41 $courseid = (int)array_shift($args);
42 $relativepath = '/'.implode('/', $args);
43
44 // security: limit access to existing course subdirectories
45 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
33aa5723 46 print_error('invalidcourseid');
76112421 47 }
21ddaf60 48
172dd12c 49 if ($course->id != SITEID) {
f4013c10 50 require_login($course->id, true, null, false);
172dd12c 51
f57cd38b 52 } else if ($CFG->forcelogin) {
90a32fd5 53 if (!empty($CFG->sitepolicy)
54 and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php'.$relativepath
55 or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file='.$relativepath)) {
56 //do not require login for policy file
57 } else {
f4013c10 58 require_login(0, true, null, false);
90a32fd5 59 }
f9903ed0 60 }
61
172dd12c 62 $context = get_context_instance(CONTEXT_COURSE, $course->id);
f9903ed0 63
172dd12c 64 $fs = get_file_storage();
fd05dffe 65
172dd12c 66 $fullpath = $context->id.'course_content0'.$relativepath;
fd05dffe 67
172dd12c 68 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
69 if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
70 $fullpath .= '/';
71 }
72 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
9e5fa330 73 send_file_not_found();
fd05dffe 74 }
75 }
172dd12c 76 // do not serve dirs
77 if ($file->get_filename() == '.') {
78 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
79 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
80 if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
9e5fa330 81 send_file_not_found();
172dd12c 82 }
83 }
84 }
e7f927a0 85 }
e5890e71 86
e7f927a0 87 // ========================================
88 // finally send the file
89 // ========================================
56949c17 90 session_get_instance()->write_close(); // unlock session during fileserving
172dd12c 91 send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
e7f927a0 92
172dd12c 93