2005-04-16 04:33:34 +00:00
|
|
|
<?php
|
2010-08-14 17:42:40 +00:00
|
|
|
/**
|
|
|
|
|
* Functions related to the output of file content
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2011-08-13 19:03:51 +00:00
|
|
|
class StreamFile {
|
2012-01-08 08:40:00 +00:00
|
|
|
const READY_STREAM = 1;
|
|
|
|
|
const NOT_MODIFIED = 2;
|
|
|
|
|
|
2011-08-13 19:03:51 +00:00
|
|
|
/**
|
2012-01-05 01:33:58 +00:00
|
|
|
* Stream a file to the browser, adding all the headings and fun stuff.
|
|
|
|
|
* Headers sent include: Content-type, Content-Length, Last-Modified,
|
|
|
|
|
* and Content-Disposition.
|
|
|
|
|
*
|
2011-08-13 19:03:51 +00:00
|
|
|
* @param $fname string Full name and path of the file to stream
|
|
|
|
|
* @param $headers array Any additional headers to send
|
2011-11-20 10:24:04 +00:00
|
|
|
* @param $sendErrors bool Send error messages if errors occur (like 404)
|
|
|
|
|
* @return bool Success
|
2011-08-13 19:03:51 +00:00
|
|
|
*/
|
2011-11-20 10:24:04 +00:00
|
|
|
public static function stream( $fname, $headers = array(), $sendErrors = true ) {
|
2011-08-13 19:03:51 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$stat = stat( $fname );
|
|
|
|
|
wfRestoreWarnings();
|
2012-01-08 08:40:00 +00:00
|
|
|
|
|
|
|
|
$res = self::prepareForStream( $fname, $stat, $headers, $sendErrors );
|
|
|
|
|
if ( $res == self::NOT_MODIFIED ) {
|
|
|
|
|
return true; // use client cache
|
|
|
|
|
} elseif ( $res == self::READY_STREAM ) {
|
|
|
|
|
return readfile( $fname );
|
|
|
|
|
} else {
|
|
|
|
|
return false; // failed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call this function used in preparation before streaming a file.
|
|
|
|
|
* This function does the following:
|
|
|
|
|
* (a) sends Last-Modified, Content-type, and Content-Disposition headers
|
|
|
|
|
* (b) cancels any PHP output buffering and automatic gzipping of output
|
|
|
|
|
* (c) sends Content-Length header based on HTTP_IF_MODIFIED_SINCE check
|
|
|
|
|
*
|
|
|
|
|
* @param $path string Storage path or file system path
|
2012-01-17 05:34:01 +00:00
|
|
|
* @param $info Array|false File stat info with 'mtime' and 'size' fields
|
2012-01-08 08:40:00 +00:00
|
|
|
* @param $headers Array Additional headers to send
|
|
|
|
|
* @param $sendErrors bool Send error messages if errors occur (like 404)
|
|
|
|
|
* @return int|false READY_STREAM, NOT_MODIFIED, or false on failure
|
|
|
|
|
*/
|
|
|
|
|
public static function prepareForStream(
|
2012-01-17 05:34:01 +00:00
|
|
|
$path, $info, $headers = array(), $sendErrors = true
|
2012-01-08 08:40:00 +00:00
|
|
|
) {
|
|
|
|
|
global $wgLanguageCode;
|
|
|
|
|
|
2012-01-17 05:34:01 +00:00
|
|
|
if ( !is_array( $info ) ) {
|
2011-11-20 10:24:04 +00:00
|
|
|
if ( $sendErrors ) {
|
|
|
|
|
header( 'HTTP/1.0 404 Not Found' );
|
|
|
|
|
header( 'Cache-Control: no-cache' );
|
|
|
|
|
header( 'Content-Type: text/html; charset=utf-8' );
|
2012-01-08 08:40:00 +00:00
|
|
|
$encFile = htmlspecialchars( $path );
|
2011-11-20 10:24:04 +00:00
|
|
|
$encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
|
|
|
|
|
echo "<html><body>
|
|
|
|
|
<h1>File not found</h1>
|
|
|
|
|
<p>Although this PHP script ($encScript) exists, the file requested for output
|
|
|
|
|
($encFile) does not.</p>
|
|
|
|
|
</body></html>
|
|
|
|
|
";
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2011-08-13 19:03:51 +00:00
|
|
|
}
|
2005-04-16 04:33:34 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
// Sent Last-Modified HTTP header for client-side caching
|
|
|
|
|
header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) );
|
2008-11-18 05:07:54 +00:00
|
|
|
|
2011-08-13 19:03:51 +00:00
|
|
|
// Cancel output buffering and gzipping if set
|
|
|
|
|
wfResetOutputBuffers();
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
$type = self::getType( $path );
|
2011-08-13 19:03:51 +00:00
|
|
|
if ( $type && $type != 'unknown/unknown' ) {
|
|
|
|
|
header( "Content-type: $type" );
|
|
|
|
|
} else {
|
|
|
|
|
header( 'Content-type: application/x-wiki' );
|
|
|
|
|
}
|
2007-11-03 02:38:40 +00:00
|
|
|
|
2011-08-13 19:03:51 +00:00
|
|
|
// Don't stream it out as text/html if there was a PHP error
|
|
|
|
|
if ( headers_sent() ) {
|
|
|
|
|
echo "Headers already sent, terminating.\n";
|
2011-11-20 10:24:04 +00:00
|
|
|
return false;
|
2005-04-16 05:48:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-11-20 10:24:04 +00:00
|
|
|
header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" .
|
2012-01-08 08:40:00 +00:00
|
|
|
urlencode( basename( $path ) ) );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-11-20 10:24:04 +00:00
|
|
|
// Send additional headers
|
2011-08-13 19:03:51 +00:00
|
|
|
foreach ( $headers as $header ) {
|
|
|
|
|
header( $header );
|
|
|
|
|
}
|
2008-11-18 05:07:54 +00:00
|
|
|
|
2011-11-20 10:24:04 +00:00
|
|
|
// Don't send if client has up to date cache
|
2011-08-13 19:03:51 +00:00
|
|
|
if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
|
|
|
|
|
$modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
|
2012-01-08 08:40:00 +00:00
|
|
|
if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) {
|
2011-08-13 19:03:51 +00:00
|
|
|
ini_set( 'zlib.output_compression', 0 );
|
|
|
|
|
header( "HTTP/1.0 304 Not Modified" );
|
2012-01-08 08:40:00 +00:00
|
|
|
return self::NOT_MODIFIED; // ok
|
2011-08-13 19:03:51 +00:00
|
|
|
}
|
2005-04-16 04:33:34 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
header( 'Content-Length: ' . $info['size'] );
|
2011-08-13 19:03:51 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
return self::READY_STREAM; // ok
|
2005-05-21 07:46:17 +00:00
|
|
|
}
|
2008-11-18 05:07:54 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-08-13 19:03:51 +00:00
|
|
|
* Determine the filetype we're dealing with
|
2012-01-08 08:40:00 +00:00
|
|
|
* @param $filename string Storage path or file system path
|
|
|
|
|
* @param $safe bool Whether to do retroactive upload blacklist checks
|
2011-08-13 19:03:51 +00:00
|
|
|
* @return null|string
|
2008-11-18 05:07:54 +00:00
|
|
|
*/
|
2012-01-08 08:40:00 +00:00
|
|
|
protected static function getType( $filename, $safe = true ) {
|
2011-08-13 19:03:51 +00:00
|
|
|
global $wgTrivialMimeDetection;
|
|
|
|
|
|
|
|
|
|
$ext = strrchr( $filename, '.' );
|
|
|
|
|
$ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) );
|
|
|
|
|
|
|
|
|
|
# trivial detection by file extension,
|
|
|
|
|
# used for thumbnails (thumb.php)
|
|
|
|
|
if ( $wgTrivialMimeDetection ) {
|
|
|
|
|
switch ( $ext ) {
|
|
|
|
|
case 'gif': return 'image/gif';
|
|
|
|
|
case 'png': return 'image/png';
|
|
|
|
|
case 'jpg': return 'image/jpeg';
|
|
|
|
|
case 'jpeg': return 'image/jpeg';
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-18 05:07:54 +00:00
|
|
|
return 'unknown/unknown';
|
|
|
|
|
}
|
2011-08-13 19:03:51 +00:00
|
|
|
|
|
|
|
|
$magic = MimeMagic::singleton();
|
|
|
|
|
// Use the extension only, rather than magic numbers, to avoid opening
|
|
|
|
|
// up vulnerabilities due to uploads of files with allowed extensions
|
|
|
|
|
// but disallowed types.
|
|
|
|
|
$type = $magic->guessTypesForExtension( $ext );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Double-check some security settings that were done on upload but might
|
|
|
|
|
* have changed since.
|
|
|
|
|
*/
|
|
|
|
|
if ( $safe ) {
|
|
|
|
|
global $wgFileBlacklist, $wgCheckFileExtensions, $wgStrictFileExtensions,
|
|
|
|
|
$wgFileExtensions, $wgVerifyMimeType, $wgMimeTypeBlacklist;
|
|
|
|
|
list( , $extList ) = UploadBase::splitExtensions( $filename );
|
|
|
|
|
if ( UploadBase::checkFileExtensionList( $extList, $wgFileBlacklist ) ) {
|
|
|
|
|
return 'unknown/unknown';
|
|
|
|
|
}
|
|
|
|
|
if ( $wgCheckFileExtensions && $wgStrictFileExtensions
|
|
|
|
|
&& !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions ) )
|
|
|
|
|
{
|
|
|
|
|
return 'unknown/unknown';
|
|
|
|
|
}
|
|
|
|
|
if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeBlacklist ) ) {
|
|
|
|
|
return 'unknown/unknown';
|
|
|
|
|
}
|
2008-11-18 05:07:54 +00:00
|
|
|
}
|
2011-08-13 19:03:51 +00:00
|
|
|
return $type;
|
2005-04-16 04:33:34 +00:00
|
|
|
}
|
|
|
|
|
}
|