123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839 |
- <?php
-
- define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
- application/ogg ogg ogm ogv
- application/pdf pdf
- application/vnd.oasis.opendocument.chart odc
- application/vnd.oasis.opendocument.chart-template otc
- application/vnd.oasis.opendocument.formula odf
- application/vnd.oasis.opendocument.formula-template otf
- application/vnd.oasis.opendocument.graphics odg
- application/vnd.oasis.opendocument.graphics-template otg
- application/vnd.oasis.opendocument.image odi
- application/vnd.oasis.opendocument.image-template oti
- application/vnd.oasis.opendocument.presentation odp
- application/vnd.oasis.opendocument.presentation-template otp
- application/vnd.oasis.opendocument.spreadsheet ods
- application/vnd.oasis.opendocument.spreadsheet-template ots
- application/vnd.oasis.opendocument.text odt
- application/vnd.oasis.opendocument.text-template ott
- application/vnd.oasis.opendocument.text-master otm
- application/vnd.oasis.opendocument.text-web oth
- application/x-javascript js
- application/x-shockwave-flash swf
- audio/midi mid midi kar
- audio/mpeg mpga mpa mp2 mp3
- audio/x-aiff aif aiff aifc
- audio/x-wav wav
- audio/ogg ogg
- image/x-bmp bmp
- image/gif gif
- image/jpeg jpeg jpg jpe
- image/png png
- image/svg+xml image/svg svg
- image/tiff tiff tif
- image/vnd.djvu image/x.djvu image/x-djvu djvu
- image/x-portable-pixmap ppm
- image/x-xcf xcf
- text/plain txt
- text/html html htm
- video/ogg ogm ogg ogv
- video/mpeg mpg mpeg
- END_STRING
- );
-
- define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
- application/pdf [OFFICE]
- application/vnd.oasis.opendocument.chart [OFFICE]
- application/vnd.oasis.opendocument.chart-template [OFFICE]
- application/vnd.oasis.opendocument.formula [OFFICE]
- application/vnd.oasis.opendocument.formula-template [OFFICE]
- application/vnd.oasis.opendocument.graphics [OFFICE]
- application/vnd.oasis.opendocument.graphics-template [OFFICE]
- application/vnd.oasis.opendocument.image [OFFICE]
- application/vnd.oasis.opendocument.image-template [OFFICE]
- application/vnd.oasis.opendocument.presentation [OFFICE]
- application/vnd.oasis.opendocument.presentation-template [OFFICE]
- application/vnd.oasis.opendocument.spreadsheet [OFFICE]
- application/vnd.oasis.opendocument.spreadsheet-template [OFFICE]
- application/vnd.oasis.opendocument.text [OFFICE]
- application/vnd.oasis.opendocument.text-template [OFFICE]
- application/vnd.oasis.opendocument.text-master [OFFICE]
- application/vnd.oasis.opendocument.text-web [OFFICE]
- text/javascript application/x-javascript [EXECUTABLE]
- application/x-shockwave-flash [MULTIMEDIA]
- audio/midi [AUDIO]
- audio/x-aiff [AUDIO]
- audio/x-wav [AUDIO]
- audio/mp3 audio/mpeg [AUDIO]
- application/ogg audio/ogg video/ogg [MULTIMEDIA]
- image/x-bmp image/bmp [BITMAP]
- image/gif [BITMAP]
- image/jpeg [BITMAP]
- image/png [BITMAP]
- image/svg+xml [DRAWING]
- image/tiff [BITMAP]
- image/vnd.djvu [BITMAP]
- image/x-xcf [BITMAP]
- image/x-portable-pixmap [BITMAP]
- text/plain [TEXT]
- text/html [TEXT]
- video/ogg [VIDEO]
- video/mpeg [VIDEO]
- unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
- END_STRING
- );
- global $wgLoadFileinfoExtension;
- if ($wgLoadFileinfoExtension) {
- if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
- }
- class MimeMagic {
-
- var $mMediaTypes= NULL;
-
- var $mMimeTypeAliases= NULL;
-
- var $mMimeToExt= NULL;
-
- var $mExtToMime= NULL;
-
- var $mIEAnalyzer;
-
- private static $instance;
-
- function __construct() {
-
- global $wgMimeTypeFile, $IP;
- $types = MM_WELL_KNOWN_MIME_TYPES;
- if ( $wgMimeTypeFile == 'includes/mime.types' ) {
- $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
- }
- if ( $wgMimeTypeFile ) {
- if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
- wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
- $types .= "\n";
- $types .= file_get_contents( $wgMimeTypeFile );
- } else {
- wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" );
- }
- } else {
- wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" );
- }
- $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
- $types = str_replace( "\t", " ", $types );
- $this->mMimeToExt = array();
- $this->mToMime = array();
- $lines = explode( "\n",$types );
- foreach ( $lines as $s ) {
- $s = trim( $s );
- if ( empty( $s ) ) continue;
- if ( strpos( $s, '#' ) === 0 ) continue;
- $s = strtolower( $s );
- $i = strpos( $s, ' ' );
- if ( $i === false ) continue;
-
- $mime = substr( $s, 0, $i );
- $ext = trim( substr($s, $i+1 ) );
- if ( empty( $ext ) ) continue;
- if ( !empty( $this->mMimeToExt[$mime] ) ) {
- $this->mMimeToExt[$mime] .= ' ' . $ext;
- } else {
- $this->mMimeToExt[$mime] = $ext;
- }
- $extensions = explode( ' ', $ext );
- foreach ( $extensions as $e ) {
- $e = trim( $e );
- if ( empty( $e ) ) continue;
- if ( !empty( $this->mExtToMime[$e] ) ) {
- $this->mExtToMime[$e] .= ' ' . $mime;
- } else {
- $this->mExtToMime[$e] = $mime;
- }
- }
- }
-
- global $wgMimeInfoFile;
- if ( $wgMimeInfoFile == 'includes/mime.info' ) {
- $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
- }
- $info = MM_WELL_KNOWN_MIME_INFO;
- if ( $wgMimeInfoFile ) {
- if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
- wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" );
- $info .= "\n";
- $info .= file_get_contents( $wgMimeInfoFile );
- } else {
- wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n");
- }
- } else {
- wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n");
- }
- $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info);
- $info = str_replace( "\t", " ", $info );
- $this->mMimeTypeAliases = array();
- $this->mMediaTypes = array();
- $lines = explode( "\n", $info );
- foreach ( $lines as $s ) {
- $s = trim( $s );
- if ( empty( $s ) ) continue;
- if ( strpos( $s, '#' ) === 0 ) continue;
- $s = strtolower( $s );
- $i = strpos( $s, ' ' );
- if ( $i === false ) continue;
-
- $match = array();
- if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
- $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
- $mtype = trim( strtoupper( $match[1] ) );
- } else {
- $mtype = MEDIATYPE_UNKNOWN;
- }
- $m = explode( ' ', $s );
- if ( !isset( $this->mMediaTypes[$mtype] ) ) {
- $this->mMediaTypes[$mtype] = array();
- }
- foreach ( $m as $mime ) {
- $mime = trim( $mime );
- if ( empty( $mime ) ) continue;
- $this->mMediaTypes[$mtype][] = $mime;
- }
- if ( sizeof( $m ) > 1 ) {
- $main = $m[0];
- for ( $i=1; $i<sizeof($m); $i += 1 ) {
- $mime = $m[$i];
- $this->mMimeTypeAliases[$mime] = $main;
- }
- }
- }
- }
-
- static function &singleton() {
- if ( !isset( self::$instance ) ) {
- self::$instance = new MimeMagic;
- }
- return self::$instance;
- }
-
- function getExtensionsForType( $mime ) {
- $mime = strtolower( $mime );
- $r = @$this->mMimeToExt[$mime];
- if ( @!$r and isset( $this->mMimeTypeAliases[$mime] ) ) {
- $mime = $this->mMimeTypeAliases[$mime];
- $r = @$this->mMimeToExt[$mime];
- }
- return $r;
- }
-
- function getTypesForExtension( $ext ) {
- $ext = strtolower( $ext );
- $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
- return $r;
- }
-
- function guessTypesForExtension( $ext ) {
- $m = $this->getTypesForExtension( $ext );
- if ( is_null( $m ) ) return NULL;
- $m = trim( $m );
- $m = preg_replace( '/\s.*$/', '', $m );
- return $m;
- }
-
- function isMatchingExtension( $extension, $mime ) {
- $ext = $this->getExtensionsForType( $mime );
- if ( !$ext ) {
- return NULL;
- }
- $ext = explode( ' ', $ext );
- $extension = strtolower( $extension );
- if ( in_array( $extension, $ext ) ) {
- return true;
- }
- return false;
- }
-
- function isPHPImageType( $mime ) {
-
- static $types = array(
- 'image/gif', 'image/jpeg', 'image/png',
- 'image/x-bmp', 'image/xbm', 'image/tiff',
- 'image/jp2', 'image/jpeg2000', 'image/iff',
- 'image/xbm', 'image/x-xbitmap',
- 'image/vnd.wap.wbmp', 'image/vnd.xiff',
- 'image/x-photoshop',
- 'application/x-shockwave-flash',
- );
- return in_array( $mime, $types );
- }
-
- function isRecognizableExtension( $extension ) {
- static $types = array(
-
- 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
- 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
- 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
- 'xbm',
-
- 'djvu', 'ogg', 'ogv', 'mid', 'pdf', 'wmf', 'xcf',
-
- 'svg',
- );
- return in_array( strtolower( $extension ), $types );
- }
-
- function guessMimeType( $file, $ext = true ) {
- $mime = $this->doGuessMimeType( $file, $ext );
- if( !$mime ) {
- wfDebug( __METHOD__.": internal type detection failed for $file (.$ext)...\n" );
- $mime = $this->detectMimeType( $file, $ext );
- }
- if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
- $mime = $this->mMimeTypeAliases[$mime];
- }
- wfDebug(__METHOD__.": final mime type of $file: $mime\n");
- return $mime;
- }
- function doGuessMimeType( $file, $ext = true ) {
-
- wfSuppressWarnings();
- $f = fopen( $file, "rt" );
- wfRestoreWarnings();
- if( !$f ) return "unknown/unknown";
- $head = fread( $f, 1024 );
- fseek( $f, -65558, SEEK_END );
- $tail = fread( $f, 65558 );
- fclose( $f );
-
- $headers = array(
-
- 'MThd' => 'audio/midi',
- 'OggS' => 'application/ogg',
-
-
- "\x01\x00\x09\x00" => 'application/x-msmetafile',
- "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
- '%PDF' => 'application/pdf',
- 'gimp xcf' => 'image/x-xcf',
-
- 'MZ' => 'application/octet-stream',
- "\xca\xfe\xba\xbe" => 'application/octet-stream',
- "\x7fELF" => 'application/octet-stream',
- );
- foreach( $headers as $magic => $candidate ) {
- if( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
- wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
- return $candidate;
- }
- }
-
- if( ( strpos( $head, '<?php' ) !== false ) ||
- ( strpos( $head, '<? ' ) !== false ) ||
- ( strpos( $head, "<?\n" ) !== false ) ||
- ( strpos( $head, "<?\t" ) !== false ) ||
- ( strpos( $head, "<?=" ) !== false ) ||
- ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
- ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
- ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
- ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
- ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
- wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
- return "application/x-php";
- }
-
- $xml = new XmlTypeCheck( $file );
- if( $xml->wellFormed ) {
- global $wgXMLMimeTypes;
- if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
- return $wgXMLMimeTypes[$xml->getRootElement()];
- } else {
- return 'application/xml';
- }
- }
-
- $script_type = NULL;
-
- if ( substr( $head, 0, 2) == "#!" ) {
- $script_type = "ASCII";
- } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
- $script_type = "UTF-8";
- } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
- $script_type = "UTF-16BE";
- } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
- $script_type= "UTF-16LE";
- }
- if ( $script_type ) {
- if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
-
- $pack = array( 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' );
- $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
- $head = '';
- foreach( $chars as $codepoint ) {
- if( $codepoint < 128 ) {
- $head .= chr( $codepoint );
- } else {
- $head .= '?';
- }
- }
- }
- $match = array();
- if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
- $mime = "application/x-{$match[2]}";
- wfDebug( __METHOD__.": shell script recognized as $mime\n" );
- return $mime;
- }
- }
-
- if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
- wfDebug( __METHOD__.": ZIP header present at end of $file\n" );
- return $this->detectZipType( $head );
- }
- wfSuppressWarnings();
- $gis = getimagesize( $file );
- wfRestoreWarnings();
- if( $gis && isset( $gis['mime'] ) ) {
- $mime = $gis['mime'];
- wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" );
- return $mime;
- }
-
- $deja = new DjVuImage( $file );
- if( $deja->isValid() ) {
- wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
- return 'image/vnd.djvu';
- }
- return false;
- }
-
-
- function detectZipType( $header ) {
- $opendocTypes = array(
- 'chart-template',
- 'chart',
- 'formula-template',
- 'formula',
- 'graphics-template',
- 'graphics',
- 'image-template',
- 'image',
- 'presentation-template',
- 'presentation',
- 'spreadsheet-template',
- 'spreadsheet',
- 'text-template',
- 'text-master',
- 'text-web',
- 'text' );
-
- $types = '(?:' . implode( '|', $opendocTypes ) . ')';
- $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
- wfDebug( __METHOD__.": $opendocRegex\n" );
-
- if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
- $mime = $matches[1];
- wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
- return $mime;
- } else {
- wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" );
- return 'application/zip';
- }
- }
-
- function detectMimeType( $file, $ext = true ) {
- global $wgMimeDetectorCommand;
- $m = NULL;
- if ( $wgMimeDetectorCommand ) {
- $fn = wfEscapeShellArg( $file );
- $m = `$wgMimeDetectorCommand $fn`;
- } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
-
-
-
-
-
-
-
-
-
- $mime_magic_resource = finfo_open(FILEINFO_MIME);
- if ($mime_magic_resource) {
- $m = finfo_file( $mime_magic_resource, $file );
- finfo_close( $mime_magic_resource );
- } else {
- wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
- }
- } elseif ( function_exists( "mime_content_type" ) ) {
-
-
-
-
-
-
-
-
- $m = mime_content_type($file);
- } else {
- wfDebug( __METHOD__.": no magic mime detector found!\n" );
- }
- if ( $m ) {
-
- $m = preg_replace( '![;, ].*$!', '', $m );
- $m = trim( $m );
- $m = strtolower( $m );
- if ( strpos( $m, 'unknown' ) !== false ) {
- $m = NULL;
- } else {
- wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
- return $m;
- }
- }
-
- if ( $ext === true ) {
- $i = strrpos( $file, '.' );
- $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
- }
- if ( $ext ) {
- if( $this->isRecognizableExtension( $ext ) ) {
- wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, we should have recognized it\n" );
- } else {
- $m = $this->guessTypesForExtension( $ext );
- if ( $m ) {
- wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
- return $m;
- }
- }
- }
-
- wfDebug( __METHOD__.": failed to guess mime type for $file!\n" );
- return "unknown/unknown";
- }
-
- function getMediaType( $path = NULL, $mime = NULL ) {
- if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
-
- if( !$mime ) $mime = $this->guessMimeType( $path, false );
-
-
- if( $mime == "application/ogg" && file_exists( $path ) ) {
-
- $f = fopen( $path, "rt" );
- if ( !$f ) return MEDIATYPE_UNKNOWN;
- $head = fread( $f, 256 );
- fclose( $f );
- $head = strtolower( $head );
-
- if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
- elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
- elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
- elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
- else return MEDIATYPE_MULTIMEDIA;
- }
-
- if( $mime ) {
- $type = $this->findMediaType( $mime );
- if( $type !== MEDIATYPE_UNKNOWN ) return $type;
- }
-
- $e = NULL;
- if ( $path ) {
- $i = strrpos( $path, '.' );
- $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
-
- $type = $this->findMediaType( '.' . $e );
- if ( $type !== MEDIATYPE_UNKNOWN ) return $type;
- }
-
- if( $mime ) {
- $i = strpos( $mime, '/' );
- if( $i !== false ) {
- $major = substr( $mime, 0, $i );
- $type = $this->findMediaType( $major );
- if( $type !== MEDIATYPE_UNKNOWN ) return $type;
- }
- }
- if( !$type ) $type = MEDIATYPE_UNKNOWN;
- return $type;
- }
-
- function findMediaType( $extMime ) {
- if ( strpos( $extMime, '.' ) === 0 ) {
- $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
- if ( !$m ) return MEDIATYPE_UNKNOWN;
- $m = explode( ' ', $m );
- } else {
-
- if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
- $extMime = $this->mMimeTypeAliases[$extMime];
- }
- $m = array($extMime);
- }
- foreach ( $m as $mime ) {
- foreach ( $this->mMediaTypes as $type => $codes ) {
- if ( in_array($mime, $codes, true ) ) {
- return $type;
- }
- }
- }
- return MEDIATYPE_UNKNOWN;
- }
-
- public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
- $ca = $this->getIEContentAnalyzer();
- return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
- }
-
- protected function getIEContentAnalyzer() {
- if ( is_null( $this->mIEAnalyzer ) ) {
- $this->mIEAnalyzer = new IEContentAnalyzer;
- }
- return $this->mIEAnalyzer;
- }
- }
|