Prevent E_STRICT errors on file upload of the following type:
[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 MimeMagic::singleton() 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 /** The singleton instance
101 */
102 private static $instance;
103
104 /** Initializes the MimeMagic object. This is called by MimeMagic::singleton().
105 *
106 * This constructor parses the mime.types and mime.info files and build internal mappings.
107 */
108 function MimeMagic() {
109 /*
110 * --- load mime.types ---
111 */
112
113 global $wgMimeTypeFile;
114
115 $types= MM_WELL_KNOWN_MIME_TYPES;
116
117 if ($wgMimeTypeFile) {
118 if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
119 wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
120
121 $types.= "\n";
122 $types.= file_get_contents($wgMimeTypeFile);
123 }
124 else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
125 }
126 else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
127
128 $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
129 $types= str_replace("\t"," ",$types);
130
131 $this->mMimeToExt= array();
132 $this->mToMime= array();
133
134 $lines= explode("\n",$types);
135 foreach ($lines as $s) {
136 $s= trim($s);
137 if (empty($s)) continue;
138 if (strpos($s,'#')===0) continue;
139
140 $s= strtolower($s);
141 $i= strpos($s,' ');
142
143 if ($i===false) continue;
144
145 #print "processing MIME line $s<br>";
146
147 $mime= substr($s,0,$i);
148 $ext= trim(substr($s,$i+1));
149
150 if (empty($ext)) continue;
151
152 if ( !empty($this->mMimeToExt[$mime])) $this->mMimeToExt[$mime] .= ' '.$ext;
153 else $this->mMimeToExt[$mime]= $ext;
154
155 $extensions= explode(' ',$ext);
156
157 foreach ($extensions as $e) {
158 $e= trim($e);
159 if (empty($e)) continue;
160
161 if ( !empty($this->mExtToMime[$e])) $this->mExtToMime[$e] .= ' '.$mime;
162 else $this->mExtToMime[$e]= $mime;
163 }
164 }
165
166 /*
167 * --- load mime.info ---
168 */
169
170 global $wgMimeInfoFile;
171
172 $info= MM_WELL_KNOWN_MIME_INFO;
173
174 if ($wgMimeInfoFile) {
175 if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
176 wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
177
178 $info.= "\n";
179 $info.= file_get_contents($wgMimeInfoFile);
180 }
181 else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
182 }
183 else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
184
185 $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
186 $info= str_replace("\t"," ",$info);
187
188 $this->mMimeTypeAliases= array();
189 $this->mMediaTypes= array();
190
191 $lines= explode("\n",$info);
192 foreach ($lines as $s) {
193 $s= trim($s);
194 if (empty($s)) continue;
195 if (strpos($s,'#')===0) continue;
196
197 $s= strtolower($s);
198 $i= strpos($s,' ');
199
200 if ($i===false) continue;
201
202 #print "processing MIME INFO line $s<br>";
203
204 $match= array();
205 if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
206 $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
207 $mtype= trim(strtoupper($match[1]));
208 }
209 else $mtype= MEDIATYPE_UNKNOWN;
210
211 $m= explode(' ',$s);
212
213 if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
214
215 foreach ($m as $mime) {
216 $mime= trim($mime);
217 if (empty($mime)) continue;
218
219 $this->mMediaTypes[$mtype][]= $mime;
220 }
221
222 if (sizeof($m)>1) {
223 $main= $m[0];
224 for ($i=1; $i<sizeof($m); $i+= 1) {
225 $mime= $m[$i];
226 $this->mMimeTypeAliases[$mime]= $main;
227 }
228 }
229 }
230
231 }
232
233 /**
234 * Get an instance of this class
235 */
236 static function &singleton() {
237 if ( !isset( self::$instance ) ) {
238 self::$instance = new MimeMagic;
239 }
240 return self::$instance;
241 }
242
243 /** returns a list of file extensions for a given mime type
244 * as a space separated string.
245 */
246 function getExtensionsForType($mime) {
247 $mime= strtolower($mime);
248
249 $r= @$this->mMimeToExt[$mime];
250
251 if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
252 $mime= $this->mMimeTypeAliases[$mime];
253 $r= @$this->mMimeToExt[$mime];
254 }
255
256 return $r;
257 }
258
259 /** returns a list of mime types for a given file extension
260 * as a space separated string.
261 */
262 function getTypesForExtension($ext) {
263 $ext= strtolower($ext);
264
265 $r= @$this->mExtToMime[$ext];
266 return $r;
267 }
268
269 /** returns a single mime type for a given file extension.
270 * This is always the first type from the list returned by getTypesForExtension($ext).
271 */
272 function guessTypesForExtension($ext) {
273 $m= $this->getTypesForExtension( $ext );
274 if( is_null($m) ) return NULL;
275
276 $m= trim( $m );
277 $m= preg_replace('/\s.*$/','',$m);
278
279 return $m;
280 }
281
282
283 /** tests if the extension matches the given mime type.
284 * returns true if a match was found, NULL if the mime type is unknown,
285 * and false if the mime type is known but no matches where found.
286 */
287 function isMatchingExtension($extension,$mime) {
288 $ext= $this->getExtensionsForType($mime);
289
290 if (!$ext) {
291 return NULL; //unknown
292 }
293
294 $ext= explode(' ',$ext);
295
296 $extension= strtolower($extension);
297 if (in_array($extension,$ext)) {
298 return true;
299 }
300
301 return false;
302 }
303
304 /** returns true if the mime type is known to represent
305 * an image format supported by the PHP GD library.
306 */
307 function isPHPImageType( $mime ) {
308 #as defined by imagegetsize and image_type_to_mime
309 static $types = array(
310 'image/gif', 'image/jpeg', 'image/png',
311 'image/x-bmp', 'image/xbm', 'image/tiff',
312 'image/jp2', 'image/jpeg2000', 'image/iff',
313 'image/xbm', 'image/x-xbitmap',
314 'image/vnd.wap.wbmp', 'image/vnd.xiff',
315 'image/x-photoshop',
316 'application/x-shockwave-flash',
317 );
318
319 return in_array( $mime, $types );
320 }
321
322 /**
323 * Returns true if the extension represents a type which can
324 * be reliably detected from its content. Use this to determine
325 * whether strict content checks should be applied to reject
326 * invalid uploads; if we can't identify the type we won't
327 * be able to say if it's invalid.
328 *
329 * @todo Be more accurate when using fancy mime detector plugins;
330 * right now this is the bare minimum getimagesize() list.
331 * @return bool
332 */
333 function isRecognizableExtension( $extension ) {
334 static $types = array(
335 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
336 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
337 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
338 'xbm', 'djvu'
339 );
340 return in_array( strtolower( $extension ), $types );
341 }
342
343
344 /** mime type detection. This uses detectMimeType to detect the mim type of the file,
345 * but applies additional checks to determine some well known file formats that may be missed
346 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
347 *
348 * @param string $file The file to check
349 * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
350 *
351 * @return string the mime type of $file
352 */
353 function guessMimeType( $file, $useExt=true ) {
354 $fname = 'MimeMagic::guessMimeType';
355 $mime= $this->detectMimeType($file,$useExt);
356
357 // Read a chunk of the file
358 $f = fopen( $file, "rt" );
359 if( !$f ) return "unknown/unknown";
360 $head = fread( $f, 1024 );
361 fclose( $f );
362
363 $sub4 = substr( $head, 0, 4 );
364 if ( $sub4 == "\x01\x00\x09\x00" || $sub4 == "\xd7\xcd\xc6\x9a" ) {
365 // WMF kill kill kill
366 // Note that WMF may have a bare header, no magic number.
367 // The former of the above two checks is theoretically prone to false positives
368 $mime = "application/x-msmetafile";
369 }
370
371 if (strpos($mime,"text/")===0 || $mime==="application/xml") {
372
373 $xml_type= NULL;
374 $script_type= NULL;
375
376 /*
377 * look for XML formats (XHTML and SVG)
378 */
379 if ($mime==="text/sgml" ||
380 $mime==="text/plain" ||
381 $mime==="text/html" ||
382 $mime==="text/xml" ||
383 $mime==="application/xml") {
384
385 if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
386 elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
387 elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
388 elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
389
390 if ($xml_type) {
391 if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
392
393 $match= array();
394 $doctype= "";
395 $tag= "";
396
397 if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
398 if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
399
400 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
401
402 if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg";
403 elseif ($tag==="svg") $mime= "image/svg";
404 elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
405 elseif ($tag==="html") $mime= "text/html";
406 }
407 }
408
409 /*
410 * look for shell scripts
411 */
412 if (!$xml_type) {
413 $script_type= NULL;
414
415 #detect by shebang
416 if (substr($head,0,2)=="#!") $script_type= "ASCII";
417 elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
418 elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
419 elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
420
421 if ($script_type) {
422 if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
423
424 $match= array();
425 $prog= "";
426
427 if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) $script= $match[2];
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 ?>