22 use Wikimedia\Timestamp\ConvertibleTimestamp;
50 $this->obResetFunc =
$params[
'obResetFunc'] ?? [ __CLASS__,
'resetOutputBuffers' ];
51 $this->streamMimeFunc =
$params[
'streamMimeFunc'] ?? [ __CLASS__,
'contentTypeFromPath' ];
66 $headers = [], $sendErrors =
true, $optHeaders = [], $flags = 0
69 if ( ( ( $flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) {
70 echo
"Headers already sent, terminating.\n";
82 Wikimedia\suppressWarnings();
83 $info = stat( $this->path );
84 Wikimedia\restoreWarnings();
86 if ( !is_array( $info ) ) {
94 $mtimeCT =
new ConvertibleTimestamp( $info[
'mtime'] );
95 $headerFunc(
'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
97 if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) {
98 call_user_func( $this->obResetFunc );
101 $type = call_user_func( $this->streamMimeFunc, $this->path );
103 $headerFunc(
"Content-type: $type" );
109 $headerFunc(
'Content-type: application/x-wiki' );
113 if ( isset( $optHeaders[
'if-modified-since'] ) ) {
114 $modsince = preg_replace(
'/;.*$/',
'', $optHeaders[
'if-modified-since'] );
115 if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) {
116 ini_set(
'zlib.output_compression', 0 );
127 if ( isset( $optHeaders[
'range'] ) ) {
129 if ( is_array( $range ) ) {
131 $headerFunc(
'Content-Length: ' . $range[2] );
132 $headerFunc(
"Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
133 } elseif ( $range ===
'invalid' ) {
136 $headerFunc(
'Cache-Control: no-cache' );
137 $headerFunc(
'Content-Type: text/html; charset=utf-8' );
138 $headerFunc(
'Content-Range: bytes */' . $info[
'size'] );
143 $headerFunc(
'Content-Length: ' . $info[
'size'] );
147 $headerFunc(
'Content-Length: ' . $info[
'size'] );
150 if ( is_array( $range ) ) {
151 $handle = fopen( $this->path,
'rb' );
154 fseek( $handle, $range[0] );
155 $remaining = $range[2];
156 while ( $remaining > 0 && $ok ) {
157 $bytes = min( $remaining, 8 * 1024 );
158 $data = fread( $handle, $bytes );
159 $remaining -= $bytes;
160 $ok = ( $data !==
false );
167 return readfile( $this->path ) !==
false;
181 if ( ( $flags & self::STREAM_HEADLESS ) == 0 ) {
183 header(
'Cache-Control: no-cache' );
184 header(
'Content-Type: text/html; charset=utf-8' );
186 $encFile = htmlspecialchars(
$fname );
187 $encScript = htmlspecialchars( $_SERVER[
'SCRIPT_NAME'] );
188 echo
"<!DOCTYPE html><html><body>
189 <h1>File not found</h1>
190 <p>Although this PHP script ($encScript) exists, the file requested for output
191 ($encFile) does not.</p>
206 if ( preg_match(
'#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
207 list( , $start, $end ) = $m;
208 if ( $start ===
'' && $end ===
'' ) {
209 $absRange = [ 0, $size - 1 ];
210 } elseif ( $start ===
'' ) {
211 $absRange = [ $size - $end, $size - 1 ];
212 } elseif ( $end ===
'' ) {
213 $absRange = [ $start, $size - 1 ];
215 $absRange = [ $start, $end ];
217 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
218 if ( $absRange[0] < $size ) {
219 $absRange[1] = min( $absRange[1], $size - 1 );
220 $absRange[2] = $absRange[1] - $absRange[0] + 1;
222 } elseif ( $absRange[0] == 0 && $size == 0 ) {
223 return 'unrecognized';
228 return 'unrecognized';
232 while ( ob_get_status() ) {
233 if ( !ob_end_clean() ) {
248 $ext = strrchr( $filename,
'.' );
249 $ext =
$ext ===
false ?
'' : strtolower( substr(
$ext, 1 ) );
262 return 'unknown/unknown';