MediaWiki
REL1_24
|
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 { 00149 protected $mMediaTypes = null; 00150 00153 protected $mMimeTypeAliases = null; 00154 00157 protected $mMimeToExt = null; 00158 00161 public $mExtToMime = null; 00162 00165 protected $mIEAnalyzer; 00166 00169 private $mExtraTypes = ''; 00170 00173 private $mExtraInfo = ''; 00174 00176 private $mConfig; 00177 00180 private static $instance = null; 00181 00189 function __construct( Config $config = null ) { 00190 if ( !$config ) { 00191 wfDebug( __METHOD__ . ' called with no Config instance passed to it' ); 00192 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' ); 00193 } 00194 $this->mConfig = $config; 00195 00200 global $IP; 00201 00202 # Allow media handling extensions adding MIME-types and MIME-info 00203 wfRunHooks( 'MimeMagicInit', array( $this ) ); 00204 00205 $types = MM_WELL_KNOWN_MIME_TYPES; 00206 00207 $mimeTypeFile = $this->mConfig->get( 'MimeTypeFile' ); 00208 if ( $mimeTypeFile == 'includes/mime.types' ) { 00209 $mimeTypeFile = "$IP/$mimeTypeFile"; 00210 } 00211 00212 if ( $mimeTypeFile ) { 00213 if ( is_file( $mimeTypeFile ) and is_readable( $mimeTypeFile ) ) { 00214 wfDebug( __METHOD__ . ": loading mime types from $mimeTypeFile\n" ); 00215 $types .= "\n"; 00216 $types .= file_get_contents( $mimeTypeFile ); 00217 } else { 00218 wfDebug( __METHOD__ . ": can't load mime types from $mimeTypeFile\n" ); 00219 } 00220 } else { 00221 wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" ); 00222 } 00223 00224 $types .= "\n" . $this->mExtraTypes; 00225 00226 $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types ); 00227 $types = str_replace( "\t", " ", $types ); 00228 00229 $this->mMimeToExt = array(); 00230 $this->mExtToMime = array(); 00231 00232 $lines = explode( "\n", $types ); 00233 foreach ( $lines as $s ) { 00234 $s = trim( $s ); 00235 if ( empty( $s ) ) { 00236 continue; 00237 } 00238 if ( strpos( $s, '#' ) === 0 ) { 00239 continue; 00240 } 00241 00242 $s = strtolower( $s ); 00243 $i = strpos( $s, ' ' ); 00244 00245 if ( $i === false ) { 00246 continue; 00247 } 00248 00249 $mime = substr( $s, 0, $i ); 00250 $ext = trim( substr( $s, $i + 1 ) ); 00251 00252 if ( empty( $ext ) ) { 00253 continue; 00254 } 00255 00256 if ( !empty( $this->mMimeToExt[$mime] ) ) { 00257 $this->mMimeToExt[$mime] .= ' ' . $ext; 00258 } else { 00259 $this->mMimeToExt[$mime] = $ext; 00260 } 00261 00262 $extensions = explode( ' ', $ext ); 00263 00264 foreach ( $extensions as $e ) { 00265 $e = trim( $e ); 00266 if ( empty( $e ) ) { 00267 continue; 00268 } 00269 00270 if ( !empty( $this->mExtToMime[$e] ) ) { 00271 $this->mExtToMime[$e] .= ' ' . $mime; 00272 } else { 00273 $this->mExtToMime[$e] = $mime; 00274 } 00275 } 00276 } 00277 00282 $mimeInfoFile = $this->mConfig->get( 'MimeInfoFile' ); 00283 if ( $mimeInfoFile == 'includes/mime.info' ) { 00284 $mimeInfoFile = "$IP/$mimeInfoFile"; 00285 } 00286 00287 $info = MM_WELL_KNOWN_MIME_INFO; 00288 00289 if ( $mimeInfoFile ) { 00290 if ( is_file( $mimeInfoFile ) and is_readable( $mimeInfoFile ) ) { 00291 wfDebug( __METHOD__ . ": loading mime info from $mimeInfoFile\n" ); 00292 $info .= "\n"; 00293 $info .= file_get_contents( $mimeInfoFile ); 00294 } else { 00295 wfDebug( __METHOD__ . ": can't load mime info from $mimeInfoFile\n" ); 00296 } 00297 } else { 00298 wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" ); 00299 } 00300 00301 $info .= "\n" . $this->mExtraInfo; 00302 00303 $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info ); 00304 $info = str_replace( "\t", " ", $info ); 00305 00306 $this->mMimeTypeAliases = array(); 00307 $this->mMediaTypes = array(); 00308 00309 $lines = explode( "\n", $info ); 00310 foreach ( $lines as $s ) { 00311 $s = trim( $s ); 00312 if ( empty( $s ) ) { 00313 continue; 00314 } 00315 if ( strpos( $s, '#' ) === 0 ) { 00316 continue; 00317 } 00318 00319 $s = strtolower( $s ); 00320 $i = strpos( $s, ' ' ); 00321 00322 if ( $i === false ) { 00323 continue; 00324 } 00325 00326 #print "processing MIME INFO line $s<br>"; 00327 00328 $match = array(); 00329 if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) { 00330 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s ); 00331 $mtype = trim( strtoupper( $match[1] ) ); 00332 } else { 00333 $mtype = MEDIATYPE_UNKNOWN; 00334 } 00335 00336 $m = explode( ' ', $s ); 00337 00338 if ( !isset( $this->mMediaTypes[$mtype] ) ) { 00339 $this->mMediaTypes[$mtype] = array(); 00340 } 00341 00342 foreach ( $m as $mime ) { 00343 $mime = trim( $mime ); 00344 if ( empty( $mime ) ) { 00345 continue; 00346 } 00347 00348 $this->mMediaTypes[$mtype][] = $mime; 00349 } 00350 00351 if ( count( $m ) > 1 ) { 00352 $main = $m[0]; 00353 $mCount = count( $m ); 00354 for ( $i = 1; $i < $mCount; $i += 1 ) { 00355 $mime = $m[$i]; 00356 $this->mMimeTypeAliases[$mime] = $main; 00357 } 00358 } 00359 } 00360 } 00361 00366 public static function singleton() { 00367 if ( self::$instance === null ) { 00368 self::$instance = new MimeMagic( 00369 ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) 00370 ); 00371 } 00372 return self::$instance; 00373 } 00374 00381 public function addExtraTypes( $types ) { 00382 $this->mExtraTypes .= "\n" . $types; 00383 } 00384 00391 public function addExtraInfo( $info ) { 00392 $this->mExtraInfo .= "\n" . $info; 00393 } 00394 00403 public function getExtensionsForType( $mime ) { 00404 $mime = strtolower( $mime ); 00405 00406 // Check the mime-to-ext map 00407 if ( isset( $this->mMimeToExt[$mime] ) ) { 00408 return $this->mMimeToExt[$mime]; 00409 } 00410 00411 // Resolve the MIME type to the canonical type 00412 if ( isset( $this->mMimeTypeAliases[$mime] ) ) { 00413 $mime = $this->mMimeTypeAliases[$mime]; 00414 if ( isset( $this->mMimeToExt[$mime] ) ) { 00415 return $this->mMimeToExt[$mime]; 00416 } 00417 } 00418 00419 return null; 00420 } 00421 00429 public function getTypesForExtension( $ext ) { 00430 $ext = strtolower( $ext ); 00431 00432 $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null; 00433 return $r; 00434 } 00435 00443 public function guessTypesForExtension( $ext ) { 00444 $m = $this->getTypesForExtension( $ext ); 00445 if ( is_null( $m ) ) { 00446 return null; 00447 } 00448 00449 // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient 00450 $m = trim( $m ); 00451 $m = preg_replace( '/\s.*$/', '', $m ); 00452 00453 return $m; 00454 } 00455 00465 public function isMatchingExtension( $extension, $mime ) { 00466 $ext = $this->getExtensionsForType( $mime ); 00467 00468 if ( !$ext ) { 00469 return null; // Unknown MIME type 00470 } 00471 00472 $ext = explode( ' ', $ext ); 00473 00474 $extension = strtolower( $extension ); 00475 return in_array( $extension, $ext ); 00476 } 00477 00486 public function isPHPImageType( $mime ) { 00487 // As defined by imagegetsize and image_type_to_mime 00488 static $types = array( 00489 'image/gif', 'image/jpeg', 'image/png', 00490 'image/x-bmp', 'image/xbm', 'image/tiff', 00491 'image/jp2', 'image/jpeg2000', 'image/iff', 00492 'image/xbm', 'image/x-xbitmap', 00493 'image/vnd.wap.wbmp', 'image/vnd.xiff', 00494 'image/x-photoshop', 00495 'application/x-shockwave-flash', 00496 ); 00497 00498 return in_array( $mime, $types ); 00499 } 00500 00513 function isRecognizableExtension( $extension ) { 00514 static $types = array( 00515 // Types recognized by getimagesize() 00516 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 00517 'bmp', 'tiff', 'tif', 'jpc', 'jp2', 00518 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 00519 'xbm', 00520 00521 // Formats we recognize magic numbers for 00522 'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 00523 'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka', 00524 'webp', 00525 00526 // XML formats we sure hope we recognize reliably 00527 'svg', 00528 ); 00529 return in_array( strtolower( $extension ), $types ); 00530 } 00531 00543 public function improveTypeFromExtension( $mime, $ext ) { 00544 if ( $mime === 'unknown/unknown' ) { 00545 if ( $this->isRecognizableExtension( $ext ) ) { 00546 wfDebug( __METHOD__ . ': refusing to guess mime type for .' . 00547 "$ext file, we should have recognized it\n" ); 00548 } else { 00549 // Not something we can detect, so simply 00550 // trust the file extension 00551 $mime = $this->guessTypesForExtension( $ext ); 00552 } 00553 } elseif ( $mime === 'application/x-opc+zip' ) { 00554 if ( $this->isMatchingExtension( $ext, $mime ) ) { 00555 // A known file extension for an OPC file, 00556 // find the proper MIME type for that file extension 00557 $mime = $this->guessTypesForExtension( $ext ); 00558 } else { 00559 wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " . 00560 ".$ext is not a known OPC extension.\n" ); 00561 $mime = 'application/zip'; 00562 } 00563 } elseif ( $mime === 'text/plain' && $this->findMediaType( ".$ext" ) === MEDIATYPE_TEXT ) { 00564 // Textual types are sometimes not recognized properly. 00565 // If detected as text/plain, and has an extension which is textual 00566 // improve to the extension's type. For example, csv and json are often 00567 // misdetected as text/plain. 00568 $mime = $this->guessTypesForExtension( $ext ); 00569 } 00570 00571 # Media handling extensions can improve the MIME detected 00572 wfRunHooks( 'MimeMagicImproveFromExtension', array( $this, $ext, &$mime ) ); 00573 00574 if ( isset( $this->mMimeTypeAliases[$mime] ) ) { 00575 $mime = $this->mMimeTypeAliases[$mime]; 00576 } 00577 00578 wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" ); 00579 return $mime; 00580 } 00581 00596 public function guessMimeType( $file, $ext = true ) { 00597 if ( $ext ) { // TODO: make $ext default to false. Or better, remove it. 00598 wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " . 00599 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); 00600 } 00601 00602 $mime = $this->doGuessMimeType( $file, $ext ); 00603 00604 if ( !$mime ) { 00605 wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" ); 00606 $mime = $this->detectMimeType( $file, $ext ); 00607 } 00608 00609 if ( isset( $this->mMimeTypeAliases[$mime] ) ) { 00610 $mime = $this->mMimeTypeAliases[$mime]; 00611 } 00612 00613 wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" ); 00614 return $mime; 00615 } 00616 00625 private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param 00626 // Read a chunk of the file 00627 wfSuppressWarnings(); 00628 $f = fopen( $file, 'rb' ); 00629 wfRestoreWarnings(); 00630 00631 if ( !$f ) { 00632 return 'unknown/unknown'; 00633 } 00634 00635 $fsize = filesize( $file ); 00636 if ( $fsize === false ) { 00637 return 'unknown/unknown'; 00638 } 00639 00640 $head = fread( $f, 1024 ); 00641 $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR 00642 if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) { 00643 throw new MWException( 00644 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ ); 00645 } 00646 $tail = fread( $f, $tailLength ); 00647 fclose( $f ); 00648 00649 wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" ); 00650 00651 // Hardcode a few magic number checks... 00652 $headers = array( 00653 // Multimedia... 00654 'MThd' => 'audio/midi', 00655 'OggS' => 'application/ogg', 00656 00657 // Image formats... 00658 // Note that WMF may have a bare header, no magic number. 00659 "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives? 00660 "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile', 00661 '%PDF' => 'application/pdf', 00662 'gimp xcf' => 'image/x-xcf', 00663 00664 // Some forbidden fruit... 00665 'MZ' => 'application/octet-stream', // DOS/Windows executable 00666 "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary 00667 "\x7fELF" => 'application/octet-stream', // ELF binary 00668 ); 00669 00670 foreach ( $headers as $magic => $candidate ) { 00671 if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) { 00672 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" ); 00673 return $candidate; 00674 } 00675 } 00676 00677 /* Look for WebM and Matroska files */ 00678 if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) { 00679 $doctype = strpos( $head, "\x42\x82" ); 00680 if ( $doctype ) { 00681 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers) 00682 $data = substr( $head, $doctype + 3, 8 ); 00683 if ( strncmp( $data, "matroska", 8 ) == 0 ) { 00684 wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" ); 00685 return "video/x-matroska"; 00686 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) { 00687 wfDebug( __METHOD__ . ": recognized file as video/webm\n" ); 00688 return "video/webm"; 00689 } 00690 } 00691 wfDebug( __METHOD__ . ": unknown EBML file\n" ); 00692 return "unknown/unknown"; 00693 } 00694 00695 /* Look for WebP */ 00696 if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8 ), "WEBPVP8 ", 8 ) == 0 ) { 00697 wfDebug( __METHOD__ . ": recognized file as image/webp\n" ); 00698 return "image/webp"; 00699 } 00700 00713 if ( ( strpos( $head, '<?php' ) !== false ) || 00714 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) || 00715 ( strpos( $head, "<\x00?\x00 " ) !== false ) || 00716 ( strpos( $head, "<\x00?\x00\n" ) !== false ) || 00717 ( strpos( $head, "<\x00?\x00\t" ) !== false ) || 00718 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) { 00719 00720 wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" ); 00721 return 'application/x-php'; 00722 } 00723 00727 $xml = new XmlTypeCheck( $file ); 00728 if ( $xml->wellFormed ) { 00729 $xmlMimeTypes = $this->mConfig->get( 'XMLMimeTypes' ); 00730 if ( isset( $xmlMimeTypes[$xml->getRootElement()] ) ) { 00731 return $xmlMimeTypes[$xml->getRootElement()]; 00732 } else { 00733 return 'application/xml'; 00734 } 00735 } 00736 00740 $script_type = null; 00741 00742 # detect by shebang 00743 if ( substr( $head, 0, 2 ) == "#!" ) { 00744 $script_type = "ASCII"; 00745 } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) { 00746 $script_type = "UTF-8"; 00747 } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) { 00748 $script_type = "UTF-16BE"; 00749 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) { 00750 $script_type = "UTF-16LE"; 00751 } 00752 00753 if ( $script_type ) { 00754 if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) { 00755 // Quick and dirty fold down to ASCII! 00756 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ); 00757 $chars = unpack( $pack[$script_type], substr( $head, 2 ) ); 00758 $head = ''; 00759 foreach ( $chars as $codepoint ) { 00760 if ( $codepoint < 128 ) { 00761 $head .= chr( $codepoint ); 00762 } else { 00763 $head .= '?'; 00764 } 00765 } 00766 } 00767 00768 $match = array(); 00769 00770 if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) { 00771 $mime = "application/x-{$match[2]}"; 00772 wfDebug( __METHOD__ . ": shell script recognized as $mime\n" ); 00773 return $mime; 00774 } 00775 } 00776 00777 // Check for ZIP variants (before getimagesize) 00778 if ( strpos( $tail, "PK\x05\x06" ) !== false ) { 00779 wfDebug( __METHOD__ . ": ZIP header present in $file\n" ); 00780 return $this->detectZipType( $head, $tail, $ext ); 00781 } 00782 00783 wfSuppressWarnings(); 00784 $gis = getimagesize( $file ); 00785 wfRestoreWarnings(); 00786 00787 if ( $gis && isset( $gis['mime'] ) ) { 00788 $mime = $gis['mime']; 00789 wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" ); 00790 return $mime; 00791 } 00792 00793 // Also test DjVu 00794 $deja = new DjVuImage( $file ); 00795 if ( $deja->isValid() ) { 00796 wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" ); 00797 return 'image/vnd.djvu'; 00798 } 00799 00800 # Media handling extensions can guess the MIME by content 00801 # It's intentionally here so that if core is wrong about a type (false positive), 00802 # people will hopefully nag and submit patches :) 00803 $mime = false; 00804 # Some strings by reference for performance - assuming well-behaved hooks 00805 wfRunHooks( 00806 'MimeMagicGuessFromContent', 00807 array( $this, &$head, &$tail, $file, &$mime ) 00808 ); 00809 00810 return $mime; 00811 } 00812 00826 function detectZipType( $header, $tail = null, $ext = false ) { 00827 if ( $ext ) { # TODO: remove $ext param 00828 wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " . 00829 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); 00830 } 00831 00832 $mime = 'application/zip'; 00833 $opendocTypes = array( 00834 'chart-template', 00835 'chart', 00836 'formula-template', 00837 'formula', 00838 'graphics-template', 00839 'graphics', 00840 'image-template', 00841 'image', 00842 'presentation-template', 00843 'presentation', 00844 'spreadsheet-template', 00845 'spreadsheet', 00846 'text-template', 00847 'text-master', 00848 'text-web', 00849 'text' ); 00850 00851 // http://lists.oasis-open.org/archives/office/200505/msg00006.html 00852 $types = '(?:' . implode( '|', $opendocTypes ) . ')'; 00853 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/"; 00854 00855 $openxmlRegex = "/^\[Content_Types\].xml/"; 00856 00857 if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) { 00858 $mime = $matches[1]; 00859 wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" ); 00860 } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) { 00861 $mime = "application/x-opc+zip"; 00862 # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere 00863 if ( $ext !== true && $ext !== false ) { 00868 if ( $this->isMatchingExtension( $ext, $mime ) ) { 00869 /* A known file extension for an OPC file, 00870 * find the proper mime type for that file extension 00871 */ 00872 $mime = $this->guessTypesForExtension( $ext ); 00873 } else { 00874 $mime = "application/zip"; 00875 } 00876 } 00877 wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" ); 00878 } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" && 00879 ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false && 00880 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) { 00881 if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) { 00882 $mime = "application/msword"; 00883 } 00884 switch ( substr( $header, 512, 6 ) ) { 00885 case "\xEC\xA5\xC1\x00\x0E\x00": 00886 case "\xEC\xA5\xC1\x00\x1C\x00": 00887 case "\xEC\xA5\xC1\x00\x43\x00": 00888 $mime = "application/vnd.ms-powerpoint"; 00889 break; 00890 case "\xFD\xFF\xFF\xFF\x10\x00": 00891 case "\xFD\xFF\xFF\xFF\x1F\x00": 00892 case "\xFD\xFF\xFF\xFF\x22\x00": 00893 case "\xFD\xFF\xFF\xFF\x23\x00": 00894 case "\xFD\xFF\xFF\xFF\x28\x00": 00895 case "\xFD\xFF\xFF\xFF\x29\x00": 00896 case "\xFD\xFF\xFF\xFF\x10\x02": 00897 case "\xFD\xFF\xFF\xFF\x1F\x02": 00898 case "\xFD\xFF\xFF\xFF\x22\x02": 00899 case "\xFD\xFF\xFF\xFF\x23\x02": 00900 case "\xFD\xFF\xFF\xFF\x28\x02": 00901 case "\xFD\xFF\xFF\xFF\x29\x02": 00902 $mime = "application/vnd.msexcel"; 00903 break; 00904 } 00905 00906 wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" ); 00907 } else { 00908 wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" ); 00909 } 00910 return $mime; 00911 } 00912 00931 private function detectMimeType( $file, $ext = true ) { 00933 if ( $ext ) { 00934 wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " 00935 . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" ); 00936 } 00937 00938 $mimeDetectorCommand = $this->mConfig->get( 'MimeDetectorCommand' ); 00939 $m = null; 00940 if ( $mimeDetectorCommand ) { 00941 $args = wfEscapeShellArg( $file ); 00942 $m = wfShellExec( "$mimeDetectorCommand $args" ); 00943 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) { 00944 $mime_magic_resource = finfo_open( FILEINFO_MIME ); 00945 00946 if ( $mime_magic_resource ) { 00947 $m = finfo_file( $mime_magic_resource, $file ); 00948 finfo_close( $mime_magic_resource ); 00949 } else { 00950 wfDebug( __METHOD__ . ": finfo_open failed on " . FILEINFO_MIME . "!\n" ); 00951 } 00952 } else { 00953 wfDebug( __METHOD__ . ": no magic mime detector found!\n" ); 00954 } 00955 00956 if ( $m ) { 00957 # normalize 00958 $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc 00959 $m = trim( $m ); 00960 $m = strtolower( $m ); 00961 00962 if ( strpos( $m, 'unknown' ) !== false ) { 00963 $m = null; 00964 } else { 00965 wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" ); 00966 return $m; 00967 } 00968 } 00969 00970 // If desired, look at extension as a fallback. 00971 if ( $ext === true ) { 00972 $i = strrpos( $file, '.' ); 00973 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' ); 00974 } 00975 if ( $ext ) { 00976 if ( $this->isRecognizableExtension( $ext ) ) { 00977 wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, " 00978 . "we should have recognized it\n" ); 00979 } else { 00980 $m = $this->guessTypesForExtension( $ext ); 00981 if ( $m ) { 00982 wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" ); 00983 return $m; 00984 } 00985 } 00986 } 00987 00988 // Unknown type 00989 wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" ); 00990 return 'unknown/unknown'; 00991 } 00992 01009 function getMediaType( $path = null, $mime = null ) { 01010 if ( !$mime && !$path ) { 01011 return MEDIATYPE_UNKNOWN; 01012 } 01013 01014 // If MIME type is unknown, guess it 01015 if ( !$mime ) { 01016 $mime = $this->guessMimeType( $path, false ); 01017 } 01018 01019 // Special code for ogg - detect if it's video (theora), 01020 // else label it as sound. 01021 if ( $mime == 'application/ogg' && file_exists( $path ) ) { 01022 01023 // Read a chunk of the file 01024 $f = fopen( $path, "rt" ); 01025 if ( !$f ) { 01026 return MEDIATYPE_UNKNOWN; 01027 } 01028 $head = fread( $f, 256 ); 01029 fclose( $f ); 01030 01031 $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) ); 01032 01033 // This is an UGLY HACK, file should be parsed correctly 01034 if ( strpos( $head, 'theora' ) !== false ) { 01035 return MEDIATYPE_VIDEO; 01036 } elseif ( strpos( $head, 'vorbis' ) !== false ) { 01037 return MEDIATYPE_AUDIO; 01038 } elseif ( strpos( $head, 'flac' ) !== false ) { 01039 return MEDIATYPE_AUDIO; 01040 } elseif ( strpos( $head, 'speex' ) !== false ) { 01041 return MEDIATYPE_AUDIO; 01042 } else { 01043 return MEDIATYPE_MULTIMEDIA; 01044 } 01045 } 01046 01047 // Check for entry for full MIME type 01048 if ( $mime ) { 01049 $type = $this->findMediaType( $mime ); 01050 if ( $type !== MEDIATYPE_UNKNOWN ) { 01051 return $type; 01052 } 01053 } 01054 01055 // Check for entry for file extension 01056 if ( $path ) { 01057 $i = strrpos( $path, '.' ); 01058 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' ); 01059 01060 // TODO: look at multi-extension if this fails, parse from full path 01061 $type = $this->findMediaType( '.' . $e ); 01062 if ( $type !== MEDIATYPE_UNKNOWN ) { 01063 return $type; 01064 } 01065 } 01066 01067 // Check major MIME type 01068 if ( $mime ) { 01069 $i = strpos( $mime, '/' ); 01070 if ( $i !== false ) { 01071 $major = substr( $mime, 0, $i ); 01072 $type = $this->findMediaType( $major ); 01073 if ( $type !== MEDIATYPE_UNKNOWN ) { 01074 return $type; 01075 } 01076 } 01077 } 01078 01079 if ( !$type ) { 01080 $type = MEDIATYPE_UNKNOWN; 01081 } 01082 01083 return $type; 01084 } 01085 01096 function findMediaType( $extMime ) { 01097 if ( strpos( $extMime, '.' ) === 0 ) { 01098 // If it's an extension, look up the MIME types 01099 $m = $this->getTypesForExtension( substr( $extMime, 1 ) ); 01100 if ( !$m ) { 01101 return MEDIATYPE_UNKNOWN; 01102 } 01103 01104 $m = explode( ' ', $m ); 01105 } else { 01106 // Normalize MIME type 01107 if ( isset( $this->mMimeTypeAliases[$extMime] ) ) { 01108 $extMime = $this->mMimeTypeAliases[$extMime]; 01109 } 01110 01111 $m = array( $extMime ); 01112 } 01113 01114 foreach ( $m as $mime ) { 01115 foreach ( $this->mMediaTypes as $type => $codes ) { 01116 if ( in_array( $mime, $codes, true ) ) { 01117 return $type; 01118 } 01119 } 01120 } 01121 01122 return MEDIATYPE_UNKNOWN; 01123 } 01124 01134 public function getIEMimeTypes( $fileName, $chunk, $proposed ) { 01135 $ca = $this->getIEContentAnalyzer(); 01136 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed ); 01137 } 01138 01144 protected function getIEContentAnalyzer() { 01145 if ( is_null( $this->mIEAnalyzer ) ) { 01146 $this->mIEAnalyzer = new IEContentAnalyzer; 01147 } 01148 return $this->mIEAnalyzer; 01149 } 01150 }