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