89 $headers = [], $sendErrors =
true, $optHeaders = [], $flags = 0
92 if ( ( ( $flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) {
93 echo
"Headers already sent, terminating.\n";
101 :
static function (
$header ) {
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 ) ) {
140 ini_set(
'zlib.output_compression', 0 );
147 foreach ( $headers as
$header ) {
151 if ( isset( $optHeaders[
'range'] ) ) {
153 if ( is_array( $range ) ) {
155 $headerFunc(
'Content-Length: ' . $range[2] );
156 $headerFunc(
"Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
157 } elseif ( $range ===
'invalid' ) {
160 $headerFunc(
'Cache-Control: no-cache' );
161 $headerFunc(
'Content-Type: text/html; charset=utf-8' );
162 $headerFunc(
'Content-Range: bytes */' . $info[
'size'] );
167 $headerFunc(
'Content-Length: ' . $info[
'size'] );
171 $headerFunc(
'Content-Length: ' . $info[
'size'] );
174 if ( is_array( $range ) ) {
175 $handle = fopen( $this->path,
'rb' );
178 fseek( $handle, $range[0] );
179 $remaining = $range[2];
180 while ( $remaining > 0 && $ok ) {
181 $bytes = min( $remaining, 8 * 1024 );
182 $data = fread( $handle, $bytes );
183 $remaining -= $bytes;
184 $ok = ( $data !== false );
191 return readfile( $this->path ) !==
false;
230 if ( preg_match(
'#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
231 [ , $start, $end ] = $m;
232 if ( $start ===
'' && $end ===
'' ) {
233 $absRange = [ 0, $size - 1 ];
234 } elseif ( $start ===
'' ) {
235 $absRange = [ $size - (int)$end, $size - 1 ];
236 } elseif ( $end ===
'' ) {
237 $absRange = [ (int)$start, $size - 1 ];
239 $absRange = [ (int)$start, (
int)$end ];
241 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
242 if ( $absRange[0] < $size ) {
243 $absRange[1] = min( $absRange[1], $size - 1 );
244 $absRange[2] = $absRange[1] - $absRange[0] + 1;
246 } elseif ( $absRange[0] == 0 && $size == 0 ) {
247 return 'unrecognized';
252 return 'unrecognized';