MediaWiki master
StreamFile.php
Go to the documentation of this file.
1<?php
2
10namespace MediaWiki\Output;
11
12use InvalidArgumentException;
19
24
25 private const UNKNOWN_CONTENT_TYPE = 'unknown/unknown';
26
39 public static function stream(
40 $fname,
41 $headers = [],
42 $sendErrors = true,
43 $optHeaders = [],
44 $flags = 0
45 ) {
46 if ( FileBackend::isStoragePath( $fname ) ) {
47 throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." );
48 }
49
50 $streamer = new HTTPFileStreamer(
51 $fname,
52 [
53 'obResetFunc' => wfResetOutputBuffers( ... ),
54 'streamMimeFunc' => self::contentTypeFromPath( ... ),
55 'headerFunc' => static function ( string $header ): void {
56 RequestContext::getMain()->getRequest()->response()->header( $header );
57 },
58 ]
59 );
60
61 return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags );
62 }
63
71 public static function contentTypeFromPath( $filename, $safe = true ) {
73 // NOTE: TrivialMimeDetection is forced by ThumbnailEntryPoint. When this
74 // code is moved to a non-static method in a service object, we can no
75 // longer rely on that.
76 $trivialMimeDetection = $services->getMainConfig()
78
79 $ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
80
81 # trivial detection by file extension,
82 # used for thumbnails (thumb.php)
83 if ( $trivialMimeDetection ) {
84 return match ( $ext ) {
85 'gif' => 'image/gif',
86 'png' => 'image/png',
87 'jpg',
88 'jpeg' => 'image/jpeg',
89 'webp' => 'image/webp',
90 default => self::UNKNOWN_CONTENT_TYPE,
91 };
92 }
93
94 // Use the extension only, rather than magic numbers, to avoid opening
95 // up vulnerabilities due to uploads of files with allowed extensions
96 // but disallowed types.
97 $type = $services->getMimeAnalyzer()->getMimeTypeFromExtensionOrNull( $ext );
98
103 if ( $safe ) {
104 $mainConfig = $services->getMainConfig();
105 $prohibitedFileExtensions = $mainConfig->get( MainConfigNames::ProhibitedFileExtensions );
106 $checkFileExtensions = $mainConfig->get( MainConfigNames::CheckFileExtensions );
107 $strictFileExtensions = $mainConfig->get( MainConfigNames::StrictFileExtensions );
108 $fileExtensions = $mainConfig->get( MainConfigNames::FileExtensions );
109 $verifyMimeType = $mainConfig->get( MainConfigNames::VerifyMimeType );
110 $mimeTypeExclusions = $mainConfig->get( MainConfigNames::MimeTypeExclusions );
111 [ , $extList ] = UploadBase::splitExtensions( $filename );
112 if ( UploadBase::checkFileExtensionList( $extList, $prohibitedFileExtensions ) ) {
113 return self::UNKNOWN_CONTENT_TYPE;
114 }
115 if (
116 $checkFileExtensions &&
117 $strictFileExtensions &&
118 !UploadBase::checkFileExtensionList( $extList, $fileExtensions )
119 ) {
120 return self::UNKNOWN_CONTENT_TYPE;
121 }
122 if ( $verifyMimeType && $type !== null && in_array( strtolower( $type ), $mimeTypeExclusions ) ) {
123 return self::UNKNOWN_CONTENT_TYPE;
124 }
125 }
126 return $type;
127 }
128}
129
131class_alias( StreamFile::class, 'StreamFile' );
wfResetOutputBuffers( $resetGzipEncoding=true)
Clear away any user-level output buffers, discarding contents.
Group all the pieces relevant to the context of a request into one instance.
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.
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.
stream( $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.