diff --git a/config-dist.php b/config-dist.php index 9684ced01d9..4b3534fa32b 100644 --- a/config-dist.php +++ b/config-dist.php @@ -225,6 +225,16 @@ $CFG->admin = 'admin'; // $CFG->xsendfile = 'X-Sendfile'; // Apache {@see https://tn123.org/mod_xsendfile/} // $CFG->xsendfile = 'X-LIGHTTPD-send-file'; // Lighttpd {@see http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file} // $CFG->xsendfile = 'X-Accel-Redirect'; // Nginx {@see http://wiki.nginx.org/XSendfile} +// If your X-Sendfile implementation (usually Nginx) uses directory aliases specify them +// in the following array setting: +// $CFG->xsendfilealiases = array( +// '/dataroot/' => $CFG->dataroot, +// '/cachedir/' => '/var/www/moodle/cache', // for custom $CFG->cachedir locations +// '/tempdir/' => '/var/www/moodle/temp', // for custom $CFG->tempdir locations +// '/filedir' => '/var/www/moodle/filedir', // for custom $CFG->filedir locations +// ); +// +// // // This setting will prevent the 'My Courses' page being displayed when a student // logs in. The site front page will always show the same (logged-out) view. diff --git a/lib/xsendfilelib.php b/lib/xsendfilelib.php index 784facb8ca7..39106f7324d 100644 --- a/lib/xsendfilelib.php +++ b/lib/xsendfilelib.php @@ -47,19 +47,30 @@ function xsendfile($filepath) { return false; } + $filepath = realpath($filepath); + + $aliased = false; + if (!empty($CFG->xsendfilealiases) and is_array($CFG->xsendfilealiases)) { + foreach ($CFG->xsendfilealiases as $alias=>$dir) { + $dir = realpath($dir).PATH_SEPARATOR; + if (strpos($filepath, $dir) === 0) { + $filepath = $alias.substr($filepath, strlen($dir)); + $aliased = true; + break; + } + } + } + if ($CFG->xsendfile === 'X-LIGHTTPD-send-file') { // http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file says 1.4 it does not support byteserving header('Accept-Ranges: none'); } else if ($CFG->xsendfile === 'X-Accel-Redirect') { // http://wiki.nginx.org/XSendfile - // Nginx is using relative path to protected folder, please note you can not use cache dir, tempdir and file pool outside of dataroot! - $filepath = realpath($filepath); - $dataroot = realpath($CFG->dataroot); - if (strpos($filepath, $dataroot) !== 0) { + // Nginx requires paths relative to aliases, you need to specify them in config.php + if (!$aliased) { return false; } - $filepath = substr($filepath, strlen($dataroot)); } header("$CFG->xsendfile: $filepath");