Cheetah
unzip.lib.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /* $Id: unzip.lib.php 10142 2007-03-20 10:32:13Z cybot_tm $ */
4 
48  class SimpleUnzip {
49 // 2003-12-02 - HB >
58  var $Comment = '';
59 // 2003-12-02 - HB <
60 
69  var $Entries = array();
70 
79  var $Name = '';
80 
89  var $Size = 0;
90 
99  var $Time = 0;
100 
110  function __construct($in_FileName = '')
111  {
112  if ($in_FileName !== '') {
113  $this->ReadFile($in_FileName);
114  }
115  } // end of the 'SimpleUnzip' constructor
116 
125  function Count()
126  {
127  return count($this->Entries);
128  } // end of the 'Count()' method
129 
139  function GetData($in_Index)
140  {
141  return $this->Entries[$in_Index]->Data;
142  } // end of the 'GetData()' method
143 
153  function GetEntry($in_Index)
154  {
155  return $this->Entries[$in_Index];
156  } // end of the 'GetEntry()' method
157 
167  function GetError($in_Index)
168  {
169  return $this->Entries[$in_Index]->Error;
170  } // end of the 'GetError()' method
171 
181  function GetErrorMsg($in_Index)
182  {
183  return $this->Entries[$in_Index]->ErrorMsg;
184  } // end of the 'GetErrorMsg()' method
185 
195  function GetName($in_Index)
196  {
197  return $this->Entries[$in_Index]->Name;
198  } // end of the 'GetName()' method
199 
209  function GetPath($in_Index)
210  {
211  return $this->Entries[$in_Index]->Path;
212  } // end of the 'GetPath()' method
213 
223  function GetTime($in_Index)
224  {
225  return $this->Entries[$in_Index]->Time;
226  } // end of the 'GetTime()' method
227 
237  function ReadFile($in_FileName)
238  {
239  $this->Entries = array();
240 
241  // Get file parameters
242  $this->Name = $in_FileName;
243  $this->Time = filemtime($in_FileName);
244  $this->Size = filesize($in_FileName);
245 
246  // Read file
247  $oF = fopen($in_FileName, 'rb');
248  $vZ = fread($oF, $this->Size);
249  fclose($oF);
250 
251 // 2003-12-02 - HB >
252  // Cut end of central directory
253  $aE = explode("\x50\x4b\x05\x06", $vZ);
254 
255  // Easiest way, but not sure if format changes
256  //$this->Comment = substr($aE[1], 18);
257 
258  // Normal way
259  $aP = unpack('x16/v1CL', $aE[1]);
260  $this->Comment = substr($aE[1], 18, $aP['CL']);
261 
262  // Translates end of line from other operating systems
263  $this->Comment = strtr($this->Comment, array("\r\n" => "\n",
264  "\r" => "\n"));
265 // 2003-12-02 - HB <
266 
267  // Cut the entries from the central directory
268  $aE = explode("\x50\x4b\x01\x02", $vZ);
269  // Explode to each part
270  $aE = explode("\x50\x4b\x03\x04", $aE[0]);
271  // Shift out spanning signature or empty entry
272  array_shift($aE);
273 
274  // Loop through the entries
275  foreach ($aE as $vZ) {
276  $aI = array();
277  $aI['E'] = 0;
278  $aI['EM'] = '';
279  // Retrieving local file header information
280  $aP = unpack('v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ);
281  // Check if data is encrypted
282  $bE = ($aP['GPF'] & 0x0001) ? TRUE : FALSE;
283  $nF = $aP['FNL'];
284 
285  // Special case : value block after the compressed data
286  if ($aP['GPF'] & 0x0008) {
287  $aP1 = unpack('V1CRC/V1CS/V1UCS', substr($vZ, -12));
288 
289  $aP['CRC'] = $aP1['CRC'];
290  $aP['CS'] = $aP1['CS'];
291  $aP['UCS'] = $aP1['UCS'];
292 
293  $vZ = substr($vZ, 0, -12);
294  }
295 
296  // Getting stored filename
297  $aI['N'] = substr($vZ, 26, $nF);
298 
299  if (substr($aI['N'], -1) == '/') {
300  // is a directory entry - will be skipped
301  continue;
302  }
303 
304  // Truncate full filename in path and filename
305  $aI['P'] = dirname($aI['N']);
306  $aI['P'] = $aI['P'] == '.' ? '' : $aI['P'];
307  $aI['N'] = basename($aI['N']);
308 
309  $vZ = substr($vZ, 26 + $nF);
310 
311  if (strlen($vZ) != $aP['CS']) {
312  $aI['E'] = 1;
313  $aI['EM'] = 'Compressed size is not equal with the value in header information.';
314  } else {
315  if ($bE) {
316  $aI['E'] = 5;
317  $aI['EM'] = 'File is encrypted, which is not supported from this class.';
318  } else {
319  switch($aP['CM']) {
320  case 0: // Stored
321  // Here is nothing to do, the file ist flat.
322  break;
323 
324  case 8: // Deflated
325  $vZ = gzinflate($vZ);
326  break;
327 
328  case 12: // BZIP2
329 // 2003-12-02 - HB >
330  if (! extension_loaded('bz2')) {
331  if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
332  @dl('php_bz2.dll');
333  } else {
334  @dl('bz2.so');
335  }
336  }
337 
338  if (extension_loaded('bz2')) {
339 // 2003-12-02 - HB <
340  $vZ = bzdecompress($vZ);
341 // 2003-12-02 - HB >
342  } else {
343  $aI['E'] = 7;
344  $aI['EM'] = "PHP BZIP2 extension not available.";
345  }
346 // 2003-12-02 - HB <
347 
348  break;
349 
350  default:
351  $aI['E'] = 6;
352  $aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
353  }
354 
355 // 2003-12-02 - HB >
356  if (! $aI['E']) {
357 // 2003-12-02 - HB <
358  if ($vZ === FALSE) {
359  $aI['E'] = 2;
360  $aI['EM'] = 'Decompression of data failed.';
361  } else {
362  if (strlen($vZ) != $aP['UCS']) {
363  $aI['E'] = 3;
364  $aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
365  } else {
366  if (crc32($vZ) != $aP['CRC']) {
367  $aI['E'] = 4;
368  $aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
369  }
370  }
371  }
372 // 2003-12-02 - HB >
373  }
374 // 2003-12-02 - HB <
375  }
376  }
377 
378  $aI['D'] = $vZ;
379 
380  // DOS to UNIX timestamp
381  $aI['T'] = mktime(($aP['FT'] & 0xf800) >> 11,
382  ($aP['FT'] & 0x07e0) >> 5,
383  ($aP['FT'] & 0x001f) << 1,
384  ($aP['FD'] & 0x01e0) >> 5,
385  ($aP['FD'] & 0x001f),
386  (($aP['FD'] & 0xfe00) >> 9) + 1980);
387 
388  $this->Entries[] = new SimpleUnzipEntry($aI);
389  } // end for each entries
390 
391  return $this->Entries;
392  } // end of the 'ReadFile()' method
393  } // end of the 'SimpleUnzip' class
394 
414  var $Data = '';
415 
433  var $Error = 0;
434 
443  var $ErrorMsg = '';
444 
453  var $Name = '';
454 
463  var $Path = '';
464 
473  var $Time = 0;
474 
483  function __construct($in_Entry)
484  {
485  $this->Data = $in_Entry['D'];
486  $this->Error = $in_Entry['E'];
487  $this->ErrorMsg = $in_Entry['EM'];
488  $this->Name = $in_Entry['N'];
489  $this->Path = $in_Entry['P'];
490  $this->Time = $in_Entry['T'];
491  } // end of the 'SimpleUnzipEntry' constructor
492  } // end of the 'SimpleUnzipEntry' class
493 ?>
TRUE
URI MungeSecretKey $secret_key</pre >< p > If the output is TRUE
Definition: URI.MungeSecretKey.txt:17
SimpleUnzipEntry\$ErrorMsg
$ErrorMsg
Definition: unzip.lib.php:443
SimpleUnzip\$Comment
$Comment
Definition: unzip.lib.php:58
SimpleUnzip\GetError
GetError($in_Index)
Definition: unzip.lib.php:167
php
SimpleUnzip\__construct
__construct($in_FileName='')
Definition: unzip.lib.php:110
SimpleUnzip\GetErrorMsg
GetErrorMsg($in_Index)
Definition: unzip.lib.php:181
SimpleUnzip\$Name
$Name
Definition: unzip.lib.php:79
SimpleUnzipEntry
Definition: unzip.lib.php:405
SimpleUnzipEntry\__construct
__construct($in_Entry)
Definition: unzip.lib.php:483
SimpleUnzip\$Size
$Size
Definition: unzip.lib.php:89
SimpleUnzipEntry\$Path
$Path
Definition: unzip.lib.php:463
SimpleUnzip\GetTime
GetTime($in_Index)
Definition: unzip.lib.php:223
SimpleUnzip\GetEntry
GetEntry($in_Index)
Definition: unzip.lib.php:153
SimpleUnzip\GetPath
GetPath($in_Index)
Definition: unzip.lib.php:209
SimpleUnzip\Count
Count()
Definition: unzip.lib.php:125
SimpleUnzipEntry\$Error
$Error
Definition: unzip.lib.php:433
SimpleUnzip
Definition: unzip.lib.php:48
SimpleUnzip\ReadFile
ReadFile($in_FileName)
Definition: unzip.lib.php:237
SimpleUnzipEntry\$Name
$Name
Definition: unzip.lib.php:453
SimpleUnzip\GetName
GetName($in_Index)
Definition: unzip.lib.php:195
SimpleUnzip\$Entries
$Entries
Definition: unzip.lib.php:69
SimpleUnzipEntry\$Time
$Time
Definition: unzip.lib.php:473
SimpleUnzip\$Time
$Time
Definition: unzip.lib.php:99
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
SimpleUnzipEntry\$Data
$Data
Definition: unzip.lib.php:414
SimpleUnzip\GetData
GetData($in_Index)
Definition: unzip.lib.php:139