MediaWiki  REL1_31
JpegMetadataExtractor.php
Go to the documentation of this file.
1 <?php
33  const MAX_JPEG_SEGMENTS = 200;
34 
35  // the max segment is a sanity check.
36  // A jpeg file should never even remotely have
37  // that many segments. Your average file has about 10.
38 
50  static function segmentSplitter( $filename ) {
51  $showXMP = XMPReader::isSupported();
52 
53  $segmentCount = 0;
54 
55  $segments = [
56  'XMP_ext' => [],
57  'COM' => [],
58  'PSIR' => [],
59  ];
60 
61  if ( !$filename ) {
62  throw new MWException( "No filename specified for " . __METHOD__ );
63  }
64  if ( !file_exists( $filename ) || is_dir( $filename ) ) {
65  throw new MWException( "Invalid file $filename passed to " . __METHOD__ );
66  }
67 
68  $fh = fopen( $filename, "rb" );
69 
70  if ( !$fh ) {
71  throw new MWException( "Could not open file $filename" );
72  }
73 
74  $buffer = fread( $fh, 2 );
75  if ( $buffer !== "\xFF\xD8" ) {
76  throw new MWException( "Not a jpeg, no SOI" );
77  }
78  while ( !feof( $fh ) ) {
79  $buffer = fread( $fh, 1 );
80  $segmentCount++;
81  if ( $segmentCount > self::MAX_JPEG_SEGMENTS ) {
82  // this is just a sanity check
83  throw new MWException( 'Too many jpeg segments. Aborting' );
84  }
85  while ( $buffer !== "\xFF" && !feof( $fh ) ) {
86  // In theory JPEG files are not allowed to contain anything between the sections,
87  // but in practice they sometimes do. It's customary to ignore the garbage data.
88  $buffer = fread( $fh, 1 );
89  }
90 
91  $buffer = fread( $fh, 1 );
92  while ( $buffer === "\xFF" && !feof( $fh ) ) {
93  // Skip through any 0xFF padding bytes.
94  $buffer = fread( $fh, 1 );
95  }
96  if ( $buffer === "\xFE" ) {
97  // COM section -- file comment
98  // First see if valid utf-8,
99  // if not try to convert it to windows-1252.
100  $com = $oldCom = trim( self::jpegExtractMarker( $fh ) );
101  UtfNormal\Validator::quickIsNFCVerify( $com );
102  // turns $com to valid utf-8.
103  // thus if no change, its utf-8, otherwise its something else.
104  if ( $com !== $oldCom ) {
105  Wikimedia\suppressWarnings();
106  $com = $oldCom = iconv( 'windows-1252', 'UTF-8//IGNORE', $oldCom );
107  Wikimedia\restoreWarnings();
108  }
109  // Try it again, if its still not a valid string, then probably
110  // binary junk or some really weird encoding, so don't extract.
111  UtfNormal\Validator::quickIsNFCVerify( $com );
112  if ( $com === $oldCom ) {
113  $segments["COM"][] = $oldCom;
114  } else {
115  wfDebug( __METHOD__ . " Ignoring JPEG comment as is garbage.\n" );
116  }
117  } elseif ( $buffer === "\xE1" ) {
118  // APP1 section (Exif, XMP, and XMP extended)
119  // only extract if XMP is enabled.
120  $temp = self::jpegExtractMarker( $fh );
121  // check what type of app segment this is.
122  if ( substr( $temp, 0, 29 ) === "http://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
123  // use trim to remove trailing \0 chars
124  $segments["XMP"] = trim( substr( $temp, 29 ) );
125  } elseif ( substr( $temp, 0, 35 ) === "http://ns.adobe.com/xmp/extension/\x00" && $showXMP ) {
126  // use trim to remove trailing \0 chars
127  $segments["XMP_ext"][] = trim( substr( $temp, 35 ) );
128  } elseif ( substr( $temp, 0, 29 ) === "XMP\x00://ns.adobe.com/xap/1.0/\x00" && $showXMP ) {
129  // Some images (especially flickr images) seem to have this.
130  // I really have no idea what the deal is with them, but
131  // whatever...
132  // use trim to remove trailing \0 chars
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" ) {
137  // Just need to find out what the byte order is.
138  // because php's exif plugin sucks...
139  // This is a II for little Endian, MM for big. Not a unicode BOM.
140  $byteOrderMarker = substr( $temp, 6, 2 );
141  if ( $byteOrderMarker === 'MM' ) {
142  $segments['byteOrder'] = 'BE';
143  } elseif ( $byteOrderMarker === 'II' ) {
144  $segments['byteOrder'] = 'LE';
145  } else {
146  wfDebug( __METHOD__ . " Invalid byte ordering?!\n" );
147  }
148  }
149  } elseif ( $buffer === "\xED" ) {
150  // APP13 - PSIR. IPTC and some photoshop stuff
151  $temp = self::jpegExtractMarker( $fh );
152  if ( substr( $temp, 0, 14 ) === "Photoshop 3.0\x00" ) {
153  $segments["PSIR"][] = $temp;
154  }
155  } elseif ( $buffer === "\xD9" || $buffer === "\xDA" ) {
156  // EOI - end of image or SOS - start of scan. either way we're past any interesting segments
157  return $segments;
158  } else {
159  // segment we don't care about, so skip
160  $size = wfUnpack( "nint", fread( $fh, 2 ), 2 );
161  if ( $size['int'] < 2 ) {
162  throw new MWException( "invalid marker size in jpeg" );
163  }
164  // Note it's possible to seek beyond end of file if truncated.
165  // fseek doesn't report a failure in this case.
166  fseek( $fh, $size['int'] - 2, SEEK_CUR );
167  }
168  }
169  // shouldn't get here.
170  throw new MWException( "Reached end of jpeg file unexpectedly" );
171  }
172 
179  private static function jpegExtractMarker( &$fh ) {
180  $size = wfUnpack( "nint", fread( $fh, 2 ), 2 );
181  if ( $size['int'] < 2 ) {
182  throw new MWException( "invalid marker size in jpeg" );
183  }
184  if ( $size['int'] === 2 ) {
185  // fread( ..., 0 ) generates a warning
186  return '';
187  }
188  $segment = fread( $fh, $size['int'] - 2 );
189  if ( strlen( $segment ) !== $size['int'] - 2 ) {
190  throw new MWException( "Segment shorter than expected" );
191  }
192 
193  return $segment;
194  }
195 
210  public static function doPSIR( $app13 ) {
211  if ( !$app13 ) {
212  throw new MWException( "No App13 segment given" );
213  }
214  // First compare hash with real thing
215  // 0x404 contains IPTC, 0x425 has hash
216  // This is used to determine if the iptc is newer than
217  // the xmp data, as xmp programs update the hash,
218  // where non-xmp programs don't.
219 
220  $offset = 14; // skip past PHOTOSHOP 3.0 identifier. should already be checked.
221  $appLen = strlen( $app13 );
222  $realHash = "";
223  $recordedHash = "";
224 
225  // the +12 is the length of an empty item.
226  while ( $offset + 12 <= $appLen ) {
227  $valid = true;
228  if ( substr( $app13, $offset, 4 ) !== '8BIM' ) {
229  // its supposed to be 8BIM
230  // but apparently sometimes isn't esp. in
231  // really old jpg's
232  $valid = false;
233  }
234  $offset += 4;
235  $id = substr( $app13, $offset, 2 );
236  // id is a 2 byte id number which identifies
237  // the piece of info this record contains.
238 
239  $offset += 2;
240 
241  // some record types can contain a name, which
242  // is a pascal string 0-padded to be an even
243  // number of bytes. Most times (and any time
244  // we care) this is empty, making it two null bytes.
245 
246  $lenName = ord( substr( $app13, $offset, 1 ) ) + 1;
247  // we never use the name so skip it. +1 for length byte
248  if ( $lenName % 2 == 1 ) {
249  $lenName++;
250  } // pad to even.
251  $offset += $lenName;
252 
253  // now length of data (unsigned long big endian)
254  $lenData = wfUnpack( 'Nlen', substr( $app13, $offset, 4 ), 4 );
255  // PHP can take issue with very large unsigned ints and make them negative.
256  // Which should never ever happen, as this has to be inside a segment
257  // which is limited to a 16 bit number.
258  if ( $lenData['len'] < 0 ) {
259  throw new MWException( "Too big PSIR (" . $lenData['len'] . ')' );
260  }
261 
262  $offset += 4; // 4bytes length field;
263 
264  // this should not happen, but check.
265  if ( $lenData['len'] + $offset > $appLen ) {
266  throw new MWException( "PSIR data too long. (item length=" . $lenData['len']
267  . "; offset=$offset; total length=$appLen)" );
268  }
269 
270  if ( $valid ) {
271  switch ( $id ) {
272  case "\x04\x04":
273  // IPTC block
274  $realHash = md5( substr( $app13, $offset, $lenData['len'] ), true );
275  break;
276  case "\x04\x25":
277  $recordedHash = substr( $app13, $offset, $lenData['len'] );
278  break;
279  }
280  }
281 
282  // if odd, add 1 to length to account for
283  // null pad byte.
284  if ( $lenData['len'] % 2 == 1 ) {
285  $lenData['len']++;
286  }
287  $offset += $lenData['len'];
288  }
289 
290  if ( !$realHash || !$recordedHash ) {
291  return 'iptc-no-hash';
292  } elseif ( $realHash === $recordedHash ) {
293  return 'iptc-good-hash';
294  } else { /*$realHash !== $recordedHash */
295  return 'iptc-bad-hash';
296  }
297  }
298 }
JpegMetadataExtractor
Class for reading jpegs and extracting metadata.
Definition: JpegMetadataExtractor.php:32
JpegMetadataExtractor\jpegExtractMarker
static jpegExtractMarker(&$fh)
Helper function for jpegSegmentSplitter.
Definition: JpegMetadataExtractor.php:179
wfUnpack
wfUnpack( $format, $data, $length=false)
Wrapper around php's unpack.
Definition: GlobalFunctions.php:3202
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
MWException
MediaWiki exception.
Definition: MWException.php:26
JpegMetadataExtractor\doPSIR
static doPSIR( $app13)
This reads the photoshop image resource.
Definition: JpegMetadataExtractor.php:210
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:994
XMPReader\isSupported
static isSupported()
Check if this instance supports using this class.
Definition: XMP.php:202
JpegMetadataExtractor\segmentSplitter
static segmentSplitter( $filename)
Function to extract metadata segments of interest from jpeg files based on GIFMetadataExtractor.
Definition: JpegMetadataExtractor.php:50
JpegMetadataExtractor\MAX_JPEG_SEGMENTS
const MAX_JPEG_SEGMENTS
Definition: JpegMetadataExtractor.php:33
$buffer
$buffer
Definition: mwdoc-filter.php:49