PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_zoo/framework/helpers/archive.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 2219 lines | 1524 code | 216 blank | 479 comment | 362 complexity | 9744b13119f7b551d7f7069046e5c0e0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1

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

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * Archive Helper class
  10. *
  11. * This class deals with the opening for archive files in tar and zip formats
  12. *
  13. * @package Framework.Helpers
  14. */
  15. class ArchiveHelper extends AppHelper {
  16. /**
  17. * Class Constructor
  18. *
  19. * @param App $app A reference to the global App object
  20. */
  21. public function __construct($app) {
  22. parent::__construct($app);
  23. // increase memory limit
  24. @ini_set('memory_limit', '256M');
  25. }
  26. /**
  27. * Open an archive file
  28. *
  29. * @param string $file The full file path to open
  30. * @param string $format The file format. If not specified, will be guessed by the file extension
  31. *
  32. * @return AppZipArchive|AppTarArchive The archive object (zip or tar)
  33. *
  34. * @since 1.0.0
  35. */
  36. public function open($file, $format = null) {
  37. // auto-detect format
  38. if (!$format) {
  39. $format = $this->format($file);
  40. }
  41. // create archive object
  42. if ($format == 'zip') {
  43. return new AppZipArchive($file);
  44. } elseif ($format == 'tar') {
  45. return new AppTarArchive($file);
  46. }
  47. return null;
  48. }
  49. /**
  50. * Get an archive format based on the file extension
  51. *
  52. * Any tar-related format (tgz, gz, etc) will be returned as tar
  53. *
  54. * @param string $file The filename or the full file path
  55. *
  56. * @return string The file format (zip or tar)
  57. *
  58. * @since 1.0.0
  59. */
  60. public function format($file) {
  61. // detect .zip format
  62. if (preg_match('/\.zip$/i', $file)) {
  63. return 'zip';
  64. }
  65. // detect .tar format
  66. if (preg_match('/\.tar$|\.tar\.gz$|\.tgz$|\.tar\.bz2$|\.tbz2$/i', $file)) {
  67. return 'tar';
  68. }
  69. return null;
  70. }
  71. }
  72. /**
  73. * Class to manage zip archives
  74. *
  75. * @package Framework.Classes
  76. */
  77. class AppZipArchive {
  78. /**
  79. * The ZipArchive class that deals with the zip file
  80. *
  81. * @var ZipArchive
  82. * @since 1.0.0
  83. */
  84. public $zip;
  85. /**
  86. * Class Constructor
  87. *
  88. * @param string $file The file to open
  89. *
  90. * @since 1.0.0
  91. */
  92. public function __construct($file) {
  93. $this->zip = new ZipArchive();
  94. $this->zip->open($file);
  95. }
  96. /**
  97. * Add a file to the archive
  98. *
  99. * @param string $file The file to add
  100. *
  101. * @since 1.0.0
  102. */
  103. public function add($file) {
  104. $this->zip->addFile($file);
  105. }
  106. /**
  107. * Extract the archive into a specific directory
  108. *
  109. * @param string $path The directory in which to extract the file
  110. * @param string|array $files The file(s) to extract from the archive. If empty, it will extract the entire archive content
  111. *
  112. * @return boolean If the operation was successful
  113. */
  114. public function extract($path, $files = array()) {
  115. return empty($files) ? $this->zip->extractTo($path) : $this->zip->extractTo($path, $files);
  116. }
  117. }
  118. /**
  119. * Class to manage Tar-based archives
  120. *
  121. * @package Framework.Classes
  122. */
  123. class AppTarArchive {
  124. /**
  125. * The Archive_Tar object that will manage the archive
  126. *
  127. * @var Archive_Tar
  128. * @since 1.0.0
  129. */
  130. public $tar;
  131. /**
  132. * Class Constructor
  133. *
  134. * @param string $file The full file path of the archive
  135. *
  136. * @since 1.0.0
  137. */
  138. public function __construct($file) {
  139. $this->tar = new Archive_Tar($file);
  140. }
  141. /**
  142. * Add a file to the archive
  143. *
  144. * @param string $file The file to add
  145. *
  146. * @since 1.0.0
  147. */
  148. public function add($file) {
  149. $this->tar->add($file);
  150. }
  151. /**
  152. * Extract the archive into a specific directory
  153. *
  154. * @param string $path The directory in which to extract the file
  155. * @param string|array $files The file(s) to extract from the archive. If empty, it will extract the entire archive content
  156. *
  157. * @return boolean If the operation was successful
  158. */
  159. public function extract($path, $files = array()) {
  160. return empty($files) ? $this->tar->extract($path) : $this->tar->extractList($files, $path);
  161. }
  162. }
  163. /**
  164. * File::CSV
  165. *
  166. * PHP versions 4 and 5
  167. *
  168. * Copyright (c) 1997-2008,
  169. * Vincent Blavet <vincent@phpconcept.net>
  170. * All rights reserved.
  171. *
  172. * Redistribution and use in source and binary forms, with or without
  173. * modification, are permitted provided that the following conditions are met:
  174. *
  175. * * Redistributions of source code must retain the above copyright notice,
  176. * this list of conditions and the following disclaimer.
  177. * * Redistributions in binary form must reproduce the above copyright
  178. * notice, this list of conditions and the following disclaimer in the
  179. * documentation and/or other materials provided with the distribution.
  180. *
  181. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  182. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  183. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  184. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  185. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  186. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  187. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  188. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  189. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  190. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  191. *
  192. *
  193. * @package Framework.Classes
  194. * @author Vincent Blavet <vincent@phpconcept.net>
  195. * @copyright 1997-2008 The Authors
  196. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  197. * @version CVS: $Id: system.tar.inc,v 1.5 2010/02/09 12:29:39 dries Exp $
  198. * @link http://pear.php.net/package/Archive_Tar
  199. */
  200. define('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
  201. define('ARCHIVE_TAR_END_BLOCK', pack("a512", ''));
  202. /**
  203. * Creates a (compressed) Tar archive
  204. *
  205. * @author Vincent Blavet <vincent@phpconcept.net>
  206. * @version $Revision: 1.5 $
  207. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  208. * @package Framework.Classes
  209. */
  210. class Archive_Tar
  211. {
  212. /**
  213. * Name of the Tar
  214. *
  215. * @var string
  216. */
  217. var $_tarname='';
  218. /**
  219. * if true, the Tar file will be gzipped
  220. *
  221. * @var boolean
  222. */
  223. var $_compress=false;
  224. /**
  225. * Type of compression : 'none', 'gz' or 'bz2'
  226. *
  227. * @var string
  228. */
  229. var $_compress_type='none';
  230. /**
  231. * Explode separator
  232. *
  233. * @var string
  234. */
  235. var $_separator=' ';
  236. /**
  237. * Descriptor
  238. *
  239. * @var file
  240. */
  241. var $_file=0;
  242. /**
  243. * Local Tar name of a remote Tar (http:// or ftp://)
  244. * @var string
  245. */
  246. var $_temp_tarname='';
  247. /**
  248. * Archive_Tar Class constructor. This flavour of the constructor only
  249. * declare a new Archive_Tar object, identifying it by the name of the
  250. * tar file.
  251. * If the compress argument is set the tar will be read or created as a
  252. * gzip or bz2 compressed TAR file.
  253. *
  254. * @param string $p_tarname The name of the tar archive to create
  255. * @param string $p_compress can be null, 'gz' or 'bz2'. This
  256. * parameter indicates if gzip or bz2 compression
  257. * is required. For compatibility reason the
  258. * boolean value 'true' means 'gz'.
  259. * @access public
  260. */
  261. function __construct($p_tarname, $p_compress = null)
  262. {
  263. $this->_compress = false;
  264. $this->_compress_type = 'none';
  265. if (($p_compress === null) || ($p_compress == '')) {
  266. if (@file_exists($p_tarname)) {
  267. if ($fp = @fopen($p_tarname, "rb")) {
  268. // look for gzip magic cookie
  269. $data = fread($fp, 2);
  270. fclose($fp);
  271. if ($data == "\37\213") {
  272. $this->_compress = true;
  273. $this->_compress_type = 'gz';
  274. // No sure it's enought for a magic code ....
  275. } elseif ($data == "BZ") {
  276. $this->_compress = true;
  277. $this->_compress_type = 'bz2';
  278. }
  279. }
  280. } else {
  281. // probably a remote file or some file accessible
  282. // through a stream interface
  283. if (substr($p_tarname, -2) == 'gz') {
  284. $this->_compress = true;
  285. $this->_compress_type = 'gz';
  286. } elseif ((substr($p_tarname, -3) == 'bz2') ||
  287. (substr($p_tarname, -2) == 'bz')) {
  288. $this->_compress = true;
  289. $this->_compress_type = 'bz2';
  290. }
  291. }
  292. } else {
  293. if (($p_compress === true) || ($p_compress == 'gz')) {
  294. $this->_compress = true;
  295. $this->_compress_type = 'gz';
  296. } else if ($p_compress == 'bz2') {
  297. $this->_compress = true;
  298. $this->_compress_type = 'bz2';
  299. } else {
  300. die("Unsupported compression type '$p_compress'\n".
  301. "Supported types are 'gz' and 'bz2'.\n");
  302. return false;
  303. }
  304. }
  305. $this->_tarname = $p_tarname;
  306. if ($this->_compress) { // assert zlib or bz2 extension support
  307. if ($this->_compress_type == 'gz')
  308. $extname = 'zlib';
  309. else if ($this->_compress_type == 'bz2')
  310. $extname = 'bz2';
  311. if (!extension_loaded($extname)) {
  312. $this->loadExtension($extname);
  313. }
  314. if (!extension_loaded($extname)) {
  315. die("The extension '$extname' couldn't be found.\n".
  316. "Make sure your version of PHP was built ".
  317. "with '$extname' support.\n");
  318. return false;
  319. }
  320. }
  321. }
  322. // }}}
  323. /**
  324. * OS independant PHP extension load. Remember to take care
  325. * on the correct extension name for case sensitive OSes.
  326. *
  327. * @param string $ext The extension name
  328. * @return bool Success or not on the dl() call
  329. */
  330. function loadExtension($ext)
  331. {
  332. if (!extension_loaded($ext)) {
  333. // if either returns true dl() will produce a FATAL error, stop that
  334. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  335. return false;
  336. }
  337. if (OS_WINDOWS) {
  338. $suffix = '.dll';
  339. } elseif (PHP_OS == 'HP-UX') {
  340. $suffix = '.sl';
  341. } elseif (PHP_OS == 'AIX') {
  342. $suffix = '.a';
  343. } elseif (PHP_OS == 'OSX') {
  344. $suffix = '.bundle';
  345. } else {
  346. $suffix = '.so';
  347. }
  348. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  349. }
  350. return true;
  351. }
  352. /**
  353. * Destructor
  354. */
  355. function __destruct()
  356. {
  357. $this->_close();
  358. // ----- Look for a local copy to delete
  359. if ($this->_temp_tarname != '')
  360. @unlink($this->_temp_tarname);
  361. }
  362. /**
  363. * This method creates the archive file and add the files / directories
  364. * that are listed in $p_filelist.
  365. * If a file with the same name exist and is writable, it is replaced
  366. * by the new tar.
  367. * The method return false and a PEAR error text.
  368. * The $p_filelist parameter can be an array of string, each string
  369. * representing a filename or a directory name with their path if
  370. * needed. It can also be a single string with names separated by a
  371. * single blank.
  372. * For each directory added in the archive, the files and
  373. * sub-directories are also added.
  374. * See also createModify() method for more details.
  375. *
  376. * @param array $p_filelist An array of filenames and directory names, or a
  377. * single string with names separated by a single
  378. * blank space.
  379. * @return true on success, false on error.
  380. * @see createModify()
  381. * @access public
  382. */
  383. function create($p_filelist)
  384. {
  385. return $this->createModify($p_filelist, '', '');
  386. }
  387. // }}}
  388. // {{{ add()
  389. /**
  390. * This method add the files / directories that are listed in $p_filelist in
  391. * the archive. If the archive does not exist it is created.
  392. * The method return false and a PEAR error text.
  393. * The files and directories listed are only added at the end of the archive,
  394. * even if a file with the same name is already archived.
  395. * See also createModify() method for more details.
  396. *
  397. * @param array $p_filelist An array of filenames and directory names, or a
  398. * single string with names separated by a single
  399. * blank space.
  400. * @return true on success, false on error.
  401. * @see createModify()
  402. * @access public
  403. */
  404. function add($p_filelist)
  405. {
  406. return $this->addModify($p_filelist, '', '');
  407. }
  408. // }}}
  409. /**
  410. * This method extract all the content of the archive in the directory
  411. *
  412. * @param string $p_path The path in which to extract the files
  413. *
  414. * @return boolean If the operation was successfull
  415. *
  416. * @see Archive_Tar::extractModify()
  417. */
  418. function extract($p_path='')
  419. {
  420. return $this->extractModify($p_path, '');
  421. }
  422. // }}}
  423. /**
  424. * Return the content of the archive
  425. *
  426. * @return array The content of the archive
  427. */
  428. function listContent()
  429. {
  430. $v_list_detail = array();
  431. if ($this->_openRead()) {
  432. if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
  433. unset($v_list_detail);
  434. $v_list_detail = 0;
  435. }
  436. $this->_close();
  437. }
  438. return $v_list_detail;
  439. }
  440. // }}}
  441. // {{{ createModify()
  442. /**
  443. * This method creates the archive file and add the files / directories
  444. * that are listed in $p_filelist.
  445. * If the file already exists and is writable, it is replaced by the
  446. * new tar. It is a create and not an add. If the file exists and is
  447. * read-only or is a directory it is not replaced. The method return
  448. * false and a PEAR error text.
  449. * The $p_filelist parameter can be an array of string, each string
  450. * representing a filename or a directory name with their path if
  451. * needed. It can also be a single string with names separated by a
  452. * single blank.
  453. * The path indicated in $p_remove_dir will be removed from the
  454. * memorized path of each file / directory listed when this path
  455. * exists. By default nothing is removed (empty path '')
  456. * The path indicated in $p_add_dir will be added at the beginning of
  457. * the memorized path of each file / directory listed. However it can
  458. * be set to empty ''. The adding of a path is done after the removing
  459. * of path.
  460. * The path add/remove ability enables the user to prepare an archive
  461. * for extraction in a different path than the origin files are.
  462. * See also addModify() method for file adding properties.
  463. *
  464. * @param array $p_filelist An array of filenames and directory names,
  465. * or a single string with names separated by
  466. * a single blank space.
  467. * @param string $p_add_dir A string which contains a path to be added
  468. * to the memorized path of each element in
  469. * the list.
  470. * @param string $p_remove_dir A string which contains a path to be
  471. * removed from the memorized path of each
  472. * element in the list, when relevant.
  473. * @return boolean true on success, false on error.
  474. * @access public
  475. * @see addModify()
  476. */
  477. function createModify($p_filelist, $p_add_dir, $p_remove_dir='')
  478. {
  479. $v_result = true;
  480. if (!$this->_openWrite())
  481. return false;
  482. if ($p_filelist != '') {
  483. if (is_array($p_filelist))
  484. $v_list = $p_filelist;
  485. elseif (is_string($p_filelist))
  486. $v_list = explode($this->_separator, $p_filelist);
  487. else {
  488. $this->_cleanFile();
  489. $this->_error('Invalid file list');
  490. return false;
  491. }
  492. $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
  493. }
  494. if ($v_result) {
  495. $this->_writeFooter();
  496. $this->_close();
  497. } else
  498. $this->_cleanFile();
  499. return $v_result;
  500. }
  501. // }}}
  502. // {{{ addModify()
  503. /**
  504. * This method add the files / directories listed in $p_filelist at the
  505. * end of the existing archive. If the archive does not yet exists it
  506. * is created.
  507. * The $p_filelist parameter can be an array of string, each string
  508. * representing a filename or a directory name with their path if
  509. * needed. It can also be a single string with names separated by a
  510. * single blank.
  511. * The path indicated in $p_remove_dir will be removed from the
  512. * memorized path of each file / directory listed when this path
  513. * exists. By default nothing is removed (empty path '')
  514. * The path indicated in $p_add_dir will be added at the beginning of
  515. * the memorized path of each file / directory listed. However it can
  516. * be set to empty ''. The adding of a path is done after the removing
  517. * of path.
  518. * The path add/remove ability enables the user to prepare an archive
  519. * for extraction in a different path than the origin files are.
  520. * If a file/dir is already in the archive it will only be added at the
  521. * end of the archive. There is no update of the existing archived
  522. * file/dir. However while extracting the archive, the last file will
  523. * replace the first one. This results in a none optimization of the
  524. * archive size.
  525. * If a file/dir does not exist the file/dir is ignored. However an
  526. * error text is send to PEAR error.
  527. * If a file/dir is not readable the file/dir is ignored. However an
  528. * error text is send to PEAR error.
  529. *
  530. * @param array $p_filelist An array of filenames and directory
  531. * names, or a single string with names
  532. * separated by a single blank space.
  533. * @param string $p_add_dir A string which contains a path to be
  534. * added to the memorized path of each
  535. * element in the list.
  536. * @param string $p_remove_dir A string which contains a path to be
  537. * removed from the memorized path of
  538. * each element in the list, when
  539. * relevant.
  540. * @return true on success, false on error.
  541. * @access public
  542. */
  543. function addModify($p_filelist, $p_add_dir, $p_remove_dir='')
  544. {
  545. $v_result = true;
  546. if (!$this->_isArchive())
  547. $v_result = $this->createModify($p_filelist, $p_add_dir,
  548. $p_remove_dir);
  549. else {
  550. if (is_array($p_filelist))
  551. $v_list = $p_filelist;
  552. elseif (is_string($p_filelist))
  553. $v_list = explode($this->_separator, $p_filelist);
  554. else {
  555. $this->_error('Invalid file list');
  556. return false;
  557. }
  558. $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
  559. }
  560. return $v_result;
  561. }
  562. // }}}
  563. // {{{ addString()
  564. /**
  565. * This method add a single string as a file at the
  566. * end of the existing archive. If the archive does not yet exists it
  567. * is created.
  568. *
  569. * @param string $p_filename A string which contains the full
  570. * filename path that will be associated
  571. * with the string.
  572. * @param string $p_string The content of the file added in
  573. * the archive.
  574. * @return true on success, false on error.
  575. * @access public
  576. */
  577. function addString($p_filename, $p_string)
  578. {
  579. $v_result = true;
  580. if (!$this->_isArchive()) {
  581. if (!$this->_openWrite()) {
  582. return false;
  583. }
  584. $this->_close();
  585. }
  586. if (!$this->_openAppend())
  587. return false;
  588. // Need to check the get back to the temporary file ? ....
  589. $v_result = $this->_addString($p_filename, $p_string);
  590. $this->_writeFooter();
  591. $this->_close();
  592. return $v_result;
  593. }
  594. // }}}
  595. // {{{ extractModify()
  596. /**
  597. * This method extract all the content of the archive in the directory
  598. * indicated by $p_path. When relevant the memorized path of the
  599. * files/dir can be modified by removing the $p_remove_path path at the
  600. * beginning of the file/dir path.
  601. * While extracting a file, if the directory path does not exists it is
  602. * created.
  603. * While extracting a file, if the file already exists it is replaced
  604. * without looking for last modification date.
  605. * While extracting a file, if the file already exists and is write
  606. * protected, the extraction is aborted.
  607. * While extracting a file, if a directory with the same name already
  608. * exists, the extraction is aborted.
  609. * While extracting a directory, if a file with the same name already
  610. * exists, the extraction is aborted.
  611. * While extracting a file/directory if the destination directory exist
  612. * and is write protected, or does not exist but can not be created,
  613. * the extraction is aborted.
  614. * If after extraction an extracted file does not show the correct
  615. * stored file size, the extraction is aborted.
  616. * When the extraction is aborted, a PEAR error text is set and false
  617. * is returned. However the result can be a partial extraction that may
  618. * need to be manually cleaned.
  619. *
  620. * @param string $p_path The path of the directory where the
  621. * files/dir need to by extracted.
  622. * @param string $p_remove_path Part of the memorized path that can be
  623. * removed if present at the beginning of
  624. * the file/dir path.
  625. * @return boolean true on success, false on error.
  626. * @access public
  627. * @see extractList()
  628. */
  629. function extractModify($p_path, $p_remove_path)
  630. {
  631. $v_result = true;
  632. $v_list_detail = array();
  633. if ($v_result = $this->_openRead()) {
  634. $v_result = $this->_extractList($p_path, $v_list_detail,
  635. "complete", 0, $p_remove_path);
  636. $this->_close();
  637. }
  638. return $v_result;
  639. }
  640. // }}}
  641. // {{{ extractInString()
  642. /**
  643. * This method extract from the archive one file identified by $p_filename.
  644. * The return value is a string with the file content, or NULL on error.
  645. * @param string $p_filename The path of the file to extract in a string.
  646. * @return a string with the file content or NULL.
  647. * @access public
  648. */
  649. function extractInString($p_filename)
  650. {
  651. if ($this->_openRead()) {
  652. $v_result = $this->_extractInString($p_filename);
  653. $this->_close();
  654. } else {
  655. $v_result = NULL;
  656. }
  657. return $v_result;
  658. }
  659. // }}}
  660. // {{{ extractList()
  661. /**
  662. * This method extract from the archive only the files indicated in the
  663. * $p_filelist. These files are extracted in the current directory or
  664. * in the directory indicated by the optional $p_path parameter.
  665. * If indicated the $p_remove_path can be used in the same way as it is
  666. * used in extractModify() method.
  667. * @param array $p_filelist An array of filenames and directory names,
  668. * or a single string with names separated
  669. * by a single blank space.
  670. * @param string $p_path The path of the directory where the
  671. * files/dir need to by extracted.
  672. * @param string $p_remove_path Part of the memorized path that can be
  673. * removed if present at the beginning of
  674. * the file/dir path.
  675. * @return true on success, false on error.
  676. * @access public
  677. * @see extractModify()
  678. */
  679. function extractList($p_filelist, $p_path='', $p_remove_path='')
  680. {
  681. $v_result = true;
  682. $v_list_detail = array();
  683. if (is_array($p_filelist))
  684. $v_list = $p_filelist;
  685. elseif (is_string($p_filelist))
  686. $v_list = explode($this->_separator, $p_filelist);
  687. else {
  688. $this->_error('Invalid string list');
  689. return false;
  690. }
  691. if ($v_result = $this->_openRead()) {
  692. $v_result = $this->_extractList($p_path, $v_list_detail, "partial",
  693. $v_list, $p_remove_path);
  694. $this->_close();
  695. }
  696. return $v_result;
  697. }
  698. // }}}
  699. // {{{ setAttribute()
  700. /**
  701. * This method set specific attributes of the archive. It uses a variable
  702. * list of parameters, in the format attribute code + attribute values :
  703. * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
  704. *
  705. * @return true on success, false on error.
  706. * @access public
  707. */
  708. function setAttribute()
  709. {
  710. $v_result = true;
  711. // ----- Get the number of variable list of arguments
  712. if (($v_size = func_num_args()) == 0) {
  713. return true;
  714. }
  715. // ----- Get the arguments
  716. $v_att_list = &func_get_args();
  717. // ----- Read the attributes
  718. $i=0;
  719. while ($i<$v_size) {
  720. // ----- Look for next option
  721. switch ($v_att_list[$i]) {
  722. // ----- Look for options that request a string value
  723. case ARCHIVE_TAR_ATT_SEPARATOR :
  724. // ----- Check the number of parameters
  725. if (($i+1) >= $v_size) {
  726. $this->_error('Invalid number of parameters for '
  727. .'attribute ARCHIVE_TAR_ATT_SEPARATOR');
  728. return false;
  729. }
  730. // ----- Get the value
  731. $this->_separator = $v_att_list[$i+1];
  732. $i++;
  733. break;
  734. default :
  735. $this->_error('Unknow attribute code '.$v_att_list[$i].'');
  736. return false;
  737. }
  738. // ----- Next attribute
  739. $i++;
  740. }
  741. return $v_result;
  742. }
  743. // }}}
  744. /**
  745. * Throw an exception
  746. *
  747. * @param string $p_message The error string
  748. *
  749. * @throws Exception
  750. */
  751. function _error($p_message)
  752. {
  753. // ----- To be completed
  754. throw new Exception($p_message);
  755. }
  756. // }}}
  757. /**
  758. * Throw an exception
  759. *
  760. * @param string $p_message The error string
  761. *
  762. * @throws Exception
  763. */
  764. function _warning($p_message)
  765. {
  766. // ----- To be completed
  767. throw new Exception($p_message);
  768. }
  769. // }}}
  770. /**
  771. * Check if the file is an archive
  772. *
  773. * @param string $p_filename The file to check
  774. *
  775. * @return boolean If the file is an archive
  776. */
  777. function _isArchive($p_filename=NULL)
  778. {
  779. if ($p_filename == NULL) {
  780. $p_filename = $this->_tarname;
  781. }
  782. clearstatcache();
  783. return @is_file($p_filename) && !@is_link($p_filename);
  784. }
  785. // }}}
  786. /**
  787. * Opens a file for writing
  788. *
  789. * @return boolean True on success
  790. */
  791. function _openWrite()
  792. {
  793. if ($this->_compress_type == 'gz')
  794. $this->_file = @gzopen($this->_tarname, "wb9");
  795. else if ($this->_compress_type == 'bz2')
  796. $this->_file = @bzopen($this->_tarname, "w");
  797. else if ($this->_compress_type == 'none')
  798. $this->_file = @fopen($this->_tarname, "wb");
  799. else
  800. $this->_error('Unknown or missing compression type ('
  801. .$this->_compress_type.')');
  802. if ($this->_file == 0) {
  803. $this->_error('Unable to open in write mode \''
  804. .$this->_tarname.'\'');
  805. return false;
  806. }
  807. return true;
  808. }
  809. // }}}
  810. /**
  811. * Open a file for reading
  812. *
  813. * @return boolean True on success
  814. */
  815. function _openRead()
  816. {
  817. if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
  818. // ----- Look if a local copy need to be done
  819. if ($this->_temp_tarname == '') {
  820. $this->_temp_tarname = uniqid('tar').'.tmp';
  821. if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
  822. $this->_error('Unable to open in read mode \''
  823. .$this->_tarname.'\'');
  824. $this->_temp_tarname = '';
  825. return false;
  826. }
  827. if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
  828. $this->_error('Unable to open in write mode \''
  829. .$this->_temp_tarname.'\'');
  830. $this->_temp_tarname = '';
  831. return false;
  832. }
  833. while ($v_data = @fread($v_file_from, 1024))
  834. @fwrite($v_file_to, $v_data);
  835. @fclose($v_file_from);
  836. @fclose($v_file_to);
  837. }
  838. // ----- File to open if the local copy
  839. $v_filename = $this->_temp_tarname;
  840. } else
  841. // ----- File to open if the normal Tar file
  842. $v_filename = $this->_tarname;
  843. if ($this->_compress_type == 'gz')
  844. $this->_file = @gzopen($v_filename, "rb");
  845. else if ($this->_compress_type == 'bz2')
  846. $this->_file = @bzopen($v_filename, "r");
  847. else if ($this->_compress_type == 'none')
  848. $this->_file = @fopen($v_filename, "rb");
  849. else
  850. $this->_error('Unknown or missing compression type ('
  851. .$this->_compress_type.')');
  852. if ($this->_file == 0) {
  853. $this->_error('Unable to open in read mode \''.$v_filename.'\'');
  854. return false;
  855. }
  856. return true;
  857. }
  858. // }}}
  859. /**
  860. * Open a file for reading and writing
  861. *
  862. * @return boolean true on success
  863. */
  864. function _openReadWrite()
  865. {
  866. if ($this->_compress_type == 'gz')
  867. $this->_file = @gzopen($this->_tarname, "r+b");
  868. else if ($this->_compress_type == 'bz2') {
  869. $this->_error('Unable to open bz2 in read/write mode \''
  870. .$this->_tarname.'\' (limitation of bz2 extension)');
  871. return false;
  872. } else if ($this->_compress_type == 'none')
  873. $this->_file = @fopen($this->_tarname, "r+b");
  874. else
  875. $this->_error('Unknown or missing compression type ('
  876. .$this->_compress_type.')');
  877. if ($this->_file == 0) {
  878. $this->_error('Unable to open in read/write mode \''
  879. .$this->_tarname.'\'');
  880. return false;
  881. }
  882. return true;
  883. }
  884. // }}}
  885. /**
  886. * Close a file handler
  887. */
  888. function _close()
  889. {
  890. //if (isset($this->_file)) {
  891. if (is_resource($this->_file)) {
  892. if ($this->_compress_type == 'gz')
  893. @gzclose($this->_file);
  894. else if ($this->_compress_type == 'bz2')
  895. @bzclose($this->_file);
  896. else if ($this->_compress_type == 'none')
  897. @fclose($this->_file);
  898. else
  899. $this->_error('Unknown or missing compression type ('
  900. .$this->_compress_type.')');
  901. $this->_file = 0;
  902. }
  903. // ----- Look if a local copy need to be erase
  904. // Note that it might be interesting to keep the url for a time : ToDo
  905. if ($this->_temp_tarname != '') {
  906. @unlink($this->_temp_tarname);
  907. $this->_temp_tarname = '';
  908. }
  909. return true;
  910. }
  911. // }}}
  912. /**
  913. * Close the file handler and cleans the local vars
  914. *
  915. * @return boolean true on success
  916. */
  917. function _cleanFile()
  918. {
  919. $this->_close();
  920. // ----- Look for a local copy
  921. if ($this->_temp_tarname != '') {
  922. // ----- Remove the local copy but not the remote tarname
  923. @unlink($this->_temp_tarname);
  924. $this->_temp_tarname = '';
  925. } else {
  926. // ----- Remove the local tarname file
  927. @unlink($this->_tarname);
  928. }
  929. $this->_tarname = '';
  930. return true;
  931. }
  932. // }}}
  933. /**
  934. * Write a block of data
  935. *
  936. * @param mixed $p_binary_data The data to write
  937. * @param int $p_len The length of the data to write
  938. *
  939. * @return boolean true on success
  940. */
  941. function _writeBlock($p_binary_data, $p_len=null)
  942. {
  943. if (is_resource($this->_file)) {
  944. if ($p_len === null) {
  945. if ($this->_compress_type == 'gz')
  946. @gzputs($this->_file, $p_binary_data);
  947. else if ($this->_compress_type == 'bz2')
  948. @bzwrite($this->_file, $p_binary_data);
  949. else if ($this->_compress_type == 'none')
  950. @fputs($this->_file, $p_binary_data);
  951. else
  952. $this->_error('Unknown or missing compression type ('
  953. .$this->_compress_type.')');
  954. } else {
  955. if ($this->_compress_type == 'gz')
  956. @gzputs($this->_file, $p_binary_data, $p_len);
  957. else if ($this->_compress_type == 'bz2')
  958. @bzwrite($this->_file, $p_binary_data, $p_len);
  959. else if ($this->_compress_type == 'none')
  960. @fputs($this->_file, $p_binary_data, $p_len);
  961. else
  962. $this->_error('Unknown or missing compression type ('
  963. .$this->_compress_type.')');
  964. }
  965. }
  966. return true;
  967. }
  968. // }}}
  969. /**
  970. * Read a block of data
  971. *
  972. * @return mixed The data read
  973. */
  974. function _readBlock()
  975. {
  976. $v_block = null;
  977. if (is_resource($this->_file)) {
  978. if ($this->_compress_type == 'gz')
  979. $v_block = @gzread($this->_file, 512);
  980. else if ($this->_compress_type == 'bz2')
  981. $v_block = @bzread($this->_file, 512);
  982. else if ($this->_compress_type == 'none')
  983. $v_block = @fread($this->_file, 512);
  984. else
  985. $this->_error('Unknown or missing compression type ('
  986. .$this->_compress_type.')');
  987. }
  988. return $v_block;
  989. }
  990. // }}}
  991. /**
  992. * Jump to a block of data
  993. *
  994. * @param int $p_len The position to jump to
  995. *
  996. * @return boolean True on success
  997. */
  998. function _jumpBlock($p_len=null)
  999. {
  1000. if (is_resource($this->_file)) {
  1001. if ($p_len === null)
  1002. $p_len = 1;
  1003. if ($this->_compress_type == 'gz') {
  1004. @gzseek($this->_file, gztell($this->_file)+($p_len*512));
  1005. }
  1006. else if ($this->_compress_type == 'bz2') {
  1007. // ----- Replace missing bztell() and bzseek()
  1008. for ($i=0; $i<$p_len; $i++)
  1009. $this->_readBlock();
  1010. } else if ($this->_compress_type == 'none')
  1011. @fseek($this->_file, ftell($this->_file)+($p_len*512));
  1012. else
  1013. $this->_error('Unknown or missing compression type ('
  1014. .$this->_compress_type.')');
  1015. }
  1016. return true;
  1017. }
  1018. // }}}
  1019. /**
  1020. * Write the end of the file
  1021. *
  1022. * @return boolean true on success
  1023. */
  1024. function _writeFooter()
  1025. {
  1026. if (is_resource($this->_file)) {
  1027. // ----- Write the last 0 filled block for end of archive
  1028. $v_binary_data = pack('a1024', '');
  1029. $this->_writeBlock($v_binary_data);
  1030. }
  1031. return true;
  1032. }
  1033. // }}}
  1034. /**
  1035. * Add a list of resources
  1036. *
  1037. * @param array $p_list The list of files to add
  1038. * @param string $p_add_dir The directory to add the files to
  1039. * @param string $p_remove_dir A directory to remove from the file path
  1040. *
  1041. * @return boolean True on success
  1042. */
  1043. function _addList($p_list, $p_add_dir, $p_remove_dir)
  1044. {
  1045. $v_result=true;
  1046. $v_header = array();
  1047. // ----- Remove potential windows directory separator
  1048. $p_add_dir = $this->_translateWinPath($p_add_dir);
  1049. $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);
  1050. if (!$this->_file) {
  1051. $this->_error('Invalid file descriptor');
  1052. return false;
  1053. }
  1054. if (sizeof($p_list) == 0)
  1055. return true;
  1056. foreach ($p_list as $v_filename) {
  1057. if (!$v_result) {
  1058. break;
  1059. }
  1060. // ----- Skip the current tar name
  1061. if ($v_filename == $this->_tarname)
  1062. continue;
  1063. if ($v_filename == '')
  1064. continue;
  1065. if (!file_exists($v_filename)) {
  1066. $this->_warning("File '$v_filename' does not exist");
  1067. continue;
  1068. }
  1069. // ----- Add the file or directory header
  1070. if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir))
  1071. return false;
  1072. if (@is_dir($v_filename) && !@is_link($v_filename)) {
  1073. if (!($p_hdir = opendir($v_filename))) {
  1074. $this->_warning("Directory '$v_filename' can not be read");
  1075. continue;
  1076. }
  1077. while (false !== ($p_hitem = readdir($p_hdir))) {
  1078. if (($p_hitem != '.') && ($p_hitem != '..')) {
  1079. if ($v_filename != ".")
  1080. $p_temp_list[0] = $v_filename.'/'.$p_hitem;
  1081. else
  1082. $p_temp_list[0] = $p_hitem;
  1083. $v_result = $this->_addList($p_temp_list,
  1084. $p_add_dir,
  1085. $p_remove_dir);
  1086. }
  1087. }
  1088. unset($p_temp_list);
  1089. unset($p_hdir);
  1090. unset($p_hitem);
  1091. }
  1092. }
  1093. return $v_result;
  1094. }
  1095. // }}}
  1096. /**
  1097. * Add a file
  1098. *
  1099. * @param string $p_filename The file to add
  1100. * @param string $p_header Header
  1101. * @param string $p_add_dir The directory to add the files to
  1102. * @param string $p_remove_dir A directory to remove from the file path
  1103. *
  1104. * @return boolean True on success
  1105. */
  1106. function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir)
  1107. {
  1108. if (!$this->_file) {
  1109. $this->_error('Invalid file descriptor');
  1110. return false;
  1111. }
  1112. if ($p_filename == '') {
  1113. $this->_error('Invalid file name');
  1114. return false;
  1115. }
  1116. // ----- Calculate the stored filename
  1117. $p_filename = $this->_translateWinPath($p_filename, false);
  1118. $v_stored_filename = $p_filename;
  1119. if (strcmp($p_filename, $p_remove_dir) == 0) {
  1120. return true;
  1121. }
  1122. if ($p_remove_dir != '') {
  1123. if (substr($p_remove_dir, -1) != '/')
  1124. $p_remove_dir .= '/';
  1125. if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  1126. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1127. }
  1128. $v_stored_filename = $this->_translateWinPath($v_stored_filename);
  1129. if ($p_add_dir != '') {
  1130. if (substr($p_add_dir, -1) == '/')
  1131. $v_stored_filename = $p_add_dir.$v_stored_filename;
  1132. else
  1133. $v_stored_filename = $p_add_dir.'/'.$v_stored_filename;
  1134. }
  1135. $v_stored_filename = $this->_pathReduction($v_stored_filename);
  1136. if ($this->_isArchive($p_filename)) {
  1137. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1138. $this->_warning("Unable to open file '".$p_filename
  1139. ."' in binary read mode");
  1140. return true;
  1141. }
  1142. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  1143. return false;
  1144. while (($v_buffer = fread($v_file, 512)) != '') {
  1145. $v_binary_data = pack("a512", "$v_buffer");
  1146. $this->_writeBlock($v_binary_data);
  1147. }
  1148. fclose($v_file);
  1149. } else {
  1150. // ----- Only header for dir
  1151. if (!$this->_writeHeader($p_filename, $v_stored_filename))
  1152. return false;
  1153. }
  1154. return true;
  1155. }
  1156. // }}}
  1157. /**
  1158. * Add a string to a file
  1159. *
  1160. * @param string $p_filename The file
  1161. * @param string $p_string The string
  1162. *
  1163. * @return boolean true on success
  1164. */
  1165. function _addString($p_filename, $p_string)
  1166. {
  1167. if (!$this->_file) {
  1168. $this->_error('Invalid file descriptor');
  1169. return false;
  1170. }
  1171. if ($p_filename == '') {
  1172. $this->_error('Invalid file name');
  1173. return false;
  1174. }
  1175. // ----- Calculate the stored filename
  1176. $p_filename = $this->_translateWinPath($p_filename, false);
  1177. if (!$this->_writeHeaderBlock($p_filename, strlen($p_string),
  1178. time(), 384, "", 0, 0))
  1179. return false;
  1180. $i=0;
  1181. while (($v_buffer = substr($p_string, (($i++)*512), 512)) != '') {
  1182. $v_binary_data = pack("a512", $v_buffer);
  1183. $this->_writeBlock($v_binary_data);
  1184. }
  1185. return true;
  1186. }
  1187. // }}}
  1188. /**
  1189. * Write an header to a file
  1190. *
  1191. * @param string $p_filename The file
  1192. * @param string $p_stored_filename The stored file
  1193. */
  1194. function _writeHeader($p_filename, $p_stored_filename)
  1195. {
  1196. if ($p_stored_filename == '')
  1197. $p_stored_filename = $p_filename;
  1198. $v_reduce_filename = $this->_pathReduction($p_stored_filename);
  1199. if (strlen($v_reduce_filename) > 99) {
  1200. if (!$this->_writeLongHeader($v_reduce_filename))
  1201. return false;
  1202. }
  1203. $v_info = lstat($p_filename);
  1204. $v_uid = sprintf("%6s ", DecOct($v_info[4]));
  1205. $v_gid = sprintf("%6s ", DecOct($v_info[5]));
  1206. $v_perms = sprintf("%6s ", DecOct($v_info['mode']));
  1207. $v_mtime = sprintf("%11s", DecOct($v_info['mode']));
  1208. $v_linkname = '';
  1209. if (@is_link($p_filename)) {
  1210. $v_typeflag = '2';
  1211. $v_linkname = readlink($p_filename);
  1212. $v_size = sprintf("%11s ", DecOct(0));
  1213. } elseif (@is_dir($p_filename)) {
  1214. $v_typeflag = "5";
  1215. $v_size = sprintf("%11s ", DecOct(0));
  1216. } else {
  1217. $v_typeflag = '';
  1218. clearstatcache();
  1219. $v_size = sprintf("%11s ", DecOct($v_info['size']));
  1220. }
  1221. $v_magic = '';
  1222. $v_version = '';
  1223. $v_uname = '';
  1224. $v_gname = '';
  1225. $v_devmajor = '';
  1226. $v_devminor = '';
  1227. $v_prefix = '';
  1228. $v_binary_data_first = pack("a100a8a8a8a12A12",
  1229. $v_reduce_filename, $v_perms, $v_uid,
  1230. $v_gid, $v_size, $v_mtime);
  1231. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1232. $v_typeflag, $v_linkname, $v_magic,
  1233. $v_version, $v_uname, $v_gname,
  1234. $v_devmajor, $v_devminor, $v_prefix, '');
  1235. // ----- Calculate the checksum
  1236. $v_checksum = 0;
  1237. // ..... First part of the header
  1238. for ($i=0; $i<148; $i++)
  1239. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1240. // ..... Ignore the checksum value and replace it by ' ' (space)
  1241. for ($i=148; $i<156; $i++)
  1242. $v_checksum += ord(' ');
  1243. // ..... Last part of the header
  1244. for ($i=156, $j=0; $i<512; $i++, $j++)
  1245. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1246. // ----- Write the first 148 bytes of the header in the archive
  1247. $this->_writeBlock($v_binary_data_first, 148);
  1248. // ----- Write the calculated checksum
  1249. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  1250. $v_binary_data = pack("a8", $v_checksum);
  1251. $this->_writeBlock($v_binary_data, 8);
  1252. // ----- Write the last 356 bytes of the header in the archive
  1253. $this->_writeBlock($v_binary_data_last, 356);
  1254. return true;
  1255. }
  1256. // }}}
  1257. /**
  1258. * Write an header block
  1259. *
  1260. * @param string $p_filename The file
  1261. * @param int $p_size The size
  1262. * @param int $p_mtime The mtime
  1263. * @param string $p_perms The permissions
  1264. * @param string $p_type The type of the file
  1265. * @param int $p_uid The uid
  1266. * @param int $p_gid the gid
  1267. *
  1268. * @return boolean Ture on succcess
  1269. */
  1270. function _writeHeaderBlock($p_filename, $p_size, $p_mtime=0, $p_perms=0,
  1271. $p_type='', $p_uid=0, $p_gid=0)
  1272. {
  1273. $p_filename = $this->_pathReduction($p_filename);
  1274. if (strlen($p_filename) > 99) {
  1275. if (!$this->_writeLongHeader($p_filename))
  1276. return false;
  1277. }
  1278. if ($p_type == "5") {
  1279. $v_size = sprintf("%11s ", DecOct(0));
  1280. } else {
  1281. $v_size = sprintf("%11s ", DecOct($p_size));
  1282. }
  1283. $v_uid = sprintf("%6s ", DecOct($p_uid));
  1284. $v_gid = sprintf("%6s ", DecOct($p_gid));
  1285. $v_perms = sprintf("%6s ", DecOct($p_perms));
  1286. $v_mtime = sprintf("%11s", DecOct($p_mtime));
  1287. $v_linkname = '';
  1288. $v_magic = '';
  1289. $v_version = '';
  1290. $v_uname = '';
  1291. $v_gname = '';
  1292. $v_devmajor = '';
  1293. $v_devminor = '';
  1294. $v_prefix = '';
  1295. $v_binary_data_first = pack("a100a8a8a8a12A12",
  1296. $p_filename, $v_perms, $v_uid, $v_gid,
  1297. $v_size, $v_mtime);
  1298. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1299. $p_type, $v_linkname, $v_magic,
  1300. $v_version, $v_uname, $v_gname,
  1301. $v_devmajor, $v_devminor, $v_prefix, '');
  1302. // ----- Calculate the checksum
  1303. $v_checksum = 0;
  1304. // ..... First part of the header
  1305. for ($i=0; $i<148; $i++)
  1306. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1307. // ..... Ignore the checksum value and replace it by ' ' (space)
  1308. for ($i=148; $i<156; $i++)
  1309. $v_checksum += ord(' ');
  1310. // ..... Last part of the header
  1311. for ($i=156, $j=0; $i<512; $i++, $j++)
  1312. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1313. // ----- Write the first 148 bytes of the header in the archive
  1314. $this->_writeBlock($v_binary_data_first, 148);
  1315. // ----- Write the calculated checksum
  1316. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  1317. $v_binary_data = pack("a8", $v_checksum);
  1318. $this->_writeBlock($v_binary_data, 8);
  1319. // ----- Write the last 356 bytes of the header in the archive
  1320. $this->_writeBlock($v_binary_data_last, 356);
  1321. return true;
  1322. }
  1323. // }}}
  1324. /**
  1325. * Write a long header
  1326. *
  1327. * @param string $p_filename The file
  1328. */
  1329. function _writeLongHeader($p_filename)
  1330. {
  1331. $v_size = sprintf("%11s ", DecOct(strlen($p_filename)));
  1332. $v_typeflag = 'L';
  1333. $v_linkname = '';
  1334. $v_magic = '';
  1335. $v_version = '';
  1336. $v_uname = '';
  1337. $v_gname = '';
  1338. $v_devmajor = '';
  1339. $v_devminor = '';
  1340. $v_prefix = '';
  1341. $v_binary_data_first = pack("a100a8a8a8a12A12",
  1342. '././@LongLink', 0, 0, 0, $v_size, 0);
  1343. $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12",
  1344. $v_typeflag, $v_linkname, $v_magic,
  1345. $v_version, $v_uname, $v_gname,
  1346. $v_devmajor, $v_devminor, $v_prefix, '');
  1347. // ----- Calculate the checksum
  1348. $v_checksum = 0;
  1349. // ..... First part of the header
  1350. for ($i=0; $i<148; $i++)
  1351. $v_checksum += ord(substr($v_binary_data_first,$i,1));
  1352. // ..... Ignore the checksum value and replace it by ' ' (space)
  1353. for ($i=148; $i<156; $i++)
  1354. $v_checksum += ord(' ');
  1355. // ..... Last part of the header
  1356. for ($i=156, $j=0; $i<512; $i++, $j++)
  1357. $v_checksum += ord(substr($v_binary_data_last,$j,1));
  1358. // ----- Write the first 148 bytes of the header in the archive
  1359. $this->_writeBlock($v_binary_data_first, 148);
  1360. // ----- Write the calculated checksum
  1361. $v_checksum = sprintf("%6s ", DecOct($v_checksum));
  1362. $v_binary_data = pack("a8", $v_checksum);
  1363. $this->_writeBlock($v_binary_data, 8);
  1364. // ----- Write the last 356 bytes of the header in the archive
  1365. $this->_writeBlock($v_binary_data_last, 356);
  1366. // ----- Write the filename as content of the block
  1367. $i=0;
  1368. w

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