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