ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.archive.zip.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
7 // See readme.txt for more details //
9 // //
10 // module.archive.zip.php //
11 // module for analyzing pkZip files //
12 // dependencies: NONE //
13 // ///
15 
16 
18 {
19 
20  function getid3_zip(&$fd, &$ThisFileInfo) {
21 
22  $ThisFileInfo['fileformat'] = 'zip';
23  $ThisFileInfo['zip']['encoding'] = 'ISO-8859-1';
24  $ThisFileInfo['zip']['files'] = array();
25 
26  $ThisFileInfo['zip']['compressed_size'] = 0;
27  $ThisFileInfo['zip']['uncompressed_size'] = 0;
28  $ThisFileInfo['zip']['entries_count'] = 0;
29 
30  $EOCDsearchData = '';
31  $EOCDsearchCounter = 0;
32  while ($EOCDsearchCounter++ < 512) {
33 
34  fseek($fd, -128 * $EOCDsearchCounter, SEEK_END);
35  $EOCDsearchData = fread($fd, 128).$EOCDsearchData;
36 
37  if (strstr($EOCDsearchData, 'PK'."\x05\x06")) {
38 
39  $EOCDposition = strpos($EOCDsearchData, 'PK'."\x05\x06");
40  fseek($fd, (-128 * $EOCDsearchCounter) + $EOCDposition, SEEK_END);
41  $ThisFileInfo['zip']['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory($fd);
42 
43  fseek($fd, $ThisFileInfo['zip']['end_central_directory']['directory_offset'], SEEK_SET);
44  $ThisFileInfo['zip']['entries_count'] = 0;
45  while ($centraldirectoryentry = $this->ZIPparseCentralDirectory($fd)) {
46  $ThisFileInfo['zip']['central_directory'][] = $centraldirectoryentry;
47  $ThisFileInfo['zip']['entries_count']++;
48  $ThisFileInfo['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
49  $ThisFileInfo['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
50 
51  if ($centraldirectoryentry['uncompressed_size'] > 0) {
52  $ThisFileInfo['zip']['files'] = getid3_lib::array_merge_clobber($ThisFileInfo['zip']['files'], getid3_lib::CreateDeepArray($centraldirectoryentry['filename'], '/', $centraldirectoryentry['uncompressed_size']));
53  }
54  }
55 
56  if ($ThisFileInfo['zip']['entries_count'] == 0) {
57  $ThisFileInfo['error'][] = 'No Central Directory entries found (truncated file?)';
58  return false;
59  }
60 
61  if (!empty($ThisFileInfo['zip']['end_central_directory']['comment'])) {
62  $ThisFileInfo['zip']['comments']['comment'][] = $ThisFileInfo['zip']['end_central_directory']['comment'];
63  }
64 
65  if (isset($ThisFileInfo['zip']['central_directory'][0]['compression_method'])) {
66  $ThisFileInfo['zip']['compression_method'] = $ThisFileInfo['zip']['central_directory'][0]['compression_method'];
67  }
68  if (isset($ThisFileInfo['zip']['central_directory'][0]['flags']['compression_speed'])) {
69  $ThisFileInfo['zip']['compression_speed'] = $ThisFileInfo['zip']['central_directory'][0]['flags']['compression_speed'];
70  }
71  if (isset($ThisFileInfo['zip']['compression_method']) && ($ThisFileInfo['zip']['compression_method'] == 'store') && !isset($ThisFileInfo['zip']['compression_speed'])) {
72  $ThisFileInfo['zip']['compression_speed'] = 'store';
73  }
74 
75  return true;
76 
77  }
78 
79  }
80 
81  if ($this->getZIPentriesFilepointer($fd, $ThisFileInfo)) {
82 
83  // central directory couldn't be found and/or parsed
84  // scan through actual file data entries, recover as much as possible from probable trucated file
85  if ($ThisFileInfo['zip']['compressed_size'] > ($ThisFileInfo['filesize'] - 46 - 22)) {
86  $ThisFileInfo['error'][] = 'Warning: Truncated file! - Total compressed file sizes ('.$ThisFileInfo['zip']['compressed_size'].' bytes) is greater than filesize minus Central Directory and End Of Central Directory structures ('.($ThisFileInfo['filesize'] - 46 - 22).' bytes)';
87  }
88  $ThisFileInfo['error'][] = 'Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete';
89  foreach ($ThisFileInfo['zip']['entries'] as $key => $valuearray) {
90  $ThisFileInfo['zip']['files'][$valuearray['filename']] = $valuearray['uncompressed_size'];
91  }
92  return true;
93 
94  } else {
95 
96  unset($ThisFileInfo['zip']);
97  $ThisFileInfo['fileformat'] = '';
98  $ThisFileInfo['error'][] = 'Cannot find End Of Central Directory (truncated file?)';
99  return false;
100 
101  }
102  }
103 
104 
105  function getZIPHeaderFilepointerTopDown(&$fd, &$ThisFileInfo) {
106  $ThisFileInfo['fileformat'] = 'zip';
107 
108  $ThisFileInfo['zip']['compressed_size'] = 0;
109  $ThisFileInfo['zip']['uncompressed_size'] = 0;
110  $ThisFileInfo['zip']['entries_count'] = 0;
111 
112  rewind($fd);
113  while ($fileentry = $this->ZIPparseLocalFileHeader($fd)) {
114  $ThisFileInfo['zip']['entries'][] = $fileentry;
115  $ThisFileInfo['zip']['entries_count']++;
116  }
117  if ($ThisFileInfo['zip']['entries_count'] == 0) {
118  $ThisFileInfo['error'][] = 'No Local File Header entries found';
119  return false;
120  }
121 
122  $ThisFileInfo['zip']['entries_count'] = 0;
123  while ($centraldirectoryentry = $this->ZIPparseCentralDirectory($fd)) {
124  $ThisFileInfo['zip']['central_directory'][] = $centraldirectoryentry;
125  $ThisFileInfo['zip']['entries_count']++;
126  $ThisFileInfo['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
127  $ThisFileInfo['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
128  }
129  if ($ThisFileInfo['zip']['entries_count'] == 0) {
130  $ThisFileInfo['error'][] = 'No Central Directory entries found (truncated file?)';
131  return false;
132  }
133 
134  if ($EOCD = $this->ZIPparseEndOfCentralDirectory($fd)) {
135  $ThisFileInfo['zip']['end_central_directory'] = $EOCD;
136  } else {
137  $ThisFileInfo['error'][] = 'No End Of Central Directory entry found (truncated file?)';
138  return false;
139  }
140 
141  if (!empty($ThisFileInfo['zip']['end_central_directory']['comment'])) {
142  $ThisFileInfo['zip']['comments']['comment'][] = $ThisFileInfo['zip']['end_central_directory']['comment'];
143  }
144 
145  return true;
146  }
147 
148 
149  function getZIPentriesFilepointer(&$fd, &$ThisFileInfo) {
150  $ThisFileInfo['zip']['compressed_size'] = 0;
151  $ThisFileInfo['zip']['uncompressed_size'] = 0;
152  $ThisFileInfo['zip']['entries_count'] = 0;
153 
154  rewind($fd);
155  while ($fileentry = $this->ZIPparseLocalFileHeader($fd)) {
156  $ThisFileInfo['zip']['entries'][] = $fileentry;
157  $ThisFileInfo['zip']['entries_count']++;
158  $ThisFileInfo['zip']['compressed_size'] += $fileentry['compressed_size'];
159  $ThisFileInfo['zip']['uncompressed_size'] += $fileentry['uncompressed_size'];
160  }
161  if ($ThisFileInfo['zip']['entries_count'] == 0) {
162  $ThisFileInfo['error'][] = 'No Local File Header entries found';
163  return false;
164  }
165 
166  return true;
167  }
168 
169 
170  function ZIPparseLocalFileHeader(&$fd) {
171  $LocalFileHeader['offset'] = ftell($fd);
172 
173  $ZIPlocalFileHeader = fread($fd, 30);
174 
175  $LocalFileHeader['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 0, 4));
176  if ($LocalFileHeader['raw']['signature'] != 0x04034B50) {
177  // invalid Local File Header Signature
178  fseek($fd, $LocalFileHeader['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
179  return false;
180  }
181  $LocalFileHeader['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 4, 2));
182  $LocalFileHeader['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 6, 2));
183  $LocalFileHeader['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 8, 2));
184  $LocalFileHeader['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 10, 2));
185  $LocalFileHeader['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 12, 2));
186  $LocalFileHeader['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 14, 4));
187  $LocalFileHeader['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 18, 4));
188  $LocalFileHeader['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 22, 4));
189  $LocalFileHeader['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 26, 2));
190  $LocalFileHeader['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 28, 2));
191 
192  $LocalFileHeader['extract_version'] = sprintf('%1.1f', $LocalFileHeader['raw']['extract_version'] / 10);
193  $LocalFileHeader['host_os'] = $this->ZIPversionOSLookup(($LocalFileHeader['raw']['extract_version'] & 0xFF00) >> 8);
194  $LocalFileHeader['compression_method'] = $this->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']);
195  $LocalFileHeader['compressed_size'] = $LocalFileHeader['raw']['compressed_size'];
196  $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['raw']['uncompressed_size'];
197  $LocalFileHeader['flags'] = $this->ZIPparseGeneralPurposeFlags($LocalFileHeader['raw']['general_flags'], $LocalFileHeader['raw']['compression_method']);
198  $LocalFileHeader['last_modified_timestamp'] = $this->DOStime2UNIXtime($LocalFileHeader['raw']['last_mod_file_date'], $LocalFileHeader['raw']['last_mod_file_time']);
199 
200  $FilenameExtrafieldLength = $LocalFileHeader['raw']['filename_length'] + $LocalFileHeader['raw']['extra_field_length'];
201  if ($FilenameExtrafieldLength > 0) {
202  $ZIPlocalFileHeader .= fread($fd, $FilenameExtrafieldLength);
203 
204  if ($LocalFileHeader['raw']['filename_length'] > 0) {
205  $LocalFileHeader['filename'] = substr($ZIPlocalFileHeader, 30, $LocalFileHeader['raw']['filename_length']);
206  }
207  if ($LocalFileHeader['raw']['extra_field_length'] > 0) {
208  $LocalFileHeader['raw']['extra_field_data'] = substr($ZIPlocalFileHeader, 30 + $LocalFileHeader['raw']['filename_length'], $LocalFileHeader['raw']['extra_field_length']);
209  }
210  }
211 
212  $LocalFileHeader['data_offset'] = ftell($fd);
213  //$LocalFileHeader['compressed_data'] = fread($fd, $LocalFileHeader['raw']['compressed_size']);
214  fseek($fd, $LocalFileHeader['raw']['compressed_size'], SEEK_CUR);
215 
216  if ($LocalFileHeader['flags']['data_descriptor_used']) {
217  $DataDescriptor = fread($fd, 12);
218  $LocalFileHeader['data_descriptor']['crc_32'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 0, 4));
219  $LocalFileHeader['data_descriptor']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 4, 4));
220  $LocalFileHeader['data_descriptor']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 8, 4));
221  }
222 
223  return $LocalFileHeader;
224  }
225 
226 
227  function ZIPparseCentralDirectory(&$fd) {
228  $CentralDirectory['offset'] = ftell($fd);
229 
230  $ZIPcentralDirectory = fread($fd, 46);
231 
232  $CentralDirectory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 0, 4));
233  if ($CentralDirectory['raw']['signature'] != 0x02014B50) {
234  // invalid Central Directory Signature
235  fseek($fd, $CentralDirectory['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
236  return false;
237  }
238  $CentralDirectory['raw']['create_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 4, 2));
239  $CentralDirectory['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 6, 2));
240  $CentralDirectory['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 8, 2));
241  $CentralDirectory['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 10, 2));
242  $CentralDirectory['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 12, 2));
243  $CentralDirectory['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 14, 2));
244  $CentralDirectory['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 16, 4));
245  $CentralDirectory['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 20, 4));
246  $CentralDirectory['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 24, 4));
247  $CentralDirectory['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 28, 2));
248  $CentralDirectory['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 30, 2));
249  $CentralDirectory['raw']['file_comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 32, 2));
250  $CentralDirectory['raw']['disk_number_start'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 34, 2));
251  $CentralDirectory['raw']['internal_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 36, 2));
252  $CentralDirectory['raw']['external_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 38, 4));
253  $CentralDirectory['raw']['local_header_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 42, 4));
254 
255  $CentralDirectory['entry_offset'] = $CentralDirectory['raw']['local_header_offset'];
256  $CentralDirectory['create_version'] = sprintf('%1.1f', $CentralDirectory['raw']['create_version'] / 10);
257  $CentralDirectory['extract_version'] = sprintf('%1.1f', $CentralDirectory['raw']['extract_version'] / 10);
258  $CentralDirectory['host_os'] = $this->ZIPversionOSLookup(($CentralDirectory['raw']['extract_version'] & 0xFF00) >> 8);
259  $CentralDirectory['compression_method'] = $this->ZIPcompressionMethodLookup($CentralDirectory['raw']['compression_method']);
260  $CentralDirectory['compressed_size'] = $CentralDirectory['raw']['compressed_size'];
261  $CentralDirectory['uncompressed_size'] = $CentralDirectory['raw']['uncompressed_size'];
262  $CentralDirectory['flags'] = $this->ZIPparseGeneralPurposeFlags($CentralDirectory['raw']['general_flags'], $CentralDirectory['raw']['compression_method']);
263  $CentralDirectory['last_modified_timestamp'] = $this->DOStime2UNIXtime($CentralDirectory['raw']['last_mod_file_date'], $CentralDirectory['raw']['last_mod_file_time']);
264 
265  $FilenameExtrafieldCommentLength = $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'] + $CentralDirectory['raw']['file_comment_length'];
266  if ($FilenameExtrafieldCommentLength > 0) {
267  $FilenameExtrafieldComment = fread($fd, $FilenameExtrafieldCommentLength);
268 
269  if ($CentralDirectory['raw']['filename_length'] > 0) {
270  $CentralDirectory['filename'] = substr($FilenameExtrafieldComment, 0, $CentralDirectory['raw']['filename_length']);
271  }
272  if ($CentralDirectory['raw']['extra_field_length'] > 0) {
273  $CentralDirectory['raw']['extra_field_data'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'], $CentralDirectory['raw']['extra_field_length']);
274  }
275  if ($CentralDirectory['raw']['file_comment_length'] > 0) {
276  $CentralDirectory['file_comment'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'], $CentralDirectory['raw']['file_comment_length']);
277  }
278  }
279 
280  return $CentralDirectory;
281  }
282 
284  $EndOfCentralDirectory['offset'] = ftell($fd);
285 
286  $ZIPendOfCentralDirectory = fread($fd, 22);
287 
288  $EndOfCentralDirectory['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 0, 4));
289  if ($EndOfCentralDirectory['signature'] != 0x06054B50) {
290  // invalid End Of Central Directory Signature
291  fseek($fd, $EndOfCentralDirectory['offset'], SEEK_SET); // seek back to where filepointer originally was so it can be handled properly
292  return false;
293  }
294  $EndOfCentralDirectory['disk_number_current'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 4, 2));
295  $EndOfCentralDirectory['disk_number_start_directory'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 6, 2));
296  $EndOfCentralDirectory['directory_entries_this_disk'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 8, 2));
297  $EndOfCentralDirectory['directory_entries_total'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 10, 2));
298  $EndOfCentralDirectory['directory_size'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 12, 4));
299  $EndOfCentralDirectory['directory_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 16, 4));
300  $EndOfCentralDirectory['comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 20, 2));
301 
302  if ($EndOfCentralDirectory['comment_length'] > 0) {
303  $EndOfCentralDirectory['comment'] = fread($fd, $EndOfCentralDirectory['comment_length']);
304  }
305 
306  return $EndOfCentralDirectory;
307  }
308 
309 
310  function ZIPparseGeneralPurposeFlags($flagbytes, $compressionmethod) {
311  $ParsedFlags['encrypted'] = (bool) ($flagbytes & 0x0001);
312 
313  switch ($compressionmethod) {
314  case 6:
315  $ParsedFlags['dictionary_size'] = (($flagbytes & 0x0002) ? 8192 : 4096);
316  $ParsedFlags['shannon_fano_trees'] = (($flagbytes & 0x0004) ? 3 : 2);
317  break;
318 
319  case 8:
320  case 9:
321  switch (($flagbytes & 0x0006) >> 1) {
322  case 0:
323  $ParsedFlags['compression_speed'] = 'normal';
324  break;
325  case 1:
326  $ParsedFlags['compression_speed'] = 'maximum';
327  break;
328  case 2:
329  $ParsedFlags['compression_speed'] = 'fast';
330  break;
331  case 3:
332  $ParsedFlags['compression_speed'] = 'superfast';
333  break;
334  }
335  break;
336  }
337  $ParsedFlags['data_descriptor_used'] = (bool) ($flagbytes & 0x0008);
338 
339  return $ParsedFlags;
340  }
341 
342 
343  function ZIPversionOSLookup($index) {
344  static $ZIPversionOSLookup = array(
345  0 => 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
346  1 => 'Amiga',
347  2 => 'OpenVMS',
348  3 => 'Unix',
349  4 => 'VM/CMS',
350  5 => 'Atari ST',
351  6 => 'OS/2 H.P.F.S.',
352  7 => 'Macintosh',
353  8 => 'Z-System',
354  9 => 'CP/M',
355  10 => 'Windows NTFS',
356  11 => 'MVS',
357  12 => 'VSE',
358  13 => 'Acorn Risc',
359  14 => 'VFAT',
360  15 => 'Alternate MVS',
361  16 => 'BeOS',
362  17 => 'Tandem'
363  );
364 
365  return (isset($ZIPversionOSLookup[$index]) ? $ZIPversionOSLookup[$index] : '[unknown]');
366  }
367 
368  function ZIPcompressionMethodLookup($index) {
369  static $ZIPcompressionMethodLookup = array(
370  0 => 'store',
371  1 => 'shrink',
372  2 => 'reduce-1',
373  3 => 'reduce-2',
374  4 => 'reduce-3',
375  5 => 'reduce-4',
376  6 => 'implode',
377  7 => 'tokenize',
378  8 => 'deflate',
379  9 => 'deflate64',
380  10 => 'PKWARE Date Compression Library Imploding'
381  );
382 
383  return (isset($ZIPcompressionMethodLookup[$index]) ? $ZIPcompressionMethodLookup[$index] : '[unknown]');
384  }
385 
386  function DOStime2UNIXtime($DOSdate, $DOStime) {
387  // wFatDate
388  // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
389  // Bits Contents
390  // 0-4 Day of the month (1-31)
391  // 5-8 Month (1 = January, 2 = February, and so on)
392  // 9-15 Year offset from 1980 (add 1980 to get actual year)
393 
394  $UNIXday = ($DOSdate & 0x001F);
395  $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
396  $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
397 
398  // wFatTime
399  // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
400  // Bits Contents
401  // 0-4 Second divided by 2
402  // 5-10 Minute (0-59)
403  // 11-15 Hour (0-23 on a 24-hour clock)
404 
405  $UNIXsecond = ($DOStime & 0x001F) * 2;
406  $UNIXminute = (($DOStime & 0x07E0) >> 5);
407  $UNIXhour = (($DOStime & 0xF800) >> 11);
408 
409  return gmmktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
410  }
411 
412 }
413 
414 
415 ?>