56 $showXMP = XMPReader::isSupported();
69 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
73 $fh = fopen( $filename,
"rb" );
79 $buffer = fread( $fh, 2 );
80 if ( $buffer !==
"\xFF\xD8" ) {
83 while ( !feof( $fh ) ) {
84 $buffer = fread( $fh, 1 );
86 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
89 while ( $buffer !==
"\xFF" && !feof( $fh ) ) {
92 $buffer = fread( $fh, 1 );
95 $buffer = fread( $fh, 1 );
96 while ( $buffer ===
"\xFF" && !feof( $fh ) ) {
98 $buffer = fread( $fh, 1 );
100 if ( $buffer ===
"\xFE" ) {
104 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
105 UtfNormal\Validator::quickIsNFCVerify( $com );
108 if ( $com !== $oldCom ) {
109 AtEase::suppressWarnings();
110 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
111 AtEase::restoreWarnings();
115 UtfNormal\Validator::quickIsNFCVerify( $com );
116 if ( $com === $oldCom ) {
117 $segments[
"COM"][] = $oldCom;
119 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage." );
121 } elseif ( $buffer ===
"\xE1" ) {
124 $temp = self::jpegExtractMarker( $fh );
126 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
128 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
129 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
131 $segments[
"XMP_ext"][] = trim( substr( $temp, 35 ) );
132 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
137 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
138 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
139 .
"Using anyways." );
140 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
144 $byteOrderMarker = substr( $temp, 6, 2 );
145 if ( $byteOrderMarker ===
'MM' ) {
146 $segments[
'byteOrder'] =
'BE';
147 } elseif ( $byteOrderMarker ===
'II' ) {
148 $segments[
'byteOrder'] =
'LE';
150 wfDebug( __METHOD__ .
" Invalid byte ordering?!" );
153 } elseif ( $buffer ===
"\xED" ) {
155 $temp = self::jpegExtractMarker( $fh );
156 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
157 $segments[
"PSIR"][] = $temp;
159 } elseif ( $buffer ===
"\xD9" || $buffer ===
"\xDA" ) {
162 } elseif ( in_array( $buffer, [
163 "\xC0",
"\xC1",
"\xC2",
"\xC3",
"\xC5",
"\xC6",
"\xC7",
164 "\xC9",
"\xCA",
"\xCB",
"\xCD",
"\xCE",
"\xCF" ] )
167 $temp = self::jpegExtractMarker( $fh );
169 $segments[
"SOF"] = StringUtils::unpack(
'Cbits/nheight/nwidth/Ccomponents', $temp );
176 $size = StringUtils::unpack(
"nint", fread( $fh, 2 ), 2 );
180 if ( $size[
'int'] < 2 ) {
185 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
233 public static function doPSIR( $app13 ) {
244 $appLen = strlen( $app13 );
249 while ( $offset + 12 <= $appLen ) {
251 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
258 $id = substr( $app13, $offset, 2 );
269 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
271 if ( $lenName % 2 === 1 ) {
278 $lenData = StringUtils::unpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
285 if ( $lenData[
'len'] < 0 ) {
292 if ( $lenData[
'len'] + $offset > $appLen ) {
294 .
"; offset=$offset; total length=$appLen)" );
301 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
304 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
311 if ( $lenData[
'len'] % 2 === 1 ) {
314 $offset += $lenData[
'len'];
317 if ( !$realHash || !$recordedHash ) {
318 return 'iptc-no-hash';
320 if ( $realHash === $recordedHash ) {
321 return 'iptc-good-hash';
324 return 'iptc-bad-hash';