Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / MimeMagic.php
index 582ba60..c2ac186 100644 (file)
@@ -569,20 +569,30 @@ class MimeMagic {
         * @param string $file
         * @param mixed $ext
         * @return bool|string
+        * @throws MWException
         */
        private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
                // Read a chunk of the file
                wfSuppressWarnings();
-               // @todo FIXME: Shouldn't this be rb?
-               $f = fopen( $file, 'rt' );
+               $f = fopen( $file, 'rb' );
                wfRestoreWarnings();
 
                if ( !$f ) {
                        return 'unknown/unknown';
                }
+
+               $fsize = filesize( $file );
+               if ( $fsize === false ) {
+                       return 'unknown/unknown';
+               }
+
                $head = fread( $f, 1024 );
-               fseek( $f, -65558, SEEK_END );
-               $tail = fread( $f, 65558 ); // 65558 = maximum size of a zip EOCDR
+               $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
+               if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
+                       throw new MWException(
+                               "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
+               }
+               $tail = fread( $f, $tailLength );
                fclose( $f );
 
                wfDebug( __METHOD__ . ": analyzing head and tail of $file for magic numbers.\n" );
@@ -984,7 +994,7 @@ class MimeMagic {
                        $head = fread( $f, 256 );
                        fclose( $f );
 
-                       $head = strtolower( $head );
+                       $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) );
 
                        // This is an UGLY HACK, file should be parsed correctly
                        if ( strpos( $head, 'theora' ) !== false ) {