file.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This script fetches legacy course files in dataroot directory, it is enabled
  18. * only if course->legacyfiles == 2. DO not link to this file in new code.
  19. *
  20. * Syntax: file.php/courseid/dir/dir/dir/filename.ext
  21. * file.php/courseid/dir/dir/dir/filename.ext?forcedownload=1 (download instead of inline)
  22. * file.php/courseid/dir (returns index.html from dir)
  23. * Workaround: file.php?file=/courseid/dir/dir/dir/filename.ext
  24. *
  25. * @package core
  26. * @subpackage file
  27. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. // disable moodle specific debug messages and any errors in output
  31. define('NO_DEBUG_DISPLAY', true);
  32. require_once('config.php');
  33. require_once('lib/filelib.php');
  34. $relativepath = get_file_argument();
  35. $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
  36. // relative path must start with '/', because of backup/restore!!!
  37. if (!$relativepath) {
  38. print_error('invalidargorconf');
  39. } else if ($relativepath{0} != '/') {
  40. print_error('pathdoesnotstartslash');
  41. }
  42. // extract relative path components
  43. $args = explode('/', ltrim($relativepath, '/'));
  44. if (count($args) == 0) { // always at least courseid, may search for index.html in course root
  45. print_error('invalidarguments');
  46. }
  47. $courseid = (int)array_shift($args);
  48. $relativepath = implode('/', $args);
  49. // security: limit access to existing course subdirectories
  50. $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  51. if ($course->legacyfiles != 2) {
  52. // course files disabled
  53. send_file_not_found();
  54. }
  55. if ($course->id != SITEID) {
  56. require_login($course, true, null, false);
  57. } else if ($CFG->forcelogin) {
  58. if (!empty($CFG->sitepolicy)
  59. and ($CFG->sitepolicy == $CFG->wwwroot.'/file.php/'.$relativepath
  60. or $CFG->sitepolicy == $CFG->wwwroot.'/file.php?file=/'.$relativepath)) {
  61. //do not require login for policy file
  62. } else {
  63. require_login(0, true, null, false);
  64. }
  65. }
  66. $context = context_course::instance($course->id);
  67. $fs = get_file_storage();
  68. $fullpath = "/$context->id/course/legacy/0/$relativepath";
  69. if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
  70. if (strrpos($fullpath, '/') !== strlen($fullpath) -1 ) {
  71. $fullpath .= '/';
  72. }
  73. // Try to fallback to the directory named as the supposed file.
  74. if (!$file = $fs->get_file_by_hash(sha1($fullpath.'.'))) {
  75. send_file_not_found();
  76. }
  77. }
  78. // do not serve dirs
  79. if ($file->get_filename() == '.') {
  80. if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.html'))) {
  81. if (!$file = $fs->get_file_by_hash(sha1($fullpath.'index.htm'))) {
  82. if (!$file = $fs->get_file_by_hash(sha1($fullpath.'Default.htm'))) {
  83. send_file_not_found();
  84. }
  85. }
  86. }
  87. }
  88. // ========================================
  89. // finally send the file
  90. // ========================================
  91. \core\session\manager::write_close(); // Unlock session during file serving.
  92. send_stored_file($file, null, $CFG->filteruploadedfiles, $forcedownload);