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 ) ) {
100 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
101 UtfNormal\Validator::quickIsNFCVerify( $com );
104 if ( $com !== $oldCom ) {
105 MediaWiki\suppressWarnings();
106 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
107 MediaWiki\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 ) {
123 $segments[
"XMP"] = substr( $temp, 29 );
124 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
125 $segments[
"XMP_ext"][] = substr( $temp, 35 );
126 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
130 $segments[
"XMP"] = substr( $temp, 29 );
131 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
132 .
"Using anyways.\n" );
133 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
137 $byteOrderMarker = substr( $temp, 6, 2 );
138 if ( $byteOrderMarker ===
'MM' ) {
139 $segments[
'byteOrder'] =
'BE';
140 } elseif ( $byteOrderMarker ===
'II' ) {
141 $segments[
'byteOrder'] =
'LE';
143 wfDebug( __METHOD__ .
" Invalid byte ordering?!\n" );
146 } elseif (
$buffer ===
"\xED" ) {
149 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
150 $segments[
"PSIR"][] = $temp;
157 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
158 if ( $size[
'int'] < 2 ) {
159 throw new MWException(
"invalid marker size in jpeg" );
161 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
165 throw new MWException(
"Reached end of jpeg file unexpectedly" );
175 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
176 if ( $size[
'int'] < 2 ) {
177 throw new MWException(
"invalid marker size in jpeg" );
179 if ( $size[
'int'] === 2 ) {
183 $segment = fread( $fh, $size[
'int'] - 2 );
184 if ( strlen( $segment ) !== $size[
'int'] - 2 ) {
185 throw new MWException(
"Segment shorter than expected" );
205 public static function doPSIR( $app13 ) {
216 $appLen = strlen( $app13 );
221 while ( $offset + 12 <= $appLen ) {
223 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
230 $id = substr( $app13, $offset, 2 );
241 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
243 if ( $lenName % 2 == 1 ) {
249 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
253 if ( $lenData[
'len'] < 0 ) {
254 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
260 if ( $lenData[
'len'] + $offset > $appLen ) {
261 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
262 .
"; offset=$offset; total length=$appLen)" );
269 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
272 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
279 if ( $lenData[
'len'] % 2 == 1 ) {
282 $offset += $lenData[
'len'];
285 if ( !$realHash || !$recordedHash ) {
286 return 'iptc-no-hash';
287 } elseif ( $realHash === $recordedHash ) {
288 return 'iptc-good-hash';
290 return 'iptc-bad-hash';