2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 2012 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 //NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
28 * Serve file using X-Sendfile header, this needs special server module
29 * or configuration. Please make sure that all headers are already sent
30 * and the all access control checks passed.
32 * @param string $filepath
33 * @return bool success
35 function xsendfile($filepath) {
38 if (empty($CFG->xsendfile
)) {
42 if (!file_exists($filepath)) {
50 $filepath = realpath($filepath);
53 if (!empty($CFG->xsendfilealiases
) and is_array($CFG->xsendfilealiases
)) {
54 foreach ($CFG->xsendfilealiases
as $alias=>$dir) {
55 $dir = realpath($dir);
59 if (substr($dir, -1) !== DIRECTORY_SEPARATOR
) {
60 // add trailing dir separator
61 $dir .= DIRECTORY_SEPARATOR
;
63 if (strpos($filepath, $dir) === 0) {
64 $filepath = $alias.substr($filepath, strlen($dir));
71 if ($CFG->xsendfile
=== 'X-LIGHTTPD-send-file') {
72 // http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file says 1.4 it does not support byteserving
73 header('Accept-Ranges: none');
75 } else if ($CFG->xsendfile
=== 'X-Accel-Redirect') {
76 // http://wiki.nginx.org/XSendfile
77 // Nginx requires paths relative to aliases, you need to specify them in config.php
83 header("$CFG->xsendfile: $filepath");