7cb8719d2d82d53e4abd82a88fa32cf90282153d
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.archive.zip.php
1 <?php
2
3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
10 // //
11 // module.archive.zip.php //
12 // module for analyzing pkZip files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_zip extends getid3_handler
19 {
20 /**
21 * @return bool
22 */
23 public function Analyze() {
24 $info = &$this->getid3->info;
25
26 $info['fileformat'] = 'zip';
27 $info['zip']['encoding'] = 'ISO-8859-1';
28 $info['zip']['files'] = array();
29
30 $info['zip']['compressed_size'] = 0;
31 $info['zip']['uncompressed_size'] = 0;
32 $info['zip']['entries_count'] = 0;
33
34 if (!getid3_lib::intValueSupported($info['filesize'])) {
35 $this->error('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB, not supported by PHP');
36 return false;
37 } else {
38 $EOCDsearchData = '';
39 $EOCDsearchCounter = 0;
40 while ($EOCDsearchCounter++ < 512) {
41
42 $this->fseek(-128 * $EOCDsearchCounter, SEEK_END);
43 $EOCDsearchData = $this->fread(128).$EOCDsearchData;
44
45 if (strstr($EOCDsearchData, 'PK'."\x05\x06")) {
46
47 $EOCDposition = strpos($EOCDsearchData, 'PK'."\x05\x06");
48 $this->fseek((-128 * $EOCDsearchCounter) + $EOCDposition, SEEK_END);
49 $info['zip']['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory();
50
51 $this->fseek($info['zip']['end_central_directory']['directory_offset']);
52 $info['zip']['entries_count'] = 0;
53 while ($centraldirectoryentry = $this->ZIPparseCentralDirectory()) {
54 $info['zip']['central_directory'][] = $centraldirectoryentry;
55 $info['zip']['entries_count']++;
56 $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
57 $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
58
59 //if ($centraldirectoryentry['uncompressed_size'] > 0) { zero-byte files are valid
60 if (!empty($centraldirectoryentry['filename'])) {
61 $info['zip']['files'] = getid3_lib::array_merge_clobber($info['zip']['files'], getid3_lib::CreateDeepArray($centraldirectoryentry['filename'], '/', $centraldirectoryentry['uncompressed_size']));
62 }
63 }
64
65 if ($info['zip']['entries_count'] == 0) {
66 $this->error('No Central Directory entries found (truncated file?)');
67 return false;
68 }
69
70 if (!empty($info['zip']['end_central_directory']['comment'])) {
71 $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
72 }
73
74 if (isset($info['zip']['central_directory'][0]['compression_method'])) {
75 $info['zip']['compression_method'] = $info['zip']['central_directory'][0]['compression_method'];
76 }
77 if (isset($info['zip']['central_directory'][0]['flags']['compression_speed'])) {
78 $info['zip']['compression_speed'] = $info['zip']['central_directory'][0]['flags']['compression_speed'];
79 }
80 if (isset($info['zip']['compression_method']) && ($info['zip']['compression_method'] == 'store') && !isset($info['zip']['compression_speed'])) {
81 $info['zip']['compression_speed'] = 'store';
82 }
83
84 // secondary check - we (should) already have all the info we NEED from the Central Directory above, but scanning each
85 // Local File Header entry will
86 foreach ($info['zip']['central_directory'] as $central_directory_entry) {
87 $this->fseek($central_directory_entry['entry_offset']);
88 if ($fileentry = $this->ZIPparseLocalFileHeader()) {
89 $info['zip']['entries'][] = $fileentry;
90 } else {
91 $this->warning('Error parsing Local File Header at offset '.$central_directory_entry['entry_offset']);
92 }
93 }
94
95 if (!empty($info['zip']['files']['[Content_Types].xml']) &&
96 !empty($info['zip']['files']['_rels']['.rels']) &&
97 !empty($info['zip']['files']['docProps']['app.xml']) &&
98 !empty($info['zip']['files']['docProps']['core.xml'])) {
99 // http://technet.microsoft.com/en-us/library/cc179224.aspx
100 $info['fileformat'] = 'zip.msoffice';
101 if (!empty($ThisFileInfo['zip']['files']['ppt'])) {
102 $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
103 } elseif (!empty($ThisFileInfo['zip']['files']['xl'])) {
104 $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
105 } elseif (!empty($ThisFileInfo['zip']['files']['word'])) {
106 $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
107 }
108 }
109
110 return true;
111 }
112 }
113 }
114
115 if (!$this->getZIPentriesFilepointer()) {
116 unset($info['zip']);
117 $info['fileformat'] = '';
118 $this->error('Cannot find End Of Central Directory (truncated file?)');
119 return false;
120 }
121
122 // central directory couldn't be found and/or parsed
123 // scan through actual file data entries, recover as much as possible from probable trucated file
124 if ($info['zip']['compressed_size'] > ($info['filesize'] - 46 - 22)) {
125 $this->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)');
126 }
127 $this->error('Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete');
128 foreach ($info['zip']['entries'] as $key => $valuearray) {
129 $info['zip']['files'][$valuearray['filename']] = $valuearray['uncompressed_size'];
130 }
131 return true;
132 }
133
134 /**
135 * @return bool
136 */
137 public function getZIPHeaderFilepointerTopDown() {
138 $info = &$this->getid3->info;
139
140 $info['fileformat'] = 'zip';
141
142 $info['zip']['compressed_size'] = 0;
143 $info['zip']['uncompressed_size'] = 0;
144 $info['zip']['entries_count'] = 0;
145
146 rewind($this->getid3->fp);
147 while ($fileentry = $this->ZIPparseLocalFileHeader()) {
148 $info['zip']['entries'][] = $fileentry;
149 $info['zip']['entries_count']++;
150 }
151 if ($info['zip']['entries_count'] == 0) {
152 $this->error('No Local File Header entries found');
153 return false;
154 }
155
156 $info['zip']['entries_count'] = 0;
157 while ($centraldirectoryentry = $this->ZIPparseCentralDirectory()) {
158 $info['zip']['central_directory'][] = $centraldirectoryentry;
159 $info['zip']['entries_count']++;
160 $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
161 $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
162 }
163 if ($info['zip']['entries_count'] == 0) {
164 $this->error('No Central Directory entries found (truncated file?)');
165 return false;
166 }
167
168 if ($EOCD = $this->ZIPparseEndOfCentralDirectory()) {
169 $info['zip']['end_central_directory'] = $EOCD;
170 } else {
171 $this->error('No End Of Central Directory entry found (truncated file?)');
172 return false;
173 }
174
175 if (!empty($info['zip']['end_central_directory']['comment'])) {
176 $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
177 }
178
179 return true;
180 }
181
182 /**
183 * @return bool
184 */
185 public function getZIPentriesFilepointer() {
186 $info = &$this->getid3->info;
187
188 $info['zip']['compressed_size'] = 0;
189 $info['zip']['uncompressed_size'] = 0;
190 $info['zip']['entries_count'] = 0;
191
192 rewind($this->getid3->fp);
193 while ($fileentry = $this->ZIPparseLocalFileHeader()) {
194 $info['zip']['entries'][] = $fileentry;
195 $info['zip']['entries_count']++;
196 $info['zip']['compressed_size'] += $fileentry['compressed_size'];
197 $info['zip']['uncompressed_size'] += $fileentry['uncompressed_size'];
198 }
199 if ($info['zip']['entries_count'] == 0) {
200 $this->error('No Local File Header entries found');
201 return false;
202 }
203
204 return true;
205 }
206
207 /**
208 * @return array|false
209 */
210 public function ZIPparseLocalFileHeader() {
211 $LocalFileHeader['offset'] = $this->ftell();
212
213 $ZIPlocalFileHeader = $this->fread(30);
214
215 $LocalFileHeader['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 0, 4));
216 if ($LocalFileHeader['raw']['signature'] != 0x04034B50) { // "PK\x03\x04"
217 // invalid Local File Header Signature
218 $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
219 return false;
220 }
221 $LocalFileHeader['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 4, 2));
222 $LocalFileHeader['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 6, 2));
223 $LocalFileHeader['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 8, 2));
224 $LocalFileHeader['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 10, 2));
225 $LocalFileHeader['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 12, 2));
226 $LocalFileHeader['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 14, 4));
227 $LocalFileHeader['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 18, 4));
228 $LocalFileHeader['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 22, 4));
229 $LocalFileHeader['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 26, 2));
230 $LocalFileHeader['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 28, 2));
231
232 $LocalFileHeader['extract_version'] = sprintf('%1.1f', $LocalFileHeader['raw']['extract_version'] / 10);
233 $LocalFileHeader['host_os'] = $this->ZIPversionOSLookup(($LocalFileHeader['raw']['extract_version'] & 0xFF00) >> 8);
234 $LocalFileHeader['compression_method'] = $this->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']);
235 $LocalFileHeader['compressed_size'] = $LocalFileHeader['raw']['compressed_size'];
236 $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['raw']['uncompressed_size'];
237 $LocalFileHeader['flags'] = $this->ZIPparseGeneralPurposeFlags($LocalFileHeader['raw']['general_flags'], $LocalFileHeader['raw']['compression_method']);
238 $LocalFileHeader['last_modified_timestamp'] = $this->DOStime2UNIXtime($LocalFileHeader['raw']['last_mod_file_date'], $LocalFileHeader['raw']['last_mod_file_time']);
239
240 $FilenameExtrafieldLength = $LocalFileHeader['raw']['filename_length'] + $LocalFileHeader['raw']['extra_field_length'];
241 if ($FilenameExtrafieldLength > 0) {
242 $ZIPlocalFileHeader .= $this->fread($FilenameExtrafieldLength);
243
244 if ($LocalFileHeader['raw']['filename_length'] > 0) {
245 $LocalFileHeader['filename'] = substr($ZIPlocalFileHeader, 30, $LocalFileHeader['raw']['filename_length']);
246 }
247 if ($LocalFileHeader['raw']['extra_field_length'] > 0) {
248 $LocalFileHeader['raw']['extra_field_data'] = substr($ZIPlocalFileHeader, 30 + $LocalFileHeader['raw']['filename_length'], $LocalFileHeader['raw']['extra_field_length']);
249 }
250 }
251
252 if ($LocalFileHeader['compressed_size'] == 0) {
253 // *Could* be a zero-byte file
254 // But could also be a file written on the fly that didn't know compressed filesize beforehand.
255 // Correct compressed filesize should be in the data_descriptor located after this file data, and also in Central Directory (at end of zip file)
256 if (!empty($this->getid3->info['zip']['central_directory'])) {
257 foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
258 if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
259 if ($central_directory_entry['compressed_size'] > 0) {
260 // overwrite local zero value (but not ['raw']'compressed_size']) so that seeking for data_descriptor (and next file entry) works correctly
261 $LocalFileHeader['compressed_size'] = $central_directory_entry['compressed_size'];
262 }
263 break;
264 }
265 }
266 }
267
268 }
269 $LocalFileHeader['data_offset'] = $this->ftell();
270 $this->fseek($LocalFileHeader['compressed_size'], SEEK_CUR); // this should (but may not) match value in $LocalFileHeader['raw']['compressed_size'] -- $LocalFileHeader['compressed_size'] could have been overwritten above with value from Central Directory
271
272 if ($LocalFileHeader['flags']['data_descriptor_used']) {
273 $DataDescriptor = $this->fread(16);
274 $LocalFileHeader['data_descriptor']['signature'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 0, 4));
275 if ($LocalFileHeader['data_descriptor']['signature'] != 0x08074B50) { // "PK\x07\x08"
276 $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']));
277 $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
278 return false;
279 }
280 $LocalFileHeader['data_descriptor']['crc_32'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 4, 4));
281 $LocalFileHeader['data_descriptor']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 8, 4));
282 $LocalFileHeader['data_descriptor']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 12, 4));
283 if (!$LocalFileHeader['raw']['compressed_size'] && $LocalFileHeader['data_descriptor']['compressed_size']) {
284 foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
285 if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
286 if ($LocalFileHeader['data_descriptor']['compressed_size'] == $central_directory_entry['compressed_size']) {
287 // $LocalFileHeader['compressed_size'] already set from Central Directory
288 } else {
289 $this->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']);
290 }
291
292 if ($LocalFileHeader['data_descriptor']['uncompressed_size'] == $central_directory_entry['uncompressed_size']) {
293 $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['data_descriptor']['uncompressed_size'];
294 } else {
295 $this->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']);
296 }
297 break;
298 }
299 }
300 }
301 }
302 return $LocalFileHeader;
303 }
304
305 /**
306 * @return array|false
307 */
308 public function ZIPparseCentralDirectory() {
309 $CentralDirectory['offset'] = $this->ftell();
310
311 $ZIPcentralDirectory = $this->fread(46);
312
313 $CentralDirectory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 0, 4));
314 if ($CentralDirectory['raw']['signature'] != 0x02014B50) {
315 // invalid Central Directory Signature
316 $this->fseek($CentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
317 return false;
318 }
319 $CentralDirectory['raw']['create_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 4, 2));
320 $CentralDirectory['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 6, 2));
321 $CentralDirectory['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 8, 2));
322 $CentralDirectory['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 10, 2));
323 $CentralDirectory['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 12, 2));
324 $CentralDirectory['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 14, 2));
325 $CentralDirectory['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 16, 4));
326 $CentralDirectory['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 20, 4));
327 $CentralDirectory['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 24, 4));
328 $CentralDirectory['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 28, 2));
329 $CentralDirectory['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 30, 2));
330 $CentralDirectory['raw']['file_comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 32, 2));
331 $CentralDirectory['raw']['disk_number_start'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 34, 2));
332 $CentralDirectory['raw']['internal_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 36, 2));
333 $CentralDirectory['raw']['external_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 38, 4));
334 $CentralDirectory['raw']['local_header_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 42, 4));
335
336 $CentralDirectory['entry_offset'] = $CentralDirectory['raw']['local_header_offset'];
337 $CentralDirectory['create_version'] = sprintf('%1.1f', $CentralDirectory['raw']['create_version'] / 10);
338 $CentralDirectory['extract_version'] = sprintf('%1.1f', $CentralDirectory['raw']['extract_version'] / 10);
339 $CentralDirectory['host_os'] = $this->ZIPversionOSLookup(($CentralDirectory['raw']['extract_version'] & 0xFF00) >> 8);
340 $CentralDirectory['compression_method'] = $this->ZIPcompressionMethodLookup($CentralDirectory['raw']['compression_method']);
341 $CentralDirectory['compressed_size'] = $CentralDirectory['raw']['compressed_size'];
342 $CentralDirectory['uncompressed_size'] = $CentralDirectory['raw']['uncompressed_size'];
343 $CentralDirectory['flags'] = $this->ZIPparseGeneralPurposeFlags($CentralDirectory['raw']['general_flags'], $CentralDirectory['raw']['compression_method']);
344 $CentralDirectory['last_modified_timestamp'] = $this->DOStime2UNIXtime($CentralDirectory['raw']['last_mod_file_date'], $CentralDirectory['raw']['last_mod_file_time']);
345
346 $FilenameExtrafieldCommentLength = $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'] + $CentralDirectory['raw']['file_comment_length'];
347 if ($FilenameExtrafieldCommentLength > 0) {
348 $FilenameExtrafieldComment = $this->fread($FilenameExtrafieldCommentLength);
349
350 if ($CentralDirectory['raw']['filename_length'] > 0) {
351 $CentralDirectory['filename'] = substr($FilenameExtrafieldComment, 0, $CentralDirectory['raw']['filename_length']);
352 }
353 if ($CentralDirectory['raw']['extra_field_length'] > 0) {
354 $CentralDirectory['raw']['extra_field_data'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'], $CentralDirectory['raw']['extra_field_length']);
355 }
356 if ($CentralDirectory['raw']['file_comment_length'] > 0) {
357 $CentralDirectory['file_comment'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'], $CentralDirectory['raw']['file_comment_length']);
358 }
359 }
360
361 return $CentralDirectory;
362 }
363
364 /**
365 * @return array|false
366 */
367 public function ZIPparseEndOfCentralDirectory() {
368 $EndOfCentralDirectory['offset'] = $this->ftell();
369
370 $ZIPendOfCentralDirectory = $this->fread(22);
371
372 $EndOfCentralDirectory['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 0, 4));
373 if ($EndOfCentralDirectory['signature'] != 0x06054B50) {
374 // invalid End Of Central Directory Signature
375 $this->fseek($EndOfCentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
376 return false;
377 }
378 $EndOfCentralDirectory['disk_number_current'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 4, 2));
379 $EndOfCentralDirectory['disk_number_start_directory'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 6, 2));
380 $EndOfCentralDirectory['directory_entries_this_disk'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 8, 2));
381 $EndOfCentralDirectory['directory_entries_total'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 10, 2));
382 $EndOfCentralDirectory['directory_size'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 12, 4));
383 $EndOfCentralDirectory['directory_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 16, 4));
384 $EndOfCentralDirectory['comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 20, 2));
385
386 if ($EndOfCentralDirectory['comment_length'] > 0) {
387 $EndOfCentralDirectory['comment'] = $this->fread($EndOfCentralDirectory['comment_length']);
388 }
389
390 return $EndOfCentralDirectory;
391 }
392
393 /**
394 * @param int $flagbytes
395 * @param int $compressionmethod
396 *
397 * @return array
398 */
399 public static function ZIPparseGeneralPurposeFlags($flagbytes, $compressionmethod) {
400 // https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip-printable.html
401 $ParsedFlags = array();
402 $ParsedFlags['encrypted'] = (bool) ($flagbytes & 0x0001);
403 // 0x0002 -- see below
404 // 0x0004 -- see below
405 $ParsedFlags['data_descriptor_used'] = (bool) ($flagbytes & 0x0008);
406 $ParsedFlags['enhanced_deflation'] = (bool) ($flagbytes & 0x0010);
407 $ParsedFlags['compressed_patched_data'] = (bool) ($flagbytes & 0x0020);
408 $ParsedFlags['strong_encryption'] = (bool) ($flagbytes & 0x0040);
409 // 0x0080 - unused
410 // 0x0100 - unused
411 // 0x0200 - unused
412 // 0x0400 - unused
413 $ParsedFlags['language_encoding'] = (bool) ($flagbytes & 0x0800);
414 // 0x1000 - reserved
415 $ParsedFlags['mask_header_values'] = (bool) ($flagbytes & 0x2000);
416 // 0x4000 - reserved
417 // 0x8000 - reserved
418
419 switch ($compressionmethod) {
420 case 6:
421 $ParsedFlags['dictionary_size'] = (($flagbytes & 0x0002) ? 8192 : 4096);
422 $ParsedFlags['shannon_fano_trees'] = (($flagbytes & 0x0004) ? 3 : 2);
423 break;
424
425 case 8:
426 case 9:
427 switch (($flagbytes & 0x0006) >> 1) {
428 case 0:
429 $ParsedFlags['compression_speed'] = 'normal';
430 break;
431 case 1:
432 $ParsedFlags['compression_speed'] = 'maximum';
433 break;
434 case 2:
435 $ParsedFlags['compression_speed'] = 'fast';
436 break;
437 case 3:
438 $ParsedFlags['compression_speed'] = 'superfast';
439 break;
440 }
441 break;
442 }
443
444 return $ParsedFlags;
445 }
446
447 /**
448 * @param int $index
449 *
450 * @return string
451 */
452 public static function ZIPversionOSLookup($index) {
453 static $ZIPversionOSLookup = array(
454 0 => 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
455 1 => 'Amiga',
456 2 => 'OpenVMS',
457 3 => 'Unix',
458 4 => 'VM/CMS',
459 5 => 'Atari ST',
460 6 => 'OS/2 H.P.F.S.',
461 7 => 'Macintosh',
462 8 => 'Z-System',
463 9 => 'CP/M',
464 10 => 'Windows NTFS',
465 11 => 'MVS',
466 12 => 'VSE',
467 13 => 'Acorn Risc',
468 14 => 'VFAT',
469 15 => 'Alternate MVS',
470 16 => 'BeOS',
471 17 => 'Tandem',
472 18 => 'OS/400',
473 19 => 'OS/X (Darwin)',
474 );
475
476 return (isset($ZIPversionOSLookup[$index]) ? $ZIPversionOSLookup[$index] : '[unknown]');
477 }
478
479 /**
480 * @param int $index
481 *
482 * @return string
483 */
484 public static function ZIPcompressionMethodLookup($index) {
485 // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/ZIP.html
486 static $ZIPcompressionMethodLookup = array(
487 0 => 'store',
488 1 => 'shrink',
489 2 => 'reduce-1',
490 3 => 'reduce-2',
491 4 => 'reduce-3',
492 5 => 'reduce-4',
493 6 => 'implode',
494 7 => 'tokenize',
495 8 => 'deflate',
496 9 => 'deflate64',
497 10 => 'Imploded (old IBM TERSE)',
498 11 => 'RESERVED[11]',
499 12 => 'BZIP2',
500 13 => 'RESERVED[13]',
501 14 => 'LZMA (EFS)',
502 15 => 'RESERVED[15]',
503 16 => 'RESERVED[16]',
504 17 => 'RESERVED[17]',
505 18 => 'IBM TERSE (new)',
506 19 => 'IBM LZ77 z Architecture (PFS)',
507 96 => 'JPEG recompressed',
508 97 => 'WavPack compressed',
509 98 => 'PPMd version I, Rev 1',
510 );
511
512 return (isset($ZIPcompressionMethodLookup[$index]) ? $ZIPcompressionMethodLookup[$index] : '[unknown]');
513 }
514
515 /**
516 * @param int $DOSdate
517 * @param int $DOStime
518 *
519 * @return int
520 */
521 public static function DOStime2UNIXtime($DOSdate, $DOStime) {
522 // wFatDate
523 // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
524 // Bits Contents
525 // 0-4 Day of the month (1-31)
526 // 5-8 Month (1 = January, 2 = February, and so on)
527 // 9-15 Year offset from 1980 (add 1980 to get actual year)
528
529 $UNIXday = ($DOSdate & 0x001F);
530 $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
531 $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
532
533 // wFatTime
534 // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
535 // Bits Contents
536 // 0-4 Second divided by 2
537 // 5-10 Minute (0-59)
538 // 11-15 Hour (0-23 on a 24-hour clock)
539
540 $UNIXsecond = ($DOStime & 0x001F) * 2;
541 $UNIXminute = (($DOStime & 0x07E0) >> 5);
542 $UNIXhour = (($DOStime & 0xF800) >> 11);
543
544 return gmmktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
545 }
546
547 }