51 $showXMP = function_exists(
'xml_parser_create_ns' );
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" );
74 $buffer = fread( $fh, 2 );
75 if ( $buffer !==
"\xFF\xD8" ) {
78 while ( !feof( $fh ) ) {
79 $buffer = fread( $fh, 1 );
81 if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
83 throw new MWException(
'Too many jpeg segments. Aborting' );
85 if ( $buffer !==
"\xFF" ) {
86 throw new MWException(
"Error reading jpeg file marker. " .
87 "Expected 0xFF but got " . bin2hex( $buffer ) );
90 $buffer = fread( $fh, 1 );
91 while ( $buffer ===
"\xFF" && !feof( $fh ) ) {
93 $buffer = fread( $fh, 1 );
95 if ( $buffer ===
"\xFE" ) {
100 $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
104 if ( $com !== $oldCom ) {
106 $com = $oldCom = iconv(
'windows-1252',
'UTF-8//IGNORE', $oldCom );
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;
152 } elseif ( $buffer ===
"\xD9" || $buffer ===
"\xDA" ) {
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" );
176 if (
$size[
'int'] <= 2 ) {
177 throw new MWException(
"invalid marker size in jpeg" );
179 $segment = fread( $fh,
$size[
'int'] - 2 );
180 if ( strlen( $segment ) !==
$size[
'int'] - 2 ) {
181 throw new MWException(
"Segment shorter than expected" );
201 public static function doPSIR( $app13 ) {
212 $appLen = strlen( $app13 );
217 while ( $offset + 12 <= $appLen ) {
219 if ( substr( $app13, $offset, 4 ) !==
'8BIM' ) {
226 $id = substr( $app13, $offset, 2 );
237 $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
239 if ( $lenName % 2 == 1 ) {
245 $lenData =
wfUnpack(
'Nlen', substr( $app13, $offset, 4 ), 4 );
249 if ( $lenData[
'len'] < 0 ) {
250 throw new MWException(
"Too big PSIR (" . $lenData[
'len'] .
')' );
256 if ( $lenData[
'len'] + $offset > $appLen ) {
257 throw new MWException(
"PSIR data too long. (item length=" . $lenData[
'len']
258 .
"; offset=$offset; total length=$appLen)" );
265 $realHash = md5( substr( $app13, $offset, $lenData[
'len'] ),
true );
268 $recordedHash = substr( $app13, $offset, $lenData[
'len'] );
275 if ( $lenData[
'len'] % 2 == 1 ) {
278 $offset += $lenData[
'len'];
281 if ( !$realHash || !$recordedHash ) {
282 return 'iptc-no-hash';
283 } elseif ( $realHash === $recordedHash ) {
284 return 'iptc-good-hash';
286 return 'iptc-bad-hash';