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