Fixed some obvious bugs with the new code and implemented If-Modified-Since handling
[lhc/web/wiklou.git] / includes / StreamFile.php
1 <?php
2
3 function wfStreamFile( $fname ) {
4 global $wgSquidMaxage;
5 $stat = stat( $fname );
6 if ( !$stat ) {
7 header( 'HTTP/1.0 404 Not Found' );
8 echo "<html><body>
9 <h1>File not found</h1>
10 <p>Although this PHP script ({$_SERVER['SCRIPT_NAME']}) exists, the file requested for output
11 does not.</p>
12 </body></html>";
13 return;
14 }
15
16 header( "Cache-Control: s-maxage=$wgSquidMaxage, must-revalidate, max-age=0" );
17 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' );
18
19 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
20 $sinceTime = strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
21 if ( $stat['mtime'] <= $sinceTime ) {
22 header( "HTTP/1.0 304 Not Modified" );
23 return;
24 }
25 }
26
27 $type = wfGetType( $fname );
28 if ( $type ) {
29 header("Content-type: $type");
30 } else {
31 header('Content-type: application/x-wiki');
32 }
33 readfile( $fname );
34 }
35
36 function wfGetType( $filename ) {
37 # There's probably a better way to do this
38 $types = <<<END_STRING
39 application/andrew-inset ez
40 application/mac-binhex40 hqx
41 application/mac-compactpro cpt
42 application/mathml+xml mathml
43 application/msword doc
44 application/octet-stream bin dms lha lzh exe class so dll
45 application/oda oda
46 application/ogg ogg
47 application/pdf pdf
48 application/postscript ai eps ps
49 application/rdf+xml rdf
50 application/smil smi smil
51 application/srgs gram
52 application/srgs+xml grxml
53 application/vnd.mif mif
54 application/vnd.ms-excel xls
55 application/vnd.ms-powerpoint ppt
56 application/vnd.wap.wbxml wbxml
57 application/vnd.wap.wmlc wmlc
58 application/vnd.wap.wmlscriptc wmlsc
59 application/voicexml+xml vxml
60 application/x-bcpio bcpio
61 application/x-cdlink vcd
62 application/x-chess-pgn pgn
63 application/x-cpio cpio
64 application/x-csh csh
65 application/x-director dcr dir dxr
66 application/x-dvi dvi
67 application/x-futuresplash spl
68 application/x-gtar gtar
69 application/x-hdf hdf
70 application/x-javascript js
71 application/x-koan skp skd skt skm
72 application/x-latex latex
73 application/x-netcdf nc cdf
74 application/x-sh sh
75 application/x-shar shar
76 application/x-shockwave-flash swf
77 application/x-stuffit sit
78 application/x-sv4cpio sv4cpio
79 application/x-sv4crc sv4crc
80 application/x-tar tar
81 application/x-tcl tcl
82 application/x-tex tex
83 application/x-texinfo texinfo texi
84 application/x-troff t tr roff
85 application/x-troff-man man
86 application/x-troff-me me
87 application/x-troff-ms ms
88 application/x-ustar ustar
89 application/x-wais-source src
90 application/xhtml+xml xhtml xht
91 application/xslt+xml xslt
92 application/xml xml xsl
93 application/xml-dtd dtd
94 application/zip zip
95 audio/basic au snd
96 audio/midi mid midi kar
97 audio/mpeg mpga mp2 mp3
98 audio/x-aiff aif aiff aifc
99 audio/x-mpegurl m3u
100 audio/x-pn-realaudio ram rm
101 audio/x-pn-realaudio-plugin rpm
102 audio/x-realaudio ra
103 audio/x-wav wav
104 chemical/x-pdb pdb
105 chemical/x-xyz xyz
106 image/bmp bmp
107 image/cgm cgm
108 image/gif gif
109 image/ief ief
110 image/jpeg jpeg jpg jpe
111 image/png png
112 image/svg+xml svg
113 image/tiff tiff tif
114 image/vnd.djvu djvu djv
115 image/vnd.wap.wbmp wbmp
116 image/x-cmu-raster ras
117 image/x-icon ico
118 image/x-portable-anymap pnm
119 image/x-portable-bitmap pbm
120 image/x-portable-graymap pgm
121 image/x-portable-pixmap ppm
122 image/x-rgb rgb
123 image/x-xbitmap xbm
124 image/x-xpixmap xpm
125 image/x-xwindowdump xwd
126 model/iges igs iges
127 model/mesh msh mesh silo
128 model/vrml wrl vrml
129 text/calendar ics ifb
130 text/css css
131 text/richtext rtx
132 text/rtf rtf
133 text/sgml sgml sgm
134 text/tab-separated-values tsv
135 text/vnd.wap.wml wml
136 text/vnd.wap.wmlscript wmls
137 text/x-setext etx
138 video/mpeg mpeg mpg mpe
139 video/quicktime qt mov
140 video/vnd.mpegurl mxu
141 video/x-msvideo avi
142 video/x-sgi-movie movie
143 x-conference/x-cooltalk ice
144 END_STRING;
145 // Needed for windows servers who use \r\n not \n
146 $endl = "
147 ";
148 $types = explode( $endl, $types );
149 if ( !preg_match( "/\.([^.]*?)$/", $filename, $matches ) ) {
150 return false;
151 }
152
153 foreach( $types as $type ) {
154 $extensions = explode( " ", $type );
155 for ( $i=1; $i<count( $extensions ); $i++ ) {
156 if ( $extensions[$i] == $matches[1] ) {
157 return $extensions[0];
158 }
159 }
160 }
161 return false;
162 }
163
164 ?>