* Stream a file to the browser, adding all the headings and fun stuff
* @param $fname string Full name and path of the file to stream
* @param $headers array Any additional headers to send
+ * @param $sendErrors bool Send error messages if errors occur (like 404)
+ * @return bool Success
*/
- public static function stream( $fname, $headers = array(), $request = null ) {
+ public static function stream( $fname, $headers = array(), $sendErrors = true ) {
+ global $wgLanguageCode;
+
wfSuppressWarnings();
$stat = stat( $fname );
wfRestoreWarnings();
if ( !$stat ) {
- header( 'HTTP/1.0 404 Not Found' );
- header( 'Cache-Control: no-cache' );
- header( 'Content-Type: text/html; charset=utf-8' );
- $encFile = htmlspecialchars( $fname );
- $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
- echo "<html><body>
- <h1>File not found</h1>
- <p>Although this PHP script ($encScript) exists, the file requested for output
- ($encFile) does not.</p>
- </body></html>
- ";
- return;
+ if ( $sendErrors ) {
+ header( 'HTTP/1.0 404 Not Found' );
+ header( 'Cache-Control: no-cache' );
+ header( 'Content-Type: text/html; charset=utf-8' );
+ $encFile = htmlspecialchars( $fname );
+ $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
+ echo "<html><body>
+ <h1>File not found</h1>
+ <p>Although this PHP script ($encScript) exists, the file requested for output
+ ($encFile) does not.</p>
+ </body></html>
+ ";
+ }
+ return false;
}
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' );
// Don't stream it out as text/html if there was a PHP error
if ( headers_sent() ) {
echo "Headers already sent, terminating.\n";
- return;
+ return false;
}
- global $wgLanguageCode;
- header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . urlencode( basename( $fname ) ) );
+ header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" .
+ urlencode( basename( $fname ) ) );
+ // Send additional headers
foreach ( $headers as $header ) {
header( $header );
}
+ // Don't send if client has up to date cache
if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
$modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
$sinceTime = strtotime( $modsince );
if ( $stat['mtime'] <= $sinceTime ) {
ini_set( 'zlib.output_compression', 0 );
header( "HTTP/1.0 304 Not Modified" );
- return;
+ return true; // ok
}
}
header( 'Content-Length: ' . $stat['size'] );
- readfile( $fname );
+ return readfile( $fname );
}
/**