22 use Wikimedia\Timestamp\ConvertibleTimestamp;
50 $this->obResetFunc = isset(
$params[
'obResetFunc'] )
52 : [ __CLASS__,
'resetOutputBuffers' ];
53 $this->streamMimeFunc = isset(
$params[
'streamMimeFunc'] )
55 : [ __CLASS__,
'contentTypeFromPath' ];
71 $headers = [], $sendErrors =
true, $optHeaders = [],
$flags = 0
74 if ( ( (
$flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) {
75 echo
"Headers already sent, terminating.\n";
87 MediaWiki\suppressWarnings();
88 $info = stat( $this->path );
89 MediaWiki\restoreWarnings();
91 if ( !is_array( $info ) ) {
99 $mtimeCT =
new ConvertibleTimestamp( $info[
'mtime'] );
100 $headerFunc(
'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
102 if ( (
$flags & self::STREAM_ALLOW_OB ) == 0 ) {
103 call_user_func( $this->obResetFunc );
106 $type = call_user_func( $this->streamMimeFunc, $this->path );
108 $headerFunc(
"Content-type: $type" );
114 $headerFunc(
'Content-type: application/x-wiki' );
118 if ( isset( $optHeaders[
'if-modified-since'] ) ) {
119 $modsince = preg_replace(
'/;.*$/',
'', $optHeaders[
'if-modified-since'] );
120 if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) {
121 ini_set(
'zlib.output_compression', 0 );
132 if ( isset( $optHeaders[
'range'] ) ) {
134 if ( is_array( $range ) ) {
136 $headerFunc(
'Content-Length: ' . $range[2] );
137 $headerFunc(
"Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
138 } elseif ( $range ===
'invalid' ) {
141 $headerFunc(
'Cache-Control: no-cache' );
142 $headerFunc(
'Content-Type: text/html; charset=utf-8' );
143 $headerFunc(
'Content-Range: bytes */' . $info[
'size'] );
148 $headerFunc(
'Content-Length: ' . $info[
'size'] );
152 $headerFunc(
'Content-Length: ' . $info[
'size'] );
155 if ( is_array( $range ) ) {
156 $handle = fopen( $this->path,
'rb' );
159 fseek( $handle, $range[0] );
160 $remaining = $range[2];
161 while ( $remaining > 0 && $ok ) {
162 $bytes = min( $remaining, 8 * 1024 );
163 $data = fread( $handle, $bytes );
164 $remaining -= $bytes;
165 $ok = ( $data !==
false );
172 return readfile( $this->path ) !==
false;
186 if ( (
$flags & self::STREAM_HEADLESS ) == 0 ) {
188 header(
'Cache-Control: no-cache' );
189 header(
'Content-Type: text/html; charset=utf-8' );
191 $encFile = htmlspecialchars(
$fname );
192 $encScript = htmlspecialchars( $_SERVER[
'SCRIPT_NAME'] );
193 echo
"<!DOCTYPE html><html><body>
194 <h1>File not found</h1>
195 <p>Although this PHP script ($encScript) exists, the file requested for output
196 ($encFile) does not.</p>
211 if ( preg_match(
'#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
212 list( , $start, $end ) = $m;
213 if ( $start ===
'' && $end ===
'' ) {
214 $absRange = [ 0, $size - 1 ];
215 } elseif ( $start ===
'' ) {
216 $absRange = [ $size - $end, $size - 1 ];
217 } elseif ( $end ===
'' ) {
218 $absRange = [ $start, $size - 1 ];
220 $absRange = [ $start, $end ];
222 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
223 if ( $absRange[0] < $size ) {
224 $absRange[1] = min( $absRange[1], $size - 1 );
225 $absRange[2] = $absRange[1] - $absRange[0] + 1;
227 } elseif ( $absRange[0] == 0 && $size == 0 ) {
228 return 'unrecognized';
233 return 'unrecognized';
237 while ( ob_get_status() ) {
238 if ( !ob_end_clean() ) {
253 $ext = strrchr( $filename,
'.' );
254 $ext =
$ext ===
false ?
'' : strtolower( substr(
$ext, 1 ) );
267 return 'unknown/unknown';