00001 <?php
00011 define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
00012 application/ogg ogg ogm ogv
00013 application/pdf pdf
00014 application/vnd.oasis.opendocument.chart odc
00015 application/vnd.oasis.opendocument.chart-template otc
00016 application/vnd.oasis.opendocument.formula odf
00017 application/vnd.oasis.opendocument.formula-template otf
00018 application/vnd.oasis.opendocument.graphics odg
00019 application/vnd.oasis.opendocument.graphics-template otg
00020 application/vnd.oasis.opendocument.image odi
00021 application/vnd.oasis.opendocument.image-template oti
00022 application/vnd.oasis.opendocument.presentation odp
00023 application/vnd.oasis.opendocument.presentation-template otp
00024 application/vnd.oasis.opendocument.spreadsheet ods
00025 application/vnd.oasis.opendocument.spreadsheet-template ots
00026 application/vnd.oasis.opendocument.text odt
00027 application/vnd.oasis.opendocument.text-template ott
00028 application/vnd.oasis.opendocument.text-master otm
00029 application/vnd.oasis.opendocument.text-web oth
00030 application/x-javascript js
00031 application/x-shockwave-flash swf
00032 audio/midi mid midi kar
00033 audio/mpeg mpga mpa mp2 mp3
00034 audio/x-aiff aif aiff aifc
00035 audio/x-wav wav
00036 audio/ogg ogg
00037 image/x-bmp bmp
00038 image/gif gif
00039 image/jpeg jpeg jpg jpe
00040 image/png png
00041 image/svg+xml image/svg svg
00042 image/tiff tiff tif
00043 image/vnd.djvu image/x.djvu image/x-djvu djvu
00044 image/x-portable-pixmap ppm
00045 image/x-xcf xcf
00046 text/plain txt
00047 text/html html htm
00048 video/ogg ogm ogg ogv
00049 video/mpeg mpg mpeg
00050 END_STRING
00051 );
00052
00058 define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
00059 application/pdf [OFFICE]
00060 application/vnd.oasis.opendocument.chart [OFFICE]
00061 application/vnd.oasis.opendocument.chart-template [OFFICE]
00062 application/vnd.oasis.opendocument.formula [OFFICE]
00063 application/vnd.oasis.opendocument.formula-template [OFFICE]
00064 application/vnd.oasis.opendocument.graphics [OFFICE]
00065 application/vnd.oasis.opendocument.graphics-template [OFFICE]
00066 application/vnd.oasis.opendocument.image [OFFICE]
00067 application/vnd.oasis.opendocument.image-template [OFFICE]
00068 application/vnd.oasis.opendocument.presentation [OFFICE]
00069 application/vnd.oasis.opendocument.presentation-template [OFFICE]
00070 application/vnd.oasis.opendocument.spreadsheet [OFFICE]
00071 application/vnd.oasis.opendocument.spreadsheet-template [OFFICE]
00072 application/vnd.oasis.opendocument.text [OFFICE]
00073 application/vnd.oasis.opendocument.text-template [OFFICE]
00074 application/vnd.oasis.opendocument.text-master [OFFICE]
00075 application/vnd.oasis.opendocument.text-web [OFFICE]
00076 text/javascript application/x-javascript [EXECUTABLE]
00077 application/x-shockwave-flash [MULTIMEDIA]
00078 audio/midi [AUDIO]
00079 audio/x-aiff [AUDIO]
00080 audio/x-wav [AUDIO]
00081 audio/mp3 audio/mpeg [AUDIO]
00082 application/ogg audio/ogg video/ogg [MULTIMEDIA]
00083 image/x-bmp image/bmp [BITMAP]
00084 image/gif [BITMAP]
00085 image/jpeg [BITMAP]
00086 image/png [BITMAP]
00087 image/svg+xml [DRAWING]
00088 image/tiff [BITMAP]
00089 image/vnd.djvu [BITMAP]
00090 image/x-xcf [BITMAP]
00091 image/x-portable-pixmap [BITMAP]
00092 text/plain [TEXT]
00093 text/html [TEXT]
00094 video/ogg [VIDEO]
00095 video/mpeg [VIDEO]
00096 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
00097 END_STRING
00098 );
00099
00100 #note: because this file is possibly included by a function,
00101 #we need to access the global scope explicitely!
00102 global $wgLoadFileinfoExtension;
00103
00104 if ($wgLoadFileinfoExtension) {
00105 if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
00106 }
00107
00115 class MimeMagic {
00116
00121 var $mMediaTypes= NULL;
00122
00125 var $mMimeTypeAliases= NULL;
00126
00129 var $mMimeToExt= NULL;
00130
00133 var $mExtToMime= NULL;
00134
00137 var $mIEAnalyzer;
00138
00141 private static $instance;
00142
00147 function __construct() {
00148
00149
00150
00151
00152 global $wgMimeTypeFile, $IP;
00153
00154 $types = MM_WELL_KNOWN_MIME_TYPES;
00155
00156 if ( $wgMimeTypeFile == 'includes/mime.types' ) {
00157 $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
00158 }
00159
00160 if ( $wgMimeTypeFile ) {
00161 if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
00162 wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
00163 $types .= "\n";
00164 $types .= file_get_contents( $wgMimeTypeFile );
00165 } else {
00166 wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" );
00167 }
00168 } else {
00169 wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" );
00170 }
00171
00172 $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
00173 $types = str_replace( "\t", " ", $types );
00174
00175 $this->mMimeToExt = array();
00176 $this->mToMime = array();
00177
00178 $lines = explode( "\n",$types );
00179 foreach ( $lines as $s ) {
00180 $s = trim( $s );
00181 if ( empty( $s ) ) continue;
00182 if ( strpos( $s, '#' ) === 0 ) continue;
00183
00184 $s = strtolower( $s );
00185 $i = strpos( $s, ' ' );
00186
00187 if ( $i === false ) continue;
00188
00189 #print "processing MIME line $s<br>";
00190
00191 $mime = substr( $s, 0, $i );
00192 $ext = trim( substr($s, $i+1 ) );
00193
00194 if ( empty( $ext ) ) continue;
00195
00196 if ( !empty( $this->mMimeToExt[$mime] ) ) {
00197 $this->mMimeToExt[$mime] .= ' ' . $ext;
00198 } else {
00199 $this->mMimeToExt[$mime] = $ext;
00200 }
00201
00202 $extensions = explode( ' ', $ext );
00203
00204 foreach ( $extensions as $e ) {
00205 $e = trim( $e );
00206 if ( empty( $e ) ) continue;
00207
00208 if ( !empty( $this->mExtToMime[$e] ) ) {
00209 $this->mExtToMime[$e] .= ' ' . $mime;
00210 } else {
00211 $this->mExtToMime[$e] = $mime;
00212 }
00213 }
00214 }
00215
00216
00217
00218
00219
00220 global $wgMimeInfoFile;
00221 if ( $wgMimeInfoFile == 'includes/mime.info' ) {
00222 $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
00223 }
00224
00225 $info = MM_WELL_KNOWN_MIME_INFO;
00226
00227 if ( $wgMimeInfoFile ) {
00228 if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
00229 wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" );
00230 $info .= "\n";
00231 $info .= file_get_contents( $wgMimeInfoFile );
00232 } else {
00233 wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n");
00234 }
00235 } else {
00236 wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n");
00237 }
00238
00239 $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info);
00240 $info = str_replace( "\t", " ", $info );
00241
00242 $this->mMimeTypeAliases = array();
00243 $this->mMediaTypes = array();
00244
00245 $lines = explode( "\n", $info );
00246 foreach ( $lines as $s ) {
00247 $s = trim( $s );
00248 if ( empty( $s ) ) continue;
00249 if ( strpos( $s, '#' ) === 0 ) continue;
00250
00251 $s = strtolower( $s );
00252 $i = strpos( $s, ' ' );
00253
00254 if ( $i === false ) continue;
00255
00256 #print "processing MIME INFO line $s<br>";
00257
00258 $match = array();
00259 if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
00260 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
00261 $mtype = trim( strtoupper( $match[1] ) );
00262 } else {
00263 $mtype = MEDIATYPE_UNKNOWN;
00264 }
00265
00266 $m = explode( ' ', $s );
00267
00268 if ( !isset( $this->mMediaTypes[$mtype] ) ) {
00269 $this->mMediaTypes[$mtype] = array();
00270 }
00271
00272 foreach ( $m as $mime ) {
00273 $mime = trim( $mime );
00274 if ( empty( $mime ) ) continue;
00275
00276 $this->mMediaTypes[$mtype][] = $mime;
00277 }
00278
00279 if ( sizeof( $m ) > 1 ) {
00280 $main = $m[0];
00281 for ( $i=1; $i<sizeof($m); $i += 1 ) {
00282 $mime = $m[$i];
00283 $this->mMimeTypeAliases[$mime] = $main;
00284 }
00285 }
00286 }
00287
00288 }
00289
00293 static function &singleton() {
00294 if ( !isset( self::$instance ) ) {
00295 self::$instance = new MimeMagic;
00296 }
00297 return self::$instance;
00298 }
00299
00303 function getExtensionsForType( $mime ) {
00304 $mime = strtolower( $mime );
00305
00306 $r = @$this->mMimeToExt[$mime];
00307
00308 if ( @!$r and isset( $this->mMimeTypeAliases[$mime] ) ) {
00309 $mime = $this->mMimeTypeAliases[$mime];
00310 $r = @$this->mMimeToExt[$mime];
00311 }
00312
00313 return $r;
00314 }
00315
00319 function getTypesForExtension( $ext ) {
00320 $ext = strtolower( $ext );
00321
00322 $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
00323 return $r;
00324 }
00325
00329 function guessTypesForExtension( $ext ) {
00330 $m = $this->getTypesForExtension( $ext );
00331 if ( is_null( $m ) ) return NULL;
00332
00333 $m = trim( $m );
00334 $m = preg_replace( '/\s.*$/', '', $m );
00335
00336 return $m;
00337 }
00338
00339
00344 function isMatchingExtension( $extension, $mime ) {
00345 $ext = $this->getExtensionsForType( $mime );
00346
00347 if ( !$ext ) {
00348 return NULL;
00349 }
00350
00351 $ext = explode( ' ', $ext );
00352
00353 $extension = strtolower( $extension );
00354 if ( in_array( $extension, $ext ) ) {
00355 return true;
00356 }
00357
00358 return false;
00359 }
00360
00364 function isPHPImageType( $mime ) {
00365 #as defined by imagegetsize and image_type_to_mime
00366 static $types = array(
00367 'image/gif', 'image/jpeg', 'image/png',
00368 'image/x-bmp', 'image/xbm', 'image/tiff',
00369 'image/jp2', 'image/jpeg2000', 'image/iff',
00370 'image/xbm', 'image/x-xbitmap',
00371 'image/vnd.wap.wbmp', 'image/vnd.xiff',
00372 'image/x-photoshop',
00373 'application/x-shockwave-flash',
00374 );
00375
00376 return in_array( $mime, $types );
00377 }
00378
00390 function isRecognizableExtension( $extension ) {
00391 static $types = array(
00392
00393 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
00394 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
00395 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
00396 'xbm',
00397
00398
00399 'djvu', 'ogg', 'ogv', 'mid', 'pdf', 'wmf', 'xcf',
00400
00401
00402 'svg',
00403 );
00404 return in_array( strtolower( $extension ), $types );
00405 }
00406
00407
00418 function guessMimeType( $file, $ext = true ) {
00419 $mime = $this->doGuessMimeType( $file, $ext );
00420
00421 if( !$mime ) {
00422 wfDebug( __METHOD__.": internal type detection failed for $file (.$ext)...\n" );
00423 $mime = $this->detectMimeType( $file, $ext );
00424 }
00425
00426 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00427 $mime = $this->mMimeTypeAliases[$mime];
00428 }
00429
00430 wfDebug(__METHOD__.": final mime type of $file: $mime\n");
00431 return $mime;
00432 }
00433
00434 function doGuessMimeType( $file, $ext = true ) {
00435
00436 wfSuppressWarnings();
00437 $f = fopen( $file, "rt" );
00438 wfRestoreWarnings();
00439 if( !$f ) return "unknown/unknown";
00440 $head = fread( $f, 1024 );
00441 fseek( $f, -65558, SEEK_END );
00442 $tail = fread( $f, 65558 );
00443 fclose( $f );
00444
00445
00446 $headers = array(
00447
00448 'MThd' => 'audio/midi',
00449 'OggS' => 'application/ogg',
00450
00451
00452
00453 "\x01\x00\x09\x00" => 'application/x-msmetafile',
00454 "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
00455 '%PDF' => 'application/pdf',
00456 'gimp xcf' => 'image/x-xcf',
00457
00458
00459 'MZ' => 'application/octet-stream',
00460 "\xca\xfe\xba\xbe" => 'application/octet-stream',
00461 "\x7fELF" => 'application/octet-stream',
00462 );
00463
00464 foreach( $headers as $magic => $candidate ) {
00465 if( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
00466 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
00467 return $candidate;
00468 }
00469 }
00470
00471
00472
00473
00474
00475
00476
00477 if( ( strpos( $head, '<?php' ) !== false ) ||
00478 ( strpos( $head, '<? ' ) !== false ) ||
00479 ( strpos( $head, "<?\n" ) !== false ) ||
00480 ( strpos( $head, "<?\t" ) !== false ) ||
00481 ( strpos( $head, "<?=" ) !== false ) ||
00482
00483 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
00484 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
00485 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
00486 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
00487 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
00488
00489 wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
00490 return "application/x-php";
00491 }
00492
00493
00494
00495
00496 $xml = new XmlTypeCheck( $file );
00497 if( $xml->wellFormed ) {
00498 global $wgXMLMimeTypes;
00499 if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
00500 return $wgXMLMimeTypes[$xml->getRootElement()];
00501 } else {
00502 return 'application/xml';
00503 }
00504 }
00505
00506
00507
00508
00509 $script_type = NULL;
00510
00511 # detect by shebang
00512 if ( substr( $head, 0, 2) == "#!" ) {
00513 $script_type = "ASCII";
00514 } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
00515 $script_type = "UTF-8";
00516 } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
00517 $script_type = "UTF-16BE";
00518 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
00519 $script_type= "UTF-16LE";
00520 }
00521
00522 if ( $script_type ) {
00523 if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
00524
00525 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
00526 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
00527 $head = '';
00528 foreach( $chars as $codepoint ) {
00529 if( $codepoint < 128 ) {
00530 $head .= chr( $codepoint );
00531 } else {
00532 $head .= '?';
00533 }
00534 }
00535 }
00536
00537 $match = array();
00538
00539 if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
00540 $mime = "application/x-{$match[2]}";
00541 wfDebug( __METHOD__.": shell script recognized as $mime\n" );
00542 return $mime;
00543 }
00544 }
00545
00546
00547 if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
00548 wfDebug( __METHOD__.": ZIP header present at end of $file\n" );
00549 return $this->detectZipType( $head );
00550 }
00551
00552 wfSuppressWarnings();
00553 $gis = getimagesize( $file );
00554 wfRestoreWarnings();
00555
00556 if( $gis && isset( $gis['mime'] ) ) {
00557 $mime = $gis['mime'];
00558 wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" );
00559 return $mime;
00560 }
00561
00562
00563 $deja = new DjVuImage( $file );
00564 if( $deja->isValid() ) {
00565 wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
00566 return 'image/vnd.djvu';
00567 }
00568
00569 return false;
00570 }
00571
00580 function detectZipType( $header ) {
00581 $opendocTypes = array(
00582 'chart-template',
00583 'chart',
00584 'formula-template',
00585 'formula',
00586 'graphics-template',
00587 'graphics',
00588 'image-template',
00589 'image',
00590 'presentation-template',
00591 'presentation',
00592 'spreadsheet-template',
00593 'spreadsheet',
00594 'text-template',
00595 'text-master',
00596 'text-web',
00597 'text' );
00598
00599
00600 $types = '(?:' . implode( '|', $opendocTypes ) . ')';
00601 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
00602 wfDebug( __METHOD__.": $opendocRegex\n" );
00603
00604 if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
00605 $mime = $matches[1];
00606 wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
00607 return $mime;
00608 } else {
00609 wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" );
00610 return 'application/zip';
00611 }
00612 }
00613
00629 function detectMimeType( $file, $ext = true ) {
00630 global $wgMimeDetectorCommand;
00631
00632 $m = NULL;
00633 if ( $wgMimeDetectorCommand ) {
00634 $fn = wfEscapeShellArg( $file );
00635 $m = `$wgMimeDetectorCommand $fn`;
00636 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
00637
00638 # This required the fileinfo extension by PECL,
00639 # see http://pecl.php.net/package/fileinfo
00640 # This must be compiled into PHP
00641 #
00642 # finfo is the official replacement for the deprecated
00643 # mime_content_type function, see below.
00644 #
00645 # If you may need to load the fileinfo extension at runtime, set
00646 # $wgLoadFileinfoExtension in LocalSettings.php
00647
00648 $mime_magic_resource = finfo_open(FILEINFO_MIME);
00649
00650 if ($mime_magic_resource) {
00651 $m = finfo_file( $mime_magic_resource, $file );
00652 finfo_close( $mime_magic_resource );
00653 } else {
00654 wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
00655 }
00656 } elseif ( function_exists( "mime_content_type" ) ) {
00657
00658 # NOTE: this function is available since PHP 4.3.0, but only if
00659 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
00660 #
00661 # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
00662 # sometimes, this may even be needed under linus/unix.
00663 #
00664 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
00665 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
00666
00667 $m = mime_content_type($file);
00668 } else {
00669 wfDebug( __METHOD__.": no magic mime detector found!\n" );
00670 }
00671
00672 if ( $m ) {
00673 # normalize
00674 $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
00675 $m = trim( $m );
00676 $m = strtolower( $m );
00677
00678 if ( strpos( $m, 'unknown' ) !== false ) {
00679 $m = NULL;
00680 } else {
00681 wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
00682 return $m;
00683 }
00684 }
00685
00686 # if desired, look at extension as a fallback.
00687 if ( $ext === true ) {
00688 $i = strrpos( $file, '.' );
00689 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
00690 }
00691 if ( $ext ) {
00692 if( $this->isRecognizableExtension( $ext ) ) {
00693 wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
00694 } else {
00695 $m = $this->guessTypesForExtension( $ext );
00696 if ( $m ) {
00697 wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
00698 return $m;
00699 }
00700 }
00701 }
00702
00703 #unknown type
00704 wfDebug( __METHOD__.": failed to guess mime type for $file!\n" );
00705 return "unknown/unknown";
00706 }
00707
00724 function getMediaType( $path = NULL, $mime = NULL ) {
00725 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
00726
00727 # If mime type is unknown, guess it
00728 if( !$mime ) $mime = $this->guessMimeType( $path, false );
00729
00730 # Special code for ogg - detect if it's video (theora),
00731 # else label it as sound.
00732 if( $mime == "application/ogg" && file_exists( $path ) ) {
00733
00734
00735 $f = fopen( $path, "rt" );
00736 if ( !$f ) return MEDIATYPE_UNKNOWN;
00737 $head = fread( $f, 256 );
00738 fclose( $f );
00739
00740 $head = strtolower( $head );
00741
00742 # This is an UGLY HACK, file should be parsed correctly
00743 if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
00744 elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
00745 elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
00746 elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
00747 else return MEDIATYPE_MULTIMEDIA;
00748 }
00749
00750 # check for entry for full mime type
00751 if( $mime ) {
00752 $type = $this->findMediaType( $mime );
00753 if( $type !== MEDIATYPE_UNKNOWN ) return $type;
00754 }
00755
00756 # Check for entry for file extension
00757 $e = NULL;
00758 if ( $path ) {
00759 $i = strrpos( $path, '.' );
00760 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
00761
00762 # TODO: look at multi-extension if this fails, parse from full path
00763
00764 $type = $this->findMediaType( '.' . $e );
00765 if ( $type !== MEDIATYPE_UNKNOWN ) return $type;
00766 }
00767
00768 # Check major mime type
00769 if( $mime ) {
00770 $i = strpos( $mime, '/' );
00771 if( $i !== false ) {
00772 $major = substr( $mime, 0, $i );
00773 $type = $this->findMediaType( $major );
00774 if( $type !== MEDIATYPE_UNKNOWN ) return $type;
00775 }
00776 }
00777
00778 if( !$type ) $type = MEDIATYPE_UNKNOWN;
00779
00780 return $type;
00781 }
00782
00790 function findMediaType( $extMime ) {
00791 if ( strpos( $extMime, '.' ) === 0 ) { #if it's an extension, look up the mime types
00792 $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
00793 if ( !$m ) return MEDIATYPE_UNKNOWN;
00794
00795 $m = explode( ' ', $m );
00796 } else {
00797 # Normalize mime type
00798 if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
00799 $extMime = $this->mMimeTypeAliases[$extMime];
00800 }
00801
00802 $m = array($extMime);
00803 }
00804
00805 foreach ( $m as $mime ) {
00806 foreach ( $this->mMediaTypes as $type => $codes ) {
00807 if ( in_array($mime, $codes, true ) ) {
00808 return $type;
00809 }
00810 }
00811 }
00812
00813 return MEDIATYPE_UNKNOWN;
00814 }
00815
00824 public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
00825 $ca = $this->getIEContentAnalyzer();
00826 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
00827 }
00828
00832 protected function getIEContentAnalyzer() {
00833 if ( is_null( $this->mIEAnalyzer ) ) {
00834 $this->mIEAnalyzer = new IEContentAnalyzer;
00835 }
00836 return $this->mIEAnalyzer;
00837 }
00838 }