<Website>

getID3

PHK Home

File: /lib/module.archive.zip.php

Size:20442
Storage flags:strip

<?php
















class getid3_zip extends getid3_handler
{

public function 
Analyze() {
$info = &$this->getid3->info;

$info['fileformat'] = 'zip';
$info['zip']['encoding'] = 'ISO-8859-1';
$info['zip']['files'] = array();

$info['zip']['compressed_size'] = 0;
$info['zip']['uncompressed_size'] = 0;
$info['zip']['entries_count'] = 0;

if (!
getid3_lib::intValueSupported($info['filesize'])) {
$info['error'][] = 'File is larger than '.round(PHP_INT_MAX 1073741824).'GB, not supported by PHP';
return 
false;
} else {
$EOCDsearchData '';
$EOCDsearchCounter 0;
while (
$EOCDsearchCounter++ < 512) {

$this->fseek(-128 $EOCDsearchCounterSEEK_END);
$EOCDsearchData $this->fread(128).$EOCDsearchData;

if (
strstr($EOCDsearchData'PK'."\x05\x06")) {

$EOCDposition strpos($EOCDsearchData'PK'."\x05\x06");
$this->fseek((-128 $EOCDsearchCounter) + $EOCDpositionSEEK_END);
$info['zip']['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory();

$this->fseek($info['zip']['end_central_directory']['directory_offset']);
$info['zip']['entries_count'] = 0;
while (
$centraldirectoryentry $this->ZIPparseCentralDirectory($this->getid3->fp)) {
$info['zip']['central_directory'][] = $centraldirectoryentry;
$info['zip']['entries_count']++;
$info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
$info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];


 if (!empty(
$centraldirectoryentry['filename'])) {
$info['zip']['files'] = getid3_lib::array_merge_clobber($info['zip']['files'], getid3_lib::CreateDeepArray($centraldirectoryentry['filename'], '/'$centraldirectoryentry['uncompressed_size']));
}
}

if (
$info['zip']['entries_count'] == 0) {
$info['error'][] = 'No Central Directory entries found (truncated file?)';
return 
false;
}

if (!empty(
$info['zip']['end_central_directory']['comment'])) {
$info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
}

if (isset(
$info['zip']['central_directory'][0]['compression_method'])) {
$info['zip']['compression_method'] = $info['zip']['central_directory'][0]['compression_method'];
}
if (isset(
$info['zip']['central_directory'][0]['flags']['compression_speed'])) {
$info['zip']['compression_speed'] = $info['zip']['central_directory'][0]['flags']['compression_speed'];
}
if (isset(
$info['zip']['compression_method']) && ($info['zip']['compression_method'] == 'store') && !isset($info['zip']['compression_speed'])) {
$info['zip']['compression_speed'] = 'store';
}


 
 foreach (
$info['zip']['central_directory'] as $central_directory_entry) {
$this->fseek($central_directory_entry['entry_offset']);
if (
$fileentry $this->ZIPparseLocalFileHeader()) {
$info['zip']['entries'][] = $fileentry;
} else {
$info['warning'][] = 'Error parsing Local File Header at offset '.$central_directory_entry['entry_offset'];
}
}

if (!empty(
$info['zip']['files']['[Content_Types].xml']) &&
!empty(
$info['zip']['files']['_rels']['.rels']) &&
!empty(
$info['zip']['files']['docProps']['app.xml']) &&
!empty(
$info['zip']['files']['docProps']['core.xml'])) {

 
$info['fileformat'] = 'zip.msoffice';
if (!empty(
$ThisFileInfo['zip']['files']['ppt'])) {
$info['mime_type'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
} elseif (!empty(
$ThisFileInfo['zip']['files']['xl'])) {
$info['mime_type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
} elseif (!empty(
$ThisFileInfo['zip']['files']['word'])) {
$info['mime_type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
}

return 
true;
}
}
}

if (!
$this->getZIPentriesFilepointer()) {
unset(
$info['zip']);
$info['fileformat'] = '';
$info['error'][] = 'Cannot find End Of Central Directory (truncated file?)';
return 
false;
}


 
 if (
$info['zip']['compressed_size'] > ($info['filesize'] - 46 22)) {
$info['error'][] = '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 ('.($info['filesize'] - 46 22).' bytes)';
}
$info['error'][] = 'Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete';
foreach (
$info['zip']['entries'] as $key => $valuearray) {
$info['zip']['files'][$valuearray['filename']] = $valuearray['uncompressed_size'];
}
return 
true;
}


public function 
getZIPHeaderFilepointerTopDown() {
$info = &$this->getid3->info;

$info['fileformat'] = 'zip';

$info['zip']['compressed_size'] = 0;
$info['zip']['uncompressed_size'] = 0;
$info['zip']['entries_count'] = 0;

rewind($this->getid3->fp);
while (
$fileentry $this->ZIPparseLocalFileHeader()) {
$info['zip']['entries'][] = $fileentry;
$info['zip']['entries_count']++;
}
if (
$info['zip']['entries_count'] == 0) {
$info['error'][] = 'No Local File Header entries found';
return 
false;
}

$info['zip']['entries_count'] = 0;
while (
$centraldirectoryentry $this->ZIPparseCentralDirectory($this->getid3->fp)) {
$info['zip']['central_directory'][] = $centraldirectoryentry;
$info['zip']['entries_count']++;
$info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
$info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
}
if (
$info['zip']['entries_count'] == 0) {
$info['error'][] = 'No Central Directory entries found (truncated file?)';
return 
false;
}

if (
$EOCD $this->ZIPparseEndOfCentralDirectory()) {
$info['zip']['end_central_directory'] = $EOCD;
} else {
$info['error'][] = 'No End Of Central Directory entry found (truncated file?)';
return 
false;
}

if (!empty(
$info['zip']['end_central_directory']['comment'])) {
$info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
}

return 
true;
}


public function 
getZIPentriesFilepointer() {
$info = &$this->getid3->info;

$info['zip']['compressed_size'] = 0;
$info['zip']['uncompressed_size'] = 0;
$info['zip']['entries_count'] = 0;

rewind($this->getid3->fp);
while (
$fileentry $this->ZIPparseLocalFileHeader()) {
$info['zip']['entries'][] = $fileentry;
$info['zip']['entries_count']++;
$info['zip']['compressed_size'] += $fileentry['compressed_size'];
$info['zip']['uncompressed_size'] += $fileentry['uncompressed_size'];
}
if (
$info['zip']['entries_count'] == 0) {
$info['error'][] = 'No Local File Header entries found';
return 
false;
}

return 
true;
}


public function 
ZIPparseLocalFileHeader() {
$LocalFileHeader['offset'] = $this->ftell();

$ZIPlocalFileHeader $this->fread(30);

$LocalFileHeader['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader04));
if (
$LocalFileHeader['raw']['signature'] != 0x04034B50) { 
 
 
$this->fseek($LocalFileHeader['offset']); 
 return 
false;
}
$LocalFileHeader['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader42));
$LocalFileHeader['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader62));
$LocalFileHeader['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader82));
$LocalFileHeader['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader102));
$LocalFileHeader['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader122));
$LocalFileHeader['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader144));
$LocalFileHeader['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader184));
$LocalFileHeader['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader224));
$LocalFileHeader['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader262));
$LocalFileHeader['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader282));

$LocalFileHeader['extract_version'] = sprintf('%1.1f'$LocalFileHeader['raw']['extract_version'] / 10);
$LocalFileHeader['host_os'] = $this->ZIPversionOSLookup(($LocalFileHeader['raw']['extract_version'] & 0xFF00) >> 8);
$LocalFileHeader['compression_method'] = $this->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']);
$LocalFileHeader['compressed_size'] = $LocalFileHeader['raw']['compressed_size'];
$LocalFileHeader['uncompressed_size'] = $LocalFileHeader['raw']['uncompressed_size'];
$LocalFileHeader['flags'] = $this->ZIPparseGeneralPurposeFlags($LocalFileHeader['raw']['general_flags'], $LocalFileHeader['raw']['compression_method']);
$LocalFileHeader['last_modified_timestamp'] = $this->DOStime2UNIXtime($LocalFileHeader['raw']['last_mod_file_date'], $LocalFileHeader['raw']['last_mod_file_time']);

$FilenameExtrafieldLength $LocalFileHeader['raw']['filename_length'] + $LocalFileHeader['raw']['extra_field_length'];
if (
$FilenameExtrafieldLength 0) {
$ZIPlocalFileHeader .= $this->fread($FilenameExtrafieldLength);

if (
$LocalFileHeader['raw']['filename_length'] > 0) {
$LocalFileHeader['filename'] = substr($ZIPlocalFileHeader30$LocalFileHeader['raw']['filename_length']);
}
if (
$LocalFileHeader['raw']['extra_field_length'] > 0) {
$LocalFileHeader['raw']['extra_field_data'] = substr($ZIPlocalFileHeader30 $LocalFileHeader['raw']['filename_length'], $LocalFileHeader['raw']['extra_field_length']);
}
}

if (
$LocalFileHeader['compressed_size'] == 0) {

 
 
 if (!empty(
$this->getid3->info['zip']['central_directory'])) {
foreach (
$this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
if (
$central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
if (
$central_directory_entry['compressed_size'] > 0) {

 
$LocalFileHeader['compressed_size'] = $central_directory_entry['compressed_size'];
}
break;
}
}
}

}
$LocalFileHeader['data_offset'] = $this->ftell();
$this->fseek($LocalFileHeader['compressed_size'], SEEK_CUR); 

if (
$LocalFileHeader['flags']['data_descriptor_used']) {
$DataDescriptor $this->fread(16);
$LocalFileHeader['data_descriptor']['signature'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor04));
if (
$LocalFileHeader['data_descriptor']['signature'] != 0x08074B50) { 
 
$this->getid3->warning[] = 'invalid Local File Header Data Descriptor Signature at offset '.($this->ftell() - 16).' - expecting 08 07 4B 50, found '.getid3_lib::PrintHexBytes($LocalFileHeader['data_descriptor']['signature']);
$this->fseek($LocalFileHeader['offset']); 
 return 
false;
}
$LocalFileHeader['data_descriptor']['crc_32'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor44));
$LocalFileHeader['data_descriptor']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor84));
$LocalFileHeader['data_descriptor']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor124));
if (!
$LocalFileHeader['raw']['compressed_size'] && $LocalFileHeader['data_descriptor']['compressed_size']) {
foreach (
$this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
if (
$central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
if (
$LocalFileHeader['data_descriptor']['compressed_size'] == $central_directory_entry['compressed_size']) {

 } else {
$this->getid3->info['warning'][] = 'conflicting compressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['compressed_size'].') vs Central Directory ('.$central_directory_entry['compressed_size'].') for file at offset '.$LocalFileHeader['offset'];
}

if (
$LocalFileHeader['data_descriptor']['uncompressed_size'] == $central_directory_entry['uncompressed_size']) {
$LocalFileHeader['uncompressed_size'] = $LocalFileHeader['data_descriptor']['uncompressed_size'];
} else {
$this->getid3->info['warning'][] = 'conflicting uncompressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['uncompressed_size'].') vs Central Directory ('.$central_directory_entry['uncompressed_size'].') for file at offset '.$LocalFileHeader['offset'];
}
break;
}
}
}
}
return 
$LocalFileHeader;
}


public function 
ZIPparseCentralDirectory() {
$CentralDirectory['offset'] = $this->ftell();

$ZIPcentralDirectory $this->fread(46);

$CentralDirectory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory04));
if (
$CentralDirectory['raw']['signature'] != 0x02014B50) {

 
$this->fseek($CentralDirectory['offset']); 
 return 
false;
}
$CentralDirectory['raw']['create_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory42));
$CentralDirectory['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory62));
$CentralDirectory['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory82));
$CentralDirectory['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory102));
$CentralDirectory['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory122));
$CentralDirectory['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory142));
$CentralDirectory['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory164));
$CentralDirectory['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory204));
$CentralDirectory['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory244));
$CentralDirectory['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory282));
$CentralDirectory['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory302));
$CentralDirectory['raw']['file_comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory322));
$CentralDirectory['raw']['disk_number_start'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory342));
$CentralDirectory['raw']['internal_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory362));
$CentralDirectory['raw']['external_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory384));
$CentralDirectory['raw']['local_header_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory424));

$CentralDirectory['entry_offset'] = $CentralDirectory['raw']['local_header_offset'];
$CentralDirectory['create_version'] = sprintf('%1.1f'$CentralDirectory['raw']['create_version'] / 10);
$CentralDirectory['extract_version'] = sprintf('%1.1f'$CentralDirectory['raw']['extract_version'] / 10);
$CentralDirectory['host_os'] = $this->ZIPversionOSLookup(($CentralDirectory['raw']['extract_version'] & 0xFF00) >> 8);
$CentralDirectory['compression_method'] = $this->ZIPcompressionMethodLookup($CentralDirectory['raw']['compression_method']);
$CentralDirectory['compressed_size'] = $CentralDirectory['raw']['compressed_size'];
$CentralDirectory['uncompressed_size'] = $CentralDirectory['raw']['uncompressed_size'];
$CentralDirectory['flags'] = $this->ZIPparseGeneralPurposeFlags($CentralDirectory['raw']['general_flags'], $CentralDirectory['raw']['compression_method']);
$CentralDirectory['last_modified_timestamp'] = $this->DOStime2UNIXtime($CentralDirectory['raw']['last_mod_file_date'], $CentralDirectory['raw']['last_mod_file_time']);

$FilenameExtrafieldCommentLength $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'] + $CentralDirectory['raw']['file_comment_length'];
if (
$FilenameExtrafieldCommentLength 0) {
$FilenameExtrafieldComment $this->fread($FilenameExtrafieldCommentLength);

if (
$CentralDirectory['raw']['filename_length'] > 0) {
$CentralDirectory['filename'] = substr($FilenameExtrafieldComment0$CentralDirectory['raw']['filename_length']);
}
if (
$CentralDirectory['raw']['extra_field_length'] > 0) {
$CentralDirectory['raw']['extra_field_data'] = substr($FilenameExtrafieldComment$CentralDirectory['raw']['filename_length'], $CentralDirectory['raw']['extra_field_length']);
}
if (
$CentralDirectory['raw']['file_comment_length'] > 0) {
$CentralDirectory['file_comment'] = substr($FilenameExtrafieldComment$CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'], $CentralDirectory['raw']['file_comment_length']);
}
}

return 
$CentralDirectory;
}

public function 
ZIPparseEndOfCentralDirectory() {
$EndOfCentralDirectory['offset'] = $this->ftell();

$ZIPendOfCentralDirectory $this->fread(22);

$EndOfCentralDirectory['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory04));
if (
$EndOfCentralDirectory['signature'] != 0x06054B50) {

 
$this->fseek($EndOfCentralDirectory['offset']); 
 return 
false;
}
$EndOfCentralDirectory['disk_number_current'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory42));
$EndOfCentralDirectory['disk_number_start_directory'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory62));
$EndOfCentralDirectory['directory_entries_this_disk'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory82));
$EndOfCentralDirectory['directory_entries_total'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory102));
$EndOfCentralDirectory['directory_size'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory124));
$EndOfCentralDirectory['directory_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory164));
$EndOfCentralDirectory['comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory202));

if (
$EndOfCentralDirectory['comment_length'] > 0) {
$EndOfCentralDirectory['comment'] = $this->fread($EndOfCentralDirectory['comment_length']);
}

return 
$EndOfCentralDirectory;
}


public static function 
ZIPparseGeneralPurposeFlags($flagbytes$compressionmethod) {

 
$ParsedFlags['encrypted'] = (bool) ($flagbytes 0x0001);

 
 
$ParsedFlags['data_descriptor_used'] = (bool) ($flagbytes 0x0008);
$ParsedFlags['enhanced_deflation'] = (bool) ($flagbytes 0x0010);
$ParsedFlags['compressed_patched_data'] = (bool) ($flagbytes 0x0020);
$ParsedFlags['strong_encryption'] = (bool) ($flagbytes 0x0040);

 
 
 
 
$ParsedFlags['language_encoding'] = (bool) ($flagbytes 0x0800);

 
$ParsedFlags['mask_header_values'] = (bool) ($flagbytes 0x2000);

 

switch (
$compressionmethod) {
case 
6:
$ParsedFlags['dictionary_size'] = (($flagbytes 0x0002) ? 8192 4096);
$ParsedFlags['shannon_fano_trees'] = (($flagbytes 0x0004) ? 2);
break;

case 
8:
case 
9:
switch ((
$flagbytes 0x0006) >> 1) {
case 
0:
$ParsedFlags['compression_speed'] = 'normal';
break;
case 
1:
$ParsedFlags['compression_speed'] = 'maximum';
break;
case 
2:
$ParsedFlags['compression_speed'] = 'fast';
break;
case 
3:
$ParsedFlags['compression_speed'] = 'superfast';
break;
}
break;
}

return 
$ParsedFlags;
}


public static function 
ZIPversionOSLookup($index) {
static 
$ZIPversionOSLookup = array(
=> 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
=> 'Amiga',
=> 'OpenVMS',
=> 'Unix',
=> 'VM/CMS',
=> 'Atari ST',
=> 'OS/2 H.P.F.S.',
=> 'Macintosh',
=> 'Z-System',
=> 'CP/M',
10 => 'Windows NTFS',
11 => 'MVS',
12 => 'VSE',
13 => 'Acorn Risc',
14 => 'VFAT',
15 => 'Alternate MVS',
16 => 'BeOS',
17 => 'Tandem',
18 => 'OS/400',
19 => 'OS/X (Darwin)',
);

return (isset(
$ZIPversionOSLookup[$index]) ? $ZIPversionOSLookup[$index] : '[unknown]');
}

public static function 
ZIPcompressionMethodLookup($index) {

 static 
$ZIPcompressionMethodLookup = array(
=> 'store',
=> 'shrink',
=> 'reduce-1',
=> 'reduce-2',
=> 'reduce-3',
=> 'reduce-4',
=> 'implode',
=> 'tokenize',
=> 'deflate',
=> 'deflate64',
10 => 'Imploded (old IBM TERSE)',
11 => 'RESERVED[11]',
12 => 'BZIP2',
13 => 'RESERVED[13]',
14 => 'LZMA (EFS)',
15 => 'RESERVED[15]',
16 => 'RESERVED[16]',
17 => 'RESERVED[17]',
18 => 'IBM TERSE (new)',
19 => 'IBM LZ77 z Architecture (PFS)',
96 => 'JPEG recompressed',
97 => 'WavPack compressed',
98 => 'PPMd version I, Rev 1',
);

return (isset(
$ZIPcompressionMethodLookup[$index]) ? $ZIPcompressionMethodLookup[$index] : '[unknown]');
}

public static function 
DOStime2UNIXtime($DOSdate$DOStime) {

 
 
 
 
 

$UNIXday = ($DOSdate 0x001F);
$UNIXmonth = (($DOSdate 0x01E0) >> 5);
$UNIXyear = (($DOSdate 0xFE00) >> 9) + 1980;


 
 
 
 
 

$UNIXsecond = ($DOStime 0x001F) * 2;
$UNIXminute = (($DOStime 0x07E0) >> 5);
$UNIXhour = (($DOStime 0xF800) >> 11);

return 
gmmktime($UNIXhour$UNIXminute$UNIXsecond$UNIXmonth$UNIXday$UNIXyear);
}

}

For more information about the PHK package format: http://phk.tekwire.net