Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/ -> file.php (source)

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  /**
  19   * This script fetches legacy course files in dataroot directory, it is enabled
  20   * only if course->legacyfiles == 2. DO not link to this file in new code.
  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   *
  27   * @package    core
  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  
  33  // disable moodle specific debug messages and any errors in output
  34  define('NO_DEBUG_DISPLAY', true);
  35  
  36  require_once('config.php');
  37  require_once ('lib/filelib.php');
  38  
  39  $relativepath  = get_file_argument();
  40  $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
  41  
  42  // relative path must start with '/', because of backup/restore!!!
  43  if (!$relativepath) {
  44      print_error('invalidargorconf');
  45  } else if ($relativepath[0] != '/') {
  46      print_error('pathdoesnotstartslash');
  47  }
  48  
  49  // extract relative path components
  50  $args = explode('/', ltrim($relativepath, '/'));
  51  
  52  if (count($args) == 0) { // always at least courseid, may search for index.html in course root
  53      print_error('invalidarguments');
  54  }
  55  
  56  $courseid = (int)array_shift($args);
  57  $relativepath = implode('/', $args);
  58  
  59  // security: limit access to existing course subdirectories
  60  $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  61  
  62  if ($course->legacyfiles != 2) {
  63      // course files disabled
  64      send_file_not_found();
  65  }
  66  
  67  if ($course->id != SITEID) {
  68      require_login($course, true, null, false);
  69  
  70  } else if ($CFG->forcelogin) {
  71      if (empty($CFG->sitepolicyhandler) and !empty($CFG->sitepolicy)
  72          and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
  73               or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
  74          //do not require login for policy file
  75      } else {
  76          require_login(0, true, null, false);
  77      }
  78  }
  79  
  80  $context = context_course::instance($course->id);
  81  
  82  $fs = get_file_storage();
  83  
  84  $fullpath = "/$context->id/course/legacy/0/$relativepath";
  85  
  86  if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
  87      if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
  88          $fullpath .= '/';
  89      }
  90      // Try to fallback to the directory named as the supposed file.
  91      if (!$file = $fs->get_file_by_hash(sha1($fullpath.'.'))) {
  92          send_file_not_found();
  93      }
  94  }
  95  // do not serve dirs
  96  if ($file->get_filename() == '.') {
  97      if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
  98          if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
  99              if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
 100                  send_file_not_found();
 101              }
 102          }
 103      }
 104  }
 105  
 106  // ========================================
 107  // finally send the file
 108  // ========================================
 109  \core\session\manager::write_close(); // Unlock session during file serving.
 110  send_stored_file($file, null, $CFG->filteruploadedfiles, $forcedownload);
 111  
 112