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