* (bug 898) Mime type autodetection.
[lhc/web/wiklou.git] / includes / MimeMagic.php
1 <?php
2 /** Module defining helper functions for detecting and dealing with mime types.
3 *
4 * @package MediaWiki
5 */
6
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.
11 */
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
33 );
34
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.
39 */
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
61 );
62
63 #note: because this file is possibly included by a function,
64 #we need to access the global scope explicitely!
65 global $wgLoadFileinfoExtension;
66
67 if ($wgLoadFileinfoExtension) {
68 if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
69 }
70
71 /** Implements functions related to mime types such as detection and mapping to
72 * file extension,
73 *
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 */
77 class MimeMagic {
78
79 /**
80 * Mapping of media types to arrays of mime types.
81 * This is used by findMediaType and getMediaType, respectively
82 */
83 var $mMediaTypes= NULL;
84
85 /** Map of mime type aliases
86 */
87 var $mMimeTypeAliases= NULL;
88
89 /** map of mime types to file extensions (as a space seprarated list)
90 */
91 var $mMimeToExt= NULL;
92
93 /** map of file extensions types to mime types (as a space seprarated list)
94 */
95 var $mExtToMime= NULL;
96
97 /** Initializes the MimeMagic object. This is called by wfGetMimeMagic when instantiation
98 * the global MimeMagic singleton object.
99 *
100 * This constructor parses the mime.types and mime.info files and build internal mappings.
101 */
102 function MimeMagic() {
103 /*
104 * --- load mime.types ---
105 */
106
107 global $wgMimeTypeFile;
108
109 $types= MM_WELL_KNOWN_MIME_TYPES;
110
111 if ($wgMimeTypeFile) {
112 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
113 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
114
115 $types.= "\n";
116 $types.= file_get_contents($wgMimeTypeFile);
117 }
118 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
119 }
120 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
121
122 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
123 $types= str_replace("\t"," ",$types);
124
125 $this->mMimeToExt= array();
126 $this->mToMime= array();
127
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;
133
134 $s= strtolower($s);
135 $i= strpos($s,' ');
136
137 if ($i===false) continue;
138
139 #print "processing MIME line $s<br>";
140
141 $mime= substr($s,0,$i);
142 $ext= trim(substr($s,$i+1));
143
144 if (empty($ext)) continue;
145
146 if (@$this->mMimeToExt[$mime]) $this->mMimeToExt[$mime] .= ' '.$ext;
147 else $this->mMimeToExt[$mime]= $ext;
148
149 $extensions= explode(' ',$ext);
150
151 foreach ($extensions as $e) {
152 $e= trim($e);
153 if (empty($e)) continue;
154
155 if (@$this->mExtToMime[$e]) $this->mExtToMime[$e] .= ' '.$mime;
156 else $this->mExtToMime[$e]= $mime;
157 }
158 }
159
160 /*
161 * --- load mime.info ---
162 */
163
164 global $wgMimeInfoFile;
165
166 $info= MM_WELL_KNOWN_MIME_INFO;
167
168 if ($wgMimeInfoFile) {
169 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
170 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
171
172 $info.= "\n";
173 $info.= file_get_contents($wgMimeInfoFile);
174 }
175 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
176 }
177 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
178
179 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
180 $info= str_replace("\t"," ",$info);
181
182 $this->mMimeTypeAliases= array();
183 $this->mMediaTypes= array();
184
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;
190
191 $s= strtolower($s);
192 $i= strpos($s,' ');
193
194 if ($i===false) continue;
195
196 #print "processing MIME INFO line $s<br>";
197
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]));
202 }
203 else $mtype= MEDIATYPE_UNKNOWN;
204
205 $m= explode(' ',$s);
206
207 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
208
209 foreach ($m as $mime) {
210 $mime= trim($mime);
211 if (empty($mime)) continue;
212
213 $this->mMediaTypes[$mtype][]= $mime;
214 }
215
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;
221 }
222 }
223 }
224
225 }
226
227 /** returns a list of file extensions for a given mime type
228 * as a space separated string.
229 */
230 function getExtensionsForType($mime) {
231 $mime= strtolower($mime);
232
233 $r= @$this->mMimeToExt[$mime];
234
235 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
236 $mime= $this->mMimeTypeAliases[$mime];
237 $r= @$this->mMimeToExt[$mime];
238 }
239
240 return $r;
241 }
242
243 /** returns a list of mime types for a given file extension
244 * as a space separated string.
245 */
246 function getTypesForExtension($ext) {
247 $ext= strtolower($ext);
248
249 $r= @$this->mExtToMime[$ext];
250 return $r;
251 }
252
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).
255 */
256 function guessTypesForExtension($ext) {
257 $m= $this->getTypesForExtension( $ext );
258 if( is_null($m) ) return NULL;
259
260 $m= trim( $m );
261 $m= preg_replace('/\s.*$/','',$m);
262
263 return $m;
264 }
265
266
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.
270 */
271 function isMatchingExtension($extension,$mime) {
272 $ext= $this->getExtensionsForType($mime);
273
274 if (!$ext) {
275 return NULL; //unknown
276 }
277
278 $ext= explode(' ',$ext);
279
280 $extension= strtolower($extension);
281 if (in_array($extension,$ext)) {
282 return true;
283 }
284
285 return false;
286 }
287
288 /** returns true if the mime type is known to represent
289 * an image format supported by the PHP GD library.
290 */
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',
301 );
302
303 return in_array( $mime, $types );
304 }
305
306
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).
310 *
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.
313 *
314 * @return string the mime type of $file
315 */
316 function guessMimeType( $file, $useExt=true ) {
317 $fname = 'MimeMagic::guessMimeType';
318 $mime= $this->detectMimeType($file,$useExt);
319
320 if (strpos($mime,"text/")===0 ||
321 $mime==="application/xml") {
322
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 );
328
329 $xml_type= NULL;
330 $script_type= NULL;
331
332 /*
333 * look for XML formats (XHTML and SVG)
334 */
335 if ($mime==="text/sgml" ||
336 $mime==="text/plain" ||
337 $mime==="text/html" ||
338 $mime==="text/xml" ||
339 $mime==="application/xml") {
340
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";
345
346 if ($xml_type) {
347 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
348
349 $match= array();
350 $doctype= "";
351 $tag= "";
352
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];
355
356 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
357
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";
362
363 $test_more= false;
364 }
365 }
366
367 /*
368 * look for shell scripts
369 */
370 if (!$xml_type) {
371 $script_type= NULL;
372
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";
378
379 if ($script_type) {
380 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
381
382 $match= array();
383 $prog= "";
384
385 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) $script= $match[2];
386
387 $mime= "application/x-$prog";
388 }
389 }
390
391 /*
392 * look for PHP
393 */
394 if( !$xml_type && !$script_type ) {
395
396 if( ( strpos( $head, '<?php' ) !== false ) ||
397 ( strpos( $head, '<? ' ) !== false ) ||
398 ( strpos( $head, "<?\n" ) !== false ) ||
399 ( strpos( $head, "<?\t" ) !== false ) ||
400 ( strpos( $head, "<?=" ) !== false ) ||
401
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 ) ) {
407
408 $mime= "application/x-php";
409 }
410 }
411
412 }
413
414 if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
415
416 wfDebug("$fname: final mime type of $file: $mime\n");
417 return $mime;
418 }
419
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".
426 *
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.
429 *
430 * @return string the mime type of $file
431 * @private
432 */
433 function detectMimeType( $file, $useExt=true ) {
434 $fname = 'MimeMagic::detectMimeType';
435
436 global $wgMimeDetectorCommand;
437
438 $m= NULL;
439 if ($wgMimeDetectorCommand) {
440 $fn= wfEscapeShellArg($file);
441 $m= `$wgMimeDetectorCommand $fn`;
442 }
443 else if (function_exists("finfo_open") && function_exists("finfo_file")) {
444
445 # This required the fileinfo extension by PECL,
446 # see http://pecl.php.net/package/fileinfo
447 # This must be compiled into PHP
448 #
449 # finfo is the official replacement for the deprecated
450 # mime_content_type function, see below.
451 #
452 # If you may need to load the fileinfo extension at runtime, set
453 # $wgLoadFileinfoExtension in LocalSettings.php
454
455 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
456
457 if ($mime_magic_resource) {
458 $m= finfo_file($mime_magic_resource, $file);
459
460 finfo_close($mime_magic_resource);
461 }
462 else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
463 }
464 else if (function_exists("mime_content_type")) {
465
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.
468 #
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.
471 #
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.
474
475 $m= mime_content_type($file);
476 }
477 else wfDebug("$fname: no magic mime detector found!\n");
478
479 if ($m) {
480 #normalize
481 $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
482 $m= trim($m);
483 $m= strtolower($m);
484
485 if (strpos($m,'unknown')!==false) $m= NULL;
486 else {
487 wfDebug("$fname: magic mime type of $file: $m\n");
488 return $m;
489 }
490 }
491
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();
497
498 $notAnImage= false;
499
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;
518 }
519
520 if ($m) {
521 wfDebug("$fname: image mime type of $file: $m\n");
522 return $m;
523 }
524 else $notAnImage= true;
525 }
526
527 #if desired, look at extension as a fallback.
528 if ($useExt) {
529 $i = strrpos( $file, '.' );
530 $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
531
532 $m= $this->guessTypesForExtension($e);
533
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
537
538 if ($m) {
539 wfDebug("$fname: extension mime type of $file: $m\n");
540 return $m;
541 }
542 }
543
544 #unknown type
545 wfDebug("$fname: failed to guess mime type for $file!\n");
546 return "unknown/unknown";
547 }
548
549 /**
550 * Determine the media type code for a file, using its mime type, name and possibly
551 * its contents.
552 *
553 * This function relies on the findMediaType(), mapping extensions and mime
554 * types to media types.
555 *
556 * @todo analyse file if need be
557 * @todo look at multiple extension, separately and together.
558 *
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.
562 *
563 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
564 */
565 function getMediaType($path=NULL,$mime=NULL) {
566 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
567
568 #if mime type is unknown, guess it
569 if( !$mime ) $mime= $this->guessMimeType($path,false);
570
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) ) {
574
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 );
580
581 $head= strtolower( $head );
582
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;
589 }
590
591 #check for entry for full mime type
592 if( $mime ) {
593 $type= $this->findMediaType($mime);
594 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
595 }
596
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 ) : '' );
602
603 #TODO: look at multi-extension if this fails, parse from full path
604
605 $type= $this->findMediaType('.'.$e);
606 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
607 }
608
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;
616 }
617 }
618
619 if( !$type ) $type= MEDIATYPE_UNKNOWN;
620
621 return $type;
622 }
623
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.
627 *
628 * This funktion relies on the mapping defined by $this->mMediaTypes
629 * @private
630 */
631 function findMediaType($extMime) {
632
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;
636
637 $m= explode(' ',$m);
638 }
639 else { #normalize mime type
640 if (isset($this->mMimeTypeAliases[$extMime])) {
641 $extMime= $this->mMimeTypeAliases[$extMime];
642 }
643
644 $m= array($extMime);
645 }
646
647 foreach ($m as $mime) {
648 foreach ($this->mMediaTypes as $type => $codes) {
649 if (in_array($mime,$codes,true)) return $type;
650 }
651 }
652
653 return MEDIATYPE_UNKNOWN;
654 }
655 }
656
657 ?>