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