MediaWiki  REL1_21
MimeMagic.php
Go to the documentation of this file.
00001 <?php
00042 define('MM_WELL_KNOWN_MIME_TYPES', <<<END_STRING
00043 application/ogg ogx ogg ogm ogv oga spx
00044 application/pdf pdf
00045 application/vnd.oasis.opendocument.chart odc
00046 application/vnd.oasis.opendocument.chart-template otc
00047 application/vnd.oasis.opendocument.database odb
00048 application/vnd.oasis.opendocument.formula odf
00049 application/vnd.oasis.opendocument.formula-template otf
00050 application/vnd.oasis.opendocument.graphics odg
00051 application/vnd.oasis.opendocument.graphics-template otg
00052 application/vnd.oasis.opendocument.image odi
00053 application/vnd.oasis.opendocument.image-template oti
00054 application/vnd.oasis.opendocument.presentation odp
00055 application/vnd.oasis.opendocument.presentation-template otp
00056 application/vnd.oasis.opendocument.spreadsheet ods
00057 application/vnd.oasis.opendocument.spreadsheet-template ots
00058 application/vnd.oasis.opendocument.text odt
00059 application/vnd.oasis.opendocument.text-master otm
00060 application/vnd.oasis.opendocument.text-template ott
00061 application/vnd.oasis.opendocument.text-web oth
00062 application/x-javascript js
00063 application/x-shockwave-flash swf
00064 audio/midi mid midi kar
00065 audio/mpeg mpga mpa mp2 mp3
00066 audio/x-aiff aif aiff aifc
00067 audio/x-wav wav
00068 audio/ogg oga spx ogg
00069 image/x-bmp bmp
00070 image/gif gif
00071 image/jpeg jpeg jpg jpe
00072 image/png png
00073 image/svg+xml svg
00074 image/svg svg
00075 image/tiff tiff tif
00076 image/vnd.djvu djvu
00077 image/x.djvu djvu
00078 image/x-djvu djvu
00079 image/x-portable-pixmap ppm
00080 image/x-xcf xcf
00081 text/plain txt
00082 text/html html htm
00083 video/ogg ogv ogm ogg
00084 video/mpeg mpg mpeg
00085 END_STRING
00086 );
00087 
00094 define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
00095 application/pdf [OFFICE]
00096 application/vnd.oasis.opendocument.chart [OFFICE]
00097 application/vnd.oasis.opendocument.chart-template [OFFICE]
00098 application/vnd.oasis.opendocument.database [OFFICE]
00099 application/vnd.oasis.opendocument.formula [OFFICE]
00100 application/vnd.oasis.opendocument.formula-template [OFFICE]
00101 application/vnd.oasis.opendocument.graphics [OFFICE]
00102 application/vnd.oasis.opendocument.graphics-template [OFFICE]
00103 application/vnd.oasis.opendocument.image [OFFICE]
00104 application/vnd.oasis.opendocument.image-template [OFFICE]
00105 application/vnd.oasis.opendocument.presentation [OFFICE]
00106 application/vnd.oasis.opendocument.presentation-template [OFFICE]
00107 application/vnd.oasis.opendocument.spreadsheet [OFFICE]
00108 application/vnd.oasis.opendocument.spreadsheet-template [OFFICE]
00109 application/vnd.oasis.opendocument.text [OFFICE]
00110 application/vnd.oasis.opendocument.text-template [OFFICE]
00111 application/vnd.oasis.opendocument.text-master [OFFICE]
00112 application/vnd.oasis.opendocument.text-web [OFFICE]
00113 text/javascript application/x-javascript [EXECUTABLE]
00114 application/x-shockwave-flash [MULTIMEDIA]
00115 audio/midi [AUDIO]
00116 audio/x-aiff [AUDIO]
00117 audio/x-wav [AUDIO]
00118 audio/mp3 audio/mpeg [AUDIO]
00119 application/ogg audio/ogg video/ogg [MULTIMEDIA]
00120 image/x-bmp image/x-ms-bmp image/bmp [BITMAP]
00121 image/gif [BITMAP]
00122 image/jpeg [BITMAP]
00123 image/png [BITMAP]
00124 image/svg+xml [DRAWING]
00125 image/tiff [BITMAP]
00126 image/vnd.djvu [BITMAP]
00127 image/x-xcf [BITMAP]
00128 image/x-portable-pixmap [BITMAP]
00129 text/plain [TEXT]
00130 text/html [TEXT]
00131 video/ogg [VIDEO]
00132 video/mpeg [VIDEO]
00133 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
00134 END_STRING
00135 );
00136 
00144 class MimeMagic {
00145 
00150         var $mMediaTypes = null;
00151 
00154         var $mMimeTypeAliases = null;
00155 
00158         var $mMimeToExt = null;
00159 
00162         var $mExtToMime = null;
00163 
00166         var $mIEAnalyzer;
00167 
00170         private static $instance;
00171 
00174         private static $extensionLoaded = false;
00175 
00180         function __construct() {
00185                 global $wgMimeTypeFile, $IP, $wgLoadFileinfoExtension;
00186 
00187                 $types = MM_WELL_KNOWN_MIME_TYPES;
00188 
00189                 if ( $wgMimeTypeFile == 'includes/mime.types' ) {
00190                         $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
00191                 }
00192 
00193                 if ( $wgLoadFileinfoExtension && !self::$extensionLoaded ) {
00194                         self::$extensionLoaded = true;
00195                         wfDl( 'fileinfo' );
00196                 }
00197 
00198                 if ( $wgMimeTypeFile ) {
00199                         if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
00200                                 wfDebug( __METHOD__ . ": loading mime types from $wgMimeTypeFile\n" );
00201                                 $types .= "\n";
00202                                 $types .= file_get_contents( $wgMimeTypeFile );
00203                         } else {
00204                                 wfDebug( __METHOD__ . ": can't load mime types from $wgMimeTypeFile\n" );
00205                         }
00206                 } else {
00207                         wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" );
00208                 }
00209 
00210                 $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
00211                 $types = str_replace( "\t", " ", $types );
00212 
00213                 $this->mMimeToExt = array();
00214                 $this->mToMime = array();
00215 
00216                 $lines = explode( "\n", $types );
00217                 foreach ( $lines as $s ) {
00218                         $s = trim( $s );
00219                         if ( empty( $s ) ) {
00220                                 continue;
00221                         }
00222                         if ( strpos( $s, '#' ) === 0 ) {
00223                                 continue;
00224                         }
00225 
00226                         $s = strtolower( $s );
00227                         $i = strpos( $s, ' ' );
00228 
00229                         if ( $i === false ) {
00230                                 continue;
00231                         }
00232 
00233                         $mime = substr( $s, 0, $i );
00234                         $ext = trim( substr( $s, $i+1 ) );
00235 
00236                         if ( empty( $ext ) ) {
00237                                 continue;
00238                         }
00239 
00240                         if ( !empty( $this->mMimeToExt[$mime] ) ) {
00241                                 $this->mMimeToExt[$mime] .= ' ' . $ext;
00242                         } else {
00243                                 $this->mMimeToExt[$mime] = $ext;
00244                         }
00245 
00246                         $extensions = explode( ' ', $ext );
00247 
00248                         foreach ( $extensions as $e ) {
00249                                 $e = trim( $e );
00250                                 if ( empty( $e ) ) {
00251                                         continue;
00252                                 }
00253 
00254                                 if ( !empty( $this->mExtToMime[$e] ) ) {
00255                                         $this->mExtToMime[$e] .= ' ' . $mime;
00256                                 } else {
00257                                         $this->mExtToMime[$e] = $mime;
00258                                 }
00259                         }
00260                 }
00261 
00266                 global $wgMimeInfoFile;
00267                 if ( $wgMimeInfoFile == 'includes/mime.info' ) {
00268                         $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
00269                 }
00270 
00271                 $info = MM_WELL_KNOWN_MIME_INFO;
00272 
00273                 if ( $wgMimeInfoFile ) {
00274                         if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
00275                                 wfDebug( __METHOD__ . ": loading mime info from $wgMimeInfoFile\n" );
00276                                 $info .= "\n";
00277                                 $info .= file_get_contents( $wgMimeInfoFile );
00278                         } else {
00279                                 wfDebug( __METHOD__ . ": can't load mime info from $wgMimeInfoFile\n" );
00280                         }
00281                 } else {
00282                         wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" );
00283                 }
00284 
00285                 $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info );
00286                 $info = str_replace( "\t", " ", $info );
00287 
00288                 $this->mMimeTypeAliases = array();
00289                 $this->mMediaTypes = array();
00290 
00291                 $lines = explode( "\n", $info );
00292                 foreach ( $lines as $s ) {
00293                         $s = trim( $s );
00294                         if ( empty( $s ) ) {
00295                                 continue;
00296                         }
00297                         if ( strpos( $s, '#' ) === 0 ) {
00298                                 continue;
00299                         }
00300 
00301                         $s = strtolower( $s );
00302                         $i = strpos( $s, ' ' );
00303 
00304                         if ( $i === false ) {
00305                                 continue;
00306                         }
00307 
00308                         #print "processing MIME INFO line $s<br>";
00309 
00310                         $match = array();
00311                         if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
00312                                 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
00313                                 $mtype = trim( strtoupper( $match[1] ) );
00314                         } else {
00315                                 $mtype = MEDIATYPE_UNKNOWN;
00316                         }
00317 
00318                         $m = explode( ' ', $s );
00319 
00320                         if ( !isset( $this->mMediaTypes[$mtype] ) ) {
00321                                 $this->mMediaTypes[$mtype] = array();
00322                         }
00323 
00324                         foreach ( $m as $mime ) {
00325                                 $mime = trim( $mime );
00326                                 if ( empty( $mime ) ) {
00327                                         continue;
00328                                 }
00329 
00330                                 $this->mMediaTypes[$mtype][] = $mime;
00331                         }
00332 
00333                         if ( count( $m ) > 1 ) {
00334                                 $main = $m[0];
00335                                 for ( $i = 1; $i < count( $m ); $i += 1 ) {
00336                                         $mime = $m[$i];
00337                                         $this->mMimeTypeAliases[$mime] = $main;
00338                                 }
00339                         }
00340                 }
00341 
00342         }
00343 
00348         public static function &singleton() {
00349                 if ( self::$instance === null ) {
00350                         self::$instance = new MimeMagic;
00351                 }
00352                 return self::$instance;
00353         }
00354 
00363         public function getExtensionsForType( $mime ) {
00364                 $mime = strtolower( $mime );
00365 
00366                 // Check the mime-to-ext map
00367                 if ( isset( $this->mMimeToExt[$mime] ) ) {
00368                         return $this->mMimeToExt[$mime];
00369                 }
00370 
00371                 // Resolve the mime type to the canonical type
00372                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00373                         $mime = $this->mMimeTypeAliases[$mime];
00374                         if ( isset( $this->mMimeToExt[$mime] ) ) {
00375                                 return $this->mMimeToExt[$mime];
00376                         }
00377                 }
00378 
00379                 return null;
00380         }
00381 
00389         public function getTypesForExtension( $ext ) {
00390                 $ext = strtolower( $ext );
00391 
00392                 $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
00393                 return $r;
00394         }
00395 
00403         public function guessTypesForExtension( $ext ) {
00404                 $m = $this->getTypesForExtension( $ext );
00405                 if ( is_null( $m ) ) {
00406                         return null;
00407                 }
00408 
00409                 // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient
00410                 $m = trim( $m );
00411                 $m = preg_replace( '/\s.*$/', '', $m );
00412 
00413                 return $m;
00414         }
00415 
00425         public function isMatchingExtension( $extension, $mime ) {
00426                 $ext = $this->getExtensionsForType( $mime );
00427 
00428                 if ( !$ext ) {
00429                         return null; // Unknown mime type
00430                 }
00431 
00432                 $ext = explode( ' ', $ext );
00433 
00434                 $extension = strtolower( $extension );
00435                 return  in_array( $extension, $ext );
00436         }
00437 
00446         public function isPHPImageType( $mime ) {
00447                 // As defined by imagegetsize and image_type_to_mime
00448                 static $types = array(
00449                         'image/gif', 'image/jpeg', 'image/png',
00450                         'image/x-bmp', 'image/xbm', 'image/tiff',
00451                         'image/jp2', 'image/jpeg2000', 'image/iff',
00452                         'image/xbm', 'image/x-xbitmap',
00453                         'image/vnd.wap.wbmp', 'image/vnd.xiff',
00454                         'image/x-photoshop',
00455                         'application/x-shockwave-flash',
00456                 );
00457 
00458                 return in_array( $mime, $types );
00459         }
00460 
00472         function isRecognizableExtension( $extension ) {
00473                 static $types = array(
00474                         // Types recognized by getimagesize()
00475                         'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
00476                         'bmp', 'tiff', 'tif', 'jpc', 'jp2',
00477                         'jpx', 'jb2', 'swc', 'iff', 'wbmp',
00478                         'xbm',
00479 
00480                         // Formats we recognize magic numbers for
00481                         'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx',
00482                         'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
00483                         'webp',
00484 
00485                         // XML formats we sure hope we recognize reliably
00486                         'svg',
00487                 );
00488                 return in_array( strtolower( $extension ), $types );
00489         }
00490 
00510         public function improveTypeFromExtension( $mime, $ext ) {
00511                 if ( $mime === 'unknown/unknown' ) {
00512                         if ( $this->isRecognizableExtension( $ext ) ) {
00513                                 wfDebug( __METHOD__ . ': refusing to guess mime type for .' .
00514                                         "$ext file, we should have recognized it\n" );
00515                         } else {
00516                                 // Not something we can detect, so simply
00517                                 // trust the file extension
00518                                 $mime = $this->guessTypesForExtension( $ext );
00519                         }
00520                 }
00521                 elseif ( $mime === 'application/x-opc+zip' ) {
00522                         if ( $this->isMatchingExtension( $ext, $mime ) ) {
00523                                 // A known file extension for an OPC file,
00524                                 // find the proper mime type for that file extension
00525                                 $mime = $this->guessTypesForExtension( $ext );
00526                         } else {
00527                                 wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " .
00528                                         ".$ext is not a known OPC extension.\n" );
00529                                 $mime = 'application/zip';
00530                         }
00531                 }
00532 
00533                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00534                         $mime = $this->mMimeTypeAliases[$mime];
00535                 }
00536 
00537                 wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
00538                 return $mime;
00539         }
00540 
00555         public function guessMimeType( $file, $ext = true ) {
00556                 if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
00557                         wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00558                                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00559                 }
00560 
00561                 $mime = $this->doGuessMimeType( $file, $ext );
00562 
00563                 if( !$mime ) {
00564                         wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" );
00565                         $mime = $this->detectMimeType( $file, $ext );
00566                 }
00567 
00568                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00569                         $mime = $this->mMimeTypeAliases[$mime];
00570                 }
00571 
00572                 wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" );
00573                 return $mime;
00574         }
00575 
00584         private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
00585                 // Read a chunk of the file
00586                 wfSuppressWarnings();
00587                 $f = fopen( $file, 'rb' );
00588                 wfRestoreWarnings();
00589 
00590                 if( !$f ) {
00591                         return 'unknown/unknown';
00592                 }
00593 
00594                 $fsize = filesize( $file );
00595                 if ( $fsize === false ) {
00596                         return 'unknown/unknown';
00597                 }
00598 
00599                 $head = fread( $f, 1024 );
00600                 $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
00601                 if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
00602                         throw new MWException(
00603                                 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
00604                 }
00605                 $tail = fread( $f, $tailLength );
00606                 fclose( $f );
00607 
00608                 wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
00609 
00610                 // Hardcode a few magic number checks...
00611                 $headers = array(
00612                         // Multimedia...
00613                         'MThd'             => 'audio/midi',
00614                         'OggS'             => 'application/ogg',
00615 
00616                         // Image formats...
00617                         // Note that WMF may have a bare header, no magic number.
00618                         "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
00619                         "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
00620                         '%PDF'             => 'application/pdf',
00621                         'gimp xcf'         => 'image/x-xcf',
00622 
00623                         // Some forbidden fruit...
00624                         'MZ'               => 'application/octet-stream', // DOS/Windows executable
00625                         "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
00626                         "\x7fELF"          => 'application/octet-stream', // ELF binary
00627                 );
00628 
00629                 foreach ( $headers as $magic => $candidate ) {
00630                         if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
00631                                 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
00632                                 return $candidate;
00633                         }
00634                 }
00635 
00636                 /* Look for WebM and Matroska files */
00637                 if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
00638                         $doctype = strpos( $head, "\x42\x82" );
00639                         if ( $doctype ) {
00640                                 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
00641                                 $data = substr( $head, $doctype+3, 8 );
00642                                 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
00643                                         wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
00644                                         return "video/x-matroska";
00645                                 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
00646                                         wfDebug( __METHOD__ . ": recognized file as video/webm\n" );
00647                                         return "video/webm";
00648                                 }
00649                         }
00650                         wfDebug( __METHOD__ . ": unknown EBML file\n" );
00651                         return "unknown/unknown";
00652                 }
00653 
00654                 /* Look for WebP */
00655                 if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) {
00656                         wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
00657                         return "image/webp";
00658                 }
00659 
00672                 if ( ( strpos( $head, '<?php' ) !== false ) ||
00673                         ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
00674                         ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
00675                         ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
00676                         ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
00677                         ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
00678 
00679                         wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
00680                         return 'application/x-php';
00681                 }
00682 
00686                 $xml = new XmlTypeCheck( $file );
00687                 if ( $xml->wellFormed ) {
00688                         global $wgXMLMimeTypes;
00689                         if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
00690                                 return $wgXMLMimeTypes[$xml->getRootElement()];
00691                         } else {
00692                                 return 'application/xml';
00693                         }
00694                 }
00695 
00699                 $script_type = null;
00700 
00701                 # detect by shebang
00702                 if ( substr( $head, 0, 2) == "#!" ) {
00703                         $script_type = "ASCII";
00704                 } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
00705                         $script_type = "UTF-8";
00706                 } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
00707                         $script_type = "UTF-16BE";
00708                 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
00709                         $script_type = "UTF-16LE";
00710                 }
00711 
00712                 if ( $script_type ) {
00713                         if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
00714                                 // Quick and dirty fold down to ASCII!
00715                                 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
00716                                 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
00717                                 $head = '';
00718                                 foreach( $chars as $codepoint ) {
00719                                         if( $codepoint < 128 ) {
00720                                                 $head .= chr( $codepoint );
00721                                         } else {
00722                                                 $head .= '?';
00723                                         }
00724                                 }
00725                         }
00726 
00727                         $match = array();
00728 
00729                         if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
00730                                 $mime = "application/x-{$match[2]}";
00731                                 wfDebug( __METHOD__ . ": shell script recognized as $mime\n" );
00732                                 return $mime;
00733                         }
00734                 }
00735 
00736                 // Check for ZIP variants (before getimagesize)
00737                 if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
00738                         wfDebug( __METHOD__ . ": ZIP header present in $file\n" );
00739                         return $this->detectZipType( $head, $tail, $ext );
00740                 }
00741 
00742                 wfSuppressWarnings();
00743                 $gis = getimagesize( $file );
00744                 wfRestoreWarnings();
00745 
00746                 if( $gis && isset( $gis['mime'] ) ) {
00747                         $mime = $gis['mime'];
00748                         wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" );
00749                         return $mime;
00750                 }
00751 
00752                 // Also test DjVu
00753                 $deja = new DjVuImage( $file );
00754                 if( $deja->isValid() ) {
00755                         wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
00756                         return 'image/vnd.djvu';
00757                 }
00758 
00759                 return false;
00760         }
00761 
00775         function detectZipType( $header, $tail = null, $ext = false ) {
00776                 if( $ext ) { # TODO: remove $ext param
00777                         wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00778                                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00779                 }
00780 
00781                 $mime = 'application/zip';
00782                 $opendocTypes = array(
00783                         'chart-template',
00784                         'chart',
00785                         'formula-template',
00786                         'formula',
00787                         'graphics-template',
00788                         'graphics',
00789                         'image-template',
00790                         'image',
00791                         'presentation-template',
00792                         'presentation',
00793                         'spreadsheet-template',
00794                         'spreadsheet',
00795                         'text-template',
00796                         'text-master',
00797                         'text-web',
00798                         'text' );
00799 
00800                 // http://lists.oasis-open.org/archives/office/200505/msg00006.html
00801                 $types = '(?:' . implode( '|', $opendocTypes ) . ')';
00802                 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
00803 
00804                 $openxmlRegex = "/^\[Content_Types\].xml/";
00805 
00806                 if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
00807                         $mime = $matches[1];
00808                         wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" );
00809                 } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
00810                         $mime = "application/x-opc+zip";
00811                         # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
00812                         if ( $ext !== true && $ext !== false ) {
00817                                 if ( $this->isMatchingExtension( $ext, $mime) ) {
00818                                         /* A known file extension for an OPC file,
00819                                          * find the proper mime type for that file extension
00820                                          */
00821                                         $mime = $this->guessTypesForExtension( $ext );
00822                                 } else {
00823                                         $mime = "application/zip";
00824                                 }
00825                         }
00826                         wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" );
00827                 } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
00828                                 ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
00829                                 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
00830                         if ( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) {
00831                                 $mime = "application/msword";
00832                         }
00833                         switch( substr( $header, 512, 6) ) {
00834                                 case "\xEC\xA5\xC1\x00\x0E\x00":
00835                                 case "\xEC\xA5\xC1\x00\x1C\x00":
00836                                 case "\xEC\xA5\xC1\x00\x43\x00":
00837                                         $mime = "application/vnd.ms-powerpoint";
00838                                         break;
00839                                 case "\xFD\xFF\xFF\xFF\x10\x00":
00840                                 case "\xFD\xFF\xFF\xFF\x1F\x00":
00841                                 case "\xFD\xFF\xFF\xFF\x22\x00":
00842                                 case "\xFD\xFF\xFF\xFF\x23\x00":
00843                                 case "\xFD\xFF\xFF\xFF\x28\x00":
00844                                 case "\xFD\xFF\xFF\xFF\x29\x00":
00845                                 case "\xFD\xFF\xFF\xFF\x10\x02":
00846                                 case "\xFD\xFF\xFF\xFF\x1F\x02":
00847                                 case "\xFD\xFF\xFF\xFF\x22\x02":
00848                                 case "\xFD\xFF\xFF\xFF\x23\x02":
00849                                 case "\xFD\xFF\xFF\xFF\x28\x02":
00850                                 case "\xFD\xFF\xFF\xFF\x29\x02":
00851                                         $mime = "application/vnd.msexcel";
00852                                         break;
00853                         }
00854 
00855                         wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" );
00856                 } else {
00857                         wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" );
00858                 }
00859                 return $mime;
00860         }
00861 
00880         private function detectMimeType( $file, $ext = true ) {
00881                 global $wgMimeDetectorCommand;
00882 
00883                 if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
00884                         wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00885                 }
00886 
00887                 $m = null;
00888                 if ( $wgMimeDetectorCommand ) {
00889                         // @todo FIXME: Use wfShellExec
00890                         $fn = wfEscapeShellArg( $file );
00891                         $m = `$wgMimeDetectorCommand $fn`;
00892                 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
00893 
00894                         # This required the fileinfo extension by PECL,
00895                         # see http://pecl.php.net/package/fileinfo
00896                         # This must be compiled into PHP
00897                         #
00898                         # finfo is the official replacement for the deprecated
00899                         # mime_content_type function, see below.
00900                         #
00901                         # If you may need to load the fileinfo extension at runtime, set
00902                         # $wgLoadFileinfoExtension in LocalSettings.php
00903 
00904                         $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */
00905 
00906                         if ( $mime_magic_resource ) {
00907                                 $m = finfo_file( $mime_magic_resource, $file );
00908                                 finfo_close( $mime_magic_resource );
00909                         } else {
00910                                 wfDebug( __METHOD__ . ": finfo_open failed on ".FILEINFO_MIME."!\n" );
00911                         }
00912                 } elseif ( function_exists( "mime_content_type" ) ) {
00913 
00914                         # NOTE: this function is available since PHP 4.3.0, but only if
00915                         # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
00916                         #
00917                         # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundled with PHP;
00918                         # sometimes, this may even be needed under linus/unix.
00919                         #
00920                         # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
00921                         # see http://www.php.net/manual/en/ref.mime-magic.php for details.
00922 
00923                         $m = mime_content_type( $file );
00924                 } else {
00925                         wfDebug( __METHOD__ . ": no magic mime detector found!\n" );
00926                 }
00927 
00928                 if ( $m ) {
00929                         # normalize
00930                         $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
00931                         $m = trim( $m );
00932                         $m = strtolower( $m );
00933 
00934                         if ( strpos( $m, 'unknown' ) !== false ) {
00935                                 $m = null;
00936                         } else {
00937                                 wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" );
00938                                 return $m;
00939                         }
00940                 }
00941 
00942                 // If desired, look at extension as a fallback.
00943                 if ( $ext === true ) {
00944                         $i = strrpos( $file, '.' );
00945                         $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
00946                 }
00947                 if ( $ext ) {
00948                         if( $this->isRecognizableExtension( $ext ) ) {
00949                                 wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
00950                         } else {
00951                                 $m = $this->guessTypesForExtension( $ext );
00952                                 if ( $m ) {
00953                                         wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" );
00954                                         return $m;
00955                                 }
00956                         }
00957                 }
00958 
00959                 // Unknown type
00960                 wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" );
00961                 return 'unknown/unknown';
00962         }
00963 
00980         function getMediaType( $path = null, $mime = null ) {
00981                 if( !$mime && !$path ) {
00982                         return MEDIATYPE_UNKNOWN;
00983                 }
00984 
00985                 // If mime type is unknown, guess it
00986                 if( !$mime ) {
00987                         $mime = $this->guessMimeType( $path, false );
00988                 }
00989 
00990                 // Special code for ogg - detect if it's video (theora),
00991                 // else label it as sound.
00992                 if ( $mime == 'application/ogg' && file_exists( $path ) ) {
00993 
00994                         // Read a chunk of the file
00995                         $f = fopen( $path, "rt" );
00996                         if ( !$f ) return MEDIATYPE_UNKNOWN;
00997                         $head = fread( $f, 256 );
00998                         fclose( $f );
00999 
01000                         $head = strtolower( $head );
01001 
01002                         // This is an UGLY HACK, file should be parsed correctly
01003                         if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
01004                         elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
01005                         elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
01006                         elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
01007                         else return MEDIATYPE_MULTIMEDIA;
01008                 }
01009 
01010                 // Check for entry for full mime type
01011                 if( $mime ) {
01012                         $type = $this->findMediaType( $mime );
01013                         if ( $type !== MEDIATYPE_UNKNOWN ) {
01014                                 return $type;
01015                         }
01016                 }
01017 
01018                 // Check for entry for file extension
01019                 if ( $path ) {
01020                         $i = strrpos( $path, '.' );
01021                         $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
01022 
01023                         // TODO: look at multi-extension if this fails, parse from full path
01024                         $type = $this->findMediaType( '.' . $e );
01025                         if ( $type !== MEDIATYPE_UNKNOWN ) {
01026                                 return $type;
01027                         }
01028                 }
01029 
01030                 // Check major mime type
01031                 if ( $mime ) {
01032                         $i = strpos( $mime, '/' );
01033                         if ( $i !== false ) {
01034                                 $major = substr( $mime, 0, $i );
01035                                 $type = $this->findMediaType( $major );
01036                                 if ( $type !== MEDIATYPE_UNKNOWN ) {
01037                                         return $type;
01038                                 }
01039                         }
01040                 }
01041 
01042                 if( !$type ) {
01043                         $type = MEDIATYPE_UNKNOWN;
01044                 }
01045 
01046                 return $type;
01047         }
01048 
01058         function findMediaType( $extMime ) {
01059                 if ( strpos( $extMime, '.' ) === 0 ) {
01060                         // If it's an extension, look up the mime types
01061                         $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
01062                         if ( !$m ) {
01063                                 return MEDIATYPE_UNKNOWN;
01064                         }
01065 
01066                         $m = explode( ' ', $m );
01067                 } else {
01068                         // Normalize mime type
01069                         if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
01070                                 $extMime = $this->mMimeTypeAliases[$extMime];
01071                         }
01072 
01073                         $m = array( $extMime );
01074                 }
01075 
01076                 foreach ( $m as $mime ) {
01077                         foreach ( $this->mMediaTypes as $type => $codes ) {
01078                                 if ( in_array( $mime, $codes, true ) ) {
01079                                         return $type;
01080                                 }
01081                         }
01082                 }
01083 
01084                 return MEDIATYPE_UNKNOWN;
01085         }
01086 
01096         public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
01097                 $ca = $this->getIEContentAnalyzer();
01098                 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
01099         }
01100 
01106         protected function getIEContentAnalyzer() {
01107                 if ( is_null( $this->mIEAnalyzer ) ) {
01108                         $this->mIEAnalyzer = new IEContentAnalyzer;
01109                 }
01110                 return $this->mIEAnalyzer;
01111         }
01112 }