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