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

/libraries/joomla/filesystem/archive/tar.php

https://bitbucket.org/asosso/joomla15
PHP | 188 lines | 100 code | 15 blank | 73 comment | 20 complexity | 1dbc9947f0fc2e8318af8bfd34751d10 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id:tar.php 6961 2007-03-15 16:06:53Z tcp $
  4. * @package Joomla.Framework
  5. * @subpackage FileSystem
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Tar format adapter for the JArchive class
  18. *
  19. * This class is inspired from and draws heavily in code and concept from the Compress package of
  20. * The Horde Project <http://www.horde.org>
  21. *
  22. * @contributor Michael Slusarz <slusarz@horde.org>
  23. * @contributor Michael Cochrane <mike@graftonhall.co.nz>
  24. *
  25. * @package Joomla.Framework
  26. * @subpackage FileSystem
  27. * @since 1.5
  28. */
  29. class JArchiveTar extends JObject
  30. {
  31. /**
  32. * Tar file types.
  33. * @var array
  34. */
  35. var $_types = array (
  36. 0x0 => 'Unix file',
  37. 0x30 => 'File',
  38. 0x31 => 'Link',
  39. 0x32 => 'Symbolic link',
  40. 0x33 => 'Character special file',
  41. 0x34 => 'Block special file',
  42. 0x35 => 'Directory',
  43. 0x36 => 'FIFO special file',
  44. 0x37 => 'Contiguous file'
  45. );
  46. /**
  47. * Tar file flags.
  48. * @var array
  49. */
  50. var $_flags = array (
  51. 'FTEXT' => 0x01,
  52. 'FHCRC' => 0x02,
  53. 'FEXTRA' => 0x04,
  54. 'FNAME' => 0x08,
  55. 'FCOMMENT' => 0x10
  56. );
  57. /**
  58. * Tar file data buffer
  59. * @var string
  60. */
  61. var $_data = null;
  62. /**
  63. * Tar file metadata array
  64. * @var array
  65. */
  66. var $_metadata = null;
  67. /**
  68. * Extract a ZIP compressed file to a given path
  69. *
  70. * @access public
  71. * @param string $archive Path to ZIP archive to extract
  72. * @param string $destination Path to extract archive into
  73. * @param array $options Extraction options [unused]
  74. * @return boolean True if successful
  75. * @since 1.5
  76. */
  77. function extract($archive, $destination, $options = array ())
  78. {
  79. // Initialize variables
  80. $this->_data = null;
  81. $this->_metadata = null;
  82. if (!$this->_data = JFile::read($archive))
  83. {
  84. $this->set('error.message', 'Unable to read archive');
  85. return JError::raiseWarning(100, $this->get('error.message'));
  86. }
  87. if (!$this->_getTarInfo($this->_data))
  88. {
  89. return JError::raiseWarning(100, $this->get('error.message'));
  90. }
  91. for ($i=0,$n=count($this->_metadata);$i<$n;$i++)
  92. {
  93. $type = strtolower( $this->_metadata[$i]['type'] );
  94. if ($type == 'file' || $type == 'unix file')
  95. {
  96. $buffer = $this->_metadata[$i]['data'];
  97. $path = JPath::clean($destination.DS.$this->_metadata[$i]['name']);
  98. // Make sure the destination folder exists
  99. if (!JFolder::create(dirname($path)))
  100. {
  101. $this->set('error.message', 'Unable to create destination');
  102. return JError::raiseWarning(100, $this->get('error.message'));
  103. }
  104. if (JFile::write($path, $buffer) === false)
  105. {
  106. $this->set('error.message', 'Unable to write entry');
  107. return JError::raiseWarning(100, $this->get('error.message'));
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. /**
  114. * Get the list of files/data from a Tar archive buffer.
  115. *
  116. * @access private
  117. * @param string $data The Tar archive buffer.
  118. * @return array Archive metadata array
  119. * <pre>
  120. * KEY: Position in the array
  121. * VALUES: 'attr' -- File attributes
  122. * 'data' -- Raw file contents
  123. * 'date' -- File modification time
  124. * 'name' -- Filename
  125. * 'size' -- Original file size
  126. * 'type' -- File type
  127. * </pre>
  128. * @since 1.5
  129. */
  130. function _getTarInfo(& $data)
  131. {
  132. $position = 0;
  133. $return_array = array ();
  134. while ($position < strlen($data))
  135. {
  136. $info = @ unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", substr($data, $position));
  137. if (!$info) {
  138. $this->set('error.message', 'Unable to decompress data');
  139. return false;
  140. }
  141. $position += 512;
  142. $contents = substr($data, $position, octdec($info['size']));
  143. $position += ceil(octdec($info['size']) / 512) * 512;
  144. if ($info['filename']) {
  145. $file = array (
  146. 'attr' => null,
  147. 'data' => null,
  148. 'date' => octdec($info['mtime']
  149. ), 'name' => trim($info['filename']), 'size' => octdec($info['size']), 'type' => isset ($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null);
  150. if (($info['typeflag'] == 0) || ($info['typeflag'] == 0x30) || ($info['typeflag'] == 0x35)) {
  151. /* File or folder. */
  152. $file['data'] = $contents;
  153. $mode = hexdec(substr($info['mode'], 4, 3));
  154. $file['attr'] = (($info['typeflag'] == 0x35) ? 'd' : '-') .
  155. (($mode & 0x400) ? 'r' : '-') .
  156. (($mode & 0x200) ? 'w' : '-') .
  157. (($mode & 0x100) ? 'x' : '-') .
  158. (($mode & 0x040) ? 'r' : '-') .
  159. (($mode & 0x020) ? 'w' : '-') .
  160. (($mode & 0x010) ? 'x' : '-') .
  161. (($mode & 0x004) ? 'r' : '-') .
  162. (($mode & 0x002) ? 'w' : '-') .
  163. (($mode & 0x001) ? 'x' : '-');
  164. } else {
  165. /* Some other type. */
  166. }
  167. $return_array[] = $file;
  168. }
  169. }
  170. $this->_metadata = $return_array;
  171. return true;
  172. }
  173. }