PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_archives.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 796 lines | 255 code | 23 blank | 518 comment | 44 complexity | 10d25243f18bd07ded9e8b0ea6ea3515 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /*--------------------------------------------------
  11. * | TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.0
  12. * | By Devin Doucette
  13. * | Copyright (c) 2004 Devin Doucette
  14. * | Email: darksnoopy@shaw.ca
  15. * +--------------------------------------------------
  16. * | Email bugs/suggestions to darksnoopy@shaw.ca
  17. * +--------------------------------------------------
  18. * | This script has been created and released under
  19. * | the GNU GPL and is free to use and redistribute
  20. * | only if this copyright statement is not removed
  21. * +--------------------------------------------------*/
  22. if (!defined('IN_ICYPHOENIX'))
  23. {
  24. die('Hacking attempt');
  25. }
  26. class archive
  27. {
  28. function __construct($name)
  29. {
  30. $this->archive_init($name);
  31. }
  32. function archive_init($name)
  33. {
  34. $this->options = array(
  35. 'basedir' => ".",
  36. 'name' => $name,
  37. 'prepend' => "",
  38. 'inmemory' => 0,
  39. 'overwrite' => 0,
  40. 'recurse' => 1,
  41. 'storepaths' => 1,
  42. 'level' => 3,
  43. 'method' => 1,
  44. 'sfx' => "",
  45. 'type' => "",
  46. 'comment' => ""
  47. );
  48. $this->files = array();
  49. $this->exclude = array();
  50. $this->storeonly = array();
  51. $this->error = array();
  52. }
  53. function set_options($options)
  54. {
  55. foreach($options as $key => $value)
  56. {
  57. $this->options[$key] = $value;
  58. }
  59. if(!empty($this->options['basedir']))
  60. {
  61. $this->options['basedir'] = str_replace("\\","/",$this->options['basedir']);
  62. $this->options['basedir'] = preg_replace("/\/+/","/",$this->options['basedir']);
  63. $this->options['basedir'] = preg_replace("/\/$/","",$this->options['basedir']);
  64. }
  65. if(!empty($this->options['name']))
  66. {
  67. $this->options['name'] = str_replace("\\","/",$this->options['name']);
  68. $this->options['name'] = preg_replace("/\/+/","/",$this->options['name']);
  69. }
  70. if(!empty($this->options['prepend']))
  71. {
  72. $this->options['prepend'] = str_replace("\\","/",$this->options['prepend']);
  73. $this->options['prepend'] = preg_replace("/^(\.*\/+)+/","",$this->options['prepend']);
  74. $this->options['prepend'] = preg_replace("/\/+/","/",$this->options['prepend']);
  75. $this->options['prepend'] = preg_replace("/\/$/","",$this->options['prepend']) . "/";
  76. }
  77. }
  78. function create_archive()
  79. {
  80. $this->make_list();
  81. if($this->options['inmemory'] == 0)
  82. {
  83. $pwd = getcwd();
  84. chdir($this->options['basedir']);
  85. if($this->options['overwrite'] == 0 && file_exists($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : "")))
  86. {
  87. $this->error[] = "File {$this->options['name']} already exists.";
  88. chdir($pwd);
  89. return 0;
  90. }
  91. elseif($this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+"))
  92. {
  93. chdir($pwd);
  94. }
  95. else
  96. {
  97. $this->error[] = "Could not open {$this->options['name']} for writing.";
  98. chdir($pwd);
  99. return 0;
  100. }
  101. }
  102. else
  103. {
  104. $this->archive = "";
  105. }
  106. switch($this->options['type'])
  107. {
  108. case "zip":
  109. if(!$this->create_zip())
  110. {
  111. $this->error[] = "Could not create zip file.";
  112. return 0;
  113. }
  114. break;
  115. case "bzip":
  116. if(!$this->create_tar())
  117. {
  118. $this->error[] = "Could not create tar file.";
  119. return 0;
  120. }
  121. if(!$this->create_bzip())
  122. {
  123. $this->error[] = "Could not create bzip2 file.";
  124. return 0;
  125. }
  126. break;
  127. case "gzip":
  128. if(!$this->create_tar())
  129. {
  130. $this->error[] = "Could not create tar file.";
  131. return 0;
  132. }
  133. if(!$this->create_gzip())
  134. {
  135. $this->error[] = "Could not create gzip file.";
  136. return 0;
  137. }
  138. break;
  139. case "tar":
  140. if(!$this->create_tar())
  141. {
  142. $this->error[] = "Could not create tar file.";
  143. return 0;
  144. }
  145. }
  146. if($this->options['inmemory'] == 0)
  147. {
  148. fclose($this->archive);
  149. if($this->options['type'] == "gzip" || $this->options['type'] == "bzip")
  150. {
  151. unlink($this->options['basedir'] . "/" . $this->options['name'] . ".tmp");
  152. }
  153. }
  154. }
  155. function add_data($data)
  156. {
  157. if($this->options['inmemory'] == 0)
  158. {
  159. fwrite($this->archive,$data);
  160. }
  161. else
  162. {
  163. $this->archive .= $data;
  164. }
  165. }
  166. function make_list()
  167. {
  168. if(!empty($this->exclude))
  169. {
  170. foreach($this->files as $key => $value)
  171. {
  172. foreach($this->exclude as $current)
  173. {
  174. if($value['name'] == $current['name'])
  175. {
  176. unset($this->files[$key]);
  177. }
  178. }
  179. }
  180. }
  181. if(!empty($this->storeonly))
  182. {
  183. foreach($this->files as $key => $value)
  184. {
  185. foreach($this->storeonly as $current)
  186. {
  187. if($value['name'] == $current['name'])
  188. {
  189. $this->files[$key]['method'] = 0;
  190. }
  191. }
  192. }
  193. }
  194. unset($this->exclude,$this->storeonly);
  195. }
  196. function add_files($list)
  197. {
  198. $temp = $this->list_files($list);
  199. foreach($temp as $current)
  200. {
  201. $this->files[] = $current;
  202. }
  203. }
  204. function exclude_files($list)
  205. {
  206. $temp = $this->list_files($list);
  207. foreach($temp as $current)
  208. {
  209. $this->exclude[] = $current;
  210. }
  211. }
  212. function store_files($list)
  213. {
  214. $temp = $this->list_files($list);
  215. foreach($temp as $current)
  216. {
  217. $this->storeonly[] = $current;
  218. }
  219. }
  220. function list_files($list)
  221. {
  222. if(!is_array($list))
  223. {
  224. $temp = $list;
  225. $list = array($temp);
  226. unset($temp);
  227. }
  228. $files = array();
  229. $pwd = getcwd();
  230. chdir($this->options['basedir']);
  231. foreach($list as $current)
  232. {
  233. $current = str_replace("\\","/",$current);
  234. $current = preg_replace("/\/+/","/",$current);
  235. $current = preg_replace("/\/$/","",$current);
  236. if(strstr($current,"*"))
  237. {
  238. $regex = preg_replace("/([\\\^\$\.\[\]\|\(\)\?\+\{\}\/])/","\\\\\\1",$current);
  239. $regex = str_replace("*",".*",$regex);
  240. $dir = strstr($current,"/")? substr($current,0,strrpos($current,"/")) : ".";
  241. $temp = $this->parse_dir($dir);
  242. foreach($temp as $current2)
  243. {
  244. if(preg_match("/^{$regex}$/i",$current2['name']))
  245. {
  246. $files[] = $current2;
  247. }
  248. }
  249. unset($regex,$dir,$temp,$current);
  250. }
  251. elseif(@is_dir($current))
  252. {
  253. $temp = $this->parse_dir($current);
  254. foreach($temp as $file)
  255. {
  256. $files[] = $file;
  257. }
  258. unset($temp,$file);
  259. }
  260. elseif(@file_exists($current))
  261. {
  262. $files[] = array('name'=>$current,'name2'=>$this->options['prepend'] .
  263. preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($current,"/"))?
  264. substr($current,strrpos($current,"/") + 1) : $current),'type'=>0,
  265. 'ext'=>substr($current,strrpos($current,".")),'stat'=>stat($current));
  266. }
  267. }
  268. chdir($pwd);
  269. unset($current,$pwd);
  270. usort($files,array("archive","sort_files"));
  271. return $files;
  272. }
  273. function parse_dir($dirname)
  274. {
  275. if($this->options['storepaths'] == 1 && !preg_match("/^(\.+\/*)+$/",$dirname))
  276. {
  277. $files = array(array('name'=>$dirname,'name2'=>$this->options['prepend'] .
  278. preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($dirname,"/"))?
  279. substr($dirname,strrpos($dirname,"/") + 1) : $dirname),'type'=>5,'stat'=>stat($dirname)));
  280. }
  281. else
  282. {
  283. $files = array();
  284. }
  285. $dir = @opendir($dirname);
  286. while($file = @readdir($dir))
  287. {
  288. if(($file == ".") || ($file == ".."))
  289. {
  290. continue;
  291. }
  292. elseif(@is_dir($dirname . "/" . $file))
  293. {
  294. if(empty($this->options['recurse']))
  295. {
  296. continue;
  297. }
  298. $temp = $this->parse_dir($dirname . "/" . $file);
  299. foreach($temp as $file2)
  300. {
  301. $files[] = $file2;
  302. }
  303. }
  304. elseif(@file_exists($dirname . "/" . $file))
  305. {
  306. $files[] = array('name' => $dirname . "/" . $file, 'name2' => $this->options['prepend'] . preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($dirname . "/" . $file, "/")) ? substr($dirname . "/" . $file, strrpos($dirname . "/" . $file, "/") + 1) : $dirname . "/" . $file), 'type' => 0, 'ext' => substr($file , strrpos($file, ".")), 'stat' => stat($dirname . "/" . $file));
  307. }
  308. }
  309. @closedir($dir);
  310. return $files;
  311. }
  312. function sort_files($a, $b)
  313. {
  314. if($a['type'] != $b['type'])
  315. {
  316. return $a['type'] > $b['type']? -1 : 1;
  317. }
  318. elseif($a['type'] == 5)
  319. {
  320. return strcmp(strtolower($a['name']),strtolower($b['name']));
  321. }
  322. else
  323. {
  324. if($a['ext'] != $b['ext'])
  325. {
  326. return strcmp($a['ext'],$b['ext']);
  327. }
  328. elseif($a['stat'][7] != $b['stat'][7])
  329. {
  330. return $a['stat'][7] > $b['stat'][7]? -1 : 1;
  331. }
  332. else
  333. {
  334. return strcmp(strtolower($a['name']),strtolower($b['name']));
  335. }
  336. }
  337. return 0;
  338. }
  339. function download_file()
  340. {
  341. if($this->options['inmemory'] == 0)
  342. {
  343. $this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
  344. return;
  345. }
  346. switch($this->options['type'])
  347. {
  348. case "zip":
  349. header("Content-type:application/zip");
  350. break;
  351. case "bzip":
  352. header("Content-type:application/x-compressed");
  353. break;
  354. case "gzip":
  355. header("Content-type:application/x-compressed");
  356. break;
  357. case "tar":
  358. header("Content-type:application/x-tar");
  359. }
  360. $header = "Content-disposition: attachment; filename=\"";
  361. $header .= strstr($this->options['name'],"/")? substr($this->options['name'],strrpos($this->options['name'],"/") + 1) : $this->options['name'];
  362. $header .= "\"";
  363. header($header);
  364. header("Content-length: " . strlen($this->archive));
  365. header("Content-transfer-encoding: binary");
  366. header("Pragma: no-cache");
  367. header("Expires: 0");
  368. print($this->archive);
  369. }
  370. }
  371. class tar_file extends archive
  372. {
  373. function __construct($name)
  374. {
  375. $this->archive($name);
  376. $this->options['type'] = "tar";
  377. }
  378. function create_tar()
  379. {
  380. $pwd = getcwd();
  381. chdir($this->options['basedir']);
  382. foreach($this->files as $current)
  383. {
  384. if($current['name'] == $this->options['name'])
  385. {
  386. continue;
  387. }
  388. if(strlen($current['name2']) > 99)
  389. {
  390. $path = substr($current['name2'],0,strpos($current['name2'],"/",strlen($current['name2']) - 100) + 1);
  391. $current['name2'] = substr($current['name2'],strlen($path));
  392. if(strlen($path) > 154 || strlen($current['name2']) > 99)
  393. {
  394. $this->error[] = "Could not add {$path}{$current['name2']} to archive because the filename is too long.";
  395. continue;
  396. }
  397. }
  398. $block = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",$current['name2'],decoct($current['stat'][2]),
  399. sprintf("%6s ",decoct($current['stat'][4])),sprintf("%6s ",decoct($current['stat'][5])),
  400. sprintf("%11s ",decoct($current['stat'][7])),sprintf("%11s ",decoct($current['stat'][9])),
  401. " ",$current['type'],"","ustar","00","Unknown","Unknown","","",!empty($path)? $path : "","");
  402. $checksum = 0;
  403. for($i = 0; $i < 512; $i++)
  404. {
  405. $checksum += ord(substr($block,$i,1));
  406. }
  407. $checksum = pack("a8",sprintf("%6s ",decoct($checksum)));
  408. $block = substr_replace($block,$checksum,148,8);
  409. if($current['stat'][7] == 0)
  410. {
  411. $this->add_data($block);
  412. }
  413. elseif($fp = @fopen($current['name'],"rb"))
  414. {
  415. $this->add_data($block);
  416. while($temp = fread($fp,1048576))
  417. {
  418. $this->add_data($temp);
  419. }
  420. if($current['stat'][7] % 512 > 0)
  421. {
  422. $temp = "";
  423. for($i = 0; $i < 512 - $current['stat'][7] % 512; $i++)
  424. {
  425. $temp .= "\0";
  426. }
  427. $this->add_data($temp);
  428. }
  429. fclose($fp);
  430. }
  431. else
  432. {
  433. $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
  434. }
  435. }
  436. $this->add_data(pack("a512",""));
  437. chdir($pwd);
  438. return 1;
  439. }
  440. function extract_files()
  441. {
  442. $pwd = getcwd();
  443. chdir($this->options['basedir']);
  444. if($fp = $this->open_archive())
  445. {
  446. if($this->options['inmemory'] == 1)
  447. {
  448. $this->files = array();
  449. }
  450. while($block = fread($fp,512))
  451. {
  452. $temp = unpack("a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100temp/a6magic/a2temp/a32temp/a32temp/a8temp/a8temp/a155prefix/a12temp",$block);
  453. $file = array(
  454. 'name'=>$temp['prefix'] . $temp['name'],
  455. 'stat'=>array(
  456. 2=>$temp['mode'],
  457. 4=>octdec($temp['uid']),
  458. 5=>octdec($temp['gid']),
  459. 7=>octdec($temp['size']),
  460. 9=>octdec($temp['mtime']),
  461. ),
  462. 'checksum'=>octdec($temp['checksum']),
  463. 'type'=>$temp['type'],
  464. 'magic'=>$temp['magic'],
  465. );
  466. if($file['checksum'] == 0x00000000)
  467. {
  468. break;
  469. }
  470. elseif($file['magic'] != "ustar")
  471. {
  472. $this->error[] = "This script does not support extracting this type of tar file.";
  473. break;
  474. }
  475. $block = substr_replace($block," ",148,8);
  476. $checksum = 0;
  477. for($i = 0; $i < 512; $i++)
  478. {
  479. $checksum += ord(substr($block,$i,1));
  480. }
  481. if($file['checksum'] != $checksum)
  482. {
  483. $this->error[] = "Could not extract from {$this->options['name']}, it is corrupt.";
  484. }
  485. if($this->options['inmemory'] == 1)
  486. {
  487. $file['data'] = fread($fp,$file['stat'][7]);
  488. fread($fp,(512 - $file['stat'][7] % 512) == 512? 0 : (512 - $file['stat'][7] % 512));
  489. unset($file['checksum'],$file['magic']);
  490. $this->files[] = $file;
  491. }
  492. else
  493. {
  494. if($file['type'] == 5)
  495. {
  496. if(!is_dir($file['name']))
  497. {
  498. mkdir($file['name'],$file['stat'][2]);
  499. chown($file['name'],$file['stat'][4]);
  500. chgrp($file['name'],$file['stat'][5]);
  501. }
  502. }
  503. elseif($this->options['overwrite'] == 0 && file_exists($file['name']))
  504. {
  505. $this->error[] = "{$file['name']} already exists.";
  506. }
  507. elseif($new = @fopen($file['name'],"wb"))
  508. {
  509. fwrite($new,fread($fp,$file['stat'][7]));
  510. fread($fp,(512 - $file['stat'][7] % 512) == 512? 0 : (512 - $file['stat'][7] % 512));
  511. fclose($new);
  512. chmod($file['name'],$file['stat'][2]);
  513. chown($file['name'],$file['stat'][4]);
  514. chgrp($file['name'],$file['stat'][5]);
  515. }
  516. else
  517. {
  518. $this->error[] = "Could not open {$file['name']} for writing.";
  519. }
  520. }
  521. unset($file);
  522. }
  523. }
  524. else
  525. {
  526. $this->error[] = "Could not open file {$this->options['name']}";
  527. }
  528. chdir($pwd);
  529. }
  530. function open_archive()
  531. {
  532. return @fopen($this->options['name'],"rb");
  533. }
  534. }
  535. class gzip_file extends tar_file
  536. {
  537. function __construct($name)
  538. {
  539. $this->tar_file($name);
  540. $this->options['type'] = "gzip";
  541. }
  542. function create_gzip()
  543. {
  544. if($this->options['inmemory'] == 0)
  545. {
  546. $pwd = getcwd();
  547. chdir($this->options['basedir']);
  548. if($fp = gzopen($this->options['name'],"wb{$this->options['level']}"))
  549. {
  550. fseek($this->archive,0);
  551. while($temp = fread($this->archive,1048576))
  552. {
  553. gzwrite($fp,$temp);
  554. }
  555. gzclose($fp);
  556. chdir($pwd);
  557. }
  558. else
  559. {
  560. $this->error[] = "Could not open {$this->options['name']} for writing.";
  561. chdir($pwd);
  562. return 0;
  563. }
  564. }
  565. else
  566. {
  567. $this->archive = gzencode($this->archive,$this->options['level']);
  568. }
  569. return 1;
  570. }
  571. function open_archive()
  572. {
  573. return @gzopen($this->options['name'],"rb");
  574. }
  575. }
  576. class bzip_file extends tar_file
  577. {
  578. function __construct($name)
  579. {
  580. $this->tar_file($name);
  581. $this->options['type'] = "bzip";
  582. }
  583. function create_bzip()
  584. {
  585. if($this->options['inmemory'] == 0)
  586. {
  587. $pwd = getcwd();
  588. chdir($this->options['basedir']);
  589. if($fp = bzopen($this->options['name'],"wb"))
  590. {
  591. fseek($this->archive,0);
  592. while($temp = fread($this->archive,1048576))
  593. {
  594. bzwrite($fp,$temp);
  595. }
  596. bzclose($fp);
  597. chdir($pwd);
  598. }
  599. else
  600. {
  601. $this->error[] = "Could not open {$this->options['name']} for writing.";
  602. chdir($pwd);
  603. return 0;
  604. }
  605. }
  606. else
  607. {
  608. $this->archive = bzcompress($this->archive,$this->options['level']);
  609. }
  610. return 1;
  611. }
  612. function open_archive()
  613. {
  614. return @bzopen($this->options['name'],"rb");
  615. }
  616. }
  617. class zip_file extends archive
  618. {
  619. function __construct($name)
  620. {
  621. //$this->archive($name);
  622. $this->archive_init($name);
  623. $this->options['type'] = "zip";
  624. }
  625. function create_zip()
  626. {
  627. $files = 0;
  628. $offset = 0;
  629. $central = "";
  630. if(!empty($this->options['sfx']))
  631. {
  632. if($fp = @fopen($this->options['sfx'],"rb"))
  633. {
  634. $temp = fread($fp,filesize($this->options['sfx']));
  635. fclose($fp);
  636. $this->add_data($temp);
  637. $offset += strlen($temp);
  638. unset($temp);
  639. }
  640. else
  641. {
  642. $this->error[] = "Could not open sfx module from {$this->options['sfx']}.";
  643. }
  644. }
  645. $pwd = getcwd();
  646. chdir($this->options['basedir']);
  647. foreach($this->files as $current)
  648. {
  649. if($current['name'] == $this->options['name'])
  650. {
  651. continue;
  652. }
  653. $translate = array('Ç'=>pack("C",128),'ü'=>pack("C",129),'é'=>pack("C",130),'â'=>pack("C",131),'ä'=>pack("C",132),
  654. 'à'=>pack("C",133),'å'=>pack("C",134),'ç'=>pack("C",135),'ê'=>pack("C",136),'ë'=>pack("C",137),
  655. 'è'=>pack("C",138),'ï'=>pack("C",139),'î'=>pack("C",140),'ì'=>pack("C",141),'Ä'=>pack("C",142),
  656. 'Å'=>pack("C",143),'É'=>pack("C",144),'æ'=>pack("C",145),'Æ'=>pack("C",146),'ô'=>pack("C",147),
  657. 'ö'=>pack("C",148),'ò'=>pack("C",149),'û'=>pack("C",150),'ù'=>pack("C",151),'Ö'=>pack("C",153),
  658. 'Ü'=>pack("C",154),'£'=>pack("C",156),'¥'=>pack("C",157),'ƒ'=>pack("C",159),'á'=>pack("C",160),
  659. 'í'=>pack("C",161),'ó'=>pack("C",162),'ú'=>pack("C",163),'ñ'=>pack("C",164),'Ñ'=>pack("C",165));
  660. $current['name2'] = strtr($current['name2'],$translate);
  661. $timedate = explode(" ", gmdate("Y n j G i s", $current['stat'][9]));
  662. $timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) |
  663. ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]);
  664. $block = pack("VvvvV",0x04034b50,0x000A,0x0000,(isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate);
  665. if($current['stat'][7] == 0 && $current['type'] == 5)
  666. {
  667. $block .= pack("VVVvv",0x00000000,0x00000000,0x00000000,strlen($current['name2']) + 1,0x0000);
  668. $block .= $current['name2'] . "/";
  669. $this->add_data($block);
  670. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  671. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  672. 0x00000000,0x00000000,0x00000000,strlen($current['name2']) + 1,0x0000,0x0000,0x0000,0x0000,$current['type'] == 5? 0x00000010 : 0x00000000,$offset);
  673. $central .= $current['name2'] . "/";
  674. $files++;
  675. $offset += (31 + strlen($current['name2']));
  676. }
  677. elseif($current['stat'][7] == 0)
  678. {
  679. $block .= pack("VVVvv",0x00000000,0x00000000,0x00000000,strlen($current['name2']),0x0000);
  680. $block .= $current['name2'];
  681. $this->add_data($block);
  682. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  683. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  684. 0x00000000,0x00000000,0x00000000,strlen($current['name2']),0x0000,0x0000,0x0000,0x0000,$current['type'] == 5? 0x00000010 : 0x00000000,$offset);
  685. $central .= $current['name2'];
  686. $files++;
  687. $offset += (30 + strlen($current['name2']));
  688. }
  689. elseif($fp = @fopen($current['name'],"rb"))
  690. {
  691. $temp = fread($fp,$current['stat'][7]);
  692. fclose($fp);
  693. $crc32 = crc32($temp);
  694. if(!isset($current['method']) && $this->options['method'] == 1)
  695. {
  696. $temp = gzcompress($temp,$this->options['level']);
  697. $size = strlen($temp) - 6;
  698. $temp = substr($temp,2,$size);
  699. }
  700. else
  701. {
  702. $size = strlen($temp);
  703. }
  704. $block .= pack("VVVvv",$crc32,$size,$current['stat'][7],strlen($current['name2']),0x0000);
  705. $block .= $current['name2'];
  706. $this->add_data($block);
  707. $this->add_data($temp);
  708. unset($temp);
  709. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  710. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  711. $crc32,$size,$current['stat'][7],strlen($current['name2']),0x0000,0x0000,0x0000,0x0000,0x00000000,$offset);
  712. $central .= $current['name2'];
  713. $files++;
  714. $offset += (30 + strlen($current['name2']) + $size);
  715. }
  716. else
  717. {
  718. $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
  719. }
  720. }
  721. $this->add_data($central);
  722. $this->add_data(pack("VvvvvVVv",0x06054b50,0x0000,0x0000,$files,$files,strlen($central),$offset,
  723. !empty($this->options['comment'])? strlen($this->options['comment']) : 0x0000));
  724. if(!empty($this->options['comment']))
  725. {
  726. $this->add_data($this->options['comment']);
  727. }
  728. chdir($pwd);
  729. return 1;
  730. }
  731. }
  732. ?>