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 ) {
125 $segments[
"XMP"] = substr( $temp, 29 );
126 } elseif ( substr( $temp, 0, 35 ) ===
"http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
127 $segments[
"XMP_ext"][] = substr( $temp, 35 );
128 } elseif ( substr( $temp, 0, 29 ) ===
"XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
132 $segments[
"XMP"] = substr( $temp, 29 );
133 wfDebug( __METHOD__ .
' Found XMP section with wrong app identifier '
134 .
"Using anyways.\n" );
135 } elseif ( substr( $temp, 0, 6 ) ===
"Exif\0\0" ) {
139 $byteOrderMarker = substr( $temp, 6, 2 );
140 if ( $byteOrderMarker ===
'MM' ) {
141 $segments[
'byteOrder'] =
'BE';
142 } elseif ( $byteOrderMarker ===
'II' ) {
143 $segments[
'byteOrder'] =
'LE';
145 wfDebug( __METHOD__ .
" Invalid byte ordering?!\n" );
148 } elseif (
$buffer ===
"\xED" ) {
151 if ( substr( $temp, 0, 14 ) ===
"Photoshop 3.0\x00" ) {
152 $segments[
"PSIR"][] = $temp;
159 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
160 if ( $size[
'int'] < 2 ) {
161 throw new MWException(
"invalid marker size in jpeg" );
165 fseek( $fh, $size[
'int'] - 2, SEEK_CUR );
169 throw new MWException(
"Reached end of jpeg file unexpectedly" );
179 $size =
wfUnpack(
"nint", fread( $fh, 2 ), 2 );
180 if ( $size[
'int'] < 2 ) {
181 throw new MWException(
"invalid marker size in jpeg" );
183 if ( $size[
'int'] === 2 ) {
187 $segment = fread( $fh, $size[
'int'] - 2 );
188 if ( strlen( $segment ) !== $size[
'int'] - 2 ) {
189 throw new MWException(
"Segment shorter than expected" );
209 public static function doPSIR( $app13 ) {
220 $appLen = strlen( $app13 );
225 while ( $offset + 12 <= $appLen ) {
227 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
234 $id = substr( $app13, $offset, 2 );
245 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
247 if ( $lenName % 2 == 1 ) {
253 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
257 if ( $lenData[
'len'] < 0 ) {
258 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
264 if ( $lenData[
'len'] + $offset > $appLen ) {
265 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
266 .
"; offset=$offset; total length=$appLen)" );
273 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
276 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
283 if ( $lenData[
'len'] % 2 == 1 ) {
286 $offset += $lenData[
'len'];
289 if ( !$realHash || !$recordedHash ) {
290 return 'iptc-no-hash';
291 } elseif ( $realHash === $recordedHash ) {
292 return 'iptc-good-hash';
294 return 'iptc-bad-hash';