Remove never used stuff. There is some $rand = wfRandom calls that might
[lhc/web/wiklou.git] / includes / Exif.php
index b3bf369..9c3965a 100644 (file)
@@ -30,7 +30,7 @@ if ( !defined( 'MEDIAWIKI' ) ) die();
 /**#@+
  * Exif tag type definition
  */
-define('MW_EXIF_BYTE', 1);             # An 8-bit unsigned integer.
+define('MW_EXIF_BYTE', 1);             # An 8-bit (1-byte) unsigned integer.
 define('MW_EXIF_ASCII', 2);            # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
 define('MW_EXIF_SHORT', 3);            # A 16-bit (2-byte) unsigned integer.
 define('MW_EXIF_LONG', 4);             # A 32-bit (4-byte) unsigned integer.
@@ -80,10 +80,34 @@ class Exif {
         */
        var $mFormattedExifData;
        
+       /**#@-*/
+       
+       /**#@+
+        * @var string
+        * @access private
+        */
+       
+       /**
+        * The file being processed
+        */
+       var $file;
+
+       /**
+        * The basename of the file being processed
+        */
+       var $basename;
+       
+       /**
+        * The private log to log to
+        */
+       var $log = 'exif';
+       
        /**#@-*/
 
        /**
         * Constructor
+        *
+        * @param string $file
         */
        function Exif( $file ) {
                /**
@@ -209,7 +233,7 @@ class Exif {
                                        'SpatialFrequencyResponse' => MW_EXIF_UNDEFINED,        # Spatial frequency response
                                        'FocalPlaneXResolution' => MW_EXIF_RATIONAL,            # Focal plane X resolution
                                        'FocalPlaneYResolution' => MW_EXIF_RATIONAL,            # Focal plane Y resolution
-                                       'FocalPlaneResolutionUnit' => MW_EXIF_SHORT,            # Focal plane resolution unit
+                                       'FocalPlaneResolutionUnit' => MW_EXIF_SHORT,            # Focal plane resolution unit #p46
                                        'SubjectLocation' => MW_EXIF_SHORT,                     # Subject location
                                        'ExposureIndex' => MW_EXIF_RATIONAL,                    # Exposure index
                                        'SensingMethod' => MW_EXIF_SHORT,                       # Sensing method #p46
@@ -271,12 +295,26 @@ class Exif {
                        ),
                );
 
+               $this->file = $file;
+               $this->basename = basename( $this->file );
+               
                $this->makeFlatExifTags();
+               
+               $this->debugFile( $this->basename, __FUNCTION__, true );
                wfSuppressWarnings();
-               $this->mRawExifData = exif_read_data( $file );
+               $data = exif_read_data( $this->file );
                wfRestoreWarnings();
+               /**
+                * exif_read_data() will return false on invalid input, such as
+                * when somebody uploads a file called something.jpeg
+                * containing random gibberish.
+                */
+               $this->mRawExifData = $data ? $data : array();
+               
                $this->makeFilteredData();
                $this->makeFormattedData();
+               
+               $this->debugFile( __FUNCTION__, false );
        }
        
        /**#@+
@@ -305,8 +343,6 @@ class Exif {
                }
        }
        
-       /**#@-*/
-       
        /**
         * Make $this->mFilteredExifData
         */
@@ -377,12 +413,14 @@ class Exif {
 
        /**#@+
         * Validates if a tag value is of the type it should be according to the Exif spec
-        * 
+        *
+        * @access private
+        *
         * @param mixed $in The input value to check
         * @return bool
         */
        function isByte( $in ) {
-               if ( sprintf('%d', $in) === $in && $in >= 0 && $in <= 255 ) {
+               if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
                        $this->debug( $in, __FUNCTION__, true );
                        return true;
                } else {
@@ -402,26 +440,21 @@ class Exif {
                        return false;
                }
                
-               if ( strlen($in) > 1024 ) { // TODO: This might not be according to the spec
-                       $this->debug( $in, __FUNCTION__, 'input was too long' );
-                       return false;
-               }
-               
                return true;
        }
 
        function isShort( $in ) {
-               if ( sprintf('%d', $in) === $in && $in >= 0 && $in <= 65536 ) {
+               if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
                        $this->debug( $in, __FUNCTION__, true );
                        return true;
                } else {
-                       $this->debug( $in, __FUNCTION__, true );
+                       $this->debug( $in, __FUNCTION__, false );
                        return false;
                }
        }
 
        function isLong( $in ) {
-               if ( sprintf('%d', $in) === $in && $in >= 0 && $in <= 4294967296 ) {
+               if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
                        $this->debug( $in, __FUNCTION__, true );
                        return true;
                } else {
@@ -431,7 +464,7 @@ class Exif {
        }
        
        function isRational( $in ) {
-               if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
+               if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
                        return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
                } else {
                        $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
@@ -440,10 +473,11 @@ class Exif {
        }
 
        function isUndefined( $in ) {
-               $this->debug( $in, __FUNCTION__ );
                if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
+                       $this->debug( $in, __FUNCTION__, true );
                        return true;
                } else {
+                       $this->debug( $in, __FUNCTION__, false );
                        return false;
                }
        }
@@ -471,30 +505,42 @@ class Exif {
        /**
         * Validates if a tag has a legal value according to the Exif spec
         *
+        * @access private
+        *
         * @param string $tag The tag to check
         * @param mixed  $val The value of the tag
         * @return bool
         */
        function validate( $tag, $val ) {
+               $debug = "tag is '$tag'";
                // Fucks up if not typecast 
                switch( (string)$this->mFlatExifTags[$tag] ) {
                        case (string)MW_EXIF_BYTE:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isByte( $val );
                        case (string)MW_EXIF_ASCII:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isASCII( $val );
                        case (string)MW_EXIF_SHORT:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isShort( $val );
                        case (string)MW_EXIF_LONG:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isLong( $val );
                        case (string)MW_EXIF_RATIONAL:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isRational( $val );
                        case (string)MW_EXIF_UNDEFINED:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isUndefined( $val );
                        case (string)MW_EXIF_SLONG:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isSlong( $val );
                        case (string)MW_EXIF_SRATIONAL:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isSrational( $val );
                        case (string)MW_EXIF_SHORT.','.MW_EXIF_LONG:
+                               $this->debug( $val, __FUNCTION__, $debug );
                                return $this->isShort( $val ) || $this->isLong( $val );
                        default:
                                $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
@@ -505,6 +551,8 @@ class Exif {
        /**
         * Conviniance function for debugging output
         *
+        * @access private
+        *
         * @param mixed $in 
         * @param string $fname
         * @param mixed $action
@@ -516,13 +564,30 @@ class Exif {
                        $in = print_r( $in, true ); 
         
                if ( $action === true )
-                       wfDebug( "$class::$fname: accepted: '$in' (type: $type)\n");
+                       wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
                elseif ( $action === false ) 
-                       wfDebug( "$class::$fname: rejected: '$in' (type: $type)\n");
+                       wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
                elseif ( $action === null )
-                       wfDebug( "$class::$fname: input was: '$in' (type: $type)\n");
+                       wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
                else
-                       wfDebug( "$class::$fname: $action (type: $type; content: '$in')\n");
+                       wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
+       }
+
+       /**
+        * Conviniance function for debugging output
+        *
+        * @access private
+        *
+        * @param string $basename The name of the file being processed
+        * @paran string $fname The name of the function calling this function
+        * @param bool $bool $io Specify whether we're beginning or ending
+        */
+       function debugFile( $fname, $io ) {
+               $class = ucfirst( __CLASS__ );
+               if ( $io )
+                       wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
+               else
+                       wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
        }
 
 }
@@ -539,7 +604,7 @@ class FormatExif {
         * @access private
         */
        var $mExif;
-
+       
        /**
         * Constructor
         *
@@ -562,6 +627,9 @@ class FormatExif {
                global $wgLang;
                
                $tags =& $this->mExif;
+
+               $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
+               unset( $tags['ResolutionUnit'] );
                
                foreach( $tags as $tag => $val ) {
                        switch( $tag ) {
@@ -574,6 +642,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'PhotometricInterpretation':
                                switch( $val ) {
@@ -584,6 +653,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                        
                        case 'Orientation':
                                switch( $val ) {
@@ -594,6 +664,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                        
                        case 'PlanarConfiguration':
                                switch( $val ) {
@@ -604,20 +675,26 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                        
                        // TODO: YCbCrSubSampling
                        // TODO: YCbCrPositioning
-                       // TODO: If this field does not exists use 2
-                       case 'ResolutionUnit': #p26
-                               switch( $val ) {
-                               case 2: case 3:
-                                       $tags[$tag] = $this->msg( $tag, $val );
-                                       break;
-                               default:
-                                       $tags[$tag] = $val;
-                                       break;
-                               }
                        
+                       case 'XResolution':
+                       case 'YResolution':
+                               switch( $resolutionunit ) {
+                                       case 2:
+                                               $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
+                                               break;
+                                       case 3:
+                                               $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
+                                               break;
+                                       default:
+                                               $tags[$tag] = $val;
+                                               break;
+                               }
+                               break;
+                               
                        // TODO: YCbCrCoefficients  #p27 (see annex E)
                        case 'ExifVersion': case 'FlashpixVersion':
                                $tags[$tag] = "$val"/100;
@@ -632,6 +709,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                        
                        case 'ComponentsConfiguration':
                                switch( $val ) {
@@ -642,6 +720,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                        
                        case 'DateTime':
                        case 'DateTimeOriginal':
@@ -658,6 +737,11 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
+
+                       case 'SubjectDistance':
+                               $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
+                               break;
        
                        case 'MeteringMode':
                                switch( $val ) {
@@ -668,6 +752,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'LightSource':
                                switch( $val ) {
@@ -680,8 +765,20 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        // TODO: Flash
+                       case 'FocalPlaneResolutionUnit':
+                               switch( $val ) {
+                               case 2:
+                                       $tags[$tag] = $this->msg( $tag, $val );
+                                       break;
+                               default:
+                                       $tags[$tag] = $val;
+                                       break;
+                               }
+                               break;
+                                               
                        case 'SensingMethod':
                                switch( $val ) {
                                case 1: case 2: case 3: case 4: case 5: case 7: case 8:
@@ -691,6 +788,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'FileSource':
                                switch( $val ) {
@@ -701,6 +799,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'SceneType':
                                switch( $val ) {
@@ -711,6 +810,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'CustomRendered':
                                switch( $val ) {
@@ -721,6 +821,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'ExposureMode':
                                switch( $val ) {
@@ -731,6 +832,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'WhiteBalance':
                                switch( $val ) {
@@ -741,6 +843,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'SceneCaptureType':
                                switch( $val ) {
@@ -751,6 +854,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
        
                        case 'GainControl':
                                switch( $val ) {
@@ -761,6 +865,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'Contrast':
                                switch( $val ) {
@@ -771,6 +876,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'Saturation':
                                switch( $val ) {
@@ -781,6 +887,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'Sharpness':
                                switch( $val ) {
@@ -791,6 +898,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'SubjectDistanceRange':
                                switch( $val ) {
@@ -801,6 +909,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSLatitudeRef':
                        case 'GPSDestLatitudeRef':
@@ -812,6 +921,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSLongitudeRef':
                        case 'GPSDestLongitudeRef':
@@ -823,6 +933,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSStatus':
                                switch( $val ) {
@@ -833,6 +944,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSMeasureMode':
                                switch( $val ) {
@@ -843,6 +955,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSSpeedRef':
                        case 'GPSDestDistanceRef':
@@ -854,6 +967,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSTrackRef':
                        case 'GPSImgDirectionRef':
@@ -866,6 +980,7 @@ class FormatExif {
                                        $tags[$tag] = $val;
                                        break;
                                }
+                               break;
                                
                        case 'GPSDateStamp':
                                $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
@@ -877,14 +992,27 @@ class FormatExif {
                        case 'Make':
                        case 'Model':
                        case 'Software':
-                               $tags[$tag] = wfMsg( strtolower( "exif-$tag-value" ), $val );
+                               $tags[$tag] = $this->msg( $tag, '', $val );
+                               break;
+                       
+                       case 'ExposureTime':
+                               // Show the pretty fraction as well as decimal version
+                               $tags[$tag] = wfMsg( 'exif-exposuretime-format',
+                                       $this->formatFraction( $val ), $this->formatNum( $val ) );
+                               break;
+                       
+                       case 'FNumber':
+                               $tags[$tag] = wfMsg( 'exif-fnumber-format',
+                                       $this->formatNum( $val ) );
                                break;
+                       
+                       case 'FocalLength':
+                               $tags[$tag] = wfMsg( 'exif-focallength-format',
+                                       $this->formatNum( $val ) );
+                               break;
+                       
                        default:
-                               if ( preg_match( '/^(\d+)\/(\d+)$/', $val, $m ) ) {
-                                       $tags[$tag] = $m[2] != 0 ? $m[1]/$m[2] : $val;
-                                       break;
-                               }
-                               $tags[$tag] = $val;
+                               $tags[$tag] = $this->formatNum( $val );
                                break;
                        }
                }
@@ -893,13 +1021,84 @@ class FormatExif {
        }
 
        /**
-        * Conviniance function for format()
+        * Conviniance function for getFormattedData()
+        *
+        * @access private
         *
         * @param string $tag The tag name to pass on
         * @param string $val The value of the tag
+        * @param string $arg An argument to pass ($1)
         * @return string A wfMsg of "exif-$tag-$val" in lower case
         */
-       function msg( $tag, $val ) {
-               return wfMsg( strtolower("exif-$tag-$val") );
+       function msg( $tag, $val, $arg = null ) {
+               global $wgContLang;
+               
+               if ($val === '')
+                       $val = 'value';
+               return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
+       }
+
+       /**
+        * Format a number, convert numbers from fractions into floating point
+        * numbers
+        *
+        * @access private
+        *
+        * @param mixed $num The value to format
+        * @return mixed A floating point number or whatever we were fed
+        */
+       function formatNum( $num ) {
+               if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
+                       return $m[2] != 0 ? $m[1] / $m[2] : $num;
+               else
+                       return $num;
+       }
+       
+       /**
+        * Format a rational number, reducing fractions
+        *
+        * @access private
+        *
+        * @param mixed $num The value to format
+        * @return mixed A floating point number or whatever we were fed
+        */
+       function formatFraction( $num ) {
+               if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) {
+                       $numerator = intval( $m[1] );
+                       $denominator = intval( $m[2] );
+                       $gcd = $this->gcd( $numerator, $denominator );
+                       if( $gcd != 0 ) {
+                               // 0 shouldn't happen! ;)
+                               return $numerator / $gcd . '/' . $denominator / $gcd;
+                       }
+               }
+               return $this->formatNum( $num );
+       }
+
+       /**
+        * Calculate the greatest common divisor of two integers.
+        *
+        * @param int $a
+        * @param int $b
+        * @return int
+        * @access private
+        */
+       function gcd( $a, $b ) {
+               /*
+                       // http://en.wikipedia.org/wiki/Euclidean_algorithm
+                       // Recursive form would be:
+                       if( $b == 0 )
+                               return $a;
+                       else
+                               return gcd( $b, $a % $b );
+               */
+               while( $b != 0 ) {
+                       $remainder = $a % $b;
+                       
+                       // tail recursion...
+                       $a = $b;
+                       $b = $remainder;
+               }
+               return $a;
        }
 }