PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/include/phpzip.php

https://github.com/Bigjoos/U-232-V3
PHP | 211 lines | 173 code | 1 blank | 37 comment | 21 complexity | 86592290e916551031bb712db84763b1 MD5 | raw file
  1. <?php
  2. /**
  3. * https://github.com/Bigjoos/
  4. * Licence Info: GPL
  5. * Copyright (C) 2010 U-232 v.3
  6. * A bittorrent tracker source based on TBDev.net/tbsource/bytemonsoon.
  7. * Project Leaders: Mindless, putyn.
  8. *
  9. */
  10. //
  11. // PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18
  12. // (Changed: 2003-03-01)
  13. //
  14. // Makes zip archive
  15. //
  16. // Based on "Zip file creation class", uses zLib
  17. //
  18. // Examples in sample1.php, sample2.php and sample3.php
  19. //
  20. class PHPZip
  21. {
  22. function Zip($dir, $zipfilename)
  23. {
  24. if (@function_exists('gzcompress')) {
  25. $curdir = getcwd();
  26. if (is_array($dir)) {
  27. $filelist = $dir;
  28. } else {
  29. $filelist = $this->GetFileList($dir);
  30. }
  31. if ((!empty($dir)) && (!is_array($dir)) && (file_exists($dir))) chdir($dir);
  32. else chdir($curdir);
  33. if (count($filelist) > 0) {
  34. foreach ($filelist as $filename) {
  35. if (is_file($filename)) {
  36. $fd = fopen($filename, "r");
  37. $content = fread($fd, filesize($filename));
  38. fclose($fd);
  39. if (is_array($dir)) $filename = basename($filename);
  40. $this->addFile($content, $filename);
  41. }
  42. }
  43. $out = $this->file();
  44. chdir($curdir);
  45. $fp = fopen($zipfilename, "w");
  46. fwrite($fp, $out, strlen($out));
  47. fclose($fp);
  48. }
  49. return 1;
  50. } else return 0;
  51. }
  52. function GetFileList($dir)
  53. {
  54. if (file_exists($dir)) {
  55. $args = func_get_args();
  56. $pref = $args[1];
  57. $dh = opendir($dir);
  58. while ($files = readdir($dh)) {
  59. if (($files != ".") && ($files != "..")) {
  60. if (is_dir($dir.$files)) {
  61. $curdir = getcwd();
  62. chdir($dir.$files);
  63. $file = array_merge($file, $this->GetFileList("", "$pref$files/"));
  64. chdir($curdir);
  65. } else $file[] = $pref.$files;
  66. }
  67. }
  68. closedir($dh);
  69. }
  70. return $file;
  71. }
  72. var $datasec = array();
  73. var $ctrl_dir = array();
  74. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  75. var $old_offset = 0;
  76. /**
  77. * Converts an Unix timestamp to a four byte DOS date and time format (date
  78. * in high two bytes, time in low two bytes allowing magnitude comparison).
  79. *
  80. * @param integer the current Unix timestamp
  81. *
  82. * @return integer the current date in a four byte DOS format
  83. *
  84. * @access private
  85. */
  86. function unix2DosTime($unixtime = 0)
  87. {
  88. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  89. if ($timearray['year'] < 1980) {
  90. $timearray['year'] = 1980;
  91. $timearray['mon'] = 1;
  92. $timearray['mday'] = 1;
  93. $timearray['hours'] = 0;
  94. $timearray['minutes'] = 0;
  95. $timearray['seconds'] = 0;
  96. } // end if
  97. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  98. } // end of the 'unix2DosTime()' method
  99. /**
  100. * Adds "file" to archive
  101. *
  102. * @param string file contents
  103. * @param string name of the file in the archive (may contains the path)
  104. * @param integer the current timestamp
  105. *
  106. * @access public
  107. */
  108. function addFile($data, $name, $time = 0)
  109. {
  110. $name = str_replace('\\', '/', $name);
  111. $dtime = dechex($this->unix2DosTime($time));
  112. $hexdtime = '\x'.$dtime[6].$dtime[7].'\x'.$dtime[4].$dtime[5].'\x'.$dtime[2].$dtime[3].'\x'.$dtime[0].$dtime[1];
  113. eval('$hexdtime = "'.$hexdtime.'";');
  114. $fr = "\x50\x4b\x03\x04";
  115. $fr.= "\x14\x00"; // ver needed to extract
  116. $fr.= "\x00\x00"; // gen purpose bit flag
  117. $fr.= "\x08\x00"; // compression method
  118. $fr.= $hexdtime; // last mod time and date
  119. // "local file header" segment
  120. $unc_len = strlen($data);
  121. $crc = crc32($data);
  122. $zdata = gzcompress($data);
  123. $c_len = strlen($zdata);
  124. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4) , 2); // fix crc bug
  125. $fr.= pack('V', $crc); // crc32
  126. $fr.= pack('V', $c_len); // compressed filesize
  127. $fr.= pack('V', $unc_len); // uncompressed filesize
  128. $fr.= pack('v', strlen($name)); // length of filename
  129. $fr.= pack('v', 0); // extra field length
  130. $fr.= $name;
  131. // "file data" segment
  132. $fr.= $zdata;
  133. // "data descriptor" segment (optional but necessary if archive is not
  134. // served as file)
  135. $fr.= pack('V', $crc); // crc32
  136. $fr.= pack('V', $c_len); // compressed filesize
  137. $fr.= pack('V', $unc_len); // uncompressed filesize
  138. // add this entry to array
  139. $this->datasec[] = $fr;
  140. $new_offset = strlen(implode('', $this->datasec));
  141. // now add to central directory record
  142. $cdrec = "\x50\x4b\x01\x02";
  143. $cdrec.= "\x00\x00"; // version made by
  144. $cdrec.= "\x14\x00"; // version needed to extract
  145. $cdrec.= "\x00\x00"; // gen purpose bit flag
  146. $cdrec.= "\x08\x00"; // compression method
  147. $cdrec.= $hexdtime; // last mod time & date
  148. $cdrec.= pack('V', $crc); // crc32
  149. $cdrec.= pack('V', $c_len); // compressed filesize
  150. $cdrec.= pack('V', $unc_len); // uncompressed filesize
  151. $cdrec.= pack('v', strlen($name)); // length of filename
  152. $cdrec.= pack('v', 0); // extra field length
  153. $cdrec.= pack('v', 0); // file comment length
  154. $cdrec.= pack('v', 0); // disk number start
  155. $cdrec.= pack('v', 0); // internal file attributes
  156. $cdrec.= pack('V', 32); // external file attributes - 'archive' bit set
  157. $cdrec.= pack('V', $this->old_offset); // relative offset of local header
  158. $this->old_offset = $new_offset;
  159. $cdrec.= $name;
  160. // optional extra field, file comment goes here
  161. // save to central directory
  162. $this->ctrl_dir[] = $cdrec;
  163. } // end of the 'addFile()' method
  164. /**
  165. * Dumps out file
  166. *
  167. * @return string the zipped file
  168. *
  169. * @access public
  170. */
  171. function file()
  172. {
  173. $data = implode('', $this->datasec);
  174. $ctrldir = implode('', $this->ctrl_dir);
  175. return $data.$ctrldir.$this->eof_ctrl_dir.pack('v', sizeof($this->ctrl_dir)). // total # of entries "on this disk"
  176. pack('v', sizeof($this->ctrl_dir)). // total # of entries overall
  177. pack('V', strlen($ctrldir)). // size of central dir
  178. pack('V', strlen($data)). // offset to start of central dir
  179. "\x00\x00"; // .zip file comment length
  180. } // end of the 'file()' method
  181. function forceDownload($archiveName)
  182. {
  183. $headerInfo = '';
  184. if (ini_get('zlib.output_compression')) {
  185. ini_set('zlib.output_compression', 'Off');
  186. }
  187. // Security checks
  188. if ($archiveName == "") {
  189. echo "<html><title>Public Photo Directory - Download </title><body><br /><b>ERROR:</b> The download file was NOT SPECIFIED.</body></html>";
  190. exit;
  191. } elseif (!file_exists($archiveName)) {
  192. echo "<html><title>Public Photo Directory - Download </title><body><br /><b>ERROR:</b> File not found.</body></html>";
  193. exit;
  194. }
  195. header("Pragma: public");
  196. header("Expires: 0");
  197. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  198. header("Cache-Control: private", false);
  199. header("Content-Type: application/zip");
  200. header("Content-Encoding: zlib,deflate,gzip");
  201. header("Content-Disposition: attachment; filename=".basename($archiveName).";");
  202. header("Content-Transfer-Encoding: binary");
  203. header("Content-Length: ".filesize($archiveName));
  204. readfile("$archiveName");
  205. }
  206. } // end of the 'PHPZip' class
  207. ?>