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