MediaWiki REL1_41
StreamFile.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Output;
25
26use FileBackend;
28use InvalidArgumentException;
31use UploadBase;
32
38 private const UNKNOWN_CONTENT_TYPE = 'unknown/unknown';
39
52 public static function stream(
53 $fname,
54 $headers = [],
55 $sendErrors = true,
56 $optHeaders = [],
57 $flags = 0
58 ) {
59 if ( FileBackend::isStoragePath( $fname ) ) {
60 throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." );
61 }
62
63 $streamer = new HTTPFileStreamer(
64 $fname,
65 [
66 'obResetFunc' => 'wfResetOutputBuffers',
67 'streamMimeFunc' => [ __CLASS__, 'contentTypeFromPath' ]
68 ]
69 );
70
71 return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags );
72 }
73
81 public static function contentTypeFromPath( $filename, $safe = true ) {
82 $trivialMimeDetection = MediaWikiServices::getInstance()->getMainConfig()
84
85 $ext = strrchr( $filename, '.' );
86 $ext = $ext ? strtolower( substr( $ext, 1 ) ) : '';
87
88 # trivial detection by file extension,
89 # used for thumbnails (thumb.php)
90 if ( $trivialMimeDetection ) {
91 switch ( $ext ) {
92 case 'gif':
93 return 'image/gif';
94 case 'png':
95 return 'image/png';
96 case 'jpg':
97 case 'jpeg':
98 return 'image/jpeg';
99 }
100
101 return self::UNKNOWN_CONTENT_TYPE;
102 }
103
104 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
105 // Use the extension only, rather than magic numbers, to avoid opening
106 // up vulnerabilities due to uploads of files with allowed extensions
107 // but disallowed types.
108 $type = $magic->getMimeTypeFromExtensionOrNull( $ext );
109
114 if ( $safe ) {
115 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
116 $prohibitedFileExtensions = $mainConfig->get( MainConfigNames::ProhibitedFileExtensions );
117 $checkFileExtensions = $mainConfig->get( MainConfigNames::CheckFileExtensions );
118 $strictFileExtensions = $mainConfig->get( MainConfigNames::StrictFileExtensions );
119 $fileExtensions = $mainConfig->get( MainConfigNames::FileExtensions );
120 $verifyMimeType = $mainConfig->get( MainConfigNames::VerifyMimeType );
121 $mimeTypeExclusions = $mainConfig->get( MainConfigNames::MimeTypeExclusions );
122 [ , $extList ] = UploadBase::splitExtensions( $filename );
123 if ( UploadBase::checkFileExtensionList( $extList, $prohibitedFileExtensions ) ) {
124 return self::UNKNOWN_CONTENT_TYPE;
125 }
126 if (
127 $checkFileExtensions &&
128 $strictFileExtensions &&
129 !UploadBase::checkFileExtensionList( $extList, $fileExtensions )
130 ) {
131 return self::UNKNOWN_CONTENT_TYPE;
132 }
133 if ( $verifyMimeType && $type !== null && in_array( strtolower( $type ), $mimeTypeExclusions ) ) {
134 return self::UNKNOWN_CONTENT_TYPE;
135 }
136 }
137 return $type;
138 }
139}
140
144class_alias( StreamFile::class, 'StreamFile' );
Base class for all file backend classes (including multi-write backends).
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Functions related to the output of file content.
A class containing constants representing the names of configuration variables.
const MimeTypeExclusions
Name constant for the MimeTypeExclusions setting, for use with Config::get()
const ProhibitedFileExtensions
Name constant for the ProhibitedFileExtensions setting, for use with Config::get()
const TrivialMimeDetection
Name constant for the TrivialMimeDetection setting, for use with Config::get()
const VerifyMimeType
Name constant for the VerifyMimeType setting, for use with Config::get()
const StrictFileExtensions
Name constant for the StrictFileExtensions setting, for use with Config::get()
const FileExtensions
Name constant for the FileExtensions setting, for use with Config::get()
const CheckFileExtensions
Name constant for the CheckFileExtensions setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Functions related to the output of file content.
static contentTypeFromPath( $filename, $safe=true)
Determine the file type of a file based on the path.
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
UploadBase and subclasses are the backend of MediaWiki's file uploads.
static splitExtensions( $filename)
Split a file into a base name and all dot-delimited 'extensions' on the end.
static checkFileExtensionList( $ext, $list)
Perform case-insensitive match against a list of file extensions.
if(!is_readable( $file)) $ext
Definition router.php:48