From d96c6f01479efebb5c636cb1deeda43ff2deafdf Mon Sep 17 00:00:00 2001 From: rillke Date: Tue, 10 Jun 2014 23:50:30 +0200 Subject: [PATCH] MimeMagic: Don't seek before BOF This has weird side effects like only extracting the tail of the file partially or not at all. Bug: 66428 Change-Id: I182128c6958244f1515227ee742c3206a7484aee --- includes/MimeMagic.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php index 582ba60be2..f4d4697c38 100644 --- a/includes/MimeMagic.php +++ b/includes/MimeMagic.php @@ -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" ); -- 2.20.1