MediaWiki  1.32.5
StreamFile.php
Go to the documentation of this file.
1 <?php
26 class StreamFile {
27  // Do not send any HTTP headers unless requested by caller (e.g. body only)
29  // Do not try to tear down any PHP output buffers
31 
45  public static function stream(
46  $fname, $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0
47  ) {
48  if ( FileBackend::isStoragePath( $fname ) ) { // sanity
49  throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." );
50  }
51 
52  $streamer = new HTTPFileStreamer(
53  $fname,
54  [
55  'obResetFunc' => 'wfResetOutputBuffers',
56  'streamMimeFunc' => [ __CLASS__, 'contentTypeFromPath' ]
57  ]
58  );
59 
60  return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags );
61  }
62 
70  public static function send404Message( $fname, $flags = 0 ) {
72  }
73 
82  public static function parseRange( $range, $size ) {
83  return HTTPFileStreamer::parseRange( $range, $size );
84  }
85 
93  public static function contentTypeFromPath( $filename, $safe = true ) {
95 
96  $ext = strrchr( $filename, '.' );
97  $ext = $ext === false ? '' : strtolower( substr( $ext, 1 ) );
98 
99  # trivial detection by file extension,
100  # used for thumbnails (thumb.php)
101  if ( $wgTrivialMimeDetection ) {
102  switch ( $ext ) {
103  case 'gif':
104  return 'image/gif';
105  case 'png':
106  return 'image/png';
107  case 'jpg':
108  return 'image/jpeg';
109  case 'jpeg':
110  return 'image/jpeg';
111  }
112 
113  return 'unknown/unknown';
114  }
115 
116  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
117  // Use the extension only, rather than magic numbers, to avoid opening
118  // up vulnerabilities due to uploads of files with allowed extensions
119  // but disallowed types.
120  $type = $magic->guessTypesForExtension( $ext );
121 
126  if ( $safe ) {
129  list( , $extList ) = UploadBase::splitExtensions( $filename );
130  if ( UploadBase::checkFileExtensionList( $extList, $wgFileBlacklist ) ) {
131  return 'unknown/unknown';
132  }
134  && !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions )
135  ) {
136  return 'unknown/unknown';
137  }
138  if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeBlacklist ) ) {
139  return 'unknown/unknown';
140  }
141  }
142  return $type;
143  }
144 }
$wgFileBlacklist
$wgFileBlacklist
Files with these extensions will never be allowed as uploads.
Definition: DefaultSettings.php:938
StreamFile\contentTypeFromPath
static contentTypeFromPath( $filename, $safe=true)
Determine the file type of a file based on the path.
Definition: StreamFile.php:93
$wgFileExtensions
$wgFileExtensions
This is the list of preferred extensions for uploading files.
Definition: DefaultSettings.php:931
$wgMimeTypeBlacklist
$wgMimeTypeBlacklist
Files with these MIME types will never be allowed as uploads if $wgVerifyMimeType is enabled.
Definition: DefaultSettings.php:952
$wgStrictFileExtensions
$wgStrictFileExtensions
If this is turned off, users may override the warning for files not covered by $wgFileExtensions.
Definition: DefaultSettings.php:985
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
StreamFile\stream
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
Definition: StreamFile.php:45
HTTPFileStreamer
Functions related to the output of file content.
Definition: HTTPFileStreamer.php:29
FileBackend\isStoragePath
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Definition: FileBackend.php:1428
HTTPFileStreamer\parseRange
static parseRange( $range, $size)
Convert a Range header value to an absolute (start, end) range tuple.
Definition: HTTPFileStreamer.php:225
StreamFile\send404Message
static send404Message( $fname, $flags=0)
Send out a standard 404 message for a file.
Definition: StreamFile.php:70
HTTPFileStreamer\STREAM_ALLOW_OB
const STREAM_ALLOW_OB
Definition: HTTPFileStreamer.php:40
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:121
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:120
StreamFile
Functions related to the output of file content.
Definition: StreamFile.php:26
StreamFile\parseRange
static parseRange( $range, $size)
Convert a Range header value to an absolute (start, end) range tuple.
Definition: StreamFile.php:82
HTTPFileStreamer\STREAM_HEADLESS
const STREAM_HEADLESS
Definition: HTTPFileStreamer.php:38
$wgTrivialMimeDetection
$wgTrivialMimeDetection
Switch for trivial MIME detection.
Definition: DefaultSettings.php:1413
HTTPFileStreamer\send404Message
static send404Message( $fname, $flags=0)
Send out a standard 404 message for a file.
Definition: HTTPFileStreamer.php:201
$wgVerifyMimeType
$wgVerifyMimeType
Determines if the MIME type of uploaded files should be checked.
Definition: DefaultSettings.php:1380
StreamFile\STREAM_ALLOW_OB
const STREAM_ALLOW_OB
Definition: StreamFile.php:30
$ext
$ext
Definition: router.php:55
$wgCheckFileExtensions
$wgCheckFileExtensions
This is a flag to determine whether or not to check file extensions on upload.
Definition: DefaultSettings.php:977
StreamFile\STREAM_HEADLESS
const STREAM_HEADLESS
Definition: StreamFile.php:28
$type
$type
Definition: testCompression.php:48