Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * X-Sendfile support
19
 *
20
 * @package   core
21
 * @copyright 2012 Petr Skoda {@link http://skodak.org}
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Serve files using the X-Sendfile header.
27
 *
28
 * This needs special server module or configuration.
29
 * Please make sure that all headers are already sent and the all access control checks passed.
30
 *
31
 * @param string $filepath
32
 * @return bool success
33
 */
34
function xsendfile($filepath) {
35
    global $CFG;
36
 
37
    if (empty($CFG->xsendfile)) {
38
        return false;
39
    }
40
 
41
    if (!file_exists($filepath)) {
42
        return false;
43
    }
44
 
45
    if (headers_sent()) {
46
        return false;
47
    }
48
 
49
    $filepath = realpath($filepath);
50
 
51
    $localrequestdir = realpath($CFG->localrequestdir);
52
    if (str_contains($filepath, $localrequestdir)) {
53
        // Do not serve files from local request directory using xsendfile.
54
        // They are likely to be removed before xsendfile can serve them.
55
        return false;
56
    }
57
 
58
    $aliased = false;
59
    if (!empty($CFG->xsendfilealiases) && is_array($CFG->xsendfilealiases)) {
60
        foreach ($CFG->xsendfilealiases as $alias => $dir) {
61
            $dir = realpath($dir);
62
            if ($dir === false) {
63
                continue;
64
            }
65
            if (substr($dir, -1) !== DIRECTORY_SEPARATOR) {
66
                // Add trailing dir separator.
67
                $dir .= DIRECTORY_SEPARATOR;
68
            }
69
            if (str_starts_with($filepath, $dir)) {
70
                $filepath = $alias . substr($filepath, strlen($dir));
71
                $aliased = true;
72
                break;
73
            }
74
        }
75
    }
76
 
77
    if ($CFG->xsendfile === 'X-LIGHTTPD-send-file') {
78
        // Version 1.4.40 and earlier do not support byte serving.
79
        // See http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file for more information.
80
        header('Accept-Ranges: none');
81
    } else if ($CFG->xsendfile === 'X-Accel-Redirect') {
82
        // Nginx requires paths relative to aliases, you need to specify them in config.php
83
        // See http://wiki.nginx.org/XSendfile for more information.
84
        if (!$aliased) {
85
            return false;
86
        }
87
    }
88
 
89
    header("$CFG->xsendfile: $filepath");
90
 
91
    return true;
92
}