Merge to trunk everything in img_metadata branch.
[lhc/web/wiklou.git] / includes / media / GIFMetadataExtractor.php
index 8d0e87e..71003b7 100644 (file)
@@ -21,6 +21,13 @@ class GIFMetadataExtractor {
        static $gif_extension_sep;
        static $gif_term;
 
+       const VERSION = 1;
+
+       // Each sub-block is less than or equal to 255 bytes.
+       // Most of the time its 255 bytes, except for in XMP
+       // blocks, where it's usually between 32-127 bytes each.
+       const MAX_SUBBLOCKS = 262144; // 5mb divided by 20.
+
        static function getMetadata( $filename ) {
                self::$gif_frame_sep = pack( "C", ord("," ) );
                self::$gif_extension_sep = pack( "C", ord("!" ) );
@@ -29,7 +36,9 @@ class GIFMetadataExtractor {
                $frameCount = 0;
                $duration = 0.0;
                $isLooped = false;
-
+               $xmp = "";
+               $comment = array();
+               
                if ( !$filename ) {
                        throw new Exception( "No file name specified" );
                } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
@@ -103,37 +112,94 @@ class GIFMetadataExtractor {
                                        if ($term != 0 ) {
                                                throw new Exception( "Malformed Graphics Control Extension block" );
                                        }
+                               } elseif ($extension_code == 0xFE) {
+                                       // Comment block(s).
+                                       $data = '';
+
+                                       $data = self::readBlock( $fh );
+                                       if ( $data === "" ) {
+                                               throw new Exception( 'Read error, zero-length comment block' );
+                                       }
+
+                                       // The standard says this should be ASCII, however its unclear if
+                                       // thats true in practise. Check to see if its valid utf-8, if so
+                                       // assume its that, otherwise assume its iso-8859-1
+                                       $dataCopy = $data;
+                                       // quickIsNFCVerify has the side effect of replacing any invalid characters
+                                       UtfNormal::quickIsNFCVerify( $dataCopy );
+
+                                       if ( $dataCopy !== $data ) {
+                                               wfSuppressWarnings();
+                                               $data = iconv( 'ISO-8859-1', 'UTF-8', $data );
+                                               wfRestoreWarnings();
+                                       }
+
+                                       $commentCount = count( $comment );
+                                       if ( $commentCount === 0
+                                               || $comment[$commentCount-1] !== $data )
+                                       {
+                                               // Some applications repeat the same comment on each
+                                               // frame of an animated GIF image, so if this comment
+                                               // is identical to the last, only extract once.
+                                               $comment[] = $data;
+                                       }
                                } elseif ($extension_code == 0xFF) {
                                        // Application extension (Netscape info about the animated gif)
+                                       // or XMP (or theoretically any other type of extension block)
                                        $blockLength = fread( $fh, 1 );
                                        $blockLength = unpack( 'C', $blockLength );
                                        $blockLength = $blockLength[1];
                                        $data = fread( $fh, $blockLength );
 
-                                       // NETSCAPE2.0 (application name)
-                                       if ($blockLength != 11 || $data != 'NETSCAPE2.0') {
+                                       if ($blockLength != 11 ) {
+                                               wfDebug( __METHOD__ . ' GIF application block with wrong length' );
                                                fseek( $fh, -($blockLength + 1), SEEK_CUR );
                                                self::skipBlock( $fh );
                                                continue;
                                        }
 
-                                       $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
-
-                                       if ($data != "\x03\x01") {
-                                               throw new Exception( "Expected \x03\x01, got $data" );
-                                       }
-
-                                       // Unsigned little-endian integer, loop count or zero for "forever"
-                                       $loopData = fread( $fh, 2 );
-                                       $loopData = unpack( 'v', $loopData );
-                                       $loopCount = $loopData[1];
-
-                                       if ($loopCount != 1) {
-                                               $isLooped = true;
+                                       // NETSCAPE2.0 (application name for animated gif)
+                                       if ( $data == 'NETSCAPE2.0' ) {
+                                       
+                                               $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
+
+                                               if ($data != "\x03\x01") {
+                                                       throw new Exception( "Expected \x03\x01, got $data" );
+                                               }
+                                               
+                                               // Unsigned little-endian integer, loop count or zero for "forever"
+                                               $loopData = fread( $fh, 2 );
+                                               $loopData = unpack( 'v', $loopData );
+                                               $loopCount = $loopData[1];
+                                               
+                                               if ($loopCount != 1) {
+                                                       $isLooped = true;
+                                               }
+                                               
+                                               // Read out terminator byte
+                                               fread( $fh, 1 );
+                                       } elseif ( $data == 'XMP DataXMP' ) {
+                                               // application name for XMP data.
+                                               // see pg 18 of XMP spec part 3.
+
+                                               $xmp = self::readBlock( $fh, true );
+
+                                               if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
+                                                       || substr( $xmp, -4 ) !== "\x03\x02\x01\x00" )
+                                               {
+                                                       // this is just a sanity check.
+                                                       throw new Exception( "XMP does not have magic trailer!" );
+                                               }
+
+                                               // strip out trailer.
+                                               $xmp = substr( $xmp, 0, -257 );
+
+                                       } else {
+                                               // unrecognized extension block
+                                               fseek( $fh, -($blockLength + 1), SEEK_CUR );
+                                               self::skipBlock( $fh );
+                                               continue;
                                        }
-
-                                       // Read out terminator byte
-                                       fread( $fh, 1 );
                                } else {
                                        self::skipBlock( $fh );
                                }
@@ -149,7 +215,9 @@ class GIFMetadataExtractor {
                return array(
                        'frameCount' => $frameCount,
                        'looped' => $isLooped,
-                       'duration' => $duration
+                       'duration' => $duration,
+                       'xmp' => $xmp,
+                       'comment' => $comment,
                );
        }
 
@@ -183,4 +251,40 @@ class GIFMetadataExtractor {
                        fread( $fh, $block_len );
                }
        }
+       /**
+        * Read a block. In the GIF format, a block is made up of
+        * several sub-blocks. Each sub block starts with one byte
+        * saying how long the sub-block is, followed by the sub-block.
+        * The entire block is terminated by a sub-block of length
+        * 0.
+        * @param $fh FileHandle
+        * @param $includeLengths Boolean Include the length bytes of the
+        *  sub-blocks in the returned value. Normally this is false,
+        *  except XMP is weird and does a hack where you need to keep
+        *  these length bytes.
+        * @return The data.
+        */
+       static function readBlock( $fh, $includeLengths = false ) {
+               $data = '';
+               $subLength = fread( $fh, 1 );
+               $blocks = 0;
+
+               while( $subLength !== "\0" ) {
+                       $blocks++;
+                       if ( $blocks > self::MAX_SUBBLOCKS ) {
+                               throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
+                       }
+                       if ( feof( $fh ) ) {
+                               throw new Exception( "Read error: Unexpected EOF." );
+                       }
+                       if ( $includeLengths ) {
+                               $data .= $subLength;
+                       }
+
+                       $data .= fread( $fh, ord( $subLength ) );
+                       $subLength = fread( $fh, 1 );
+               }
+               return $data;
+       }
+
 }