MediaWiki  master
MimeAnalyzer Class Reference

Implements functions related to MIME types such as detection and mapping to file extension. More...

Inheritance diagram for MimeAnalyzer:
Collaboration diagram for MimeAnalyzer:

Public Member Functions

 __construct (array $params)
 
 addExtraInfo ( $info)
 Adds to the list mapping MIME to media type. More...
 
 addExtraTypes ( $types)
 Adds to the list mapping MIME to file extensions. More...
 
 detectZipType ( $header, $tail=null, $ext=false)
 Detect application-specific file type of a given ZIP file from its header data. More...
 
 findMediaType ( $extMime)
 Returns a media code matching the given MIME type or file extension. More...
 
 getExtensionFromMimeTypeOrNull ( $mime)
 Returns a single file extension for a given MIME type or null if unknown. More...
 
 getExtensionsForType ( $mime)
 Returns a list of file extensions for a given MIME type as a space separated string or null if the MIME type was unrecognized. More...
 
 getExtensionsFromMimeType ( $mime)
 Returns an array of file extensions associated with a given MIME type. More...
 
 getIEMimeTypes ( $fileName, $chunk, $proposed)
 Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content. More...
 
 getMediaType ( $path=null, $mime=null)
 Determine the media type code for a file, using its MIME type, name and possibly its contents. More...
 
 getMediaTypes ()
 Returns an array of media types (MEDIATYPE_xxx constants) More...
 
 getMimeTypeFromExtensionOrNull ( $ext)
 Returns a single MIME type for a given file extension or null if unknown. More...
 
 getMimeTypesFromExtension ( $ext)
 Returns an array of MIME types associated with a given file extension. More...
 
 getTypesForExtension ( $ext)
 Returns a list of MIME types for a given file extension as a space separated string or null if the extension was unrecognized. More...
 
 guessMimeType ( $file, $ext=true)
 MIME type detection. More...
 
 guessTypesForExtension ( $ext)
 Returns a single file extension for a given MIME type or null if unknown. More...
 
 improveTypeFromExtension ( $mime, $ext)
 Improves a MIME type using the file extension. More...
 
 isMatchingExtension ( $extension, $mime)
 Tests if the extension matches the given MIME type. More...
 
 isPHPImageType ( $mime)
 Returns true if the MIME type is known to represent an image format supported by the PHP GD library. More...
 
 isRecognizableExtension ( $extension)
 Returns true if the extension represents a type which can be reliably detected from its content. More...
 
 setLogger (LoggerInterface $logger)
 

Public Attributes

array $mExtToMime = []
 Map of file extensions types to MIME types (as a space separated list) More...
 

Protected Member Functions

 getIEContentAnalyzer ()
 Get a cached instance of IEContentAnalyzer. More...
 
 loadFiles ()
 
 parseMimeInfo ( $rawMimeInfo)
 
 parseMimeTypes ( $rawMimeTypes)
 

Protected Attributes

callable $detectCallback
 
callable $extCallback
 
array< string, string[]> $extToMimes = []
 Map of file extensions to MIME types. More...
 
callable $guessCallback
 
IEContentAnalyzer $IEAnalyzer
 
string $infoFile
 
callable $initCallback
 
array $mediaTypes = null
 Mapping of media types to arrays of MIME types. More...
 
array< string, string[]> $mimeToExts = []
 Map of MIME types to file extensions. More...
 
array $mimeTypeAliases = null
 Map of MIME type aliases. More...
 
string $typeFile
 
string $xmlTypes
 

Private Member Functions

 detectMicrosoftBinaryType ( $handle)
 Detect the type of a Microsoft Compound Binary a.k.a. More...
 
 detectMimeType ( $file, $ext=true)
 Internal MIME type detection. More...
 
 doGuessMimeType ( $file, $ext)
 Guess the MIME type from the file contents. More...
 

Private Attributes

string $extraInfo = ''
 Extra MIME info, set for example by media handling extensions. More...
 
string $extraTypes = ''
 Extra MIME types, set for example by media handling extensions. More...
 
LoggerInterface $logger
 

Detailed Description

Implements functions related to MIME types such as detection and mapping to file extension.

Since
1.28

Definition at line 33 of file MimeAnalyzer.php.

Constructor & Destructor Documentation

◆ __construct()

MimeAnalyzer::__construct ( array  $params)
Parameters
array$paramsConfiguration map, includes:
  • typeFile: path to file with the list of known MIME types
  • infoFile: path to file with the MIME type info
  • xmlTypes: map of root element names to XML MIME types
  • initCallback: initialization callback that is passed this object [optional]
  • detectCallback: alternative to finfo that returns the mime type for a file. For example, the callback can return the output of "file -bi". [optional]
  • guessCallback: callback to improve the guessed MIME type using the file data. This is intended for fixing mistakes in fileinfo or "detectCallback". [optional]
  • extCallback: callback to improve the guessed MIME type using the extension. [optional]
  • logger: PSR-3 logger [optional]
Note
Constructing these instances is expensive due to file reads. A service or singleton pattern should be used to avoid creating instances again and again.

Definition at line 89 of file MimeAnalyzer.php.

References loadFiles().

Member Function Documentation

◆ addExtraInfo()

MimeAnalyzer::addExtraInfo (   $info)

Adds to the list mapping MIME to media type.

As an extension author, you are encouraged to submit patches to MediaWiki's core to add new MIME info to MimeMap.php.

Parameters
string$info

Definition at line 266 of file MimeAnalyzer.php.

◆ addExtraTypes()

MimeAnalyzer::addExtraTypes (   $types)

Adds to the list mapping MIME to file extensions.

As an extension author, you are encouraged to submit patches to MediaWiki's core to add new MIME types to MimeMap.php.

Parameters
string$types

Definition at line 256 of file MimeAnalyzer.php.

◆ detectMicrosoftBinaryType()

MimeAnalyzer::detectMicrosoftBinaryType (   $handle)
private

Detect the type of a Microsoft Compound Binary a.k.a.

OLE file. These are old style pre-ODF files such as .doc and .xls

Parameters
resource$handleAn opened seekable file handle
Returns
string The detected MIME type

Definition at line 892 of file MimeAnalyzer.php.

References MSCompoundFileReader\readHandle().

Referenced by doGuessMimeType().

◆ detectMimeType()

MimeAnalyzer::detectMimeType (   $file,
  $ext = true 
)
private

Internal MIME type detection.

Detection is done using the fileinfo extension if it is available. It can be overriden by callback, which could use an external program, for example. If detection fails and $ext is not false, the MIME type is guessed from the file extension, using getMimeTypeFromExtensionOrNull.

If the MIME type is still unknown, getimagesize is used to detect the MIME type if the file is an image. If no MIME type can be determined, this function returns 'unknown/unknown'.

Parameters
string$fileThe file to check
string | bool$extThe file extension, or true (default) to extract it from the filename. Set it to false to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string The MIME type of $file

Definition at line 922 of file MimeAnalyzer.php.

References $detectCallback, $ext, $file, getMimeTypeFromExtensionOrNull(), and isRecognizableExtension().

Referenced by guessMimeType().

◆ detectZipType()

MimeAnalyzer::detectZipType (   $header,
  $tail = null,
  $ext = false 
)

Detect application-specific file type of a given ZIP file from its header data.

Currently works for OpenDocument and OpenXML types... If can't tell, returns 'application/zip'.

Parameters
string$headerSome reasonably-sized chunk of file header
string | null$tailThe tail of the file
string | bool$extThe file extension, or true to extract it from the filename. Set it to false (default) to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string

Definition at line 784 of file MimeAnalyzer.php.

References $ext, $header, $matches, $mime, getMimeTypeFromExtensionOrNull(), and isMatchingExtension().

Referenced by doGuessMimeType().

◆ doGuessMimeType()

MimeAnalyzer::doGuessMimeType (   $file,
  $ext 
)
private

Guess the MIME type from the file contents.

Todo:
Remove $ext param
Parameters
string$file
mixed$ext
Returns
bool|string
Exceptions
UnexpectedValueException

Definition at line 546 of file MimeAnalyzer.php.

References $ext, $file, $guessCallback, $mime, $xmlTypes, detectMicrosoftBinaryType(), and detectZipType().

Referenced by guessMimeType().

◆ findMediaType()

MimeAnalyzer::findMediaType (   $extMime)

Returns a media code matching the given MIME type or file extension.

File extensions are represented by a string starting with a dot (.) to distinguish them from MIME types.

This function relies on the mapping defined by $this->mMediaTypes

Definition at line 1080 of file MimeAnalyzer.php.

References $mime, $type, getTypesForExtension(), and MEDIATYPE_UNKNOWN.

Referenced by getMediaType(), and improveTypeFromExtension().

◆ getExtensionFromMimeTypeOrNull()

MimeAnalyzer::getExtensionFromMimeTypeOrNull (   $mime)

Returns a single file extension for a given MIME type or null if unknown.

This is always the first type from the list returned by getExtensionsFromMimeType($mime).

Since
1.35
Parameters
string$mime
Returns
string|null

Definition at line 361 of file MimeAnalyzer.php.

References $mime, and getExtensionsFromMimeType().

◆ getExtensionsForType()

MimeAnalyzer::getExtensionsForType (   $mime)

Returns a list of file extensions for a given MIME type as a space separated string or null if the MIME type was unrecognized.

Resolves MIME type aliases.

Deprecated:
since 1.35 Use getExtensionsFromMimeType() instead.
Parameters
string$mime
Returns
string|null

Definition at line 279 of file MimeAnalyzer.php.

References $mime, and getExtensionsFromMimeType().

◆ getExtensionsFromMimeType()

MimeAnalyzer::getExtensionsFromMimeType (   $mime)

Returns an array of file extensions associated with a given MIME type.

The returned array is empty if the MIME type was unrecognized. Resolves MIME type aliases.

Since
1.35
Parameters
string$mime
Returns
array

Definition at line 293 of file MimeAnalyzer.php.

References $mime.

Referenced by getExtensionFromMimeTypeOrNull(), getExtensionsForType(), and isMatchingExtension().

◆ getIEContentAnalyzer()

MimeAnalyzer::getIEContentAnalyzer ( )
protected

Get a cached instance of IEContentAnalyzer.

Returns
IEContentAnalyzer

Definition at line 1137 of file MimeAnalyzer.php.

References $IEAnalyzer.

Referenced by getIEMimeTypes().

◆ getIEMimeTypes()

MimeAnalyzer::getIEMimeTypes (   $fileName,
  $chunk,
  $proposed 
)

Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content.

Parameters
string$fileNameThe file name (unused at present)
string$chunkThe first 256 bytes of the file
string$proposedThe MIME type proposed by the server
Returns
array

Definition at line 1127 of file MimeAnalyzer.php.

References getIEContentAnalyzer().

◆ getMediaType()

MimeAnalyzer::getMediaType (   $path = null,
  $mime = null 
)

Determine the media type code for a file, using its MIME type, name and possibly its contents.

This function relies on the findMediaType(), mapping extensions and MIME types to media types.

Todo:

analyse file if need be

look at multiple extension, separately and together.

Parameters
string | null$pathFull path to the image file, in case we have to look at the contents (if null, only the MIME type is used to determine the media type code).
string | null$mimeMIME type. If null it will be guessed using guessMimeType.
Returns
string A value to be used with the MEDIATYPE_xxx constants.

Definition at line 991 of file MimeAnalyzer.php.

References $mime, $path, $type, findMediaType(), guessMimeType(), MEDIATYPE_AUDIO, MEDIATYPE_MULTIMEDIA, MEDIATYPE_UNKNOWN, and MEDIATYPE_VIDEO.

◆ getMediaTypes()

MimeAnalyzer::getMediaTypes ( )

Returns an array of media types (MEDIATYPE_xxx constants)

Returns
array

Definition at line 1114 of file MimeAnalyzer.php.

◆ getMimeTypeFromExtensionOrNull()

MimeAnalyzer::getMimeTypeFromExtensionOrNull (   $ext)

Returns a single MIME type for a given file extension or null if unknown.

This is always the first type from the list returned by getMimeTypesFromExtension($ext).

Since
1.35
Parameters
string$ext
Returns
string|null

Definition at line 323 of file MimeAnalyzer.php.

References $ext, and getMimeTypesFromExtension().

Referenced by detectMimeType(), detectZipType(), guessTypesForExtension(), and improveTypeFromExtension().

◆ getMimeTypesFromExtension()

MimeAnalyzer::getMimeTypesFromExtension (   $ext)

Returns an array of MIME types associated with a given file extension.

The returned array is empty if the file extension is not associated with any MIME types.

Since
1.35
Parameters
string$ext
Returns
array

Definition at line 310 of file MimeAnalyzer.php.

References $ext.

Referenced by getMimeTypeFromExtensionOrNull(), and getTypesForExtension().

◆ getTypesForExtension()

MimeAnalyzer::getTypesForExtension (   $ext)

Returns a list of MIME types for a given file extension as a space separated string or null if the extension was unrecognized.

Deprecated:
since 1.35 Use getMimeTypesFromExtension() instead.
Parameters
string$ext
Returns
string|null

Definition at line 348 of file MimeAnalyzer.php.

References $ext, and getMimeTypesFromExtension().

Referenced by findMediaType().

◆ guessMimeType()

MimeAnalyzer::guessMimeType (   $file,
  $ext = true 
)

MIME type detection.

This uses detectMimeType to detect the MIME type of the file, but applies additional checks to determine some well known file formats that may be missed or misinterpreted by the default MIME detection (namely XML based formats like XHTML or SVG, as well as ZIP based formats like OPC/ODF files).

Parameters
string$fileThe file to check
string | bool$extThe file extension, or true (default) to extract it from the filename. Set it to false to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string The MIME type of $file

Definition at line 513 of file MimeAnalyzer.php.

References $ext, $file, $mime, detectMimeType(), and doGuessMimeType().

Referenced by getMediaType().

◆ guessTypesForExtension()

MimeAnalyzer::guessTypesForExtension (   $ext)

Returns a single file extension for a given MIME type or null if unknown.

This is always the first type from the list returned by getExtensionsFromMimeType($mime).

Deprecated:
since 1.35 Use getMimeTypeFromExtensionOrNull() instead.
Parameters
string$ext
Returns
string|null

Definition at line 336 of file MimeAnalyzer.php.

References $ext, and getMimeTypeFromExtensionOrNull().

◆ improveTypeFromExtension()

MimeAnalyzer::improveTypeFromExtension (   $mime,
  $ext 
)

Improves a MIME type using the file extension.

Some file formats are very generic, so their MIME type is not very meaningful. A more useful MIME type can be derived by looking at the file extension. Typically, this method would be called on the result of guessMimeType().

XXX: Null-returning behavior is probably an accident and definitely confusing (T253483).

Parameters
string$mimeThe MIME type, typically guessed from a file's content.
string$extThe file extension, as taken from the file name
Returns
string|null The improved MIME type, or null if the MIME type is unknown/unknown and the extension is not recognized.

Definition at line 456 of file MimeAnalyzer.php.

References $ext, $extCallback, $mime, findMediaType(), getMimeTypeFromExtensionOrNull(), isMatchingExtension(), isRecognizableExtension(), and MEDIATYPE_TEXT.

◆ isMatchingExtension()

MimeAnalyzer::isMatchingExtension (   $extension,
  $mime 
)

Tests if the extension matches the given MIME type.

Returns true if a match was found, null if the MIME type is unknown, and false if the MIME type is known but no matches where found.

Parameters
string$extension
string$mime
Returns
bool|null

Definition at line 375 of file MimeAnalyzer.php.

References $mime, and getExtensionsFromMimeType().

Referenced by detectZipType(), and improveTypeFromExtension().

◆ isPHPImageType()

MimeAnalyzer::isPHPImageType (   $mime)

Returns true if the MIME type is known to represent an image format supported by the PHP GD library.

Parameters
string$mime
Returns
bool

Definition at line 393 of file MimeAnalyzer.php.

References $mime.

◆ isRecognizableExtension()

MimeAnalyzer::isRecognizableExtension (   $extension)

Returns true if the extension represents a type which can be reliably detected from its content.

Use this to determine whether strict content checks should be applied to reject invalid uploads; if we can't identify the type we won't be able to say if it's invalid.

Todo:
Be more accurate when using fancy MIME detector plugins; right now this is the bare minimum getimagesize() list.
Parameters
string$extension
Returns
bool

Definition at line 420 of file MimeAnalyzer.php.

Referenced by detectMimeType(), and improveTypeFromExtension().

◆ loadFiles()

MimeAnalyzer::loadFiles ( )
protected

Definition at line 102 of file MimeAnalyzer.php.

References $ext, $extraInfo, $extraTypes, $mime, parseMimeInfo(), and parseMimeTypes().

Referenced by __construct().

◆ parseMimeInfo()

MimeAnalyzer::parseMimeInfo (   $rawMimeInfo)
protected

Definition at line 189 of file MimeAnalyzer.php.

References $lines, $mime, $s, and MEDIATYPE_UNKNOWN.

Referenced by loadFiles().

◆ parseMimeTypes()

MimeAnalyzer::parseMimeTypes (   $rawMimeTypes)
protected

Definition at line 152 of file MimeAnalyzer.php.

References $ext, $lines, $mime, and $s.

Referenced by loadFiles().

◆ setLogger()

MimeAnalyzer::setLogger ( LoggerInterface  $logger)

Definition at line 246 of file MimeAnalyzer.php.

References $logger.

Member Data Documentation

◆ $detectCallback

callable MimeAnalyzer::$detectCallback
protected

Definition at line 43 of file MimeAnalyzer.php.

Referenced by detectMimeType().

◆ $extCallback

callable MimeAnalyzer::$extCallback
protected

Definition at line 47 of file MimeAnalyzer.php.

Referenced by improveTypeFromExtension().

◆ $extraInfo

string MimeAnalyzer::$extraInfo = ''
private

Extra MIME info, set for example by media handling extensions.

Definition at line 66 of file MimeAnalyzer.php.

Referenced by loadFiles().

◆ $extraTypes

string MimeAnalyzer::$extraTypes = ''
private

Extra MIME types, set for example by media handling extensions.

Definition at line 64 of file MimeAnalyzer.php.

Referenced by loadFiles().

◆ $extToMimes

array<string,string[]> MimeAnalyzer::$extToMimes = []
protected

Map of file extensions to MIME types.

Definition at line 55 of file MimeAnalyzer.php.

◆ $guessCallback

callable MimeAnalyzer::$guessCallback
protected

Definition at line 45 of file MimeAnalyzer.php.

Referenced by doGuessMimeType().

◆ $IEAnalyzer

IEContentAnalyzer MimeAnalyzer::$IEAnalyzer
protected

Definition at line 61 of file MimeAnalyzer.php.

Referenced by getIEContentAnalyzer().

◆ $infoFile

string MimeAnalyzer::$infoFile
protected

Definition at line 37 of file MimeAnalyzer.php.

◆ $initCallback

callable MimeAnalyzer::$initCallback
protected

Definition at line 41 of file MimeAnalyzer.php.

◆ $logger

LoggerInterface MimeAnalyzer::$logger
private

Definition at line 69 of file MimeAnalyzer.php.

Referenced by setLogger().

◆ $mediaTypes

array MimeAnalyzer::$mediaTypes = null
protected

Mapping of media types to arrays of MIME types.

Definition at line 49 of file MimeAnalyzer.php.

◆ $mExtToMime

array MimeAnalyzer::$mExtToMime = []

Map of file extensions types to MIME types (as a space separated list)

Definition at line 58 of file MimeAnalyzer.php.

◆ $mimeToExts

array<string,string[]> MimeAnalyzer::$mimeToExts = []
protected

Map of MIME types to file extensions.

Definition at line 53 of file MimeAnalyzer.php.

◆ $mimeTypeAliases

array MimeAnalyzer::$mimeTypeAliases = null
protected

Map of MIME type aliases.

Definition at line 51 of file MimeAnalyzer.php.

◆ $typeFile

string MimeAnalyzer::$typeFile
protected

Definition at line 35 of file MimeAnalyzer.php.

◆ $xmlTypes

string MimeAnalyzer::$xmlTypes
protected

Definition at line 39 of file MimeAnalyzer.php.

Referenced by doGuessMimeType().


The documentation for this class was generated from the following file: