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