Store original media dimensions as additional header
[lhc/web/wiklou.git] / includes / media / Exif.php
1 <?php
2 /**
3 * Extraction and validation of image metadata.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Media
21 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
22 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason, 2009 Brent Garber
23 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
25 * @file
26 */
27
28 /**
29 * Class to extract and validate Exif data from jpeg (and possibly tiff) files.
30 * @ingroup Media
31 */
32 class Exif {
33 /** An 8-bit (1-byte) unsigned integer. */
34 const BYTE = 1;
35
36 /** An 8-bit byte containing one 7-bit ASCII code.
37 * The final byte is terminated with NULL.
38 */
39 const ASCII = 2;
40
41 /** A 16-bit (2-byte) unsigned integer. */
42 const SHORT = 3;
43
44 /** A 32-bit (4-byte) unsigned integer. */
45 const LONG = 4;
46
47 /** Two LONGs. The first LONG is the numerator and the second LONG expresses
48 * the denominator
49 */
50 const RATIONAL = 5;
51
52 /** A 16-bit (2-byte) or 32-bit (4-byte) unsigned integer. */
53 const SHORT_OR_LONG = 6;
54
55 /** An 8-bit byte that can take any value depending on the field definition */
56 const UNDEFINED = 7;
57
58 /** A 32-bit (4-byte) signed integer (2's complement notation), */
59 const SLONG = 9;
60
61 /** Two SLONGs. The first SLONG is the numerator and the second SLONG is
62 * the denominator.
63 */
64 const SRATIONAL = 10;
65
66 /** A fake value for things we don't want or don't support. */
67 const IGNORE = -1;
68
69 /** @var array Exif tags grouped by category, the tagname itself is the key
70 * and the type is the value, in the case of more than one possible value
71 * type they are separated by commas.
72 */
73 private $mExifTags;
74
75 /** @var array The raw Exif data returned by exif_read_data() */
76 private $mRawExifData;
77
78 /** @var array A Filtered version of $mRawExifData that has been pruned
79 * of invalid tags and tags that contain content they shouldn't contain
80 * according to the Exif specification
81 */
82 private $mFilteredExifData;
83
84 /** @var string The file being processed */
85 private $file;
86
87 /** @var string The basename of the file being processed */
88 private $basename;
89
90 /** @var string The private log to log to, e.g. 'exif' */
91 private $log = false;
92
93 /** @var string The byte order of the file. Needed because php's extension
94 * doesn't fully process some obscure props.
95 */
96 private $byteOrder;
97
98 /**
99 * Constructor
100 *
101 * @param string $file Filename.
102 * @param string $byteOrder Type of byte ordering either 'BE' (Big Endian)
103 * or 'LE' (Little Endian). Default ''.
104 * @throws MWException
105 * @todo FIXME: The following are broke:
106 * SubjectArea. Need to test the more obscure tags.
107 * DigitalZoomRatio = 0/0 is rejected. need to determine if that's valid.
108 * Possibly should treat 0/0 = 0. need to read exif spec on that.
109 */
110 function __construct( $file, $byteOrder = '' ) {
111 /**
112 * Page numbers here refer to pages in the Exif 2.2 standard
113 *
114 * Note, Exif::UNDEFINED is treated as a string, not as an array of bytes
115 * so don't put a count parameter for any UNDEFINED values.
116 *
117 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
118 */
119 $this->mExifTags = [
120 'COMPUTED' => [
121 'Width' => Exif::SHORT_OR_LONG, # Image width
122 'Height' => Exif::SHORT_OR_LONG, # Image height
123 ],
124
125 # TIFF Rev. 6.0 Attribute Information (p22)
126 'IFD0' => [
127 # Tags relating to image structure
128 'ImageWidth' => Exif::SHORT_OR_LONG, # Image width
129 'ImageLength' => Exif::SHORT_OR_LONG, # Image height
130 'BitsPerSample' => [ Exif::SHORT, 3 ], # Number of bits per component
131 # "When a primary image is JPEG compressed, this designation is not"
132 # "necessary and is omitted." (p23)
133 'Compression' => Exif::SHORT, # Compression scheme #p23
134 'PhotometricInterpretation' => Exif::SHORT, # Pixel composition #p23
135 'Orientation' => Exif::SHORT, # Orientation of image #p24
136 'SamplesPerPixel' => Exif::SHORT, # Number of components
137 'PlanarConfiguration' => Exif::SHORT, # Image data arrangement #p24
138 'YCbCrSubSampling' => [ Exif::SHORT, 2 ], # Subsampling ratio of Y to C #p24
139 'YCbCrPositioning' => Exif::SHORT, # Y and C positioning #p24-25
140 'XResolution' => Exif::RATIONAL, # Image resolution in width direction
141 'YResolution' => Exif::RATIONAL, # Image resolution in height direction
142 'ResolutionUnit' => Exif::SHORT, # Unit of X and Y resolution #(p26)
143
144 # Tags relating to recording offset
145 'StripOffsets' => Exif::SHORT_OR_LONG, # Image data location
146 'RowsPerStrip' => Exif::SHORT_OR_LONG, # Number of rows per strip
147 'StripByteCounts' => Exif::SHORT_OR_LONG, # Bytes per compressed strip
148 'JPEGInterchangeFormat' => Exif::SHORT_OR_LONG, # Offset to JPEG SOI
149 'JPEGInterchangeFormatLength' => Exif::SHORT_OR_LONG, # Bytes of JPEG data
150
151 # Tags relating to image data characteristics
152 'TransferFunction' => Exif::IGNORE, # Transfer function
153 'WhitePoint' => [ Exif::RATIONAL, 2 ], # White point chromaticity
154 'PrimaryChromaticities' => [ Exif::RATIONAL, 6 ], # Chromaticities of primarities
155 # Color space transformation matrix coefficients #p27
156 'YCbCrCoefficients' => [ Exif::RATIONAL, 3 ],
157 'ReferenceBlackWhite' => [ Exif::RATIONAL, 6 ], # Pair of black and white reference values
158
159 # Other tags
160 'DateTime' => Exif::ASCII, # File change date and time
161 'ImageDescription' => Exif::ASCII, # Image title
162 'Make' => Exif::ASCII, # Image input equipment manufacturer
163 'Model' => Exif::ASCII, # Image input equipment model
164 'Software' => Exif::ASCII, # Software used
165 'Artist' => Exif::ASCII, # Person who created the image
166 'Copyright' => Exif::ASCII, # Copyright holder
167 ],
168
169 # Exif IFD Attribute Information (p30-31)
170 'EXIF' => [
171 # @todo NOTE: Nonexistence of this field is taken to mean nonconformance
172 # to the Exif 2.1 AND 2.2 standards
173 'ExifVersion' => Exif::UNDEFINED, # Exif version
174 'FlashPixVersion' => Exif::UNDEFINED, # Supported Flashpix version #p32
175
176 # Tags relating to Image Data Characteristics
177 'ColorSpace' => Exif::SHORT, # Color space information #p32
178
179 # Tags relating to image configuration
180 'ComponentsConfiguration' => Exif::UNDEFINED, # Meaning of each component #p33
181 'CompressedBitsPerPixel' => Exif::RATIONAL, # Image compression mode
182 'PixelYDimension' => Exif::SHORT_OR_LONG, # Valid image height
183 'PixelXDimension' => Exif::SHORT_OR_LONG, # Valid image width
184
185 # Tags relating to related user information
186 'MakerNote' => Exif::IGNORE, # Manufacturer notes
187 'UserComment' => Exif::UNDEFINED, # User comments #p34
188
189 # Tags relating to related file information
190 'RelatedSoundFile' => Exif::ASCII, # Related audio file
191
192 # Tags relating to date and time
193 'DateTimeOriginal' => Exif::ASCII, # Date and time of original data generation #p36
194 'DateTimeDigitized' => Exif::ASCII, # Date and time of original data generation
195 'SubSecTime' => Exif::ASCII, # DateTime subseconds
196 'SubSecTimeOriginal' => Exif::ASCII, # DateTimeOriginal subseconds
197 'SubSecTimeDigitized' => Exif::ASCII, # DateTimeDigitized subseconds
198
199 # Tags relating to picture-taking conditions (p31)
200 'ExposureTime' => Exif::RATIONAL, # Exposure time
201 'FNumber' => Exif::RATIONAL, # F Number
202 'ExposureProgram' => Exif::SHORT, # Exposure Program #p38
203 'SpectralSensitivity' => Exif::ASCII, # Spectral sensitivity
204 'ISOSpeedRatings' => Exif::SHORT, # ISO speed rating
205 'OECF' => Exif::IGNORE,
206 # Optoelectronic conversion factor. Note: We don't have support for this atm.
207 'ShutterSpeedValue' => Exif::SRATIONAL, # Shutter speed
208 'ApertureValue' => Exif::RATIONAL, # Aperture
209 'BrightnessValue' => Exif::SRATIONAL, # Brightness
210 'ExposureBiasValue' => Exif::SRATIONAL, # Exposure bias
211 'MaxApertureValue' => Exif::RATIONAL, # Maximum land aperture
212 'SubjectDistance' => Exif::RATIONAL, # Subject distance
213 'MeteringMode' => Exif::SHORT, # Metering mode #p40
214 'LightSource' => Exif::SHORT, # Light source #p40-41
215 'Flash' => Exif::SHORT, # Flash #p41-42
216 'FocalLength' => Exif::RATIONAL, # Lens focal length
217 'SubjectArea' => [ Exif::SHORT, 4 ], # Subject area
218 'FlashEnergy' => Exif::RATIONAL, # Flash energy
219 'SpatialFrequencyResponse' => Exif::IGNORE, # Spatial frequency response. Not supported atm.
220 'FocalPlaneXResolution' => Exif::RATIONAL, # Focal plane X resolution
221 'FocalPlaneYResolution' => Exif::RATIONAL, # Focal plane Y resolution
222 'FocalPlaneResolutionUnit' => Exif::SHORT, # Focal plane resolution unit #p46
223 'SubjectLocation' => [ Exif::SHORT, 2 ], # Subject location
224 'ExposureIndex' => Exif::RATIONAL, # Exposure index
225 'SensingMethod' => Exif::SHORT, # Sensing method #p46
226 'FileSource' => Exif::UNDEFINED, # File source #p47
227 'SceneType' => Exif::UNDEFINED, # Scene type #p47
228 'CFAPattern' => Exif::IGNORE, # CFA pattern. not supported atm.
229 'CustomRendered' => Exif::SHORT, # Custom image processing #p48
230 'ExposureMode' => Exif::SHORT, # Exposure mode #p48
231 'WhiteBalance' => Exif::SHORT, # White Balance #p49
232 'DigitalZoomRatio' => Exif::RATIONAL, # Digital zoom ration
233 'FocalLengthIn35mmFilm' => Exif::SHORT, # Focal length in 35 mm film
234 'SceneCaptureType' => Exif::SHORT, # Scene capture type #p49
235 'GainControl' => Exif::SHORT, # Scene control #p49-50
236 'Contrast' => Exif::SHORT, # Contrast #p50
237 'Saturation' => Exif::SHORT, # Saturation #p50
238 'Sharpness' => Exif::SHORT, # Sharpness #p50
239 'DeviceSettingDescription' => Exif::IGNORE,
240 # Device settings description. This could maybe be supported. Need to find an
241 # example file that uses this to see if it has stuff of interest in it.
242 'SubjectDistanceRange' => Exif::SHORT, # Subject distance range #p51
243
244 'ImageUniqueID' => Exif::ASCII, # Unique image ID
245 ],
246
247 # GPS Attribute Information (p52)
248 'GPS' => [
249 'GPSVersion' => Exif::UNDEFINED,
250 # Should be an array of 4 Exif::BYTE's. However php treats it as an undefined
251 # Note exif standard calls this GPSVersionID, but php doesn't like the id suffix
252 'GPSLatitudeRef' => Exif::ASCII, # North or South Latitude #p52-53
253 'GPSLatitude' => [ Exif::RATIONAL, 3 ], # Latitude
254 'GPSLongitudeRef' => Exif::ASCII, # East or West Longitude #p53
255 'GPSLongitude' => [ Exif::RATIONAL, 3 ], # Longitude
256 'GPSAltitudeRef' => Exif::UNDEFINED,
257 # Altitude reference. Note, the exif standard says this should be an EXIF::Byte,
258 # but php seems to disagree.
259 'GPSAltitude' => Exif::RATIONAL, # Altitude
260 'GPSTimeStamp' => [ Exif::RATIONAL, 3 ], # GPS time (atomic clock)
261 'GPSSatellites' => Exif::ASCII, # Satellites used for measurement
262 'GPSStatus' => Exif::ASCII, # Receiver status #p54
263 'GPSMeasureMode' => Exif::ASCII, # Measurement mode #p54-55
264 'GPSDOP' => Exif::RATIONAL, # Measurement precision
265 'GPSSpeedRef' => Exif::ASCII, # Speed unit #p55
266 'GPSSpeed' => Exif::RATIONAL, # Speed of GPS receiver
267 'GPSTrackRef' => Exif::ASCII, # Reference for direction of movement #p55
268 'GPSTrack' => Exif::RATIONAL, # Direction of movement
269 'GPSImgDirectionRef' => Exif::ASCII, # Reference for direction of image #p56
270 'GPSImgDirection' => Exif::RATIONAL, # Direction of image
271 'GPSMapDatum' => Exif::ASCII, # Geodetic survey data used
272 'GPSDestLatitudeRef' => Exif::ASCII, # Reference for latitude of destination #p56
273 'GPSDestLatitude' => [ Exif::RATIONAL, 3 ], # Latitude destination
274 'GPSDestLongitudeRef' => Exif::ASCII, # Reference for longitude of destination #p57
275 'GPSDestLongitude' => [ Exif::RATIONAL, 3 ], # Longitude of destination
276 'GPSDestBearingRef' => Exif::ASCII, # Reference for bearing of destination #p57
277 'GPSDestBearing' => Exif::RATIONAL, # Bearing of destination
278 'GPSDestDistanceRef' => Exif::ASCII, # Reference for distance to destination #p57-58
279 'GPSDestDistance' => Exif::RATIONAL, # Distance to destination
280 'GPSProcessingMethod' => Exif::UNDEFINED, # Name of GPS processing method
281 'GPSAreaInformation' => Exif::UNDEFINED, # Name of GPS area
282 'GPSDateStamp' => Exif::ASCII, # GPS date
283 'GPSDifferential' => Exif::SHORT, # GPS differential correction
284 ],
285 ];
286
287 $this->file = $file;
288 $this->basename = wfBaseName( $this->file );
289 if ( $byteOrder === 'BE' || $byteOrder === 'LE' ) {
290 $this->byteOrder = $byteOrder;
291 } else {
292 // Only give a warning for b/c, since originally we didn't
293 // require this. The number of things affected by this is
294 // rather small.
295 wfWarn( 'Exif class did not have byte order specified. ' .
296 'Some properties may be decoded incorrectly.' );
297 $this->byteOrder = 'BE'; // BE seems about twice as popular as LE in jpg's.
298 }
299
300 $this->debugFile( $this->basename, __FUNCTION__, true );
301 if ( function_exists( 'exif_read_data' ) ) {
302 MediaWiki\suppressWarnings();
303 $data = exif_read_data( $this->file, 0, true );
304 MediaWiki\restoreWarnings();
305 } else {
306 throw new MWException( "Internal error: exif_read_data not present. " .
307 "\$wgShowEXIF may be incorrectly set or not checked by an extension." );
308 }
309 /**
310 * exif_read_data() will return false on invalid input, such as
311 * when somebody uploads a file called something.jpeg
312 * containing random gibberish.
313 */
314 $this->mRawExifData = $data ?: [];
315 $this->makeFilteredData();
316 $this->collapseData();
317 $this->debugFile( __FUNCTION__, false );
318 }
319
320 /**
321 * Make $this->mFilteredExifData
322 */
323 function makeFilteredData() {
324 $this->mFilteredExifData = [];
325
326 foreach ( array_keys( $this->mRawExifData ) as $section ) {
327 if ( !array_key_exists( $section, $this->mExifTags ) ) {
328 $this->debug( $section, __FUNCTION__, "'$section' is not a valid Exif section" );
329 continue;
330 }
331
332 foreach ( array_keys( $this->mRawExifData[$section] ) as $tag ) {
333 if ( !array_key_exists( $tag, $this->mExifTags[$section] ) ) {
334 $this->debug( $tag, __FUNCTION__, "'$tag' is not a valid tag in '$section'" );
335 continue;
336 }
337
338 $this->mFilteredExifData[$tag] = $this->mRawExifData[$section][$tag];
339 // This is ok, as the tags in the different sections do not conflict.
340 // except in computed and thumbnail section, which we don't use.
341
342 $value = $this->mRawExifData[$section][$tag];
343 if ( !$this->validate( $section, $tag, $value ) ) {
344 $this->debug( $value, __FUNCTION__, "'$tag' contained invalid data" );
345 unset( $this->mFilteredExifData[$tag] );
346 }
347 }
348 }
349 }
350
351 /**
352 * Collapse some fields together.
353 * This converts some fields from exif form, to a more friendly form.
354 * For example GPS latitude to a single number.
355 *
356 * The rationale behind this is that we're storing data, not presenting to the user
357 * For example a longitude is a single number describing how far away you are from
358 * the prime meridian. Well it might be nice to split it up into minutes and seconds
359 * for the user, it doesn't really make sense to split a single number into 4 parts
360 * for storage. (degrees, minutes, second, direction vs single floating point number).
361 *
362 * Other things this might do (not really sure if they make sense or not):
363 * Dates -> mediawiki date format.
364 * convert values that can be in different units to be in one standardized unit.
365 *
366 * As an alternative approach, some of this could be done in the validate phase
367 * if we make up our own types like Exif::DATE.
368 */
369 function collapseData() {
370
371 $this->exifGPStoNumber( 'GPSLatitude' );
372 $this->exifGPStoNumber( 'GPSDestLatitude' );
373 $this->exifGPStoNumber( 'GPSLongitude' );
374 $this->exifGPStoNumber( 'GPSDestLongitude' );
375
376 if ( isset( $this->mFilteredExifData['GPSAltitude'] )
377 && isset( $this->mFilteredExifData['GPSAltitudeRef'] )
378 ) {
379 // We know altitude data is a <num>/<denom> from the validation
380 // functions ran earlier. But multiplying such a string by -1
381 // doesn't work well, so convert.
382 list( $num, $denom ) = explode( '/', $this->mFilteredExifData['GPSAltitude'] );
383 $this->mFilteredExifData['GPSAltitude'] = $num / $denom;
384
385 if ( $this->mFilteredExifData['GPSAltitudeRef'] === "\1" ) {
386 $this->mFilteredExifData['GPSAltitude'] *= -1;
387 }
388 unset( $this->mFilteredExifData['GPSAltitudeRef'] );
389 }
390
391 $this->exifPropToOrd( 'FileSource' );
392 $this->exifPropToOrd( 'SceneType' );
393
394 $this->charCodeString( 'UserComment' );
395 $this->charCodeString( 'GPSProcessingMethod' );
396 $this->charCodeString( 'GPSAreaInformation' );
397
398 // ComponentsConfiguration should really be an array instead of a string...
399 // This turns a string of binary numbers into an array of numbers.
400
401 if ( isset( $this->mFilteredExifData['ComponentsConfiguration'] ) ) {
402 $val = $this->mFilteredExifData['ComponentsConfiguration'];
403 $ccVals = [];
404
405 $strLen = strlen( $val );
406 for ( $i = 0; $i < $strLen; $i++ ) {
407 $ccVals[$i] = ord( substr( $val, $i, 1 ) );
408 }
409 $ccVals['_type'] = 'ol'; // this is for formatting later.
410 $this->mFilteredExifData['ComponentsConfiguration'] = $ccVals;
411 }
412
413 // GPSVersion(ID) is treated as the wrong type by php exif support.
414 // Go through each byte turning it into a version string.
415 // For example: "\x02\x02\x00\x00" -> "2.2.0.0"
416
417 // Also change exif tag name from GPSVersion (what php exif thinks it is)
418 // to GPSVersionID (what the exif standard thinks it is).
419
420 if ( isset( $this->mFilteredExifData['GPSVersion'] ) ) {
421 $val = $this->mFilteredExifData['GPSVersion'];
422 $newVal = '';
423
424 $strLen = strlen( $val );
425 for ( $i = 0; $i < $strLen; $i++ ) {
426 if ( $i !== 0 ) {
427 $newVal .= '.';
428 }
429 $newVal .= ord( substr( $val, $i, 1 ) );
430 }
431
432 if ( $this->byteOrder === 'LE' ) {
433 // Need to reverse the string
434 $newVal2 = '';
435 for ( $i = strlen( $newVal ) - 1; $i >= 0; $i-- ) {
436 $newVal2 .= substr( $newVal, $i, 1 );
437 }
438 $this->mFilteredExifData['GPSVersionID'] = $newVal2;
439 } else {
440 $this->mFilteredExifData['GPSVersionID'] = $newVal;
441 }
442 unset( $this->mFilteredExifData['GPSVersion'] );
443 }
444 }
445
446 /**
447 * Do userComment tags and similar. See pg. 34 of exif standard.
448 * basically first 8 bytes is charset, rest is value.
449 * This has not been tested on any shift-JIS strings.
450 * @param string $prop Prop name
451 */
452 private function charCodeString( $prop ) {
453 if ( isset( $this->mFilteredExifData[$prop] ) ) {
454
455 if ( strlen( $this->mFilteredExifData[$prop] ) <= 8 ) {
456 // invalid. Must be at least 9 bytes long.
457
458 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, false );
459 unset( $this->mFilteredExifData[$prop] );
460
461 return;
462 }
463 $charCode = substr( $this->mFilteredExifData[$prop], 0, 8 );
464 $val = substr( $this->mFilteredExifData[$prop], 8 );
465
466 switch ( $charCode ) {
467 case "\x4A\x49\x53\x00\x00\x00\x00\x00":
468 // JIS
469 $charset = "Shift-JIS";
470 break;
471 case "UNICODE\x00":
472 $charset = "UTF-16" . $this->byteOrder;
473 break;
474 default: // ascii or undefined.
475 $charset = "";
476 break;
477 }
478 if ( $charset ) {
479 MediaWiki\suppressWarnings();
480 $val = iconv( $charset, 'UTF-8//IGNORE', $val );
481 MediaWiki\restoreWarnings();
482 } else {
483 // if valid utf-8, assume that, otherwise assume windows-1252
484 $valCopy = $val;
485 UtfNormal\Validator::quickIsNFCVerify( $valCopy ); // validates $valCopy.
486 if ( $valCopy !== $val ) {
487 MediaWiki\suppressWarnings();
488 $val = iconv( 'Windows-1252', 'UTF-8//IGNORE', $val );
489 MediaWiki\restoreWarnings();
490 }
491 }
492
493 // trim and check to make sure not only whitespace.
494 $val = trim( $val );
495 if ( strlen( $val ) === 0 ) {
496 // only whitespace.
497 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, "$prop: Is only whitespace" );
498 unset( $this->mFilteredExifData[$prop] );
499
500 return;
501 }
502
503 // all's good.
504 $this->mFilteredExifData[$prop] = $val;
505 }
506 }
507
508 /**
509 * Convert an Exif::UNDEFINED from a raw binary string
510 * to its value. This is sometimes needed depending on
511 * the type of UNDEFINED field
512 * @param string $prop Name of property
513 */
514 private function exifPropToOrd( $prop ) {
515 if ( isset( $this->mFilteredExifData[$prop] ) ) {
516 $this->mFilteredExifData[$prop] = ord( $this->mFilteredExifData[$prop] );
517 }
518 }
519
520 /**
521 * Convert gps in exif form to a single floating point number
522 * for example 10 degress 20`40`` S -> -10.34444
523 * @param string $prop A GPS coordinate exif tag name (like GPSLongitude)
524 */
525 private function exifGPStoNumber( $prop ) {
526 $loc =& $this->mFilteredExifData[$prop];
527 $dir =& $this->mFilteredExifData[$prop . 'Ref'];
528 $res = false;
529
530 if ( isset( $loc ) && isset( $dir )
531 && ( $dir === 'N' || $dir === 'S' || $dir === 'E' || $dir === 'W' )
532 ) {
533 list( $num, $denom ) = explode( '/', $loc[0] );
534 $res = $num / $denom;
535 list( $num, $denom ) = explode( '/', $loc[1] );
536 $res += ( $num / $denom ) * ( 1 / 60 );
537 list( $num, $denom ) = explode( '/', $loc[2] );
538 $res += ( $num / $denom ) * ( 1 / 3600 );
539
540 if ( $dir === 'S' || $dir === 'W' ) {
541 $res *= -1; // make negative
542 }
543 }
544
545 // update the exif records.
546
547 if ( $res !== false ) { // using !== as $res could potentially be 0
548 $this->mFilteredExifData[$prop] = $res;
549 unset( $this->mFilteredExifData[$prop . 'Ref'] );
550 } else { // if invalid
551 unset( $this->mFilteredExifData[$prop] );
552 unset( $this->mFilteredExifData[$prop . 'Ref'] );
553 }
554 }
555
556 /**#@-*/
557
558 /**#@+
559 * @return array
560 */
561 /**
562 * Get $this->mRawExifData
563 * @return array
564 */
565 function getData() {
566 return $this->mRawExifData;
567 }
568
569 /**
570 * Get $this->mFilteredExifData
571 * @return array
572 */
573 function getFilteredData() {
574 return $this->mFilteredExifData;
575 }
576
577 /**#@-*/
578
579 /**
580 * The version of the output format
581 *
582 * Before the actual metadata information is saved in the database we
583 * strip some of it since we don't want to save things like thumbnails
584 * which usually accompany Exif data. This value gets saved in the
585 * database along with the actual Exif data, and if the version in the
586 * database doesn't equal the value returned by this function the Exif
587 * data is regenerated.
588 *
589 * @return int
590 */
591 public static function version() {
592 return 2; // We don't need no bloddy constants!
593 }
594
595 /**
596 * Validates if a tag value is of the type it should be according to the Exif spec
597 *
598 * @param mixed $in The input value to check
599 * @return bool
600 */
601 private function isByte( $in ) {
602 if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 255 ) {
603 $this->debug( $in, __FUNCTION__, true );
604
605 return true;
606 } else {
607 $this->debug( $in, __FUNCTION__, false );
608
609 return false;
610 }
611 }
612
613 /**
614 * @param mixed $in The input value to check
615 * @return bool
616 */
617 private function isASCII( $in ) {
618 if ( is_array( $in ) ) {
619 return false;
620 }
621
622 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
623 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
624
625 return false;
626 }
627
628 if ( preg_match( '/^\s*$/', $in ) ) {
629 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
630
631 return false;
632 }
633
634 return true;
635 }
636
637 /**
638 * @param mixed $in The input value to check
639 * @return bool
640 */
641 private function isShort( $in ) {
642 if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 65536 ) {
643 $this->debug( $in, __FUNCTION__, true );
644
645 return true;
646 } else {
647 $this->debug( $in, __FUNCTION__, false );
648
649 return false;
650 }
651 }
652
653 /**
654 * @param mixed $in The input value to check
655 * @return bool
656 */
657 private function isLong( $in ) {
658 if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 4294967296 ) {
659 $this->debug( $in, __FUNCTION__, true );
660
661 return true;
662 } else {
663 $this->debug( $in, __FUNCTION__, false );
664
665 return false;
666 }
667 }
668
669 /**
670 * @param mixed $in The input value to check
671 * @return bool
672 */
673 private function isRational( $in ) {
674 $m = [];
675
676 # Avoid division by zero
677 if ( !is_array( $in )
678 && preg_match( '/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m )
679 ) {
680 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
681 } else {
682 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
683
684 return false;
685 }
686 }
687
688 /**
689 * @param mixed $in The input value to check
690 * @return bool
691 */
692 private function isUndefined( $in ) {
693 $this->debug( $in, __FUNCTION__, true );
694
695 return true;
696 }
697
698 /**
699 * @param mixed $in The input value to check
700 * @return bool
701 */
702 private function isSlong( $in ) {
703 if ( $this->isLong( abs( $in ) ) ) {
704 $this->debug( $in, __FUNCTION__, true );
705
706 return true;
707 } else {
708 $this->debug( $in, __FUNCTION__, false );
709
710 return false;
711 }
712 }
713
714 /**
715 * @param mixed $in The input value to check
716 * @return bool
717 */
718 private function isSrational( $in ) {
719 $m = [];
720
721 # Avoid division by zero
722 if ( !is_array( $in ) &&
723 preg_match( '/^(-?\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m )
724 ) {
725 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
726 } else {
727 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
728
729 return false;
730 }
731 }
732
733 /**#@-*/
734
735 /**
736 * Validates if a tag has a legal value according to the Exif spec
737 *
738 * @param string $section Section where tag is located.
739 * @param string $tag The tag to check.
740 * @param mixed $val The value of the tag.
741 * @param bool $recursive True if called recursively for array types.
742 * @return bool
743 */
744 private function validate( $section, $tag, $val, $recursive = false ) {
745 $debug = "tag is '$tag'";
746 $etype = $this->mExifTags[$section][$tag];
747 $ecount = 1;
748 if ( is_array( $etype ) ) {
749 list( $etype, $ecount ) = $etype;
750 if ( $recursive ) {
751 $ecount = 1; // checking individual elements
752 }
753 }
754 $count = count( $val );
755 if ( $ecount != $count ) {
756 $this->debug( $val, __FUNCTION__, "Expected $ecount elements for $tag but got $count" );
757
758 return false;
759 }
760 if ( $count > 1 ) {
761 foreach ( $val as $v ) {
762 if ( !$this->validate( $section, $tag, $v, true ) ) {
763 return false;
764 }
765 }
766
767 return true;
768 }
769 // Does not work if not typecast
770 switch ( (string)$etype ) {
771 case (string)Exif::BYTE:
772 $this->debug( $val, __FUNCTION__, $debug );
773
774 return $this->isByte( $val );
775 case (string)Exif::ASCII:
776 $this->debug( $val, __FUNCTION__, $debug );
777
778 return $this->isASCII( $val );
779 case (string)Exif::SHORT:
780 $this->debug( $val, __FUNCTION__, $debug );
781
782 return $this->isShort( $val );
783 case (string)Exif::LONG:
784 $this->debug( $val, __FUNCTION__, $debug );
785
786 return $this->isLong( $val );
787 case (string)Exif::RATIONAL:
788 $this->debug( $val, __FUNCTION__, $debug );
789
790 return $this->isRational( $val );
791 case (string)Exif::SHORT_OR_LONG:
792 $this->debug( $val, __FUNCTION__, $debug );
793
794 return $this->isShort( $val ) || $this->isLong( $val );
795 case (string)Exif::UNDEFINED:
796 $this->debug( $val, __FUNCTION__, $debug );
797
798 return $this->isUndefined( $val );
799 case (string)Exif::SLONG:
800 $this->debug( $val, __FUNCTION__, $debug );
801
802 return $this->isSlong( $val );
803 case (string)Exif::SRATIONAL:
804 $this->debug( $val, __FUNCTION__, $debug );
805
806 return $this->isSrational( $val );
807 case (string)Exif::IGNORE:
808 $this->debug( $val, __FUNCTION__, $debug );
809
810 return false;
811 default:
812 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
813
814 return false;
815 }
816 }
817
818 /**
819 * Convenience function for debugging output
820 *
821 * @param mixed $in Arrays will be processed with print_r().
822 * @param string $fname Function name to log.
823 * @param string|bool|null $action Default null.
824 */
825 private function debug( $in, $fname, $action = null ) {
826 if ( !$this->log ) {
827 return;
828 }
829 $type = gettype( $in );
830 $class = ucfirst( __CLASS__ );
831 if ( is_array( $in ) ) {
832 $in = print_r( $in, true );
833 }
834
835 if ( $action === true ) {
836 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)" );
837 } elseif ( $action === false ) {
838 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)" );
839 } elseif ( $action === null ) {
840 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)" );
841 } else {
842 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')" );
843 }
844 }
845
846 /**
847 * Convenience function for debugging output
848 *
849 * @param string $fname The name of the function calling this function
850 * @param bool $io Specify whether we're beginning or ending
851 */
852 private function debugFile( $fname, $io ) {
853 if ( !$this->log ) {
854 return;
855 }
856 $class = ucfirst( __CLASS__ );
857 if ( $io ) {
858 wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'" );
859 } else {
860 wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'" );
861 }
862 }
863 }