Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / filebackend / HTTPFileStreamer.php
blobe8789d9c4aa00d360820f592b3a41170ab981756
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 namespace Wikimedia\FileBackend;
25 use HttpStatus;
26 use Wikimedia\AtEase\AtEase;
27 use Wikimedia\Timestamp\ConvertibleTimestamp;
29 /**
30 * Functions related to the output of file content
32 * @since 1.28
34 class HTTPFileStreamer {
35 /** @var string */
36 protected $path;
37 /** @var callable */
38 protected $obResetFunc;
39 /** @var callable */
40 protected $streamMimeFunc;
41 /** @var callable */
42 protected $headerFunc;
44 // Do not send any HTTP headers (i.e. body only)
45 public const STREAM_HEADLESS = 1;
46 // Do not try to tear down any PHP output buffers
47 public const STREAM_ALLOW_OB = 2;
49 /**
50 * Takes HTTP headers in a name => value format and converts them to the weird format
51 * expected by stream().
52 * @param string[] $headers
53 * @return array[] [ $headers, $optHeaders ]
54 * @since 1.34
56 public static function preprocessHeaders( $headers ) {
57 $rawHeaders = [];
58 $optHeaders = [];
59 foreach ( $headers as $name => $header ) {
60 $nameLower = strtolower( $name );
61 if ( in_array( $nameLower, [ 'range', 'if-modified-since' ], true ) ) {
62 $optHeaders[$nameLower] = $header;
63 } else {
64 $rawHeaders[] = "$name: $header";
67 return [ $rawHeaders, $optHeaders ];
70 /**
71 * @param string $path Local filesystem path to a file
72 * @param array $params Options map, which includes:
73 * - obResetFunc : alternative callback to clear the output buffer
74 * - streamMimeFunc : alternative method to determine the content type from the path
75 * - headerFunc : alternative method for sending response headers
77 public function __construct( $path, array $params = [] ) {
78 $this->path = $path;
80 $this->obResetFunc = $params['obResetFunc'] ??
81 [ __CLASS__, 'resetOutputBuffers' ];
83 $this->streamMimeFunc = $params['streamMimeFunc'] ??
84 [ __CLASS__, 'contentTypeFromPath' ];
86 $this->headerFunc = $params['headerFunc'] ?? 'header';
89 /**
90 * Stream a file to the browser, adding all the headings and fun stuff.
91 * Headers sent include: Content-type, Content-Length, Last-Modified,
92 * and Content-Disposition.
94 * @param array $headers Any additional headers to send if the file exists
95 * @param bool $sendErrors Send error messages if errors occur (like 404)
96 * @param array $optHeaders HTTP request header map (e.g. "range") (use lowercase keys)
97 * @param int $flags Bitfield of STREAM_* constants
98 * @return bool Success
100 public function stream(
101 $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0
103 $headless = ( $flags & self::STREAM_HEADLESS );
105 // Don't stream it out as text/html if there was a PHP error
106 if ( $headers && headers_sent() ) {
107 echo "Headers already sent, terminating.\n";
108 return false;
111 $headerFunc = $headless
112 ? static function ( $header ) {
113 // no-op
115 : [ $this, 'header' ];
117 AtEase::suppressWarnings();
118 $info = stat( $this->path );
119 AtEase::restoreWarnings();
121 if ( !is_array( $info ) ) {
122 if ( $sendErrors ) {
123 self::send404Message( $this->path, $flags );
125 return false;
128 // Send Last-Modified HTTP header for client-side caching
129 $mtimeCT = new ConvertibleTimestamp( $info['mtime'] );
130 $headerFunc( 'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
132 if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) {
133 call_user_func( $this->obResetFunc );
136 $type = call_user_func( $this->streamMimeFunc, $this->path );
137 if ( $type && $type != 'unknown/unknown' ) {
138 $headerFunc( "Content-type: $type" );
139 } else {
140 // Send a content type which is not known to Internet Explorer, to
141 // avoid triggering IE's content type detection. Sending a standard
142 // unknown content type here essentially gives IE license to apply
143 // whatever content type it likes.
144 $headerFunc( 'Content-type: application/x-wiki' );
147 // Don't send if client has up to date cache
148 if ( isset( $optHeaders['if-modified-since'] ) ) {
149 $modsince = preg_replace( '/;.*$/', '', $optHeaders['if-modified-since'] );
150 if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) {
151 ini_set( 'zlib.output_compression', 0 );
152 $headerFunc( 304 );
153 return true; // ok
157 // Send additional headers
158 foreach ( $headers as $header ) {
159 $headerFunc( $header );
162 if ( isset( $optHeaders['range'] ) ) {
163 $range = self::parseRange( $optHeaders['range'], $info['size'] );
164 if ( is_array( $range ) ) {
165 $headerFunc( 206 );
166 $headerFunc( 'Content-Length: ' . $range[2] );
167 $headerFunc( "Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
168 } elseif ( $range === 'invalid' ) {
169 if ( $sendErrors ) {
170 $headerFunc( 416 );
171 $headerFunc( 'Cache-Control: no-cache' );
172 $headerFunc( 'Content-Type: text/html; charset=utf-8' );
173 $headerFunc( 'Content-Range: bytes */' . $info['size'] );
175 return false;
176 } else { // unsupported Range request (e.g. multiple ranges)
177 $range = null;
178 $headerFunc( 'Content-Length: ' . $info['size'] );
180 } else {
181 $range = null;
182 $headerFunc( 'Content-Length: ' . $info['size'] );
185 if ( is_array( $range ) ) {
186 $handle = fopen( $this->path, 'rb' );
187 if ( $handle ) {
188 $ok = true;
189 fseek( $handle, $range[0] );
190 $remaining = $range[2];
191 while ( $remaining > 0 && $ok ) {
192 $bytes = min( $remaining, 8 * 1024 );
193 $data = fread( $handle, $bytes );
194 $remaining -= $bytes;
195 $ok = ( $data !== false );
196 print $data;
198 } else {
199 return false;
201 } else {
202 return readfile( $this->path ) !== false; // faster
205 return true;
209 * Send out a standard 404 message for a file
211 * @param string $fname Full name and path of the file to stream
212 * @param int $flags Bitfield of STREAM_* constants
213 * @since 1.24
215 public static function send404Message( $fname, $flags = 0 ) {
216 if ( ( $flags & self::STREAM_HEADLESS ) == 0 ) {
217 HttpStatus::header( 404 );
218 header( 'Cache-Control: no-cache' );
219 header( 'Content-Type: text/html; charset=utf-8' );
221 $encFile = htmlspecialchars( $fname );
222 $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
223 echo "<!DOCTYPE html><html><body>
224 <h1>File not found</h1>
225 <p>Although this PHP script ($encScript) exists, the file requested for output
226 ($encFile) does not.</p>
227 </body></html>
232 * Convert a Range header value to an absolute (start, end) range tuple
234 * @param string $range Range header value
235 * @param int $size File size
236 * @return array|string Returns error string on failure (start, end, length)
237 * @since 1.24
239 public static function parseRange( $range, $size ) {
240 $m = [];
241 if ( preg_match( '#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
242 [ , $start, $end ] = $m;
243 if ( $start === '' && $end === '' ) {
244 $absRange = [ 0, $size - 1 ];
245 } elseif ( $start === '' ) {
246 $absRange = [ $size - (int)$end, $size - 1 ];
247 } elseif ( $end === '' ) {
248 $absRange = [ (int)$start, $size - 1 ];
249 } else {
250 $absRange = [ (int)$start, (int)$end ];
252 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
253 if ( $absRange[0] < $size ) {
254 $absRange[1] = min( $absRange[1], $size - 1 ); // stop at EOF
255 $absRange[2] = $absRange[1] - $absRange[0] + 1;
256 return $absRange;
257 } elseif ( $absRange[0] == 0 && $size == 0 ) {
258 return 'unrecognized'; // the whole file should just be sent
261 return 'invalid';
263 return 'unrecognized';
266 protected static function resetOutputBuffers() {
267 while ( ob_get_status() ) {
268 if ( !ob_end_clean() ) {
269 // Could not remove output buffer handler; abort now
270 // to avoid getting in some kind of infinite loop.
271 break;
277 * Determine the file type of a file based on the path
279 * @param string $filename Storage path or file system path
280 * @return null|string
282 protected static function contentTypeFromPath( $filename ) {
283 $ext = strrchr( $filename, '.' );
284 $ext = $ext ? strtolower( substr( $ext, 1 ) ) : '';
286 switch ( $ext ) {
287 case 'gif':
288 return 'image/gif';
289 case 'png':
290 return 'image/png';
291 case 'jpg':
292 case 'jpeg':
293 return 'image/jpeg';
294 // T366422: Support webp here as well for consistency
295 case 'webp':
296 return 'image/webp';
299 return 'unknown/unknown';
302 private function header( $header ) {
303 if ( is_int( $header ) ) {
304 $header = HttpStatus::getHeader( $header );
307 ( $this->headerFunc )( $header );
311 /** @deprecated class alias since 1.43 */
312 class_alias( HTTPFileStreamer::class, 'HTTPFileStreamer' );