MimeMagic Class Reference

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

List of all members.

Public Member Functions

 __construct ()
 Initializes the MimeMagic object.
 getExtensionsForType ($mime)
 returns a list of file extensions for a given mime type as a space separated string.
 getTypesForExtension ($ext)
 returns a list of mime types for a given file extension as a space separated string.
 guessTypesForExtension ($ext)
 returns a single mime type for a given file extension.
 isMatchingExtension ($extension, $mime)
 tests if the extension matches the given mime type.
 isPHPImageType ($mime)
 returns true if the mime type is known to represent an image format supported by the PHP GD library.
 isRecognizableExtension ($extension)
 Returns true if the extension represents a type which can be reliably detected from its content.
 guessMimeType ($file, $ext=true)
 mime type detection.
 doGuessMimeType ($file, $ext=true)
 detectZipType ($header)
 Detect application-specific file type of a given ZIP file from its header data.
 detectMimeType ($file, $ext=true)
 Internal mime type detection, please use guessMimeType() for application code instead.
 getMediaType ($path=NULL, $mime=NULL)
 Determine the media type code for a file, using its mime type, name and possibly its contents.
 findMediaType ($extMime)
 returns a media code matching the given mime type or file extension.
 getIEMimeTypes ($fileName, $chunk, $proposed)
 Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content.

Static Public Member Functions

static & singleton ()
 Get an instance of this class.

Public Attributes

 $mMediaTypes = NULL
 Mapping of media types to arrays of mime types.
 $mMimeTypeAliases = NULL
 Map of mime type aliases.
 $mMimeToExt = NULL
 map of mime types to file extensions (as a space seprarated list)
 $mExtToMime = NULL
 map of file extensions types to mime types (as a space seprarated list)
 $mIEAnalyzer
 IEContentAnalyzer instance.

Protected Member Functions

 getIEContentAnalyzer ()
 Get a cached instance of IEContentAnalyzer.

Static Private Attributes

static $instance
 The singleton instance.


Detailed Description

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

Instances of this class are stateles, there only needs to be one global instance of MimeMagic. Please use MimeMagic::singleton() to get that instance.

Definition at line 115 of file MimeMagic.php.


Constructor & Destructor Documentation

MimeMagic::__construct (  ) 

Initializes the MimeMagic object.

This is called by MimeMagic::singleton().

This constructor parses the mime.types and mime.info files and build internal mappings.

Definition at line 147 of file MimeMagic.php.

References $ext, $i, $IP, $s, $wgMimeInfoFile, $wgMimeTypeFile, and wfDebug().


Member Function Documentation

MimeMagic::detectMimeType ( file,
ext = true 
)

Internal mime type detection, please use guessMimeType() for application code instead.

Detection is done using an external program, if $wgMimeDetectorCommand is set. Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available. If the dections fails and $ext is not false, the mime type is guessed from the file extension, using guessTypesForExtension. 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 $file The file to check
mixed $ext The file extension, or true to extract it from the filename. Set it to false to ignore the extension.
Returns:
string the mime type of $file
Access:
private

Definition at line 629 of file MimeMagic.php.

References $ext, $file, $i, $wgMimeDetectorCommand, guessTypesForExtension(), isRecognizableExtension(), wfDebug(), and wfEscapeShellArg().

Referenced by guessMimeType().

MimeMagic::detectZipType ( header  ) 

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

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

Parameters:
string $header Some reasonably-sized chunk of file header
Returns:
string

Definition at line 580 of file MimeMagic.php.

References wfDebug().

Referenced by doGuessMimeType().

MimeMagic::doGuessMimeType ( file,
ext = true 
)

MimeMagic::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 funktion relies on the mapping defined by $this->mMediaTypes private

Definition at line 790 of file MimeMagic.php.

Referenced by getMediaType().

MimeMagic::getExtensionsForType ( mime  ) 

returns a list of file extensions for a given mime type as a space separated string.

Definition at line 303 of file MimeMagic.php.

Referenced by isMatchingExtension().

MimeMagic::getIEContentAnalyzer (  )  [protected]

Get a cached instance of IEContentAnalyzer.

Definition at line 832 of file MimeMagic.php.

MimeMagic::getIEMimeTypes ( fileName,
chunk,
proposed 
)

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

Parameters:
string $fileName The file name (unused at present)
string $chunk The first 256 bytes of the file
string $proposed The MIME type proposed by the server

Definition at line 824 of file MimeMagic.php.

MimeMagic::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 $path full 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 $mime mime type. If null it will be guessed using guessMimeType.
Returns:
(int?string?) a value to be used with the MEDIATYPE_xxx constants.

Definition at line 724 of file MimeMagic.php.

References $i, $path, $type, findMediaType(), and guessMimeType().

MimeMagic::getTypesForExtension ( ext  ) 

returns a list of mime types for a given file extension as a space separated string.

Definition at line 319 of file MimeMagic.php.

References $ext.

Referenced by guessTypesForExtension().

MimeMagic::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 misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).

Parameters:
string $file The file to check
mixed $ext The file extension, or true to extract it from the filename. Set it to false to ignore the extension.
Returns:
string the mime type of $file

Definition at line 418 of file MimeMagic.php.

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

Referenced by getMediaType().

MimeMagic::guessTypesForExtension ( ext  ) 

returns a single mime type for a given file extension.

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

Definition at line 329 of file MimeMagic.php.

References $ext, and getTypesForExtension().

Referenced by detectMimeType().

MimeMagic::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.

Definition at line 344 of file MimeMagic.php.

References $ext, and getExtensionsForType().

MimeMagic::isPHPImageType ( mime  ) 

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

Definition at line 364 of file MimeMagic.php.

MimeMagic::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.
Returns:
bool

Definition at line 390 of file MimeMagic.php.

Referenced by detectMimeType().

static& MimeMagic::singleton (  )  [static]


Member Data Documentation

MimeMagic::$instance [static, private]

The singleton instance.

Definition at line 141 of file MimeMagic.php.

MimeMagic::$mExtToMime = NULL

map of file extensions types to mime types (as a space seprarated list)

Definition at line 133 of file MimeMagic.php.

MimeMagic::$mIEAnalyzer

IEContentAnalyzer instance.

Definition at line 137 of file MimeMagic.php.

MimeMagic::$mMediaTypes = NULL

Mapping of media types to arrays of mime types.

This is used by findMediaType and getMediaType, respectively

Definition at line 121 of file MimeMagic.php.

MimeMagic::$mMimeToExt = NULL

map of mime types to file extensions (as a space seprarated list)

Definition at line 129 of file MimeMagic.php.

MimeMagic::$mMimeTypeAliases = NULL

Map of mime type aliases.

Definition at line 125 of file MimeMagic.php.


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

Generated on Sat Sep 5 02:08:42 2009 for MediaWiki by  doxygen 1.5.9