MDL-57789 files: Let send_stored_file() rely on send_file().

This commit is contained in:
Matteo Scaramuccia
2017-02-25 23:36:04 +01:00
parent 0f59b6dd75
commit 7338ce0182
+15 -90
View File
@@ -2139,11 +2139,14 @@ function file_safe_save_content($content, $destination) {
* byteranges etc.
*
* @category files
* @param string $path Path of file on disk (including real filename), or actual content of file as string
* @param string|stored_file $path Path of file on disk (including real filename),
* or actual content of file as string,
* or stored_file object
* @param string $filename Filename to send
* @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $pathisstring If true (default false), $path is the content to send and not the pathname
* @param bool $pathisstring If true (default false), $path is the content to send and not the pathname.
* Forced to false when $path is a stored_file object.
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename
* @param bool $dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
@@ -2166,6 +2169,10 @@ function send_file($path, $filename, $lifetime = null , $filter=0, $pathisstring
$lifetime = $CFG->filelifetime;
}
if (is_object($path)) {
$pathisstring = false;
}
\core\session\manager::write_close(); // Unlock session during file serving.
// Use given MIME type if specified, otherwise guess it.
@@ -2231,8 +2238,8 @@ function send_file($path, $filename, $lifetime = null , $filter=0, $pathisstring
$options = new stdClass();
$options->noclean = true;
$options->nocache = true; // temporary workaround for MDL-5136
$text = $pathisstring ? $path : implode('', file($path));
$text = is_object($path) ? $path->get_content() :
$pathisstring ? $path : implode('', file($path));
$output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
readstring_accel($output, $mimetype, false);
@@ -2242,7 +2249,8 @@ function send_file($path, $filename, $lifetime = null , $filter=0, $pathisstring
$options = new stdClass();
$options->newlines = false;
$options->noclean = true;
$text = htmlentities($pathisstring ? $path : implode('', file($path)), ENT_QUOTES, 'UTF-8');
$text = is_object($path) ? $path->get_content() :
htmlentities($pathisstring ? $path : implode('', file($path)), ENT_QUOTES, 'UTF-8');
$output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
readstring_accel($output, $mimetype, false);
@@ -2343,101 +2351,18 @@ function send_stored_file($stored_file, $lifetime=null, $filter=0, $forcedownloa
die;
}
if ($dontdie) {
ignore_user_abort(true);
}
\core\session\manager::write_close(); // Unlock session during file serving.
$filename = is_null($filename) ? $stored_file->get_filename() : $filename;
$filename = is_null($filename) ? $stored_file->get_filename() : $filename;
// Use given MIME type if specified.
$mimetype = $stored_file->get_mimetype();
// Otherwise guess it.
if (!$mimetype || $mimetype === 'document/unknown') {
$mimetype = get_mimetype_for_sending($filename);
}
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (core_useragent::is_ie()) {
$filename = rawurlencode($filename);
}
if ($forcedownload) {
header('Content-Disposition: attachment; filename="'.$filename.'"');
} else if ($mimetype !== 'application/x-shockwave-flash') {
// If this is an swf don't pass content-disposition with filename as this makes the flash player treat the file
// as an upload and enforces security that may prevent the file from being loaded.
header('Content-Disposition: inline; filename="'.$filename.'"');
}
if ($lifetime > 0) {
$cacheability = ' public,';
if (!empty($options['cacheability']) && ($options['cacheability'] === 'public')) {
// This file must be cache-able by both browsers and proxies.
$cacheability = ' public,';
} else if (!empty($options['cacheability']) && ($options['cacheability'] === 'private')) {
// This file must be cache-able only by browsers.
$cacheability = ' private,';
} else if (isloggedin() and !isguestuser()) {
$cacheability = ' private,';
}
header('Cache-Control:'.$cacheability.' max-age='.$lifetime.', no-transform');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
} else { // Do not cache files in proxies and browsers
if (is_https()) { // HTTPS sites - watch out for IE! KB812935 and KB316431.
header('Cache-Control: private, max-age=10, no-transform');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0, no-transform');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
}
// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
if (WS_SERVER) {
header('Access-Control-Allow-Origin: *');
}
if (empty($filter)) {
// send the contents
readfile_accel($stored_file, $mimetype, !$dontdie);
} else { // Try to put the file through filters
if ($mimetype == 'text/html' || $mimetype == 'application/xhtml+xml') {
$options = new stdClass();
$options->noclean = true;
$options->nocache = true; // temporary workaround for MDL-5136
$text = $stored_file->get_content();
$output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
readstring_accel($output, $mimetype, false);
} else if (($mimetype == 'text/plain') and ($filter == 1)) {
// only filter text if filter all files is selected
$options = new stdClass();
$options->newlines = false;
$options->noclean = true;
$text = $stored_file->get_content();
$output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
readstring_accel($output, $mimetype, false);
} else { // Just send it out raw
readfile_accel($stored_file, $mimetype, !$dontdie);
}
}
if ($dontdie) {
return;
}
die; //no more chars to output!!!
send_file($stored_file, $filename, $lifetime, $filter, false, $forcedownload, $mimetype, $dontdie, $options);
}
/**