MediaWiki  REL1_23
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 = null;
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         } elseif ( $mime === 'application/x-opc+zip' ) {
00512             if ( $this->isMatchingExtension( $ext, $mime ) ) {
00513                 // A known file extension for an OPC file,
00514                 // find the proper mime type for that file extension
00515                 $mime = $this->guessTypesForExtension( $ext );
00516             } else {
00517                 wfDebug( __METHOD__ . ": refusing to guess better type for $mime file, " .
00518                     ".$ext is not a known OPC extension.\n" );
00519                 $mime = 'application/zip';
00520             }
00521         }
00522 
00523         if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00524             $mime = $this->mMimeTypeAliases[$mime];
00525         }
00526 
00527         wfDebug( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
00528         return $mime;
00529     }
00530 
00545     public function guessMimeType( $file, $ext = true ) {
00546         if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
00547             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00548                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00549         }
00550 
00551         $mime = $this->doGuessMimeType( $file, $ext );
00552 
00553         if ( !$mime ) {
00554             wfDebug( __METHOD__ . ": internal type detection failed for $file (.$ext)...\n" );
00555             $mime = $this->detectMimeType( $file, $ext );
00556         }
00557 
00558         if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
00559             $mime = $this->mMimeTypeAliases[$mime];
00560         }
00561 
00562         wfDebug( __METHOD__ . ": guessed mime type of $file: $mime\n" );
00563         return $mime;
00564     }
00565 
00574     private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
00575         // Read a chunk of the file
00576         wfSuppressWarnings();
00577         $f = fopen( $file, 'rb' );
00578         wfRestoreWarnings();
00579 
00580         if ( !$f ) {
00581             return 'unknown/unknown';
00582         }
00583 
00584         $fsize = filesize( $file );
00585         if ( $fsize === false ) {
00586             return 'unknown/unknown';
00587         }
00588 
00589         $head = fread( $f, 1024 );
00590         $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
00591         if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
00592             throw new MWException(
00593                 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
00594         }
00595         $tail = fread( $f, $tailLength );
00596         fclose( $f );
00597 
00598         wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
00599 
00600         // Hardcode a few magic number checks...
00601         $headers = array(
00602             // Multimedia...
00603             'MThd'             => 'audio/midi',
00604             'OggS'             => 'application/ogg',
00605 
00606             // Image formats...
00607             // Note that WMF may have a bare header, no magic number.
00608             "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
00609             "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
00610             '%PDF'             => 'application/pdf',
00611             'gimp xcf'         => 'image/x-xcf',
00612 
00613             // Some forbidden fruit...
00614             'MZ'               => 'application/octet-stream', // DOS/Windows executable
00615             "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
00616             "\x7fELF"          => 'application/octet-stream', // ELF binary
00617         );
00618 
00619         foreach ( $headers as $magic => $candidate ) {
00620             if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
00621                 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
00622                 return $candidate;
00623             }
00624         }
00625 
00626         /* Look for WebM and Matroska files */
00627         if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
00628             $doctype = strpos( $head, "\x42\x82" );
00629             if ( $doctype ) {
00630                 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
00631                 $data = substr( $head, $doctype + 3, 8 );
00632                 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
00633                     wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
00634                     return "video/x-matroska";
00635                 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
00636                     wfDebug( __METHOD__ . ": recognized file as video/webm\n" );
00637                     return "video/webm";
00638                 }
00639             }
00640             wfDebug( __METHOD__ . ": unknown EBML file\n" );
00641             return "unknown/unknown";
00642         }
00643 
00644         /* Look for WebP */
00645         if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8 ), "WEBPVP8 ", 8 ) == 0 ) {
00646             wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
00647             return "image/webp";
00648         }
00649 
00662         if ( ( strpos( $head, '<?php' ) !== false ) ||
00663             ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
00664             ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
00665             ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
00666             ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
00667             ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
00668 
00669             wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
00670             return 'application/x-php';
00671         }
00672 
00676         $xml = new XmlTypeCheck( $file );
00677         if ( $xml->wellFormed ) {
00678             global $wgXMLMimeTypes;
00679             if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
00680                 return $wgXMLMimeTypes[$xml->getRootElement()];
00681             } else {
00682                 return 'application/xml';
00683             }
00684         }
00685 
00689         $script_type = null;
00690 
00691         # detect by shebang
00692         if ( substr( $head, 0, 2 ) == "#!" ) {
00693             $script_type = "ASCII";
00694         } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) {
00695             $script_type = "UTF-8";
00696         } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) {
00697             $script_type = "UTF-16BE";
00698         } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
00699             $script_type = "UTF-16LE";
00700         }
00701 
00702         if ( $script_type ) {
00703             if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
00704                 // Quick and dirty fold down to ASCII!
00705                 $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
00706                 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
00707                 $head = '';
00708                 foreach ( $chars as $codepoint ) {
00709                     if ( $codepoint < 128 ) {
00710                         $head .= chr( $codepoint );
00711                     } else {
00712                         $head .= '?';
00713                     }
00714                 }
00715             }
00716 
00717             $match = array();
00718 
00719             if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
00720                 $mime = "application/x-{$match[2]}";
00721                 wfDebug( __METHOD__ . ": shell script recognized as $mime\n" );
00722                 return $mime;
00723             }
00724         }
00725 
00726         // Check for ZIP variants (before getimagesize)
00727         if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
00728             wfDebug( __METHOD__ . ": ZIP header present in $file\n" );
00729             return $this->detectZipType( $head, $tail, $ext );
00730         }
00731 
00732         wfSuppressWarnings();
00733         $gis = getimagesize( $file );
00734         wfRestoreWarnings();
00735 
00736         if ( $gis && isset( $gis['mime'] ) ) {
00737             $mime = $gis['mime'];
00738             wfDebug( __METHOD__ . ": getimagesize detected $file as $mime\n" );
00739             return $mime;
00740         }
00741 
00742         // Also test DjVu
00743         $deja = new DjVuImage( $file );
00744         if ( $deja->isValid() ) {
00745             wfDebug( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
00746             return 'image/vnd.djvu';
00747         }
00748 
00749         return false;
00750     }
00751 
00765     function detectZipType( $header, $tail = null, $ext = false ) {
00766         if ( $ext ) { # TODO: remove $ext param
00767             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. " .
00768                 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00769         }
00770 
00771         $mime = 'application/zip';
00772         $opendocTypes = array(
00773             'chart-template',
00774             'chart',
00775             'formula-template',
00776             'formula',
00777             'graphics-template',
00778             'graphics',
00779             'image-template',
00780             'image',
00781             'presentation-template',
00782             'presentation',
00783             'spreadsheet-template',
00784             'spreadsheet',
00785             'text-template',
00786             'text-master',
00787             'text-web',
00788             'text' );
00789 
00790         // http://lists.oasis-open.org/archives/office/200505/msg00006.html
00791         $types = '(?:' . implode( '|', $opendocTypes ) . ')';
00792         $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
00793 
00794         $openxmlRegex = "/^\[Content_Types\].xml/";
00795 
00796         if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
00797             $mime = $matches[1];
00798             wfDebug( __METHOD__ . ": detected $mime from ZIP archive\n" );
00799         } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
00800             $mime = "application/x-opc+zip";
00801             # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
00802             if ( $ext !== true && $ext !== false ) {
00807                 if ( $this->isMatchingExtension( $ext, $mime ) ) {
00808                     /* A known file extension for an OPC file,
00809                      * find the proper mime type for that file extension
00810                      */
00811                     $mime = $this->guessTypesForExtension( $ext );
00812                 } else {
00813                     $mime = "application/zip";
00814                 }
00815             }
00816             wfDebug( __METHOD__ . ": detected an Open Packaging Conventions archive: $mime\n" );
00817         } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
00818                 ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
00819                 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
00820             if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) {
00821                 $mime = "application/msword";
00822             }
00823             switch ( substr( $header, 512, 6 ) ) {
00824                 case "\xEC\xA5\xC1\x00\x0E\x00":
00825                 case "\xEC\xA5\xC1\x00\x1C\x00":
00826                 case "\xEC\xA5\xC1\x00\x43\x00":
00827                     $mime = "application/vnd.ms-powerpoint";
00828                     break;
00829                 case "\xFD\xFF\xFF\xFF\x10\x00":
00830                 case "\xFD\xFF\xFF\xFF\x1F\x00":
00831                 case "\xFD\xFF\xFF\xFF\x22\x00":
00832                 case "\xFD\xFF\xFF\xFF\x23\x00":
00833                 case "\xFD\xFF\xFF\xFF\x28\x00":
00834                 case "\xFD\xFF\xFF\xFF\x29\x00":
00835                 case "\xFD\xFF\xFF\xFF\x10\x02":
00836                 case "\xFD\xFF\xFF\xFF\x1F\x02":
00837                 case "\xFD\xFF\xFF\xFF\x22\x02":
00838                 case "\xFD\xFF\xFF\xFF\x23\x02":
00839                 case "\xFD\xFF\xFF\xFF\x28\x02":
00840                 case "\xFD\xFF\xFF\xFF\x29\x02":
00841                     $mime = "application/vnd.msexcel";
00842                     break;
00843             }
00844 
00845             wfDebug( __METHOD__ . ": detected a MS Office document with OPC trailer\n" );
00846         } else {
00847             wfDebug( __METHOD__ . ": unable to identify type of ZIP archive\n" );
00848         }
00849         return $mime;
00850     }
00851 
00870     private function detectMimeType( $file, $ext = true ) {
00871         global $wgMimeDetectorCommand;
00872 
00873         if ( $ext ) { # TODO:  make $ext default to false. Or better, remove it.
00874             wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
00875         }
00876 
00877         $m = null;
00878         if ( $wgMimeDetectorCommand ) {
00879             $args = wfEscapeShellArg( $file );
00880             $m = wfShellExec( "$wgMimeDetectorCommand $args" );
00881         } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
00882 
00883             # This required the fileinfo extension by PECL,
00884             # see http://pecl.php.net/package/fileinfo
00885             # This must be compiled into PHP
00886             #
00887             # finfo is the official replacement for the deprecated
00888             # mime_content_type function, see below.
00889             #
00890             # If you may need to load the fileinfo extension at runtime, set
00891             # $wgLoadFileinfoExtension in LocalSettings.php
00892 
00893             $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */
00894 
00895             if ( $mime_magic_resource ) {
00896                 $m = finfo_file( $mime_magic_resource, $file );
00897                 finfo_close( $mime_magic_resource );
00898             } else {
00899                 wfDebug( __METHOD__ . ": finfo_open failed on " . FILEINFO_MIME . "!\n" );
00900             }
00901         } elseif ( function_exists( "mime_content_type" ) ) {
00902 
00903             # NOTE: this function is available since PHP 4.3.0, but only if
00904             # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
00905             #
00906             # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundled with PHP;
00907             # sometimes, this may even be needed under linus/unix.
00908             #
00909             # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
00910             # see http://www.php.net/manual/en/ref.mime-magic.php for details.
00911 
00912             $m = mime_content_type( $file );
00913         } else {
00914             wfDebug( __METHOD__ . ": no magic mime detector found!\n" );
00915         }
00916 
00917         if ( $m ) {
00918             # normalize
00919             $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
00920             $m = trim( $m );
00921             $m = strtolower( $m );
00922 
00923             if ( strpos( $m, 'unknown' ) !== false ) {
00924                 $m = null;
00925             } else {
00926                 wfDebug( __METHOD__ . ": magic mime type of $file: $m\n" );
00927                 return $m;
00928             }
00929         }
00930 
00931         // If desired, look at extension as a fallback.
00932         if ( $ext === true ) {
00933             $i = strrpos( $file, '.' );
00934             $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
00935         }
00936         if ( $ext ) {
00937             if ( $this->isRecognizableExtension( $ext ) ) {
00938                 wfDebug( __METHOD__ . ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
00939             } else {
00940                 $m = $this->guessTypesForExtension( $ext );
00941                 if ( $m ) {
00942                     wfDebug( __METHOD__ . ": extension mime type of $file: $m\n" );
00943                     return $m;
00944                 }
00945             }
00946         }
00947 
00948         // Unknown type
00949         wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" );
00950         return 'unknown/unknown';
00951     }
00952 
00969     function getMediaType( $path = null, $mime = null ) {
00970         if ( !$mime && !$path ) {
00971             return MEDIATYPE_UNKNOWN;
00972         }
00973 
00974         // If mime type is unknown, guess it
00975         if ( !$mime ) {
00976             $mime = $this->guessMimeType( $path, false );
00977         }
00978 
00979         // Special code for ogg - detect if it's video (theora),
00980         // else label it as sound.
00981         if ( $mime == 'application/ogg' && file_exists( $path ) ) {
00982 
00983             // Read a chunk of the file
00984             $f = fopen( $path, "rt" );
00985             if ( !$f ) {
00986                 return MEDIATYPE_UNKNOWN;
00987             }
00988             $head = fread( $f, 256 );
00989             fclose( $f );
00990 
00991             $head = strtolower( $head );
00992 
00993             // This is an UGLY HACK, file should be parsed correctly
00994             if ( strpos( $head, 'theora' ) !== false ) {
00995                 return MEDIATYPE_VIDEO;
00996             } elseif ( strpos( $head, 'vorbis' ) !== false ) {
00997                 return MEDIATYPE_AUDIO;
00998             } elseif ( strpos( $head, 'flac' ) !== false ) {
00999                 return MEDIATYPE_AUDIO;
01000             } elseif ( strpos( $head, 'speex' ) !== false ) {
01001                 return MEDIATYPE_AUDIO;
01002             } else {
01003                 return MEDIATYPE_MULTIMEDIA;
01004             }
01005         }
01006 
01007         // Check for entry for full mime type
01008         if ( $mime ) {
01009             $type = $this->findMediaType( $mime );
01010             if ( $type !== MEDIATYPE_UNKNOWN ) {
01011                 return $type;
01012             }
01013         }
01014 
01015         // Check for entry for file extension
01016         if ( $path ) {
01017             $i = strrpos( $path, '.' );
01018             $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
01019 
01020             // TODO: look at multi-extension if this fails, parse from full path
01021             $type = $this->findMediaType( '.' . $e );
01022             if ( $type !== MEDIATYPE_UNKNOWN ) {
01023                 return $type;
01024             }
01025         }
01026 
01027         // Check major mime type
01028         if ( $mime ) {
01029             $i = strpos( $mime, '/' );
01030             if ( $i !== false ) {
01031                 $major = substr( $mime, 0, $i );
01032                 $type = $this->findMediaType( $major );
01033                 if ( $type !== MEDIATYPE_UNKNOWN ) {
01034                     return $type;
01035                 }
01036             }
01037         }
01038 
01039         if ( !$type ) {
01040             $type = MEDIATYPE_UNKNOWN;
01041         }
01042 
01043         return $type;
01044     }
01045 
01055     function findMediaType( $extMime ) {
01056         if ( strpos( $extMime, '.' ) === 0 ) {
01057             // If it's an extension, look up the mime types
01058             $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
01059             if ( !$m ) {
01060                 return MEDIATYPE_UNKNOWN;
01061             }
01062 
01063             $m = explode( ' ', $m );
01064         } else {
01065             // Normalize mime type
01066             if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
01067                 $extMime = $this->mMimeTypeAliases[$extMime];
01068             }
01069 
01070             $m = array( $extMime );
01071         }
01072 
01073         foreach ( $m as $mime ) {
01074             foreach ( $this->mMediaTypes as $type => $codes ) {
01075                 if ( in_array( $mime, $codes, true ) ) {
01076                     return $type;
01077                 }
01078             }
01079         }
01080 
01081         return MEDIATYPE_UNKNOWN;
01082     }
01083 
01093     public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
01094         $ca = $this->getIEContentAnalyzer();
01095         return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
01096     }
01097 
01103     protected function getIEContentAnalyzer() {
01104         if ( is_null( $this->mIEAnalyzer ) ) {
01105             $this->mIEAnalyzer = new IEContentAnalyzer;
01106         }
01107         return $this->mIEAnalyzer;
01108     }
01109 }