Index: branches/FileBackend/phase3/includes/StreamFile.php |
— | — | @@ -9,24 +9,30 @@ |
10 | 10 | * Stream a file to the browser, adding all the headings and fun stuff |
11 | 11 | * @param $fname string Full name and path of the file to stream |
12 | 12 | * @param $headers array Any additional headers to send |
| 13 | + * @param $sendErrors bool Send error messages if errors occur (like 404) |
| 14 | + * @return bool Success |
13 | 15 | */ |
14 | | - public static function stream( $fname, $headers = array(), $request = null ) { |
| 16 | + public static function stream( $fname, $headers = array(), $sendErrors = true ) { |
| 17 | + global $wgLanguageCode; |
| 18 | + |
15 | 19 | wfSuppressWarnings(); |
16 | 20 | $stat = stat( $fname ); |
17 | 21 | wfRestoreWarnings(); |
18 | 22 | if ( !$stat ) { |
19 | | - header( 'HTTP/1.0 404 Not Found' ); |
20 | | - header( 'Cache-Control: no-cache' ); |
21 | | - header( 'Content-Type: text/html; charset=utf-8' ); |
22 | | - $encFile = htmlspecialchars( $fname ); |
23 | | - $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] ); |
24 | | - echo "<html><body> |
25 | | - <h1>File not found</h1> |
26 | | - <p>Although this PHP script ($encScript) exists, the file requested for output |
27 | | - ($encFile) does not.</p> |
28 | | - </body></html> |
29 | | - "; |
30 | | - return; |
| 23 | + if ( $sendErrors ) { |
| 24 | + header( 'HTTP/1.0 404 Not Found' ); |
| 25 | + header( 'Cache-Control: no-cache' ); |
| 26 | + header( 'Content-Type: text/html; charset=utf-8' ); |
| 27 | + $encFile = htmlspecialchars( $fname ); |
| 28 | + $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] ); |
| 29 | + echo "<html><body> |
| 30 | + <h1>File not found</h1> |
| 31 | + <p>Although this PHP script ($encScript) exists, the file requested for output |
| 32 | + ($encFile) does not.</p> |
| 33 | + </body></html> |
| 34 | + "; |
| 35 | + } |
| 36 | + return false; |
31 | 37 | } |
32 | 38 | |
33 | 39 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' ); |
— | — | @@ -44,29 +50,31 @@ |
45 | 51 | // Don't stream it out as text/html if there was a PHP error |
46 | 52 | if ( headers_sent() ) { |
47 | 53 | echo "Headers already sent, terminating.\n"; |
48 | | - return; |
| 54 | + return false; |
49 | 55 | } |
50 | 56 | |
51 | | - global $wgLanguageCode; |
52 | | - header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . urlencode( basename( $fname ) ) ); |
| 57 | + header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . |
| 58 | + urlencode( basename( $fname ) ) ); |
53 | 59 | |
| 60 | + // Send additional headers |
54 | 61 | foreach ( $headers as $header ) { |
55 | 62 | header( $header ); |
56 | 63 | } |
57 | 64 | |
| 65 | + // Don't send if client has up to date cache |
58 | 66 | if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { |
59 | 67 | $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); |
60 | 68 | $sinceTime = strtotime( $modsince ); |
61 | 69 | if ( $stat['mtime'] <= $sinceTime ) { |
62 | 70 | ini_set( 'zlib.output_compression', 0 ); |
63 | 71 | header( "HTTP/1.0 304 Not Modified" ); |
64 | | - return; |
| 72 | + return true; // ok |
65 | 73 | } |
66 | 74 | } |
67 | 75 | |
68 | 76 | header( 'Content-Length: ' . $stat['size'] ); |
69 | 77 | |
70 | | - readfile( $fname ); |
| 78 | + return readfile( $fname ); |
71 | 79 | } |
72 | 80 | |
73 | 81 | /** |