In FileBackend:
[lhc/web/wiklou.git] / includes / StreamFile.php
1 <?php
2 /**
3 * Functions related to the output of file content
4 *
5 * @file
6 */
7 class StreamFile {
8 const READY_STREAM = 1;
9 const NOT_MODIFIED = 2;
10
11 /**
12 * Stream a file to the browser, adding all the headings and fun stuff.
13 * Headers sent include: Content-type, Content-Length, Last-Modified,
14 * and Content-Disposition.
15 *
16 * @param $fname string Full name and path of the file to stream
17 * @param $headers array Any additional headers to send
18 * @param $sendErrors bool Send error messages if errors occur (like 404)
19 * @return bool Success
20 */
21 public static function stream( $fname, $headers = array(), $sendErrors = true ) {
22 wfSuppressWarnings();
23 $stat = stat( $fname );
24 wfRestoreWarnings();
25
26 $res = self::prepareForStream( $fname, $stat, $headers, $sendErrors );
27 if ( $res == self::NOT_MODIFIED ) {
28 return true; // use client cache
29 } elseif ( $res == self::READY_STREAM ) {
30 return readfile( $fname );
31 } else {
32 return false; // failed
33 }
34 }
35
36 /**
37 * Call this function used in preparation before streaming a file.
38 * This function does the following:
39 * (a) sends Last-Modified, Content-type, and Content-Disposition headers
40 * (b) cancels any PHP output buffering and automatic gzipping of output
41 * (c) sends Content-Length header based on HTTP_IF_MODIFIED_SINCE check
42 *
43 * @param $path string Storage path or file system path
44 * @param $info Array File stat info with 'mtime' and 'size' fields
45 * @param $headers Array Additional headers to send
46 * @param $sendErrors bool Send error messages if errors occur (like 404)
47 * @return int|false READY_STREAM, NOT_MODIFIED, or false on failure
48 */
49 public static function prepareForStream(
50 $path, array $info, $headers = array(), $sendErrors = true
51 ) {
52 global $wgLanguageCode;
53
54 if ( !$info ) {
55 if ( $sendErrors ) {
56 header( 'HTTP/1.0 404 Not Found' );
57 header( 'Cache-Control: no-cache' );
58 header( 'Content-Type: text/html; charset=utf-8' );
59 $encFile = htmlspecialchars( $path );
60 $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
61 echo "<html><body>
62 <h1>File not found</h1>
63 <p>Although this PHP script ($encScript) exists, the file requested for output
64 ($encFile) does not.</p>
65 </body></html>
66 ";
67 }
68 return false;
69 }
70
71 // Sent Last-Modified HTTP header for client-side caching
72 header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) );
73
74 // Cancel output buffering and gzipping if set
75 wfResetOutputBuffers();
76
77 $type = self::getType( $path );
78 if ( $type && $type != 'unknown/unknown' ) {
79 header( "Content-type: $type" );
80 } else {
81 header( 'Content-type: application/x-wiki' );
82 }
83
84 // Don't stream it out as text/html if there was a PHP error
85 if ( headers_sent() ) {
86 echo "Headers already sent, terminating.\n";
87 return false;
88 }
89
90 header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" .
91 urlencode( basename( $path ) ) );
92
93 // Send additional headers
94 foreach ( $headers as $header ) {
95 header( $header );
96 }
97
98 // Don't send if client has up to date cache
99 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
100 $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
101 if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) {
102 ini_set( 'zlib.output_compression', 0 );
103 header( "HTTP/1.0 304 Not Modified" );
104 return self::NOT_MODIFIED; // ok
105 }
106 }
107
108 header( 'Content-Length: ' . $info['size'] );
109
110 return self::READY_STREAM; // ok
111 }
112
113 /**
114 * Determine the filetype we're dealing with
115 * @param $filename string Storage path or file system path
116 * @param $safe bool Whether to do retroactive upload blacklist checks
117 * @return null|string
118 */
119 protected static function getType( $filename, $safe = true ) {
120 global $wgTrivialMimeDetection;
121
122 $ext = strrchr( $filename, '.' );
123 $ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) );
124
125 # trivial detection by file extension,
126 # used for thumbnails (thumb.php)
127 if ( $wgTrivialMimeDetection ) {
128 switch ( $ext ) {
129 case 'gif': return 'image/gif';
130 case 'png': return 'image/png';
131 case 'jpg': return 'image/jpeg';
132 case 'jpeg': return 'image/jpeg';
133 }
134
135 return 'unknown/unknown';
136 }
137
138 $magic = MimeMagic::singleton();
139 // Use the extension only, rather than magic numbers, to avoid opening
140 // up vulnerabilities due to uploads of files with allowed extensions
141 // but disallowed types.
142 $type = $magic->guessTypesForExtension( $ext );
143
144 /**
145 * Double-check some security settings that were done on upload but might
146 * have changed since.
147 */
148 if ( $safe ) {
149 global $wgFileBlacklist, $wgCheckFileExtensions, $wgStrictFileExtensions,
150 $wgFileExtensions, $wgVerifyMimeType, $wgMimeTypeBlacklist;
151 list( , $extList ) = UploadBase::splitExtensions( $filename );
152 if ( UploadBase::checkFileExtensionList( $extList, $wgFileBlacklist ) ) {
153 return 'unknown/unknown';
154 }
155 if ( $wgCheckFileExtensions && $wgStrictFileExtensions
156 && !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions ) )
157 {
158 return 'unknown/unknown';
159 }
160 if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeBlacklist ) ) {
161 return 'unknown/unknown';
162 }
163 }
164 return $type;
165 }
166 }