54 $showXMP = XMPReader::isSupported();
65 throw new MWException(
"No filename specified for " . __METHOD__ );
67 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
68 throw new MWException(
"Invalid file $filename passed to " . __METHOD__ );
71 $fh = fopen( $filename,
"rb" );
74 throw new MWException(
"Could not open file $filename" );
77 $buffer = fread( $fh, 2 );
78 if ( $buffer !==
"\xFF\xD8" ) {
81 while ( !feof( $fh ) ) {
82 $buffer = fread( $fh, 1 );
84 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
85 throw new MWException(
'Too many jpeg segments. Aborting' );
87 while ( $buffer !==
"\xFF" && !feof( $fh ) ) {
90 $buffer = fread( $fh, 1 );
93 $buffer = fread( $fh, 1 );
94 while ( $buffer ===
"\xFF" && !feof( $fh ) ) {
96 $buffer = fread( $fh, 1 );
98 if ( $buffer ===
"\xFE" ) {
102 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
103 UtfNormal\Validator::quickIsNFCVerify( $com );
106 if ( $com !== $oldCom ) {
107 AtEase::suppressWarnings();
108 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
109 AtEase::restoreWarnings();
113 UtfNormal\Validator::quickIsNFCVerify( $com );
114 if ( $com === $oldCom ) {
115 $segments[
"COM"][] = $oldCom;
117 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage." );
119 } elseif ( $buffer ===
"\xE1" ) {
122 $temp = self::jpegExtractMarker( $fh );
124 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
126 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
127 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
129 $segments[
"XMP_ext"][] = trim( substr( $temp, 35 ) );
130 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
135 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
136 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
137 .
"Using anyways." );
138 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
142 $byteOrderMarker = substr( $temp, 6, 2 );
143 if ( $byteOrderMarker ===
'MM' ) {
144 $segments[
'byteOrder'] =
'BE';
145 } elseif ( $byteOrderMarker ===
'II' ) {
146 $segments[
'byteOrder'] =
'LE';
148 wfDebug( __METHOD__ .
" Invalid byte ordering?!" );
151 } elseif ( $buffer ===
"\xED" ) {
153 $temp = self::jpegExtractMarker( $fh );
154 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
155 $segments[
"PSIR"][] = $temp;
157 } elseif ( $buffer ===
"\xD9" || $buffer ===
"\xDA" ) {
160 } elseif ( in_array( $buffer, [
161 "\xC0",
"\xC1",
"\xC2",
"\xC3",
"\xC5",
"\xC6",
"\xC7",
162 "\xC9",
"\xCA",
"\xCB",
"\xCD",
"\xCE",
"\xCF" ] )
165 $temp = self::jpegExtractMarker( $fh );
166 $segments[
"SOF"] =
wfUnpack(
'Cbits/nheight/nwidth/Ccomponents', $temp );
169 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
170 if ( $size[
'int'] < 2 ) {
171 throw new MWException(
"invalid marker size in jpeg" );
175 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
179 throw new MWException(
"Reached end of jpeg file unexpectedly" );
219 public static function doPSIR( $app13 ) {
230 $appLen = strlen( $app13 );
235 while ( $offset + 12 <= $appLen ) {
237 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
244 $id = substr( $app13, $offset, 2 );
255 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
257 if ( $lenName % 2 === 1 ) {
263 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
267 if ( $lenData[
'len'] < 0 ) {
268 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
274 if ( $lenData[
'len'] + $offset > $appLen ) {
275 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
276 .
"; offset=$offset; total length=$appLen)" );
283 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
286 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
293 if ( $lenData[
'len'] % 2 === 1 ) {
296 $offset += $lenData[
'len'];
299 if ( !$realHash || !$recordedHash ) {
300 return 'iptc-no-hash';
302 if ( $realHash === $recordedHash ) {
303 return 'iptc-good-hash';
306 return 'iptc-bad-hash';