Made loadFromFileCache() always disable $wgOut regardless of whether compression...
[lhc/web/wiklou.git] / includes / StreamFile.php
1 <?php
2 /**
3 * Functions related to the output of file content
4 *
5 * @file
6 */
7 class StreamFile {
8 const READY_STREAM = 1;
9 const NOT_MODIFIED = 2;
10
11 /**
12 * Stream a file to the browser, adding all the headings and fun stuff.
13 * Headers sent include: Content-type, Content-Length, Last-Modified,
14 * and Content-Disposition.
15 *
16 * @param $fname string Full name and path of the file to stream
17 * @param $headers array Any additional headers to send
18 * @param $sendErrors bool Send error messages if errors occur (like 404)
19 * @return bool Success
20 */
21 public static function stream( $fname, $headers = array(), $sendErrors = true ) {
22 wfSuppressWarnings();
23 $stat = stat( $fname );
24 wfRestoreWarnings();
25
26 $res = self::prepareForStream( $fname, $stat, $headers, $sendErrors );
27 if ( $res == self::NOT_MODIFIED ) {
28 return true; // use client cache
29 } elseif ( $res == self::READY_STREAM ) {
30 return readfile( $fname );
31 } else {
32 return false; // failed
33 }
34 }
35
36 /**
37 * Call this function used in preparation before streaming a file.
38 * This function does the following:
39 * (a) sends Last-Modified, Content-type, and Content-Disposition headers
40 * (b) cancels any PHP output buffering and automatic gzipping of output
41 * (c) sends Content-Length header based on HTTP_IF_MODIFIED_SINCE check
42 *
43 * @param $path string Storage path or file system path
44 * @param $info Array|bool File stat info with 'mtime' and 'size' fields
45 * @param $headers Array Additional headers to send
46 * @param $sendErrors bool Send error messages if errors occur (like 404)
47 * @return int|bool READY_STREAM, NOT_MODIFIED, or false on failure
48 */
49 public static function prepareForStream(
50 $path, $info, $headers = array(), $sendErrors = true
51 ) {
52 global $wgLanguageCode;
53
54 if ( !is_array( $info ) ) {
55 if ( $sendErrors ) {
56 header( 'HTTP/1.0 404 Not Found' );
57 header( 'Cache-Control: no-cache' );
58 header( 'Content-Type: text/html; charset=utf-8' );
59 $encFile = htmlspecialchars( $path );
60 $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
61 echo "<html><body>
62 <h1>File not found</h1>
63 <p>Although this PHP script ($encScript) exists, the file requested for output
64 ($encFile) does not.</p>
65 </body></html>
66 ";
67 }
68 return false;
69 }
70
71 // Sent Last-Modified HTTP header for client-side caching
72 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) );
73
74 // Cancel output buffering and gzipping if set
75 wfResetOutputBuffers();
76
77 $type = self::contentTypeFromPath( $path );
78 if ( $type && $type != 'unknown/unknown' ) {
79 header( "Content-type: $type" );
80 } else {
81 // Send a content type which is not known to Internet Explorer, to
82 // avoid triggering IE's content type detection. Sending a standard
83 // unknown content type here essentially gives IE license to apply
84 // whatever content type it likes.
85 header( 'Content-type: application/x-wiki' );
86 }
87
88 // Don't stream it out as text/html if there was a PHP error
89 if ( headers_sent() ) {
90 echo "Headers already sent, terminating.\n";
91 return false;
92 }
93
94 header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" .
95 urlencode( basename( $path ) ) );
96
97 // Send additional headers
98 foreach ( $headers as $header ) {
99 header( $header );
100 }
101
102 // Don't send if client has up to date cache
103 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
104 $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
105 if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) {
106 ini_set( 'zlib.output_compression', 0 );
107 header( "HTTP/1.0 304 Not Modified" );
108 return self::NOT_MODIFIED; // ok
109 }
110 }
111
112 header( 'Content-Length: ' . $info['size'] );
113
114 return self::READY_STREAM; // ok
115 }
116
117 /**
118 * Determine the file type of a file based on the path
119 *
120 * @param $filename string Storage path or file system path
121 * @param $safe bool Whether to do retroactive upload blacklist checks
122 * @return null|string
123 */
124 public static function contentTypeFromPath( $filename, $safe = true ) {
125 global $wgTrivialMimeDetection;
126
127 $ext = strrchr( $filename, '.' );
128 $ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) );
129
130 # trivial detection by file extension,
131 # used for thumbnails (thumb.php)
132 if ( $wgTrivialMimeDetection ) {
133 switch ( $ext ) {
134 case 'gif': return 'image/gif';
135 case 'png': return 'image/png';
136 case 'jpg': return 'image/jpeg';
137 case 'jpeg': return 'image/jpeg';
138 }
139
140 return 'unknown/unknown';
141 }
142
143 $magic = MimeMagic::singleton();
144 // Use the extension only, rather than magic numbers, to avoid opening
145 // up vulnerabilities due to uploads of files with allowed extensions
146 // but disallowed types.
147 $type = $magic->guessTypesForExtension( $ext );
148
149 /**
150 * Double-check some security settings that were done on upload but might
151 * have changed since.
152 */
153 if ( $safe ) {
154 global $wgFileBlacklist, $wgCheckFileExtensions, $wgStrictFileExtensions,
155 $wgFileExtensions, $wgVerifyMimeType, $wgMimeTypeBlacklist;
156 list( , $extList ) = UploadBase::splitExtensions( $filename );
157 if ( UploadBase::checkFileExtensionList( $extList, $wgFileBlacklist ) ) {
158 return 'unknown/unknown';
159 }
160 if ( $wgCheckFileExtensions && $wgStrictFileExtensions
161 && !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions ) )
162 {
163 return 'unknown/unknown';
164 }
165 if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeBlacklist ) ) {
166 return 'unknown/unknown';
167 }
168 }
169 return $type;
170 }
171 }