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