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