1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?php
/**
* Functions related to the output of file content.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Output;
use InvalidArgumentException;
use MediaWiki\Context\RequestContext;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use UploadBase;
use Wikimedia\FileBackend\FileBackend;
use Wikimedia\FileBackend\HTTPFileStreamer;
/**
* Functions related to the output of file content
*/
class StreamFile {
private const UNKNOWN_CONTENT_TYPE = 'unknown/unknown';
/**
* Stream a file to the browser, adding all the headings and fun stuff.
* Headers sent include: Content-type, Content-Length, Last-Modified,
* and Content-Disposition.
*
* @param string $fname Full name and path of the file to stream
* @param array $headers Any additional headers to send if the file exists
* @param bool $sendErrors Send error messages if errors occur (like 404)
* @param array $optHeaders HTTP request header map (e.g. "range") (use lowercase keys)
* @param int $flags Bitfield of STREAM_* constants
* @return bool Success
*/
public static function stream(
$fname,
$headers = [],
$sendErrors = true,
$optHeaders = [],
$flags = 0
) {
if ( FileBackend::isStoragePath( $fname ) ) {
throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." );
}
$streamer = new HTTPFileStreamer(
$fname,
[
'obResetFunc' => 'wfResetOutputBuffers',
'streamMimeFunc' => [ __CLASS__, 'contentTypeFromPath' ],
'headerFunc' => [ __CLASS__, 'setHeader' ],
]
);
return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags );
}
/**
* @param string $header
*
* @internal
*/
public static function setHeader( $header ) {
RequestContext::getMain()->getRequest()->response()->header( $header );
}
/**
* Determine the file type of a file based on the path
*
* @param string $filename Storage path or file system path
* @param bool $safe Whether to do retroactive upload prevention checks
* @return null|string
*/
public static function contentTypeFromPath( $filename, $safe = true ) {
// NOTE: TrivialMimeDetection is forced by ThumbnailEntryPoint. When this
// code is moved to a non-static method in a service object, we can no
// longer rely on that.
$trivialMimeDetection = MediaWikiServices::getInstance()->getMainConfig()
->get( MainConfigNames::TrivialMimeDetection );
$ext = strrchr( $filename, '.' );
$ext = $ext ? strtolower( substr( $ext, 1 ) ) : '';
# trivial detection by file extension,
# used for thumbnails (thumb.php)
if ( $trivialMimeDetection ) {
switch ( $ext ) {
case 'gif':
return 'image/gif';
case 'png':
return 'image/png';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'webp':
return 'image/webp';
}
return self::UNKNOWN_CONTENT_TYPE;
}
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
// Use the extension only, rather than magic numbers, to avoid opening
// up vulnerabilities due to uploads of files with allowed extensions
// but disallowed types.
$type = $magic->getMimeTypeFromExtensionOrNull( $ext );
/**
* Double-check some security settings that were done on upload but might
* have changed since.
*/
if ( $safe ) {
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
$prohibitedFileExtensions = $mainConfig->get( MainConfigNames::ProhibitedFileExtensions );
$checkFileExtensions = $mainConfig->get( MainConfigNames::CheckFileExtensions );
$strictFileExtensions = $mainConfig->get( MainConfigNames::StrictFileExtensions );
$fileExtensions = $mainConfig->get( MainConfigNames::FileExtensions );
$verifyMimeType = $mainConfig->get( MainConfigNames::VerifyMimeType );
$mimeTypeExclusions = $mainConfig->get( MainConfigNames::MimeTypeExclusions );
[ , $extList ] = UploadBase::splitExtensions( $filename );
if ( UploadBase::checkFileExtensionList( $extList, $prohibitedFileExtensions ) ) {
return self::UNKNOWN_CONTENT_TYPE;
}
if (
$checkFileExtensions &&
$strictFileExtensions &&
!UploadBase::checkFileExtensionList( $extList, $fileExtensions )
) {
return self::UNKNOWN_CONTENT_TYPE;
}
if ( $verifyMimeType && $type !== null && in_array( strtolower( $type ), $mimeTypeExclusions ) ) {
return self::UNKNOWN_CONTENT_TYPE;
}
}
return $type;
}
}
/** @deprecated class alias since 1.41 */
class_alias( StreamFile::class, 'StreamFile' );
|