unbroke classic and cologne blue skins
[mediawiki.git] / includes / MimeMagic.php
blob076dcc215c4ced2cf2a14a859d2824f529a32834
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 text/plain txt
29 text/html html htm
30 video/ogg ogm ogg
31 video/mpeg mpg mpeg
32 END_STRING
35 /** Defines a set of well known mime info entries
36 * This is used as a fallback to mime.info files.
37 * An extensive list of well known mime types is provided by
38 * the file mime.info in the includes directory.
40 define('MM_WELL_KNOWN_MIME_INFO', <<<END_STRING
41 application/pdf [OFFICE]
42 text/javascript application/x-javascript [EXECUTABLE]
43 application/x-shockwave-flash [MULTIMEDIA]
44 audio/midi [AUDIO]
45 audio/x-aiff [AUDIO]
46 audio/x-wav [AUDIO]
47 audio/mp3 audio/mpeg [AUDIO]
48 application/ogg audio/ogg video/ogg [MULTIMEDIA]
49 image/x-bmp image/bmp [BITMAP]
50 image/gif [BITMAP]
51 image/jpeg [BITMAP]
52 image/png [BITMAP]
53 image/svg image/svg+xml [DRAWING]
54 image/tiff [BITMAP]
55 text/plain [TEXT]
56 text/html [TEXT]
57 video/ogg [VIDEO]
58 video/mpeg [VIDEO]
59 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
60 END_STRING
63 #note: because this file is possibly included by a function,
64 #we need to access the global scope explicitely!
65 global $wgLoadFileinfoExtension;
67 if ($wgLoadFileinfoExtension) {
68 if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
71 /** Implements functions related to mime types such as detection and mapping to
72 * file extension,
74 * Instances of this class are stateles, there only needs to be one global instance
75 * of MimeMagic. Please use wfGetMimeMagic to get that instance.
77 class MimeMagic {
79 /**
80 * Mapping of media types to arrays of mime types.
81 * This is used by findMediaType and getMediaType, respectively
83 var $mMediaTypes= NULL;
85 /** Map of mime type aliases
87 var $mMimeTypeAliases= NULL;
89 /** map of mime types to file extensions (as a space seprarated list)
91 var $mMimeToExt= NULL;
93 /** map of file extensions types to mime types (as a space seprarated list)
95 var $mExtToMime= NULL;
97 /** Initializes the MimeMagic object. This is called by wfGetMimeMagic when instantiation
98 * the global MimeMagic singleton object.
100 * This constructor parses the mime.types and mime.info files and build internal mappings.
102 function MimeMagic() {
104 * --- load mime.types ---
107 global $wgMimeTypeFile;
109 $types= MM_WELL_KNOWN_MIME_TYPES;
111 if ($wgMimeTypeFile) {
112 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
113 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
115 $types.= "\n";
116 $types.= file_get_contents($wgMimeTypeFile);
118 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
120 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
122 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
123 $types= str_replace("\t"," ",$types);
125 $this->mMimeToExt= array();
126 $this->mToMime= array();
128 $lines= explode("\n",$types);
129 foreach ($lines as $s) {
130 $s= trim($s);
131 if (empty($s)) continue;
132 if (strpos($s,'#')===0) continue;
134 $s= strtolower($s);
135 $i= strpos($s,' ');
137 if ($i===false) continue;
139 #print "processing MIME line $s<br>";
141 $mime= substr($s,0,$i);
142 $ext= trim(substr($s,$i+1));
144 if (empty($ext)) continue;
146 if (@$this->mMimeToExt[$mime]) $this->mMimeToExt[$mime] .= ' '.$ext;
147 else $this->mMimeToExt[$mime]= $ext;
149 $extensions= explode(' ',$ext);
151 foreach ($extensions as $e) {
152 $e= trim($e);
153 if (empty($e)) continue;
155 if (@$this->mExtToMime[$e]) $this->mExtToMime[$e] .= ' '.$mime;
156 else $this->mExtToMime[$e]= $mime;
161 * --- load mime.info ---
164 global $wgMimeInfoFile;
166 $info= MM_WELL_KNOWN_MIME_INFO;
168 if ($wgMimeInfoFile) {
169 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
170 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
172 $info.= "\n";
173 $info.= file_get_contents($wgMimeInfoFile);
175 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
177 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
179 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
180 $info= str_replace("\t"," ",$info);
182 $this->mMimeTypeAliases= array();
183 $this->mMediaTypes= array();
185 $lines= explode("\n",$info);
186 foreach ($lines as $s) {
187 $s= trim($s);
188 if (empty($s)) continue;
189 if (strpos($s,'#')===0) continue;
191 $s= strtolower($s);
192 $i= strpos($s,' ');
194 if ($i===false) continue;
196 #print "processing MIME INFO line $s<br>";
198 $match= array();
199 if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
200 $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
201 $mtype= trim(strtoupper($match[1]));
203 else $mtype= MEDIATYPE_UNKNOWN;
205 $m= explode(' ',$s);
207 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
209 foreach ($m as $mime) {
210 $mime= trim($mime);
211 if (empty($mime)) continue;
213 $this->mMediaTypes[$mtype][]= $mime;
216 if (sizeof($m)>1) {
217 $main= $m[0];
218 for ($i=1; $i<sizeof($m); $i+= 1) {
219 $mime= $m[$i];
220 $this->mMimeTypeAliases[$mime]= $main;
227 /** returns a list of file extensions for a given mime type
228 * as a space separated string.
230 function getExtensionsForType($mime) {
231 $mime= strtolower($mime);
233 $r= @$this->mMimeToExt[$mime];
235 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
236 $mime= $this->mMimeTypeAliases[$mime];
237 $r= @$this->mMimeToExt[$mime];
240 return $r;
243 /** returns a list of mime types for a given file extension
244 * as a space separated string.
246 function getTypesForExtension($ext) {
247 $ext= strtolower($ext);
249 $r= @$this->mExtToMime[$ext];
250 return $r;
253 /** returns a single mime type for a given file extension.
254 * This is always the first type from the list returned by getTypesForExtension($ext).
256 function guessTypesForExtension($ext) {
257 $m= $this->getTypesForExtension( $ext );
258 if( is_null($m) ) return NULL;
260 $m= trim( $m );
261 $m= preg_replace('/\s.*$/','',$m);
263 return $m;
267 /** tests if the extension matches the given mime type.
268 * returns true if a match was found, NULL if the mime type is unknown,
269 * and false if the mime type is known but no matches where found.
271 function isMatchingExtension($extension,$mime) {
272 $ext= $this->getExtensionsForType($mime);
274 if (!$ext) {
275 return NULL; //unknown
278 $ext= explode(' ',$ext);
280 $extension= strtolower($extension);
281 if (in_array($extension,$ext)) {
282 return true;
285 return false;
288 /** returns true if the mime type is known to represent
289 * an image format supported by the PHP GD library.
291 function isPHPImageType( $mime ) {
292 #as defined by imagegetsize and image_type_to_mime
293 static $types = array(
294 'image/gif', 'image/jpeg', 'image/png',
295 'image/x-bmp', 'image/xbm', 'image/tiff',
296 'image/jp2', 'image/jpeg2000', 'image/iff',
297 'image/xbm', 'image/x-xbitmap',
298 'image/vnd.wap.wbmp', 'image/vnd.xiff',
299 'image/x-photoshop',
300 'application/x-shockwave-flash',
303 return in_array( $mime, $types );
307 /** mime type detection. This uses detectMimeType to detect the mim type of the file,
308 * but applies additional checks to determine some well known file formats that may be missed
309 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
311 * @param string $file The file to check
312 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
314 * @return string the mime type of $file
316 function guessMimeType( $file, $useExt=true ) {
317 $fname = 'MimeMagic::guessMimeType';
318 $mime= $this->detectMimeType($file,$useExt);
320 if (strpos($mime,"text/")===0 ||
321 $mime==="application/xml") {
323 // Read a chunk of the file
324 $f = fopen( $file, "rt" );
325 if( !$f ) return "unknown/unknown";
326 $head = fread( $f, 1024 );
327 fclose( $f );
329 $xml_type= NULL;
330 $script_type= NULL;
333 * look for XML formats (XHTML and SVG)
335 if ($mime==="text/sgml" ||
336 $mime==="text/plain" ||
337 $mime==="text/html" ||
338 $mime==="text/xml" ||
339 $mime==="application/xml") {
341 if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
342 elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
343 elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
344 elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
346 if ($xml_type) {
347 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
349 $match= array();
350 $doctype= "";
351 $tag= "";
353 if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
354 if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
356 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
358 if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg";
359 elseif ($tag==="svg") $mime= "image/svg";
360 elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
361 elseif ($tag==="html") $mime= "text/html";
363 $test_more= false;
368 * look for shell scripts
370 if (!$xml_type) {
371 $script_type= NULL;
373 #detect by shebang
374 if (substr($head,0,2)=="#!") $script_type= "ASCII";
375 elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
376 elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
377 elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
379 if ($script_type) {
380 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
382 $match= array();
383 $prog= "";
385 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) $script= $match[2];
387 $mime= "application/x-$prog";
392 * look for PHP
394 if( !$xml_type && !$script_type ) {
396 if( ( strpos( $head, '<?php' ) !== false ) ||
397 ( strpos( $head, '<? ' ) !== false ) ||
398 ( strpos( $head, "<?\n" ) !== false ) ||
399 ( strpos( $head, "<?\t" ) !== false ) ||
400 ( strpos( $head, "<?=" ) !== false ) ||
402 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
403 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
404 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
405 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
406 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
408 $mime= "application/x-php";
414 if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
416 wfDebug("$fname: final mime type of $file: $mime\n");
417 return $mime;
420 /** Internal mime type detection, please use guessMimeType() for application code instead.
421 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
422 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
423 * If the dections fails and $useExt is true, the mime type is guessed from the file extension, using guessTypesForExtension.
424 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
425 * If no mime type can be determined, this function returns "unknown/unknown".
427 * @param string $file The file to check
428 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
430 * @return string the mime type of $file
431 * @private
433 function detectMimeType( $file, $useExt=true ) {
434 $fname = 'MimeMagic::detectMimeType';
436 global $wgMimeDetectorCommand;
438 $m= NULL;
439 if ($wgMimeDetectorCommand) {
440 $fn= wfEscapeShellArg($file);
441 $m= `$wgMimeDetectorCommand $fn`;
443 else if (function_exists("finfo_open") && function_exists("finfo_file")) {
445 # This required the fileinfo extension by PECL,
446 # see http://pecl.php.net/package/fileinfo
447 # This must be compiled into PHP
449 # finfo is the official replacement for the deprecated
450 # mime_content_type function, see below.
452 # If you may need to load the fileinfo extension at runtime, set
453 # $wgLoadFileinfoExtension in LocalSettings.php
455 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
457 if ($mime_magic_resource) {
458 $m= finfo_file($mime_magic_resource, $file);
460 finfo_close($mime_magic_resource);
462 else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
464 else if (function_exists("mime_content_type")) {
466 # NOTE: this function is available since PHP 4.3.0, but only if
467 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
469 # On Winodws, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
470 # sometimes, this may even be needed under linus/unix.
472 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
473 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
475 $m= mime_content_type($file);
477 else wfDebug("$fname: no magic mime detector found!\n");
479 if ($m) {
480 #normalize
481 $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
482 $m= trim($m);
483 $m= strtolower($m);
485 if (strpos($m,'unknown')!==false) $m= NULL;
486 else {
487 wfDebug("$fname: magic mime type of $file: $m\n");
488 return $m;
492 #if still not known, use getimagesize to find out the type of image
493 #TODO: skip things that do not have a well-known image extension? Would that be safe?
494 wfSuppressWarnings();
495 $gis = getimagesize( $file );
496 wfRestoreWarnings();
498 $notAnImage= false;
500 if ($gis && is_array($gis) && $gis[2]) {
501 switch ($gis[2]) {
502 case IMAGETYPE_GIF: $m= "image/gif"; break;
503 case IMAGETYPE_JPEG: $m= "image/jpeg"; break;
504 case IMAGETYPE_PNG: $m= "image/png"; break;
505 case IMAGETYPE_SWF: $m= "application/x-shockwave-flash"; break;
506 case IMAGETYPE_PSD: $m= "application/photoshop"; break;
507 case IMAGETYPE_BMP: $m= "image/bmp"; break;
508 case IMAGETYPE_TIFF_II: $m= "image/tiff"; break;
509 case IMAGETYPE_TIFF_MM: $m= "image/tiff"; break;
510 case IMAGETYPE_JPC: $m= "image"; break;
511 case IMAGETYPE_JP2: $m= "image/jpeg2000"; break;
512 case IMAGETYPE_JPX: $m= "image/jpeg2000"; break;
513 case IMAGETYPE_JB2: $m= "image"; break;
514 case IMAGETYPE_SWC: $m= "application/x-shockwave-flash"; break;
515 case IMAGETYPE_IFF: $m= "image/vnd.xiff"; break;
516 case IMAGETYPE_WBMP: $m= "image/vnd.wap.wbmp"; break;
517 case IMAGETYPE_XBM: $m= "image/x-xbitmap"; break;
520 if ($m) {
521 wfDebug("$fname: image mime type of $file: $m\n");
522 return $m;
524 else $notAnImage= true;
527 #if desired, look at extension as a fallback.
528 if ($useExt) {
529 $i = strrpos( $file, '.' );
530 $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
532 $m= $this->guessTypesForExtension($e);
534 #TODO: if $notAnImage is set, do not trust the file extension if
535 # the results is one of the image types that should have been recognized
536 # by getimagesize
538 if ($m) {
539 wfDebug("$fname: extension mime type of $file: $m\n");
540 return $m;
544 #unknown type
545 wfDebug("$fname: failed to guess mime type for $file!\n");
546 return "unknown/unknown";
550 * Determine the media type code for a file, using its mime type, name and possibly
551 * its contents.
553 * This function relies on the findMediaType(), mapping extensions and mime
554 * types to media types.
556 * @todo analyse file if need be
557 * @todo look at multiple extension, separately and together.
559 * @param string $path full path to the image file, in case we have to look at the contents
560 * (if null, only the mime type is used to determine the media type code).
561 * @param string $mime mime type. If null it will be guessed using guessMimeType.
563 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
565 function getMediaType($path=NULL,$mime=NULL) {
566 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
568 #if mime type is unknown, guess it
569 if( !$mime ) $mime= $this->guessMimeType($path,false);
571 #special code for ogg - detect if it's video (theora),
572 #else label it as sound.
573 if( $mime=="application/ogg" && file_exists($path) ) {
575 // Read a chunk of the file
576 $f = fopen( $path, "rt" );
577 if( !$f ) return MEDIATYPE_UNKNOWN;
578 $head = fread( $f, 256 );
579 fclose( $f );
581 $head= strtolower( $head );
583 #This is an UGLY HACK, file should be parsed correctly
584 if( strpos($head,'theora')!==false ) return MEDIATYPE_VIDEO;
585 elseif( strpos($head,'vorbis')!==false ) return MEDIATYPE_AUDIO;
586 elseif( strpos($head,'flac')!==false ) return MEDIATYPE_AUDIO;
587 elseif( strpos($head,'speex')!==false ) return MEDIATYPE_AUDIO;
588 else return MEDIATYPE_MULTIMEDIA;
591 #check for entry for full mime type
592 if( $mime ) {
593 $type= $this->findMediaType($mime);
594 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
597 #check for entry for file extension
598 $e= NULL;
599 if( $path ) {
600 $i = strrpos( $path, '.' );
601 $e= strtolower( $i ? substr( $path, $i + 1 ) : '' );
603 #TODO: look at multi-extension if this fails, parse from full path
605 $type= $this->findMediaType('.'.$e);
606 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
609 #check major mime type
610 if( $mime ) {
611 $i= strpos($mime,'/');
612 if( $i !== false ) {
613 $major= substr($mime,0,$i);
614 $type= $this->findMediaType($major);
615 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
619 if( !$type ) $type= MEDIATYPE_UNKNOWN;
621 return $type;
624 /** returns a media code matching the given mime type or file extension.
625 * File extensions are represented by a string starting with a dot (.) to
626 * distinguish them from mime types.
628 * This funktion relies on the mapping defined by $this->mMediaTypes
629 * @private
631 function findMediaType($extMime) {
633 if (strpos($extMime,'.')===0) { #if it's an extension, look up the mime types
634 $m= $this->getTypesForExtension(substr($extMime,1));
635 if (!$m) return MEDIATYPE_UNKNOWN;
637 $m= explode(' ',$m);
639 else { #normalize mime type
640 if (isset($this->mMimeTypeAliases[$extMime])) {
641 $extMime= $this->mMimeTypeAliases[$extMime];
644 $m= array($extMime);
647 foreach ($m as $mime) {
648 foreach ($this->mMediaTypes as $type => $codes) {
649 if (in_array($mime,$codes,true)) return $type;
653 return MEDIATYPE_UNKNOWN;