r81613 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81612‎ | r81613 | r81614 >
Date:22:47, 6 February 2011
Author:btongminh
Status:ok
Tags:
Comment:
Cleanup MimeMagic: Add public identifier to functions; kill error operator; Cleanup function documentation; Manual stylize
Modified paths:
  • /trunk/phase3/includes/MimeMagic.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/MimeMagic.php
@@ -117,11 +117,11 @@
118118 END_STRING
119119 );
120120
121 -#note: because this file is possibly included by a function,
122 -#we need to access the global scope explicitely!
 121+// Note: because this file is possibly included by a function,
 122+// we need to access the global scope explicitely!
123123 global $wgLoadFileinfoExtension;
124124
125 -if ($wgLoadFileinfoExtension) {
 125+if ( $wgLoadFileinfoExtension ) {
126126 wfDl( 'fileinfo' );
127127 }
128128
@@ -138,19 +138,19 @@
139139 * Mapping of media types to arrays of mime types.
140140 * This is used by findMediaType and getMediaType, respectively
141141 */
142 - var $mMediaTypes= null;
 142+ var $mMediaTypes = null;
143143
144144 /** Map of mime type aliases
145145 */
146 - var $mMimeTypeAliases= null;
 146+ var $mMimeTypeAliases = null;
147147
148148 /** map of mime types to file extensions (as a space seprarated list)
149149 */
150 - var $mMimeToExt= null;
 150+ var $mMimeToExt = null;
151151
152152 /** map of file extensions types to mime types (as a space seprarated list)
153153 */
154 - var $mExtToMime= null;
 154+ var $mExtToMime = null;
155155
156156 /** IEContentAnalyzer instance
157157 */
@@ -311,46 +311,68 @@
312312 * Get an instance of this class
313313 * @return MimeMagic
314314 */
315 - static function &singleton() {
 315+ public static function &singleton() {
316316 if ( !isset( self::$instance ) ) {
317317 self::$instance = new MimeMagic;
318318 }
319319 return self::$instance;
320320 }
321321
322 - /** returns a list of file extensions for a given mime type
323 - * as a space separated string.
324 - */
325 - function getExtensionsForType( $mime ) {
 322+ /**
 323+ * Returns a list of file extensions for a given mime type as a space
 324+ * separated string or null if the mime type was unrecognized. Resolves
 325+ * mime type aliases.
 326+ *
 327+ * @param $mime string
 328+ * @return string|null
 329+ */
 330+ public function getExtensionsForType( $mime ) {
326331 $mime = strtolower( $mime );
327332
328 - $r = @$this->mMimeToExt[$mime];
 333+ // Check the mime-to-ext map
 334+ if ( isset( $this->mMimeToExt[$mime] ) ) {
 335+ return $this->mMimeToExt[$mime];
 336+ }
329337
330 - if ( @!$r && isset( $this->mMimeTypeAliases[$mime] ) ) {
 338+ // Resolve the mime type to the canonical type
 339+ if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
331340 $mime = $this->mMimeTypeAliases[$mime];
332 - $r = @$this->mMimeToExt[$mime];
 341+ if ( isset( $this->mMimeToExt[$mime] ) ) {
 342+ return $this->mMimeToExt[$mime];
 343+ }
333344 }
334345
335 - return $r;
 346+ return null;
336347 }
337348
338 - /** returns a list of mime types for a given file extension
339 - * as a space separated string.
340 - */
341 - function getTypesForExtension( $ext ) {
 349+ /**
 350+ * Returns a list of mime types for a given file extension as a space
 351+ * separated string or null if the extension was unrecognized.
 352+ *
 353+ * @param $ext string
 354+ * @return string|null
 355+ */
 356+ public function getTypesForExtension( $ext ) {
342357 $ext = strtolower( $ext );
343358
344359 $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
345360 return $r;
346361 }
347362
348 - /** returns a single mime type for a given file extension.
349 - * This is always the first type from the list returned by getTypesForExtension($ext).
350 - */
351 - function guessTypesForExtension( $ext ) {
 363+ /**
 364+ * Returns a single mime type for a given file extension or null if unknown.
 365+ * This is always the first type from the list returned by getTypesForExtension($ext).
 366+ *
 367+ * @param $ext string
 368+ * @return string|null
 369+ */
 370+ public function guessTypesForExtension( $ext ) {
352371 $m = $this->getTypesForExtension( $ext );
353 - if ( is_null( $m ) ) return null;
 372+ if ( is_null( $m ) ) {
 373+ return null;
 374+ }
354375
 376+ // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient
355377 $m = trim( $m );
356378 $m = preg_replace( '/\s.*$/', '', $m );
357379
@@ -358,32 +380,34 @@
359381 }
360382
361383
362 - /** tests if the extension matches the given mime type.
363 - * returns true if a match was found, NULL if the mime type is unknown,
364 - * and false if the mime type is known but no matches where found.
365 - */
366 - function isMatchingExtension( $extension, $mime ) {
 384+ /**
 385+ * Tests if the extension matches the given mime type. Returns true if a
 386+ * match was found, null if the mime type is unknown, and false if the
 387+ * mime type is known but no matches where found.
 388+ *
 389+ * @param $extension string
 390+ * @param $mime string
 391+ * @return bool|null
 392+ */
 393+ public function isMatchingExtension( $extension, $mime ) {
367394 $ext = $this->getExtensionsForType( $mime );
368395
369396 if ( !$ext ) {
370 - return null; //unknown
 397+ return null; // Unknown mime type
371398 }
372399
373400 $ext = explode( ' ', $ext );
374401
375402 $extension = strtolower( $extension );
376 - if ( in_array( $extension, $ext ) ) {
377 - return true;
378 - }
379 -
380 - return false;
 403+ return in_array( $extension, $ext );
381404 }
382405
383 - /** returns true if the mime type is known to represent
384 - * an image format supported by the PHP GD library.
385 - */
386 - function isPHPImageType( $mime ) {
387 - #as defined by imagegetsize and image_type_to_mime
 406+ /**
 407+ * Returns true if the mime type is known to represent an image format
 408+ * supported by the PHP GD library.
 409+ */
 410+ public function isPHPImageType( $mime ) {
 411+ // As defined by imagegetsize and image_type_to_mime
388412 static $types = array(
389413 'image/gif', 'image/jpeg', 'image/png',
390414 'image/x-bmp', 'image/xbm', 'image/tiff',
@@ -427,44 +451,45 @@
428452 return in_array( strtolower( $extension ), $types );
429453 }
430454
431 - /** improves a mime type using the file extension. Some file formats are very generic,
432 - * so their mime type is not very meaningful. A more useful mime type can be derived
433 - * by looking at the file extension. Typically, this method would be called on the
434 - * result of guessMimeType().
435 - *
436 - * Currently, this method does the following:
437 - *
438 - * If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false,
439 - * return the result of guessTypesForExtension($ext).
440 - *
441 - * If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime )
442 - * gives true, return the result of guessTypesForExtension($ext).
443 - *
444 - * @param $mime String: the mime type, typically guessed from a file's content.
445 - * @param $ext String: the file extension, as taken from the file name
446 - *
447 - * @return string the mime type
448 - */
449 - function improveTypeFromExtension( $mime, $ext ) {
450 - if ( $mime === "unknown/unknown" ) {
451 - if( $this->isRecognizableExtension( $ext ) ) {
452 - wfDebug( __METHOD__. ": refusing to guess mime type for .$ext file, " .
453 - "we should have recognized it\n" );
 455+ /**
 456+ * Improves a mime type using the file extension. Some file formats are very generic,
 457+ * so their mime type is not very meaningful. A more useful mime type can be derived
 458+ * by looking at the file extension. Typically, this method would be called on the
 459+ * result of guessMimeType().
 460+ *
 461+ * Currently, this method does the following:
 462+ *
 463+ * If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false,
 464+ * return the result of guessTypesForExtension($ext).
 465+ *
 466+ * If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime )
 467+ * gives true, return the result of guessTypesForExtension($ext).
 468+ *
 469+ * @param $mime String: the mime type, typically guessed from a file's content.
 470+ * @param $ext String: the file extension, as taken from the file name
 471+ *
 472+ * @return string the mime type
 473+ */
 474+ public function improveTypeFromExtension( $mime, $ext ) {
 475+ if ( $mime === 'unknown/unknown' ) {
 476+ if ( $this->isRecognizableExtension( $ext ) ) {
 477+ wfDebug( __METHOD__. ': refusing to guess mime type for .' .
 478+ "$ext file, we should have recognized it\n" );
454479 } else {
455 - /* Not something we can detect, so simply
456 - * trust the file extension */
 480+ // Not something we can detect, so simply
 481+ // trust the file extension
457482 $mime = $this->guessTypesForExtension( $ext );
458483 }
459484 }
460 - else if ( $mime === "application/x-opc+zip" ) {
 485+ elseif ( $mime === 'application/x-opc+zip' ) {
461486 if ( $this->isMatchingExtension( $ext, $mime ) ) {
462 - /* A known file extension for an OPC file,
463 - * find the proper mime type for that file extension */
 487+ // A known file extension for an OPC file,
 488+ // find the proper mime type for that file extension
464489 $mime = $this->guessTypesForExtension( $ext );
465490 } else {
466491 wfDebug( __METHOD__. ": refusing to guess better type for $mime file, " .
467492 ".$ext is not a known OPC extension.\n" );
468 - $mime = "application/zip";
 493+ $mime = 'application/zip';
469494 }
470495 }
471496
@@ -476,20 +501,22 @@
477502 return $mime;
478503 }
479504
480 - /** mime type detection. This uses detectMimeType to detect the mime type of the file,
481 - * but applies additional checks to determine some well known file formats that may be missed
482 - * or misinterpreter by the default mime detection (namely XML based formats like XHTML or SVG,
483 - * as well as ZIP based formats like OPC/ODF files).
484 - *
485 - * @param $file String: the file to check
486 - * @param $ext Mixed: the file extension, or true (default) to extract it from the filename.
487 - * Set it to false to ignore the extension. DEPRECATED! Set to false, use
488 - * improveTypeFromExtension($mime, $ext) later to improve mime type.
489 - *
490 - * @return string the mime type of $file
491 - */
492 - function guessMimeType( $file, $ext = true ) {
493 - if( $ext ) { # TODO: make $ext default to false. Or better, remove it.
 505+ /**
 506+ * Mime type detection. This uses detectMimeType to detect the mime type
 507+ * of the file, but applies additional checks to determine some well known
 508+ * file formats that may be missed or misinterpreter by the default mime
 509+ * detection (namely XML based formats like XHTML or SVG, as well as ZIP
 510+ * based formats like OPC/ODF files).
 511+ *
 512+ * @param $file String: the file to check
 513+ * @param $ext Mixed: the file extension, or true (default) to extract it from the filename.
 514+ * Set it to false to ignore the extension. DEPRECATED! Set to false, use
 515+ * improveTypeFromExtension($mime, $ext) later to improve mime type.
 516+ *
 517+ * @return string the mime type of $file
 518+ */
 519+ public function guessMimeType( $file, $ext = true ) {
 520+ if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
494521 wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. " .
495522 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
496523 }
@@ -509,12 +536,21 @@
510537 return $mime;
511538 }
512539
513 - private function doGuessMimeType( $file, $ext ) { # TODO: remove $ext param
 540+ /**
 541+ * Guess the mime type from the file contents.
 542+ *
 543+ * @param string $file
 544+ * @param mixed $ext
 545+ */
 546+ private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
514547 // Read a chunk of the file
515548 wfSuppressWarnings();
516 - $f = fopen( $file, "rt" );
 549+ $f = fopen( $file, 'rt' ); // FIXME: Shouldn't this be rb?
517550 wfRestoreWarnings();
518 - if( !$f ) return "unknown/unknown";
 551+
 552+ if( !$f ) {
 553+ return 'unknown/unknown';
 554+ }
519555 $head = fread( $f, 1024 );
520556 fseek( $f, -65558, SEEK_END );
521557 $tail = fread( $f, 65558 ); // 65558 = maximum size of a zip EOCDR
@@ -541,23 +577,23 @@
542578 "\x7fELF" => 'application/octet-stream', // ELF binary
543579 );
544580
545 - foreach( $headers as $magic => $candidate ) {
546 - if( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
 581+ foreach ( $headers as $magic => $candidate ) {
 582+ if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
547583 wfDebug( __METHOD__ . ": magic header in $file recognized as $candidate\n" );
548584 return $candidate;
549585 }
550586 }
551587
552588 /* Look for WebM and Matroska files */
553 - if( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
 589+ if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
554590 $doctype = strpos( $head, "\x42\x82" );
555 - if( $doctype ) {
 591+ if ( $doctype ) {
556592 // Next byte is datasize, then data (sizes larger than 1 byte are very stupid muxers)
557593 $data = substr($head, $doctype+3, 8);
558 - if( strncmp( $data, "matroska", 8 ) == 0 ) {
 594+ if ( strncmp( $data, "matroska", 8 ) == 0 ) {
559595 wfDebug( __METHOD__ . ": recognized file as video/x-matroska\n" );
560596 return "video/x-matroska";
561 - } else if ( strncmp( $data, "webm", 4 ) == 0 ) {
 597+ } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
562598 wfDebug( __METHOD__ . ": recognized file as video/webm\n" );
563599 return "video/webm";
564600 }
@@ -567,7 +603,7 @@
568604 }
569605
570606 /* Look for WebP */
571 - if( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) {
 607+ if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8), "WEBPVP8 ", 8 ) == 0 ) {
572608 wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
573609 return "image/webp";
574610 }
@@ -584,7 +620,7 @@
585621 * 16583). The heuristic has been cut down to exclude three-character
586622 * strings like "<? ", but should it be axed completely?
587623 */
588 - if( ( strpos( $head, '<?php' ) !== false ) ||
 624+ if ( ( strpos( $head, '<?php' ) !== false ) ||
589625
590626 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
591627 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
@@ -593,16 +629,16 @@
594630 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
595631
596632 wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
597 - return "application/x-php";
 633+ return 'application/x-php';
598634 }
599635
600636 /*
601637 * look for XML formats (XHTML and SVG)
602638 */
603639 $xml = new XmlTypeCheck( $file );
604 - if( $xml->wellFormed ) {
 640+ if ( $xml->wellFormed ) {
605641 global $wgXMLMimeTypes;
606 - if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
 642+ if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
607643 return $wgXMLMimeTypes[$xml->getRootElement()];
608644 } else {
609645 return 'application/xml';
@@ -719,18 +755,18 @@
720756
721757 $openxmlRegex = "/^\[Content_Types\].xml/";
722758
723 - if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
 759+ if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
724760 $mime = $matches[1];
725761 wfDebug( __METHOD__.": detected $mime from ZIP archive\n" );
726 - } elseif( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
 762+ } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
727763 $mime = "application/x-opc+zip";
728764 # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
729 - if( $ext !== true && $ext !== false ) {
 765+ if ( $ext !== true && $ext !== false ) {
730766 /** This is the mode used by getPropsFromPath
731767 * These mime's are stored in the database, where we don't really want
732768 * x-opc+zip, because we use it only for internal purposes
733769 */
734 - if( $this->isMatchingExtension( $ext, $mime) ) {
 770+ if ( $this->isMatchingExtension( $ext, $mime) ) {
735771 /* A known file extension for an OPC file,
736772 * find the proper mime type for that file extension */
737773 $mime = $this->guessTypesForExtension( $ext );
@@ -739,10 +775,10 @@
740776 }
741777 }
742778 wfDebug( __METHOD__.": detected an Open Packaging Conventions archive: $mime\n" );
743 - } else if( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
 779+ } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
744780 ($headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
745781 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
746 - if( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) {
 782+ if ( substr( $header, 512, 4) == "\xEC\xA5\xC1\x00" ) {
747783 $mime = "application/msword";
748784 }
749785 switch( substr( $header, 512, 6) ) {
@@ -774,31 +810,34 @@
775811 return $mime;
776812 }
777813
778 - /** Internal mime type detection, please use guessMimeType() for application code instead.
779 - * Detection is done using an external program, if $wgMimeDetectorCommand is set.
780 - * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
781 - * If the dections fails and $ext is not false, the mime type is guessed from the file extension, using
782 - * guessTypesForExtension.
783 - * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
784 - * If no mime type can be determined, this function returns "unknown/unknown".
785 - *
786 - * @param $file String: the file to check
787 - * @param $ext Mixed: the file extension, or true (default) to extract it from the filename.
788 - * Set it to false to ignore the extension. DEPRECATED! Set to false, use
789 - * improveTypeFromExtension($mime, $ext) later to improve mime type.
790 - *
791 - * @return string the mime type of $file
792 - * @access private
793 - */
 814+ /**
 815+ * Internal mime type detection. Detection is done using an external
 816+ * program, if $wgMimeDetectorCommand is set. Otherwise, the fileinfo
 817+ * extension and mime_content_type are tried (in this order), if they
 818+ * are available. If the dections fails and $ext is not false, the mime
 819+ * type is guessed from the file extension, using guessTypesForExtension.
 820+ *
 821+ * If the mime type is still unknown, getimagesize is used to detect the
 822+ * mime type if the file is an image. If no mime type can be determined,
 823+ * this function returns 'unknown/unknown'.
 824+ *
 825+ * @param $file String: the file to check
 826+ * @param $ext Mixed: the file extension, or true (default) to extract it from the filename.
 827+ * Set it to false to ignore the extension. DEPRECATED! Set to false, use
 828+ * improveTypeFromExtension($mime, $ext) later to improve mime type.
 829+ *
 830+ * @return string the mime type of $file
 831+ */
794832 private function detectMimeType( $file, $ext = true ) {
795833 global $wgMimeDetectorCommand;
796834
797 - if( $ext ) { # TODO: make $ext default to false. Or better, remove it.
 835+ if ( $ext ) { # TODO: make $ext default to false. Or better, remove it.
798836 wfDebug( __METHOD__.": WARNING: use of the \$ext parameter is deprecated. Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
799837 }
800838
801839 $m = null;
802840 if ( $wgMimeDetectorCommand ) {
 841+ // FIXME: Use wfShellExec
803842 $fn = wfEscapeShellArg( $file );
804843 $m = `$wgMimeDetectorCommand $fn`;
805844 } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
@@ -813,9 +852,9 @@
814853 # If you may need to load the fileinfo extension at runtime, set
815854 # $wgLoadFileinfoExtension in LocalSettings.php
816855
817 - $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
 856+ $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return mime type ala mimetype extension */
818857
819 - if ($mime_magic_resource) {
 858+ if ( $mime_magic_resource ) {
820859 $m = finfo_file( $mime_magic_resource, $file );
821860 finfo_close( $mime_magic_resource );
822861 } else {
@@ -851,7 +890,7 @@
852891 }
853892 }
854893
855 - # if desired, look at extension as a fallback.
 894+ // If desired, look at extension as a fallback.
856895 if ( $ext === true ) {
857896 $i = strrpos( $file, '.' );
858897 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
@@ -868,36 +907,40 @@
869908 }
870909 }
871910
872 - #unknown type
873 - wfDebug( __METHOD__.": failed to guess mime type for $file!\n" );
874 - return "unknown/unknown";
 911+ // Unknown type
 912+ wfDebug( __METHOD__ . ": failed to guess mime type for $file!\n" );
 913+ return 'unknown/unknown';
875914 }
876915
877916 /**
878 - * Determine the media type code for a file, using its mime type, name and possibly
879 - * its contents.
880 - *
881 - * This function relies on the findMediaType(), mapping extensions and mime
882 - * types to media types.
883 - *
884 - * @todo analyse file if need be
885 - * @todo look at multiple extension, separately and together.
886 - *
887 - * @param $path String: full path to the image file, in case we have to look at the contents
888 - * (if null, only the mime type is used to determine the media type code).
889 - * @param $mime String: mime type. If null it will be guessed using guessMimeType.
890 - *
891 - * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
892 - */
 917+ * Determine the media type code for a file, using its mime type, name and
 918+ * possibly its contents.
 919+ *
 920+ * This function relies on the findMediaType(), mapping extensions and mime
 921+ * types to media types.
 922+ *
 923+ * @todo analyse file if need be
 924+ * @todo look at multiple extension, separately and together.
 925+ *
 926+ * @param $path String: full path to the image file, in case we have to look at the contents
 927+ * (if null, only the mime type is used to determine the media type code).
 928+ * @param $mime String: mime type. If null it will be guessed using guessMimeType.
 929+ *
 930+ * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
 931+ */
893932 function getMediaType( $path = null, $mime = null ) {
894 - if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
 933+ if( !$mime && !$path ) {
 934+ return MEDIATYPE_UNKNOWN;
 935+ }
895936
896 - # If mime type is unknown, guess it
897 - if( !$mime ) $mime = $this->guessMimeType( $path, false );
 937+ // If mime type is unknown, guess it
 938+ if( !$mime ) {
 939+ $mime = $this->guessMimeType( $path, false );
 940+ }
898941
899 - # Special code for ogg - detect if it's video (theora),
900 - # else label it as sound.
901 - if( $mime == "application/ogg" && file_exists( $path ) ) {
 942+ // Special code for ogg - detect if it's video (theora),
 943+ // else label it as sound.
 944+ if ( $mime == 'application/ogg' && file_exists( $path ) ) {
902945
903946 // Read a chunk of the file
904947 $f = fopen( $path, "rt" );
@@ -907,7 +950,7 @@
908951
909952 $head = strtolower( $head );
910953
911 - # This is an UGLY HACK, file should be parsed correctly
 954+ // This is an UGLY HACK, file should be parsed correctly
912955 if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
913956 elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
914957 elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
@@ -915,58 +958,69 @@
916959 else return MEDIATYPE_MULTIMEDIA;
917960 }
918961
919 - # check for entry for full mime type
 962+ // Check for entry for full mime type
920963 if( $mime ) {
921964 $type = $this->findMediaType( $mime );
922 - if( $type !== MEDIATYPE_UNKNOWN ) return $type;
 965+ if ( $type !== MEDIATYPE_UNKNOWN ) {
 966+ return $type;
 967+ }
923968 }
924969
925 - # Check for entry for file extension
 970+ // Check for entry for file extension
926971 if ( $path ) {
927972 $i = strrpos( $path, '.' );
928973 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
929974
930 - # TODO: look at multi-extension if this fails, parse from full path
931 -
 975+ // TODO: look at multi-extension if this fails, parse from full path
932976 $type = $this->findMediaType( '.' . $e );
933 - if ( $type !== MEDIATYPE_UNKNOWN ) return $type;
 977+ if ( $type !== MEDIATYPE_UNKNOWN ) {
 978+ return $type;
 979+ }
934980 }
935981
936 - # Check major mime type
937 - if( $mime ) {
 982+ // Check major mime type
 983+ if ( $mime ) {
938984 $i = strpos( $mime, '/' );
939 - if( $i !== false ) {
 985+ if ( $i !== false ) {
940986 $major = substr( $mime, 0, $i );
941987 $type = $this->findMediaType( $major );
942 - if( $type !== MEDIATYPE_UNKNOWN ) return $type;
 988+ if ( $type !== MEDIATYPE_UNKNOWN ) {
 989+ return $type;
 990+ }
943991 }
944992 }
945993
946 - if( !$type ) $type = MEDIATYPE_UNKNOWN;
 994+ if( !$type ) {
 995+ $type = MEDIATYPE_UNKNOWN;
 996+ }
947997
948998 return $type;
949999 }
9501000
951 - /** returns a media code matching the given mime type or file extension.
952 - * File extensions are represented by a string starting with a dot (.) to
953 - * distinguish them from mime types.
954 - *
955 - * This funktion relies on the mapping defined by $this->mMediaTypes
956 - * @access private
957 - */
 1001+ /**
 1002+ * Returns a media code matching the given mime type or file extension.
 1003+ * File extensions are represented by a string starting with a dot (.) to
 1004+ * distinguish them from mime types.
 1005+ *
 1006+ * This funktion relies on the mapping defined by $this->mMediaTypes
 1007+ * @access private
 1008+ */
9581009 function findMediaType( $extMime ) {
959 - if ( strpos( $extMime, '.' ) === 0 ) { #if it's an extension, look up the mime types
 1010+ if ( strpos( $extMime, '.' ) === 0 ) {
 1011+ // If it's an extension, look up the mime types
9601012 $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
961 - if ( !$m ) return MEDIATYPE_UNKNOWN;
 1013+ if ( !$m ) {
 1014+ return MEDIATYPE_UNKNOWN;
 1015+ }
9621016
9631017 $m = explode( ' ', $m );
9641018 } else {
965 - # Normalize mime type
 1019+ // Normalize mime type
9661020 if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
9671021 $extMime = $this->mMimeTypeAliases[$extMime];
9681022 }
9691023
970 - $m = array($extMime);
 1024+ $m = array( $extMime );
9711025 }
9721026
9731027 foreach ( $m as $mime ) {

Status & tagging log