53 $showXMP = XMPReader::isSupported();
64 throw new MWException(
"No filename specified for " . __METHOD__ );
66 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
67 throw new MWException(
"Invalid file $filename passed to " . __METHOD__ );
70 $fh = fopen( $filename,
"rb" );
73 throw new MWException(
"Could not open file $filename" );
80 while ( !feof( $fh ) ) {
83 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
85 throw new MWException(
'Too many jpeg segments. Aborting' );
87 while (
$buffer !==
"\xFF" && !feof( $fh ) ) {
94 while (
$buffer ===
"\xFF" && !feof( $fh ) ) {
102 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
103 UtfNormal\Validator::quickIsNFCVerify( $com );
106 if ( $com !== $oldCom ) {
107 Wikimedia\suppressWarnings();
108 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
109 Wikimedia\restoreWarnings();
113 UtfNormal\Validator::quickIsNFCVerify( $com );
114 if ( $com === $oldCom ) {
115 $segments[
"COM"][] = $oldCom;
117 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage.\n" );
119 } elseif (
$buffer ===
"\xE1" ) {
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.\n" );
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?!\n" );
151 } elseif (
$buffer ===
"\xED" ) {
154 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
155 $segments[
"PSIR"][] = $temp;
162 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
163 if ( $size[
'int'] < 2 ) {
164 throw new MWException(
"invalid marker size in jpeg" );
168 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
172 throw new MWException(
"Reached end of jpeg file unexpectedly" );
212 public static function doPSIR( $app13 ) {
223 $appLen = strlen( $app13 );
228 while ( $offset + 12 <= $appLen ) {
230 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
237 $id = substr( $app13, $offset, 2 );
248 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
250 if ( $lenName % 2 == 1 ) {
256 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
260 if ( $lenData[
'len'] < 0 ) {
261 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
267 if ( $lenData[
'len'] + $offset > $appLen ) {
268 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
269 .
"; offset=$offset; total length=$appLen)" );
276 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
279 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
286 if ( $lenData[
'len'] % 2 == 1 ) {
289 $offset += $lenData[
'len'];
292 if ( !$realHash || !$recordedHash ) {
293 return 'iptc-no-hash';
294 } elseif ( $realHash === $recordedHash ) {
295 return 'iptc-good-hash';
297 return 'iptc-bad-hash';