don't produce warnings in get() if the item is missing
[mediawiki.git] / includes / MimeMagic.php
blob5be0ee602954e86e58b6835e82eff536931c519a
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.
76 * @package MediaWiki
78 class MimeMagic {
80 /**
81 * Mapping of media types to arrays of mime types.
82 * This is used by findMediaType and getMediaType, respectively
84 var $mMediaTypes= NULL;
86 /** Map of mime type aliases
88 var $mMimeTypeAliases= NULL;
90 /** map of mime types to file extensions (as a space seprarated list)
92 var $mMimeToExt= NULL;
94 /** map of file extensions types to mime types (as a space seprarated list)
96 var $mExtToMime= NULL;
98 /** Initializes the MimeMagic object. This is called by wfGetMimeMagic when instantiation
99 * the global MimeMagic singleton object.
101 * This constructor parses the mime.types and mime.info files and build internal mappings.
103 function MimeMagic() {
105 * --- load mime.types ---
108 global $wgMimeTypeFile;
110 $types= MM_WELL_KNOWN_MIME_TYPES;
112 if ($wgMimeTypeFile) {
113 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
114 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
116 $types.= "\n";
117 $types.= file_get_contents($wgMimeTypeFile);
119 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
121 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
123 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
124 $types= str_replace("\t"," ",$types);
126 $this->mMimeToExt= array();
127 $this->mToMime= array();
129 $lines= explode("\n",$types);
130 foreach ($lines as $s) {
131 $s= trim($s);
132 if (empty($s)) continue;
133 if (strpos($s,'#')===0) continue;
135 $s= strtolower($s);
136 $i= strpos($s,' ');
138 if ($i===false) continue;
140 #print "processing MIME line $s<br>";
142 $mime= substr($s,0,$i);
143 $ext= trim(substr($s,$i+1));
145 if (empty($ext)) continue;
147 if (@$this->mMimeToExt[$mime]) $this->mMimeToExt[$mime] .= ' '.$ext;
148 else $this->mMimeToExt[$mime]= $ext;
150 $extensions= explode(' ',$ext);
152 foreach ($extensions as $e) {
153 $e= trim($e);
154 if (empty($e)) continue;
156 if (@$this->mExtToMime[$e]) $this->mExtToMime[$e] .= ' '.$mime;
157 else $this->mExtToMime[$e]= $mime;
162 * --- load mime.info ---
165 global $wgMimeInfoFile;
167 $info= MM_WELL_KNOWN_MIME_INFO;
169 if ($wgMimeInfoFile) {
170 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
171 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
173 $info.= "\n";
174 $info.= file_get_contents($wgMimeInfoFile);
176 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
178 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
180 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
181 $info= str_replace("\t"," ",$info);
183 $this->mMimeTypeAliases= array();
184 $this->mMediaTypes= array();
186 $lines= explode("\n",$info);
187 foreach ($lines as $s) {
188 $s= trim($s);
189 if (empty($s)) continue;
190 if (strpos($s,'#')===0) continue;
192 $s= strtolower($s);
193 $i= strpos($s,' ');
195 if ($i===false) continue;
197 #print "processing MIME INFO line $s<br>";
199 $match= array();
200 if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
201 $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
202 $mtype= trim(strtoupper($match[1]));
204 else $mtype= MEDIATYPE_UNKNOWN;
206 $m= explode(' ',$s);
208 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
210 foreach ($m as $mime) {
211 $mime= trim($mime);
212 if (empty($mime)) continue;
214 $this->mMediaTypes[$mtype][]= $mime;
217 if (sizeof($m)>1) {
218 $main= $m[0];
219 for ($i=1; $i<sizeof($m); $i+= 1) {
220 $mime= $m[$i];
221 $this->mMimeTypeAliases[$mime]= $main;
228 /** returns a list of file extensions for a given mime type
229 * as a space separated string.
231 function getExtensionsForType($mime) {
232 $mime= strtolower($mime);
234 $r= @$this->mMimeToExt[$mime];
236 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
237 $mime= $this->mMimeTypeAliases[$mime];
238 $r= @$this->mMimeToExt[$mime];
241 return $r;
244 /** returns a list of mime types for a given file extension
245 * as a space separated string.
247 function getTypesForExtension($ext) {
248 $ext= strtolower($ext);
250 $r= @$this->mExtToMime[$ext];
251 return $r;
254 /** returns a single mime type for a given file extension.
255 * This is always the first type from the list returned by getTypesForExtension($ext).
257 function guessTypesForExtension($ext) {
258 $m= $this->getTypesForExtension( $ext );
259 if( is_null($m) ) return NULL;
261 $m= trim( $m );
262 $m= preg_replace('/\s.*$/','',$m);
264 return $m;
268 /** tests if the extension matches the given mime type.
269 * returns true if a match was found, NULL if the mime type is unknown,
270 * and false if the mime type is known but no matches where found.
272 function isMatchingExtension($extension,$mime) {
273 $ext= $this->getExtensionsForType($mime);
275 if (!$ext) {
276 return NULL; //unknown
279 $ext= explode(' ',$ext);
281 $extension= strtolower($extension);
282 if (in_array($extension,$ext)) {
283 return true;
286 return false;
289 /** returns true if the mime type is known to represent
290 * an image format supported by the PHP GD library.
292 function isPHPImageType( $mime ) {
293 #as defined by imagegetsize and image_type_to_mime
294 static $types = array(
295 'image/gif', 'image/jpeg', 'image/png',
296 'image/x-bmp', 'image/xbm', 'image/tiff',
297 'image/jp2', 'image/jpeg2000', 'image/iff',
298 'image/xbm', 'image/x-xbitmap',
299 'image/vnd.wap.wbmp', 'image/vnd.xiff',
300 'image/x-photoshop',
301 'application/x-shockwave-flash',
304 return in_array( $mime, $types );
308 /** mime type detection. This uses detectMimeType to detect the mim type of the file,
309 * but applies additional checks to determine some well known file formats that may be missed
310 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
312 * @param string $file The file to check
313 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
315 * @return string the mime type of $file
317 function guessMimeType( $file, $useExt=true ) {
318 $fname = 'MimeMagic::guessMimeType';
319 $mime= $this->detectMimeType($file,$useExt);
321 if (strpos($mime,"text/")===0 ||
322 $mime==="application/xml") {
324 // Read a chunk of the file
325 $f = fopen( $file, "rt" );
326 if( !$f ) return "unknown/unknown";
327 $head = fread( $f, 1024 );
328 fclose( $f );
330 $xml_type= NULL;
331 $script_type= NULL;
334 * look for XML formats (XHTML and SVG)
336 if ($mime==="text/sgml" ||
337 $mime==="text/plain" ||
338 $mime==="text/html" ||
339 $mime==="text/xml" ||
340 $mime==="application/xml") {
342 if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
343 elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
344 elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
345 elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
347 if ($xml_type) {
348 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
350 $match= array();
351 $doctype= "";
352 $tag= "";
354 if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
355 if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
357 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
359 if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg";
360 elseif ($tag==="svg") $mime= "image/svg";
361 elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
362 elseif ($tag==="html") $mime= "text/html";
364 $test_more= false;
369 * look for shell scripts
371 if (!$xml_type) {
372 $script_type= NULL;
374 #detect by shebang
375 if (substr($head,0,2)=="#!") $script_type= "ASCII";
376 elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
377 elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
378 elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
380 if ($script_type) {
381 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
383 $match= array();
384 $prog= "";
386 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) $script= $match[2];
388 $mime= "application/x-$prog";
393 * look for PHP
395 if( !$xml_type && !$script_type ) {
397 if( ( strpos( $head, '<?php' ) !== false ) ||
398 ( strpos( $head, '<? ' ) !== false ) ||
399 ( strpos( $head, "<?\n" ) !== false ) ||
400 ( strpos( $head, "<?\t" ) !== false ) ||
401 ( strpos( $head, "<?=" ) !== false ) ||
403 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
404 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
405 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
406 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
407 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
409 $mime= "application/x-php";
415 if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
417 wfDebug("$fname: final mime type of $file: $mime\n");
418 return $mime;
421 /** Internal mime type detection, please use guessMimeType() for application code instead.
422 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
423 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
424 * If the dections fails and $useExt is true, the mime type is guessed from the file extension, using guessTypesForExtension.
425 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
426 * If no mime type can be determined, this function returns "unknown/unknown".
428 * @param string $file The file to check
429 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
431 * @return string the mime type of $file
432 * @private
434 function detectMimeType( $file, $useExt=true ) {
435 $fname = 'MimeMagic::detectMimeType';
437 global $wgMimeDetectorCommand;
439 $m= NULL;
440 if ($wgMimeDetectorCommand) {
441 $fn= wfEscapeShellArg($file);
442 $m= `$wgMimeDetectorCommand $fn`;
444 else if (function_exists("finfo_open") && function_exists("finfo_file")) {
446 # This required the fileinfo extension by PECL,
447 # see http://pecl.php.net/package/fileinfo
448 # This must be compiled into PHP
450 # finfo is the official replacement for the deprecated
451 # mime_content_type function, see below.
453 # If you may need to load the fileinfo extension at runtime, set
454 # $wgLoadFileinfoExtension in LocalSettings.php
456 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
458 if ($mime_magic_resource) {
459 $m= finfo_file($mime_magic_resource, $file);
461 finfo_close($mime_magic_resource);
463 else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
465 else if (function_exists("mime_content_type")) {
467 # NOTE: this function is available since PHP 4.3.0, but only if
468 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
470 # On Winodws, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
471 # sometimes, this may even be needed under linus/unix.
473 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
474 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
476 $m= mime_content_type($file);
478 else wfDebug("$fname: no magic mime detector found!\n");
480 if ($m) {
481 #normalize
482 $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
483 $m= trim($m);
484 $m= strtolower($m);
486 if (strpos($m,'unknown')!==false) $m= NULL;
487 else {
488 wfDebug("$fname: magic mime type of $file: $m\n");
489 return $m;
493 #if still not known, use getimagesize to find out the type of image
494 #TODO: skip things that do not have a well-known image extension? Would that be safe?
495 wfSuppressWarnings();
496 $gis = getimagesize( $file );
497 wfRestoreWarnings();
499 $notAnImage= false;
501 if ($gis && is_array($gis) && $gis[2]) {
502 switch ($gis[2]) {
503 case IMAGETYPE_GIF: $m= "image/gif"; break;
504 case IMAGETYPE_JPEG: $m= "image/jpeg"; break;
505 case IMAGETYPE_PNG: $m= "image/png"; break;
506 case IMAGETYPE_SWF: $m= "application/x-shockwave-flash"; break;
507 case IMAGETYPE_PSD: $m= "application/photoshop"; break;
508 case IMAGETYPE_BMP: $m= "image/bmp"; break;
509 case IMAGETYPE_TIFF_II: $m= "image/tiff"; break;
510 case IMAGETYPE_TIFF_MM: $m= "image/tiff"; break;
511 case IMAGETYPE_JPC: $m= "image"; break;
512 case IMAGETYPE_JP2: $m= "image/jpeg2000"; break;
513 case IMAGETYPE_JPX: $m= "image/jpeg2000"; break;
514 case IMAGETYPE_JB2: $m= "image"; break;
515 case IMAGETYPE_SWC: $m= "application/x-shockwave-flash"; break;
516 case IMAGETYPE_IFF: $m= "image/vnd.xiff"; break;
517 case IMAGETYPE_WBMP: $m= "image/vnd.wap.wbmp"; break;
518 case IMAGETYPE_XBM: $m= "image/x-xbitmap"; break;
521 if ($m) {
522 wfDebug("$fname: image mime type of $file: $m\n");
523 return $m;
525 else $notAnImage= true;
528 #if desired, look at extension as a fallback.
529 if ($useExt) {
530 $i = strrpos( $file, '.' );
531 $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
533 $m= $this->guessTypesForExtension($e);
535 #TODO: if $notAnImage is set, do not trust the file extension if
536 # the results is one of the image types that should have been recognized
537 # by getimagesize
539 if ($m) {
540 wfDebug("$fname: extension mime type of $file: $m\n");
541 return $m;
545 #unknown type
546 wfDebug("$fname: failed to guess mime type for $file!\n");
547 return "unknown/unknown";
551 * Determine the media type code for a file, using its mime type, name and possibly
552 * its contents.
554 * This function relies on the findMediaType(), mapping extensions and mime
555 * types to media types.
557 * @todo analyse file if need be
558 * @todo look at multiple extension, separately and together.
560 * @param string $path full path to the image file, in case we have to look at the contents
561 * (if null, only the mime type is used to determine the media type code).
562 * @param string $mime mime type. If null it will be guessed using guessMimeType.
564 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
566 function getMediaType($path=NULL,$mime=NULL) {
567 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
569 #if mime type is unknown, guess it
570 if( !$mime ) $mime= $this->guessMimeType($path,false);
572 #special code for ogg - detect if it's video (theora),
573 #else label it as sound.
574 if( $mime=="application/ogg" && file_exists($path) ) {
576 // Read a chunk of the file
577 $f = fopen( $path, "rt" );
578 if( !$f ) return MEDIATYPE_UNKNOWN;
579 $head = fread( $f, 256 );
580 fclose( $f );
582 $head= strtolower( $head );
584 #This is an UGLY HACK, file should be parsed correctly
585 if( strpos($head,'theora')!==false ) return MEDIATYPE_VIDEO;
586 elseif( strpos($head,'vorbis')!==false ) return MEDIATYPE_AUDIO;
587 elseif( strpos($head,'flac')!==false ) return MEDIATYPE_AUDIO;
588 elseif( strpos($head,'speex')!==false ) return MEDIATYPE_AUDIO;
589 else return MEDIATYPE_MULTIMEDIA;
592 #check for entry for full mime type
593 if( $mime ) {
594 $type= $this->findMediaType($mime);
595 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
598 #check for entry for file extension
599 $e= NULL;
600 if( $path ) {
601 $i = strrpos( $path, '.' );
602 $e= strtolower( $i ? substr( $path, $i + 1 ) : '' );
604 #TODO: look at multi-extension if this fails, parse from full path
606 $type= $this->findMediaType('.'.$e);
607 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
610 #check major mime type
611 if( $mime ) {
612 $i= strpos($mime,'/');
613 if( $i !== false ) {
614 $major= substr($mime,0,$i);
615 $type= $this->findMediaType($major);
616 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
620 if( !$type ) $type= MEDIATYPE_UNKNOWN;
622 return $type;
625 /** returns a media code matching the given mime type or file extension.
626 * File extensions are represented by a string starting with a dot (.) to
627 * distinguish them from mime types.
629 * This funktion relies on the mapping defined by $this->mMediaTypes
630 * @private
632 function findMediaType($extMime) {
634 if (strpos($extMime,'.')===0) { #if it's an extension, look up the mime types
635 $m= $this->getTypesForExtension(substr($extMime,1));
636 if (!$m) return MEDIATYPE_UNKNOWN;
638 $m= explode(' ',$m);
640 else { #normalize mime type
641 if (isset($this->mMimeTypeAliases[$extMime])) {
642 $extMime= $this->mMimeTypeAliases[$extMime];
645 $m= array($extMime);
648 foreach ($m as $mime) {
649 foreach ($this->mMediaTypes as $type => $codes) {
650 if (in_array($mime,$codes,true)) return $type;
654 return MEDIATYPE_UNKNOWN;