Subversion Repositories personal-webbase

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?
  2.  
  3. if (!defined('WBLEGAL')) die('Kann nicht ohne Personal WebBase ausgef&uuml;hrt werden.');
  4.  
  5. // 15/07/2006 (2.6)
  6. // - Changed the algorithm to parse the ZIP file.. Now, the script will try to mount the compressed
  7. //       list, searching on the 'Central Dir' records. If it fails, the script will try to search by
  8. //       checking every signature. Thanks to Jayson Cruz for pointing it.
  9. // 25/01/2006 (2.51)
  10. // - Fixed bug when calling 'unzip' without calling 'getList' first. Thanks to Bala Murthu for pointing it.
  11. // 01/12/2006 (2.5)
  12. // - Added optional parameter "applyChmod" for the "unzip()" method. It auto applies the given chmod for
  13. //       extracted files.
  14. // - Permission 777 (all read-write-exec) is default. If you want to change it, you'll need to make it
  15. //       explicit. (If you want the OS to determine, set "false" as "applyChmod" parameter)
  16. // 28/11/2005 (2.4)
  17. // - dUnzip2 is now compliant with old-style "Data Description", made by some compressors,
  18. //       like the classes ZipLib and ZipLib2 by 'Hasin Hayder'. Thanks to Ricardo Parreno for pointing it.
  19. // 09/11/2005 (2.3)
  20. // - Added optional parameter '$stopOnFile' on method 'getList()'.
  21. //       If given, file listing will stop when find given filename. (Useful to open and unzip an exact file)
  22. // 06/11/2005 (2.21)
  23. // - Added support to PK00 file format (Packed to Removable Disk) (thanks to Lito [PHPfileNavigator])
  24. // - Method 'getExtraInfo': If requested file doesn't exist, return FALSE instead of Array()
  25. // 31/10/2005 (2.2)
  26. // - Removed redundant 'file_name' on centralDirs declaration (thanks to Lito [PHPfileNavigator])
  27. // - Fixed redeclaration of file_put_contents when in PHP4 (not returning true)
  28.  
  29. ##############################################################
  30. # Class dUnzip2 v2.6
  31. #
  32. #       Author: Alexandre Tedeschi (d)
  33. #       E-Mail: alexandrebr at gmail dot com
  34. #       Londrina - PR / Brazil
  35. #
  36. #       Objective:
  37. #               This class allows programmer to easily unzip files on the fly.
  38. #
  39. #       Requirements:
  40. #               This class requires extension ZLib Enabled. It is default
  41. #               for most site hosts around the world, and for the PHP Win32 dist.
  42. #
  43. #       To do:
  44. #        * Error handling
  45. #        * Write a PHP-Side gzinflate, to completely avoid any external extensions
  46. #        * Write other decompress algorithms
  47. #
  48. #       If you modify this class, or have any ideas to improve it, please contact me!
  49. #       You are allowed to redistribute this class, if you keep my name and contact e-mail on it.
  50. #
  51. #       PLEASE! IF YOU USE THIS CLASS IN ANY OF YOUR PROJECTS, PLEASE LET ME KNOW!
  52. #       If you have problems using it, don't think twice before contacting me!
  53. #
  54. ##############################################################
  55.  
  56. // Personal WebBase Zusatz für Kompatibilität mit Safe-Mode-Restriktion
  57.  
  58. function my_mkdir($dr)
  59. {
  60.         global $conn_id;
  61.         global $configuration;
  62.         global $uid;
  63.         global $modul;
  64.         $dr2 = substr($dr, strlen('modules/'.$modul.'/temp/'.$uid), strlen($dr)-strlen('modules/'.$modul.'/temp/'.$uid));
  65.         @ftp_mkdir($conn_id, $configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2);
  66.         @ftp_site($conn_id, 'CHMOD 0755 '.$configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2);
  67. }
  68.  
  69. function my_chmod($dr)
  70. {
  71.         global $conn_id;
  72.         global $configuration;
  73.         global $uid;
  74.         global $modul;
  75.         $dr2 = substr($dr, strlen('modules/'.$modul.'/temp/'.$uid), strlen($dr)-strlen('modules/'.$modul.'/temp/'.$uid));
  76.         if (is_dir($dr))
  77.                 @ftp_site($conn_id, 'CHMOD 0755 '.$configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2);
  78.         else
  79.                 @ftp_site($conn_id, 'CHMOD 0644 '.$configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2);
  80. }
  81.  
  82. function pre_create_file($dr)
  83. {
  84.         global $conn_id;
  85.         global $configuration;
  86.         global $uid;
  87.         global $modul;
  88.         $dr2 = substr($dr, strlen('modules/'.$modul.'/temp/'.$uid), strlen($dr)-strlen('modules/'.$modul.'/temp/'.$uid));
  89.         @ftp_put($conn_id, $configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2, 'modules/'.$modul.'/other/null-file.txt', FTP_ASCII);
  90.         @ftp_site($conn_id, 'CHMOD 0777 '.$configuration['common_directftp']['ftp-verzeichnis'].'modules/'.$modul.'/temp/'.$uid.$dr2);
  91. }
  92.  
  93. function file_put_contents_precreate($file, $data)
  94. {
  95.                 pre_create_file($file);
  96.                 if($tmp = fopen($file, "w")){
  97.                                 fwrite($tmp, $data);
  98.                                 fclose($tmp);
  99.                                 return true;
  100.                 }
  101.                 echo "<b>file_put_contents_precreate:</b> Cannot create file $file<br>";
  102.                 return false;
  103. }
  104.  
  105. // Ende - Personal WebBase Zusatz
  106.  
  107. class dUnzip2{
  108.                 Function getVersion(){
  109.                                 return "2.6";
  110.                 }
  111.                 // Public
  112.                 var $fileName;
  113.                 var $compressedList; // You will problably use only this one!
  114.                 var $centralDirList; // Central dir list... It's a kind of 'extra attributes' for a set of files
  115.                 var $endOfCentral;       // End of central dir, contains ZIP Comments
  116.                 var $debug;
  117.  
  118.                 // Private
  119.                 var $fh;
  120.                 var $zipSignature = "\x50\x4b\x03\x04"; // local file header signature
  121.                 var $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature
  122.                 var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature
  123.  
  124.                 // Public
  125.                 Function dUnzip2($fileName){
  126.                                 $this->fileName                  = $fileName;
  127.                                 $this->compressedList =
  128.                                 $this->centralDirList =
  129.                                 $this->endOfCentral      = Array();
  130.                 }
  131.  
  132.                 Function getList($stopOnFile=false){
  133.                                 if(sizeof($this->compressedList)){
  134.                                                 $this->debugMsg(1, "Returning already loaded file list.");
  135.                                                 return $this->compressedList;
  136.                                 }
  137.  
  138.                                 // Open file, and set file handler
  139.                                 $fh = fopen($this->fileName, "r");
  140.                                 $this->fh = &$fh;
  141.                                 if(!$fh){
  142.                                                 $this->debugMsg(2, "Failed to load file.");
  143.                                                 return false;
  144.                                 }
  145.  
  146.                                 $this->debugMsg(1, "Loading list from 'End of Central Dir' index list...");
  147.                                 if(!$this->_loadFileListByEOF($fh, $stopOnFile)){
  148.                                                 $this->debugMsg(1, "Failed! Trying to load list looking for signatures...");
  149.                                                 if(!$this->_loadFileListBySignatures($fh, $stopOnFile)){
  150.                                                                 $this->debugMsg(1, "Failed! Could not find any valid header.");
  151.                                                                 $this->debugMsg(2, "ZIP File is corrupted or empty");
  152.                                                                 return false;
  153.                                                 }
  154.                                 }
  155.  
  156.                                 if($this->debug){
  157.                                                 #------- Debug compressedList
  158.                                                 $kkk = 0;
  159.                                                 echo "<table border='0' style='font: 11px Verdana; border: 1px solid #000'>";
  160.                                                 foreach($this->compressedList as $fileName=>$item){
  161.                                                                 if(!$kkk && $kkk=1){
  162.                                                                                 echo "<tr style='background: #ADA'>";
  163.                                                                                 foreach($item as $fieldName=>$value)
  164.                                                                                                 echo "<td>$fieldName</td>";
  165.                                                                                 echo '</tr>';
  166.                                                                 }
  167.                                                                 echo "<tr style='background: #CFC'>";
  168.                                                                 foreach($item as $fieldName=>$value){
  169.                                                                                 if($fieldName == 'lastmod_datetime')
  170.                                                                                                 echo "<td title='$fieldName' nowrap='nowrap'>".date("d/m/Y H:i:s", $value)."</td>";
  171.                                                                                 else
  172.                                                                                                 echo "<td title='$fieldName' nowrap='nowrap'>$value</td>";
  173.                                                                 }
  174.                                                                 echo "</tr>";
  175.                                                 }
  176.                                                 echo "</table>";
  177.  
  178.                                                 #------- Debug centralDirList
  179.                                                 $kkk = 0;
  180.                                                 if(sizeof($this->centralDirList)){
  181.                                                                 echo "<table border='0' style='font: 11px Verdana; border: 1px solid #000'>";
  182.                                                                 foreach($this->centralDirList as $fileName=>$item){
  183.                                                                                 if(!$kkk && $kkk=1){
  184.                                                                                                 echo "<tr style='background: #AAD'>";
  185.                                                                                                 foreach($item as $fieldName=>$value)
  186.                                                                                                                 echo "<td>$fieldName</td>";
  187.                                                                                                 echo '</tr>';
  188.                                                                                 }
  189.                                                                                 echo "<tr style='background: #CCF'>";
  190.                                                                                 foreach($item as $fieldName=>$value){
  191.                                                                                                 if($fieldName == 'lastmod_datetime')
  192.                                                                                                                 echo "<td title='$fieldName' nowrap='nowrap'>".date("d/m/Y H:i:s", $value)."</td>";
  193.                                                                                                 else
  194.                                                                                                                 echo "<td title='$fieldName' nowrap='nowrap'>$value</td>";
  195.                                                                                 }
  196.                                                                                 echo "</tr>";
  197.                                                                 }
  198.                                                                 echo "</table>";
  199.                                                 }
  200.  
  201.                                                 #------- Debug endOfCentral
  202.                                                 $kkk = 0;
  203.                                                 if(sizeof($this->endOfCentral)){
  204.                                                                 echo "<table border='0' style='font: 11px Verdana' style='border: 1px solid #000'>";
  205.                                                                 echo "<tr style='background: #DAA'><td colspan='2'>dUnzip - End of file</td></tr>";
  206.                                                                 foreach($this->endOfCentral as $field=>$value){
  207.                                                                                 echo "<tr>";
  208.                                                                                 echo "<td style='background: #FCC'>$field</td>";
  209.                                                                                 echo "<td style='background: #FDD'>$value</td>";
  210.                                                                                 echo "</tr>";
  211.                                                                 }
  212.                                                                 echo "</table>";
  213.                                                 }
  214.                                 }
  215.  
  216.                                 return $this->compressedList;
  217.                 }
  218.                 Function getExtraInfo($compressedFileName){
  219.                                 return
  220.                                                 isset($this->centralDirList[$compressedFileName])?
  221.                                                 $this->centralDirList[$compressedFileName]:
  222.                                                 false;
  223.                 }
  224.                 Function getZipInfo($detail=false){
  225.                                 return $detail?
  226.                                                 $this->endOfCentral[$detail]:
  227.                                                 $this->endOfCentral;
  228.                 }
  229.  
  230.                 Function unzip($compressedFileName, $targetFileName=false){
  231.                                 if(!sizeof($this->compressedList)){
  232.                                                 $this->debugMsg(1, "Trying to unzip before loading file list... Loading it!");
  233.                                                 $this->getList(false, $compressedFileName);
  234.                                 }
  235.  
  236.                                 $fdetails = &$this->compressedList[$compressedFileName];
  237.                                 if(!isset($this->compressedList[$compressedFileName])){
  238.                                                 $this->debugMsg(2, "File '<b>$compressedFileName</b>' is not compressed in the zip.");
  239.                                                 return false;
  240.                                 }
  241.                                 if(substr($compressedFileName, -1) == "/"){
  242.                                                 $this->debugMsg(2, "Trying to unzip a folder name '<b>$compressedFileName</b>'.");
  243.                                                 return false;
  244.                                 }
  245.                                 if(!$fdetails['uncompressed_size']){
  246.                                                 $this->debugMsg(1, "File '<b>$compressedFileName</b>' is empty.");
  247.                                                 // Verändert für Personal WebBase
  248.                                                 if ($targetFileName)
  249.                                                 {
  250.                                                         $x = file_put_contents_precreate($targetFileName, "");
  251.                                                         my_chmod($targetFileName);
  252.                                                         return $x;
  253.                                                 }
  254.                                                 else
  255.                                                 {
  256.                                                         return "";
  257.                                                 }
  258.                                 }
  259.  
  260.                                 fseek($this->fh, $fdetails['contents-startOffset']);
  261.                                 $ret = $this->uncompress(
  262.                                                                 fread($this->fh, $fdetails['compressed_size']),
  263.                                                                 $fdetails['compression_method'],
  264.                                                                 $fdetails['uncompressed_size'],
  265.                                                                 $targetFileName
  266.                                                 );
  267.                                 //if($applyChmod && $targetFileName)
  268.                                                 my_chmod($targetFileName);
  269.  
  270.                                 return $ret;
  271.                 }
  272.                 Function unzipAll($targetDir=false, $baseDir="", $maintainStructure=true){
  273.                                 if($targetDir === false)
  274.                                                 $targetDir = dirname(__FILE__)."/";
  275.  
  276.                                 $lista = $this->getList();
  277.                                 if(sizeof($lista)) foreach($lista as $fileName=>$trash){
  278.                                                 $dirname        = dirname($fileName);
  279.                                                 $outDN          = "$targetDir/$dirname";
  280.  
  281.                                                 if(substr($dirname, 0, strlen($baseDir)) != $baseDir)
  282.                                                                 continue;
  283.  
  284.                                                 if(!is_dir($outDN) && $maintainStructure){
  285.                                                                 $str = "";
  286.                                                                 $folders = explode("/", $dirname);
  287.                                                                 foreach($folders as $folder){
  288.                                                                                 $str = $str?"$str/$folder":$folder;
  289.                                                                                 if(!is_dir("$targetDir/$str")){
  290.                                                                                                 $this->debugMsg(1, "Creating folder: $targetDir/$str");
  291.                                                                                                 my_mkdir("$targetDir/$str");
  292.                                                                                                 //if($applyChmod)
  293.                                                                                                                 my_chmod("$targetDir/$str");
  294.                                                                                 }
  295.                                                                 }
  296.                                                 }
  297.                                                 if(substr($fileName, -1, 1) == "/")
  298.                                                                 continue;
  299.  
  300.                                                 $maintainStructure?
  301.                                                                 $this->unzip($fileName, "$targetDir/$fileName"):
  302.                                                                 $this->unzip($fileName, "$targetDir/".basename($fileName));
  303.                                 }
  304.                 }
  305.  
  306.                 Function close(){                // Free the file resource
  307.                                 if($this->fh)
  308.                                                 fclose($this->fh);
  309.                 }
  310.                 Function __destroy(){
  311.                                 $this->close();
  312.                 }
  313.  
  314.                 // Private (you should NOT call these methods):
  315.                 Function uncompress($content, $mode, $uncompressedSize, $targetFileName=false){
  316.                                 switch($mode){
  317.                                                 case 0:
  318.                                                                 // Not compressed
  319.                                                                 return $targetFileName?
  320.                                                                                 file_put_contents_precreate($targetFileName, $content):
  321.                                                                                 $content;
  322.                                                 case 1:
  323.                                                                 $this->debugMsg(2, "Shrunk mode is not supported... yet?");
  324.                                                                 return false;
  325.                                                 case 2:
  326.                                                 case 3:
  327.                                                 case 4:
  328.                                                 case 5:
  329.                                                                 $this->debugMsg(2, "Compression factor ".($mode-1)." is not supported... yet?");
  330.                                                                 return false;
  331.                                                 case 6:
  332.                                                                 $this->debugMsg(2, "Implode is not supported... yet?");
  333.                                                                 return false;
  334.                                                 case 7:
  335.                                                                 $this->debugMsg(2, "Tokenizing compression algorithm is not supported... yet?");
  336.                                                                 return false;
  337.                                                 case 8:
  338.                                                                 // Deflate
  339.                                                                 return $targetFileName?
  340.                                                                                 file_put_contents_precreate($targetFileName, gzinflate($content, $uncompressedSize)):
  341.                                                                                 gzinflate($content, $uncompressedSize);
  342.                                                 case 9:
  343.                                                                 $this->debugMsg(2, "Enhanced Deflating is not supported... yet?");
  344.                                                                 return false;
  345.                                                 case 10:
  346.                                                                 $this->debugMsg(2, "PKWARE Date Compression Library Impoloding is not supported... yet?");
  347.                                                                 return false;
  348.                                          case 12:
  349.                                                          // Bzip2
  350.                                                          return $targetFileName?
  351.                                                                          file_put_contents_precreate($targetFileName, bzdecompress($content)):
  352.                                                                          bzdecompress($content);
  353.                                                 case 18:
  354.                                                                 $this->debugMsg(2, "IBM TERSE is not supported... yet?");
  355.                                                                 return false;
  356.                                                 default:
  357.                                                                 $this->debugMsg(2, "Unknown uncompress method: $mode");
  358.                                                                 return false;
  359.                                 }
  360.                 }
  361.                 Function debugMsg($level, $string){
  362.                                 if($this->debug)
  363.                                                 if($level == 1)
  364.                                                                 echo "<b style='color: #777'>dUnzip2:</b> $string<br>";
  365.                                                 if($level == 2)
  366.                                                                 echo "<b style='color: #F00'>dUnzip2:</b> $string<br>";
  367.                 }
  368.  
  369.                 Function _loadFileListByEOF(&$fh, $stopOnFile=false){
  370.                                 // Check if there's a valid Central Dir signature.
  371.                                 // Let's consider a file comment smaller than 1024 characters...
  372.                                 // Actually, it length can be 65536.. But we're not going to support it.
  373.  
  374.                                 for($x = 0; $x < 1024; $x++){
  375.                                                 fseek($fh, -22-$x, SEEK_END);
  376.  
  377.                                                 $signature = fread($fh, 4);
  378.                                                 if($signature == $this->dirSignatureE){
  379.                                                                 // If found EOF Central Dir
  380.                                                                 $eodir['disk_number_this']       = unpack("v", fread($fh, 2)); // number of this disk
  381.                                                                 $eodir['disk_number']                           = unpack("v", fread($fh, 2)); // number of the disk with the start of the central directory
  382.                                                                 $eodir['total_entries_this'] = unpack("v", fread($fh, 2)); // total number of entries in the central dir on this disk
  383.                                                                 $eodir['total_entries']                 = unpack("v", fread($fh, 2)); // total number of entries in
  384.                                                                 $eodir['size_of_cd']                             = unpack("V", fread($fh, 4)); // size of the central directory
  385.                                                                 $eodir['offset_start_cd']               = unpack("V", fread($fh, 4)); // offset of start of central directory with respect to the starting disk number
  386.                                                                 $zipFileCommentLenght                           = unpack("v", fread($fh, 2)); // zipfile comment length
  387.                                                                 $eodir['zipfile_comment']               = $zipFileCommentLenght[1]?fread($fh, $zipFileCommentLenght[1]):''; // zipfile comment
  388.                                                                 $this->endOfCentral = Array(
  389.                                                                                 'disk_number_this'=>$eodir['disk_number_this'][1],
  390.                                                                                 'disk_number'=>$eodir['disk_number'][1],
  391.                                                                                 'total_entries_this'=>$eodir['total_entries_this'][1],
  392.                                                                                 'total_entries'=>$eodir['total_entries'][1],
  393.                                                                                 'size_of_cd'=>$eodir['size_of_cd'][1],
  394.                                                                                 'offset_start_cd'=>$eodir['offset_start_cd'][1],
  395.                                                                                 'zipfile_comment'=>$eodir['zipfile_comment'],
  396.                                                                 );
  397.  
  398.                                                                 // Then, load file list
  399.                                                                 fseek($fh, $this->endOfCentral['offset_start_cd']);
  400.                                                                 $signature = fread($fh, 4);
  401.  
  402.                                                                 while($signature == $this->dirSignature){
  403.                                                                                 $dir['version_madeby']                  = unpack("v", fread($fh, 2)); // version made by
  404.                                                                                 $dir['version_needed']                  = unpack("v", fread($fh, 2)); // version needed to extract
  405.                                                                                 $dir['general_bit_flag']                = unpack("v", fread($fh, 2)); // general purpose bit flag
  406.                                                                                 $dir['compression_method']      = unpack("v", fread($fh, 2)); // compression method
  407.                                                                                 $dir['lastmod_time']                            = unpack("v", fread($fh, 2)); // last mod file time
  408.                                                                                 $dir['lastmod_date']                            = unpack("v", fread($fh, 2)); // last mod file date
  409.                                                                                 $dir['crc-32']                                                  = fread($fh, 4);                                                        // crc-32
  410.                                                                                 $dir['compressed_size']          = unpack("V", fread($fh, 4)); // compressed size
  411.                                                                                 $dir['uncompressed_size']        = unpack("V", fread($fh, 4)); // uncompressed size
  412.                                                                                 $fileNameLength                                          = unpack("v", fread($fh, 2)); // filename length
  413.                                                                                 $extraFieldLength                                        = unpack("v", fread($fh, 2)); // extra field length
  414.                                                                                 $fileCommentLength                                      = unpack("v", fread($fh, 2)); // file comment length
  415.                                                                                 $dir['disk_number_start']        = unpack("v", fread($fh, 2)); // disk number start
  416.                                                                                 $dir['internal_attributes'] = unpack("v", fread($fh, 2)); // internal file attributes-byte1
  417.                                                                                 $dir['external_attributes1']= unpack("v", fread($fh, 2)); // external file attributes-byte2
  418.                                                                                 $dir['external_attributes2']= unpack("v", fread($fh, 2)); // external file attributes
  419.                                                                                 $dir['relative_offset']          = unpack("V", fread($fh, 4)); // relative offset of local header
  420.                                                                                 $dir['file_name']                                        = fread($fh, $fileNameLength[1]);                                                                                                               // filename
  421.                                                                                 $dir['extra_field']                              = $extraFieldLength[1] ?fread($fh, $extraFieldLength[1]) :''; // extra field
  422.                                                                                 $dir['file_comment']                            = $fileCommentLength[1]?fread($fh, $fileCommentLength[1]):''; // file comment
  423.  
  424.                                                                                 // Convert the date and time, from MS-DOS format to UNIX Timestamp
  425.                                                                                 $BINlastmod_date = str_pad(decbin($dir['lastmod_date'][1]), 16, '0', STR_PAD_LEFT);
  426.                                                                                 $BINlastmod_time = str_pad(decbin($dir['lastmod_time'][1]), 16, '0', STR_PAD_LEFT);
  427.                                                                                 $lastmod_dateY = bindec(substr($BINlastmod_date,        0, 7))+1980;
  428.                                                                                 $lastmod_dateM = bindec(substr($BINlastmod_date,        7, 4));
  429.                                                                                 $lastmod_dateD = bindec(substr($BINlastmod_date, 11, 5));
  430.                                                                                 $lastmod_timeH = bindec(substr($BINlastmod_time,         0, 5));
  431.                                                                                 $lastmod_timeM = bindec(substr($BINlastmod_time,         5, 6));
  432.                                                                                 $lastmod_timeS = bindec(substr($BINlastmod_time,        11, 5));
  433.  
  434.                                                                                 $this->centralDirList[$dir['file_name']] = Array(
  435.                                                                                                 'version_madeby'=>$dir['version_madeby'][1],
  436.                                                                                                 'version_needed'=>$dir['version_needed'][1],
  437.                                                                                                 'general_bit_flag'=>str_pad(decbin($dir['general_bit_flag'][1]), 8, '0', STR_PAD_LEFT),
  438.                                                                                                 'compression_method'=>$dir['compression_method'][1],
  439.                                                                                                 'lastmod_datetime'      =>mktime($lastmod_timeH, $lastmod_timeM, $lastmod_timeS, $lastmod_dateM, $lastmod_dateD, $lastmod_dateY),
  440.                                                                                                 'crc-32'                                                =>str_pad(dechex(ord($dir['crc-32'][3])), 2, '0', STR_PAD_LEFT).
  441.                                                                                                                                                                                         str_pad(dechex(ord($dir['crc-32'][2])), 2, '0', STR_PAD_LEFT).
  442.                                                                                                                                                                                         str_pad(dechex(ord($dir['crc-32'][1])), 2, '0', STR_PAD_LEFT).
  443.                                                                                                                                                                                         str_pad(dechex(ord($dir['crc-32'][0])), 2, '0', STR_PAD_LEFT),
  444.                                                                                                 'compressed_size'=>$dir['compressed_size'][1],
  445.                                                                                                 'uncompressed_size'=>$dir['uncompressed_size'][1],
  446.                                                                                                 'disk_number_start'=>$dir['disk_number_start'][1],
  447.                                                                                                 'internal_attributes'=>$dir['internal_attributes'][1],
  448.                                                                                                 'external_attributes1'=>$dir['external_attributes1'][1],
  449.                                                                                                 'external_attributes2'=>$dir['external_attributes2'][1],
  450.                                                                                                 'relative_offset'=>$dir['relative_offset'][1],
  451.                                                                                                 'file_name'=>$dir['file_name'],
  452.                                                                                                 'extra_field'=>$dir['extra_field'],
  453.                                                                                                 'file_comment'=>$dir['file_comment'],
  454.                                                                                 );
  455.                                                                                 $signature = fread($fh, 4);
  456.                                                                 }
  457.  
  458.                                                                 // If loaded centralDirs, then try to identify the offsetPosition of the compressed data.
  459.                                                                 if($this->centralDirList) foreach($this->centralDirList as $filename=>$details){
  460.                                                                                 $i = $this->_getFileHeaderInformation($fh, $details['relative_offset']);
  461.                                                                                 $this->compressedList[$filename]['file_name']                                   = $filename;
  462.                                                                                 $this->compressedList[$filename]['compression_method'] = $details['compression_method'];
  463.                                                                                 $this->compressedList[$filename]['version_needed']               = $details['version_needed'];
  464.                                                                                 $this->compressedList[$filename]['lastmod_datetime']     = $details['lastmod_datetime'];
  465.                                                                                 $this->compressedList[$filename]['crc-32']                                               = $details['crc-32'];
  466.                                                                                 $this->compressedList[$filename]['compressed_size']             = $details['compressed_size'];
  467.                                                                                 $this->compressedList[$filename]['uncompressed_size']   = $details['uncompressed_size'];
  468.                                                                                 $this->compressedList[$filename]['lastmod_datetime']     = $details['lastmod_datetime'];
  469.                                                                                 $this->compressedList[$filename]['extra_field']                         = $i['extra_field'];
  470.                                                                                 $this->compressedList[$filename]['contents-startOffset']=$i['contents-startOffset'];
  471.                                                                                 if(strtolower($stopOnFile) == strtolower($filename))
  472.                                                                                                 break;
  473.                                                                 }
  474.                                                                 return true;
  475.                                                 }
  476.                                 }
  477.                                 return false;
  478.                 }
  479.                 Function _loadFileListBySignatures(&$fh, $stopOnFile=false){
  480.                                 fseek($fh, 0);
  481.  
  482.                                 $return = false;
  483.                                 for(;;){
  484.                                                 $details = $this->_getFileHeaderInformation($fh);
  485.                                                 if(!$details){
  486.                                                                 $this->debugMsg(1, "Invalid signature. Trying to verify if is old style Data Descriptor...");
  487.                                                                 fseek($fh, 12 - 4, SEEK_CUR); // 12: Data descriptor - 4: Signature (that will be read again)
  488.                                                                 $details = $this->_getFileHeaderInformation($fh);
  489.                                                 }
  490.                                                 if(!$details){
  491.                                                                 $this->debugMsg(1, "Still invalid signature. Probably reached the end of the file.");
  492.                                                                 break;
  493.                                                 }
  494.                                                 $filename = $details['file_name'];
  495.                                                 $this->compressedList[$filename] = $details;
  496.                                                 $return = true;
  497.                                                 if(strtolower($stopOnFile) == strtolower($filename))
  498.                                                                 break;
  499.                                 }
  500.  
  501.                                 return $return;
  502.                 }
  503.                 Function _getFileHeaderInformation(&$fh, $startOffset=false){
  504.                                 if($startOffset !== false)
  505.                                                 fseek($fh, $startOffset);
  506.  
  507.                                 $signature = fread($fh, 4);
  508.                                 if($signature == $this->zipSignature){
  509.                                                 # $this->debugMsg(1, "Zip Signature!");
  510.  
  511.                                                 // Get information about the zipped file
  512.                                                 $file['version_needed']          = unpack("v", fread($fh, 2)); // version needed to extract
  513.                                                 $file['general_bit_flag']        = unpack("v", fread($fh, 2)); // general purpose bit flag
  514.                                                 $file['compression_method'] = unpack("v", fread($fh, 2)); // compression method
  515.                                                 $file['lastmod_time']                    = unpack("v", fread($fh, 2)); // last mod file time
  516.                                                 $file['lastmod_date']                    = unpack("v", fread($fh, 2)); // last mod file date
  517.                                                 $file['crc-32']                                          = fread($fh, 4);                                                       // crc-32
  518.                                                 $file['compressed_size']                = unpack("V", fread($fh, 4)); // compressed size
  519.                                                 $file['uncompressed_size']      = unpack("V", fread($fh, 4)); // uncompressed size
  520.                                                 $fileNameLength                                          = unpack("v", fread($fh, 2)); // filename length
  521.                                                 $extraFieldLength                                        = unpack("v", fread($fh, 2)); // extra field length
  522.                                                 $file['file_name']                                      = fread($fh, $fileNameLength[1]); // filename
  523.                                                 $file['extra_field']                            = $extraFieldLength[1]?fread($fh, $extraFieldLength[1]):''; // extra field
  524.                                                 $file['contents-startOffset']= ftell($fh);
  525.  
  526.                                                 // Bypass the whole compressed contents, and look for the next file
  527.                                                 fseek($fh, $file['compressed_size'][1], SEEK_CUR);
  528.  
  529.                                                 // Convert the date and time, from MS-DOS format to UNIX Timestamp
  530.                                                 $BINlastmod_date = str_pad(decbin($file['lastmod_date'][1]), 16, '0', STR_PAD_LEFT);
  531.                                                 $BINlastmod_time = str_pad(decbin($file['lastmod_time'][1]), 16, '0', STR_PAD_LEFT);
  532.                                                 $lastmod_dateY = bindec(substr($BINlastmod_date,        0, 7))+1980;
  533.                                                 $lastmod_dateM = bindec(substr($BINlastmod_date,        7, 4));
  534.                                                 $lastmod_dateD = bindec(substr($BINlastmod_date, 11, 5));
  535.                                                 $lastmod_timeH = bindec(substr($BINlastmod_time,         0, 5));
  536.                                                 $lastmod_timeM = bindec(substr($BINlastmod_time,         5, 6));
  537.                                                 $lastmod_timeS = bindec(substr($BINlastmod_time,        11, 5));
  538.  
  539.                                                 // Mount file table
  540.                                                 $i = Array(
  541.                                                                 'file_name'                              =>$file['file_name'],
  542.                                                                 'compression_method'=>$file['compression_method'][1],
  543.                                                                 'version_needed'                =>$file['version_needed'][1],
  544.                                                                 'lastmod_datetime'      =>mktime($lastmod_timeH, $lastmod_timeM, $lastmod_timeS, $lastmod_dateM, $lastmod_dateD, $lastmod_dateY),
  545.                                                                 'crc-32'                                                =>str_pad(dechex(ord($file['crc-32'][3])), 2, '0', STR_PAD_LEFT).
  546.                                                                                                                                                         str_pad(dechex(ord($file['crc-32'][2])), 2, '0', STR_PAD_LEFT).
  547.                                                                                                                                                         str_pad(dechex(ord($file['crc-32'][1])), 2, '0', STR_PAD_LEFT).
  548.                                                                                                                                                         str_pad(dechex(ord($file['crc-32'][0])), 2, '0', STR_PAD_LEFT),
  549.                                                                 'compressed_size'        =>$file['compressed_size'][1],
  550.                                                                 'uncompressed_size' =>$file['uncompressed_size'][1],
  551.                                                                 'extra_field'                    =>$file['extra_field'],
  552.                                                                 'general_bit_flag'      =>str_pad(decbin($file['general_bit_flag'][1]), 8, '0', STR_PAD_LEFT),
  553.                                                                 'contents-startOffset'=>$file['contents-startOffset']
  554.                                                 );
  555.                                                 return $i;
  556.                                 }
  557.                                 return false;
  558.                 }
  559. }
  560.