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