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