23 use Wikimedia\AtEase\AtEase;
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
54 foreach ( $headers as $name =>
$header ) {
55 $nameLower = strtolower( $name );
56 if ( in_array( $nameLower, [
'range',
'if-modified-since' ],
true ) ) {
57 $optHeaders[$nameLower] =
$header;
59 $rawHeaders[] =
"$name: $header";
62 return [ $rawHeaders, $optHeaders ];
73 $this->obResetFunc = $params[
'obResetFunc'] ?? [ __CLASS__,
'resetOutputBuffers' ];
74 $this->streamMimeFunc = $params[
'streamMimeFunc'] ?? [ __CLASS__,
'contentTypeFromPath' ];
89 $headers = [], $sendErrors =
true, $optHeaders = [], $flags = 0
92 if ( ( ( $flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) {
93 echo
"Headers already sent, terminating.\n";
105 AtEase::suppressWarnings();
106 $info = stat( $this->path );
107 AtEase::restoreWarnings();
109 if ( !is_array( $info ) ) {
117 $mtimeCT =
new ConvertibleTimestamp( $info[
'mtime'] );
118 $headerFunc(
'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
120 if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) {
121 call_user_func( $this->obResetFunc );
124 $type = call_user_func( $this->streamMimeFunc, $this->path );
126 $headerFunc(
"Content-type: $type" );
132 $headerFunc(
'Content-type: application/x-wiki' );
136 if ( isset( $optHeaders[
'if-modified-since'] ) ) {
137 $modsince = preg_replace(
'/;.*$/',
'', $optHeaders[
'if-modified-since'] );
138 if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) {
139 ini_set(
'zlib.output_compression', 0 );
146 foreach ( $headers as
$header ) {
150 if ( isset( $optHeaders[
'range'] ) ) {
152 if ( is_array( $range ) ) {
154 $headerFunc(
'Content-Length: ' . $range[2] );
155 $headerFunc(
"Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
156 } elseif ( $range ===
'invalid' ) {
159 $headerFunc(
'Cache-Control: no-cache' );
160 $headerFunc(
'Content-Type: text/html; charset=utf-8' );
161 $headerFunc(
'Content-Range: bytes */' . $info[
'size'] );
166 $headerFunc(
'Content-Length: ' . $info[
'size'] );
170 $headerFunc(
'Content-Length: ' . $info[
'size'] );
173 if ( is_array( $range ) ) {
174 $handle = fopen( $this->path,
'rb' );
177 fseek( $handle, $range[0] );
178 $remaining = $range[2];
179 while ( $remaining > 0 && $ok ) {
180 $bytes = min( $remaining, 8 * 1024 );
181 $data = fread( $handle, $bytes );
182 $remaining -= $bytes;
183 $ok = ( $data !== false );
190 return readfile( $this->path ) !==
false;
204 if ( ( $flags & self::STREAM_HEADLESS ) == 0 ) {
206 header(
'Cache-Control: no-cache' );
207 header(
'Content-Type: text/html; charset=utf-8' );
209 $encFile = htmlspecialchars( $fname );
210 $encScript = htmlspecialchars( $_SERVER[
'SCRIPT_NAME'] );
211 echo
"<!DOCTYPE html><html><body>
212 <h1>File not found</h1>
213 <p>Although this PHP script ($encScript) exists, the file requested for output
214 ($encFile) does not.</p>
229 if ( preg_match(
'#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
230 list( , $start, $end ) = $m;
231 if ( $start ===
'' && $end ===
'' ) {
232 $absRange = [ 0, $size - 1 ];
233 } elseif ( $start ===
'' ) {
234 $absRange = [ $size - $end, $size - 1 ];
235 } elseif ( $end ===
'' ) {
236 $absRange = [ $start, $size - 1 ];
238 $absRange = [ $start, $end ];
240 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
241 if ( $absRange[0] < $size ) {
242 $absRange[1] = min( $absRange[1], $size - 1 );
243 $absRange[2] = $absRange[1] - $absRange[0] + 1;
245 } elseif ( $absRange[0] == 0 && $size == 0 ) {
246 return 'unrecognized';
251 return 'unrecognized';
255 while ( ob_get_status() ) {
256 if ( !ob_end_clean() ) {
271 $ext = strrchr( $filename,
'.' );
272 $ext =
$ext ===
false ?
'' : strtolower( substr(
$ext, 1 ) );
285 return 'unknown/unknown';