Add link to wiki explaining configuration options.
[phpmyadmin/crack.git] / libraries / unzip.lib.php
blobdf3264f495209278db6aa5ea4d1d829c53c1e9ad
1 <?PHP
2 /* $Id$ */
4 /**
5 * ZIP file unpack classes. Contributed to the phpMyAdmin project.
7 * @category phpPublic
8 * @package File-Formats-ZIP
9 * @subpackage Unzip
10 * @filesource unzip.lib.php
11 * @version 1.0.1
13 * @author Holger Boskugel <vbwebprofi@gmx.de>
14 * @copyright Copyright © 2003, Holger Boskugel, Berlin, Germany
15 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
17 * @history
18 * 2003-12-02 - HB : Patched : naming bug : Time/Size of file
19 * Added : ZIP file comment
20 * Added : Check BZIP2 support of PHP
21 * 2003-11-29 - HB * Initial version
24 /**
25 * Unzip class, which retrieves entries from ZIP files.
27 * Supports only the compression modes
28 * - 0 : Stored,
29 * - 8 : Deflated and
30 * - 12 : BZIP2
32 * Based on :<BR>
33 * <BR>
34 * {@link http://www.pkware.com/products/enterprise/white_papers/appnote.html
35 * * Official ZIP file format}<BR>
36 * {@link http://msdn.microsoft.com/library/en-us/w98ddk/hh/w98ddk/storage_5l4m.asp
37 * * Microsoft DOS date/time format}
39 * @category phpPublic
40 * @package File-Formats-ZIP
41 * @subpackage Unzip
42 * @version 1.0.1
43 * @author Holger Boskugel <vbwebprofi@gmx.de>
44 * @uses SimpleUnzipEntry
45 * @example example.unzip.php Two examples
47 class SimpleUnzip {
48 // 2003-12-02 - HB >
49 /**
50 * Array to store file entries
52 * @var string
53 * @access public
54 * @see ReadFile()
55 * @since 1.0.1
57 var $Comment = '';
58 // 2003-12-02 - HB <
60 /**
61 * Array to store file entries
63 * @var array
64 * @access public
65 * @see ReadFile()
66 * @since 1.0
68 var $Entries = array();
70 /**
71 * Name of the ZIP file
73 * @var string
74 * @access public
75 * @see ReadFile()
76 * @since 1.0
78 var $Name = '';
80 /**
81 * Size of the ZIP file
83 * @var integer
84 * @access public
85 * @see ReadFile()
86 * @since 1.0
88 var $Size = 0;
90 /**
91 * Time of the ZIP file (unix timestamp)
93 * @var integer
94 * @access public
95 * @see ReadFile()
96 * @since 1.0
98 var $Time = 0;
101 * Contructor of the class
103 * @param string File name
104 * @return SimpleUnzip Instanced class
105 * @access public
106 * @uses SimpleUnzip::ReadFile() Opens file on new if specified
107 * @since 1.0
109 function SimpleUnzip($in_FileName = '')
111 if ($in_FileName !== '') {
112 SimpleUnzip::ReadFile($in_FileName);
114 } // end of the 'SimpleUnzip' constructor
117 * Counts the entries
119 * @return integer Count of ZIP entries
120 * @access public
121 * @uses $Entries
122 * @since 1.0
124 function Count()
126 return count($this->Entries);
127 } // end of the 'Count()' method
130 * Gets data of the specified ZIP entry
132 * @param integer Index of the ZIP entry
133 * @return mixed Data for the ZIP entry
134 * @uses SimpleUnzipEntry::$Data
135 * @access public
136 * @since 1.0
138 function GetData($in_Index)
140 return $this->Entries[$in_Index]->Data;
141 } // end of the 'GetData()' method
144 * Gets an entry of the ZIP file
146 * @param integer Index of the ZIP entry
147 * @return SimpleUnzipEntry Entry of the ZIP file
148 * @uses $Entries
149 * @access public
150 * @since 1.0
152 function GetEntry($in_Index)
154 return $this->Entries[$in_Index];
155 } // end of the 'GetEntry()' method
158 * Gets error code for the specified ZIP entry
160 * @param integer Index of the ZIP entry
161 * @return integer Error code for the ZIP entry
162 * @uses SimpleUnzipEntry::$Error
163 * @access public
164 * @since 1.0
166 function GetError($in_Index)
168 return $this->Entries[$in_Index]->Error;
169 } // end of the 'GetError()' method
172 * Gets error message for the specified ZIP entry
174 * @param integer Index of the ZIP entry
175 * @return string Error message for the ZIP entry
176 * @uses SimpleUnzipEntry::$ErrorMsg
177 * @access public
178 * @since 1.0
180 function GetErrorMsg($in_Index)
182 return $this->Entries[$in_Index]->ErrorMsg;
183 } // end of the 'GetErrorMsg()' method
186 * Gets file name for the specified ZIP entry
188 * @param integer Index of the ZIP entry
189 * @return string File name for the ZIP entry
190 * @uses SimpleUnzipEntry::$Name
191 * @access public
192 * @since 1.0
194 function GetName($in_Index)
196 return $this->Entries[$in_Index]->Name;
197 } // end of the 'GetName()' method
200 * Gets path of the file for the specified ZIP entry
202 * @param integer Index of the ZIP entry
203 * @return string Path of the file for the ZIP entry
204 * @uses SimpleUnzipEntry::$Path
205 * @access public
206 * @since 1.0
208 function GetPath($in_Index)
210 return $this->Entries[$in_Index]->Path;
211 } // end of the 'GetPath()' method
214 * Gets file time for the specified ZIP entry
216 * @param integer Index of the ZIP entry
217 * @return integer File time for the ZIP entry (unix timestamp)
218 * @uses SimpleUnzipEntry::$Time
219 * @access public
220 * @since 1.0
222 function GetTime($in_Index)
224 return $this->Entries[$in_Index]->Time;
225 } // end of the 'GetTime()' method
228 * Reads ZIP file and extracts the entries
230 * @param string File name of the ZIP archive
231 * @return array ZIP entry list (see also class variable {@link $Entries $Entries})
232 * @uses SimpleUnzipEntry For the entries
233 * @access public
234 * @since 1.0
236 function ReadFile($in_FileName)
238 $this->Entries = array();
240 // Get file parameters
241 $this->Name = $in_FileName;
242 $this->Time = filemtime($in_FileName);
243 $this->Size = filesize($in_FileName);
245 // Read file
246 $oF = fopen($in_FileName, 'rb');
247 $vZ = fread($oF, $this->Size);
248 fclose($oF);
250 // 2003-12-02 - HB >
251 // Cut end of central directory
252 $aE = explode("\x50\x4b\x05\x06", $vZ);
254 // Easiest way, but not sure if format changes
255 //$this->Comment = substr($aE[1], 18);
257 // Normal way
258 $aP = unpack('x16/v1CL', $aE[1]);
259 $this->Comment = substr($aE[1], 18, $aP['CL']);
261 // Translates end of line from other operating systems
262 $this->Comment = strtr($this->Comment, array("\r\n" => "\n",
263 "\r" => "\n"));
264 // 2003-12-02 - HB <
266 // Cut the entries from the central directory
267 $aE = explode("\x50\x4b\x01\x02", $vZ);
268 // Explode to each part
269 $aE = explode("\x50\x4b\x03\x04", $aE[0]);
270 // Shift out spanning signature or empty entry
271 array_shift($aE);
273 // Loop through the entries
274 foreach ($aE as $vZ) {
275 $aI = array();
276 $aI['E'] = 0;
277 $aI['EM'] = '';
278 // Retrieving local file header information
279 $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
280 // Check if data is encrypted
281 $bE = ($aP['GPF'] && 0x0001) ? TRUE : FALSE;
282 $nF = $aP['FNL'];
284 // Special case : value block after the compressed data
285 if ($aP['GPF'] & 0x0008) {
286 $aP1 = unpack('V1CRC/V1CS/V1UCS', substr($vZ, -12));
288 $aP['CRC'] = $aP1['CRC'];
289 $aP['CS'] = $aP1['CS'];
290 $aP['UCS'] = $aP1['UCS'];
292 $vZ = substr($vZ, 0, -12);
295 // Getting stored filename
296 $aI['N'] = substr($vZ, 26, $nF);
298 if (substr($aI['N'], -1) == '/') {
299 // is a directory entry - will be skipped
300 continue;
303 // Truncate full filename in path and filename
304 $aI['P'] = dirname($aI['N']);
305 $aI['P'] = $aI['P'] == '.' ? '' : $aI['P'];
306 $aI['N'] = basename($aI['N']);
308 $vZ = substr($vZ, 26 + $nF);
310 if (strlen($vZ) != $aP['CS']) {
311 $aI['E'] = 1;
312 $aI['EM'] = 'Compressed size is not equal with the value in header information.';
313 } else {
314 if ($bE) {
315 $aI['E'] = 5;
316 $aI['EM'] = 'File is encrypted, which is not supported from this class.';
317 } else {
318 switch($aP['CM']) {
319 case 0: // Stored
320 // Here is nothing to do, the file ist flat.
321 break;
323 case 8: // Deflated
324 $vZ = gzinflate($vZ);
325 break;
327 case 12: // BZIP2
328 // 2003-12-02 - HB >
329 if (! extension_loaded('bz2')) {
330 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
331 @dl('php_bz2.dll');
332 } else {
333 @dl('bz2.so');
337 if (extension_loaded('bz2')) {
338 // 2003-12-02 - HB <
339 $vZ = bzdecompress($vZ);
340 // 2003-12-02 - HB >
341 } else {
342 $aI['E'] = 7;
343 $aI['EM'] = "PHP BZIP2 extension not available.";
345 // 2003-12-02 - HB <
347 break;
349 default:
350 $aI['E'] = 6;
351 $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
354 // 2003-12-02 - HB >
355 if (! $aI['E']) {
356 // 2003-12-02 - HB <
357 if ($vZ === FALSE) {
358 $aI['E'] = 2;
359 $aI['EM'] = 'Decompression of data failed.';
360 } else {
361 if (strlen($vZ) != $aP['UCS']) {
362 $aI['E'] = 3;
363 $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
364 } else {
365 if (crc32($vZ) != $aP['CRC']) {
366 $aI['E'] = 4;
367 $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
371 // 2003-12-02 - HB >
373 // 2003-12-02 - HB <
377 $aI['D'] = $vZ;
379 // DOS to UNIX timestamp
380 $aI['T'] = mktime(($aP['FT'] & 0xf800) >> 11,
381 ($aP['FT'] & 0x07e0) >> 5,
382 ($aP['FT'] & 0x001f) << 1,
383 ($aP['FD'] & 0x01e0) >> 5,
384 ($aP['FD'] & 0x001f),
385 (($aP['FD'] & 0xfe00) >> 9) + 1980);
387 $this->Entries[] = &new SimpleUnzipEntry($aI);
388 } // end for each entries
390 return $this->Entries;
391 } // end of the 'ReadFile()' method
392 } // end of the 'SimpleUnzip' class
395 * Entry of the ZIP file.
397 * @category phpPublic
398 * @package File-Formats-ZIP
399 * @subpackage Unzip
400 * @version 1.0
401 * @author Holger Boskugel <vbwebprofi@gmx.de>
402 * @example example.unzip.php Two examples
404 class SimpleUnzipEntry {
406 * Data of the file entry
408 * @var mixed
409 * @access public
410 * @see SimpleUnzipEntry()
411 * @since 1.0
413 var $Data = '';
416 * Error of the file entry
418 * - 0 : No error raised.<BR>
419 * - 1 : Compressed size is not equal with the value in header information.<BR>
420 * - 2 : Decompression of data failed.<BR>
421 * - 3 : Uncompressed size is not equal with the value in header information.<BR>
422 * - 4 : CRC32 checksum is not equal with the value in header information.<BR>
423 * - 5 : File is encrypted, which is not supported from this class.<BR>
424 * - 6 : De-/Compression method ... is not supported.<BR>
425 * - 7 : PHP BZIP2 extension not available.
427 * @var integer
428 * @access public
429 * @see SimpleUnzipEntry()
430 * @since 1.0
432 var $Error = 0;
435 * Error message of the file entry
437 * @var string
438 * @access public
439 * @see SimpleUnzipEntry()
440 * @since 1.0
442 var $ErrorMsg = '';
445 * File name of the file entry
447 * @var string
448 * @access public
449 * @see SimpleUnzipEntry()
450 * @since 1.0
452 var $Name = '';
455 * File path of the file entry
457 * @var string
458 * @access public
459 * @see SimpleUnzipEntry()
460 * @since 1.0
462 var $Path = '';
465 * File time of the file entry (unix timestamp)
467 * @var integer
468 * @access public
469 * @see SimpleUnzipEntry()
470 * @since 1.0
472 var $Time = 0;
475 * Contructor of the class
477 * @param array Entry datas
478 * @return SimpleUnzipEntry Instanced class
479 * @access public
480 * @since 1.0
482 function SimpleUnzipEntry($in_Entry)
484 $this->Data = $in_Entry['D'];
485 $this->Error = $in_Entry['E'];
486 $this->ErrorMsg = $in_Entry['EM'];
487 $this->Name = $in_Entry['N'];
488 $this->Path = $in_Entry['P'];
489 $this->Time = $in_Entry['T'];
490 } // end of the 'SimpleUnzipEntry' constructor
491 } // end of the 'SimpleUnzipEntry' class