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