47 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
51 $fh = fopen( $filename,
"rb" );
57 $buffer = fread( $fh, 2 );
58 if ( $buffer !==
"\xFF\xD8" ) {
62 $showXMP = XMPReader::isSupported();
72 while ( !feof( $fh ) ) {
73 $buffer = fread( $fh, 1 );
75 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
78 while ( $buffer !==
"\xFF" && !feof( $fh ) ) {
81 $buffer = fread( $fh, 1 );
84 $buffer = fread( $fh, 1 );
85 while ( $buffer ===
"\xFF" && !feof( $fh ) ) {
87 $buffer = fread( $fh, 1 );
89 if ( $buffer ===
"\xFE" ) {
93 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
94 \UtfNormal\Validator::quickIsNFCVerify( $com );
97 if ( $com !== $oldCom ) {
99 $com = $oldCom = @iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
103 \UtfNormal\Validator::quickIsNFCVerify( $com );
104 if ( $com === $oldCom ) {
105 $segments[
"COM"][] = $oldCom;
107 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage." );
109 } elseif ( $buffer ===
"\xE1" ) {
112 $temp = self::jpegExtractMarker( $fh );
114 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
116 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
117 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
119 $segments[
"XMP_ext"][] = trim( substr( $temp, 35 ) );
120 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
125 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
126 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
127 .
"Using anyways." );
128 } elseif ( str_starts_with( $temp,
"Exif\0\0" ) ) {
132 $byteOrderMarker = substr( $temp, 6, 2 );
133 if ( $byteOrderMarker ===
'MM' ) {
134 $segments[
'byteOrder'] =
'BE';
135 } elseif ( $byteOrderMarker ===
'II' ) {
136 $segments[
'byteOrder'] =
'LE';
138 wfDebug( __METHOD__ .
" Invalid byte ordering?!" );
141 } elseif ( $buffer ===
"\xED" ) {
143 $temp = self::jpegExtractMarker( $fh );
144 if ( str_starts_with( $temp,
"Photoshop 3.0\x00" ) ) {
145 $segments[
"PSIR"][] = $temp;
147 } elseif ( $buffer ===
"\xD9" || $buffer ===
"\xDA" ) {
150 } elseif ( in_array( $buffer, [
151 "\xC0",
"\xC1",
"\xC2",
"\xC3",
"\xC5",
"\xC6",
"\xC7",
152 "\xC9",
"\xCA",
"\xCB",
"\xCD",
"\xCE",
"\xCF" ] )
155 $temp = self::jpegExtractMarker( $fh );
157 $segments[
"SOF"] = StringUtils::unpack(
'Cbits/nheight/nwidth/Ccomponents', $temp );
164 $size = StringUtils::unpack(
"nint", fread( $fh, 2 ), 2 );
168 if ( $size[
'int'] < 2 ) {
173 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
221 public static function doPSIR( $app13 ) {
233 $appLen = strlen( $app13 );
238 while ( $offset + 12 <= $appLen ) {
240 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
245 $id = substr( $app13, $offset, 2 );
256 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
258 if ( $lenName % 2 === 1 ) {
266 $lenData = StringUtils::unpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
273 if ( $lenData[
'len'] < 0 ) {
281 if ( $lenData[
'len'] + $offset > $appLen ) {
283 .
"; offset=$offset; total length=$appLen)" );
290 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
293 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
300 if ( $lenData[
'len'] % 2 === 1 ) {
303 $offset += $lenData[
'len'];
306 if ( !$realHash || !$recordedHash ) {
307 return 'iptc-no-hash';
309 if ( $realHash === $recordedHash ) {
310 return 'iptc-good-hash';
313 return 'iptc-bad-hash';