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' );
92 while (
$buffer ===
"\xFF" && !feof( $fh ) ) {
101 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
102 UtfNormal\Validator::quickIsNFCVerify( $com );
105 if ( $com !== $oldCom ) {
106 MediaWiki\suppressWarnings();
107 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
108 MediaWiki\restoreWarnings();
112 UtfNormal\Validator::quickIsNFCVerify( $com );
113 if ( $com === $oldCom ) {
114 $segments[
"COM"][] = $oldCom;
116 wfDebug( __METHOD__ .
" Ignoring JPEG comment as is garbage.\n" );
118 } elseif (
$buffer ===
"\xE1" ) {
123 if ( substr( $temp, 0, 29 ) ===
"http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
124 $segments[
"XMP"] = substr( $temp, 29 );
125 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
126 $segments[
"XMP_ext"][] = substr( $temp, 35 );
127 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
131 $segments[
"XMP"] = substr( $temp, 29 );
132 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
133 .
"Using anyways.\n" );
134 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
138 $byteOrderMarker = substr( $temp, 6, 2 );
139 if ( $byteOrderMarker ===
'MM' ) {
140 $segments[
'byteOrder'] =
'BE';
141 } elseif ( $byteOrderMarker ===
'II' ) {
142 $segments[
'byteOrder'] =
'LE';
144 wfDebug( __METHOD__ .
" Invalid byte ordering?!\n" );
147 } elseif (
$buffer ===
"\xED" ) {
150 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
151 $segments[
"PSIR"][] = $temp;
158 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
159 if ( $size[
'int'] < 2 ) {
160 throw new MWException(
"invalid marker size in jpeg" );
162 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
166 throw new MWException(
"Reached end of jpeg file unexpectedly" );
176 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
177 if ( $size[
'int'] < 2 ) {
178 throw new MWException(
"invalid marker size in jpeg" );
180 if ( $size[
'int'] === 2 ) {
184 $segment = fread( $fh, $size[
'int'] - 2 );
185 if ( strlen( $segment ) !== $size[
'int'] - 2 ) {
186 throw new MWException(
"Segment shorter than expected" );
206 public static function doPSIR( $app13 ) {
217 $appLen = strlen( $app13 );
222 while ( $offset + 12 <= $appLen ) {
224 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
231 $id = substr( $app13, $offset, 2 );
242 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
244 if ( $lenName % 2 == 1 ) {
250 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
254 if ( $lenData[
'len'] < 0 ) {
255 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
261 if ( $lenData[
'len'] + $offset > $appLen ) {
262 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
263 .
"; offset=$offset; total length=$appLen)" );
270 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
273 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
280 if ( $lenData[
'len'] % 2 == 1 ) {
283 $offset += $lenData[
'len'];
286 if ( !$realHash || !$recordedHash ) {
287 return 'iptc-no-hash';
288 } elseif ( $realHash === $recordedHash ) {
289 return 'iptc-good-hash';
291 return 'iptc-bad-hash';