* Duck warnings when fed an invalid file
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 if ( !defined( 'MEDIAWIKI' ) ) die();
3 /**
4 * @package MediaWiki
5 * @subpackage Metadata
6 *
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
27 * @bug 1555, 1947
28 */
29
30 /**#@+
31 * Exif tag type definition
32 */
33 define('MW_EXIF_BYTE', 1); # An 8-bit (1-byte) unsigned integer.
34 define('MW_EXIF_ASCII', 2); # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
35 define('MW_EXIF_SHORT', 3); # A 16-bit (2-byte) unsigned integer.
36 define('MW_EXIF_LONG', 4); # A 32-bit (4-byte) unsigned integer.
37 define('MW_EXIF_RATIONAL', 5); # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
38 define('MW_EXIF_UNDEFINED', 7); # An 8-bit byte that can take any value depending on the field definition
39 define('MW_EXIF_SLONG', 9); # A 32-bit (4-byte) signed integer (2's complement notation),
40 define('MW_EXIF_SRATIONAL', 10); # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
41 /**#@-*/
42
43
44 /**
45 * @package MediaWiki
46 * @subpackage Metadata
47 */
48 class Exif {
49 /**#@+
50 * @var array
51 * @access private
52 */
53
54 /**
55 * Exif tags grouped by category, the tagname itself is the key and the type
56 * is the value, in the case of more than one possible value type they are
57 * seperated by commas.
58 */
59 var $mExifTags;
60
61 /**
62 * A one dimentional array of all Exif tags
63 */
64 var $mFlatExifTags;
65
66 /**
67 * The raw Exif data returned by exif_read_data()
68 */
69 var $mRawExifData;
70
71 /**
72 * A Filtered version of $mRawExifData that has been pruned of invalid
73 * tags and tags that contain content they shouldn't contain according
74 * to the Exif specification
75 */
76 var $mFilteredExifData;
77
78 /**
79 * Filtered and formatted Exif data, see FormatExif::getFormattedData()
80 */
81 var $mFormattedExifData;
82
83 /**#@-*/
84
85 /**
86 * The private log to log to
87 *
88 * @var string
89 * @access private
90 */
91 var $log = 'exif';
92
93 /**
94 * Constructor
95 *
96 * @param string $file
97 */
98 function Exif( $file ) {
99 /**
100 * Page numbers here refer to pages in the EXIF 2.2 standard
101 *
102 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
103 */
104 $this->mExifTags = array(
105 # TIFF Rev. 6.0 Attribute Information (p22)
106 'tiff' => array(
107 # Tags relating to image structure
108 'structure' => array(
109 'ImageWidth' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image width
110 'ImageLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image height
111 'BitsPerSample' => MW_EXIF_SHORT, # Number of bits per component
112 # "When a primary image is JPEG compressed, this designation is not"
113 # "necessary and is omitted." (p23)
114 'Compression' => MW_EXIF_SHORT, # Compression scheme #p23
115 'PhotometricInterpretation' => MW_EXIF_SHORT, # Pixel composition #p23
116 'Orientation' => MW_EXIF_SHORT, # Orientation of image #p24
117 'SamplesPerPixel' => MW_EXIF_SHORT, # Number of components
118 'PlanarConfiguration' => MW_EXIF_SHORT, # Image data arrangement #p24
119 'YCbCrSubSampling' => MW_EXIF_SHORT, # Subsampling ratio of Y to C #p24
120 'YCbCrPositioning' => MW_EXIF_SHORT, # Y and C positioning #p24-25
121 'XResolution' => MW_EXIF_RATIONAL, # Image resolution in width direction
122 'YResolution' => MW_EXIF_RATIONAL, # Image resolution in height direction
123 'ResolutionUnit' => MW_EXIF_SHORT, # Unit of X and Y resolution #(p26)
124 ),
125
126 # Tags relating to recording offset
127 'offset' => array(
128 'StripOffsets' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image data location
129 'RowsPerStrip' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Number of rows per strip
130 'StripByteCounts' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes per compressed strip
131 'JPEGInterchangeFormat' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Offset to JPEG SOI
132 'JPEGInterchangeFormatLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes of JPEG data
133 ),
134
135 # Tags relating to image data characteristics
136 'characteristics' => array(
137 'TransferFunction' => MW_EXIF_SHORT, # Transfer function
138 'WhitePoint' => MW_EXIF_RATIONAL, # White point chromaticity
139 'PrimaryChromaticities' => MW_EXIF_RATIONAL, # Chromaticities of primarities
140 'YCbCrCoefficients' => MW_EXIF_RATIONAL, # Color space transformation matrix coefficients #p27
141 'ReferenceBlackWhite' => MW_EXIF_RATIONAL # Pair of black and white reference values
142 ),
143
144 # Other tags
145 'other' => array(
146 'DateTime' => MW_EXIF_ASCII, # File change date and time
147 'ImageDescription' => MW_EXIF_ASCII, # Image title
148 'Make' => MW_EXIF_ASCII, # Image input equipment manufacturer
149 'Model' => MW_EXIF_ASCII, # Image input equipment model
150 'Software' => MW_EXIF_ASCII, # Software used
151 'Artist' => MW_EXIF_ASCII, # Person who created the image
152 'Copyright' => MW_EXIF_ASCII, # Copyright holder
153 ),
154 ),
155
156 # Exif IFD Attribute Information (p30-31)
157 'exif' => array(
158 # Tags relating to version
159 'version' => 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' => MW_EXIF_UNDEFINED, # Exif version
163 'FlashpixVersion' => MW_EXIF_UNDEFINED, # Supported Flashpix version #p32
164 ),
165
166 # Tags relating to Image Data Characteristics
167 'characteristics' => array(
168 'ColorSpace' => MW_EXIF_SHORT, # Color space information #p32
169 ),
170
171 # Tags relating to image configuration
172 'configuration' => array(
173 'ComponentsConfiguration' => MW_EXIF_UNDEFINED, # Meaning of each component #p33
174 'CompressedBitsPerPixel' => MW_EXIF_RATIONAL, # Image compression mode
175 'PixelYDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valid image width
176 'PixelXDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valind image height
177 ),
178
179 # Tags relating to related user information
180 'user' => array(
181 'MakerNote' => MW_EXIF_UNDEFINED, # Manufacturer notes
182 'UserComment' => MW_EXIF_UNDEFINED, # User comments #p34
183 ),
184
185 # Tags relating to related file information
186 'related' => array(
187 'RelatedSoundFile' => MW_EXIF_ASCII, # Related audio file
188 ),
189
190 # Tags relating to date and time
191 'dateandtime' => array(
192 'DateTimeOriginal' => MW_EXIF_ASCII, # Date and time of original data generation #p36
193 'DateTimeDigitized' => MW_EXIF_ASCII, # Date and time of original data generation
194 'SubSecTime' => MW_EXIF_ASCII, # DateTime subseconds
195 'SubSecTimeOriginal' => MW_EXIF_ASCII, # DateTimeOriginal subseconds
196 'SubSecTimeDigitized' => MW_EXIF_ASCII, # DateTimeDigitized subseconds
197 ),
198
199 # Tags relating to picture-taking conditions (p31)
200 'conditions' => array(
201 'ExposureTime' => MW_EXIF_RATIONAL, # Exposure time
202 'FNumber' => MW_EXIF_RATIONAL, # F Number
203 'ExposureProgram' => MW_EXIF_SHORT, # Exposure Program #p38
204 'SpectralSensitivity' => MW_EXIF_ASCII, # Spectral sensitivity
205 'ISOSpeedRatings' => MW_EXIF_SHORT, # ISO speed rating
206 'OECF' => MW_EXIF_UNDEFINED, # Optoelectronic conversion factor
207 'ShutterSpeedValue' => MW_EXIF_SRATIONAL, # Shutter speed
208 'ApertureValue' => MW_EXIF_RATIONAL, # Aperture
209 'BrightnessValue' => MW_EXIF_SRATIONAL, # Brightness
210 'ExposureBiasValue' => MW_EXIF_SRATIONAL, # Exposure bias
211 'MaxApertureValue' => MW_EXIF_RATIONAL, # Maximum land aperture
212 'SubjectDistance' => MW_EXIF_RATIONAL, # Subject distance
213 'MeteringMode' => MW_EXIF_SHORT, # Metering mode #p40
214 'LightSource' => MW_EXIF_SHORT, # Light source #p40-41
215 'Flash' => MW_EXIF_SHORT, # Flash #p41-42
216 'FocalLength' => MW_EXIF_RATIONAL, # Lens focal length
217 'SubjectArea' => MW_EXIF_SHORT, # Subject area
218 'FlashEnergy' => MW_EXIF_RATIONAL, # Flash energy
219 'SpatialFrequencyResponse' => MW_EXIF_UNDEFINED, # Spatial frequency response
220 'FocalPlaneXResolution' => MW_EXIF_RATIONAL, # Focal plane X resolution
221 'FocalPlaneYResolution' => MW_EXIF_RATIONAL, # Focal plane Y resolution
222 'FocalPlaneResolutionUnit' => MW_EXIF_SHORT, # Focal plane resolution unit #p46
223 'SubjectLocation' => MW_EXIF_SHORT, # Subject location
224 'ExposureIndex' => MW_EXIF_RATIONAL, # Exposure index
225 'SensingMethod' => MW_EXIF_SHORT, # Sensing method #p46
226 'FileSource' => MW_EXIF_UNDEFINED, # File source #p47
227 'SceneType' => MW_EXIF_UNDEFINED, # Scene type #p47
228 'CFAPattern' => MW_EXIF_UNDEFINED, # CFA pattern
229 'CustomRendered' => MW_EXIF_SHORT, # Custom image processing #p48
230 'ExposureMode' => MW_EXIF_SHORT, # Exposure mode #p48
231 'WhiteBalance' => MW_EXIF_SHORT, # White Balance #p49
232 'DigitalZoomRatio' => MW_EXIF_RATIONAL, # Digital zoom ration
233 'FocalLengthIn35mmFilm' => MW_EXIF_SHORT, # Focal length in 35 mm film
234 'SceneCaptureType' => MW_EXIF_SHORT, # Scene capture type #p49
235 'GainControl' => MW_EXIF_RATIONAL, # Scene control #p49-50
236 'Contrast' => MW_EXIF_SHORT, # Contrast #p50
237 'Saturation' => MW_EXIF_SHORT, # Saturation #p50
238 'Sharpness' => MW_EXIF_SHORT, # Sharpness #p50
239 'DeviceSettingDescription' => MW_EXIF_UNDEFINED, # Desice settings description
240 'SubjectDistanceRange' => MW_EXIF_SHORT, # Subject distance range #p51
241 ),
242
243 'other' => array(
244 'ImageUniqueID' => MW_EXIF_ASCII, # Unique image ID
245 ),
246 ),
247
248 # GPS Attribute Information (p52)
249 'gps' => array(
250 'GPSVersionID' => MW_EXIF_BYTE, # GPS tag version
251 'GPSLatitudeRef' => MW_EXIF_ASCII, # North or South Latitude #p52-53
252 'GPSLatitude' => MW_EXIF_RATIONAL, # Latitude
253 'GPSLongitudeRef' => MW_EXIF_ASCII, # East or West Longitude #p53
254 'GPSLongitude' => MW_EXIF_RATIONAL, # Longitude
255 'GPSAltitudeRef' => MW_EXIF_BYTE, # Altitude reference
256 'GPSAltitude' => MW_EXIF_RATIONAL, # Altitude
257 'GPSTimeStamp' => MW_EXIF_RATIONAL, # GPS time (atomic clock)
258 'GPSSatellites' => MW_EXIF_ASCII, # Satellites used for measurement
259 'GPSStatus' => MW_EXIF_ASCII, # Receiver status #p54
260 'GPSMeasureMode' => MW_EXIF_ASCII, # Measurement mode #p54-55
261 'GPSDOP' => MW_EXIF_RATIONAL, # Measurement precision
262 'GPSSpeedRef' => MW_EXIF_ASCII, # Speed unit #p55
263 'GPSSpeed' => MW_EXIF_RATIONAL, # Speed of GPS receiver
264 'GPSTrackRef' => MW_EXIF_ASCII, # Reference for direction of movement #p55
265 'GPSTrack' => MW_EXIF_RATIONAL, # Direction of movement
266 'GPSImgDirectionRef' => MW_EXIF_ASCII, # Reference for direction of image #p56
267 'GPSImgDirection' => MW_EXIF_RATIONAL, # Direction of image
268 'GPSMapDatum' => MW_EXIF_ASCII, # Geodetic survey data used
269 'GPSDestLatitudeRef' => MW_EXIF_ASCII, # Reference for latitude of destination #p56
270 'GPSDestLatitude' => MW_EXIF_RATIONAL, # Latitude destination
271 'GPSDestLongitudeRef' => MW_EXIF_ASCII, # Reference for longitude of destination #p57
272 'GPSDestLongitude' => MW_EXIF_RATIONAL, # Longitude of destination
273 'GPSDestBearingRef' => MW_EXIF_ASCII, # Reference for bearing of destination #p57
274 'GPSDestBearing' => MW_EXIF_RATIONAL, # Bearing of destination
275 'GPSDestDistanceRef' => MW_EXIF_ASCII, # Reference for distance to destination #p57-58
276 'GPSDestDistance' => MW_EXIF_RATIONAL, # Distance to destination
277 'GPSProcessingMethod' => MW_EXIF_UNDEFINED, # Name of GPS processing method
278 'GPSAreaInformation' => MW_EXIF_UNDEFINED, # Name of GPS area
279 'GPSDateStamp' => MW_EXIF_ASCII, # GPS date
280 'GPSDifferential' => MW_EXIF_SHORT, # GPS differential correction
281 ),
282 );
283
284 $basename = basename( $file );
285
286 $this->makeFlatExifTags();
287
288 $this->debugFile( $basename, __FUNCTION__, true );
289 wfSuppressWarnings();
290 $data = exif_read_data( $file );
291 wfRestoreWarnings();
292 /**
293 * exif_read_data() will return false on invalid input, such as
294 * when somebody uploads a file called something.jpeg
295 * containing random gibberish.
296 */
297 $this->mRawExifData = $data ? $data : array();
298
299 $this->makeFilteredData();
300 $this->makeFormattedData();
301
302 $this->debugFile( $basename, __FUNCTION__, false );
303 }
304
305 /**#@+
306 * @access private
307 */
308 /**
309 * Generate a flat list of the exif tags
310 */
311 function makeFlatExifTags() {
312 $this->extractTags( $this->mExifTags );
313 }
314
315 /**
316 * A recursing extractor function used by makeFlatExifTags()
317 *
318 * Note: This used to use an array_walk function, but it made PHP5
319 * segfault, see `cvs diff -u -r 1.4 -r 1.5 Exif.php`
320 */
321 function extractTags( &$tagset ) {
322 foreach( $tagset as $key => $val ) {
323 if( is_array( $val ) ) {
324 $this->extractTags( $val );
325 } else {
326 $this->mFlatExifTags[$key] = $val;
327 }
328 }
329 }
330
331 /**
332 * Make $this->mFilteredExifData
333 */
334 function makeFilteredData() {
335 $this->mFilteredExifData = $this->mRawExifData;
336
337 foreach( $this->mFilteredExifData as $k => $v ) {
338 if ( !in_array( $k, array_keys( $this->mFlatExifTags ) ) ) {
339 $this->debug( $v, __FUNCTION__, "'$k' is not a valid Exif tag" );
340 unset( $this->mFilteredExifData[$k] );
341 }
342 }
343
344 foreach( $this->mFilteredExifData as $k => $v ) {
345 if ( !$this->validate($k, $v) ) {
346 $this->debug( $v, __FUNCTION__, "'$k' contained invalid data" );
347 unset( $this->mFilteredExifData[$k] );
348 }
349 }
350 }
351
352 function makeFormattedData( $data = null ) {
353 $format = new FormatExif( $this->getFilteredData() );
354 $this->mFormattedExifData = $format->getFormattedData();
355 }
356 /**#@-*/
357
358 /**#@+
359 * @return array
360 */
361 /**
362 * Get $this->mRawExifData
363 */
364 function getData() {
365 return $this->mRawExifData;
366 }
367
368 /**
369 * Get $this->mFilteredExifData
370 */
371 function getFilteredData() {
372 return $this->mFilteredExifData;
373 }
374
375 /**
376 * Get $this->mFormattedExifData
377 */
378 function getFormattedData() {
379 return $this->mFormattedExifData;
380 }
381 /**#@-*/
382
383 /**
384 * The version of the output format
385 *
386 * Before the actual metadata information is saved in the database we
387 * strip some of it since we don't want to save things like thumbnails
388 * which usually accompany Exif data. This value gets saved in the
389 * database along with the actual Exif data, and if the version in the
390 * database doesn't equal the value returned by this function the Exif
391 * data is regenerated.
392 *
393 * @return int
394 */
395 function version() {
396 return 1; // We don't need no bloddy constants!
397 }
398
399 /**#@+
400 * Validates if a tag value is of the type it should be according to the Exif spec
401 *
402 * @access private
403 *
404 * @param mixed $in The input value to check
405 * @return bool
406 */
407 function isByte( $in ) {
408 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
409 $this->debug( $in, __FUNCTION__, true );
410 return true;
411 } else {
412 $this->debug( $in, __FUNCTION__, false );
413 return false;
414 }
415 }
416
417 function isASCII( $in ) {
418 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
419 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
420 return false;
421 }
422
423 if ( preg_match( "/^\s*$/", $in ) ) {
424 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
425 return false;
426 }
427
428 return true;
429 }
430
431 function isShort( $in ) {
432 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
433 $this->debug( $in, __FUNCTION__, true );
434 return true;
435 } else {
436 $this->debug( $in, __FUNCTION__, false );
437 return false;
438 }
439 }
440
441 function isLong( $in ) {
442 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
443 $this->debug( $in, __FUNCTION__, true );
444 return true;
445 } else {
446 $this->debug( $in, __FUNCTION__, false );
447 return false;
448 }
449 }
450
451 function isRational( $in ) {
452 if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
453 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
454 } else {
455 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
456 return false;
457 }
458 }
459
460 function isUndefined( $in ) {
461 if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
462 $this->debug( $in, __FUNCTION__, true );
463 return true;
464 } else {
465 $this->debug( $in, __FUNCTION__, false );
466 return false;
467 }
468 }
469
470 function isSlong( $in ) {
471 if ( $this->isLong( abs( $in ) ) ) {
472 $this->debug( $in, __FUNCTION__, true );
473 return true;
474 } else {
475 $this->debug( $in, __FUNCTION__, false );
476 return false;
477 }
478 }
479
480 function isSrational( $in ) {
481 if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
482 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
483 } else {
484 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
485 return false;
486 }
487 }
488 /**#@-*/
489
490 /**
491 * Validates if a tag has a legal value according to the Exif spec
492 *
493 * @access private
494 *
495 * @param string $tag The tag to check
496 * @param mixed $val The value of the tag
497 * @return bool
498 */
499 function validate( $tag, $val ) {
500 $debug = "tag is '$tag'";
501 // Fucks up if not typecast
502 switch( (string)$this->mFlatExifTags[$tag] ) {
503 case (string)MW_EXIF_BYTE:
504 $this->debug( $val, __FUNCTION__, $debug );
505 return $this->isByte( $val );
506 case (string)MW_EXIF_ASCII:
507 $this->debug( $val, __FUNCTION__, $debug );
508 return $this->isASCII( $val );
509 case (string)MW_EXIF_SHORT:
510 $this->debug( $val, __FUNCTION__, $debug );
511 return $this->isShort( $val );
512 case (string)MW_EXIF_LONG:
513 $this->debug( $val, __FUNCTION__, $debug );
514 return $this->isLong( $val );
515 case (string)MW_EXIF_RATIONAL:
516 $this->debug( $val, __FUNCTION__, $debug );
517 return $this->isRational( $val );
518 case (string)MW_EXIF_UNDEFINED:
519 $this->debug( $val, __FUNCTION__, $debug );
520 return $this->isUndefined( $val );
521 case (string)MW_EXIF_SLONG:
522 $this->debug( $val, __FUNCTION__, $debug );
523 return $this->isSlong( $val );
524 case (string)MW_EXIF_SRATIONAL:
525 $this->debug( $val, __FUNCTION__, $debug );
526 return $this->isSrational( $val );
527 case (string)MW_EXIF_SHORT.','.MW_EXIF_LONG:
528 $this->debug( $val, __FUNCTION__, $debug );
529 return $this->isShort( $val ) || $this->isLong( $val );
530 default:
531 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
532 return false;
533 }
534 }
535
536 /**
537 * Conviniance function for debugging output
538 *
539 * @access private
540 *
541 * @param mixed $in
542 * @param string $fname
543 * @param mixed $action
544 */
545 function debug( $in, $fname, $action = null ) {
546 $type = gettype( $in );
547 $class = ucfirst( __CLASS__ );
548 if ( $type === 'array' )
549 $in = print_r( $in, true );
550
551 if ( $action === true )
552 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
553 elseif ( $action === false )
554 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
555 elseif ( $action === null )
556 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
557 else
558 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
559 }
560
561 /**
562 * Conviniance function for debugging output
563 *
564 * @access private
565 *
566 * @param string $basename The name of the file being processed
567 * @paran string $fname The name of the function calling this function
568 * @param bool $bool $io Specify whether we're beginning or ending
569 */
570 function debugFile( $basename, $fname, $io ) {
571 $class = ucfirst( __CLASS__ );
572 if ( $io )
573 wfDebugLog( $this->log, "$class::$fname: begin processing: '$basename'\n" );
574 else
575 wfDebugLog( $this->log, "$class::$fname: end processing: '$basename'\n" );
576 }
577
578 }
579
580 /**
581 * @package MediaWiki
582 * @subpackage Metadata
583 */
584 class FormatExif {
585 /**
586 * The Exif data to format
587 *
588 * @var array
589 * @access private
590 */
591 var $mExif;
592
593 /**
594 * Constructor
595 *
596 * @param array $exif The Exif data to format ( as returned by
597 * Exif::getFilteredData() )
598 */
599 function FormatExif( $exif ) {
600 $this->mExif = $exif;
601 }
602
603 /**
604 * Numbers given by Exif user agents are often magical, that is they
605 * should be replaced by a detailed explanation depending on their
606 * value which most of the time are plain integers. This function
607 * formats Exif values into human readable form.
608 *
609 * @return array
610 */
611 function getFormattedData() {
612 global $wgLang;
613
614 $tags =& $this->mExif;
615
616 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
617 unset( $tags['ResolutionUnit'] );
618
619 foreach( $tags as $tag => $val ) {
620 switch( $tag ) {
621 case 'Compression':
622 switch( $val ) {
623 case 1: case 6:
624 $tags[$tag] = $this->msg( $tag, $val );
625 break;
626 default:
627 $tags[$tag] = $val;
628 break;
629 }
630 break;
631
632 case 'PhotometricInterpretation':
633 switch( $val ) {
634 case 2: case 6:
635 $tags[$tag] = $this->msg( $tag, $val );
636 break;
637 default:
638 $tags[$tag] = $val;
639 break;
640 }
641 break;
642
643 case 'Orientation':
644 switch( $val ) {
645 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
646 $tags[$tag] = $this->msg( $tag, $val );
647 break;
648 default:
649 $tags[$tag] = $val;
650 break;
651 }
652 break;
653
654 case 'PlanarConfiguration':
655 switch( $val ) {
656 case 1: case 2:
657 $tags[$tag] = $this->msg( $tag, $val );
658 break;
659 default:
660 $tags[$tag] = $val;
661 break;
662 }
663 break;
664
665 // TODO: YCbCrSubSampling
666 // TODO: YCbCrPositioning
667
668 case 'XResolution':
669 case 'YResolution':
670 switch( $resolutionunit ) {
671 case 2:
672 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
673 break;
674 case 3:
675 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
676 break;
677 default:
678 $tags[$tag] = $val;
679 break;
680 }
681 break;
682
683 // TODO: YCbCrCoefficients #p27 (see annex E)
684 case 'ExifVersion': case 'FlashpixVersion':
685 $tags[$tag] = "$val"/100;
686 break;
687
688 case 'ColorSpace':
689 switch( $val ) {
690 case 1: case 'FFFF.H':
691 $tags[$tag] = $this->msg( $tag, $val );
692 break;
693 default:
694 $tags[$tag] = $val;
695 break;
696 }
697 break;
698
699 case 'ComponentsConfiguration':
700 switch( $val ) {
701 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
702 $tags[$tag] = $this->msg( $tag, $val );
703 break;
704 default:
705 $tags[$tag] = $val;
706 break;
707 }
708 break;
709
710 case 'DateTime':
711 case 'DateTimeOriginal':
712 case 'DateTimeDigitized':
713 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
714 break;
715
716 case 'ExposureProgram':
717 switch( $val ) {
718 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
719 $tags[$tag] = $this->msg( $tag, $val );
720 break;
721 default:
722 $tags[$tag] = $val;
723 break;
724 }
725 break;
726
727 case 'SubjectDistance':
728 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
729 break;
730
731 case 'MeteringMode':
732 switch( $val ) {
733 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
734 $tags[$tag] = $this->msg( $tag, $val );
735 break;
736 default:
737 $tags[$tag] = $val;
738 break;
739 }
740 break;
741
742 case 'LightSource':
743 switch( $val ) {
744 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
745 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
746 case 21: case 22: case 23: case 24: case 255:
747 $tags[$tag] = $this->msg( $tag, $val );
748 break;
749 default:
750 $tags[$tag] = $val;
751 break;
752 }
753 break;
754
755 // TODO: Flash
756 case 'FocalPlaneResolutionUnit':
757 switch( $val ) {
758 case 2:
759 $tags[$tag] = $this->msg( $tag, $val );
760 break;
761 default:
762 $tags[$tag] = $val;
763 break;
764 }
765 break;
766
767 case 'SensingMethod':
768 switch( $val ) {
769 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
770 $tags[$tag] = $this->msg( $tag, $val );
771 break;
772 default:
773 $tags[$tag] = $val;
774 break;
775 }
776 break;
777
778 case 'FileSource':
779 switch( $val ) {
780 case 3:
781 $tags[$tag] = $this->msg( $tag, $val );
782 break;
783 default:
784 $tags[$tag] = $val;
785 break;
786 }
787 break;
788
789 case 'SceneType':
790 switch( $val ) {
791 case 1:
792 $tags[$tag] = $this->msg( $tag, $val );
793 break;
794 default:
795 $tags[$tag] = $val;
796 break;
797 }
798 break;
799
800 case 'CustomRendered':
801 switch( $val ) {
802 case 0: case 1:
803 $tags[$tag] = $this->msg( $tag, $val );
804 break;
805 default:
806 $tags[$tag] = $val;
807 break;
808 }
809 break;
810
811 case 'ExposureMode':
812 switch( $val ) {
813 case 0: case 1: case 2:
814 $tags[$tag] = $this->msg( $tag, $val );
815 break;
816 default:
817 $tags[$tag] = $val;
818 break;
819 }
820 break;
821
822 case 'WhiteBalance':
823 switch( $val ) {
824 case 0: case 1:
825 $tags[$tag] = $this->msg( $tag, $val );
826 break;
827 default:
828 $tags[$tag] = $val;
829 break;
830 }
831 break;
832
833 case 'SceneCaptureType':
834 switch( $val ) {
835 case 0: case 1: case 2: case 3:
836 $tags[$tag] = $this->msg( $tag, $val );
837 break;
838 default:
839 $tags[$tag] = $val;
840 break;
841 }
842 break;
843
844 case 'GainControl':
845 switch( $val ) {
846 case 0: case 1: case 2: case 3: case 4:
847 $tags[$tag] = $this->msg( $tag, $val );
848 break;
849 default:
850 $tags[$tag] = $val;
851 break;
852 }
853 break;
854
855 case 'Contrast':
856 switch( $val ) {
857 case 0: case 1: case 2:
858 $tags[$tag] = $this->msg( $tag, $val );
859 break;
860 default:
861 $tags[$tag] = $val;
862 break;
863 }
864 break;
865
866 case 'Saturation':
867 switch( $val ) {
868 case 0: case 1: case 2:
869 $tags[$tag] = $this->msg( $tag, $val );
870 break;
871 default:
872 $tags[$tag] = $val;
873 break;
874 }
875 break;
876
877 case 'Sharpness':
878 switch( $val ) {
879 case 0: case 1: case 2:
880 $tags[$tag] = $this->msg( $tag, $val );
881 break;
882 default:
883 $tags[$tag] = $val;
884 break;
885 }
886 break;
887
888 case 'SubjectDistanceRange':
889 switch( $val ) {
890 case 0: case 1: case 2: case 3:
891 $tags[$tag] = $this->msg( $tag, $val );
892 break;
893 default:
894 $tags[$tag] = $val;
895 break;
896 }
897 break;
898
899 case 'GPSLatitudeRef':
900 case 'GPSDestLatitudeRef':
901 switch( $val ) {
902 case 'N': case 'S':
903 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
904 break;
905 default:
906 $tags[$tag] = $val;
907 break;
908 }
909 break;
910
911 case 'GPSLongitudeRef':
912 case 'GPSDestLongitudeRef':
913 switch( $val ) {
914 case 'E': case 'W':
915 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
916 break;
917 default:
918 $tags[$tag] = $val;
919 break;
920 }
921 break;
922
923 case 'GPSStatus':
924 switch( $val ) {
925 case 'A': case 'V':
926 $tags[$tag] = $this->msg( $tag, $val );
927 break;
928 default:
929 $tags[$tag] = $val;
930 break;
931 }
932 break;
933
934 case 'GPSMeasureMode':
935 switch( $val ) {
936 case 2: case 3:
937 $tags[$tag] = $this->msg( $tag, $val );
938 break;
939 default:
940 $tags[$tag] = $val;
941 break;
942 }
943 break;
944
945 case 'GPSSpeedRef':
946 case 'GPSDestDistanceRef':
947 switch( $val ) {
948 case 'K': case 'M': case 'N':
949 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
950 break;
951 default:
952 $tags[$tag] = $val;
953 break;
954 }
955 break;
956
957 case 'GPSTrackRef':
958 case 'GPSImgDirectionRef':
959 case 'GPSDestBearingRef':
960 switch( $val ) {
961 case 'T': case 'M':
962 $tags[$tag] = $this->msg( 'GPSDirection', $val );
963 break;
964 default:
965 $tags[$tag] = $val;
966 break;
967 }
968 break;
969
970 case 'GPSDateStamp':
971 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
972 break;
973
974 // This is not in the Exif standard, just a special
975 // case for our purposes which enables wikis to wikify
976 // the make, model and software name to link to their articles.
977 case 'Make':
978 case 'Model':
979 case 'Software':
980 $tags[$tag] = $this->msg( $tag, '', $val );
981 break;
982 default:
983 $tags[$tag] = $this->formatNum( $val );
984 break;
985 }
986 }
987
988 return $tags;
989 }
990
991 /**
992 * Conviniance function for getFormattedData()
993 *
994 * @access private
995 *
996 * @param string $tag The tag name to pass on
997 * @param string $val The value of the tag
998 * @param string $arg An argument to pass ($1)
999 * @return string A wfMsg of "exif-$tag-$val" in lower case
1000 */
1001 function msg( $tag, $val, $arg = null ) {
1002 if ($val === '')
1003 $val = 'value';
1004 return wfMsg( strtolower( "exif-$tag-$val" ), $arg );
1005 }
1006
1007 /**
1008 * Format a number, convert numbers from fractions into floating point
1009 * numbers
1010 *
1011 * @access private
1012 *
1013 * @param mixed $num The value to format
1014 * @return mixed A floating point number or whatever we were fed
1015 */
1016 function formatNum( $num ) {
1017 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
1018 return $m[2] != 0 ? $m[1] / $m[2] : $num;
1019 else
1020 return $num;
1021 }
1022 }