diff e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.gzip.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.gzip.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,296 @@
     1.4 +<?php
     1.5 +// +----------------------------------------------------------------------+
     1.6 +// | PHP version 5                                                        |
     1.7 +// +----------------------------------------------------------------------+
     1.8 +// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
     1.9 +// +----------------------------------------------------------------------+
    1.10 +// | This source file is subject to version 2 of the GPL license,         |
    1.11 +// | that is bundled with this package in the file license.txt and is     |
    1.12 +// | available through the world-wide-web at the following url:           |
    1.13 +// | http://www.gnu.org/copyleft/gpl.html                                 |
    1.14 +// +----------------------------------------------------------------------+
    1.15 +// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
    1.16 +// +----------------------------------------------------------------------+
    1.17 +// | Authors: James Heinrich <infoØgetid3*org>                            |
    1.18 +// |          Allan Hansen <ahØartemis*dk>                                |
    1.19 +// +----------------------------------------------------------------------+
    1.20 +// | module.archive.gzip.php                                              |
    1.21 +// | module for analyzing GZIP files                                      |
    1.22 +// | dependencies: PHP compiled with zlib support (optional)              |
    1.23 +// +----------------------------------------------------------------------+
    1.24 +// | Module originally written by Mike Mozolin <teddybearØmail*ru>        |
    1.25 +// +----------------------------------------------------------------------+
    1.26 +//
    1.27 +// $Id: module.archive.gzip.php,v 1.4 2006/12/04 16:00:35 ah Exp $
    1.28 +
    1.29 +        
    1.30 +        
    1.31 +class getid3_gzip extends getid3_handler
    1.32 +{
    1.33 +
    1.34 +    // public: Optional file list - disable for speed.
    1.35 +    public $option_gzip_parse_contents = true; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
    1.36 +
    1.37 +    
    1.38 +    // Reads the gzip-file
    1.39 +    function Analyze() {
    1.40 +
    1.41 +        $info = &$this->getid3->info;
    1.42 +
    1.43 +        $info['fileformat'] = 'gzip';
    1.44 +
    1.45 +        $start_length = 10;
    1.46 +        $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
    1.47 +        
    1.48 +        //+---+---+---+---+---+---+---+---+---+---+
    1.49 +        //|ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
    1.50 +        //+---+---+---+---+---+---+---+---+---+---+
    1.51 +        
    1.52 +        @fseek($this->getid3->fp, 0);
    1.53 +        $buffer = @fread($this->getid3->fp, $info['filesize']);
    1.54 +
    1.55 +        $arr_members = explode("\x1F\x8B\x08", $buffer);
    1.56 +        
    1.57 +        while (true) {
    1.58 +            $is_wrong_members = false;
    1.59 +            $num_members = intval(count($arr_members));
    1.60 +            for ($i = 0; $i < $num_members; $i++) {
    1.61 +                if (strlen($arr_members[$i]) == 0) {
    1.62 +                    continue;
    1.63 +                }
    1.64 +                $buf = "\x1F\x8B\x08".$arr_members[$i];
    1.65 +
    1.66 +                $attr = unpack($unpack_header, substr($buf, 0, $start_length));
    1.67 +                if (!$this->get_os_type(ord($attr['os']))) {
    1.68 +        
    1.69 +                    // Merge member with previous if wrong OS type
    1.70 +                    $arr_members[$i - 1] .= $buf;
    1.71 +                    $arr_members[$i] = '';
    1.72 +                    $is_wrong_members = true;
    1.73 +                    continue;
    1.74 +                }
    1.75 +            }
    1.76 +            if (!$is_wrong_members) {
    1.77 +                break;
    1.78 +            }
    1.79 +        }
    1.80 +
    1.81 +        $fpointer = 0;
    1.82 +        $idx = 0;
    1.83 +        for ($i = 0; $i < $num_members; $i++) {
    1.84 +            if (strlen($arr_members[$i]) == 0) {
    1.85 +                continue;
    1.86 +            }
    1.87 +            $info_gzip_member_header_idx = &$info['gzip']['member_header'][++$idx];
    1.88 +
    1.89 +            $buff = "\x1F\x8B\x08".$arr_members[$i];
    1.90 +
    1.91 +            $attr = unpack($unpack_header, substr($buff, 0, $start_length));
    1.92 +            $info_gzip_member_header_idx['filemtime']      = getid3_lib::LittleEndian2Int($attr['mtime']);
    1.93 +            $info_gzip_member_header_idx['raw']['id1']     = ord($attr['cmethod']);
    1.94 +            $info_gzip_member_header_idx['raw']['id2']     = ord($attr['cmethod']);
    1.95 +            $info_gzip_member_header_idx['raw']['cmethod'] = ord($attr['cmethod']);
    1.96 +            $info_gzip_member_header_idx['raw']['os']      = ord($attr['os']);
    1.97 +            $info_gzip_member_header_idx['raw']['xflags']  = ord($attr['xflags']);
    1.98 +            $info_gzip_member_header_idx['raw']['flags']   = ord($attr['flags']);
    1.99 +
   1.100 +            $info_gzip_member_header_idx['flags']['crc16']    = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x02);
   1.101 +            $info_gzip_member_header_idx['flags']['extra']    = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x04);
   1.102 +            $info_gzip_member_header_idx['flags']['filename'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x08);
   1.103 +            $info_gzip_member_header_idx['flags']['comment']  = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x10);
   1.104 +
   1.105 +            $info_gzip_member_header_idx['compression'] = $this->get_xflag_type($info_gzip_member_header_idx['raw']['xflags']);
   1.106 +
   1.107 +            $info_gzip_member_header_idx['os'] = $this->get_os_type($info_gzip_member_header_idx['raw']['os']);
   1.108 +            if (!$info_gzip_member_header_idx['os']) {
   1.109 +                $info['error'][] = 'Read error on gzip file';
   1.110 +                return false;
   1.111 +            }
   1.112 +
   1.113 +            $fpointer = 10;
   1.114 +            $arr_xsubfield = array ();
   1.115 +            
   1.116 +            // bit 2 - FLG.FEXTRA
   1.117 +            //+---+---+=================================+
   1.118 +            //| XLEN  |...XLEN bytes of "extra field"...|
   1.119 +            //+---+---+=================================+
   1.120 +            
   1.121 +            if ($info_gzip_member_header_idx['flags']['extra']) {
   1.122 +                $w_xlen = substr($buff, $fpointer, 2);
   1.123 +                $xlen = getid3_lib::LittleEndian2Int($w_xlen);
   1.124 +                $fpointer += 2;
   1.125 +
   1.126 +                $info_gzip_member_header_idx['raw']['xfield'] = substr($buff, $fpointer, $xlen);
   1.127 +            
   1.128 +                // Extra SubFields
   1.129 +                //+---+---+---+---+==================================+
   1.130 +                //|SI1|SI2|  LEN  |... LEN bytes of subfield data ...|
   1.131 +                //+---+---+---+---+==================================+
   1.132 +            
   1.133 +                $idx = 0;
   1.134 +                while (true) {
   1.135 +                    if ($idx >= $xlen) {
   1.136 +                        break;
   1.137 +                    }
   1.138 +                    $si1 = ord(substr($buff, $fpointer + $idx++, 1));
   1.139 +                    $si2 = ord(substr($buff, $fpointer + $idx++, 1));
   1.140 +                    if (($si1 == 0x41) && ($si2 == 0x70)) {
   1.141 +                        $w_xsublen = substr($buff, $fpointer+$idx, 2);
   1.142 +                        $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
   1.143 +                        $idx += 2;
   1.144 +                        $arr_xsubfield[] = substr($buff, $fpointer+$idx, $xsublen);
   1.145 +                        $idx += $xsublen;
   1.146 +                    } else {
   1.147 +                        break;
   1.148 +                    }
   1.149 +                }
   1.150 +                $fpointer += $xlen;
   1.151 +            }
   1.152 +            
   1.153 +            // bit 3 - FLG.FNAME
   1.154 +            //+=========================================+
   1.155 +            //|...original file name, zero-terminated...|
   1.156 +            //+=========================================+
   1.157 +            // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
   1.158 +            
   1.159 +            $info_gzip_member_header_idx['filename'] = eregi_replace('.gz$', '', @$info['filename']);
   1.160 +            if ($info_gzip_member_header_idx['flags']['filename']) {
   1.161 +                while (true) {
   1.162 +                    if (ord($buff[$fpointer]) == 0) {
   1.163 +                        $fpointer++;
   1.164 +                        break;
   1.165 +                    }
   1.166 +                    $info_gzip_member_header_idx['filename'] .= $buff[$fpointer];
   1.167 +                    $fpointer++;
   1.168 +                }
   1.169 +            }
   1.170 +            
   1.171 +            // bit 4 - FLG.FCOMMENT
   1.172 +            //+===================================+
   1.173 +            //|...file comment, zero-terminated...|
   1.174 +            //+===================================+
   1.175 +            
   1.176 +            if ($info_gzip_member_header_idx['flags']['comment']) {
   1.177 +                while (true) {
   1.178 +                    if (ord($buff[$fpointer]) == 0) {
   1.179 +                        $fpointer++;
   1.180 +                        break;
   1.181 +                    }
   1.182 +                    $info_gzip_member_header_idx['comment'] .= $buff[$fpointer];
   1.183 +                    $fpointer++;
   1.184 +                }
   1.185 +            }
   1.186 +            
   1.187 +            // bit 1 - FLG.FHCRC
   1.188 +            //+---+---+
   1.189 +            //| CRC16 |
   1.190 +            //+---+---+
   1.191 +            
   1.192 +            if ($info_gzip_member_header_idx['flags']['crc16']) {
   1.193 +                $w_crc = substr($buff, $fpointer, 2);
   1.194 +                $info_gzip_member_header_idx['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
   1.195 +                $fpointer += 2;
   1.196 +            }
   1.197 +            
   1.198 +            // bit 0 - FLG.FTEXT
   1.199 +            //if ($info_gzip_member_header_idx['raw']['flags'] & 0x01) {
   1.200 +            //  Ignored...
   1.201 +            //}
   1.202 +            // bits 5, 6, 7 - reserved
   1.203 +
   1.204 +            $info_gzip_member_header_idx['crc32']    = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
   1.205 +            $info_gzip_member_header_idx['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
   1.206 +
   1.207 +            if ($this->option_gzip_parse_contents) {
   1.208 +
   1.209 +                // Try to inflate GZip
   1.210 +                
   1.211 +                if (!function_exists('gzinflate')) {
   1.212 +                    $this->getid3->warning('PHP does not have zlib support - contents not parsed.');
   1.213 +                    return true;
   1.214 +                }
   1.215 +
   1.216 +                $csize = 0;
   1.217 +                $inflated = '';
   1.218 +                $chkcrc32 = '';
   1.219 +
   1.220 +                $cdata = substr($buff, $fpointer);
   1.221 +                $cdata = substr($cdata, 0, strlen($cdata) - 8);
   1.222 +                $csize = strlen($cdata);
   1.223 +                $inflated = gzinflate($cdata);
   1.224 +
   1.225 +                // Calculate CRC32 for inflated content
   1.226 +                $info_gzip_member_header_idx['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $info_gzip_member_header_idx['crc32']);
   1.227 +
   1.228 +                
   1.229 +                //// Analyse contents
   1.230 +                
   1.231 +                // write content to temp file
   1.232 +                if (($temp_file_name = tempnam('*', 'getID3'))  === false) {
   1.233 +                    throw new getid3_exception('Unable to create temporary file.');
   1.234 +                }
   1.235 +
   1.236 +                if ($tmp = fopen($temp_file_name, 'wb')) {
   1.237 +                    fwrite($tmp, $inflated);
   1.238 +                    fclose($tmp);
   1.239 +                    
   1.240 +                    // clone getid3 - we want same settings
   1.241 +                    $clone = clone $this->getid3;
   1.242 +                    unset($clone->info);
   1.243 +                    try {
   1.244 +                        $clone->Analyze($temp_file_name);
   1.245 +                        $info_gzip_member_header_idx['parsed_content'] = $clone->info;
   1.246 +                    }
   1.247 +                    catch (getid3_exception $e) {
   1.248 +                        // unable to parse contents
   1.249 +                    }
   1.250 +                    
   1.251 +                    unlink($temp_file_name);
   1.252 +                }
   1.253 +            
   1.254 +                // Unknown/unhandled format 
   1.255 +                else {
   1.256 +                                        
   1.257 +                }
   1.258 +            }
   1.259 +        }
   1.260 +        return true;
   1.261 +    }
   1.262 +
   1.263 +
   1.264 +    // Converts the OS type
   1.265 +    public static function get_os_type($key) {
   1.266 +        static $os_type = array (
   1.267 +            '0'   => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
   1.268 +            '1'   => 'Amiga',
   1.269 +            '2'   => 'VMS (or OpenVMS)',
   1.270 +            '3'   => 'Unix',
   1.271 +            '4'   => 'VM/CMS',
   1.272 +            '5'   => 'Atari TOS',
   1.273 +            '6'   => 'HPFS filesystem (OS/2, NT)',
   1.274 +            '7'   => 'Macintosh',
   1.275 +            '8'   => 'Z-System',
   1.276 +            '9'   => 'CP/M',
   1.277 +            '10'  => 'TOPS-20',
   1.278 +            '11'  => 'NTFS filesystem (NT)',
   1.279 +            '12'  => 'QDOS',
   1.280 +            '13'  => 'Acorn RISCOS',
   1.281 +            '255' => 'unknown'
   1.282 +        );
   1.283 +        return @$os_type[$key];
   1.284 +    }
   1.285 +
   1.286 +
   1.287 +    // Converts the eXtra FLags
   1.288 +    public static function get_xflag_type($key) {
   1.289 +        static $xflag_type = array (
   1.290 +            '0' => 'unknown',
   1.291 +            '2' => 'maximum compression',
   1.292 +            '4' => 'fastest algorithm'
   1.293 +        );
   1.294 +        return @$xflag_type[$key];
   1.295 +    }
   1.296 +    
   1.297 +}
   1.298 +
   1.299 +?>
   1.300 \ No newline at end of file