Better variable and method naming
[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, $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 $f = fopen( $file, "rt" );
378 if( !$f ) return "unknown/unknown";
379 $head = fread( $f, 1024 );
380 fclose( $f );
381
382 $sub4 = substr( $head, 0, 4 );
383 if ( $sub4 == "\x01\x00\x09\x00" || $sub4 == "\xd7\xcd\xc6\x9a" ) {
384 // WMF kill kill kill
385 // Note that WMF may have a bare header, no magic number.
386 // The former of the above two checks is theoretically prone to false positives
387 $mime = "application/x-msmetafile";
388 }
389
390 if ( strpos( $mime, "text/" ) === 0 || $mime === "application/xml" ) {
391
392 $xml_type = NULL;
393 $script_type = NULL;
394
395 /*
396 * look for XML formats (XHTML and SVG)
397 */
398 if ($mime === "text/sgml" ||
399 $mime === "text/plain" ||
400 $mime === "text/html" ||
401 $mime === "text/xml" ||
402 $mime === "application/xml") {
403
404 if ( substr( $head, 0, 5 ) == "<?xml" ) {
405 $xml_type = "ASCII";
406 } elseif ( substr( $head, 0, 8 ) == "\xef\xbb\xbf<?xml") {
407 $xml_type = "UTF-8";
408 } elseif ( substr( $head, 0, 10 ) == "\xfe\xff\x00<\x00?\x00x\x00m\x00l" ) {
409 $xml_type = "UTF-16BE";
410 } elseif ( substr( $head, 0, 10 ) == "\xff\xfe<\x00?\x00x\x00m\x00l\x00") {
411 $xml_type = "UTF-16LE";
412 }
413
414 if ( $xml_type ) {
415 if ( $xml_type !== "UTF-8" && $xml_type !== "ASCII" ) {
416 $head = iconv( $xml_type, "ASCII//IGNORE", $head );
417 }
418
419 $match = array();
420 $doctype = "";
421 $tag = "";
422
423 if ( preg_match( '%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',
424 $head, $match ) ) {
425 $doctype = $match[1];
426 }
427 if ( preg_match( '%<(\w+).*>%sim', $head, $match ) ) {
428 $tag = $match[1];
429 }
430
431 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
432
433 if ( strpos( $doctype, "-//W3C//DTD SVG" ) === 0 ) {
434 $mime = "image/svg+xml";
435 } elseif ( $tag === "svg" ) {
436 $mime = "image/svg+xml";
437 } elseif ( strpos( $doctype, "-//W3C//DTD XHTML" ) === 0 ) {
438 $mime = "text/html";
439 } elseif ( $tag === "html" ) {
440 $mime = "text/html";
441 }
442 }
443 }
444
445 /*
446 * look for shell scripts
447 */
448 if ( !$xml_type ) {
449 $script_type = NULL;
450
451 # detect by shebang
452 if ( substr( $head, 0, 2) == "#!" ) {
453 $script_type = "ASCII";
454 } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
455 $script_type = "UTF-8";
456 } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
457 $script_type = "UTF-16BE";
458 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
459 $script_type= "UTF-16LE";
460 }
461
462 if ( $script_type ) {
463 if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
464 $head = iconv( $script_type, "ASCII//IGNORE", $head);
465 }
466
467 $match = array();
468
469 if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
470 $mime = "application/x-{$match[2]}";
471 }
472 }
473 }
474
475 /*
476 * look for PHP
477 */
478 if( !$xml_type && !$script_type ) {
479
480 if( ( strpos( $head, '<?php' ) !== false ) ||
481 ( strpos( $head, '<? ' ) !== false ) ||
482 ( strpos( $head, "<?\n" ) !== false ) ||
483 ( strpos( $head, "<?\t" ) !== false ) ||
484 ( strpos( $head, "<?=" ) !== false ) ||
485
486 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
487 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
488 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
489 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
490 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
491
492 $mime = "application/x-php";
493 }
494 }
495
496 }
497
498 if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
499 $mime = $this->mMimeTypeAliases[$mime];
500 }
501
502 wfDebug(__METHOD__.": final mime type of $file: $mime\n");
503 return $mime;
504 }
505
506 /** Internal mime type detection, please use guessMimeType() for application code instead.
507 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
508 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
509 * If the dections fails and $ext is not false, the mime type is guessed from the file extension, using
510 * guessTypesForExtension.
511 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
512 * If no mime type can be determined, this function returns "unknown/unknown".
513 *
514 * @param string $file The file to check
515 * @param mixed $ext The file extension, or true to extract it from the filename.
516 * Set it to false to ignore the extension.
517 *
518 * @return string the mime type of $file
519 * @access private
520 */
521 function detectMimeType( $file, $ext = true ) {
522 global $wgMimeDetectorCommand;
523
524 $m = NULL;
525 if ( $wgMimeDetectorCommand ) {
526 $fn = wfEscapeShellArg( $file );
527 $m = `$wgMimeDetectorCommand $fn`;
528 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
529
530 # This required the fileinfo extension by PECL,
531 # see http://pecl.php.net/package/fileinfo
532 # This must be compiled into PHP
533 #
534 # finfo is the official replacement for the deprecated
535 # mime_content_type function, see below.
536 #
537 # If you may need to load the fileinfo extension at runtime, set
538 # $wgLoadFileinfoExtension in LocalSettings.php
539
540 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
541
542 if ($mime_magic_resource) {
543 $m = finfo_file( $mime_magic_resource, $file );
544 finfo_close( $mime_magic_resource );
545 } else {
546 wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
547 }
548 } elseif ( function_exists( "mime_content_type" ) ) {
549
550 # NOTE: this function is available since PHP 4.3.0, but only if
551 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
552 #
553 # On Windows, you must set mime_magic.magicfile in php.ini to point to the mime.magic file bundeled with PHP;
554 # sometimes, this may even be needed under linus/unix.
555 #
556 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
557 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
558
559 $m = mime_content_type($file);
560
561 if ( $m == 'text/plain' ) {
562 // mime_content_type sometimes considers DJVU files to be text/plain.
563 $deja = new DjVuImage( $file );
564 if( $deja->isValid() ) {
565 wfDebug( __METHOD__.": (re)detected $file as image/vnd.djvu\n" );
566 $m = 'image/vnd.djvu';
567 }
568 }
569 } else {
570 wfDebug( __METHOD__.": no magic mime detector found!\n" );
571 }
572
573 if ( $m ) {
574 # normalize
575 $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
576 $m = trim( $m );
577 $m = strtolower( $m );
578
579 if ( strpos( $m, 'unknown' ) !== false ) {
580 $m = NULL;
581 } else {
582 wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
583 return $m;
584 }
585 }
586
587 # if still not known, use getimagesize to find out the type of image
588 # TODO: skip things that do not have a well-known image extension? Would that be safe?
589 wfSuppressWarnings();
590 $gis = getimagesize( $file );
591 wfRestoreWarnings();
592
593 $notAnImage = false;
594
595 if ( $gis && is_array($gis) && $gis[2] ) {
596
597 switch ( $gis[2] ) {
598 case IMAGETYPE_GIF: $m = "image/gif"; break;
599 case IMAGETYPE_JPEG: $m = "image/jpeg"; break;
600 case IMAGETYPE_PNG: $m = "image/png"; break;
601 case IMAGETYPE_SWF: $m = "application/x-shockwave-flash"; break;
602 case IMAGETYPE_PSD: $m = "application/photoshop"; break;
603 case IMAGETYPE_BMP: $m = "image/bmp"; break;
604 case IMAGETYPE_TIFF_II: $m = "image/tiff"; break;
605 case IMAGETYPE_TIFF_MM: $m = "image/tiff"; break;
606 case IMAGETYPE_JPC: $m = "image"; break;
607 case IMAGETYPE_JP2: $m = "image/jpeg2000"; break;
608 case IMAGETYPE_JPX: $m = "image/jpeg2000"; break;
609 case IMAGETYPE_JB2: $m = "image"; break;
610 case IMAGETYPE_SWC: $m = "application/x-shockwave-flash"; break;
611 case IMAGETYPE_IFF: $m = "image/vnd.xiff"; break;
612 case IMAGETYPE_WBMP: $m = "image/vnd.wap.wbmp"; break;
613 case IMAGETYPE_XBM: $m = "image/x-xbitmap"; break;
614 }
615
616 if ( $m ) {
617 wfDebug( __METHOD__.": image mime type of $file: $m\n" );
618 return $m;
619 }
620 else {
621 $notAnImage = true;
622 }
623 } else {
624 // Also test DjVu
625 $deja = new DjVuImage( $file );
626 if( $deja->isValid() ) {
627 wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
628 return 'image/vnd.djvu';
629 }
630 }
631
632 # if desired, look at extension as a fallback.
633 if ( $ext === true ) {
634 $i = strrpos( $file, '.' );
635 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
636 }
637 if ( $ext ) {
638 $m = $this->guessTypesForExtension( $ext );
639
640 # TODO: if $notAnImage is set, do not trust the file extension if
641 # the results is one of the image types that should have been recognized
642 # by getimagesize
643
644 if ( $m ) {
645 wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
646 return $m;
647 }
648 }
649
650 #unknown type
651 wfDebug( __METHOD__.": failed to guess mime type for $file!\n" );
652 return "unknown/unknown";
653 }
654
655 /**
656 * Determine the media type code for a file, using its mime type, name and possibly
657 * its contents.
658 *
659 * This function relies on the findMediaType(), mapping extensions and mime
660 * types to media types.
661 *
662 * @todo analyse file if need be
663 * @todo look at multiple extension, separately and together.
664 *
665 * @param string $path full path to the image file, in case we have to look at the contents
666 * (if null, only the mime type is used to determine the media type code).
667 * @param string $mime mime type. If null it will be guessed using guessMimeType.
668 *
669 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
670 */
671 function getMediaType( $path = NULL, $mime = NULL ) {
672 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
673
674 # If mime type is unknown, guess it
675 if( !$mime ) $mime = $this->guessMimeType( $path, false );
676
677 # Special code for ogg - detect if it's video (theora),
678 # else label it as sound.
679 if( $mime == "application/ogg" && file_exists( $path ) ) {
680
681 // Read a chunk of the file
682 $f = fopen( $path, "rt" );
683 if ( !$f ) return MEDIATYPE_UNKNOWN;
684 $head = fread( $f, 256 );
685 fclose( $f );
686
687 $head = strtolower( $head );
688
689 # This is an UGLY HACK, file should be parsed correctly
690 if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
691 elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
692 elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
693 elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
694 else return MEDIATYPE_MULTIMEDIA;
695 }
696
697 # check for entry for full mime type
698 if( $mime ) {
699 $type = $this->findMediaType( $mime );
700 if( $type !== MEDIATYPE_UNKNOWN ) return $type;
701 }
702
703 # Check for entry for file extension
704 $e = NULL;
705 if ( $path ) {
706 $i = strrpos( $path, '.' );
707 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
708
709 # TODO: look at multi-extension if this fails, parse from full path
710
711 $type = $this->findMediaType( '.' . $e );
712 if ( $type !== MEDIATYPE_UNKNOWN ) return $type;
713 }
714
715 # Check major mime type
716 if( $mime ) {
717 $i = strpos( $mime, '/' );
718 if( $i !== false ) {
719 $major = substr( $mime, 0, $i );
720 $type = $this->findMediaType( $major );
721 if( $type !== MEDIATYPE_UNKNOWN ) return $type;
722 }
723 }
724
725 if( !$type ) $type = MEDIATYPE_UNKNOWN;
726
727 return $type;
728 }
729
730 /** returns a media code matching the given mime type or file extension.
731 * File extensions are represented by a string starting with a dot (.) to
732 * distinguish them from mime types.
733 *
734 * This funktion relies on the mapping defined by $this->mMediaTypes
735 * @access private
736 */
737 function findMediaType( $extMime ) {
738 if ( strpos( $extMime, '.' ) === 0 ) { #if it's an extension, look up the mime types
739 $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
740 if ( !$m ) return MEDIATYPE_UNKNOWN;
741
742 $m = explode( ' ', $m );
743 } else {
744 # Normalize mime type
745 if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
746 $extMime = $this->mMimeTypeAliases[$extMime];
747 }
748
749 $m = array($extMime);
750 }
751
752 foreach ( $m as $mime ) {
753 foreach ( $this->mMediaTypes as $type => $codes ) {
754 if ( in_array($mime, $codes, true ) ) {
755 return $type;
756 }
757 }
758 }
759
760 return MEDIATYPE_UNKNOWN;
761 }
762 }
763
764