MediaWiki  REL1_19
MimeMagic.php
Go to the documentation of this file.
00001 <?php
00027 define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
00028 application/ogg ogx ogg ogm ogv oga spx
00029 application/pdf pdf
00030 application/vnd.oasis.opendocument.chart odc
00031 application/vnd.oasis.opendocument.chart-template otc
00032 application/vnd.oasis.opendocument.database odb
00033 application/vnd.oasis.opendocument.formula odf
00034 application/vnd.oasis.opendocument.formula-template otf
00035 application/vnd.oasis.opendocument.graphics odg
00036 application/vnd.oasis.opendocument.graphics-template otg
00037 application/vnd.oasis.opendocument.image odi
00038 application/vnd.oasis.opendocument.image-template oti
00039 application/vnd.oasis.opendocument.presentation odp
00040 application/vnd.oasis.opendocument.presentation-template otp
00041 application/vnd.oasis.opendocument.spreadsheet ods
00042 application/vnd.oasis.opendocument.spreadsheet-template ots
00043 application/vnd.oasis.opendocument.text odt
00044 application/vnd.oasis.opendocument.text-master otm
00045 application/vnd.oasis.opendocument.text-template ott
00046 application/vnd.oasis.opendocument.text-web oth
00047 application/x-javascript js
00048 application/x-shockwave-flash swf
00049 audio/midi mid midi kar
00050 audio/mpeg mpga mpa mp2 mp3
00051 audio/x-aiff aif aiff aifc
00052 audio/x-wav wav
00053 audio/ogg oga spx ogg
00054 image/x-bmp bmp
00055 image/gif gif
00056 image/jpeg jpeg jpg jpe
00057 image/png png
00058 image/svg+xml svg 
00059 image/svg svg
00060 image/tiff tiff tif
00061 image/vnd.djvu djvu
00062 image/x.djvu djvu
00063 image/x-djvu djvu
00064 image/x-portable-pixmap ppm
00065 image/x-xcf xcf
00066 text/plain txt
00067 text/html html htm
00068 video/ogg ogv ogm ogg
00069 video/mpeg mpg mpeg
00070 END_STRING
00071 );
00072 
00079 define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
00080 application/pdf [OFFICE]
00081 application/vnd.oasis.opendocument.chart [OFFICE]
00082 application/vnd.oasis.opendocument.chart-template [OFFICE]
00083 application/vnd.oasis.opendocument.database [OFFICE]
00084 application/vnd.oasis.opendocument.formula [OFFICE]
00085 application/vnd.oasis.opendocument.formula-template [OFFICE]
00086 application/vnd.oasis.opendocument.graphics [OFFICE]
00087 application/vnd.oasis.opendocument.graphics-template [OFFICE]
00088 application/vnd.oasis.opendocument.image [OFFICE]
00089 application/vnd.oasis.opendocument.image-template [OFFICE]
00090 application/vnd.oasis.opendocument.presentation [OFFICE]
00091 application/vnd.oasis.opendocument.presentation-template [OFFICE]
00092 application/vnd.oasis.opendocument.spreadsheet [OFFICE]
00093 application/vnd.oasis.opendocument.spreadsheet-template [OFFICE]
00094 application/vnd.oasis.opendocument.text [OFFICE]
00095 application/vnd.oasis.opendocument.text-template [OFFICE]
00096 application/vnd.oasis.opendocument.text-master [OFFICE]
00097 application/vnd.oasis.opendocument.text-web [OFFICE]
00098 text/javascript application/x-javascript [EXECUTABLE]
00099 application/x-shockwave-flash [MULTIMEDIA]
00100 audio/midi [AUDIO]
00101 audio/x-aiff [AUDIO]
00102 audio/x-wav [AUDIO]
00103 audio/mp3 audio/mpeg [AUDIO]
00104 application/ogg audio/ogg video/ogg [MULTIMEDIA]
00105 image/x-bmp image/x-ms-bmp image/bmp [BITMAP]
00106 image/gif [BITMAP]
00107 image/jpeg [BITMAP]
00108 image/png [BITMAP]
00109 image/svg+xml [DRAWING]
00110 image/tiff [BITMAP]
00111 image/vnd.djvu [BITMAP]
00112 image/x-xcf [BITMAP]
00113 image/x-portable-pixmap [BITMAP]
00114 text/plain [TEXT]
00115 text/html [TEXT]
00116 video/ogg [VIDEO]
00117 video/mpeg [VIDEO]
00118 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
00119 END_STRING
00120 );
00121 
00129 class MimeMagic {
00130 
00135         var $mMediaTypes = null;
00136 
00139         var $mMimeTypeAliases = null;
00140 
00143         var $mMimeToExt = null;
00144 
00147         var $mExtToMime = null;
00148 
00151         var $mIEAnalyzer;
00152 
00155         private static $instance;
00156 
00159         private static $extensionLoaded = false;
00160 
00165         function __construct() {
00170                 global $wgMimeTypeFile, $IP, $wgLoadFileinfoExtension;
00171 
00172                 $types = MM_WELL_KNOWN_MIME_TYPES;
00173 
00174                 if ( $wgMimeTypeFile == 'includes/mime.types' ) {
00175                         $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
00176                 }
00177 
00178                 if ( $wgLoadFileinfoExtension && !self::$extensionLoaded ) {
00179                         self::$extensionLoaded = true;
00180                         wfDl( 'fileinfo' );
00181                 }
00182 
00183                 if ( $wgMimeTypeFile ) {
00184                         if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
00185                                 wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
00186                                 $types .= "\n";
00187                                 $types .= file_get_contents( $wgMimeTypeFile );
00188                         } else {
00189                                 wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" );
00190                         }
00191                 } else {
00192                         wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" );
00193                 }
00194 
00195                 $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
00196                 $types = str_replace( "\t", " ", $types );
00197 
00198                 $this->mMimeToExt = array();
00199                 $this->mToMime = array();
00200 
00201                 $lines = explode( "\n",$types );
00202                 foreach ( $lines as $s ) {
00203                         $s = trim( $s );
00204                         if ( empty( $s ) ) {
00205                                 continue;
00206                         }
00207                         if ( strpos( $s, '#' ) === 0 ) {
00208                                 continue;
00209                         }
00210 
00211                         $s = strtolower( $s );
00212                         $i = strpos( $s, ' ' );
00213 
00214                         if ( $i === false ) {
00215                                 continue;
00216                         }
00217 
00218                         #print "processing MIME line $s<br>";
00219 
00220                         $mime = substr( $s, 0, $i );
00221                         $ext = trim( substr($s, $i+1 ) );
00222 
00223                         if ( empty( $ext ) ) {
00224                                 continue;
00225                         }
00226 
00227                         if ( !empty( $this->mMimeToExt[$mime] ) ) {
00228                                 $this->mMimeToExt[$mime] .= ' ' . $ext;
00229                         } else {
00230                                 $this->mMimeToExt[$mime] = $ext;
00231                         }
00232 
00233                         $extensions = explode( ' ', $ext );
00234 
00235                         foreach ( $extensions as $e ) {
00236                                 $e = trim( $e );
00237                                 if ( empty( $e ) ) {
00238                                         continue;
00239                                 }
00240 
00241                                 if ( !empty( $this->mExtToMime[$e] ) ) {
00242                                         $this->mExtToMime[$e] .= ' ' . $mime;
00243                                 } else {
00244                                         $this->mExtToMime[$e] = $mime;
00245                                 }
00246                         }
00247                 }
00248 
00253                 global $wgMimeInfoFile;
00254                 if ( $wgMimeInfoFile == 'includes/mime.info' ) {
00255                         $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
00256                 }
00257 
00258                 $info = MM_WELL_KNOWN_MIME_INFO;
00259 
00260                 if ( $wgMimeInfoFile ) {
00261                         if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
00262                                 wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" );
00263                                 $info .= "\n";
00264                                 $info .= file_get_contents( $wgMimeInfoFile );
00265                         } else {
00266                                 wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n");
00267                         }
00268                 } else {
00269                         wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n");
00270                 }
00271 
00272                 $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info);
00273                 $info = str_replace( "\t", " ", $info );
00274 
00275                 $this->mMimeTypeAliases = array();
00276                 $this->mMediaTypes = array();
00277 
00278                 $lines = explode( "\n", $info );
00279                 foreach ( $lines as $s ) {
00280                         $s = trim( $s );
00281                         if ( empty( $s ) ) {
00282                                 continue;
00283                         }
00284                         if ( strpos( $s, '#' ) === 0 ) {
00285                                 continue;
00286                         }
00287 
00288                         $s = strtolower( $s );
00289                         $i = strpos( $s, ' ' );
00290 
00291                         if ( $i === false ) {
00292                                 continue;
00293                         }
00294 
00295                         #print "processing MIME INFO line $s<br>";
00296 
00297                         $match = array();
00298                         if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
00299                                 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
00300                                 $mtype = trim( strtoupper( $match[1] ) );
00301                         } else {
00302                                 $mtype = MEDIATYPE_UNKNOWN;
00303                         }
00304 
00305                         $m = explode( ' ', $s );
00306 
00307                         if ( !isset( $this->mMediaTypes[$mtype] ) ) {
00308                                 $this->mMediaTypes[$mtype] = array();
00309                         }
00310 
00311                         foreach ( $m as $mime ) {
00312                                 $mime = trim( $mime );
00313                                 if ( empty( $mime ) ) {
00314                                         continue;
00315                                 }
00316 
00317                                 $this->mMediaTypes[$mtype][] = $mime;
00318                         }
00319 
00320                         if ( sizeof( $m ) > 1 ) {
00321                                 $main = $m[0];
00322                                 for ( $i=1; $i<sizeof($m); $i += 1 ) {
00323                                         $mime = $m[$i];
00324                                         $this->mMimeTypeAliases[$mime] = $main;
00325                                 }
00326                         }
00327                 }
00328 
00329         }
00330 
00335         public static function &singleton() {
00336                 if ( !isset( self::$instance ) ) {
00337                         self::$instance = new MimeMagic;
00338                 }
00339                 return self::$instance;
00340         }
00341 
00350         public function getExtensionsForType( $mime ) {
00351                 $mime = strtolower( $mime );
00352 
00353                 // Check the mime-to-ext map
00354                 if ( isset( $this->mMimeToExt[$mime] ) ) {
00355                         return $this->mMimeToExt[$mime];
00356                 }
00357 
00358                 // Resolve the mime type to the canonical type
00359                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00360                         $mime = $this->mMimeTypeAliases[$mime];
00361                         if ( isset( $this->mMimeToExt[$mime] ) ) {
00362                                 return $this->mMimeToExt[$mime];
00363                         }
00364                 }
00365 
00366                 return null;
00367         }
00368 
00376         public function getTypesForExtension( $ext ) {
00377                 $ext = strtolower( $ext );
00378 
00379                 $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
00380                 return $r;
00381         }
00382 
00390         public function guessTypesForExtension( $ext ) {
00391                 $m = $this->getTypesForExtension( $ext );
00392                 if ( is_null( $m ) ) {
00393                         return null;
00394                 }
00395 
00396                 // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient
00397                 $m = trim( $m );
00398                 $m = preg_replace( '/\s.*$/', '', $m );
00399 
00400                 return $m;
00401         }
00402 
00403 
00413         public function isMatchingExtension( $extension, $mime ) {
00414                 $ext = $this->getExtensionsForType( $mime );
00415 
00416                 if ( !$ext ) {
00417                         return null;  // Unknown mime type
00418                 }
00419 
00420                 $ext = explode( ' ', $ext );
00421 
00422                 $extension = strtolower( $extension );
00423                 return  in_array( $extension, $ext );
00424         }
00425 
00434         public function isPHPImageType( $mime ) {
00435                 // As defined by imagegetsize and image_type_to_mime
00436                 static $types = array(
00437                         'image/gif', 'image/jpeg', 'image/png',
00438                         'image/x-bmp', 'image/xbm', 'image/tiff',
00439                         'image/jp2', 'image/jpeg2000', 'image/iff',
00440                         'image/xbm', 'image/x-xbitmap',
00441                         'image/vnd.wap.wbmp', 'image/vnd.xiff',
00442                         'image/x-photoshop',
00443                         'application/x-shockwave-flash',
00444                 );
00445 
00446                 return in_array( $mime, $types );
00447         }
00448 
00460         function isRecognizableExtension( $extension ) {
00461                 static $types = array(
00462                         // Types recognized by getimagesize()
00463                         'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
00464                         'bmp', 'tiff', 'tif', 'jpc', 'jp2',
00465                         'jpx', 'jb2', 'swc', 'iff', 'wbmp',
00466                         'xbm',
00467 
00468                         // Formats we recognize magic numbers for
00469                         'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx',
00470                         'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
00471                         'webp',
00472 
00473                         // XML formats we sure hope we recognize reliably
00474                         'svg',
00475                 );
00476                 return in_array( strtolower( $extension ), $types );
00477         }
00478 
00498         public function improveTypeFromExtension( $mime, $ext ) {
00499                 if ( $mime === 'unknown/unknown' ) {
00500                         if ( $this->isRecognizableExtension( $ext ) ) {
00501                                 wfDebug( __METHOD__. ': refusing to guess mime type for .' . 
00502                                         "$ext file, we should have recognized it\n" );
00503                         } else {
00504                                 // Not something we can detect, so simply 
00505                                 // trust the file extension
00506                                 $mime = $this->guessTypesForExtension( $ext );
00507                         }
00508                 }
00509                 elseif ( $mime === 'application/x-opc+zip' ) {
00510                         if ( $this->isMatchingExtension( $ext, $mime ) ) {
00511                                 // A known file extension for an OPC file,
00512                                 // find the proper mime type for that file extension
00513                                 $mime = $this->guessTypesForExtension( $ext );
00514                         } else {
00515                                 wfDebug( __METHOD__. ": refusing to guess better type for $mime file, " . 
00516                                         ".$ext is not a known OPC extension.\n" );
00517                                 $mime = 'application/zip';
00518                         }
00519                 }
00520 
00521                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00522                         $mime = $this->mMimeTypeAliases[$mime];
00523                 }
00524 
00525                 wfDebug(__METHOD__.": improved mime type for .$ext: $mime\n");
00526                 return $mime;
00527         }
00528 
00543         public function guessMimeType( $file, $ext = true ) {
00544                 if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
00545                         wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " .
00546                                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00547                 }
00548 
00549                 $mime = $this->doGuessMimeType( $file, $ext );
00550 
00551                 if( !$mime ) {
00552                         wfDebug( __METHOD__.": internal type detection failed for $file (.$ext)...\n" );
00553                         $mime = $this->detectMimeType( $file, $ext );
00554                 }
00555 
00556                 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00557                         $mime = $this->mMimeTypeAliases[$mime];
00558                 }
00559 
00560                 wfDebug(__METHOD__.": guessed mime type of $file: $mime\n");
00561                 return $mime;
00562         }
00563 
00571         private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
00572                 // Read a chunk of the file
00573                 wfSuppressWarnings();
00574                 $f = fopen( $file, 'rb' );
00575                 wfRestoreWarnings();
00576                 
00577                 if( !$f ) {
00578                         return 'unknown/unknown';
00579                 }
00580 
00581                 $fsize = filesize( $file );
00582                 if ( $fsize === false ) {
00583                         return 'unknown/unknown';
00584                 }
00585 
00586                 $head = fread( $f, 1024 );
00587                 $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
00588                 if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
00589                         throw new MWException(
00590                                 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
00591                 }
00592                 $tail = fread( $f, $tailLength );
00593                 fclose( $f );
00594 
00595                 wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
00596 
00597                 // Hardcode a few magic number checks...
00598                 $headers = array(
00599                         // Multimedia...
00600                         'MThd'             => 'audio/midi',
00601                         'OggS'             => 'application/ogg',
00602 
00603                         // Image formats...
00604                         // Note that WMF may have a bare header, no magic number.
00605                         "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
00606                         "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
00607                         '%PDF'             => 'application/pdf',
00608                         'gimp xcf'         => 'image/x-xcf',
00609 
00610                         // Some forbidden fruit...
00611                         'MZ'               => 'application/octet-stream', // DOS/Windows executable
00612                         "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
00613                         "\x7fELF"          => 'application/octet-stream', // ELF binary
00614                 );
00615 
00616                 foreach ( $headers as $magic => $candidate ) {
00617                         if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
00618                                 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
00619                                 return $candidate;
00620                         }
00621                 }
00622 
00623                 /* Look for WebM and Matroska files */
00624                 if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
00625                         $doctype = strpos( $head, "\x42\x82" );
00626                         if ( $doctype ) {
00627                                 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
00628                                 $data = substr($head, $doctype+3, 8);
00629                                 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
00630                                         wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
00631                                         return "video/x-matroska";
00632                                 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
00633                                         wfDebug( __METHOD__ . ": recognized file as video/webm\n" );
00634                                         return "video/webm";
00635                                 }
00636                         }
00637                         wfDebug( __METHOD__ . ": unknown EBML file\n" );
00638                         return "unknown/unknown";
00639                 }
00640 
00641                 /* Look for WebP */
00642                 if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) {
00643                         wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
00644                         return "image/webp";
00645                 }
00646 
00659                 if ( ( strpos( $head, '<?php' ) !== false ) ||
00660 
00661                     ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
00662                     ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
00663                     ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
00664                     ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
00665                     ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
00666 
00667                         wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
00668                         return 'application/x-php';
00669                 }
00670 
00674                 $xml = new XmlTypeCheck( $file );
00675                 if ( $xml->wellFormed ) {
00676                         global $wgXMLMimeTypes;
00677                         if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
00678                                 return $wgXMLMimeTypes[$xml->getRootElement()];
00679                         } else {
00680                                 return 'application/xml';
00681                         }
00682                 }
00683 
00687                 $script_type = null;
00688 
00689                 # detect by shebang
00690                 if ( substr( $head, 0, 2) == "#!" ) {
00691                         $script_type = "ASCII";
00692                 } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
00693                         $script_type = "UTF-8";
00694                 } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
00695                         $script_type = "UTF-16BE";
00696                 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
00697                         $script_type= "UTF-16LE";
00698                 }
00699 
00700                 if ( $script_type ) {
00701                         if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
00702                                 // Quick and dirty fold down to ASCII!
00703                                 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
00704                                 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
00705                                 $head = '';
00706                                 foreach( $chars as $codepoint ) {
00707                                         if( $codepoint < 128 ) {
00708                                                 $head .= chr( $codepoint );
00709                                         } else {
00710                                                 $head .= '?';
00711                                         }
00712                                 }
00713                         }
00714 
00715                         $match = array();
00716 
00717                         if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
00718                                 $mime = "application/x-{$match[2]}";
00719                                 wfDebug( __METHOD__.": shell script recognized as $mime\n" );
00720                                 return $mime;
00721                         }
00722                 }
00723 
00724                 // Check for ZIP variants (before getimagesize)
00725                 if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
00726                         wfDebug( __METHOD__.": ZIP header present in $file\n" );
00727                         return $this->detectZipType( $head, $tail, $ext );
00728                 }
00729 
00730                 wfSuppressWarnings();
00731                 $gis = getimagesize( $file );
00732                 wfRestoreWarnings();
00733 
00734                 if( $gis && isset( $gis['mime'] ) ) {
00735                         $mime = $gis['mime'];
00736                         wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" );
00737                         return $mime;
00738                 }
00739 
00740                 // Also test DjVu
00741                 $deja = new DjVuImage( $file );
00742                 if( $deja->isValid() ) {
00743                         wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
00744                         return 'image/vnd.djvu';
00745                 }
00746 
00747                 return false;
00748         }
00749         
00763         function detectZipType( $header, $tail = null, $ext = false ) {
00764                 if( $ext ) { # TODO: remove $ext param
00765                         wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " .
00766                                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00767                 }
00768 
00769                 $mime = 'application/zip';
00770                 $opendocTypes = array(
00771                         'chart-template',
00772                         'chart',
00773                         'formula-template',
00774                         'formula',
00775                         'graphics-template',
00776                         'graphics',
00777                         'image-template',
00778                         'image',
00779                         'presentation-template',
00780                         'presentation',
00781                         'spreadsheet-template',
00782                         'spreadsheet',
00783                         'text-template',
00784                         'text-master',
00785                         'text-web',
00786                         'text' );
00787 
00788                 // http://lists.oasis-open.org/archives/office/200505/msg00006.html
00789                 $types = '(?:' . implode( '|', $opendocTypes ) . ')';
00790                 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
00791 
00792                 $openxmlRegex = "/^\[Content_Types\].xml/";
00793 
00794                 if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
00795                         $mime = $matches[1];
00796                         wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
00797                 } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
00798                         $mime = "application/x-opc+zip";
00799                         # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere 
00800                         if ( $ext !== true && $ext !== false ) { 
00805                                 if ( $this->isMatchingExtension( $ext, $mime) ) {
00806                                         /* A known file extension for an OPC file,
00807                                         * find the proper mime type for that file extension */
00808                                         $mime = $this->guessTypesForExtension( $ext );
00809                                 } else {
00810                                         $mime = "application/zip";
00811                                 }
00812                         }
00813                         wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" );
00814                 } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && 
00815                                 ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
00816                                 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
00817                         if ( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) {
00818                                 $mime = "application/msword";
00819                         } 
00820                         switch( substr( $header, 512, 6) ) {
00821                                 case "\xEC\xA5\xC1\x00\x0E\x00":
00822                                 case "\xEC\xA5\xC1\x00\x1C\x00":
00823                                 case "\xEC\xA5\xC1\x00\x43\x00":
00824                                         $mime = "application/vnd.ms-powerpoint";
00825                                         break;
00826                                 case "\xFD\xFF\xFF\xFF\x10\x00":
00827                                 case "\xFD\xFF\xFF\xFF\x1F\x00":
00828                                 case "\xFD\xFF\xFF\xFF\x22\x00":
00829                                 case "\xFD\xFF\xFF\xFF\x23\x00":
00830                                 case "\xFD\xFF\xFF\xFF\x28\x00":
00831                                 case "\xFD\xFF\xFF\xFF\x29\x00":
00832                                 case "\xFD\xFF\xFF\xFF\x10\x02":
00833                                 case "\xFD\xFF\xFF\xFF\x1F\x02":
00834                                 case "\xFD\xFF\xFF\xFF\x22\x02":
00835                                 case "\xFD\xFF\xFF\xFF\x23\x02":
00836                                 case "\xFD\xFF\xFF\xFF\x28\x02":
00837                                 case "\xFD\xFF\xFF\xFF\x29\x02":
00838                                         $mime = "application/vnd.msexcel";
00839                                         break;
00840                         }
00841 
00842                         wfDebug( __METHOD__.": detected a MS Office document with OPC trailer\n");
00843                 } else {
00844                         wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" );
00845                 }
00846                 return $mime;
00847         }
00848 
00867         private function detectMimeType( $file, $ext = true ) {
00868                 global $wgMimeDetectorCommand;
00869 
00870                 if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
00871                         wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00872                 }
00873 
00874                 $m = null;
00875                 if ( $wgMimeDetectorCommand ) {
00876                         // @todo FIXME: Use wfShellExec
00877                         $fn = wfEscapeShellArg( $file );
00878                         $m = `$wgMimeDetectorCommand $fn`;
00879                 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
00880 
00881                         # This required the fileinfo extension by PECL,
00882                         # see http://pecl.php.net/package/fileinfo
00883                         # This must be compiled into PHP
00884                         #
00885                         # finfo is the official replacement for the deprecated
00886                         # mime_content_type function, see below.
00887                         #
00888                         # If you may need to load the fileinfo extension at runtime, set
00889                         # $wgLoadFileinfoExtension in LocalSettings.php
00890 
00891                         $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */
00892 
00893                         if ( $mime_magic_resource ) {
00894                                 $m = finfo_file( $mime_magic_resource, $file );
00895                                 finfo_close( $mime_magic_resource );
00896                         } else {
00897                                 wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
00898                         }
00899                 } elseif ( function_exists( "mime_content_type" ) ) {
00900 
00901                         # NOTE: this function is available since PHP 4.3.0, but only if
00902                         # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
00903                         #
00904                         # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
00905                         # sometimes, this may even be needed under linus/unix.
00906                         #
00907                         # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
00908                         # see http://www.php.net/manual/en/ref.mime-magic.php for details.
00909 
00910                         $m = mime_content_type($file);
00911                 } else {
00912                         wfDebug( __METHOD__.": no magic mime detector found!\n" );
00913                 }
00914 
00915                 if ( $m ) {
00916                         # normalize
00917                         $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
00918                         $m = trim( $m );
00919                         $m = strtolower( $m );
00920 
00921                         if ( strpos( $m, 'unknown' ) !== false ) {
00922                                 $m = null;
00923                         } else {
00924                                 wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
00925                                 return $m;
00926                         }
00927                 }
00928 
00929                 // If desired, look at extension as a fallback.
00930                 if ( $ext === true ) {
00931                         $i = strrpos( $file, '.' );
00932                         $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
00933                 }
00934                 if ( $ext ) {
00935                         if( $this->isRecognizableExtension( $ext ) ) {
00936                                 wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
00937                         } else {
00938                                 $m = $this->guessTypesForExtension( $ext );
00939                                 if ( $m ) {
00940                                         wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
00941                                         return $m;
00942                                 }
00943                         }
00944                 }
00945 
00946                 // Unknown type
00947                 wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" );
00948                 return 'unknown/unknown';
00949         }
00950 
00967         function getMediaType( $path = null, $mime = null ) {
00968                 if( !$mime && !$path ) {
00969                         return MEDIATYPE_UNKNOWN;
00970                 }
00971 
00972                 // If mime type is unknown, guess it
00973                 if( !$mime ) {
00974                         $mime = $this->guessMimeType( $path, false );
00975                 }
00976 
00977                 // Special code for ogg - detect if it's video (theora),
00978                 // else label it as sound.
00979                 if ( $mime == 'application/ogg' && file_exists( $path ) ) {
00980 
00981                         // Read a chunk of the file
00982                         $f = fopen( $path, "rt" );
00983                         if ( !$f ) return MEDIATYPE_UNKNOWN;
00984                         $head = fread( $f, 256 );
00985                         fclose( $f );
00986 
00987                         $head = strtolower( $head );
00988 
00989                         // This is an UGLY HACK, file should be parsed correctly
00990                         if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
00991                         elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
00992                         elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
00993                         elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
00994                         else return MEDIATYPE_MULTIMEDIA;
00995                 }
00996 
00997                 // Check for entry for full mime type
00998                 if( $mime ) {
00999                         $type = $this->findMediaType( $mime );
01000                         if ( $type !== MEDIATYPE_UNKNOWN ) {
01001                                 return $type;
01002                         }
01003                 }
01004 
01005                 // Check for entry for file extension
01006                 if ( $path ) {
01007                         $i = strrpos( $path, '.' );
01008                         $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
01009 
01010                         // TODO: look at multi-extension if this fails, parse from full path
01011                         $type = $this->findMediaType( '.' . $e );
01012                         if ( $type !== MEDIATYPE_UNKNOWN ) {
01013                                 return $type;
01014                         }
01015                 }
01016 
01017                 // Check major mime type
01018                 if ( $mime ) {
01019                         $i = strpos( $mime, '/' );
01020                         if ( $i !== false ) {
01021                                 $major = substr( $mime, 0, $i );
01022                                 $type = $this->findMediaType( $major );
01023                                 if ( $type !== MEDIATYPE_UNKNOWN ) {
01024                                         return $type;
01025                                 }
01026                         }
01027                 }
01028 
01029                 if( !$type ) {
01030                         $type = MEDIATYPE_UNKNOWN;
01031                 }
01032 
01033                 return $type;
01034         }
01035 
01044         function findMediaType( $extMime ) {
01045                 if ( strpos( $extMime, '.' ) === 0 ) { 
01046                         // If it's an extension, look up the mime types
01047                         $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
01048                         if ( !$m ) {
01049                                 return MEDIATYPE_UNKNOWN;
01050                         }
01051 
01052                         $m = explode( ' ', $m );
01053                 } else {
01054                         // Normalize mime type
01055                         if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
01056                                 $extMime = $this->mMimeTypeAliases[$extMime];
01057                         }
01058 
01059                         $m = array( $extMime );
01060                 }
01061 
01062                 foreach ( $m as $mime ) {
01063                         foreach ( $this->mMediaTypes as $type => $codes ) {
01064                                 if ( in_array($mime, $codes, true ) ) {
01065                                         return $type;
01066                                 }
01067                         }
01068                 }
01069 
01070                 return MEDIATYPE_UNKNOWN;
01071         }
01072 
01081         public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
01082                 $ca = $this->getIEContentAnalyzer();
01083                 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
01084         }
01085 
01091         protected function getIEContentAnalyzer() {
01092                 if ( is_null( $this->mIEAnalyzer ) ) {
01093                         $this->mIEAnalyzer = new IEContentAnalyzer;
01094                 }
01095                 return $this->mIEAnalyzer;
01096         }
01097 }