Cancel output buffering in StreamFile; when used inside gzip buffering this could...
[lhc/web/wiklou.git] / includes / StreamFile.php
1 <?php
2 /** */
3
4 /** */
5 function wfStreamFile( $fname ) {
6 $stat = @stat( $fname );
7 if ( !$stat ) {
8 header( 'HTTP/1.0 404 Not Found' );
9 echo "<html><body>
10 <h1>File not found</h1>
11 <p>Although this PHP script ({$_SERVER['SCRIPT_NAME']}) exists, the file requested for output
12 does not.</p>
13 </body></html>";
14 return;
15 }
16
17 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' );
18
19 // Cancel output buffering and gzipping if set
20 while( $status = ob_get_status() ) {
21 ob_end_clean();
22 if( $status['name'] == 'ob_gzhandler' ) {
23 header( 'Content-Encoding: identity' );
24 }
25 }
26
27 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
28 $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
29 $sinceTime = strtotime( $modsince );
30 if ( $stat['mtime'] <= $sinceTime ) {
31 header( "HTTP/1.0 304 Not Modified" );
32 return;
33 }
34 }
35
36 header( 'Content-Length: ' . $stat['size'] );
37
38 $type = wfGetType( $fname );
39 if ( $type and $type!="unknown/unknown") {
40 header("Content-type: $type");
41 } else {
42 header('Content-type: application/x-wiki');
43 }
44
45 readfile( $fname );
46 }
47
48 /** */
49 function wfGetType( $filename ) {
50 global $wgTrivialMimeDetection;
51
52 # trivial detection by file extension,
53 # used for thumbnails (thumb.php)
54 if ($wgTrivialMimeDetection) {
55 $ext= strtolower(strrchr($filename, '.'));
56
57 switch ($ext) {
58 case '.gif': return 'image/gif';
59 case '.png': return 'image/png';
60 case '.jpg': return 'image/jpeg';
61 case '.jpeg': return 'image/jpeg';
62 }
63
64 return 'unknown/unknown';
65 }
66 else {
67 $magic=& wfGetMimeMagic();
68 return $magic->guessMimeType($filename); //full fancy mime detection
69 }
70 }
71
72 ?>