MediaWiki  REL1_22
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 
00176     function __construct() {
00181         global $wgMimeTypeFile, $IP;
00182 
00183         $types = MM_WELL_KNOWN_MIME_TYPES;
00184 
00185         if ( $wgMimeTypeFile == 'includes/mime.types' ) {
00186             $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
00187         }
00188 
00189         if ( $wgMimeTypeFile ) {
00190             if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
00191                 wfDebug( __METHOD__ . ": loading mime types from $wgMimeTypeFile\n" );
00192                 $types .= "\n";
00193                 $types .= file_get_contents( $wgMimeTypeFile );
00194             } else {
00195                 wfDebug( __METHOD__ . ": can't load mime types from $wgMimeTypeFile\n" );
00196             }
00197         } else {
00198             wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" );
00199         }
00200 
00201         $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
00202         $types = str_replace( "\t", " ", $types );
00203 
00204         $this->mMimeToExt = array();
00205         $this->mToMime = array();
00206 
00207         $lines = explode( "\n", $types );
00208         foreach ( $lines as $s ) {
00209             $s = trim( $s );
00210             if ( empty( $s ) ) {
00211                 continue;
00212             }
00213             if ( strpos( $s, '#' ) === 0 ) {
00214                 continue;
00215             }
00216 
00217             $s = strtolower( $s );
00218             $i = strpos( $s, ' ' );
00219 
00220             if ( $i === false ) {
00221                 continue;
00222             }
00223 
00224             $mime = substr( $s, 0, $i );
00225             $ext = trim( substr( $s, $i + 1 ) );
00226 
00227             if ( empty( $ext ) ) {
00228                 continue;
00229             }
00230 
00231             if ( !empty( $this->mMimeToExt[$mime] ) ) {
00232                 $this->mMimeToExt[$mime] .= ' ' . $ext;
00233             } else {
00234                 $this->mMimeToExt[$mime] = $ext;
00235             }
00236 
00237             $extensions = explode( ' ', $ext );
00238 
00239             foreach ( $extensions as $e ) {
00240                 $e = trim( $e );
00241                 if ( empty( $e ) ) {
00242                     continue;
00243                 }
00244 
00245                 if ( !empty( $this->mExtToMime[$e] ) ) {
00246                     $this->mExtToMime[$e] .= ' ' . $mime;
00247                 } else {
00248                     $this->mExtToMime[$e] = $mime;
00249                 }
00250             }
00251         }
00252 
00257         global $wgMimeInfoFile;
00258         if ( $wgMimeInfoFile == 'includes/mime.info' ) {
00259             $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
00260         }
00261 
00262         $info = MM_WELL_KNOWN_MIME_INFO;
00263 
00264         if ( $wgMimeInfoFile ) {
00265             if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
00266                 wfDebug( __METHOD__ . ": loading mime info from $wgMimeInfoFile\n" );
00267                 $info .= "\n";
00268                 $info .= file_get_contents( $wgMimeInfoFile );
00269             } else {
00270                 wfDebug( __METHOD__ . ": can't load mime info from $wgMimeInfoFile\n" );
00271             }
00272         } else {
00273             wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" );
00274         }
00275 
00276         $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info );
00277         $info = str_replace( "\t", " ", $info );
00278 
00279         $this->mMimeTypeAliases = array();
00280         $this->mMediaTypes = array();
00281 
00282         $lines = explode( "\n", $info );
00283         foreach ( $lines as $s ) {
00284             $s = trim( $s );
00285             if ( empty( $s ) ) {
00286                 continue;
00287             }
00288             if ( strpos( $s, '#' ) === 0 ) {
00289                 continue;
00290             }
00291 
00292             $s = strtolower( $s );
00293             $i = strpos( $s, ' ' );
00294 
00295             if ( $i === false ) {
00296                 continue;
00297             }
00298 
00299             #print "processing MIME INFO line $s<br>";
00300 
00301             $match = array();
00302             if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
00303                 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
00304                 $mtype = trim( strtoupper( $match[1] ) );
00305             } else {
00306                 $mtype = MEDIATYPE_UNKNOWN;
00307             }
00308 
00309             $m = explode( ' ', $s );
00310 
00311             if ( !isset( $this->mMediaTypes[$mtype] ) ) {
00312                 $this->mMediaTypes[$mtype] = array();
00313             }
00314 
00315             foreach ( $m as $mime ) {
00316                 $mime = trim( $mime );
00317                 if ( empty( $mime ) ) {
00318                     continue;
00319                 }
00320 
00321                 $this->mMediaTypes[$mtype][] = $mime;
00322             }
00323 
00324             if ( count( $m ) > 1 ) {
00325                 $main = $m[0];
00326                 for ( $i = 1; $i < count( $m ); $i += 1 ) {
00327                     $mime = $m[$i];
00328                     $this->mMimeTypeAliases[$mime] = $main;
00329                 }
00330             }
00331         }
00332 
00333     }
00334 
00339     public static function &singleton() {
00340         if ( self::$instance === null ) {
00341             self::$instance = new MimeMagic;
00342         }
00343         return self::$instance;
00344     }
00345 
00354     public function getExtensionsForType( $mime ) {
00355         $mime = strtolower( $mime );
00356 
00357         // Check the mime-to-ext map
00358         if ( isset( $this->mMimeToExt[$mime] ) ) {
00359             return $this->mMimeToExt[$mime];
00360         }
00361 
00362         // Resolve the mime type to the canonical type
00363         if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00364             $mime = $this->mMimeTypeAliases[$mime];
00365             if ( isset( $this->mMimeToExt[$mime] ) ) {
00366                 return $this->mMimeToExt[$mime];
00367             }
00368         }
00369 
00370         return null;
00371     }
00372 
00380     public function getTypesForExtension( $ext ) {
00381         $ext = strtolower( $ext );
00382 
00383         $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
00384         return $r;
00385     }
00386 
00394     public function guessTypesForExtension( $ext ) {
00395         $m = $this->getTypesForExtension( $ext );
00396         if ( is_null( $m ) ) {
00397             return null;
00398         }
00399 
00400         // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient
00401         $m = trim( $m );
00402         $m = preg_replace( '/\s.*$/', '', $m );
00403 
00404         return $m;
00405     }
00406 
00416     public function isMatchingExtension( $extension, $mime ) {
00417         $ext = $this->getExtensionsForType( $mime );
00418 
00419         if ( !$ext ) {
00420             return null; // Unknown mime type
00421         }
00422 
00423         $ext = explode( ' ', $ext );
00424 
00425         $extension = strtolower( $extension );
00426         return in_array( $extension, $ext );
00427     }
00428 
00437     public function isPHPImageType( $mime ) {
00438         // As defined by imagegetsize and image_type_to_mime
00439         static $types = array(
00440             'image/gif', 'image/jpeg', 'image/png',
00441             'image/x-bmp', 'image/xbm', 'image/tiff',
00442             'image/jp2', 'image/jpeg2000', 'image/iff',
00443             'image/xbm', 'image/x-xbitmap',
00444             'image/vnd.wap.wbmp', 'image/vnd.xiff',
00445             'image/x-photoshop',
00446             'application/x-shockwave-flash',
00447         );
00448 
00449         return in_array( $mime, $types );
00450     }
00451 
00463     function isRecognizableExtension( $extension ) {
00464         static $types = array(
00465             // Types recognized by getimagesize()
00466             'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
00467             'bmp', 'tiff', 'tif', 'jpc', 'jp2',
00468             'jpx', 'jb2', 'swc', 'iff', 'wbmp',
00469             'xbm',
00470 
00471             // Formats we recognize magic numbers for
00472             'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx',
00473             'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
00474             'webp',
00475 
00476             // XML formats we sure hope we recognize reliably
00477             'svg',
00478         );
00479         return in_array( strtolower( $extension ), $types );
00480     }
00481 
00501     public function improveTypeFromExtension( $mime, $ext ) {
00502         if ( $mime === 'unknown/unknown' ) {
00503             if ( $this->isRecognizableExtension( $ext ) ) {
00504                 wfDebug( __METHOD__ . ': refusing to guess mime type for .' .
00505                     "$ext file, we should have recognized it\n" );
00506             } else {
00507                 // Not something we can detect, so simply
00508                 // trust the file extension
00509                 $mime = $this->guessTypesForExtension( $ext );
00510             }
00511         }
00512         elseif ( $mime === 'application/x-opc+zip' ) {
00513             if ( $this->isMatchingExtension( $ext, $mime ) ) {
00514                 // A known file extension for an OPC file,
00515                 // find the proper mime type for that file extension
00516                 $mime = $this->guessTypesForExtension( $ext );
00517             } else {
00518                 wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " .
00519                     ".$ext is not a known OPC extension.\n" );
00520                 $mime = 'application/zip';
00521             }
00522         }
00523 
00524         if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00525             $mime = $this->mMimeTypeAliases[$mime];
00526         }
00527 
00528         wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
00529         return $mime;
00530     }
00531 
00546     public function guessMimeType( $file, $ext = true ) {
00547         if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
00548             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00549                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00550         }
00551 
00552         $mime = $this->doGuessMimeType( $file, $ext );
00553 
00554         if ( !$mime ) {
00555             wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" );
00556             $mime = $this->detectMimeType( $file, $ext );
00557         }
00558 
00559         if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00560             $mime = $this->mMimeTypeAliases[$mime];
00561         }
00562 
00563         wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" );
00564         return $mime;
00565     }
00566 
00575     private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
00576         // Read a chunk of the file
00577         wfSuppressWarnings();
00578         $f = fopen( $file, 'rb' );
00579         wfRestoreWarnings();
00580 
00581         if ( !$f ) {
00582             return 'unknown/unknown';
00583         }
00584 
00585         $fsize = filesize( $file );
00586         if ( $fsize === false ) {
00587             return 'unknown/unknown';
00588         }
00589 
00590         $head = fread( $f, 1024 );
00591         $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
00592         if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
00593             throw new MWException(
00594                 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
00595         }
00596         $tail = fread( $f, $tailLength );
00597         fclose( $f );
00598 
00599         wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
00600 
00601         // Hardcode a few magic number checks...
00602         $headers = array(
00603             // Multimedia...
00604             'MThd'             => 'audio/midi',
00605             'OggS'             => 'application/ogg',
00606 
00607             // Image formats...
00608             // Note that WMF may have a bare header, no magic number.
00609             "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
00610             "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
00611             '%PDF'             => 'application/pdf',
00612             'gimp xcf'         => 'image/x-xcf',
00613 
00614             // Some forbidden fruit...
00615             'MZ'               => 'application/octet-stream', // DOS/Windows executable
00616             "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
00617             "\x7fELF"          => 'application/octet-stream', // ELF binary
00618         );
00619 
00620         foreach ( $headers as $magic => $candidate ) {
00621             if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
00622                 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
00623                 return $candidate;
00624             }
00625         }
00626 
00627         /* Look for WebM and Matroska files */
00628         if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
00629             $doctype = strpos( $head, "\x42\x82" );
00630             if ( $doctype ) {
00631                 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
00632                 $data = substr( $head, $doctype + 3, 8 );
00633                 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
00634                     wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
00635                     return "video/x-matroska";
00636                 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
00637                     wfDebug( __METHOD__ . ": recognized file as video/webm\n" );
00638                     return "video/webm";
00639                 }
00640             }
00641             wfDebug( __METHOD__ . ": unknown EBML file\n" );
00642             return "unknown/unknown";
00643         }
00644 
00645         /* Look for WebP */
00646         if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8 ), "WEBPVP8 ", 8 ) == 0 ) {
00647             wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
00648             return "image/webp";
00649         }
00650 
00663         if ( ( strpos( $head, '<?php' ) !== false ) ||
00664             ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
00665             ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
00666             ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
00667             ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
00668             ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
00669 
00670             wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
00671             return 'application/x-php';
00672         }
00673 
00677         $xml = new XmlTypeCheck( $file );
00678         if ( $xml->wellFormed ) {
00679             global $wgXMLMimeTypes;
00680             if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
00681                 return $wgXMLMimeTypes[$xml->getRootElement()];
00682             } else {
00683                 return 'application/xml';
00684             }
00685         }
00686 
00690         $script_type = null;
00691 
00692         # detect by shebang
00693         if ( substr( $head, 0, 2 ) == "#!" ) {
00694             $script_type = "ASCII";
00695         } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) {
00696             $script_type = "UTF-8";
00697         } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) {
00698             $script_type = "UTF-16BE";
00699         } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
00700             $script_type = "UTF-16LE";
00701         }
00702 
00703         if ( $script_type ) {
00704             if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
00705                 // Quick and dirty fold down to ASCII!
00706                 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
00707                 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
00708                 $head = '';
00709                 foreach ( $chars as $codepoint ) {
00710                     if ( $codepoint < 128 ) {
00711                         $head .= chr( $codepoint );
00712                     } else {
00713                         $head .= '?';
00714                     }
00715                 }
00716             }
00717 
00718             $match = array();
00719 
00720             if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
00721                 $mime = "application/x-{$match[2]}";
00722                 wfDebug( __METHOD__ . ": shell script recognized as $mime\n" );
00723                 return $mime;
00724             }
00725         }
00726 
00727         // Check for ZIP variants (before getimagesize)
00728         if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
00729             wfDebug( __METHOD__ . ": ZIP header present in $file\n" );
00730             return $this->detectZipType( $head, $tail, $ext );
00731         }
00732 
00733         wfSuppressWarnings();
00734         $gis = getimagesize( $file );
00735         wfRestoreWarnings();
00736 
00737         if ( $gis && isset( $gis['mime'] ) ) {
00738             $mime = $gis['mime'];
00739             wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" );
00740             return $mime;
00741         }
00742 
00743         // Also test DjVu
00744         $deja = new DjVuImage( $file );
00745         if ( $deja->isValid() ) {
00746             wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
00747             return 'image/vnd.djvu';
00748         }
00749 
00750         return false;
00751     }
00752 
00766     function detectZipType( $header, $tail = null, $ext = false ) {
00767         if ( $ext ) { # TODO: remove $ext param
00768             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00769                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00770         }
00771 
00772         $mime = 'application/zip';
00773         $opendocTypes = array(
00774             'chart-template',
00775             'chart',
00776             'formula-template',
00777             'formula',
00778             'graphics-template',
00779             'graphics',
00780             'image-template',
00781             'image',
00782             'presentation-template',
00783             'presentation',
00784             'spreadsheet-template',
00785             'spreadsheet',
00786             'text-template',
00787             'text-master',
00788             'text-web',
00789             'text' );
00790 
00791         // http://lists.oasis-open.org/archives/office/200505/msg00006.html
00792         $types = '(?:' . implode( '|', $opendocTypes ) . ')';
00793         $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
00794 
00795         $openxmlRegex = "/^\[Content_Types\].xml/";
00796 
00797         if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
00798             $mime = $matches[1];
00799             wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" );
00800         } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
00801             $mime = "application/x-opc+zip";
00802             # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
00803             if ( $ext !== true && $ext !== false ) {
00808                 if ( $this->isMatchingExtension( $ext, $mime ) ) {
00809                     /* A known file extension for an OPC file,
00810                      * find the proper mime type for that file extension
00811                      */
00812                     $mime = $this->guessTypesForExtension( $ext );
00813                 } else {
00814                     $mime = "application/zip";
00815                 }
00816             }
00817             wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" );
00818         } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
00819                 ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
00820                 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
00821             if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) {
00822                 $mime = "application/msword";
00823             }
00824             switch ( substr( $header, 512, 6 ) ) {
00825                 case "\xEC\xA5\xC1\x00\x0E\x00":
00826                 case "\xEC\xA5\xC1\x00\x1C\x00":
00827                 case "\xEC\xA5\xC1\x00\x43\x00":
00828                     $mime = "application/vnd.ms-powerpoint";
00829                     break;
00830                 case "\xFD\xFF\xFF\xFF\x10\x00":
00831                 case "\xFD\xFF\xFF\xFF\x1F\x00":
00832                 case "\xFD\xFF\xFF\xFF\x22\x00":
00833                 case "\xFD\xFF\xFF\xFF\x23\x00":
00834                 case "\xFD\xFF\xFF\xFF\x28\x00":
00835                 case "\xFD\xFF\xFF\xFF\x29\x00":
00836                 case "\xFD\xFF\xFF\xFF\x10\x02":
00837                 case "\xFD\xFF\xFF\xFF\x1F\x02":
00838                 case "\xFD\xFF\xFF\xFF\x22\x02":
00839                 case "\xFD\xFF\xFF\xFF\x23\x02":
00840                 case "\xFD\xFF\xFF\xFF\x28\x02":
00841                 case "\xFD\xFF\xFF\xFF\x29\x02":
00842                     $mime = "application/vnd.msexcel";
00843                     break;
00844             }
00845 
00846             wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" );
00847         } else {
00848             wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" );
00849         }
00850         return $mime;
00851     }
00852 
00871     private function detectMimeType( $file, $ext = true ) {
00872         global $wgMimeDetectorCommand;
00873 
00874         if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
00875             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00876         }
00877 
00878         $m = null;
00879         if ( $wgMimeDetectorCommand ) {
00880             $args = wfEscapeShellArg( $file );
00881             $m = wfShellExec( "$wgMimeDetectorCommand $args" );
00882         } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
00883 
00884             # This required the fileinfo extension by PECL,
00885             # see http://pecl.php.net/package/fileinfo
00886             # This must be compiled into PHP
00887             #
00888             # finfo is the official replacement for the deprecated
00889             # mime_content_type function, see below.
00890             #
00891             # If you may need to load the fileinfo extension at runtime, set
00892             # $wgLoadFileinfoExtension in LocalSettings.php
00893 
00894             $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */
00895 
00896             if ( $mime_magic_resource ) {
00897                 $m = finfo_file( $mime_magic_resource, $file );
00898                 finfo_close( $mime_magic_resource );
00899             } else {
00900                 wfDebug( __METHOD__ . ": finfo_open failed on " . FILEINFO_MIME . "!\n" );
00901             }
00902         } elseif ( function_exists( "mime_content_type" ) ) {
00903 
00904             # NOTE: this function is available since PHP 4.3.0, but only if
00905             # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
00906             #
00907             # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundled with PHP;
00908             # sometimes, this may even be needed under linus/unix.
00909             #
00910             # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
00911             # see http://www.php.net/manual/en/ref.mime-magic.php for details.
00912 
00913             $m = mime_content_type( $file );
00914         } else {
00915             wfDebug( __METHOD__ . ": no magic mime detector found!\n" );
00916         }
00917 
00918         if ( $m ) {
00919             # normalize
00920             $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
00921             $m = trim( $m );
00922             $m = strtolower( $m );
00923 
00924             if ( strpos( $m, 'unknown' ) !== false ) {
00925                 $m = null;
00926             } else {
00927                 wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" );
00928                 return $m;
00929             }
00930         }
00931 
00932         // If desired, look at extension as a fallback.
00933         if ( $ext === true ) {
00934             $i = strrpos( $file, '.' );
00935             $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
00936         }
00937         if ( $ext ) {
00938             if ( $this->isRecognizableExtension( $ext ) ) {
00939                 wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
00940             } else {
00941                 $m = $this->guessTypesForExtension( $ext );
00942                 if ( $m ) {
00943                     wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" );
00944                     return $m;
00945                 }
00946             }
00947         }
00948 
00949         // Unknown type
00950         wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" );
00951         return 'unknown/unknown';
00952     }
00953 
00970     function getMediaType( $path = null, $mime = null ) {
00971         if ( !$mime && !$path ) {
00972             return MEDIATYPE_UNKNOWN;
00973         }
00974 
00975         // If mime type is unknown, guess it
00976         if ( !$mime ) {
00977             $mime = $this->guessMimeType( $path, false );
00978         }
00979 
00980         // Special code for ogg - detect if it's video (theora),
00981         // else label it as sound.
00982         if ( $mime == 'application/ogg' && file_exists( $path ) ) {
00983 
00984             // Read a chunk of the file
00985             $f = fopen( $path, "rt" );
00986             if ( !$f ) {
00987                 return MEDIATYPE_UNKNOWN;
00988             }
00989             $head = fread( $f, 256 );
00990             fclose( $f );
00991 
00992             $head = strtolower( $head );
00993 
00994             // This is an UGLY HACK, file should be parsed correctly
00995             if ( strpos( $head, 'theora' ) !== false ) {
00996                 return MEDIATYPE_VIDEO;
00997             } elseif ( strpos( $head, 'vorbis' ) !== false ) {
00998                 return MEDIATYPE_AUDIO;
00999             } elseif ( strpos( $head, 'flac' ) !== false ) {
01000                 return MEDIATYPE_AUDIO;
01001             } elseif ( strpos( $head, 'speex' ) !== false ) {
01002                 return MEDIATYPE_AUDIO;
01003             } else {
01004                 return MEDIATYPE_MULTIMEDIA;
01005             }
01006         }
01007 
01008         // Check for entry for full mime type
01009         if ( $mime ) {
01010             $type = $this->findMediaType( $mime );
01011             if ( $type !== MEDIATYPE_UNKNOWN ) {
01012                 return $type;
01013             }
01014         }
01015 
01016         // Check for entry for file extension
01017         if ( $path ) {
01018             $i = strrpos( $path, '.' );
01019             $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
01020 
01021             // TODO: look at multi-extension if this fails, parse from full path
01022             $type = $this->findMediaType( '.' . $e );
01023             if ( $type !== MEDIATYPE_UNKNOWN ) {
01024                 return $type;
01025             }
01026         }
01027 
01028         // Check major mime type
01029         if ( $mime ) {
01030             $i = strpos( $mime, '/' );
01031             if ( $i !== false ) {
01032                 $major = substr( $mime, 0, $i );
01033                 $type = $this->findMediaType( $major );
01034                 if ( $type !== MEDIATYPE_UNKNOWN ) {
01035                     return $type;
01036                 }
01037             }
01038         }
01039 
01040         if ( !$type ) {
01041             $type = MEDIATYPE_UNKNOWN;
01042         }
01043 
01044         return $type;
01045     }
01046 
01056     function findMediaType( $extMime ) {
01057         if ( strpos( $extMime, '.' ) === 0 ) {
01058             // If it's an extension, look up the mime types
01059             $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
01060             if ( !$m ) {
01061                 return MEDIATYPE_UNKNOWN;
01062             }
01063 
01064             $m = explode( ' ', $m );
01065         } else {
01066             // Normalize mime type
01067             if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
01068                 $extMime = $this->mMimeTypeAliases[$extMime];
01069             }
01070 
01071             $m = array( $extMime );
01072         }
01073 
01074         foreach ( $m as $mime ) {
01075             foreach ( $this->mMediaTypes as $type => $codes ) {
01076                 if ( in_array( $mime, $codes, true ) ) {
01077                     return $type;
01078                 }
01079             }
01080         }
01081 
01082         return MEDIATYPE_UNKNOWN;
01083     }
01084 
01094     public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
01095         $ca = $this->getIEContentAnalyzer();
01096         return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
01097     }
01098 
01104     protected function getIEContentAnalyzer() {
01105         if ( is_null( $this->mIEAnalyzer ) ) {
01106             $this->mIEAnalyzer = new IEContentAnalyzer;
01107         }
01108         return $this->mIEAnalyzer;
01109     }
01110 }