Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / MimeMagic.php
1 <?php
2 /** Module defining helper functions for detecting and dealing with mime types.
3 *
4 */
5
6 /** Defines a set of well known mime types
7 * This is used as a fallback to mime.types files.
8 * An extensive list of well known mime types is provided by
9 * the file mime.types in the includes directory.
10 */
11 define('MM_WELL_KNOWN_MIME_TYPES',<<<END_STRING
12 application/ogg ogg ogm
13 application/pdf pdf
14 application/x-javascript js
15 application/x-shockwave-flash swf
16 audio/midi mid midi kar
17 audio/mpeg mpga mpa mp2 mp3
18 audio/x-aiff aif aiff aifc
19 audio/x-wav wav
20 audio/ogg ogg
21 image/x-bmp bmp
22 image/gif gif
23 image/jpeg jpeg jpg jpe
24 image/png png
25 image/svg+xml svg
26 image/tiff tiff tif
27 image/vnd.djvu djvu
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 image/vnd.djvu [BITMAP]
56 text/plain [TEXT]
57 text/html [TEXT]
58 video/ogg [VIDEO]
59 video/mpeg [VIDEO]
60 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
61 END_STRING
62 );
63
64 #note: because this file is possibly included by a function,
65 #we need to access the global scope explicitely!
66 global $wgLoadFileinfoExtension;
67
68 if ($wgLoadFileinfoExtension) {
69 if(!extension_loaded('fileinfo')) dl('fileinfo.' . PHP_SHLIB_SUFFIX);
70 }
71
72 /** Implements functions related to mime types such as detection and mapping to
73 * file extension,
74 *
75 * Instances of this class are stateles, there only needs to be one global instance
76 * of MimeMagic. Please use MimeMagic::singleton() to get that instance.
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 /** The singleton instance
99 */
100 private static $instance;
101
102 /** Initializes the MimeMagic object. This is called by MimeMagic::singleton().
103 *
104 * This constructor parses the mime.types and mime.info files and build internal mappings.
105 */
106 function __construct() {
107 /*
108 * --- load mime.types ---
109 */
110
111 global $wgMimeTypeFile;
112
113 $types= MM_WELL_KNOWN_MIME_TYPES;
114
115 if ($wgMimeTypeFile) {
116 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
117 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
118
119 $types.= "\n";
120 $types.= file_get_contents($wgMimeTypeFile);
121 }
122 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
123 }
124 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
125
126 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
127 $types= str_replace("\t"," ",$types);
128
129 $this->mMimeToExt= array();
130 $this->mToMime= array();
131
132 $lines= explode("\n",$types);
133 foreach ($lines as $s) {
134 $s= trim($s);
135 if (empty($s)) continue;
136 if (strpos($s,'#')===0) continue;
137
138 $s= strtolower($s);
139 $i= strpos($s,' ');
140
141 if ($i===false) continue;
142
143 #print "processing MIME line $s<br>";
144
145 $mime= substr($s,0,$i);
146 $ext= trim(substr($s,$i+1));
147
148 if (empty($ext)) continue;
149
150 if ( !empty($this->mMimeToExt[$mime])) $this->mMimeToExt[$mime] .= ' '.$ext;
151 else $this->mMimeToExt[$mime]= $ext;
152
153 $extensions= explode(' ',$ext);
154
155 foreach ($extensions as $e) {
156 $e= trim($e);
157 if (empty($e)) continue;
158
159 if ( !empty($this->mExtToMime[$e])) $this->mExtToMime[$e] .= ' '.$mime;
160 else $this->mExtToMime[$e]= $mime;
161 }
162 }
163
164 /*
165 * --- load mime.info ---
166 */
167
168 global $wgMimeInfoFile;
169
170 $info= MM_WELL_KNOWN_MIME_INFO;
171
172 if ($wgMimeInfoFile) {
173 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
174 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
175
176 $info.= "\n";
177 $info.= file_get_contents($wgMimeInfoFile);
178 }
179 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
180 }
181 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
182
183 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
184 $info= str_replace("\t"," ",$info);
185
186 $this->mMimeTypeAliases= array();
187 $this->mMediaTypes= array();
188
189 $lines= explode("\n",$info);
190 foreach ($lines as $s) {
191 $s= trim($s);
192 if (empty($s)) continue;
193 if (strpos($s,'#')===0) continue;
194
195 $s= strtolower($s);
196 $i= strpos($s,' ');
197
198 if ($i===false) continue;
199
200 #print "processing MIME INFO line $s<br>";
201
202 $match= array();
203 if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
204 $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
205 $mtype= trim(strtoupper($match[1]));
206 }
207 else $mtype= MEDIATYPE_UNKNOWN;
208
209 $m= explode(' ',$s);
210
211 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
212
213 foreach ($m as $mime) {
214 $mime= trim($mime);
215 if (empty($mime)) continue;
216
217 $this->mMediaTypes[$mtype][]= $mime;
218 }
219
220 if (sizeof($m)>1) {
221 $main= $m[0];
222 for ($i=1; $i<sizeof($m); $i+= 1) {
223 $mime= $m[$i];
224 $this->mMimeTypeAliases[$mime]= $main;
225 }
226 }
227 }
228
229 }
230
231 /**
232 * Get an instance of this class
233 */
234 static function &singleton() {
235 if ( !isset( self::$instance ) ) {
236 self::$instance = new MimeMagic;
237 }
238 return self::$instance;
239 }
240
241 /** returns a list of file extensions for a given mime type
242 * as a space separated string.
243 */
244 function getExtensionsForType($mime) {
245 $mime= strtolower($mime);
246
247 $r= @$this->mMimeToExt[$mime];
248
249 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
250 $mime= $this->mMimeTypeAliases[$mime];
251 $r= @$this->mMimeToExt[$mime];
252 }
253
254 return $r;
255 }
256
257 /** returns a list of mime types for a given file extension
258 * as a space separated string.
259 */
260 function getTypesForExtension($ext) {
261 $ext= strtolower($ext);
262
263 $r= @$this->mExtToMime[$ext];
264 return $r;
265 }
266
267 /** returns a single mime type for a given file extension.
268 * This is always the first type from the list returned by getTypesForExtension($ext).
269 */
270 function guessTypesForExtension($ext) {
271 $m= $this->getTypesForExtension( $ext );
272 if( is_null($m) ) return NULL;
273
274 $m= trim( $m );
275 $m= preg_replace('/\s.*$/','',$m);
276
277 return $m;
278 }
279
280
281 /** tests if the extension matches the given mime type.
282 * returns true if a match was found, NULL if the mime type is unknown,
283 * and false if the mime type is known but no matches where found.
284 */
285 function isMatchingExtension($extension,$mime) {
286 $ext= $this->getExtensionsForType($mime);
287
288 if (!$ext) {
289 return NULL; //unknown
290 }
291
292 $ext= explode(' ',$ext);
293
294 $extension= strtolower($extension);
295 if (in_array($extension,$ext)) {
296 return true;
297 }
298
299 return false;
300 }
301
302 /** returns true if the mime type is known to represent
303 * an image format supported by the PHP GD library.
304 */
305 function isPHPImageType( $mime ) {
306 #as defined by imagegetsize and image_type_to_mime
307 static $types = array(
308 'image/gif', 'image/jpeg', 'image/png',
309 'image/x-bmp', 'image/xbm', 'image/tiff',
310 'image/jp2', 'image/jpeg2000', 'image/iff',
311 'image/xbm', 'image/x-xbitmap',
312 'image/vnd.wap.wbmp', 'image/vnd.xiff',
313 'image/x-photoshop',
314 'application/x-shockwave-flash',
315 );
316
317 return in_array( $mime, $types );
318 }
319
320 /**
321 * Returns true if the extension represents a type which can
322 * be reliably detected from its content. Use this to determine
323 * whether strict content checks should be applied to reject
324 * invalid uploads; if we can't identify the type we won't
325 * be able to say if it's invalid.
326 *
327 * @todo Be more accurate when using fancy mime detector plugins;
328 * right now this is the bare minimum getimagesize() list.
329 * @return bool
330 */
331 function isRecognizableExtension( $extension ) {
332 static $types = array(
333 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
334 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
335 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
336 'xbm', 'djvu'
337 );
338 return in_array( strtolower( $extension ), $types );
339 }
340
341
342 /** mime type detection. This uses detectMimeType to detect the mim type of the file,
343 * but applies additional checks to determine some well known file formats that may be missed
344 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
345 *
346 * @param string $file The file to check
347 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
348 *
349 * @return string the mime type of $file
350 */
351 function guessMimeType( $file, $useExt=true ) {
352 $fname = 'MimeMagic::guessMimeType';
353 $mime= $this->detectMimeType($file,$useExt);
354
355 // Read a chunk of the file
356 $f = fopen( $file, "rt" );
357 if( !$f ) return "unknown/unknown";
358 $head = fread( $f, 1024 );
359 fclose( $f );
360
361 $sub4 = substr( $head, 0, 4 );
362 if ( $sub4 == "\x01\x00\x09\x00" || $sub4 == "\xd7\xcd\xc6\x9a" ) {
363 // WMF kill kill kill
364 // Note that WMF may have a bare header, no magic number.
365 // The former of the above two checks is theoretically prone to false positives
366 $mime = "application/x-msmetafile";
367 }
368
369 if (strpos($mime,"text/")===0 || $mime==="application/xml") {
370
371 $xml_type= NULL;
372 $script_type= NULL;
373
374 /*
375 * look for XML formats (XHTML and SVG)
376 */
377 if ($mime==="text/sgml" ||
378 $mime==="text/plain" ||
379 $mime==="text/html" ||
380 $mime==="text/xml" ||
381 $mime==="application/xml") {
382
383 if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
384 elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
385 elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
386 elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
387
388 if ($xml_type) {
389 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
390
391 $match= array();
392 $doctype= "";
393 $tag= "";
394
395 if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
396 if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
397
398 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
399
400 if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg";
401 elseif ($tag==="svg") $mime= "image/svg";
402 elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
403 elseif ($tag==="html") $mime= "text/html";
404 }
405 }
406
407 /*
408 * look for shell scripts
409 */
410 if (!$xml_type) {
411 $script_type= NULL;
412
413 #detect by shebang
414 if (substr($head,0,2)=="#!") $script_type= "ASCII";
415 elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
416 elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
417 elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
418
419 if ($script_type) {
420 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
421
422 $match= array();
423 $prog= "";
424
425 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) {
426 $script= $match[2]; // FIXME: $script variable not used; should this be "$prog = $match[2];" instead?
427 }
428
429 $mime= "application/x-$prog";
430 }
431 }
432
433 /*
434 * look for PHP
435 */
436 if( !$xml_type && !$script_type ) {
437
438 if( ( strpos( $head, '<?php' ) !== false ) ||
439 ( strpos( $head, '<? ' ) !== false ) ||
440 ( strpos( $head, "<?\n" ) !== false ) ||
441 ( strpos( $head, "<?\t" ) !== false ) ||
442 ( strpos( $head, "<?=" ) !== false ) ||
443
444 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
445 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
446 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
447 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
448 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
449
450 $mime= "application/x-php";
451 }
452 }
453
454 }
455
456 if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
457
458 wfDebug("$fname: final mime type of $file: $mime\n");
459 return $mime;
460 }
461
462 /** Internal mime type detection, please use guessMimeType() for application code instead.
463 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
464 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
465 * If the dections fails and $useExt is true, the mime type is guessed from the file extension, using guessTypesForExtension.
466 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
467 * If no mime type can be determined, this function returns "unknown/unknown".
468 *
469 * @param string $file The file to check
470 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
471 *
472 * @return string the mime type of $file
473 * @access private
474 */
475 function detectMimeType( $file, $useExt=true ) {
476 $fname = 'MimeMagic::detectMimeType';
477
478 global $wgMimeDetectorCommand;
479
480 $m= NULL;
481 if ($wgMimeDetectorCommand) {
482 $fn= wfEscapeShellArg($file);
483 $m= `$wgMimeDetectorCommand $fn`;
484 }
485 else if (function_exists("finfo_open") && function_exists("finfo_file")) {
486
487 # This required the fileinfo extension by PECL,
488 # see http://pecl.php.net/package/fileinfo
489 # This must be compiled into PHP
490 #
491 # finfo is the official replacement for the deprecated
492 # mime_content_type function, see below.
493 #
494 # If you may need to load the fileinfo extension at runtime, set
495 # $wgLoadFileinfoExtension in LocalSettings.php
496
497 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
498
499 if ($mime_magic_resource) {
500 $m= finfo_file($mime_magic_resource, $file);
501
502 finfo_close($mime_magic_resource);
503 }
504 else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
505 }
506 else if (function_exists("mime_content_type")) {
507
508 # NOTE: this function is available since PHP 4.3.0, but only if
509 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
510 #
511 # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
512 # sometimes, this may even be needed under linus/unix.
513 #
514 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
515 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
516
517 $m= mime_content_type($file);
518
519 if ( $m == 'text/plain' ) {
520 // mime_content_type sometimes considers DJVU files to be text/plain.
521 $deja = new DjVuImage( $file );
522 if( $deja->isValid() ) {
523 wfDebug("$fname: (re)detected $file as image/vnd.djvu\n");
524 $m = 'image/vnd.djvu';
525 }
526 }
527 }
528 else wfDebug("$fname: no magic mime detector found!\n");
529
530 if ($m) {
531 #normalize
532 $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
533 $m= trim($m);
534 $m= strtolower($m);
535
536 if (strpos($m,'unknown')!==false) $m= NULL;
537 else {
538 wfDebug("$fname: magic mime type of $file: $m\n");
539 return $m;
540 }
541 }
542
543 #if still not known, use getimagesize to find out the type of image
544 #TODO: skip things that do not have a well-known image extension? Would that be safe?
545 wfSuppressWarnings();
546 $gis = getimagesize( $file );
547 wfRestoreWarnings();
548
549 $notAnImage= false;
550
551 if ($gis && is_array($gis) && $gis[2]) {
552 switch ($gis[2]) {
553 case IMAGETYPE_GIF: $m= "image/gif"; break;
554 case IMAGETYPE_JPEG: $m= "image/jpeg"; break;
555 case IMAGETYPE_PNG: $m= "image/png"; break;
556 case IMAGETYPE_SWF: $m= "application/x-shockwave-flash"; break;
557 case IMAGETYPE_PSD: $m= "application/photoshop"; break;
558 case IMAGETYPE_BMP: $m= "image/bmp"; break;
559 case IMAGETYPE_TIFF_II: $m= "image/tiff"; break;
560 case IMAGETYPE_TIFF_MM: $m= "image/tiff"; break;
561 case IMAGETYPE_JPC: $m= "image"; break;
562 case IMAGETYPE_JP2: $m= "image/jpeg2000"; break;
563 case IMAGETYPE_JPX: $m= "image/jpeg2000"; break;
564 case IMAGETYPE_JB2: $m= "image"; break;
565 case IMAGETYPE_SWC: $m= "application/x-shockwave-flash"; break;
566 case IMAGETYPE_IFF: $m= "image/vnd.xiff"; break;
567 case IMAGETYPE_WBMP: $m= "image/vnd.wap.wbmp"; break;
568 case IMAGETYPE_XBM: $m= "image/x-xbitmap"; break;
569 }
570
571 if ($m) {
572 wfDebug("$fname: image mime type of $file: $m\n");
573 return $m;
574 }
575 else $notAnImage= true;
576 } else {
577 // Also test DjVu
578 $deja = new DjVuImage( $file );
579 if( $deja->isValid() ) {
580 wfDebug("$fname: detected $file as image/vnd.djvu\n");
581 return 'image/vnd.djvu';
582 }
583 }
584
585 #if desired, look at extension as a fallback.
586 if ($useExt) {
587 $i = strrpos( $file, '.' );
588 $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
589
590 $m= $this->guessTypesForExtension($e);
591
592 #TODO: if $notAnImage is set, do not trust the file extension if
593 # the results is one of the image types that should have been recognized
594 # by getimagesize
595
596 if ($m) {
597 wfDebug("$fname: extension mime type of $file: $m\n");
598 return $m;
599 }
600 }
601
602 #unknown type
603 wfDebug("$fname: failed to guess mime type for $file!\n");
604 return "unknown/unknown";
605 }
606
607 /**
608 * Determine the media type code for a file, using its mime type, name and possibly
609 * its contents.
610 *
611 * This function relies on the findMediaType(), mapping extensions and mime
612 * types to media types.
613 *
614 * @todo analyse file if need be
615 * @todo look at multiple extension, separately and together.
616 *
617 * @param string $path full path to the image file, in case we have to look at the contents
618 * (if null, only the mime type is used to determine the media type code).
619 * @param string $mime mime type. If null it will be guessed using guessMimeType.
620 *
621 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
622 */
623 function getMediaType($path=NULL,$mime=NULL) {
624 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
625
626 #if mime type is unknown, guess it
627 if( !$mime ) $mime= $this->guessMimeType($path,false);
628
629 #special code for ogg - detect if it's video (theora),
630 #else label it as sound.
631 if( $mime=="application/ogg" && file_exists($path) ) {
632
633 // Read a chunk of the file
634 $f = fopen( $path, "rt" );
635 if( !$f ) return MEDIATYPE_UNKNOWN;
636 $head = fread( $f, 256 );
637 fclose( $f );
638
639 $head= strtolower( $head );
640
641 #This is an UGLY HACK, file should be parsed correctly
642 if( strpos($head,'theora')!==false ) return MEDIATYPE_VIDEO;
643 elseif( strpos($head,'vorbis')!==false ) return MEDIATYPE_AUDIO;
644 elseif( strpos($head,'flac')!==false ) return MEDIATYPE_AUDIO;
645 elseif( strpos($head,'speex')!==false ) return MEDIATYPE_AUDIO;
646 else return MEDIATYPE_MULTIMEDIA;
647 }
648
649 #check for entry for full mime type
650 if( $mime ) {
651 $type= $this->findMediaType($mime);
652 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
653 }
654
655 #check for entry for file extension
656 $e= NULL;
657 if( $path ) {
658 $i = strrpos( $path, '.' );
659 $e= strtolower( $i ? substr( $path, $i + 1 ) : '' );
660
661 #TODO: look at multi-extension if this fails, parse from full path
662
663 $type= $this->findMediaType('.'.$e);
664 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
665 }
666
667 #check major mime type
668 if( $mime ) {
669 $i= strpos($mime,'/');
670 if( $i !== false ) {
671 $major= substr($mime,0,$i);
672 $type= $this->findMediaType($major);
673 if( $type!==MEDIATYPE_UNKNOWN ) return $type;
674 }
675 }
676
677 if( !$type ) $type= MEDIATYPE_UNKNOWN;
678
679 return $type;
680 }
681
682 /** returns a media code matching the given mime type or file extension.
683 * File extensions are represented by a string starting with a dot (.) to
684 * distinguish them from mime types.
685 *
686 * This funktion relies on the mapping defined by $this->mMediaTypes
687 * @access private
688 */
689 function findMediaType($extMime) {
690
691 if (strpos($extMime,'.')===0) { #if it's an extension, look up the mime types
692 $m= $this->getTypesForExtension(substr($extMime,1));
693 if (!$m) return MEDIATYPE_UNKNOWN;
694
695 $m= explode(' ',$m);
696 }
697 else { #normalize mime type
698 if (isset($this->mMimeTypeAliases[$extMime])) {
699 $extMime= $this->mMimeTypeAliases[$extMime];
700 }
701
702 $m= array($extMime);
703 }
704
705 foreach ($m as $mime) {
706 foreach ($this->mMediaTypes as $type => $codes) {
707 if (in_array($mime,$codes,true)) return $type;
708 }
709 }
710
711 return MEDIATYPE_UNKNOWN;
712 }
713 }
714
715 ?>