62 throw new MWException(
"No filename specified for " . __METHOD__ );
64 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
65 throw new MWException(
"Invalid file $filename passed to " . __METHOD__ );
68 $fh = fopen( $filename,
"rb" );
71 throw new MWException(
"Could not open file $filename" );
78 while ( !feof( $fh ) ) {
81 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
83 throw new MWException(
'Too many jpeg segments. Aborting' );
85 while (
$buffer !==
"\xFF" && !feof( $fh ) ) {
92 while (
$buffer ===
"\xFF" && !feof( $fh ) ) {
100 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
101 UtfNormal\Validator::quickIsNFCVerify( $com );
104 if ( $com !== $oldCom ) {
105 Wikimedia\suppressWarnings();
106 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
107 Wikimedia\restoreWarnings();
111 UtfNormal\Validator::quickIsNFCVerify( $com );
112 if ( $com === $oldCom ) {
113 $segments[
"COM"][] = $oldCom;
115 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage.\n" );
117 } elseif (
$buffer ===
"\xE1" ) {
122 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
124 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
125 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
127 $segments[
"XMP_ext"][] = trim( substr( $temp, 35 ) );
128 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
133 $segments[
"XMP"] = trim( substr( $temp, 29 ) );
134 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
135 .
"Using anyways.\n" );
136 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
140 $byteOrderMarker = substr( $temp, 6, 2 );
141 if ( $byteOrderMarker ===
'MM' ) {
142 $segments[
'byteOrder'] =
'BE';
143 } elseif ( $byteOrderMarker ===
'II' ) {
144 $segments[
'byteOrder'] =
'LE';
146 wfDebug( __METHOD__ .
" Invalid byte ordering?!\n" );
149 } elseif (
$buffer ===
"\xED" ) {
152 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
153 $segments[
"PSIR"][] = $temp;
160 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
161 if ( $size[
'int'] < 2 ) {
162 throw new MWException(
"invalid marker size in jpeg" );
166 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
170 throw new MWException(
"Reached end of jpeg file unexpectedly" );
210 public static function doPSIR( $app13 ) {
221 $appLen = strlen( $app13 );
226 while ( $offset + 12 <= $appLen ) {
228 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
235 $id = substr( $app13, $offset, 2 );
246 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
248 if ( $lenName % 2 == 1 ) {
254 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
258 if ( $lenData[
'len'] < 0 ) {
259 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
265 if ( $lenData[
'len'] + $offset > $appLen ) {
266 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
267 .
"; offset=$offset; total length=$appLen)" );
274 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
277 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
284 if ( $lenData[
'len'] % 2 == 1 ) {
287 $offset += $lenData[
'len'];
290 if ( !$realHash || !$recordedHash ) {
291 return 'iptc-no-hash';
292 } elseif ( $realHash === $recordedHash ) {
293 return 'iptc-good-hash';
295 return 'iptc-bad-hash';