* API: All pages list
[mediawiki.git] / includes / MimeMagic.php
blob8838d230ad2695f50a3123ab6e541850f9f5f239
1 <?php
2 /** Module defining helper functions for detecting and dealing with mime types.
4 * @package MediaWiki
5 */
7 /** Defines a set of well known mime types
8 * This is used as a fallback to mime.types files.
9 * An extensive list of well known mime types is provided by
10 * the file mime.types in the includes directory.
12 define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
13 application/ogg ogg ogm
14 application/pdf pdf
15 application/x-javascript js
16 application/x-shockwave-flash swf
17 audio/midi mid midi kar
18 audio/mpeg mpga mpa mp2 mp3
19 audio/x-aiff aif aiff aifc
20 audio/x-wav wav
21 audio/ogg ogg
22 image/x-bmp bmp
23 image/gif gif
24 image/jpeg jpeg jpg jpe
25 image/png png
26 image/svg+xml svg
27 image/tiff tiff tif
28 image/vnd.djvu djvu
29 text/plain txt
30 text/html html htm
31 video/ogg ogm ogg
32 video/mpeg mpg mpeg
33 END_STRING
36 /** Defines a set of well known mime info entries
37 * This is used as a fallback to mime.info files.
38 * An extensive list of well known mime types is provided by
39 * the file mime.info in the includes directory.
41 define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
42 application/pdf [OFFICE]
43 text/javascript application/x-javascript [EXECUTABLE]
44 application/x-shockwave-flash [MULTIMEDIA]
45 audio/midi [AUDIO]
46 audio/x-aiff [AUDIO]
47 audio/x-wav [AUDIO]
48 audio/mp3 audio/mpeg [AUDIO]
49 application/ogg audio/ogg video/ogg [MULTIMEDIA]
50 image/x-bmp image/bmp [BITMAP]
51 image/gif [BITMAP]
52 image/jpeg [BITMAP]
53 image/png [BITMAP]
54 image/svg image/svg+xml [DRAWING]
55 image/tiff [BITMAP]
56 image/vnd.djvu [BITMAP]
57 text/plain [TEXT]
58 text/html [TEXT]
59 video/ogg [VIDEO]
60 video/mpeg [VIDEO]
61 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
62 END_STRING
65 #note: because this file is possibly included by a function,
66 #we need to access the global scope explicitely!
67 global $wgLoadFileinfoExtension;
69 if ($wgLoadFileinfoExtension) {
70 if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
73 /** Implements functions related to mime types such as detection and mapping to
74 * file extension,
76 * Instances of this class are stateles, there only needs to be one global instance
77 * of MimeMagic. Please use wfGetMimeMagic to get that instance.
78 * @package MediaWiki
80 class MimeMagic {
82 /**
83 * Mapping of media types to arrays of mime types.
84 * This is used by findMediaType and getMediaType, respectively
86 var $mMediaTypes= NULL;
88 /** Map of mime type aliases
90 var $mMimeTypeAliases= NULL;
92 /** map of mime types to file extensions (as a space seprarated list)
94 var $mMimeToExt= NULL;
96 /** map of file extensions types to mime types (as a space seprarated list)
98 var $mExtToMime= NULL;
100 /** Initializes the MimeMagic object. This is called by wfGetMimeMagic when instantiation
101 * the global MimeMagic singleton object.
103 * This constructor parses the mime.types and mime.info files and build internal mappings.
105 function MimeMagic() {
107 * --- load mime.types ---
110 global $wgMimeTypeFile;
112 $types= MM_WELL_KNOWN_MIME_TYPES;
114 if ($wgMimeTypeFile) {
115 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
116 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
118 $types.= "\n";
119 $types.= file_get_contents($wgMimeTypeFile);
121 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
123 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
125 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
126 $types= str_replace("\t"," ",$types);
128 $this->mMimeToExt= array();
129 $this->mToMime= array();
131 $lines= explode("\n",$types);
132 foreach ($lines as $s) {
133 $s= trim($s);
134 if (empty($s)) continue;
135 if (strpos($s,'#')===0) continue;
137 $s= strtolower($s);
138 $i= strpos($s,' ');
140 if ($i===false) continue;
142 #print "processing MIME line $s<br>";
144 $mime= substr($s,0,$i);
145 $ext= trim(substr($s,$i+1));
147 if (empty($ext)) continue;
149 if (@$this->mMimeToExt[$mime]) $this->mMimeToExt[$mime] .= ' '.$ext;
150 else $this->mMimeToExt[$mime]= $ext;
152 $extensions= explode(' ',$ext);
154 foreach ($extensions as $e) {
155 $e= trim($e);
156 if (empty($e)) continue;
158 if (@$this->mExtToMime[$e]) $this->mExtToMime[$e] .= ' '.$mime;
159 else $this->mExtToMime[$e]= $mime;
164 * --- load mime.info ---
167 global $wgMimeInfoFile;
169 $info= MM_WELL_KNOWN_MIME_INFO;
171 if ($wgMimeInfoFile) {
172 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
173 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
175 $info.= "\n";
176 $info.= file_get_contents($wgMimeInfoFile);
178 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
180 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
182 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
183 $info= str_replace("\t"," ",$info);
185 $this->mMimeTypeAliases= array();
186 $this->mMediaTypes= array();
188 $lines= explode("\n",$info);
189 foreach ($lines as $s) {
190 $s= trim($s);
191 if (empty($s)) continue;
192 if (strpos($s,'#')===0) continue;
194 $s= strtolower($s);
195 $i= strpos($s,' ');
197 if ($i===false) continue;
199 #print "processing MIME INFO line $s<br>";
201 $match= array();
202 if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
203 $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
204 $mtype= trim(strtoupper($match[1]));
206 else $mtype= MEDIATYPE_UNKNOWN;
208 $m= explode(' ',$s);
210 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
212 foreach ($m as $mime) {
213 $mime= trim($mime);
214 if (empty($mime)) continue;
216 $this->mMediaTypes[$mtype][]= $mime;
219 if (sizeof($m)>1) {
220 $main= $m[0];
221 for ($i=1; $i<sizeof($m); $i+= 1) {
222 $mime= $m[$i];
223 $this->mMimeTypeAliases[$mime]= $main;
230 /** returns a list of file extensions for a given mime type
231 * as a space separated string.
233 function getExtensionsForType($mime) {
234 $mime= strtolower($mime);
236 $r= @$this->mMimeToExt[$mime];
238 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
239 $mime= $this->mMimeTypeAliases[$mime];
240 $r= @$this->mMimeToExt[$mime];
243 return $r;
246 /** returns a list of mime types for a given file extension
247 * as a space separated string.
249 function getTypesForExtension($ext) {
250 $ext= strtolower($ext);
252 $r= @$this->mExtToMime[$ext];
253 return $r;
256 /** returns a single mime type for a given file extension.
257 * This is always the first type from the list returned by getTypesForExtension($ext).
259 function guessTypesForExtension($ext) {
260 $m= $this->getTypesForExtension( $ext );
261 if( is_null($m) ) return NULL;
263 $m= trim( $m );
264 $m= preg_replace('/\s.*$/','',$m);
266 return $m;
270 /** tests if the extension matches the given mime type.
271 * returns true if a match was found, NULL if the mime type is unknown,
272 * and false if the mime type is known but no matches where found.
274 function isMatchingExtension($extension,$mime) {
275 $ext= $this->getExtensionsForType($mime);
277 if (!$ext) {
278 return NULL; //unknown
281 $ext= explode(' ',$ext);
283 $extension= strtolower($extension);
284 if (in_array($extension,$ext)) {
285 return true;
288 return false;
291 /** returns true if the mime type is known to represent
292 * an image format supported by the PHP GD library.
294 function isPHPImageType( $mime ) {
295 #as defined by imagegetsize and image_type_to_mime
296 static $types = array(
297 'image/gif', 'image/jpeg', 'image/png',
298 'image/x-bmp', 'image/xbm', 'image/tiff',
299 'image/jp2', 'image/jpeg2000', 'image/iff',
300 'image/xbm', 'image/x-xbitmap',
301 'image/vnd.wap.wbmp', 'image/vnd.xiff',
302 'image/x-photoshop',
303 'application/x-shockwave-flash',
306 return in_array( $mime, $types );
310 * Returns true if the extension represents a type which can
311 * be reliably detected from its content. Use this to determine
312 * whether strict content checks should be applied to reject
313 * invalid uploads; if we can't identify the type we won't
314 * be able to say if it's invalid.
316 * @todo Be more accurate when using fancy mime detector plugins;
317 * right now this is the bare minimum getimagesize() list.
318 * @return bool
320 function isRecognizableExtension( $extension ) {
321 static $types = array(
322 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
323 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
324 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
325 'xbm', 'djvu'
327 return in_array( strtolower( $extension ), $types );
331 /** mime type detection. This uses detectMimeType to detect the mim type of the file,
332 * but applies additional checks to determine some well known file formats that may be missed
333 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
335 * @param string $file The file to check
336 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
338 * @return string the mime type of $file
340 function guessMimeType( $file, $useExt=true ) {
341 $fname = 'MimeMagic::guessMimeType';
342 $mime= $this->detectMimeType($file,$useExt);
344 // Read a chunk of the file
345 $f = fopen( $file, "rt" );
346 if( !$f ) return "unknown/unknown";
347 $head = fread( $f, 1024 );
348 fclose( $f );
350 $sub4 = substr( $head, 0, 4 );
351 if ( $sub4 == "\x01\x00\x09\x00" || $sub4 == "\xd7\xcd\xc6\x9a" ) {
352 // WMF kill kill kill
353 // Note that WMF may have a bare header, no magic number.
354 // The former of the above two checks is theoretically prone to false positives
355 $mime = "application/x-msmetafile";
358 if (strpos($mime,"text/")===0 || $mime==="application/xml") {
360 $xml_type= NULL;
361 $script_type= NULL;
364 * look for XML formats (XHTML and SVG)
366 if ($mime==="text/sgml" ||
367 $mime==="text/plain" ||
368 $mime==="text/html" ||
369 $mime==="text/xml" ||
370 $mime==="application/xml") {
372 if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
373 elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
374 elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
375 elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
377 if ($xml_type) {
378 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
380 $match= array();
381 $doctype= "";
382 $tag= "";
384 if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
385 if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
387 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
389 if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg";
390 elseif ($tag==="svg") $mime= "image/svg";
391 elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
392 elseif ($tag==="html") $mime= "text/html";
394 $test_more= false;
399 * look for shell scripts
401 if (!$xml_type) {
402 $script_type= NULL;
404 #detect by shebang
405 if (substr($head,0,2)=="#!") $script_type= "ASCII";
406 elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
407 elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
408 elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
410 if ($script_type) {
411 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
413 $match= array();
414 $prog= "";
416 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) $script= $match[2];
418 $mime= "application/x-$prog";
423 * look for PHP
425 if( !$xml_type && !$script_type ) {
427 if( ( strpos( $head, '<?php' ) !== false ) ||
428 ( strpos( $head, '<? ' ) !== false ) ||
429 ( strpos( $head, "<?\n" ) !== false ) ||
430 ( strpos( $head, "<?\t" ) !== false ) ||
431 ( strpos( $head, "<?=" ) !== false ) ||
433 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
434 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
435 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
436 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
437 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
439 $mime= "application/x-php";
445 if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
447 wfDebug("$fname: final mime type of $file: $mime\n");
448 return $mime;
451 /** Internal mime type detection, please use guessMimeType() for application code instead.
452 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
453 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
454 * If the dections fails and $useExt is true, the mime type is guessed from the file extension, using guessTypesForExtension.
455 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
456 * If no mime type can be determined, this function returns "unknown/unknown".
458 * @param string $file The file to check
459 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
461 * @return string the mime type of $file
462 * @access private
464 function detectMimeType( $file, $useExt=true ) {
465 $fname = 'MimeMagic::detectMimeType';
467 global $wgMimeDetectorCommand;
469 $m= NULL;
470 if ($wgMimeDetectorCommand) {
471 $fn= wfEscapeShellArg($file);
472 $m= `$wgMimeDetectorCommand $fn`;
474 else if (function_exists("finfo_open") && function_exists("finfo_file")) {
476 # This required the fileinfo extension by PECL,
477 # see http://pecl.php.net/package/fileinfo
478 # This must be compiled into PHP
480 # finfo is the official replacement for the deprecated
481 # mime_content_type function, see below.
483 # If you may need to load the fileinfo extension at runtime, set
484 # $wgLoadFileinfoExtension in LocalSettings.php
486 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
488 if ($mime_magic_resource) {
489 $m= finfo_file($mime_magic_resource, $file);
491 finfo_close($mime_magic_resource);
493 else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
495 else if (function_exists("mime_content_type")) {
497 # NOTE: this function is available since PHP 4.3.0, but only if
498 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
500 # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
501 # sometimes, this may even be needed under linus/unix.
503 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
504 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
506 $m= mime_content_type($file);
508 if ( $m == 'text/plain' ) {
509 // mime_content_type sometimes considers DJVU files to be text/plain.
510 $deja = new DjVuImage( $file );
511 if( $deja->isValid() ) {
512 wfDebug("$fname: (re)detected $file as image/vnd.djvu\n");
513 $m = 'image/vnd.djvu';
517 else wfDebug("$fname: no magic mime detector found!\n");
519 if ($m) {
520 #normalize
521 $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
522 $m= trim($m);
523 $m= strtolower($m);
525 if (strpos($m,'unknown')!==false) $m= NULL;
526 else {
527 wfDebug("$fname: magic mime type of $file: $m\n");
528 return $m;
532 #if still not known, use getimagesize to find out the type of image
533 #TODO: skip things that do not have a well-known image extension? Would that be safe?
534 wfSuppressWarnings();
535 $gis = getimagesize( $file );
536 wfRestoreWarnings();
538 $notAnImage= false;
540 if ($gis && is_array($gis) && $gis[2]) {
541 switch ($gis[2]) {
542 case IMAGETYPE_GIF: $m= "image/gif"; break;
543 case IMAGETYPE_JPEG: $m= "image/jpeg"; break;
544 case IMAGETYPE_PNG: $m= "image/png"; break;
545 case IMAGETYPE_SWF: $m= "application/x-shockwave-flash"; break;
546 case IMAGETYPE_PSD: $m= "application/photoshop"; break;
547 case IMAGETYPE_BMP: $m= "image/bmp"; break;
548 case IMAGETYPE_TIFF_II: $m= "image/tiff"; break;
549 case IMAGETYPE_TIFF_MM: $m= "image/tiff"; break;
550 case IMAGETYPE_JPC: $m= "image"; break;
551 case IMAGETYPE_JP2: $m= "image/jpeg2000"; break;
552 case IMAGETYPE_JPX: $m= "image/jpeg2000"; break;
553 case IMAGETYPE_JB2: $m= "image"; break;
554 case IMAGETYPE_SWC: $m= "application/x-shockwave-flash"; break;
555 case IMAGETYPE_IFF: $m= "image/vnd.xiff"; break;
556 case IMAGETYPE_WBMP: $m= "image/vnd.wap.wbmp"; break;
557 case IMAGETYPE_XBM: $m= "image/x-xbitmap"; break;
560 if ($m) {
561 wfDebug("$fname: image mime type of $file: $m\n");
562 return $m;
564 else $notAnImage= true;
565 } else {
566 // Also test DjVu
567 $deja = new DjVuImage( $file );
568 if( $deja->isValid() ) {
569 wfDebug("$fname: detected $file as image/vnd.djvu\n");
570 return 'image/vnd.djvu';
574 #if desired, look at extension as a fallback.
575 if ($useExt) {
576 $i = strrpos( $file, '.' );
577 $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
579 $m= $this->guessTypesForExtension($e);
581 #TODO: if $notAnImage is set, do not trust the file extension if
582 # the results is one of the image types that should have been recognized
583 # by getimagesize
585 if ($m) {
586 wfDebug("$fname: extension mime type of $file: $m\n");
587 return $m;
591 #unknown type
592 wfDebug("$fname: failed to guess mime type for $file!\n");
593 return "unknown/unknown";
597 * Determine the media type code for a file, using its mime type, name and possibly
598 * its contents.
600 * This function relies on the findMediaType(), mapping extensions and mime
601 * types to media types.
603 * @todo analyse file if need be
604 * @todo look at multiple extension, separately and together.
606 * @param string $path full path to the image file, in case we have to look at the contents
607 * (if null, only the mime type is used to determine the media type code).
608 * @param string $mime mime type. If null it will be guessed using guessMimeType.
610 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
612 function getMediaType($path=NULL,$mime=NULL) {
613 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
615 #if mime type is unknown, guess it
616 if( !$mime ) $mime= $this->guessMimeType($path,false);
618 #special code for ogg - detect if it's video (theora),
619 #else label it as sound.
620 if( $mime=="application/ogg" && file_exists($path) ) {
622 // Read a chunk of the file
623 $f = fopen( $path, "rt" );
624 if( !$f ) return MEDIATYPE_UNKNOWN;
625 $head = fread( $f, 256 );
626 fclose( $f );
628 $head= strtolower( $head );
630 #This is an UGLY HACK, file should be parsed correctly
631 if( strpos($head,'theora')!==false ) return MEDIATYPE_VIDEO;
632 elseif( strpos($head,'vorbis')!==false ) return MEDIATYPE_AUDIO;
633 elseif( strpos($head,'flac')!==false ) return MEDIATYPE_AUDIO;
634 elseif( strpos($head,'speex')!==false ) return MEDIATYPE_AUDIO;
635 else return MEDIATYPE_MULTIMEDIA;
638 #check for entry for full mime type
639 if( $mime ) {
640 $type= $this->findMediaType($mime);
641 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
644 #check for entry for file extension
645 $e= NULL;
646 if( $path ) {
647 $i = strrpos( $path, '.' );
648 $e= strtolower( $i ? substr( $path, $i + 1 ) : '' );
650 #TODO: look at multi-extension if this fails, parse from full path
652 $type= $this->findMediaType('.'.$e);
653 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
656 #check major mime type
657 if( $mime ) {
658 $i= strpos($mime,'/');
659 if( $i !== false ) {
660 $major= substr($mime,0,$i);
661 $type= $this->findMediaType($major);
662 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
666 if( !$type ) $type= MEDIATYPE_UNKNOWN;
668 return $type;
671 /** returns a media code matching the given mime type or file extension.
672 * File extensions are represented by a string starting with a dot (.) to
673 * distinguish them from mime types.
675 * This funktion relies on the mapping defined by $this->mMediaTypes
676 * @access private
678 function findMediaType($extMime) {
680 if (strpos($extMime,'.')===0) { #if it's an extension, look up the mime types
681 $m= $this->getTypesForExtension(substr($extMime,1));
682 if (!$m) return MEDIATYPE_UNKNOWN;
684 $m= explode(' ',$m);
686 else { #normalize mime type
687 if (isset($this->mMimeTypeAliases[$extMime])) {
688 $extMime= $this->mMimeTypeAliases[$extMime];
691 $m= array($extMime);
694 foreach ($m as $mime) {
695 foreach ($this->mMediaTypes as $type => $codes) {
696 if (in_array($mime,$codes,true)) return $type;
700 return MEDIATYPE_UNKNOWN;