annotate e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.gzip.php @ 11:ed6ee381b8fd judyates

[svn r12] compressed photos and assembled them into a working web gallery
author rlm
date Mon, 12 Apr 2010 02:31:43 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infoØgetid3*org> |
rlm@3 15 // | Allan Hansen <ahØartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | module.archive.gzip.php |
rlm@3 18 // | module for analyzing GZIP files |
rlm@3 19 // | dependencies: PHP compiled with zlib support (optional) |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 // | Module originally written by Mike Mozolin <teddybearØmail*ru> |
rlm@3 22 // +----------------------------------------------------------------------+
rlm@3 23 //
rlm@3 24 // $Id: module.archive.gzip.php,v 1.4 2006/12/04 16:00:35 ah Exp $
rlm@3 25
rlm@3 26
rlm@3 27
rlm@3 28 class getid3_gzip extends getid3_handler
rlm@3 29 {
rlm@3 30
rlm@3 31 // public: Optional file list - disable for speed.
rlm@3 32 public $option_gzip_parse_contents = true; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
rlm@3 33
rlm@3 34
rlm@3 35 // Reads the gzip-file
rlm@3 36 function Analyze() {
rlm@3 37
rlm@3 38 $info = &$this->getid3->info;
rlm@3 39
rlm@3 40 $info['fileformat'] = 'gzip';
rlm@3 41
rlm@3 42 $start_length = 10;
rlm@3 43 $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
rlm@3 44
rlm@3 45 //+---+---+---+---+---+---+---+---+---+---+
rlm@3 46 //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
rlm@3 47 //+---+---+---+---+---+---+---+---+---+---+
rlm@3 48
rlm@3 49 @fseek($this->getid3->fp, 0);
rlm@3 50 $buffer = @fread($this->getid3->fp, $info['filesize']);
rlm@3 51
rlm@3 52 $arr_members = explode("\x1F\x8B\x08", $buffer);
rlm@3 53
rlm@3 54 while (true) {
rlm@3 55 $is_wrong_members = false;
rlm@3 56 $num_members = intval(count($arr_members));
rlm@3 57 for ($i = 0; $i < $num_members; $i++) {
rlm@3 58 if (strlen($arr_members[$i]) == 0) {
rlm@3 59 continue;
rlm@3 60 }
rlm@3 61 $buf = "\x1F\x8B\x08".$arr_members[$i];
rlm@3 62
rlm@3 63 $attr = unpack($unpack_header, substr($buf, 0, $start_length));
rlm@3 64 if (!$this->get_os_type(ord($attr['os']))) {
rlm@3 65
rlm@3 66 // Merge member with previous if wrong OS type
rlm@3 67 $arr_members[$i - 1] .= $buf;
rlm@3 68 $arr_members[$i] = '';
rlm@3 69 $is_wrong_members = true;
rlm@3 70 continue;
rlm@3 71 }
rlm@3 72 }
rlm@3 73 if (!$is_wrong_members) {
rlm@3 74 break;
rlm@3 75 }
rlm@3 76 }
rlm@3 77
rlm@3 78 $fpointer = 0;
rlm@3 79 $idx = 0;
rlm@3 80 for ($i = 0; $i < $num_members; $i++) {
rlm@3 81 if (strlen($arr_members[$i]) == 0) {
rlm@3 82 continue;
rlm@3 83 }
rlm@3 84 $info_gzip_member_header_idx = &$info['gzip']['member_header'][++$idx];
rlm@3 85
rlm@3 86 $buff = "\x1F\x8B\x08".$arr_members[$i];
rlm@3 87
rlm@3 88 $attr = unpack($unpack_header, substr($buff, 0, $start_length));
rlm@3 89 $info_gzip_member_header_idx['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
rlm@3 90 $info_gzip_member_header_idx['raw']['id1'] = ord($attr['cmethod']);
rlm@3 91 $info_gzip_member_header_idx['raw']['id2'] = ord($attr['cmethod']);
rlm@3 92 $info_gzip_member_header_idx['raw']['cmethod'] = ord($attr['cmethod']);
rlm@3 93 $info_gzip_member_header_idx['raw']['os'] = ord($attr['os']);
rlm@3 94 $info_gzip_member_header_idx['raw']['xflags'] = ord($attr['xflags']);
rlm@3 95 $info_gzip_member_header_idx['raw']['flags'] = ord($attr['flags']);
rlm@3 96
rlm@3 97 $info_gzip_member_header_idx['flags']['crc16'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x02);
rlm@3 98 $info_gzip_member_header_idx['flags']['extra'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x04);
rlm@3 99 $info_gzip_member_header_idx['flags']['filename'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x08);
rlm@3 100 $info_gzip_member_header_idx['flags']['comment'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x10);
rlm@3 101
rlm@3 102 $info_gzip_member_header_idx['compression'] = $this->get_xflag_type($info_gzip_member_header_idx['raw']['xflags']);
rlm@3 103
rlm@3 104 $info_gzip_member_header_idx['os'] = $this->get_os_type($info_gzip_member_header_idx['raw']['os']);
rlm@3 105 if (!$info_gzip_member_header_idx['os']) {
rlm@3 106 $info['error'][] = 'Read error on gzip file';
rlm@3 107 return false;
rlm@3 108 }
rlm@3 109
rlm@3 110 $fpointer = 10;
rlm@3 111 $arr_xsubfield = array ();
rlm@3 112
rlm@3 113 // bit 2 - FLG.FEXTRA
rlm@3 114 //+---+---+=================================+
rlm@3 115 //| XLEN |...XLEN bytes of "extra field"...|
rlm@3 116 //+---+---+=================================+
rlm@3 117
rlm@3 118 if ($info_gzip_member_header_idx['flags']['extra']) {
rlm@3 119 $w_xlen = substr($buff, $fpointer, 2);
rlm@3 120 $xlen = getid3_lib::LittleEndian2Int($w_xlen);
rlm@3 121 $fpointer += 2;
rlm@3 122
rlm@3 123 $info_gzip_member_header_idx['raw']['xfield'] = substr($buff, $fpointer, $xlen);
rlm@3 124
rlm@3 125 // Extra SubFields
rlm@3 126 //+---+---+---+---+==================================+
rlm@3 127 //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
rlm@3 128 //+---+---+---+---+==================================+
rlm@3 129
rlm@3 130 $idx = 0;
rlm@3 131 while (true) {
rlm@3 132 if ($idx >= $xlen) {
rlm@3 133 break;
rlm@3 134 }
rlm@3 135 $si1 = ord(substr($buff, $fpointer + $idx++, 1));
rlm@3 136 $si2 = ord(substr($buff, $fpointer + $idx++, 1));
rlm@3 137 if (($si1 == 0x41) && ($si2 == 0x70)) {
rlm@3 138 $w_xsublen = substr($buff, $fpointer+$idx, 2);
rlm@3 139 $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
rlm@3 140 $idx += 2;
rlm@3 141 $arr_xsubfield[] = substr($buff, $fpointer+$idx, $xsublen);
rlm@3 142 $idx += $xsublen;
rlm@3 143 } else {
rlm@3 144 break;
rlm@3 145 }
rlm@3 146 }
rlm@3 147 $fpointer += $xlen;
rlm@3 148 }
rlm@3 149
rlm@3 150 // bit 3 - FLG.FNAME
rlm@3 151 //+=========================================+
rlm@3 152 //|...original file name, zero-terminated...|
rlm@3 153 //+=========================================+
rlm@3 154 // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
rlm@3 155
rlm@3 156 $info_gzip_member_header_idx['filename'] = eregi_replace('.gz$', '', @$info['filename']);
rlm@3 157 if ($info_gzip_member_header_idx['flags']['filename']) {
rlm@3 158 while (true) {
rlm@3 159 if (ord($buff[$fpointer]) == 0) {
rlm@3 160 $fpointer++;
rlm@3 161 break;
rlm@3 162 }
rlm@3 163 $info_gzip_member_header_idx['filename'] .= $buff[$fpointer];
rlm@3 164 $fpointer++;
rlm@3 165 }
rlm@3 166 }
rlm@3 167
rlm@3 168 // bit 4 - FLG.FCOMMENT
rlm@3 169 //+===================================+
rlm@3 170 //|...file comment, zero-terminated...|
rlm@3 171 //+===================================+
rlm@3 172
rlm@3 173 if ($info_gzip_member_header_idx['flags']['comment']) {
rlm@3 174 while (true) {
rlm@3 175 if (ord($buff[$fpointer]) == 0) {
rlm@3 176 $fpointer++;
rlm@3 177 break;
rlm@3 178 }
rlm@3 179 $info_gzip_member_header_idx['comment'] .= $buff[$fpointer];
rlm@3 180 $fpointer++;
rlm@3 181 }
rlm@3 182 }
rlm@3 183
rlm@3 184 // bit 1 - FLG.FHCRC
rlm@3 185 //+---+---+
rlm@3 186 //| CRC16 |
rlm@3 187 //+---+---+
rlm@3 188
rlm@3 189 if ($info_gzip_member_header_idx['flags']['crc16']) {
rlm@3 190 $w_crc = substr($buff, $fpointer, 2);
rlm@3 191 $info_gzip_member_header_idx['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
rlm@3 192 $fpointer += 2;
rlm@3 193 }
rlm@3 194
rlm@3 195 // bit 0 - FLG.FTEXT
rlm@3 196 //if ($info_gzip_member_header_idx['raw']['flags'] & 0x01) {
rlm@3 197 // Ignored...
rlm@3 198 //}
rlm@3 199 // bits 5, 6, 7 - reserved
rlm@3 200
rlm@3 201 $info_gzip_member_header_idx['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
rlm@3 202 $info_gzip_member_header_idx['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
rlm@3 203
rlm@3 204 if ($this->option_gzip_parse_contents) {
rlm@3 205
rlm@3 206 // Try to inflate GZip
rlm@3 207
rlm@3 208 if (!function_exists('gzinflate')) {
rlm@3 209 $this->getid3->warning('PHP does not have zlib support - contents not parsed.');
rlm@3 210 return true;
rlm@3 211 }
rlm@3 212
rlm@3 213 $csize = 0;
rlm@3 214 $inflated = '';
rlm@3 215 $chkcrc32 = '';
rlm@3 216
rlm@3 217 $cdata = substr($buff, $fpointer);
rlm@3 218 $cdata = substr($cdata, 0, strlen($cdata) - 8);
rlm@3 219 $csize = strlen($cdata);
rlm@3 220 $inflated = gzinflate($cdata);
rlm@3 221
rlm@3 222 // Calculate CRC32 for inflated content
rlm@3 223 $info_gzip_member_header_idx['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $info_gzip_member_header_idx['crc32']);
rlm@3 224
rlm@3 225
rlm@3 226 //// Analyse contents
rlm@3 227
rlm@3 228 // write content to temp file
rlm@3 229 if (($temp_file_name = tempnam('*', 'getID3')) === false) {
rlm@3 230 throw new getid3_exception('Unable to create temporary file.');
rlm@3 231 }
rlm@3 232
rlm@3 233 if ($tmp = fopen($temp_file_name, 'wb')) {
rlm@3 234 fwrite($tmp, $inflated);
rlm@3 235 fclose($tmp);
rlm@3 236
rlm@3 237 // clone getid3 - we want same settings
rlm@3 238 $clone = clone $this->getid3;
rlm@3 239 unset($clone->info);
rlm@3 240 try {
rlm@3 241 $clone->Analyze($temp_file_name);
rlm@3 242 $info_gzip_member_header_idx['parsed_content'] = $clone->info;
rlm@3 243 }
rlm@3 244 catch (getid3_exception $e) {
rlm@3 245 // unable to parse contents
rlm@3 246 }
rlm@3 247
rlm@3 248 unlink($temp_file_name);
rlm@3 249 }
rlm@3 250
rlm@3 251 // Unknown/unhandled format
rlm@3 252 else {
rlm@3 253
rlm@3 254 }
rlm@3 255 }
rlm@3 256 }
rlm@3 257 return true;
rlm@3 258 }
rlm@3 259
rlm@3 260
rlm@3 261 // Converts the OS type
rlm@3 262 public static function get_os_type($key) {
rlm@3 263 static $os_type = array (
rlm@3 264 '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
rlm@3 265 '1' => 'Amiga',
rlm@3 266 '2' => 'VMS (or OpenVMS)',
rlm@3 267 '3' => 'Unix',
rlm@3 268 '4' => 'VM/CMS',
rlm@3 269 '5' => 'Atari TOS',
rlm@3 270 '6' => 'HPFS filesystem (OS/2, NT)',
rlm@3 271 '7' => 'Macintosh',
rlm@3 272 '8' => 'Z-System',
rlm@3 273 '9' => 'CP/M',
rlm@3 274 '10' => 'TOPS-20',
rlm@3 275 '11' => 'NTFS filesystem (NT)',
rlm@3 276 '12' => 'QDOS',
rlm@3 277 '13' => 'Acorn RISCOS',
rlm@3 278 '255' => 'unknown'
rlm@3 279 );
rlm@3 280 return @$os_type[$key];
rlm@3 281 }
rlm@3 282
rlm@3 283
rlm@3 284 // Converts the eXtra FLags
rlm@3 285 public static function get_xflag_type($key) {
rlm@3 286 static $xflag_type = array (
rlm@3 287 '0' => 'unknown',
rlm@3 288 '2' => 'maximum compression',
rlm@3 289 '4' => 'fastest algorithm'
rlm@3 290 );
rlm@3 291 return @$xflag_type[$key];
rlm@3 292 }
rlm@3 293
rlm@3 294 }
rlm@3 295
rlm@3 296 ?>