MediaWiki REL1_37
StreamFile.php
Go to the documentation of this file.
1<?php
27 // Do not send any HTTP headers unless requested by caller (e.g. body only)
30 // Do not try to tear down any PHP output buffers
33
47 public static function stream(
48 $fname, $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0
49 ) {
50 if ( FileBackend::isStoragePath( $fname ) ) { // sanity
51 throw new InvalidArgumentException( __FUNCTION__ . " given storage path '$fname'." );
52 }
53
54 $streamer = new HTTPFileStreamer(
55 $fname,
56 [
57 'obResetFunc' => 'wfResetOutputBuffers',
58 'streamMimeFunc' => [ __CLASS__, 'contentTypeFromPath' ]
59 ]
60 );
61
62 return $streamer->stream( $headers, $sendErrors, $optHeaders, $flags );
63 }
64
72 public static function contentTypeFromPath( $filename, $safe = true ) {
74
75 $ext = strrchr( $filename, '.' );
76 $ext = $ext ? strtolower( substr( $ext, 1 ) ) : '';
77
78 # trivial detection by file extension,
79 # used for thumbnails (thumb.php)
81 switch ( $ext ) {
82 case 'gif':
83 return 'image/gif';
84 case 'png':
85 return 'image/png';
86 case 'jpg':
87 case 'jpeg':
88 return 'image/jpeg';
89 }
90
91 return 'unknown/unknown';
92 }
93
94 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
95 // Use the extension only, rather than magic numbers, to avoid opening
96 // up vulnerabilities due to uploads of files with allowed extensions
97 // but disallowed types.
98 $type = $magic->getMimeTypeFromExtensionOrNull( $ext );
99
104 if ( $safe ) {
107 list( , $extList ) = UploadBase::splitExtensions( $filename );
108 if ( UploadBase::checkFileExtensionList( $extList, $wgProhibitedFileExtensions ) ) {
109 return 'unknown/unknown';
110 }
112 && !UploadBase::checkFileExtensionList( $extList, $wgFileExtensions )
113 ) {
114 return 'unknown/unknown';
115 }
116 if ( $wgVerifyMimeType && in_array( strtolower( $type ), $wgMimeTypeExclusions ) ) {
117 return 'unknown/unknown';
118 }
119 }
120 return $type;
121 }
122}
$wgProhibitedFileExtensions
Files with these extensions will never be allowed as uploads.
$wgFileExtensions
This is the list of preferred extensions for uploading files.
$wgCheckFileExtensions
This is a flag to determine whether or not to check file extensions on upload.
$wgVerifyMimeType
Determines if the MIME type of uploaded files should be checked.
$wgStrictFileExtensions
If this is turned off, users may override the warning for files not covered by $wgFileExtensions.
$wgMimeTypeExclusions
Files with these MIME types will never be allowed as uploads if $wgVerifyMimeType is enabled.
$wgTrivialMimeDetection
Switch for trivial MIME detection.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
Functions related to the output of file content.
Functions related to the output of file content.
static contentTypeFromPath( $filename, $safe=true)
Determine the file type of a file based on the path.
const STREAM_HEADLESS
const STREAM_ALLOW_OB
static stream( $fname, $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
if(!is_readable( $file)) $ext
Definition router.php:48