Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/file.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00033 // disable moodle specific debug messages and any errors in output
00034 define('NO_DEBUG_DISPLAY', true);
00035 
00036 require_once('config.php');
00037 require_once('lib/filelib.php');
00038 
00039 if (!isset($CFG->filelifetime)) {
00040     $lifetime = 86400;     // Seconds for files to remain in caches
00041 } else {
00042     $lifetime = $CFG->filelifetime;
00043 }
00044 
00045 $relativepath  = get_file_argument();
00046 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
00047 
00048 // relative path must start with '/', because of backup/restore!!!
00049 if (!$relativepath) {
00050     print_error('invalidargorconf');
00051 } else if ($relativepath{0} != '/') {
00052     print_error('pathdoesnotstartslash');
00053 }
00054 
00055 // extract relative path components
00056 $args = explode('/', ltrim($relativepath, '/'));
00057 
00058 if (count($args) == 0) { // always at least courseid, may search for index.html in course root
00059     print_error('invalidarguments');
00060 }
00061 
00062 $courseid = (int)array_shift($args);
00063 $relativepath = implode('/', $args);
00064 
00065 // security: limit access to existing course subdirectories
00066 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
00067 
00068 if ($course->legacyfiles != 2) {
00069     // course files disabled
00070     send_file_not_found();
00071 }
00072 
00073 if ($course->id != SITEID) {
00074     require_login($course->id, true, null, false);
00075 
00076 } else if ($CFG->forcelogin) {
00077     if (!empty($CFG->sitepolicy)
00078         and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
00079              or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
00080         //do not require login for policy file
00081     } else {
00082         require_login(0, true, null, false);
00083     }
00084 }
00085 
00086 $context = get_context_instance(CONTEXT_COURSE, $course->id);
00087 
00088 $fs = get_file_storage();
00089 
00090 $fullpath = "/$context->id/course/legacy/0/$relativepath";
00091 
00092 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
00093     if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
00094         $fullpath .= '/';
00095     }
00096     if (!$file = $fs->get_file_by_hash(sha1($fullpath.'/.'))) {
00097         send_file_not_found();
00098     }
00099 }
00100 // do not serve dirs
00101 if ($file->get_filename() == '.') {
00102     if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
00103         if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
00104             if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
00105                 send_file_not_found();
00106             }
00107         }
00108     }
00109 }
00110 
00111 // ========================================
00112 // finally send the file
00113 // ========================================
00114 session_get_instance()->write_close(); // unlock session during fileserving
00115 send_stored_file($file, $lifetime, $CFG->filteruploadedfiles, $forcedownload);
00116 
00117 
 All Data Structures Namespaces Files Functions Variables Enumerations