X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fmedia%2FJpegMetadataExtractor.php;h=0d8013d57559f2bacc60e688c99097ceadfdf5ce;hb=85f924e59135271ef57b50a5f8b2a52be833413a;hp=6ff07ed276928c79b03fc3077b4d2bf2a9a9cde6;hpb=8a3a8abbb5b3c8e55b6afe915657bc3d18e31d0c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/JpegMetadataExtractor.php b/includes/media/JpegMetadataExtractor.php index 6ff07ed276..0d8013d575 100644 --- a/includes/media/JpegMetadataExtractor.php +++ b/includes/media/JpegMetadataExtractor.php @@ -30,8 +30,8 @@ * @ingroup Media */ class JpegMetadataExtractor { - const MAX_JPEG_SEGMENTS = 200; + // the max segment is a sanity check. // A jpeg file should never even remotely have // that many segments. Your average file has about 10. @@ -43,11 +43,11 @@ class JpegMetadataExtractor { * but gis doesn't support having multiple app1 segments * and those can't extract xmp on files containing both exif and xmp data * - * @param string $filename name of jpeg file - * @return Array of interesting segments. - * @throws MWException if given invalid file. + * @param string $filename Name of jpeg file + * @return array Array of interesting segments. + * @throws MWException If given invalid file. */ - static function segmentSplitter ( $filename ) { + static function segmentSplitter( $filename ) { $showXMP = function_exists( 'xml_parser_create_ns' ); $segmentCount = 0; @@ -83,11 +83,12 @@ class JpegMetadataExtractor { throw new MWException( 'Too many jpeg segments. Aborting' ); } if ( $buffer !== "\xFF" ) { - throw new MWException( "Error reading jpeg file marker. Expected 0xFF but got " . bin2hex( $buffer ) ); + throw new MWException( "Error reading jpeg file marker. " . + "Expected 0xFF but got " . bin2hex( $buffer ) ); } $buffer = fread( $fh, 1 ); - while( $buffer === "\xFF" && !feof( $fh ) ) { + while ( $buffer === "\xFF" && !feof( $fh ) ) { // Skip through any 0xFF padding bytes. $buffer = fread( $fh, 1 ); } @@ -97,7 +98,7 @@ class JpegMetadataExtractor { // First see if valid utf-8, // if not try to convert it to windows-1252. $com = $oldCom = trim( self::jpegExtractMarker( $fh ) ); - UtfNormal::quickIsNFCVerify( $com ); + UtfNormal\Validator::quickIsNFCVerify( $com ); // turns $com to valid utf-8. // thus if no change, its utf-8, otherwise its something else. if ( $com !== $oldCom ) { @@ -107,13 +108,12 @@ class JpegMetadataExtractor { } // Try it again, if its still not a valid string, then probably // binary junk or some really weird encoding, so don't extract. - UtfNormal::quickIsNFCVerify( $com ); + UtfNormal\Validator::quickIsNFCVerify( $com ); if ( $com === $oldCom ) { $segments["COM"][] = $oldCom; } else { - wfDebug( __METHOD__ . ' Ignoring JPEG comment as is garbage.' ); + wfDebug( __METHOD__ . " Ignoring JPEG comment as is garbage.\n" ); } - } elseif ( $buffer === "\xE1" ) { // APP1 section (Exif, XMP, and XMP extended) // only extract if XMP is enabled. @@ -140,7 +140,7 @@ class JpegMetadataExtractor { } elseif ( $byteOrderMarker === 'II' ) { $segments['byteOrder'] = 'LE'; } else { - wfDebug( __METHOD__ . ' Invalid byte ordering?!' ); + wfDebug( __METHOD__ . " Invalid byte ordering?!\n" ); } } } elseif ( $buffer === "\xED" ) { @@ -155,10 +155,11 @@ class JpegMetadataExtractor { } else { // segment we don't care about, so skip $size = wfUnpack( "nint", fread( $fh, 2 ), 2 ); - if ( $size['int'] <= 2 ) throw new MWException( "invalid marker size in jpeg" ); + if ( $size['int'] <= 2 ) { + throw new MWException( "invalid marker size in jpeg" ); + } fseek( $fh, $size['int'] - 2, SEEK_CUR ); } - } // shouldn't get here. throw new MWException( "Reached end of jpeg file unexpectedly" ); @@ -166,9 +167,9 @@ class JpegMetadataExtractor { /** * Helper function for jpegSegmentSplitter - * @param &$fh FileHandle for jpeg file + * @param resource &$fh File handle for JPEG file * @throws MWException - * @return string data content of segment. + * @return string Data content of segment. */ private static function jpegExtractMarker( &$fh ) { $size = wfUnpack( "nint", fread( $fh, 2 ), 2 ); @@ -179,6 +180,7 @@ class JpegMetadataExtractor { if ( strlen( $segment ) !== $size['int'] - 2 ) { throw new MWException( "Segment shorter than expected" ); } + return $segment; } @@ -191,11 +193,12 @@ class JpegMetadataExtractor { * * This should generally be called by BitmapMetadataHandler::doApp13() * - * @param string $app13 photoshop psir app13 block from jpg. + * @param string $app13 Photoshop psir app13 block from jpg. * @throws MWException (It gets caught next level up though) - * @return String if the iptc hash is good or not. + * @return string If the iptc hash is good or not. One of 'iptc-no-hash', + * 'iptc-good-hash', 'iptc-bad-hash'. */ - public static function doPSIR ( $app13 ) { + public static function doPSIR( $app13 ) { if ( !$app13 ) { throw new MWException( "No App13 segment given" ); } @@ -243,7 +246,9 @@ class JpegMetadataExtractor { // PHP can take issue with very large unsigned ints and make them negative. // Which should never ever happen, as this has to be inside a segment // which is limited to a 16 bit number. - if ( $lenData['len'] < 0 ) throw new MWException( "Too big PSIR (" . $lenData['len'] . ')' ); + if ( $lenData['len'] < 0 ) { + throw new MWException( "Too big PSIR (" . $lenData['len'] . ')' ); + } $offset += 4; // 4bytes length field; @@ -267,9 +272,10 @@ class JpegMetadataExtractor { // if odd, add 1 to length to account for // null pad byte. - if ( $lenData['len'] % 2 == 1 ) $lenData['len']++; + if ( $lenData['len'] % 2 == 1 ) { + $lenData['len']++; + } $offset += $lenData['len']; - } if ( !$realHash || !$recordedHash ) {