Merge "MimeAnalyzer: Detect magic bytes for mp3"
[lhc/web/wiklou.git] / includes / libs / mime / MimeAnalyzer.php
index 5f4d7c9..565c157 100644 (file)
@@ -83,7 +83,7 @@ class MimeAnalyzer implements LoggerAwareInterface {
         * what will break? In practice this probably isn't a problem anyway -- Bryan)
         */
        protected static $wellKnownTypes = <<<EOT
-application/ogg ogx ogg ogm ogv oga spx
+application/ogg ogx ogg ogm ogv oga spx opus
 application/pdf pdf
 application/vnd.oasis.opendocument.chart odc
 application/vnd.oasis.opendocument.chart-template otc
@@ -108,7 +108,8 @@ audio/midi mid midi kar
 audio/mpeg mpga mpa mp2 mp3
 audio/x-aiff aif aiff aifc
 audio/x-wav wav
-audio/ogg oga spx ogg
+audio/ogg oga spx ogg opus
+audio/opus opus ogg oga ogg spx
 image/x-bmp bmp
 image/gif gif
 image/jpeg jpeg jpg jpe
@@ -526,9 +527,9 @@ EOT;
                        'xbm',
 
                        // Formats we recognize magic numbers for
-                       'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx',
+                       'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'opus',
                        'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
-                       'webp',
+                       'webp', 'mp3',
 
                        // XML formats we sure hope we recognize reliably
                        'svg',
@@ -669,6 +670,10 @@ EOT;
                        // Multimedia...
                        'MThd'             => 'audio/midi',
                        'OggS'             => 'application/ogg',
+                       'ID3'              => 'audio/mpeg',
+                       "\xff\xfb"         => 'audio/mpeg', // MPEG-1 layer 3
+                       "\xff\xf3"         => 'audio/mpeg', // MPEG-2 layer 3 (lower sample rates)
+                       "\xff\xe3"         => 'audio/mpeg', // MPEG-2.5 layer 3 (very low sample rates)
 
                        // Image formats...
                        // Note that WMF may have a bare header, no magic number.
@@ -734,8 +739,8 @@ EOT;
                        ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
                        ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
                        ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
-                       ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
-
+                       ( strpos( $head, "<\x00?\x00=" ) !== false )
+               ) {
                        $this->logger->info( __METHOD__ . ": recognized $file as application/x-php\n" );
                        return 'application/x-php';
                }
@@ -861,7 +866,7 @@ EOT;
                        'text-web',
                        'text' ];
 
-               // http://lists.oasis-open.org/archives/office/200505/msg00006.html
+               // https://lists.oasis-open.org/archives/office/200505/msg00006.html
                $types = '(?:' . implode( '|', $opendocTypes ) . ')';
                $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
 
@@ -1034,7 +1039,6 @@ EOT;
                // Special code for ogg - detect if it's video (theora),
                // else label it as sound.
                if ( $mime == 'application/ogg' && file_exists( $path ) ) {
-
                        // Read a chunk of the file
                        $f = fopen( $path, "rt" );
                        if ( !$f ) {
@@ -1054,6 +1058,8 @@ EOT;
                                return MEDIATYPE_AUDIO;
                        } elseif ( strpos( $head, 'speex' ) !== false ) {
                                return MEDIATYPE_AUDIO;
+                       } elseif ( strpos( $head, 'opus' ) !== false ) {
+                               return MEDIATYPE_AUDIO;
                        } else {
                                return MEDIATYPE_MULTIMEDIA;
                        }