PageRenderTime 95ms CodeModel.GetById 51ms RepoModel.GetById 2ms app.codeStats 1ms

/administrator/components/com_joomlaxplorer/libraries/Tar.php

https://github.com/DimaSamodurov/erasvit
PHP | 1661 lines | 1245 code | 196 blank | 220 comment | 333 complexity | 406bb8643c5f89ac03f77d38377fc497 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if( !defined( '_JEXEC' ) && !defined( '_VALID_MOS' ) ) die( 'Restricted access' );
  3. /* vim: set ts=4 sw=4: */
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 3.0 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available through the world-wide-web at the following url: |
  12. // | http://www.php.net/license/3_0.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Author: Vincent Blavet <vincent@blavet.net> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id:Tar.php 13 2007-05-13 07:10:43Z soeren $
  21. require_once( dirname(__FILE__) .'/PEAR.php' );
  22. define ('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
  23. /**
  24. * Creates a (compressed) Tar archive
  25. *
  26. * @author Vincent Blavet <vincent@blavet.net>
  27. * @version $Revision:13 $
  28. * @package Archive
  29. */
  30. class Archive_Tar extends PEAR
  31. {
  32. /**
  33. * @var string Name of the Tar
  34. */
  35. var $_tarname='';
  36. /**
  37. * @var boolean if true, the Tar file will be gzipped
  38. */
  39. var $_compress=false;
  40. /**
  41. * @var string Type of compression : 'none', 'gz' or 'bz2'
  42. */
  43. var $_compress_type='none';
  44. /**
  45. * @var string Explode separator
  46. */
  47. var $_separator=' ';
  48. /**
  49. * @var file descriptor
  50. */
  51. var $_file=0;
  52. /**
  53. * @var string Local Tar name of a remote Tar (http:// or ftp://)
  54. */
  55. var $_temp_tarname='';
  56. // {{{ constructor
  57. /**
  58. * Archive_Tar Class constructor. This flavour of the constructor only
  59. * declare a new Archive_Tar object, identifying it by the name of the
  60. * tar file.
  61. * If the compress argument is set the tar will be read or created as a
  62. * gzip or bz2 compressed TAR file.
  63. *
  64. * @param string $p_tarname The name of the tar archive to create
  65. * @param string $p_compress can be null, 'gz' or 'bz2'. This
  66. * parameter indicates if gzip or bz2 compression
  67. * is required. For compatibility reason the
  68. * boolean value 'true' means 'gz'.
  69. * @access public
  70. */
  71. function Archive_Tar($p_tarname, $p_compress = null)
  72. {
  73. $this->PEAR();
  74. $this->_compress = false;
  75. $this->_compress_type = 'none';
  76. if ($p_compress === null) {
  77. if (@file_exists($p_tarname)) {
  78. if ($fp = @fopen($p_tarname, "rb")) {
  79. // look for gzip magic cookie
  80. $data = fread($fp, 2);
  81. fclose($fp);
  82. if ($data == "\37\213") {
  83. $this->_compress = true;
  84. $this->_compress_type = 'gz';
  85. // No sure it's enought for a magic code ....
  86. } elseif ($data == "BZ") {
  87. $this->_compress = true;
  88. $this->_compress_type = 'bz2';
  89. }
  90. }
  91. } else {
  92. // probably a remote file or some file accessible
  93. // through a stream interface
  94. if (substr($p_tarname, -2) == 'gz') {
  95. $this->_compress = true;
  96. $this->_compress_type = 'gz';
  97. } elseif ((substr($p_tarname, -3) == 'bz2') ||
  98. (substr($p_tarname, -2) == 'bz')) {
  99. $this->_compress = true;
  100. $this->_compress_type = 'bz2';
  101. }
  102. }
  103. } else {
  104. if (($p_compress === true) || ($p_compress == 'gz')) {
  105. $this->_compress = true;
  106. $this->_compress_type = 'gz';
  107. } else if ($p_compress == 'bz2') {
  108. $this->_compress = true;
  109. $this->_compress_type = 'bz2';
  110. }
  111. }
  112. $this->_tarname = $p_tarname;
  113. if ($this->_compress) { // assert zlib or bz2 extension support
  114. if ($this->_compress_type == 'gz')
  115. $extname = 'zlib';
  116. else if ($this->_compress_type == 'bz2')
  117. $extname = 'bz2';
  118. if (!extension_loaded($extname)) {
  119. PEAR::loadExtension($extname);
  120. }
  121. if (!extension_loaded($extname)) {
  122. die("The extension '$extname' couldn't be found.\n".
  123. "Please make sure your version of PHP was built ".
  124. "with '$extname' support.\n");
  125. return false;
  126. }
  127. }
  128. }
  129. // }}}
  130. // {{{ destructor
  131. function _Archive_Tar()
  132. {
  133. $this->_close();
  134. // ----- Look for a local copy to delete
  135. if ($this->_temp_tarname != '')
  136. @unlink($this->_temp_tarname);
  137. $this->_PEAR();
  138. }
  139. // }}}
  140. // {{{ create()
  141. /**
  142. * This method creates the archive file and add the files / directories
  143. * that are listed in $p_filelist.
  144. * If a file with the same name exist and is writable, it is replaced
  145. * by the new tar.
  146. * The method return false and a PEAR error text.
  147. * The $p_filelist parameter can be an array of string, each string
  148. * representing a filename or a directory name with their path if
  149. * needed. It can also be a single string with names separated by a
  150. * single blank.
  151. * For each directory added in the archive, the files and
  152. * sub-directories are also added.
  153. * See also createModify() method for more details.
  154. *
  155. * @param array $p_filelist An array of filenames and directory names, or a single
  156. * string with names separated by a single blank space.
  157. * @return true on success, false on error.
  158. * @see createModify()
  159. * @access public
  160. */
  161. function create($p_filelist)
  162. {
  163. return $this->createModify($p_filelist, '', '');
  164. }
  165. // }}}
  166. // {{{ add()
  167. /**
  168. * This method add the files / directories that are listed in $p_filelist in
  169. * the archive. If the archive does not exist it is created.
  170. * The method return false and a PEAR error text.
  171. * The files and directories listed are only added at the end of the archive,
  172. * even if a file with the same name is already archived.
  173. * See also createModify() method for more details.
  174. *
  175. * @param array $p_filelist An array of filenames and directory names, or a single
  176. * string with names separated by a single blank space.
  177. * @return true on success, false on error.
  178. * @see createModify()
  179. * @access public
  180. */
  181. function add($p_filelist)
  182. {
  183. return $this->addModify($p_filelist, '', '');
  184. }
  185. // }}}
  186. // {{{ extract()
  187. function extract($p_path='')
  188. {
  189. return $this->extractModify($p_path, '');
  190. }
  191. // }}}
  192. // {{{ listContent()
  193. function listContent()
  194. {
  195. $v_list_detail = array();
  196. if ($this->_openRead()) {
  197. if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
  198. unset($v_list_detail);
  199. $v_list_detail = 0;
  200. }
  201. $this->_close();
  202. }
  203. return $v_list_detail;
  204. }
  205. // }}}
  206. // {{{ createModify()
  207. /**
  208. * This method creates the archive file and add the files / directories
  209. * that are listed in $p_filelist.
  210. * If the file already exists and is writable, it is replaced by the
  211. * new tar. It is a create and not an add. If the file exists and is
  212. * read-only or is a directory it is not replaced. The method return
  213. * false and a PEAR error text.
  214. * The $p_filelist parameter can be an array of string, each string
  215. * representing a filename or a directory name with their path if
  216. * needed. It can also be a single string with names separated by a
  217. * single blank.
  218. * The path indicated in $p_remove_dir will be removed from the
  219. * memorized path of each file / directory listed when this path
  220. * exists. By default nothing is removed (empty path '')
  221. * The path indicated in $p_add_dir will be added at the beginning of
  222. * the memorized path of each file / directory listed. However it can
  223. * be set to empty ''. The adding of a path is done after the removing
  224. * of path.
  225. * The path add/remove ability enables the user to prepare an archive
  226. * for extraction in a different path than the origin files are.
  227. * See also addModify() method for file adding properties.
  228. *
  229. * @param array $p_filelist An array of filenames and directory names, or a single
  230. * string with names separated by a single blank space.
  231. * @param string $p_add_dir A string which contains a path to be added to the
  232. * memorized path of each element in the list.
  233. * @param string $p_remove_dir A string which contains a path to be removed from
  234. * the memorized path of each element in the list, when
  235. * relevant.
  236. * @return boolean true on success, false on error.
  237. * @access public
  238. * @see addModify()
  239. */
  240. function createModify($p_filelist, $p_add_dir, $p_remove_dir='')
  241. {
  242. $v_result = true;
  243. if (!$this->_openWrite())
  244. return false;
  245. if ($p_filelist != '') {
  246. if (is_array($p_filelist))
  247. $v_list = $p_filelist;
  248. elseif (is_string($p_filelist))
  249. $v_list = explode($this->_separator, $p_filelist);
  250. else {
  251. $this->_cleanFile();
  252. $this->_error('Invalid file list');
  253. return false;
  254. }
  255. $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
  256. }
  257. if ($v_result) {
  258. $this->_writeFooter();
  259. $this->_close();
  260. } else
  261. $this->_cleanFile();
  262. return $v_result;
  263. }
  264. // }}}
  265. // {{{ addModify()
  266. /**
  267. * This method add the files / directories listed in $p_filelist at the
  268. * end of the existing archive. If the archive does not yet exists it
  269. * is created.
  270. * The $p_filelist parameter can be an array of string, each string
  271. * representing a filename or a directory name with their path if
  272. * needed. It can also be a single string with names separated by a
  273. * single blank.
  274. * The path indicated in $p_remove_dir will be removed from the
  275. * memorized path of each file / directory listed when this path
  276. * exists. By default nothing is removed (empty path '')
  277. * The path indicated in $p_add_dir will be added at the beginning of
  278. * the memorized path of each file / directory listed. However it can
  279. * be set to empty ''. The adding of a path is done after the removing
  280. * of path.
  281. * The path add/remove ability enables the user to prepare an archive
  282. * for extraction in a different path than the origin files are.
  283. * If a file/dir is already in the archive it will only be added at the
  284. * end of the archive. There is no update of the existing archived
  285. * file/dir. However while extracting the archive, the last file will
  286. * replace the first one. This results in a none optimization of the
  287. * archive size.
  288. * If a file/dir does not exist the file/dir is ignored. However an
  289. * error text is send to PEAR error.
  290. * If a file/dir is not readable the file/dir is ignored. However an
  291. * error text is send to PEAR error.
  292. *
  293. * @param array $p_filelist An array of filenames and directory names, or a single
  294. * string with names separated by a single blank space.
  295. * @param string $p_add_dir A string which contains a path to be added to the
  296. * memorized path of each element in the list.
  297. * @param string $p_remove_dir A string which contains a path to be removed from
  298. * the memorized path of each element in the list, when
  299. * relevant.
  300. * @return true on success, false on error.
  301. * @access public
  302. */
  303. function addModify($p_filelist, $p_add_dir, $p_remove_dir='')
  304. {
  305. $v_result = true;
  306. if (!@is_file($this->_tarname))
  307. $v_result = $this->createModify($p_filelist, $p_add_dir, $p_remove_dir);
  308. else {
  309. if (is_array($p_filelist))
  310. $v_list = $p_filelist;
  311. elseif (is_string($p_filelist))
  312. $v_list = explode($this->_separator, $p_filelist);
  313. else {
  314. $this->_error('Invalid file list');
  315. return false;
  316. }
  317. $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
  318. }
  319. return $v_result;
  320. }
  321. // }}}
  322. // {{{ addString()
  323. /**
  324. * This method add a single string as a file at the
  325. * end of the existing archive. If the archive does not yet exists it
  326. * is created.
  327. *
  328. * @param string $p_filename A string which contains the full filename path
  329. * that will be associated with the string.
  330. * @param string $p_string The content of the file added in the archive.
  331. * @return true on success, false on error.
  332. * @access public
  333. */
  334. function addString($p_filename, $p_string)
  335. {
  336. $v_result = true;
  337. if (!@is_file($this->_tarname)) {
  338. if (!$this->_openWrite()) {
  339. return false;
  340. }
  341. $this->_close();
  342. }
  343. if (!$this->_openAppend())
  344. return false;
  345. // Need to check the get back to the temporary file ? ....
  346. $v_result = $this->_addString($p_filename, $p_string);
  347. $this->_writeFooter();
  348. $this->_close();
  349. return $v_result;
  350. }
  351. // }}}
  352. // {{{ extractModify()
  353. /**
  354. * This method extract all the content of the archive in the directory
  355. * indicated by $p_path. When relevant the memorized path of the
  356. * files/dir can be modified by removing the $p_remove_path path at the
  357. * beginning of the file/dir path.
  358. * While extracting a file, if the directory path does not exists it is
  359. * created.
  360. * While extracting a file, if the file already exists it is replaced
  361. * without looking for last modification date.
  362. * While extracting a file, if the file already exists and is write
  363. * protected, the extraction is aborted.
  364. * While extracting a file, if a directory with the same name already
  365. * exists, the extraction is aborted.
  366. * While extracting a directory, if a file with the same name already
  367. * exists, the extraction is aborted.
  368. * While extracting a file/directory if the destination directory exist
  369. * and is write protected, or does not exist but can not be created,
  370. * the extraction is aborted.
  371. * If after extraction an extracted file does not show the correct
  372. * stored file size, the extraction is aborted.
  373. * When the extraction is aborted, a PEAR error text is set and false
  374. * is returned. However the result can be a partial extraction that may
  375. * need to be manually cleaned.
  376. *
  377. * @param string $p_path The path of the directory where the files/dir need to by
  378. * extracted.
  379. * @param string $p_remove_path Part of the memorized path that can be removed if
  380. * present at the beginning of the file/dir path.
  381. * @return boolean true on success, false on error.
  382. * @access public
  383. * @see extractList()
  384. */
  385. function extractModify($p_path, $p_remove_path)
  386. {
  387. $v_result = true;
  388. $v_list_detail = array();
  389. if ($v_result = $this->_openRead()) {
  390. $v_result = $this->_extractList($p_path, $v_list_detail, "complete", 0, $p_remove_path);
  391. $this->_close();
  392. }
  393. return $v_result;
  394. }
  395. // }}}
  396. // {{{ extractInString()
  397. /**
  398. * This method extract from the archive one file identified by $p_filename.
  399. * The return value is a string with the file content, or NULL on error.
  400. * @param string $p_filename The path of the file to extract in a string.
  401. * @return a string with the file content or NULL.
  402. * @access public
  403. */
  404. function extractInString($p_filename)
  405. {
  406. if ($this->_openRead()) {
  407. $v_result = $this->_extractInString($p_filename);
  408. $this->_close();
  409. } else {
  410. $v_result = NULL;
  411. }
  412. return $v_result;
  413. }
  414. // }}}
  415. // {{{ extractList()
  416. /**
  417. * This method extract from the archive only the files indicated in the
  418. * $p_filelist. These files are extracted in the current directory or
  419. * in the directory indicated by the optional $p_path parameter.
  420. * If indicated the $p_remove_path can be used in the same way as it is
  421. * used in extractModify() method.
  422. * @param array $p_filelist An array of filenames and directory names, or a single
  423. * string with names separated by a single blank space.
  424. * @param string $p_path The path of the directory where the files/dir need to by
  425. * extracted.
  426. * @param string $p_remove_path Part of the memorized path that can be removed if
  427. * present at the beginning of the file/dir path.
  428. * @return true on success, false on error.
  429. * @access public
  430. * @see extractModify()
  431. */
  432. function extractList($p_filelist, $p_path='', $p_remove_path='')
  433. {
  434. $v_result = true;
  435. $v_list_detail = array();
  436. if (is_array($p_filelist))
  437. $v_list = $p_filelist;
  438. elseif (is_string($p_filelist))
  439. $v_list = explode($this->_separator, $p_filelist);
  440. else {
  441. $this->_error('Invalid string list');
  442. return false;
  443. }
  444. if ($v_result = $this->_openRead()) {
  445. $v_result = $this->_extractList($p_path, $v_list_detail, "partial", $v_list, $p_remove_path);
  446. $this->_close();
  447. }
  448. return $v_result;
  449. }
  450. // }}}
  451. // {{{ setAttribute()
  452. /**
  453. * This method set specific attributes of the archive. It uses a variable
  454. * list of parameters, in the format attribute code + attribute values :
  455. * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
  456. * @param mixed $argv variable list of attributes and values
  457. * @return true on success, false on error.
  458. * @access public
  459. */
  460. function setAttribute()
  461. {
  462. $v_result = true;
  463. // ----- Get the number of variable list of arguments
  464. if (($v_size = func_num_args()) == 0) {
  465. return true;
  466. }
  467. // ----- Get the arguments
  468. $v_att_list = &func_get_args();
  469. // ----- Read the attributes
  470. $i=0;
  471. while ($i<$v_size) {
  472. // ----- Look for next option
  473. switch ($v_att_list[$i]) {
  474. // ----- Look for options that request a string value
  475. case ARCHIVE_TAR_ATT_SEPARATOR :
  476. // ----- Check the number of parameters
  477. if (($i+1) >= $v_size) {
  478. $this->_error('Invalid number of parameters for attribute ARCHIVE_TAR_ATT_SEPARATOR');
  479. return false;
  480. }
  481. // ----- Get the value
  482. $this->_separator = $v_att_list[$i+1];
  483. $i++;
  484. break;
  485. default :
  486. $this->_error('Unknow attribute code '.$v_att_list[$i].'');
  487. return false;
  488. }
  489. // ----- Next attribute
  490. $i++;
  491. }
  492. return $v_result;
  493. }
  494. // }}}
  495. // {{{ _error()
  496. function _error($p_message)
  497. {
  498. // ----- To be completed
  499. $this->raiseError($p_message);
  500. }
  501. // }}}
  502. // {{{ _warning()
  503. function _warning($p_message)
  504. {
  505. // ----- To be completed
  506. $this->raiseError($p_message);
  507. }
  508. // }}}
  509. // {{{ _openWrite()
  510. function _openWrite()
  511. {
  512. if ($this->_compress_type == 'gz')
  513. $this->_file = @gzopen($this->_tarname, "wb");
  514. else if ($this->_compress_type == 'bz2')
  515. $this->_file = @bzopen($this->_tarname, "wb");
  516. else if ($this->_compress_type == 'none')
  517. $this->_file = @fopen($this->_tarname, "wb");
  518. else
  519. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  520. if ($this->_file == 0) {
  521. $this->_error('Unable to open in write mode \''.$this->_tarname.'\'');
  522. return false;
  523. }
  524. return true;
  525. }
  526. // }}}
  527. // {{{ _openRead()
  528. function _openRead()
  529. {
  530. if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
  531. // ----- Look if a local copy need to be done
  532. if ($this->_temp_tarname == '') {
  533. $this->_temp_tarname = uniqid('tar').'.tmp';
  534. if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
  535. $this->_error('Unable to open in read mode \''.$this->_tarname.'\'');
  536. $this->_temp_tarname = '';
  537. return false;
  538. }
  539. if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
  540. $this->_error('Unable to open in write mode \''.$this->_temp_tarname.'\'');
  541. $this->_temp_tarname = '';
  542. return false;
  543. }
  544. while ($v_data = @fread($v_file_from, 1024))
  545. @fwrite($v_file_to, $v_data);
  546. @fclose($v_file_from);
  547. @fclose($v_file_to);
  548. }
  549. // ----- File to open if the local copy
  550. $v_filename = $this->_temp_tarname;
  551. } else
  552. // ----- File to open if the normal Tar file
  553. $v_filename = $this->_tarname;
  554. if ($this->_compress_type == 'gz')
  555. $this->_file = @gzopen($v_filename, "rb");
  556. else if ($this->_compress_type == 'bz2')
  557. $this->_file = @bzopen($v_filename, "rb");
  558. else if ($this->_compress_type == 'none')
  559. $this->_file = @fopen($v_filename, "rb");
  560. else
  561. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  562. if ($this->_file == 0) {
  563. $this->_error('Unable to open in read mode \''.$v_filename.'\'');
  564. return false;
  565. }
  566. return true;
  567. }
  568. // }}}
  569. // {{{ _openReadWrite()
  570. function _openReadWrite()
  571. {
  572. if ($this->_compress_type == 'gz')
  573. $this->_file = @gzopen($this->_tarname, "r+b");
  574. else if ($this->_compress_type == 'bz2')
  575. $this->_file = @bzopen($this->_tarname, "r+b");
  576. else if ($this->_compress_type == 'none')
  577. $this->_file = @fopen($this->_tarname, "r+b");
  578. else
  579. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  580. if ($this->_file == 0) {
  581. $this->_error('Unable to open in read/write mode \''.$this->_tarname.'\'');
  582. return false;
  583. }
  584. return true;
  585. }
  586. // }}}
  587. // {{{ _close()
  588. function _close()
  589. {
  590. //if (isset($this->_file)) {
  591. if (is_resource($this->_file)) {
  592. if ($this->_compress_type == 'gz')
  593. @gzclose($this->_file);
  594. else if ($this->_compress_type == 'bz2')
  595. @bzclose($this->_file);
  596. else if ($this->_compress_type == 'none')
  597. @fclose($this->_file);
  598. else
  599. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  600. $this->_file = 0;
  601. }
  602. // ----- Look if a local copy need to be erase
  603. // Note that it might be interesting to keep the url for a time : ToDo
  604. if ($this->_temp_tarname != '') {
  605. @unlink($this->_temp_tarname);
  606. $this->_temp_tarname = '';
  607. }
  608. return true;
  609. }
  610. // }}}
  611. // {{{ _cleanFile()
  612. function _cleanFile()
  613. {
  614. $this->_close();
  615. // ----- Look for a local copy
  616. if ($this->_temp_tarname != '') {
  617. // ----- Remove the local copy but not the remote tarname
  618. @unlink($this->_temp_tarname);
  619. $this->_temp_tarname = '';
  620. } else {
  621. // ----- Remove the local tarname file
  622. @unlink($this->_tarname);
  623. }
  624. $this->_tarname = '';
  625. return true;
  626. }
  627. // }}}
  628. // {{{ _writeBlock()
  629. function _writeBlock($p_binary_data, $p_len=null)
  630. {
  631. if (is_resource($this->_file)) {
  632. if ($p_len === null) {
  633. if ($this->_compress_type == 'gz')
  634. @gzputs($this->_file, $p_binary_data);
  635. else if ($this->_compress_type == 'bz2')
  636. @bzwrite($this->_file, $p_binary_data);
  637. else if ($this->_compress_type == 'none')
  638. @fputs($this->_file, $p_binary_data);
  639. else
  640. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  641. } else {
  642. if ($this->_compress_type == 'gz')
  643. @gzputs($this->_file, $p_binary_data, $p_len);
  644. else if ($this->_compress_type == 'bz2')
  645. @bzwrite($this->_file, $p_binary_data, $p_len);
  646. else if ($this->_compress_type == 'none')
  647. @fputs($this->_file, $p_binary_data, $p_len);
  648. else
  649. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  650. }
  651. }
  652. return true;
  653. }
  654. // }}}
  655. // {{{ _readBlock()
  656. function _readBlock($p_len=null)
  657. {
  658. $v_block = null;
  659. if (is_resource($this->_file)) {
  660. if ($p_len === null)
  661. $p_len = 512;
  662. if ($this->_compress_type == 'gz')
  663. $v_block = @gzread($this->_file, 512);
  664. else if ($this->_compress_type == 'bz2')
  665. $v_block = @bzread($this->_file, 512);
  666. else if ($this->_compress_type == 'none')
  667. $v_block = @fread($this->_file, 512);
  668. else
  669. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  670. }
  671. return $v_block;
  672. }
  673. // }}}
  674. // {{{ _jumpBlock()
  675. function _jumpBlock($p_len=null)
  676. {
  677. if (is_resource($this->_file)) {
  678. if ($p_len === null)
  679. $p_len = 1;
  680. if ($this->_compress_type == 'gz')
  681. @gzseek($this->_file, @gztell($this->_file)+($p_len*512));
  682. else if ($this->_compress_type == 'bz2') {
  683. // ----- Replace missing bztell() and bzseek()
  684. for ($i=0; $i<$p_len; $i++)
  685. $this->_readBlock();
  686. } else if ($this->_compress_type == 'none')
  687. @fseek($this->_file, @ftell($this->_file)+($p_len*512));
  688. else
  689. $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
  690. }
  691. return true;
  692. }
  693. // }}}
  694. // {{{ _writeFooter()
  695. function _writeFooter()
  696. {
  697. if (is_resource($this->_file)) {
  698. // ----- Write the last 0 filled block for end of archive
  699. $v_binary_data = pack("a512", '');
  700. $this->_writeBlock($v_binary_data);
  701. }
  702. return true;
  703. }
  704. // }}}
  705. // {{{ _addList()
  706. function _addList($p_list, $p_add_dir, $p_remove_dir)
  707. {
  708. $v_result=true;
  709. $v_header = array();
  710. // ----- Remove potential windows directory separator
  711. $p_add_dir = $this->_translateWinPath($p_add_dir);
  712. $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);
  713. if (!$this->_file) {
  714. $this->_error('Invalid file descriptor');
  715. return false;
  716. }
  717. if (sizeof($p_list) == 0)
  718. return true;
  719. for ($j=0; ($j<count($p_list)) && ($v_result); $j++) {
  720. $v_filename = $p_list[$j];
  721. // ----- Skip the current tar name
  722. if ($v_filename == $this->_tarname)
  723. continue;
  724. if ($v_filename == '')
  725. continue;
  726. if (!file_exists($v_filename)) {
  727. $this->_warning("File '$v_filename' does not exist");
  728. continue;
  729. }
  730. // ----- Add the file or directory header
  731. if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir))
  732. return false;
  733. if (@is_dir($v_filename)) {
  734. if (!($p_hdir = opendir($v_filename))) {
  735. $this->_warning("Directory '$v_filename' can not be read");
  736. continue;
  737. }
  738. $p_hitem = readdir($p_hdir); // '.' directory
  739. $p_hitem = readdir($p_hdir); // '..' directory
  740. while (false !== ($p_hitem = readdir($p_hdir))) {
  741. if ($v_filename != ".")
  742. $p_temp_list[0] = $v_filename.'/'.$p_hitem;
  743. else
  744. $p_temp_list[0] = $p_hitem;
  745. $v_result = $this->_addList($p_temp_list, $p_add_dir, $p_remove_dir);
  746. }
  747. unset($p_temp_list);
  748. unset($p_hdir);
  749. unset($p_hitem);
  750. }
  751. }
  752. return $v_result;
  753. }
  754. // }}}
  755. // {{{ _addFile()
  756. function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir)
  757. {
  758. if (!$this->_file) {
  759. $this->_error('Invalid file descriptor');
  760. return false;
  761. }
  762. if ($p_filename == '') {
  763. $this->_error('Invalid file name');
  764. return false;
  765. }
  766. // ----- Calculate the stored filename
  767. $p_filename = $this->_translateWinPath($p_filename, false);;
  768. $v_stored_filename = $p_filename;
  769. if (strcmp($p_filename, $p_remove_dir) == 0) {
  770. return true;
  771. }
  772. if ($p_remove_dir != '') {
  773. if (substr($p_remove_dir, -1) != '/')
  774. $p_remove_dir .= '/';
  775. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  776. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  777. }
  778. $v_stored_filename = $this->_translateWinPath($v_stored_filename);
  779. if ($p_add_dir != '') {
  780. if (substr($p_add_dir, -1) == '/')
  781. $v_stored_filename = $p_add_dir.$v_stored_filename;
  782. else
  783. $v_stored_filename = $p_add_dir.'/'.$v_stored_filename;
  784. }
  785. $v_stored_filename = $this->_pathReduction($v_stored_filename);
  786. if (is_file($p_filename)) {
  787. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  788. $this->_warning("Unable to open file '$p_filename' in binary read mode");
  789. return true;
  790. }
  791. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  792. return false;
  793. while (($v_buffer = fread($v_file, 512)) != '') {
  794. $v_binary_data = pack("a512", "$v_buffer");
  795. $this->_writeBlock($v_binary_data);
  796. }
  797. fclose($v_file);
  798. } else {
  799. // ----- Only header for dir
  800. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  801. return false;
  802. }
  803. return true;
  804. }
  805. // }}}
  806. // {{{ _addString()
  807. function _addString($p_filename, $p_string)
  808. {
  809. if (!$this->_file) {
  810. $this->_error('Invalid file descriptor');
  811. return false;
  812. }
  813. if ($p_filename == '') {
  814. $this->_error('Invalid file name');
  815. return false;
  816. }
  817. // ----- Calculate the stored filename
  818. $p_filename = $this->_translateWinPath($p_filename, false);;
  819. if (!$this->_writeHeaderBlock($p_filename, strlen($p_string), 0, 0, "", 0, 0))
  820. return false;
  821. $i=0;
  822. while (($v_buffer = substr($p_string, (($i++)*512), 512)) != '') {
  823. $v_binary_data = pack("a512", $v_buffer);
  824. $this->_writeBlock($v_binary_data);
  825. }
  826. return true;
  827. }
  828. // }}}
  829. // {{{ _writeHeader()
  830. function _writeHeader($p_filename, $p_stored_filename)
  831. {
  832. if ($p_stored_filename == '')
  833. $p_stored_filename = $p_filename;
  834. $v_reduce_filename = $this->_pathReduction($p_stored_filename);
  835. if (strlen($v_reduce_filename) > 99) {
  836. if (!$this->_writeLongHeader($v_reduce_filename))
  837. return false;
  838. }
  839. $v_info = stat($p_filename);
  840. $v_uid = sprintf("%6s ", DecOct($v_info[4]));
  841. $v_gid = sprintf("%6s ", DecOct($v_info[5]));
  842. $v_perms = sprintf("%6s ", DecOct(fileperms($p_filename)));
  843. $v_mtime = sprintf("%11s", DecOct(filemtime($p_filename)));
  844. if (@is_dir($p_filename)) {
  845. $v_typeflag = "5";
  846. $v_size = sprintf("%11s ", DecOct(0));
  847. } else {
  848. $v_typeflag = '';
  849. clearstatcache();
  850. $v_size = sprintf("%11s ", DecOct(filesize($p_filename)));
  851. }
  852. $v_linkname = '';
  853. $v_magic = '';
  854. $v_version = '';
  855. $v_uname = '';
  856. $v_gname = '';
  857. $v_devmajor = '';
  858. $v_devminor = '';
  859. $v_prefix = '';
  860. $v_binary_data_first = pack("a100a8a8a8a12A12", $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
  861. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
  862. // ----- Calculate the checksum
  863. $v_checksum = 0;
  864. // ..... First part of the header
  865. for ($i=0; $i<148; $i++)
  866. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  867. // ..... Ignore the checksum value and replace it by ' ' (space)
  868. for ($i=148; $i<156; $i++)
  869. $v_checksum += ord(' ');
  870. // ..... Last part of the header
  871. for ($i=156, $j=0; $i<512; $i++, $j++)
  872. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  873. // ----- Write the first 148 bytes of the header in the archive
  874. $this->_writeBlock($v_binary_data_first, 148);
  875. // ----- Write the calculated checksum
  876. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  877. $v_binary_data = pack("a8", $v_checksum);
  878. $this->_writeBlock($v_binary_data, 8);
  879. // ----- Write the last 356 bytes of the header in the archive
  880. $this->_writeBlock($v_binary_data_last, 356);
  881. return true;
  882. }
  883. // }}}
  884. // {{{ _writeHeaderBlock()
  885. function _writeHeaderBlock($p_filename, $p_size, $p_mtime=0, $p_perms=0, $p_type='', $p_uid=0, $p_gid=0)
  886. {
  887. $p_filename = $this->_pathReduction($p_filename);
  888. if (strlen($p_filename) > 99) {
  889. if (!$this->_writeLongHeader($p_filename))
  890. return false;
  891. }
  892. if ($p_type == "5") {
  893. $v_size = sprintf("%11s ", DecOct(0));
  894. } else {
  895. $v_size = sprintf("%11s ", DecOct($p_size));
  896. }
  897. $v_uid = sprintf("%6s ", DecOct($p_uid));
  898. $v_gid = sprintf("%6s ", DecOct($p_gid));
  899. $v_perms = sprintf("%6s ", DecOct($p_perms));
  900. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  901. $v_linkname = '';
  902. $v_magic = '';
  903. $v_version = '';
  904. $v_uname = '';
  905. $v_gname = '';
  906. $v_devmajor = '';
  907. $v_devminor = '';
  908. $v_prefix = '';
  909. $v_binary_data_first = pack("a100a8a8a8a12A12", $p_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
  910. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $p_type, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
  911. // ----- Calculate the checksum
  912. $v_checksum = 0;
  913. // ..... First part of the header
  914. for ($i=0; $i<148; $i++)
  915. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  916. // ..... Ignore the checksum value and replace it by ' ' (space)
  917. for ($i=148; $i<156; $i++)
  918. $v_checksum += ord(' ');
  919. // ..... Last part of the header
  920. for ($i=156, $j=0; $i<512; $i++, $j++)
  921. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  922. // ----- Write the first 148 bytes of the header in the archive
  923. $this->_writeBlock($v_binary_data_first, 148);
  924. // ----- Write the calculated checksum
  925. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  926. $v_binary_data = pack("a8", $v_checksum);
  927. $this->_writeBlock($v_binary_data, 8);
  928. // ----- Write the last 356 bytes of the header in the archive
  929. $this->_writeBlock($v_binary_data_last, 356);
  930. return true;
  931. }
  932. // }}}
  933. // {{{ _writeLongHeader()
  934. function _writeLongHeader($p_filename)
  935. {
  936. $v_size = sprintf("%11s ", DecOct(strlen($p_filename)));
  937. $v_typeflag = 'L';
  938. $v_linkname = '';
  939. $v_magic = '';
  940. $v_version = '';
  941. $v_uname = '';
  942. $v_gname = '';
  943. $v_devmajor = '';
  944. $v_devminor = '';
  945. $v_prefix = '';
  946. $v_binary_data_first = pack("a100a8a8a8a12A12", '././@LongLink', 0, 0, 0, $v_size, 0);
  947. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, '');
  948. // ----- Calculate the checksum
  949. $v_checksum = 0;
  950. // ..... First part of the header
  951. for ($i=0; $i<148; $i++)
  952. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  953. // ..... Ignore the checksum value and replace it by ' ' (space)
  954. for ($i=148; $i<156; $i++)
  955. $v_checksum += ord(' ');
  956. // ..... Last part of the header
  957. for ($i=156, $j=0; $i<512; $i++, $j++)
  958. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  959. // ----- Write the first 148 bytes of the header in the archive
  960. $this->_writeBlock($v_binary_data_first, 148);
  961. // ----- Write the calculated checksum
  962. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  963. $v_binary_data = pack("a8", $v_checksum);
  964. $this->_writeBlock($v_binary_data, 8);
  965. // ----- Write the last 356 bytes of the header in the archive
  966. $this->_writeBlock($v_binary_data_last, 356);
  967. // ----- Write the filename as content of the block
  968. $i=0;
  969. while (($v_buffer = substr($p_filename, (($i++)*512), 512)) != '') {
  970. $v_binary_data = pack("a512", "$v_buffer");
  971. $this->_writeBlock($v_binary_data);
  972. }
  973. return true;
  974. }
  975. // }}}
  976. // {{{ _readHeader()
  977. function _readHeader($v_binary_data, &$v_header)
  978. {
  979. if (strlen($v_binary_data)==0) {
  980. $v_header['filename'] = '';
  981. return true;
  982. }
  983. if (strlen($v_binary_data) != 512) {
  984. $v_header['filename'] = '';
  985. $this->_error('Invalid block size : '.strlen($v_binary_data));
  986. return false;
  987. }
  988. // ----- Calculate the checksum
  989. $v_checksum = 0;
  990. // ..... First part of the header
  991. for ($i=0; $i<148; $i++)
  992. $v_checksum+=ord(substr($v_binary_data,$i,1));
  993. // ..... Ignore the checksum value and replace it by ' ' (space)
  994. for ($i=148; $i<156; $i++)
  995. $v_checksum += ord(' ');
  996. // ..... Last part of the header
  997. for ($i=156; $i<512; $i++)
  998. $v_checksum+=ord(substr($v_binary_data,$i,1));
  999. $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data);
  1000. // ----- Extract the checksum
  1001. $v_header['checksum'] = OctDec(trim($v_data['checksum']));
  1002. if ($v_header['checksum'] != $v_checksum) {
  1003. $v_header['filename'] = '';
  1004. // ----- Look for last block (empty block)
  1005. if (($v_checksum == 256) && ($v_header['checksum'] == 0))
  1006. return true;
  1007. $this->_error('Invalid checksum for file "'.$v_data['filename'].'" : '.$v_checksum.' calculated, '.$v_header['checksum'].' expected');
  1008. return false;
  1009. }
  1010. // ----- Extract the properties
  1011. $v_header['filename'] = trim($v_data['filename']);
  1012. $v_header['mode'] = OctDec(trim($v_data['mode']));
  1013. $v_header['uid'] = OctDec(trim($v_data['uid']));
  1014. $v_header['gid'] = OctDec(trim($v_data['gid']));
  1015. $v_header['size'] = OctDec(trim($v_data['size']));
  1016. $v_header['mtime'] = OctDec(trim($v_data['mtime']));
  1017. if (($v_header['typeflag'] = $v_data['typeflag']) == "5") {
  1018. $v_header['size'] = 0;
  1019. }
  1020. /* ----- All these fields are removed form the header because they do not carry interesting info
  1021. $v_header[link] = trim($v_data[link]);
  1022. $v_header[magic] = trim($v_data[magic]);
  1023. $v_header[version] = trim($v_data[version]);
  1024. $v_header[uname] = trim($v_data[uname]);
  1025. $v_header[gname] = trim($v_data[gname]);
  1026. $v_header[devmajor] = trim($v_data[devmajor]);
  1027. $v_header[devminor] = trim($v_data[devminor]);
  1028. */
  1029. return true;
  1030. }
  1031. // }}}
  1032. // {{{ _readLongHeader()
  1033. function _readLongHeader(&$v_header)
  1034. {
  1035. $v_filename = '';
  1036. $n = floor($v_header['size']/512);
  1037. for ($i=0; $i<$n; $i++) {
  1038. $v_content = $this->_readBlock();
  1039. $v_filename .= $v_content;
  1040. }
  1041. if (($v_header['size'] % 512) != 0) {
  1042. $v_content = $this->_readBlock();
  1043. $v_filename .= $v_content;
  1044. }
  1045. // ----- Read the next header
  1046. $v_binary_data = $this->_readBlock();
  1047. if (!$this->_readHeader($v_binary_data, $v_header))
  1048. return false;
  1049. $v_header['filename'] = $v_filename;
  1050. return true;
  1051. }
  1052. // }}}
  1053. // {{{ _extractInString()
  1054. /**
  1055. * This method extract from the archive one file identified by $p_filename.
  1056. * The return value is a string with the file content, or NULL on error.
  1057. * @param string $p_filename The path of the file to extract in a string.
  1058. * @return a string with the file content or NULL.
  1059. * @access private
  1060. */
  1061. function _extractInString($p_filename)
  1062. {
  1063. $v_result_str = "";
  1064. While (strlen($v_binary_data = $this->_readBlock()) != 0)
  1065. {
  1066. if (!$this->_readHeader($v_binary_data, $v_header))
  1067. return NULL;
  1068. if ($v_header['filename'] == '')
  1069. continue;
  1070. // ----- Look for long filename
  1071. if ($v_header['typeflag'] == 'L') {
  1072. if (!$this->_readLongHeader($v_header))
  1073. return NULL;
  1074. }
  1075. if ($v_header['filename'] == $p_filename) {
  1076. if ($v_header['typeflag'] == "5") {
  1077. $this->_error('Unable to extract in string a directory entry {'.$v_header['filename'].'}');
  1078. return NULL;
  1079. } else {
  1080. $n = floor($v_header['size']/512);
  1081. for ($i=0; $i<$n; $i++) {
  1082. $v_result_str .= $this->_readBlock();
  1083. }
  1084. if (($v_header['size'] % 512) != 0) {
  1085. $v_content = $this->_readBlock();
  1086. $v_result_str .= substr($v_content, 0, ($v_header['size'] % 512));
  1087. }
  1088. return $v_result_str;
  1089. }
  1090. } else {
  1091. $this->_jumpBlock(ceil(($v_header['size']/512)));
  1092. }
  1093. }
  1094. return NULL;
  1095. }
  1096. // }}}
  1097. // {{{ _extractList()
  1098. function _extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path)
  1099. {
  1100. $v_result=true;
  1101. $v_nb = 0;
  1102. $v_extract_all = true;
  1103. $v_listing = false;
  1104. $p_path = $this->_translateWinPath($p_path, false);
  1105. if ($p_path == '' || (substr($p_path, 0, 1) != '/' && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) {
  1106. $p_path = "./".$p_path;
  1107. }
  1108. $p_remove_path = $this->_translateWinPath($p_remove_path);
  1109. // ----- Look for path to remove format (should end by /)
  1110. if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/'))
  1111. $p_remove_path .= '/';
  1112. $p_remove_path_size = strlen($p_remove_path);
  1113. switch ($p_mode) {
  1114. case "complete" :
  1115. $v_extract_all = TRUE;
  1116. $v_listing = FALSE;
  1117. break;
  1118. case "partial" :
  1119. $v_extract_all = FALSE;
  1120. $v_listing = FALSE;
  1121. break;
  1122. case "list" :
  1123. $v_extract_all = FALSE;
  1124. $v_listing = TRUE;
  1125. break;
  1126. default :
  1127. $this->_error('Invalid extract mode ('.$p_mode.')');
  1128. return false;
  1129. }
  1130. clearstatcache();
  1131. While (strlen($v_binary_data = $this->_readBlock()) != 0)
  1132. {
  1133. $v_extract_file = FALSE;
  1134. $v_extraction_stopped = 0;
  1135. if (!$this->_readHeader($v_binary_data, $v_header))
  1136. return false;
  1137. if ($v_header['filename'] == '')
  1138. continue;
  1139. // ----- Look for long filename
  1140. if ($v_header['typeflag'] == 'L') {
  1141. if (!$this->_readLongHeader($v_header))
  1142. return false;
  1143. }
  1144. if ((!$v_extract_all) && (is_array($p_file_list))) {
  1145. // ----- By default no unzip if the file is not found
  1146. $v_extract_file = false;
  1147. for ($i=0; $i<sizeof($p_file_list); $i++) {
  1148. // ----- Look if it is a directory
  1149. if (substr($p_file_list[$i], -1) == '/') {
  1150. // ----- Look if the directory is in the filename path
  1151. if ((strlen($v_header['filename']) > strlen($p_file_list[$i])) && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i])) {
  1152. $v_extract_file = TRUE;
  1153. break;
  1154. }
  1155. }
  1156. // ----- It is a file, so compare the file names
  1157. elseif ($p_file_list[$i] == $v_header['filename']) {
  1158. $v_extract_file = TRUE;
  1159. break;
  1160. }
  1161. }
  1162. } else {
  1163. $v_extract_file = TRUE;
  1164. }
  1165. // ----- Look if this file need to be extracted
  1166. if (($v_extract_file) && (!$v_listing))
  1167. {
  1168. if (($p_remove_path != '')
  1169. && (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path))
  1170. $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
  1171. if (($p_path != './') && ($p_path != '/')) {
  1172. while (substr($p_path, -1) == '/')
  1173. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1174. if (substr($v_header['filename'], 0, 1) == '/')
  1175. $v_header['filename'] = $p_path.$v_header['filename'];
  1176. else
  1177. $v_header['filename'] = $p_path.'/'.$v_header['filename'];
  1178. }
  1179. if (file_exists($v_header['filename'])) {
  1180. if ((@is_dir($v_header['filename'])) && ($v_header['typeflag'] == '')) {
  1181. $this->_error('File '.$v_header['filename'].' already exists as a directory');
  1182. return false;
  1183. }
  1184. if ((is_file($v_header['filename'])) && ($v_header['typeflag'] == "5")) {
  1185. $this->_error('Directory '.$v_header['filename'].' already exists as a file');
  1186. return false;
  1187. }
  1188. if (!is_writeable($v_header['filename'])) {
  1189. $this->_error('File '.$v_header['filename'].' already exists and is write protected');
  1190. return false;
  1191. }
  1192. if (filemtime($v_header['filename']) > $v_header['mtime']) {
  1193. // To be completed : An error or silent no replace ?
  1194. }
  1195. }
  1196. // ----- Check the directory availability and create it if necessary
  1197. elseif (($v_result = $this->_dirCheck(($v_header['typeflag'] == "5"?$v_header['filename']:dirname($v_header['filename'])))) != 1) {
  1198. $this->_error('Unable to create path for '.$v_header['filename']);
  1199. return false;
  1200. }
  1201. if ($v_extract_file) {
  1202. if ($v_header['typeflag'] == "5") {
  1203. if (!@file_exists($v_header['filename'])) {
  1204. if (!@mkdir($v_header['filename'], 0777)) {
  1205. $this->_error('Unable to create directory {'.$v_header['filename'].'}');
  1206. return false;
  1207. }
  1208. }
  1209. } else {
  1210. if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
  1211. $this->_error('Error while opening {'.$v_header['filename'].'} in write binary mode');
  1212. return false;
  1213. } else {
  1214. $n = floor($v_header['size']/512);
  1215. for ($i=0; $i<$n; $i++) {
  1216. $v_content = $this->_readBlock();
  1217. fwrite($v_dest_file, $v_content, 512);
  1218. }
  1219. if (($v_header['size'] % 512) != 0) {
  1220. $v_content = $this->_readBlock();
  1221. fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
  1222. }
  1223. @fclose($v_dest_file);
  1224. // ----- Change the file mode, mtime
  1225. @touch($v_header['filename'], $v_header['mtime']);

Large files files are truncated, but you can click here to view the full file