diff options
Diffstat (limited to 'includes/MimeMagic.php')
-rw-r--r-- | includes/MimeMagic.php | 329 |
1 files changed, 163 insertions, 166 deletions
diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php index 1873e7bf..8220e92f 100644 --- a/includes/MimeMagic.php +++ b/includes/MimeMagic.php @@ -25,21 +25,21 @@ * This is used as a fallback to mime.types files. * An extensive list of well known mime types is provided by * the file mime.types in the includes directory. - * + * * This list concatenated with mime.types is used to create a mime <-> ext * map. Each line contains a mime type followed by a space separated list of - * extensions. If multiple extensions for a single mime type exist or if + * extensions. If multiple extensions for a single mime type exist or if * multiple mime types exist for a single extension then in most cases * MediaWiki assumes that the first extension following the mime type is the * canonical extension, and the first time a mime type appears for a certain * extension is considered the canonical mime type. - * + * * (Note that appending $wgMimeTypeFile to the end of MM_WELL_KNOWN_MIME_TYPES - * sucks because you can't redefine canonical types. This could be fixed by + * sucks because you can't redefine canonical types. This could be fixed by * appending MM_WELL_KNOWN_MIME_TYPES behind $wgMimeTypeFile, but who knows * what will break? In practice this probably isn't a problem anyway -- Bryan) */ -define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING +define( 'MM_WELL_KNOWN_MIME_TYPES', <<<END_STRING application/ogg ogx ogg ogm ogv oga spx application/pdf pdf application/vnd.oasis.opendocument.chart odc @@ -70,7 +70,7 @@ image/x-bmp bmp image/gif gif image/jpeg jpeg jpg jpe image/png png -image/svg+xml svg +image/svg+xml svg image/svg svg image/tiff tiff tif image/vnd.djvu djvu @@ -91,7 +91,7 @@ END_STRING * An extensive list of well known mime types is provided by * the file mime.info in the includes directory. */ -define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING +define( 'MM_WELL_KNOWN_MIME_INFO', <<<END_STRING application/pdf [OFFICE] application/vnd.oasis.opendocument.chart [OFFICE] application/vnd.oasis.opendocument.chart-template [OFFICE] @@ -144,21 +144,21 @@ END_STRING class MimeMagic { /** - * Mapping of media types to arrays of mime types. - * This is used by findMediaType and getMediaType, respectively - */ + * Mapping of media types to arrays of mime types. + * This is used by findMediaType and getMediaType, respectively + */ var $mMediaTypes = null; /** Map of mime type aliases - */ + */ var $mMimeTypeAliases = null; - /** map of mime types to file extensions (as a space seprarated list) - */ + /** map of mime types to file extensions (as a space separated list) + */ var $mMimeToExt = null; - /** map of file extensions types to mime types (as a space seprarated list) - */ + /** map of file extensions types to mime types (as a space separated list) + */ var $mExtToMime = null; /** IEContentAnalyzer instance @@ -169,20 +169,16 @@ class MimeMagic { */ private static $instance; - /** True if the fileinfo extension has been loaded - */ - private static $extensionLoaded = false; - /** Initializes the MimeMagic object. This is called by MimeMagic::singleton(). * * This constructor parses the mime.types and mime.info files and build internal mappings. */ function __construct() { /** - * --- load mime.types --- - */ + * --- load mime.types --- + */ - global $wgMimeTypeFile, $IP, $wgLoadFileinfoExtension; + global $wgMimeTypeFile, $IP; $types = MM_WELL_KNOWN_MIME_TYPES; @@ -190,21 +186,16 @@ class MimeMagic { $wgMimeTypeFile = "$IP/$wgMimeTypeFile"; } - if ( $wgLoadFileinfoExtension && !self::$extensionLoaded ) { - self::$extensionLoaded = true; - wfDl( 'fileinfo' ); - } - if ( $wgMimeTypeFile ) { if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) { - wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" ); + wfDebug( __METHOD__ . ": loading mime types from $wgMimeTypeFile\n" ); $types .= "\n"; $types .= file_get_contents( $wgMimeTypeFile ); } else { - wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" ); + wfDebug( __METHOD__ . ": can't load mime types from $wgMimeTypeFile\n" ); } } else { - wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" ); + wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" ); } $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types ); @@ -213,7 +204,7 @@ class MimeMagic { $this->mMimeToExt = array(); $this->mToMime = array(); - $lines = explode( "\n",$types ); + $lines = explode( "\n", $types ); foreach ( $lines as $s ) { $s = trim( $s ); if ( empty( $s ) ) { @@ -231,7 +222,7 @@ class MimeMagic { } $mime = substr( $s, 0, $i ); - $ext = trim( substr($s, $i+1 ) ); + $ext = trim( substr( $s, $i + 1 ) ); if ( empty( $ext ) ) { continue; @@ -272,17 +263,17 @@ class MimeMagic { if ( $wgMimeInfoFile ) { if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) { - wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" ); + wfDebug( __METHOD__ . ": loading mime info from $wgMimeInfoFile\n" ); $info .= "\n"; $info .= file_get_contents( $wgMimeInfoFile ); } else { - wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n"); + wfDebug( __METHOD__ . ": can't load mime info from $wgMimeInfoFile\n" ); } } else { - wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n"); + wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" ); } - $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info); + $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info ); $info = str_replace( "\t", " ", $info ); $this->mMimeTypeAliases = array(); @@ -330,9 +321,9 @@ class MimeMagic { $this->mMediaTypes[$mtype][] = $mime; } - if ( sizeof( $m ) > 1 ) { + if ( count( $m ) > 1 ) { $main = $m[0]; - for ( $i=1; $i<sizeof($m); $i += 1 ) { + for ( $i = 1; $i < count( $m ); $i += 1 ) { $mime = $m[$i]; $this->mMimeTypeAliases[$mime] = $main; } @@ -346,17 +337,17 @@ class MimeMagic { * @return MimeMagic */ public static function &singleton() { - if ( !isset( self::$instance ) ) { + if ( self::$instance === null ) { self::$instance = new MimeMagic; } return self::$instance; } - /** - * Returns a list of file extensions for a given mime type as a space + /** + * 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. - * + * * @param $mime string * @return string|null */ @@ -379,10 +370,10 @@ class MimeMagic { return null; } - /** - * Returns a list of mime types for a given file extension as a space + /** + * Returns a list of mime types for a given file extension as a space * separated string or null if the extension was unrecognized. - * + * * @param $ext string * @return string|null */ @@ -393,10 +384,10 @@ class MimeMagic { return $r; } - /** + /** * 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 getTypesForExtension($ext). - * + * * @param $ext string * @return string|null */ @@ -413,12 +404,11 @@ class MimeMagic { return $m; } - - /** - * 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 + /** + * 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. - * + * * @param $extension string * @param $mime string * @return bool|null @@ -427,21 +417,21 @@ class MimeMagic { $ext = $this->getExtensionsForType( $mime ); if ( !$ext ) { - return null; // Unknown mime type + return null; // Unknown mime type } $ext = explode( ' ', $ext ); $extension = strtolower( $extension ); - return in_array( $extension, $ext ); + return in_array( $extension, $ext ); } - /** - * Returns true if the mime type is known to represent an image format + /** + * Returns true if the mime type is known to represent an image format * supported by the PHP GD library. * * @param $mime string - * + * * @return bool */ public function isPHPImageType( $mime ) { @@ -489,32 +479,32 @@ class MimeMagic { return in_array( strtolower( $extension ), $types ); } - /** + /** * 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 + * 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(). - * + * * Currently, this method does the following: * * If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false, - * return the result of guessTypesForExtension($ext). + * return the result of guessTypesForExtension($ext). * * If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime ) - * gives true, return the result of guessTypesForExtension($ext). + * gives true, return the result of guessTypesForExtension($ext). * - * @param $mime String: the mime type, typically guessed from a file's content. - * @param $ext String: the file extension, as taken from the file name + * @param string $mime the mime type, typically guessed from a file's content. + * @param string $ext the file extension, as taken from the file name * * @return string the mime type */ public function improveTypeFromExtension( $mime, $ext ) { if ( $mime === 'unknown/unknown' ) { if ( $this->isRecognizableExtension( $ext ) ) { - wfDebug( __METHOD__. ': refusing to guess mime type for .' . + wfDebug( __METHOD__ . ': refusing to guess mime type for .' . "$ext file, we should have recognized it\n" ); } else { - // Not something we can detect, so simply + // Not something we can detect, so simply // trust the file extension $mime = $this->guessTypesForExtension( $ext ); } @@ -525,7 +515,7 @@ class MimeMagic { // find the proper mime type for that file extension $mime = $this->guessTypesForExtension( $ext ); } else { - wfDebug( __METHOD__. ": refusing to guess better type for $mime file, " . + wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " . ".$ext is not a known OPC extension.\n" ); $mime = 'application/zip'; } @@ -535,34 +525,34 @@ class MimeMagic { $mime = $this->mMimeTypeAliases[$mime]; } - wfDebug(__METHOD__.": improved mime type for .$ext: $mime\n"); + wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" ); return $mime; } - /** - * 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, as well as ZIP + /** + * 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). * - * @param $file String: the file to check + * @param string $file the file to check * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. - * Set it to false to ignore the extension. DEPRECATED! Set to false, use + * Set it to false to ignore the extension. DEPRECATED! Set to false, use * improveTypeFromExtension($mime, $ext) later to improve mime type. * * @return string the mime type of $file */ public function guessMimeType( $file, $ext = true ) { if ( $ext ) { // TODO: make $ext default to false. Or better, remove it. - wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " . + wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); } $mime = $this->doGuessMimeType( $file, $ext ); - if( !$mime ) { - wfDebug( __METHOD__.": internal type detection failed for $file (.$ext)...\n" ); + if ( !$mime ) { + wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" ); $mime = $this->detectMimeType( $file, $ext ); } @@ -570,7 +560,7 @@ class MimeMagic { $mime = $this->mMimeTypeAliases[$mime]; } - wfDebug(__METHOD__.": guessed mime type of $file: $mime\n"); + wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" ); return $mime; } @@ -587,8 +577,8 @@ class MimeMagic { // @todo FIXME: Shouldn't this be rb? $f = fopen( $file, 'rt' ); wfRestoreWarnings(); - - if( !$f ) { + + if ( !$f ) { return 'unknown/unknown'; } $head = fread( $f, 1024 ); @@ -629,7 +619,7 @@ class MimeMagic { $doctype = strpos( $head, "\x42\x82" ); if ( $doctype ) { // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers) - $data = substr($head, $doctype+3, 8); + $data = substr( $head, $doctype + 3, 8 ); if ( strncmp( $data, "matroska", 8 ) == 0 ) { wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" ); return "video/x-matroska"; @@ -643,7 +633,7 @@ class MimeMagic { } /* Look for WebP */ - if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) { + if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8 ), "WEBPVP8 ", 8 ) == 0 ) { wfDebug( __METHOD__ . ": recognized file as image/webp\n" ); return "image/webp"; } @@ -661,12 +651,11 @@ class MimeMagic { * strings like "<? ", but should it be axed completely? */ if ( ( strpos( $head, '<?php' ) !== false ) || - - ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) || - ( strpos( $head, "<\x00?\x00 " ) !== false ) || - ( strpos( $head, "<\x00?\x00\n" ) !== false ) || - ( strpos( $head, "<\x00?\x00\t" ) !== false ) || - ( strpos( $head, "<\x00?\x00=" ) !== false ) ) { + ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) || + ( strpos( $head, "<\x00?\x00 " ) !== false ) || + ( strpos( $head, "<\x00?\x00\n" ) !== false ) || + ( strpos( $head, "<\x00?\x00\t" ) !== false ) || + ( strpos( $head, "<\x00?\x00=" ) !== false ) ) { wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" ); return 'application/x-php'; @@ -691,24 +680,24 @@ class MimeMagic { $script_type = null; # detect by shebang - if ( substr( $head, 0, 2) == "#!" ) { + if ( substr( $head, 0, 2 ) == "#!" ) { $script_type = "ASCII"; - } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) { + } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) { $script_type = "UTF-8"; - } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) { + } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) { $script_type = "UTF-16BE"; } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) { - $script_type= "UTF-16LE"; + $script_type = "UTF-16LE"; } if ( $script_type ) { - if ( $script_type !== "UTF-8" && $script_type !== "ASCII") { + if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) { // Quick and dirty fold down to ASCII! $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ); $chars = unpack( $pack[$script_type], substr( $head, 2 ) ); $head = ''; - foreach( $chars as $codepoint ) { - if( $codepoint < 128 ) { + foreach ( $chars as $codepoint ) { + if ( $codepoint < 128 ) { $head .= chr( $codepoint ); } else { $head .= '?'; @@ -720,14 +709,14 @@ class MimeMagic { if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) { $mime = "application/x-{$match[2]}"; - wfDebug( __METHOD__.": shell script recognized as $mime\n" ); + wfDebug( __METHOD__ . ": shell script recognized as $mime\n" ); return $mime; } } // Check for ZIP variants (before getimagesize) if ( strpos( $tail, "PK\x05\x06" ) !== false ) { - wfDebug( __METHOD__.": ZIP header present in $file\n" ); + wfDebug( __METHOD__ . ": ZIP header present in $file\n" ); return $this->detectZipType( $head, $tail, $ext ); } @@ -735,38 +724,38 @@ class MimeMagic { $gis = getimagesize( $file ); wfRestoreWarnings(); - if( $gis && isset( $gis['mime'] ) ) { + if ( $gis && isset( $gis['mime'] ) ) { $mime = $gis['mime']; - wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" ); + wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" ); return $mime; } // Also test DjVu $deja = new DjVuImage( $file ); - if( $deja->isValid() ) { - wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" ); + if ( $deja->isValid() ) { + wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" ); return 'image/vnd.djvu'; } return 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'. * - * @param $header String: some reasonably-sized chunk of file header + * @param string $header some reasonably-sized chunk of file header * @param $tail String: the tail of the file * @param $ext Mixed: the file extension, or true to extract it from the filename. - * Set it to false (default) to ignore the extension. DEPRECATED! Set to false, + * Set it to false (default) to ignore the extension. DEPRECATED! Set to false, * use improveTypeFromExtension($mime, $ext) later to improve mime type. * * @return string */ function detectZipType( $header, $tail = null, $ext = false ) { - if( $ext ) { # TODO: remove $ext param - wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " . + if ( $ext ) { # TODO: remove $ext param + wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); } @@ -797,31 +786,32 @@ class MimeMagic { if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) { $mime = $matches[1]; - wfDebug( __METHOD__.": detected $mime from ZIP archive\n" ); + wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" ); } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) { $mime = "application/x-opc+zip"; - # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere - if ( $ext !== true && $ext !== false ) { + # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere + if ( $ext !== true && $ext !== false ) { /** This is the mode used by getPropsFromPath - * These mime's are stored in the database, where we don't really want - * x-opc+zip, because we use it only for internal purposes - */ - if ( $this->isMatchingExtension( $ext, $mime) ) { + * These mime's are stored in the database, where we don't really want + * x-opc+zip, because we use it only for internal purposes + */ + if ( $this->isMatchingExtension( $ext, $mime ) ) { /* A known file extension for an OPC file, - * find the proper mime type for that file extension */ + * find the proper mime type for that file extension + */ $mime = $this->guessTypesForExtension( $ext ); } else { $mime = "application/zip"; } } - wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" ); - } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && - ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false && + wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" ); + } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && + ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false && preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) { - if ( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) { + if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) { $mime = "application/msword"; - } - switch( substr( $header, 512, 6) ) { + } + switch ( substr( $header, 512, 6 ) ) { case "\xEC\xA5\xC1\x00\x0E\x00": case "\xEC\xA5\xC1\x00\x1C\x00": case "\xEC\xA5\xC1\x00\x43\x00": @@ -843,27 +833,27 @@ class MimeMagic { break; } - wfDebug( __METHOD__.": detected a MS Office document with OPC trailer\n"); + wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" ); } else { - wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" ); + wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" ); } return $mime; } - /** - * Internal mime type detection. 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 + /** + * Internal mime type detection. 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 detections 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, + * + * 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'. * - * @param $file String: the file to check + * @param string $file the file to check * @param $ext Mixed: the file extension, or true (default) to extract it from the filename. - * Set it to false to ignore the extension. DEPRECATED! Set to false, use + * Set it to false to ignore the extension. DEPRECATED! Set to false, use * improveTypeFromExtension($mime, $ext) later to improve mime type. * * @return string the mime type of $file @@ -872,14 +862,13 @@ class MimeMagic { global $wgMimeDetectorCommand; if ( $ext ) { # TODO: make $ext default to false. Or better, remove it. - wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); + wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); } $m = null; if ( $wgMimeDetectorCommand ) { - // @todo FIXME: Use wfShellExec - $fn = wfEscapeShellArg( $file ); - $m = `$wgMimeDetectorCommand $fn`; + $args = wfEscapeShellArg( $file ); + $m = wfShellExec( "$wgMimeDetectorCommand $args" ); } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) { # This required the fileinfo extension by PECL, @@ -898,22 +887,22 @@ class MimeMagic { $m = finfo_file( $mime_magic_resource, $file ); finfo_close( $mime_magic_resource ); } else { - wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" ); + wfDebug( __METHOD__ . ": finfo_open failed on " . FILEINFO_MIME . "!\n" ); } } elseif ( function_exists( "mime_content_type" ) ) { # NOTE: this function is available since PHP 4.3.0, but only if # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic. # - # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP; + # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundled with PHP; # sometimes, this may even be needed under linus/unix. # # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above. # see http://www.php.net/manual/en/ref.mime-magic.php for details. - $m = mime_content_type($file); + $m = mime_content_type( $file ); } else { - wfDebug( __METHOD__.": no magic mime detector found!\n" ); + wfDebug( __METHOD__ . ": no magic mime detector found!\n" ); } if ( $m ) { @@ -925,7 +914,7 @@ class MimeMagic { if ( strpos( $m, 'unknown' ) !== false ) { $m = null; } else { - wfDebug( __METHOD__.": magic mime type of $file: $m\n" ); + wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" ); return $m; } } @@ -936,12 +925,12 @@ class MimeMagic { $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' ); } if ( $ext ) { - if( $this->isRecognizableExtension( $ext ) ) { - wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, we should have recognized it\n" ); + if ( $this->isRecognizableExtension( $ext ) ) { + wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" ); } else { $m = $this->guessTypesForExtension( $ext ); if ( $m ) { - wfDebug( __METHOD__.": extension mime type of $file: $m\n" ); + wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" ); return $m; } } @@ -962,19 +951,19 @@ class MimeMagic { * @todo analyse file if need be * @todo look at multiple extension, separately and together. * - * @param $path String: full path to the image file, in case we have to look at the contents + * @param 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). - * @param $mime String: mime type. If null it will be guessed using guessMimeType. + * @param string $mime mime type. If null it will be guessed using guessMimeType. * * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants. */ function getMediaType( $path = null, $mime = null ) { - if( !$mime && !$path ) { + if ( !$mime && !$path ) { return MEDIATYPE_UNKNOWN; } // If mime type is unknown, guess it - if( !$mime ) { + if ( !$mime ) { $mime = $this->guessMimeType( $path, false ); } @@ -984,22 +973,30 @@ class MimeMagic { // Read a chunk of the file $f = fopen( $path, "rt" ); - if ( !$f ) return MEDIATYPE_UNKNOWN; + if ( !$f ) { + return MEDIATYPE_UNKNOWN; + } $head = fread( $f, 256 ); fclose( $f ); $head = strtolower( $head ); // This is an UGLY HACK, file should be parsed correctly - if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO; - elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO; - elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO; - elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO; - else return MEDIATYPE_MULTIMEDIA; + if ( strpos( $head, 'theora' ) !== false ) { + return MEDIATYPE_VIDEO; + } elseif ( strpos( $head, 'vorbis' ) !== false ) { + return MEDIATYPE_AUDIO; + } elseif ( strpos( $head, 'flac' ) !== false ) { + return MEDIATYPE_AUDIO; + } elseif ( strpos( $head, 'speex' ) !== false ) { + return MEDIATYPE_AUDIO; + } else { + return MEDIATYPE_MULTIMEDIA; + } } // Check for entry for full mime type - if( $mime ) { + if ( $mime ) { $type = $this->findMediaType( $mime ); if ( $type !== MEDIATYPE_UNKNOWN ) { return $type; @@ -1030,24 +1027,24 @@ class MimeMagic { } } - if( !$type ) { + if ( !$type ) { $type = MEDIATYPE_UNKNOWN; } return $type; } - /** + /** * 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 + * This function relies on the mapping defined by $this->mMediaTypes * @access private * @return int|string */ function findMediaType( $extMime ) { - if ( strpos( $extMime, '.' ) === 0 ) { + if ( strpos( $extMime, '.' ) === 0 ) { // If it's an extension, look up the mime types $m = $this->getTypesForExtension( substr( $extMime, 1 ) ); if ( !$m ) { @@ -1066,7 +1063,7 @@ class MimeMagic { foreach ( $m as $mime ) { foreach ( $this->mMediaTypes as $type => $codes ) { - if ( in_array($mime, $codes, true ) ) { + if ( in_array( $mime, $codes, true ) ) { return $type; } } @@ -1076,12 +1073,12 @@ class MimeMagic { } /** - * Get the MIME types that various versions of Internet Explorer would + * Get the MIME types that various versions of Internet Explorer would * detect from a chunk of the content. * - * @param $fileName String: the file name (unused at present) - * @param $chunk String: the first 256 bytes of the file - * @param $proposed String: the MIME type proposed by the server + * @param string $fileName the file name (unused at present) + * @param string $chunk the first 256 bytes of the file + * @param string $proposed the MIME type proposed by the server * @return Array */ public function getIEMimeTypes( $fileName, $chunk, $proposed ) { |