Don't display multiple language links to the same language
[mediawiki.git] / includes / StreamFile.php
blob95c69a20b67240473052c9722357175ae9985685
1 <?php
2 /**
3 * Functions related to the output of file content.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Functions related to the output of file content
26 class StreamFile {
27 const READY_STREAM = 1;
28 const NOT_MODIFIED = 2;
30 /**
31 * Stream a file to the browser, adding all the headings and fun stuff.
32 * Headers sent include: Content-type, Content-Length, Last-Modified,
33 * and Content-Disposition.
35 * @param $fname string Full name and path of the file to stream
36 * @param $headers array Any additional headers to send
37 * @param $sendErrors bool Send error messages if errors occur (like 404)
38 * @return bool Success
40 public static function stream( $fname, $headers = array(), $sendErrors = true ) {
41 wfProfileIn( __METHOD__ );
43 if ( FileBackend::isStoragePath( $fname ) ) { // sanity
44 throw new MWException( __FUNCTION__ . " given storage path '$fname'." );
47 wfSuppressWarnings();
48 $stat = stat( $fname );
49 wfRestoreWarnings();
51 $res = self::prepareForStream( $fname, $stat, $headers, $sendErrors );
52 if ( $res == self::NOT_MODIFIED ) {
53 $ok = true; // use client cache
54 } elseif ( $res == self::READY_STREAM ) {
55 wfProfileIn( __METHOD__ . '-send' );
56 $ok = readfile( $fname );
57 wfProfileOut( __METHOD__ . '-send' );
58 } else {
59 $ok = false; // failed
62 wfProfileOut( __METHOD__ );
63 return $ok;
66 /**
67 * Call this function used in preparation before streaming a file.
68 * This function does the following:
69 * (a) sends Last-Modified, Content-type, and Content-Disposition headers
70 * (b) cancels any PHP output buffering and automatic gzipping of output
71 * (c) sends Content-Length header based on HTTP_IF_MODIFIED_SINCE check
73 * @param $path string Storage path or file system path
74 * @param $info Array|bool File stat info with 'mtime' and 'size' fields
75 * @param $headers Array Additional headers to send
76 * @param $sendErrors bool Send error messages if errors occur (like 404)
77 * @return int|bool READY_STREAM, NOT_MODIFIED, or false on failure
79 public static function prepareForStream(
80 $path, $info, $headers = array(), $sendErrors = true
81 ) {
82 if ( !is_array( $info ) ) {
83 if ( $sendErrors ) {
84 header( 'HTTP/1.0 404 Not Found' );
85 header( 'Cache-Control: no-cache' );
86 header( 'Content-Type: text/html; charset=utf-8' );
87 $encFile = htmlspecialchars( $path );
88 $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
89 echo "<html><body>
90 <h1>File not found</h1>
91 <p>Although this PHP script ($encScript) exists, the file requested for output
92 ($encFile) does not.</p>
93 </body></html>
96 return false;
99 // Sent Last-Modified HTTP header for client-side caching
100 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) );
102 // Cancel output buffering and gzipping if set
103 wfResetOutputBuffers();
105 $type = self::contentTypeFromPath( $path );
106 if ( $type && $type != 'unknown/unknown' ) {
107 header( "Content-type: $type" );
108 } else {
109 // Send a content type which is not known to Internet Explorer, to
110 // avoid triggering IE's content type detection. Sending a standard
111 // unknown content type here essentially gives IE license to apply
112 // whatever content type it likes.
113 header( 'Content-type: application/x-wiki' );
116 // Don't stream it out as text/html if there was a PHP error
117 if ( headers_sent() ) {
118 echo "Headers already sent, terminating.\n";
119 return false;
122 // Send additional headers
123 foreach ( $headers as $header ) {
124 header( $header );
127 // Don't send if client has up to date cache
128 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
129 $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
130 if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) {
131 ini_set( 'zlib.output_compression', 0 );
132 header( "HTTP/1.0 304 Not Modified" );
133 return self::NOT_MODIFIED; // ok
137 header( 'Content-Length: ' . $info['size'] );
139 return self::READY_STREAM; // ok
143 * Determine the file type of a file based on the path
145 * @param $filename string Storage path or file system path
146 * @param $safe bool Whether to do retroactive upload blacklist checks
147 * @return null|string
149 public static function contentTypeFromPath( $filename, $safe = true ) {
150 global $wgTrivialMimeDetection;
152 $ext = strrchr( $filename, '.' );
153 $ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) );
155 # trivial detection by file extension,
156 # used for thumbnails (thumb.php)
157 if ( $wgTrivialMimeDetection ) {
158 switch ( $ext ) {
159 case 'gif': return 'image/gif';
160 case 'png': return 'image/png';
161 case 'jpg': return 'image/jpeg';
162 case 'jpeg': return 'image/jpeg';
165 return 'unknown/unknown';
168 $magic = MimeMagic::singleton();
169 // Use the extension only, rather than magic numbers, to avoid opening
170 // up vulnerabilities due to uploads of files with allowed extensions
171 // but disallowed types.
172 $type = $magic->guessTypesForExtension( $ext );
175 * Double-check some security settings that were done on upload but might
176 * have changed since.
178 if ( $safe ) {
179 global $wgFileBlacklist, $wgCheckFileExtensions, $wgStrictFileExtensions,
180 $wgFileExtensions, $wgVerifyMimeType, $wgMimeTypeBlacklist;
181 list( , $extList ) = UploadBase::splitExtensions( $filename );
182 if ( UploadBase::checkFileExtensionList( $extList, $wgFileBlacklist ) ) {
183 return 'unknown/unknown';
185 if ( $wgCheckFileExtensions && $wgStrictFileExtensions
186 && !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions ) )
188 return 'unknown/unknown';
190 if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeBlacklist ) ) {
191 return 'unknown/unknown';
194 return $type;