view e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.zip.php @ 8:1b77e8657088 judyates

[svn r9] saving for transference to cd
author rlm
date Fri, 19 Mar 2010 07:17:36 -0400
parents 3f6b44aa6b35
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.archive.zip.php |
18 // | Module for analyzing pkZip files |
19 // | dependencies: NONE |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.archive.zip.php,v 1.4 2006/11/02 10:48:00 ah Exp $
26 class getid3_zip extends getid3_handler
27 {
29 public function Analyze() {
31 $getid3 = $this->getid3;
33 $getid3->info['zip'] = array ();
34 $info_zip = &$getid3->info['zip'];
36 $getid3->info['fileformat'] = 'zip';
38 $info_zip['encoding'] = 'ISO-8859-1';
39 $info_zip['files'] = array ();
40 $info_zip['compressed_size'] = $info_zip['uncompressed_size'] = $info_zip['entries_count'] = 0;
42 $eocd_search_data = '';
43 $eocd_search_counter = 0;
44 while ($eocd_search_counter++ < 512) {
46 fseek($getid3->fp, -128 * $eocd_search_counter, SEEK_END);
47 $eocd_search_data = fread($getid3->fp, 128).$eocd_search_data;
49 if (strstr($eocd_search_data, 'PK'."\x05\x06")) {
51 $eocd_position = strpos($eocd_search_data, 'PK'."\x05\x06");
52 fseek($getid3->fp, (-128 * $eocd_search_counter) + $eocd_position, SEEK_END);
53 $info_zip['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory();
55 fseek($getid3->fp, $info_zip['end_central_directory']['directory_offset'], SEEK_SET);
56 $info_zip['entries_count'] = 0;
57 while ($central_directoryentry = $this->ZIPparseCentralDirectory($getid3->fp)) {
58 $info_zip['central_directory'][] = $central_directoryentry;
59 $info_zip['entries_count']++;
60 $info_zip['compressed_size'] += $central_directoryentry['compressed_size'];
61 $info_zip['uncompressed_size'] += $central_directoryentry['uncompressed_size'];
63 if ($central_directoryentry['uncompressed_size'] > 0) {
64 $info_zip['files'] = getid3_zip::array_merge_clobber($info_zip['files'], getid3_zip::CreateDeepArray($central_directoryentry['filename'], '/', $central_directoryentry['uncompressed_size']));
65 }
66 }
68 if ($info_zip['entries_count'] == 0) {
69 throw new getid3_exception('No Central Directory entries found (truncated file?)');
70 }
72 if (!empty($info_zip['end_central_directory']['comment'])) {
73 $info_zip['comments']['comment'][] = $info_zip['end_central_directory']['comment'];
74 }
76 if (isset($info_zip['central_directory'][0]['compression_method'])) {
77 $info_zip['compression_method'] = $info_zip['central_directory'][0]['compression_method'];
78 }
79 if (isset($info_zip['central_directory'][0]['flags']['compression_speed'])) {
80 $info_zip['compression_speed'] = $info_zip['central_directory'][0]['flags']['compression_speed'];
81 }
82 if (isset($info_zip['compression_method']) && ($info_zip['compression_method'] == 'store') && !isset($info_zip['compression_speed'])) {
83 $info_zip['compression_speed'] = 'store';
84 }
86 return true;
87 }
88 }
90 if ($this->getZIPentriesFilepointer()) {
92 // central directory couldn't be found and/or parsed
93 // scan through actual file data entries, recover as much as possible from probable trucated file
94 if (@$info_zip['compressed_size'] > ($getid3->info['filesize'] - 46 - 22)) {
95 throw new getid3_exception('Warning: Truncated file! - Total compressed file sizes ('.$info_zip['compressed_size'].' bytes) is greater than filesize minus Central Directory and End Of Central Directory structures ('.($getid3->info['filesize'] - 46 - 22).' bytes)');
96 }
97 throw new getid3_exception('Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete');
98 }
100 //throw new getid3_exception('Cannot find End Of Central Directory (truncated file?)');
101 }
105 private function getZIPHeaderFilepointerTopDown() {
107 // shortcut
108 $getid3 = $this->getid3;
110 $getid3->info['fileformat'] = 'zip';
112 $getid3->info['zip'] = array ();
113 $info_zip['compressed_size'] = $info_zip['uncompressed_size'] = $info_zip['entries_count'] = 0;
115 rewind($getid3->fp);
116 while ($fileentry = $this->ZIPparseLocalFileHeader()) {
117 $info_zip['entries'][] = $fileentry;
118 $info_zip['entries_count']++;
119 }
120 if ($info_zip['entries_count'] == 0) {
121 throw new getid3_exception('No Local File Header entries found');
122 }
124 $info_zip['entries_count'] = 0;
125 while ($central_directoryentry = $this->ZIPparseCentralDirectory($getid3->fp)) {
126 $info_zip['central_directory'][] = $central_directoryentry;
127 $info_zip['entries_count']++;
128 $info_zip['compressed_size'] += $central_directoryentry['compressed_size'];
129 $info_zip['uncompressed_size'] += $central_directoryentry['uncompressed_size'];
130 }
131 if ($info_zip['entries_count'] == 0) {
132 throw new getid3_exception('No Central Directory entries found (truncated file?)');
133 }
135 if ($eocd = $this->ZIPparseEndOfCentralDirectory()) {
136 $info_zip['end_central_directory'] = $eocd;
137 } else {
138 throw new getid3_exception('No End Of Central Directory entry found (truncated file?)');
139 }
141 if (!@$info_zip['end_central_directory']['comment']) {
142 $info_zip['comments']['comment'][] = $info_zip['end_central_directory']['comment'];
143 }
145 return true;
146 }
150 private function getZIPentriesFilepointer() {
152 // shortcut
153 $getid3 = $this->getid3;
155 $getid3->info['zip'] = array ();
156 $info_zip['compressed_size'] = $info_zip['uncompressed_size'] = $info_zip['entries_count'] = 0;
158 rewind($getid3->fp);
159 while ($fileentry = $this->ZIPparseLocalFileHeader($getid3->fp)) {
160 $info_zip['entries'][] = $fileentry;
161 $info_zip['entries_count']++;
162 $info_zip['compressed_size'] += $fileentry['compressed_size'];
163 $info_zip['uncompressed_size'] += $fileentry['uncompressed_size'];
164 }
165 if ($info_zip['entries_count'] == 0) {
166 throw new getid3_exception('No Local File Header entries found');
167 }
169 return true;
170 }
174 private function ZIPparseLocalFileHeader() {
176 // shortcut
177 $getid3 = $this->getid3;
179 $local_file_header['offset'] = ftell($getid3->fp);
181 $zip_local_file_header = fread($getid3->fp, 30);
183 $local_file_header['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($zip_local_file_header, 0, 4));
185 // Invalid Local File Header Signature
186 if ($local_file_header['raw']['signature'] != 0x04034B50) {
187 fseek($getid3->fp, $local_file_header['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
188 return false;
189 }
191 getid3_lib::ReadSequence('LittleEndian2Int', $local_file_header['raw'], $zip_local_file_header, 4,
192 array (
193 'extract_version' => 2,
194 'general_flags' => 2,
195 'compression_method' => 2,
196 'last_mod_file_time' => 2,
197 'last_mod_file_date' => 2,
198 'crc_32' => 2,
199 'compressed_size' => 2,
200 'uncompressed_size' => 2,
201 'filename_length' => 2,
202 'extra_field_length' => 2
203 )
204 );
206 $local_file_header['extract_version'] = sprintf('%1.1f', $local_file_header['raw']['extract_version'] / 10);
207 $local_file_header['host_os'] = $this->ZIPversionOSLookup(($local_file_header['raw']['extract_version'] & 0xFF00) >> 8);
208 $local_file_header['compression_method'] = $this->ZIPcompressionMethodLookup($local_file_header['raw']['compression_method']);
209 $local_file_header['compressed_size'] = $local_file_header['raw']['compressed_size'];
210 $local_file_header['uncompressed_size'] = $local_file_header['raw']['uncompressed_size'];
211 $local_file_header['flags'] = $this->ZIPparseGeneralPurposeFlags($local_file_header['raw']['general_flags'], $local_file_header['raw']['compression_method']);
212 $local_file_header['last_modified_timestamp'] = $this->DOStime2UNIXtime($local_file_header['raw']['last_mod_file_date'], $local_file_header['raw']['last_mod_file_time']);
214 $filename_extra_field_length = $local_file_header['raw']['filename_length'] + $local_file_header['raw']['extra_field_length'];
215 if ($filename_extra_field_length > 0) {
216 $zip_local_file_header .= fread($getid3->fp, $filename_extra_field_length);
218 if ($local_file_header['raw']['filename_length'] > 0) {
219 $local_file_header['filename'] = substr($zip_local_file_header, 30, $local_file_header['raw']['filename_length']);
220 }
221 if ($local_file_header['raw']['extra_field_length'] > 0) {
222 $local_file_header['raw']['extra_field_data'] = substr($zip_local_file_header, 30 + $local_file_header['raw']['filename_length'], $local_file_header['raw']['extra_field_length']);
223 }
224 }
226 $local_file_header['data_offset'] = ftell($getid3->fp);
227 fseek($getid3->fp, $local_file_header['raw']['compressed_size'], SEEK_CUR);
229 if ($local_file_header['flags']['data_descriptor_used']) {
230 $data_descriptor = fread($getid3->fp, 12);
232 getid3_lib::ReadSequence('LittleEndian2Int', $local_file_header['data_descriptor'], $data_descriptor, 0,
233 array (
234 'crc_32' => 4,
235 'compressed_size' => 4,
236 'uncompressed_size' => 4
237 )
238 );
239 }
241 return $local_file_header;
242 }
246 private function ZIPparseCentralDirectory() {
248 // shortcut
249 $getid3 = $this->getid3;
251 $central_directory['offset'] = ftell($getid3->fp);
253 $zip_central_directory = fread($getid3->fp, 46);
255 $central_directory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($zip_central_directory, 0, 4));
257 // invalid Central Directory Signature
258 if ($central_directory['raw']['signature'] != 0x02014B50) {
259 fseek($getid3->fp, $central_directory['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
260 return false;
261 }
263 getid3_lib::ReadSequence('LittleEndian2Int', $central_directory['raw'], $zip_central_directory, 4,
264 array (
265 'create_version' => 2,
266 'extract_version' => 2,
267 'general_flags' => 2,
268 'compression_method' => 2,
269 'last_mod_file_time' => 2,
270 'last_mod_file_date' => 2,
271 'crc_32' => 4,
272 'compressed_size' => 4,
273 'uncompressed_size' => 4,
274 'filename_length' => 2,
275 'extra_field_length' => 2,
276 'file_comment_length' => 2,
277 'disk_number_start' => 2,
278 'internal_file_attrib' => 2,
279 'external_file_attrib' => 4,
280 'local_header_offset' => 4
281 )
282 );
284 $central_directory['entry_offset'] = $central_directory['raw']['local_header_offset'];
285 $central_directory['create_version'] = sprintf('%1.1f', $central_directory['raw']['create_version'] / 10);
286 $central_directory['extract_version'] = sprintf('%1.1f', $central_directory['raw']['extract_version'] / 10);
287 $central_directory['host_os'] = $this->ZIPversionOSLookup(($central_directory['raw']['extract_version'] & 0xFF00) >> 8);
288 $central_directory['compression_method'] = $this->ZIPcompressionMethodLookup($central_directory['raw']['compression_method']);
289 $central_directory['compressed_size'] = $central_directory['raw']['compressed_size'];
290 $central_directory['uncompressed_size'] = $central_directory['raw']['uncompressed_size'];
291 $central_directory['flags'] = $this->ZIPparseGeneralPurposeFlags($central_directory['raw']['general_flags'], $central_directory['raw']['compression_method']);
292 $central_directory['last_modified_timestamp'] = $this->DOStime2UNIXtime($central_directory['raw']['last_mod_file_date'], $central_directory['raw']['last_mod_file_time']);
294 $filename_extra_field_comment_length = $central_directory['raw']['filename_length'] + $central_directory['raw']['extra_field_length'] + $central_directory['raw']['file_comment_length'];
295 if ($filename_extra_field_comment_length > 0) {
296 $filename_extra_field_comment = fread($getid3->fp, $filename_extra_field_comment_length);
298 if ($central_directory['raw']['filename_length'] > 0) {
299 $central_directory['filename']= substr($filename_extra_field_comment, 0, $central_directory['raw']['filename_length']);
300 }
301 if ($central_directory['raw']['extra_field_length'] > 0) {
302 $central_directory['raw']['extra_field_data'] = substr($filename_extra_field_comment, $central_directory['raw']['filename_length'], $central_directory['raw']['extra_field_length']);
303 }
304 if ($central_directory['raw']['file_comment_length'] > 0) {
305 $central_directory['file_comment'] = substr($filename_extra_field_comment, $central_directory['raw']['filename_length'] + $central_directory['raw']['extra_field_length'], $central_directory['raw']['file_comment_length']);
306 }
307 }
309 return $central_directory;
310 }
314 private function ZIPparseEndOfCentralDirectory() {
316 // shortcut
317 $getid3 = $this->getid3;
319 $end_of_central_directory['offset'] = ftell($getid3->fp);
321 $zip_end_of_central_directory = fread($getid3->fp, 22);
323 $end_of_central_directory['signature'] = getid3_lib::LittleEndian2Int(substr($zip_end_of_central_directory, 0, 4));
325 // invalid End Of Central Directory Signature
326 if ($end_of_central_directory['signature'] != 0x06054B50) {
327 fseek($getid3->fp, $end_of_central_directory['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
328 return false;
329 }
331 getid3_lib::ReadSequence('LittleEndian2Int', $end_of_central_directory, $zip_end_of_central_directory, 4,
332 array (
333 'disk_number_current' => 2,
334 'disk_number_start_directory' => 2,
335 'directory_entries_this_disk' => 2,
336 'directory_entries_total' => 2,
337 'directory_size' => 4,
338 'directory_offset' => 4,
339 'comment_length' => 2
340 )
341 );
343 if ($end_of_central_directory['comment_length'] > 0) {
344 $end_of_central_directory['comment'] = fread($getid3->fp, $end_of_central_directory['comment_length']);
345 }
347 return $end_of_central_directory;
348 }
352 public static function ZIPparseGeneralPurposeFlags($flag_bytes, $compression_method) {
354 $parsed_flags['encrypted'] = (bool)($flag_bytes & 0x0001);
356 switch ($compression_method) {
357 case 6:
358 $parsed_flags['dictionary_size'] = (($flag_bytes & 0x0002) ? 8192 : 4096);
359 $parsed_flags['shannon_fano_trees'] = (($flag_bytes & 0x0004) ? 3 : 2);
360 break;
362 case 8:
363 case 9:
364 switch (($flag_bytes & 0x0006) >> 1) {
365 case 0:
366 $parsed_flags['compression_speed'] = 'normal';
367 break;
368 case 1:
369 $parsed_flags['compression_speed'] = 'maximum';
370 break;
371 case 2:
372 $parsed_flags['compression_speed'] = 'fast';
373 break;
374 case 3:
375 $parsed_flags['compression_speed'] = 'superfast';
376 break;
377 }
378 break;
379 }
380 $parsed_flags['data_descriptor_used'] = (bool)($flag_bytes & 0x0008);
382 return $parsed_flags;
383 }
387 public static function ZIPversionOSLookup($index) {
389 static $lookup = array (
390 0 => 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
391 1 => 'Amiga',
392 2 => 'OpenVMS',
393 3 => 'Unix',
394 4 => 'VM/CMS',
395 5 => 'Atari ST',
396 6 => 'OS/2 H.P.F.S.',
397 7 => 'Macintosh',
398 8 => 'Z-System',
399 9 => 'CP/M',
400 10 => 'Windows NTFS',
401 11 => 'MVS',
402 12 => 'VSE',
403 13 => 'Acorn Risc',
404 14 => 'VFAT',
405 15 => 'Alternate MVS',
406 16 => 'BeOS',
407 17 => 'Tandem'
408 );
409 return (isset($lookup[$index]) ? $lookup[$index] : '[unknown]');
410 }
414 public static function ZIPcompressionMethodLookup($index) {
416 static $lookup = array (
417 0 => 'store',
418 1 => 'shrink',
419 2 => 'reduce-1',
420 3 => 'reduce-2',
421 4 => 'reduce-3',
422 5 => 'reduce-4',
423 6 => 'implode',
424 7 => 'tokenize',
425 8 => 'deflate',
426 9 => 'deflate64',
427 10 => 'PKWARE Date Compression Library Imploding'
428 );
429 return (isset($lookup[$index]) ? $lookup[$index] : '[unknown]');
430 }
434 public static function DOStime2UNIXtime($DOSdate, $DOStime) {
436 /*
437 // wFatDate
438 // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
439 // Bits Contents
440 // 0-4 Day of the month (1-31)
441 // 5-8 Month (1 = January, 2 = February, and so on)
442 // 9-15 Year offset from 1980 (add 1980 to get actual year)
444 $UNIXday = ($DOSdate & 0x001F);
445 $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
446 $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
448 // wFatTime
449 // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
450 // Bits Contents
451 // 0-4 Second divided by 2
452 // 5-10 Minute (0-59)
453 // 11-15 Hour (0-23 on a 24-hour clock)
455 $UNIXsecond = ($DOStime & 0x001F) * 2;
456 $UNIXminute = (($DOStime & 0x07E0) >> 5);
457 $UNIXhour = (($DOStime & 0xF800) >> 11);
459 return gmmktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
460 */
461 return gmmktime(($DOStime & 0xF800) >> 11, ($DOStime & 0x07E0) >> 5, ($DOStime & 0x001F) * 2, ($DOSdate & 0x01E0) >> 5, $DOSdate & 0x001F, (($DOSdate & 0xFE00) >> 9) + 1980);
462 }
466 public static function array_merge_clobber($array1, $array2) {
468 // written by kcØhireability*com
469 // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
471 if (!is_array($array1) || !is_array($array2)) {
472 return false;
473 }
475 $newarray = $array1;
476 foreach ($array2 as $key => $val) {
477 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
478 $newarray[$key] = getid3_zip::array_merge_clobber($newarray[$key], $val);
479 } else {
480 $newarray[$key] = $val;
481 }
482 }
483 return $newarray;
484 }
488 public static function CreateDeepArray($array_path, $separator, $value) {
490 // assigns $value to a nested array path:
491 // $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
492 // is the same as:
493 // $foo = array ('path'=>array('to'=>'array('my'=>array('file.txt'))));
494 // or
495 // $foo['path']['to']['my'] = 'file.txt';
497 while ($array_path{0} == $separator) {
498 $array_path = substr($array_path, 1);
499 }
500 if (($pos = strpos($array_path, $separator)) !== false) {
501 return array (substr($array_path, 0, $pos) => getid3_zip::CreateDeepArray(substr($array_path, $pos + 1), $separator, $value));
502 }
504 return array ($array_path => $value);
505 }
507 }
510 ?>