MediaWiki  REL1_22
Exif.php
Go to the documentation of this file.
00001 <?php
00032 class Exif {
00033 
00034     const BYTE = 1; 
00035     const ASCII = 2; 
00036     const SHORT = 3; 
00037     const LONG = 4; 
00038     const RATIONAL = 5; 
00039     const SHORT_OR_LONG = 6; 
00040     const UNDEFINED = 7; 
00041     const SLONG = 9; 
00042     const SRATIONAL = 10; 
00043     const IGNORE = -1; // A fake value for things we don't want or don't support.
00044 
00046     /* @var array
00047      * @private
00048      */
00049 
00055     var $mExifTags;
00056 
00060     var $mRawExifData;
00061 
00067     var $mFilteredExifData;
00068 
00072     var $mFormattedExifData;
00073 
00075 
00077     /* @var string
00078      * @private
00079      */
00080 
00084     var $file;
00085 
00089     var $basename;
00090 
00094     var $log = false;
00095 
00100     private $byteOrder;
00102 
00115     function __construct( $file, $byteOrder = '' ) {
00124         $this->mExifTags = array(
00125             # TIFF Rev. 6.0 Attribute Information (p22)
00126             'IFD0' => array(
00127                 # Tags relating to image structure
00128                 'ImageWidth' => Exif::SHORT_OR_LONG,        # Image width
00129                 'ImageLength' => Exif::SHORT_OR_LONG,       # Image height
00130                 'BitsPerSample' => array( Exif::SHORT, 3 ),     # Number of bits per component
00131                 # "When a primary image is JPEG compressed, this designation is not"
00132                 # "necessary and is omitted." (p23)
00133                 'Compression' => Exif::SHORT,               # Compression scheme #p23
00134                 'PhotometricInterpretation' => Exif::SHORT,     # Pixel composition #p23
00135                 'Orientation' => Exif::SHORT,               # Orientation of image #p24
00136                 'SamplesPerPixel' => Exif::SHORT,           # Number of components
00137                 'PlanarConfiguration' => Exif::SHORT,           # Image data arrangement #p24
00138                 'YCbCrSubSampling' => array( Exif::SHORT, 2 ),      # Subsampling ratio of Y to C #p24
00139                 'YCbCrPositioning' => Exif::SHORT,          # Y and C positioning #p24-25
00140                 'XResolution' => Exif::RATIONAL,            # Image resolution in width direction
00141                 'YResolution' => Exif::RATIONAL,            # Image resolution in height direction
00142                 'ResolutionUnit' => Exif::SHORT,            # Unit of X and Y resolution #(p26)
00143 
00144                 # Tags relating to recording offset
00145                 'StripOffsets' => Exif::SHORT_OR_LONG,          # Image data location
00146                 'RowsPerStrip' => Exif::SHORT_OR_LONG,          # Number of rows per strip
00147                 'StripByteCounts' => Exif::SHORT_OR_LONG,       # Bytes per compressed strip
00148                 'JPEGInterchangeFormat' => Exif::SHORT_OR_LONG,     # Offset to JPEG SOI
00149                 'JPEGInterchangeFormatLength' => Exif::SHORT_OR_LONG,   # Bytes of JPEG data
00150 
00151                 # Tags relating to image data characteristics
00152                 'TransferFunction' => Exif::IGNORE,         # Transfer function
00153                 'WhitePoint' => array( Exif::RATIONAL, 2 ),     # White point chromaticity
00154                 'PrimaryChromaticities' => array( Exif::RATIONAL, 6 ),  # Chromaticities of primarities
00155                 'YCbCrCoefficients' => array( Exif::RATIONAL, 3 ),  # Color space transformation matrix coefficients #p27
00156                 'ReferenceBlackWhite' => array( Exif::RATIONAL, 6 ),    # Pair of black and white reference values
00157 
00158                 # Other tags
00159                 'DateTime' => Exif::ASCII,              # File change date and time
00160                 'ImageDescription' => Exif::ASCII,          # Image title
00161                 'Make' => Exif::ASCII,                  # Image input equipment manufacturer
00162                 'Model' => Exif::ASCII,                 # Image input equipment model
00163                 'Software' => Exif::ASCII,              # Software used
00164                 'Artist' => Exif::ASCII,                # Person who created the image
00165                 'Copyright' => Exif::ASCII,             # Copyright holder
00166             ),
00167 
00168             # Exif IFD Attribute Information (p30-31)
00169             'EXIF' => array(
00170                 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
00171                 # to the Exif 2.1 AND 2.2 standards
00172                 'ExifVersion' => Exif::UNDEFINED,           # Exif version
00173                 'FlashPixVersion' => Exif::UNDEFINED,           # Supported Flashpix version #p32
00174 
00175                 # Tags relating to Image Data Characteristics
00176                 'ColorSpace' => Exif::SHORT,                # Color space information #p32
00177 
00178                 # Tags relating to image configuration
00179                 'ComponentsConfiguration' => Exif::UNDEFINED,           # Meaning of each component #p33
00180                 'CompressedBitsPerPixel' => Exif::RATIONAL,         # Image compression mode
00181                 'PixelYDimension' => Exif::SHORT_OR_LONG,       # Valid image width
00182                 'PixelXDimension' => Exif::SHORT_OR_LONG,       # Valid image height
00183 
00184                 # Tags relating to related user information
00185                 'MakerNote' => Exif::IGNORE,                # Manufacturer notes
00186                 'UserComment' => Exif::UNDEFINED,           # User comments #p34
00187 
00188                 # Tags relating to related file information
00189                 'RelatedSoundFile' => Exif::ASCII,          # Related audio file
00190 
00191                 # Tags relating to date and time
00192                 'DateTimeOriginal' => Exif::ASCII,          # Date and time of original data generation #p36
00193                 'DateTimeDigitized' => Exif::ASCII,         # Date and time of original data generation
00194                 'SubSecTime' => Exif::ASCII,                # DateTime subseconds
00195                 'SubSecTimeOriginal' => Exif::ASCII,            # DateTimeOriginal subseconds
00196                 'SubSecTimeDigitized' => Exif::ASCII,           # DateTimeDigitized subseconds
00197 
00198                 # Tags relating to picture-taking conditions (p31)
00199                 'ExposureTime' => Exif::RATIONAL,           # Exposure time
00200                 'FNumber' => Exif::RATIONAL,                # F Number
00201                 'ExposureProgram' => Exif::SHORT,           # Exposure Program #p38
00202                 'SpectralSensitivity' => Exif::ASCII,           # Spectral sensitivity
00203                 'ISOSpeedRatings' => Exif::SHORT,           # ISO speed rating
00204                 'OECF' => Exif::IGNORE,
00205                 # Optoelectronic conversion factor. Note: We don't have support for this atm.
00206                 'ShutterSpeedValue' => Exif::SRATIONAL,         # Shutter speed
00207                 'ApertureValue' => Exif::RATIONAL,          # Aperture
00208                 'BrightnessValue' => Exif::SRATIONAL,           # Brightness
00209                 'ExposureBiasValue' => Exif::SRATIONAL,         # Exposure bias
00210                 'MaxApertureValue' => Exif::RATIONAL,           # Maximum land aperture
00211                 'SubjectDistance' => Exif::RATIONAL,            # Subject distance
00212                 'MeteringMode' => Exif::SHORT,              # Metering mode #p40
00213                 'LightSource' => Exif::SHORT,               # Light source #p40-41
00214                 'Flash' => Exif::SHORT,                 # Flash #p41-42
00215                 'FocalLength' => Exif::RATIONAL,            # Lens focal length
00216                 'SubjectArea' => array( Exif::SHORT, 4 ),       # Subject area
00217                 'FlashEnergy' => Exif::RATIONAL,            # Flash energy
00218                 'SpatialFrequencyResponse' => Exif::IGNORE,     # Spatial frequency response. Not supported atm.
00219                 'FocalPlaneXResolution' => Exif::RATIONAL,      # Focal plane X resolution
00220                 'FocalPlaneYResolution' => Exif::RATIONAL,      # Focal plane Y resolution
00221                 'FocalPlaneResolutionUnit' => Exif::SHORT,      # Focal plane resolution unit #p46
00222                 'SubjectLocation' => array( Exif::SHORT, 2 ),       # Subject location
00223                 'ExposureIndex' => Exif::RATIONAL,          # Exposure index
00224                 'SensingMethod' => Exif::SHORT,             # Sensing method #p46
00225                 'FileSource' => Exif::UNDEFINED,            # File source #p47
00226                 'SceneType' => Exif::UNDEFINED,             # Scene type #p47
00227                 'CFAPattern' => Exif::IGNORE,               # CFA pattern. not supported atm.
00228                 'CustomRendered' => Exif::SHORT,            # Custom image processing #p48
00229                 'ExposureMode' => Exif::SHORT,              # Exposure mode #p48
00230                 'WhiteBalance' => Exif::SHORT,              # White Balance #p49
00231                 'DigitalZoomRatio' => Exif::RATIONAL,           # Digital zoom ration
00232                 'FocalLengthIn35mmFilm' => Exif::SHORT,         # Focal length in 35 mm film
00233                 'SceneCaptureType' => Exif::SHORT,          # Scene capture type #p49
00234                 'GainControl' => Exif::SHORT,               # Scene control #p49-50
00235                 'Contrast' => Exif::SHORT,              # Contrast #p50
00236                 'Saturation' => Exif::SHORT,                # Saturation #p50
00237                 'Sharpness' => Exif::SHORT,             # Sharpness #p50
00238                 'DeviceSettingDescription' => Exif::IGNORE,
00239                 # Device settings description. This could maybe be supported. Need to find an
00240                 # example file that uses this to see if it has stuff of interest in it.
00241                 'SubjectDistanceRange' => Exif::SHORT,          # Subject distance range #p51
00242 
00243                 'ImageUniqueID' => Exif::ASCII,             # Unique image ID
00244             ),
00245 
00246             # GPS Attribute Information (p52)
00247             'GPS' => array(
00248                 'GPSVersion' => Exif::UNDEFINED,
00249                 # Should be an array of 4 Exif::BYTE's. However php treats it as an undefined
00250                 # Note exif standard calls this GPSVersionID, but php doesn't like the id suffix
00251                 'GPSLatitudeRef' => Exif::ASCII,            # North or South Latitude #p52-53
00252                 'GPSLatitude' => array( Exif::RATIONAL, 3 ),        # Latitude
00253                 'GPSLongitudeRef' => Exif::ASCII,           # East or West Longitude #p53
00254                 'GPSLongitude' => array( Exif::RATIONAL, 3 ),       # Longitude
00255                 'GPSAltitudeRef' => Exif::UNDEFINED,
00256                 # Altitude reference. Note, the exif standard says this should be an EXIF::Byte,
00257                 # but php seems to disagree.
00258                 'GPSAltitude' => Exif::RATIONAL,            # Altitude
00259                 'GPSTimeStamp' => array( Exif::RATIONAL, 3 ),       # GPS time (atomic clock)
00260                 'GPSSatellites' => Exif::ASCII,             # Satellites used for measurement
00261                 'GPSStatus' => Exif::ASCII,             # Receiver status #p54
00262                 'GPSMeasureMode' => Exif::ASCII,            # Measurement mode #p54-55
00263                 'GPSDOP' => Exif::RATIONAL,             # Measurement precision
00264                 'GPSSpeedRef' => Exif::ASCII,               # Speed unit #p55
00265                 'GPSSpeed' => Exif::RATIONAL,               # Speed of GPS receiver
00266                 'GPSTrackRef' => Exif::ASCII,               # Reference for direction of movement #p55
00267                 'GPSTrack' => Exif::RATIONAL,               # Direction of movement
00268                 'GPSImgDirectionRef' => Exif::ASCII,            # Reference for direction of image #p56
00269                 'GPSImgDirection' => Exif::RATIONAL,            # Direction of image
00270                 'GPSMapDatum' => Exif::ASCII,               # Geodetic survey data used
00271                 'GPSDestLatitudeRef' => Exif::ASCII,            # Reference for latitude of destination #p56
00272                 'GPSDestLatitude' => array( Exif::RATIONAL, 3 ),    # Latitude destination
00273                 'GPSDestLongitudeRef' => Exif::ASCII,           # Reference for longitude of destination #p57
00274                 'GPSDestLongitude' => array( Exif::RATIONAL, 3 ),   # Longitude of destination
00275                 'GPSDestBearingRef' => Exif::ASCII,         # Reference for bearing of destination #p57
00276                 'GPSDestBearing' => Exif::RATIONAL,         # Bearing of destination
00277                 'GPSDestDistanceRef' => Exif::ASCII,            # Reference for distance to destination #p57-58
00278                 'GPSDestDistance' => Exif::RATIONAL,            # Distance to destination
00279                 'GPSProcessingMethod' => Exif::UNDEFINED,       # Name of GPS processing method
00280                 'GPSAreaInformation' => Exif::UNDEFINED,        # Name of GPS area
00281                 'GPSDateStamp' => Exif::ASCII,              # GPS date
00282                 'GPSDifferential' => Exif::SHORT,           # GPS differential correction
00283             ),
00284         );
00285 
00286         $this->file = $file;
00287         $this->basename = wfBaseName( $this->file );
00288         if ( $byteOrder === 'BE' || $byteOrder === 'LE' ) {
00289             $this->byteOrder = $byteOrder;
00290         } else {
00291             // Only give a warning for b/c, since originally we didn't
00292             // require this. The number of things affected by this is
00293             // rather small.
00294             wfWarn( 'Exif class did not have byte order specified. ' .
00295                 'Some properties may be decoded incorrectly.' );
00296             $this->byteOrder = 'BE'; // BE seems about twice as popular as LE in jpg's.
00297         }
00298 
00299         $this->debugFile( $this->basename, __FUNCTION__, true );
00300         if ( function_exists( 'exif_read_data' ) ) {
00301             wfSuppressWarnings();
00302             $data = exif_read_data( $this->file, 0, true );
00303             wfRestoreWarnings();
00304         } else {
00305             throw new MWException( "Internal error: exif_read_data not present. \$wgShowEXIF may be incorrectly set or not checked by an extension." );
00306         }
00312         $this->mRawExifData = $data ? $data : array();
00313         $this->makeFilteredData();
00314         $this->collapseData();
00315         $this->debugFile( __FUNCTION__, false );
00316     }
00317 
00321     function makeFilteredData() {
00322         $this->mFilteredExifData = Array();
00323 
00324         foreach ( array_keys( $this->mRawExifData ) as $section ) {
00325             if ( !in_array( $section, array_keys( $this->mExifTags ) ) ) {
00326                 $this->debug( $section, __FUNCTION__, "'$section' is not a valid Exif section" );
00327                 continue;
00328             }
00329 
00330             foreach ( array_keys( $this->mRawExifData[$section] ) as $tag ) {
00331                 if ( !in_array( $tag, array_keys( $this->mExifTags[$section] ) ) ) {
00332                     $this->debug( $tag, __FUNCTION__, "'$tag' is not a valid tag in '$section'" );
00333                     continue;
00334                 }
00335 
00336                 $this->mFilteredExifData[$tag] = $this->mRawExifData[$section][$tag];
00337                 // This is ok, as the tags in the different sections do not conflict.
00338                 // except in computed and thumbnail section, which we don't use.
00339 
00340                 $value = $this->mRawExifData[$section][$tag];
00341                 if ( !$this->validate( $section, $tag, $value ) ) {
00342                     $this->debug( $value, __FUNCTION__, "'$tag' contained invalid data" );
00343                     unset( $this->mFilteredExifData[$tag] );
00344                 }
00345             }
00346         }
00347     }
00348 
00367     function collapseData() {
00368 
00369         $this->exifGPStoNumber( 'GPSLatitude' );
00370         $this->exifGPStoNumber( 'GPSDestLatitude' );
00371         $this->exifGPStoNumber( 'GPSLongitude' );
00372         $this->exifGPStoNumber( 'GPSDestLongitude' );
00373 
00374         if ( isset( $this->mFilteredExifData['GPSAltitude'] ) && isset( $this->mFilteredExifData['GPSAltitudeRef'] ) ) {
00375 
00376             // We know altitude data is a <num>/<denom> from the validation functions ran earlier.
00377             // But multiplying such a string by -1 doesn't work well, so convert.
00378             list( $num, $denom ) = explode( '/', $this->mFilteredExifData['GPSAltitude'] );
00379             $this->mFilteredExifData['GPSAltitude'] = $num / $denom;
00380 
00381             if ( $this->mFilteredExifData['GPSAltitudeRef'] === "\1" ) {
00382                 $this->mFilteredExifData['GPSAltitude'] *= - 1;
00383             }
00384             unset( $this->mFilteredExifData['GPSAltitudeRef'] );
00385         }
00386 
00387         $this->exifPropToOrd( 'FileSource' );
00388         $this->exifPropToOrd( 'SceneType' );
00389 
00390         $this->charCodeString( 'UserComment' );
00391         $this->charCodeString( 'GPSProcessingMethod' );
00392         $this->charCodeString( 'GPSAreaInformation' );
00393 
00394         //ComponentsConfiguration should really be an array instead of a string...
00395         //This turns a string of binary numbers into an array of numbers.
00396 
00397         if ( isset( $this->mFilteredExifData['ComponentsConfiguration'] ) ) {
00398             $val = $this->mFilteredExifData['ComponentsConfiguration'];
00399             $ccVals = array();
00400             for ( $i = 0; $i < strlen( $val ); $i++ ) {
00401                 $ccVals[$i] = ord( substr( $val, $i, 1 ) );
00402             }
00403             $ccVals['_type'] = 'ol'; //this is for formatting later.
00404             $this->mFilteredExifData['ComponentsConfiguration'] = $ccVals;
00405         }
00406 
00407         //GPSVersion(ID) is treated as the wrong type by php exif support.
00408         //Go through each byte turning it into a version string.
00409         //For example: "\x02\x02\x00\x00" -> "2.2.0.0"
00410 
00411         //Also change exif tag name from GPSVersion (what php exif thinks it is)
00412         //to GPSVersionID (what the exif standard thinks it is).
00413 
00414         if ( isset( $this->mFilteredExifData['GPSVersion'] ) ) {
00415             $val = $this->mFilteredExifData['GPSVersion'];
00416             $newVal = '';
00417             for ( $i = 0; $i < strlen( $val ); $i++ ) {
00418                 if ( $i !== 0 ) {
00419                     $newVal .= '.';
00420                 }
00421                 $newVal .= ord( substr( $val, $i, 1 ) );
00422             }
00423             if ( $this->byteOrder === 'LE' ) {
00424                 // Need to reverse the string
00425                 $newVal2 = '';
00426                 for ( $i = strlen( $newVal ) - 1; $i >= 0; $i-- ) {
00427                     $newVal2 .= substr( $newVal, $i, 1 );
00428                 }
00429                 $this->mFilteredExifData['GPSVersionID'] = $newVal2;
00430             } else {
00431                 $this->mFilteredExifData['GPSVersionID'] = $newVal;
00432             }
00433             unset( $this->mFilteredExifData['GPSVersion'] );
00434         }
00435 
00436     }
00443     private function charCodeString( $prop ) {
00444         if ( isset( $this->mFilteredExifData[$prop] ) ) {
00445 
00446             if ( strlen( $this->mFilteredExifData[$prop] ) <= 8 ) {
00447                 //invalid. Must be at least 9 bytes long.
00448 
00449                 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, false );
00450                 unset( $this->mFilteredExifData[$prop] );
00451                 return;
00452             }
00453             $charCode = substr( $this->mFilteredExifData[$prop], 0, 8 );
00454             $val = substr( $this->mFilteredExifData[$prop], 8 );
00455 
00456             switch ( $charCode ) {
00457                 case "\x4A\x49\x53\x00\x00\x00\x00\x00":
00458                     //JIS
00459                     $charset = "Shift-JIS";
00460                     break;
00461                 case "UNICODE\x00":
00462                     $charset = "UTF-16" . $this->byteOrder;
00463                     break;
00464                 default: //ascii or undefined.
00465                     $charset = "";
00466                     break;
00467             }
00468             // This could possibly check to see if iconv is really installed
00469             // or if we're using the compatibility wrapper in globalFunctions.php
00470             if ( $charset ) {
00471                 wfSuppressWarnings();
00472                 $val = iconv( $charset, 'UTF-8//IGNORE', $val );
00473                 wfRestoreWarnings();
00474             } else {
00475                 // if valid utf-8, assume that, otherwise assume windows-1252
00476                 $valCopy = $val;
00477                 UtfNormal::quickIsNFCVerify( $valCopy ); //validates $valCopy.
00478                 if ( $valCopy !== $val ) {
00479                     wfSuppressWarnings();
00480                     $val = iconv( 'Windows-1252', 'UTF-8//IGNORE', $val );
00481                     wfRestoreWarnings();
00482                 }
00483             }
00484 
00485             //trim and check to make sure not only whitespace.
00486             $val = trim( $val );
00487             if ( strlen( $val ) === 0 ) {
00488                 //only whitespace.
00489                 $this->debug( $this->mFilteredExifData[$prop], __FUNCTION__, "$prop: Is only whitespace" );
00490                 unset( $this->mFilteredExifData[$prop] );
00491                 return;
00492             }
00493 
00494             //all's good.
00495             $this->mFilteredExifData[$prop] = $val;
00496         }
00497     }
00504     private function exifPropToOrd( $prop ) {
00505         if ( isset( $this->mFilteredExifData[$prop] ) ) {
00506             $this->mFilteredExifData[$prop] = ord( $this->mFilteredExifData[$prop] );
00507         }
00508     }
00514     private function exifGPStoNumber( $prop ) {
00515         $loc =& $this->mFilteredExifData[$prop];
00516         $dir =& $this->mFilteredExifData[$prop . 'Ref'];
00517         $res = false;
00518 
00519         if ( isset( $loc ) && isset( $dir ) && ( $dir === 'N' || $dir === 'S' || $dir === 'E' || $dir === 'W' ) ) {
00520             list( $num, $denom ) = explode( '/', $loc[0] );
00521             $res = $num / $denom;
00522             list( $num, $denom ) = explode( '/', $loc[1] );
00523             $res += ( $num / $denom ) * ( 1 / 60 );
00524             list( $num, $denom ) = explode( '/', $loc[2] );
00525             $res += ( $num / $denom ) * ( 1 / 3600 );
00526 
00527             if ( $dir === 'S' || $dir === 'W' ) {
00528                 $res *= - 1; // make negative
00529             }
00530         }
00531 
00532         // update the exif records.
00533 
00534         if ( $res !== false ) { // using !== as $res could potentially be 0
00535             $this->mFilteredExifData[$prop] = $res;
00536             unset( $this->mFilteredExifData[$prop . 'Ref'] );
00537         } else { // if invalid
00538             unset( $this->mFilteredExifData[$prop] );
00539             unset( $this->mFilteredExifData[$prop . 'Ref'] );
00540         }
00541     }
00542 
00549     function makeFormattedData() {
00550         wfDeprecated( __METHOD__, '1.18' );
00551         $this->mFormattedExifData = FormatMetadata::getFormattedData(
00552             $this->mFilteredExifData );
00553     }
00563     function getData() {
00564         return $this->mRawExifData;
00565     }
00566 
00570     function getFilteredData() {
00571         return $this->mFilteredExifData;
00572     }
00573 
00582     function getFormattedData() {
00583         wfDeprecated( __METHOD__, '1.18' );
00584         if ( !$this->mFormattedExifData ) {
00585             $this->makeFormattedData();
00586         }
00587         return $this->mFormattedExifData;
00588     }
00603     public static function version() {
00604         return 2; // We don't need no bloddy constants!
00605     }
00606 
00615     private function isByte( $in ) {
00616         if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 255 ) {
00617             $this->debug( $in, __FUNCTION__, true );
00618             return true;
00619         } else {
00620             $this->debug( $in, __FUNCTION__, false );
00621             return false;
00622         }
00623     }
00624 
00629     private function isASCII( $in ) {
00630         if ( is_array( $in ) ) {
00631             return false;
00632         }
00633 
00634         if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
00635             $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
00636             return false;
00637         }
00638 
00639         if ( preg_match( '/^\s*$/', $in ) ) {
00640             $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
00641             return false;
00642         }
00643 
00644         return true;
00645     }
00646 
00651     private function isShort( $in ) {
00652         if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 65536 ) {
00653             $this->debug( $in, __FUNCTION__, true );
00654             return true;
00655         } else {
00656             $this->debug( $in, __FUNCTION__, false );
00657             return false;
00658         }
00659     }
00660 
00665     private function isLong( $in ) {
00666         if ( !is_array( $in ) && sprintf( '%d', $in ) == $in && $in >= 0 && $in <= 4294967296 ) {
00667             $this->debug( $in, __FUNCTION__, true );
00668             return true;
00669         } else {
00670             $this->debug( $in, __FUNCTION__, false );
00671             return false;
00672         }
00673     }
00674 
00679     private function isRational( $in ) {
00680         $m = array();
00681         if ( !is_array( $in ) && preg_match( '/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) ) { # Avoid division by zero
00682             return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
00683         } else {
00684             $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
00685             return false;
00686         }
00687     }
00688 
00693     private function isUndefined( $in ) {
00694         $this->debug( $in, __FUNCTION__, true );
00695         return true;
00696     }
00697 
00702     private function isSlong( $in ) {
00703         if ( $this->isLong( abs( $in ) ) ) {
00704             $this->debug( $in, __FUNCTION__, true );
00705             return true;
00706         } else {
00707             $this->debug( $in, __FUNCTION__, false );
00708             return false;
00709         }
00710     }
00711 
00716     private function isSrational( $in ) {
00717         $m = array();
00718         if ( !is_array( $in ) && preg_match( '/^(-?\d+)\/(\d+[1-9]|[1-9]\d*)$/', $in, $m ) ) { # Avoid division by zero
00719             return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
00720         } else {
00721             $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
00722             return false;
00723         }
00724     }
00737     private function validate( $section, $tag, $val, $recursive = false ) {
00738         $debug = "tag is '$tag'";
00739         $etype = $this->mExifTags[$section][$tag];
00740         $ecount = 1;
00741         if ( is_array( $etype ) ) {
00742             list( $etype, $ecount ) = $etype;
00743             if ( $recursive ) {
00744                 $ecount = 1; // checking individual elements
00745             }
00746         }
00747         $count = count( $val );
00748         if ( $ecount != $count ) {
00749             $this->debug( $val, __FUNCTION__, "Expected $ecount elements for $tag but got $count" );
00750             return false;
00751         }
00752         if ( $count > 1 ) {
00753             foreach ( $val as $v ) {
00754                 if ( !$this->validate( $section, $tag, $v, true ) ) {
00755                     return false;
00756                 }
00757             }
00758             return true;
00759         }
00760         // Does not work if not typecast
00761         switch ( (string)$etype ) {
00762             case (string)Exif::BYTE:
00763                 $this->debug( $val, __FUNCTION__, $debug );
00764                 return $this->isByte( $val );
00765             case (string)Exif::ASCII:
00766                 $this->debug( $val, __FUNCTION__, $debug );
00767                 return $this->isASCII( $val );
00768             case (string)Exif::SHORT:
00769                 $this->debug( $val, __FUNCTION__, $debug );
00770                 return $this->isShort( $val );
00771             case (string)Exif::LONG:
00772                 $this->debug( $val, __FUNCTION__, $debug );
00773                 return $this->isLong( $val );
00774             case (string)Exif::RATIONAL:
00775                 $this->debug( $val, __FUNCTION__, $debug );
00776                 return $this->isRational( $val );
00777             case (string)Exif::SHORT_OR_LONG:
00778                 $this->debug( $val, __FUNCTION__, $debug );
00779                 return $this->isShort( $val ) || $this->isLong( $val );
00780             case (string)Exif::UNDEFINED:
00781                 $this->debug( $val, __FUNCTION__, $debug );
00782                 return $this->isUndefined( $val );
00783             case (string)Exif::SLONG:
00784                 $this->debug( $val, __FUNCTION__, $debug );
00785                 return $this->isSlong( $val );
00786             case (string)Exif::SRATIONAL:
00787                 $this->debug( $val, __FUNCTION__, $debug );
00788                 return $this->isSrational( $val );
00789             case (string)Exif::IGNORE:
00790                 $this->debug( $val, __FUNCTION__, $debug );
00791                 return false;
00792             default:
00793                 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
00794                 return false;
00795         }
00796     }
00797 
00807     private function debug( $in, $fname, $action = null ) {
00808         if ( !$this->log ) {
00809             return;
00810         }
00811         $type = gettype( $in );
00812         $class = ucfirst( __CLASS__ );
00813         if ( is_array( $in ) ) {
00814             $in = print_r( $in, true );
00815         }
00816 
00817         if ( $action === true ) {
00818             wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n" );
00819         } elseif ( $action === false ) {
00820             wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n" );
00821         } elseif ( $action === null ) {
00822             wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n" );
00823         } else {
00824             wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n" );
00825         }
00826     }
00827 
00836     private function debugFile( $fname, $io ) {
00837         if ( !$this->log ) {
00838             return;
00839         }
00840         $class = ucfirst( __CLASS__ );
00841         if ( $io ) {
00842             wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
00843         } else {
00844             wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
00845         }
00846     }
00847 }