101 $headers = [], $sendErrors =
true, $optHeaders = [], $flags = 0
106 if ( $headers && headers_sent() ) {
107 echo
"Headers already sent, terminating.\n";
112 ?
static function (
$header ) {
115 : [ $this,
'header' ];
117 AtEase::suppressWarnings();
118 $info = stat( $this->path );
119 AtEase::restoreWarnings();
121 if ( !is_array( $info ) ) {
129 $mtimeCT =
new ConvertibleTimestamp( $info[
'mtime'] );
130 $headerFunc(
'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
132 if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) {
137 if ( $type && $type !=
'unknown/unknown' ) {
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 );
158 foreach ( $headers as
$header ) {
162 if ( isset( $optHeaders[
'range'] ) ) {
164 if ( is_array( $range ) ) {
167 $headerFunc(
"Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
168 } elseif ( $range ===
'invalid' ) {
172 $headerFunc(
'Content-Type: text/html; charset=utf-8' );
173 $headerFunc(
'Content-Range: bytes */' . $info[
'size'] );
185 if ( is_array( $range ) ) {
186 $handle = fopen( $this->path,
'rb' );
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 );
202 return readfile( $this->path ) !==
false;
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 ];
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 );
255 $absRange[2] = $absRange[1] - $absRange[0] + 1;
257 } elseif ( $absRange[0] == 0 && $size == 0 ) {
258 return 'unrecognized';
263 return 'unrecognized';