Merge "Show protection log on creation-protected pages"
[lhc/web/wiklou.git] / includes / libs / mime / MimeAnalyzer.php
index c361fdf..4d860bb 100644 (file)
@@ -533,6 +533,9 @@ EOT;
 
                        // XML formats we sure hope we recognize reliably
                        'svg',
+
+                       // 3D formats
+                       'stl',
                ];
                return in_array( strtolower( $extension ), $types );
        }
@@ -706,8 +709,17 @@ EOT;
                                        $this->logger->info( __METHOD__ . ": recognized file as video/x-matroska\n" );
                                        return "video/x-matroska";
                                } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
-                                       $this->logger->info( __METHOD__ . ": recognized file as video/webm\n" );
-                                       return "video/webm";
+                                       // XXX HACK look for a video track, if we don't find it, this is an audio file
+                                       $videotrack = strpos( $head, "\x86\x85V_VP" );
+
+                                       if ( $videotrack ) {
+                                               // There is a video track, so this is a video file.
+                                               $this->logger->info( __METHOD__ . ": recognized file as video/webm\n" );
+                                               return "video/webm";
+                                       }
+
+                                       $this->logger->info( __METHOD__ . ": recognized file as audio/webm\n" );
+                                       return "audio/webm";
                                }
                        }
                        $this->logger->info( __METHOD__ . ": unknown EBML file\n" );
@@ -804,6 +816,23 @@ EOT;
                        return $this->detectZipType( $head, $tail, $ext );
                }
 
+               // Check for STL (3D) files
+               // @see https://en.wikipedia.org/wiki/STL_(file_format)
+               if ( $fsize >= 15 &&
+                       stripos( $head, 'SOLID ' ) === 0 &&
+                       preg_match( '/\RENDSOLID .*$/i', $tail ) ) {
+                       // ASCII STL file
+                       return 'application/sla';
+               } elseif ( $fsize > 84 ) {
+                       // binary STL file
+                       $triangles = substr( $head, 80, 4 );
+                       $triangles = unpack( 'V', $triangles );
+                       $triangles = reset( $triangles );
+                       if ( $triangles !== false && $fsize === 84 + ( $triangles * 50 ) ) {
+                               return 'application/sla';
+                       }
+               }
+
                MediaWiki\suppressWarnings();
                $gis = getimagesize( $file );
                MediaWiki\restoreWarnings();
@@ -959,18 +988,8 @@ EOT;
                $m = null;
                if ( $callback ) {
                        $m = $callback( $file );
-               } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
-                       $mime_magic_resource = finfo_open( FILEINFO_MIME );
-
-                       if ( $mime_magic_resource ) {
-                               $m = finfo_file( $mime_magic_resource, $file );
-                               finfo_close( $mime_magic_resource );
-                       } else {
-                               $this->logger->info( __METHOD__ .
-                                       ": finfo_open failed on " . FILEINFO_MIME . "!\n" );
-                       }
                } else {
-                       $this->logger->info( __METHOD__ . ": no magic mime detector found!\n" );
+                       $m = mime_content_type( $file );
                }
 
                if ( $m ) {