X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/StreamFile.php diff --git a/includes/StreamFile.php b/includes/StreamFile.php index 5f460ee3..71113a86 100644 --- a/includes/StreamFile.php +++ b/includes/StreamFile.php @@ -1,113 +1,144 @@ -

File not found

-

Although this PHP script ($encScript) exists, the file requested for output -($encFile) does not.

- -"; - return; - } - - header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' ); +/** + * Functions related to the output of file content + */ +class StreamFile { + // Do not send any HTTP headers unless requested by caller (e.g. body only) + const STREAM_HEADLESS = HTTPFileStreamer::STREAM_HEADLESS; + // Do not try to tear down any PHP output buffers + const STREAM_ALLOW_OB = HTTPFileStreamer::STREAM_ALLOW_OB; - // Cancel output buffering and gzipping if set - wfResetOutputBuffers(); + /** + * 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. + * + * @param string $fname Full name and path of the file to stream + * @param array $headers Any additional headers to send if the file exists + * @param bool $sendErrors Send error messages if errors occur (like 404) + * @param array $optHeaders HTTP request header map (e.g. "range") (use lowercase keys) + * @param int $flags Bitfield of STREAM_* constants + * @throws MWException + * @return bool Success + */ + public static function stream( + $fname, $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0 + ) { + if ( FileBackend::isStoragePath( $fname ) ) { // sanity + throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." ); + } - $type = wfGetType( $fname ); - if ( $type and $type!="unknown/unknown") { - header("Content-type: $type"); - } else { - header('Content-type: application/x-wiki'); - } + $streamer = new HTTPFileStreamer( + $fname, + [ + 'obResetFunc' => 'wfResetOutputBuffers', + 'streamMimeFunc' => [ __CLASS__, 'contentTypeFromPath' ] + ] + ); - // Don't stream it out as text/html if there was a PHP error - if ( headers_sent() ) { - echo "Headers already sent, terminating.\n"; - return; + return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags ); } - global $wgLanguageCode; - header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . urlencode( basename( $fname ) ) ); - - foreach ( $headers as $header ) { - header( $header ); + /** + * Send out a standard 404 message for a file + * + * @param string $fname Full name and path of the file to stream + * @param int $flags Bitfield of STREAM_* constants + * @since 1.24 + */ + public static function send404Message( $fname, $flags = 0 ) { + HTTPFileStreamer::send404Message( $fname, $flags ); } - if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { - $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); - $sinceTime = strtotime( $modsince ); - if ( $stat['mtime'] <= $sinceTime ) { - ini_set('zlib.output_compression', 0); - header( "HTTP/1.0 304 Not Modified" ); - return; - } + /** + * Convert a Range header value to an absolute (start, end) range tuple + * + * @param string $range Range header value + * @param int $size File size + * @return array|string Returns error string on failure (start, end, length) + * @since 1.24 + */ + public static function parseRange( $range, $size ) { + return HTTPFileStreamer::parseRange( $range, $size ); } - header( 'Content-Length: ' . $stat['size'] ); - - readfile( $fname ); -} + /** + * Determine the file type of a file based on the path + * + * @param string $filename Storage path or file system path + * @param bool $safe Whether to do retroactive upload blacklist checks + * @return null|string + */ + public static function contentTypeFromPath( $filename, $safe = true ) { + global $wgTrivialMimeDetection; -/** */ -function wfGetType( $filename, $safe = true ) { - global $wgTrivialMimeDetection; + $ext = strrchr( $filename, '.' ); + $ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) ); - $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'; + } - # 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'; + return 'unknown/unknown'; } - return 'unknown/unknown'; - } - - $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 ); + $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'; + /** + * 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'; + } } + return $type; } - return $type; }