48 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
52 $fh = fopen( $filename,
"rb" );
58 $buffer = fread( $fh, 2 );
59 if ( $buffer !==
"\xFF\xD8" ) {
63 $showXMP = XMPReader::isSupported();
73 while ( !feof( $fh ) ) {
74 $buffer = fread( $fh, 1 );
76 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
79 while ( $buffer !==
"\xFF" && !feof( $fh ) ) {
82 $buffer = fread( $fh, 1 );
85 $buffer = fread( $fh, 1 );
86 while ( $buffer ===
"\xFF" && !feof( $fh ) ) {
88 $buffer = fread( $fh, 1 );
90 if ( $buffer ===
"\xFE" ) {
94 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
95 \UtfNormal\Validator::quickIsNFCVerify( $com );
98 if ( $com !== $oldCom ) {
99 AtEase::suppressWarnings();
100 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
101 AtEase::restoreWarnings();
105 \UtfNormal\Validator::quickIsNFCVerify( $com );
106 if ( $com === $oldCom ) {
107 $segments[
"COM"][] = $oldCom;
109 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage." );
111 } elseif ( $buffer ===
"\xE1" ) {
114 $temp = self::jpegExtractMarker( $fh );
116 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
118 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
119 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
121 $segments[
"XMP_ext"][] = trim( substr( $temp, 35 ) );
122 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
127 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
128 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
129 .
"Using anyways." );
130 } elseif ( str_starts_with( $temp,
"Exif\0\0" ) ) {
134 $byteOrderMarker = substr( $temp, 6, 2 );
135 if ( $byteOrderMarker ===
'MM' ) {
136 $segments[
'byteOrder'] =
'BE';
137 } elseif ( $byteOrderMarker ===
'II' ) {
138 $segments[
'byteOrder'] =
'LE';
140 wfDebug( __METHOD__ .
" Invalid byte ordering?!" );
143 } elseif ( $buffer ===
"\xED" ) {
145 $temp = self::jpegExtractMarker( $fh );
146 if ( str_starts_with( $temp,
"Photoshop 3.0\x00" ) ) {
147 $segments[
"PSIR"][] = $temp;
149 } elseif ( $buffer ===
"\xD9" || $buffer ===
"\xDA" ) {
152 } elseif ( in_array( $buffer, [
153 "\xC0",
"\xC1",
"\xC2",
"\xC3",
"\xC5",
"\xC6",
"\xC7",
154 "\xC9",
"\xCA",
"\xCB",
"\xCD",
"\xCE",
"\xCF" ] )
157 $temp = self::jpegExtractMarker( $fh );
159 $segments[
"SOF"] = StringUtils::unpack(
'Cbits/nheight/nwidth/Ccomponents', $temp );
166 $size = StringUtils::unpack(
"nint", fread( $fh, 2 ), 2 );
170 if ( $size[
'int'] < 2 ) {
175 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
223 public static function doPSIR( $app13 ) {
235 $appLen = strlen( $app13 );
240 while ( $offset + 12 <= $appLen ) {
242 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
247 $id = substr( $app13, $offset, 2 );
258 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
260 if ( $lenName % 2 === 1 ) {
268 $lenData = StringUtils::unpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
275 if ( $lenData[
'len'] < 0 ) {
283 if ( $lenData[
'len'] + $offset > $appLen ) {
285 .
"; offset=$offset; total length=$appLen)" );
292 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
295 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
302 if ( $lenData[
'len'] % 2 === 1 ) {
305 $offset += $lenData[
'len'];
308 if ( !$realHash || !$recordedHash ) {
309 return 'iptc-no-hash';
311 if ( $realHash === $recordedHash ) {
312 return 'iptc-good-hash';
315 return 'iptc-bad-hash';