PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 189 lines | 100 code | 15 blank | 74 comment | 20 complexity | d93636aa20382696cf032e5df85c0cee MD5 | raw file
  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 - 2008 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. * @author Louis Landry <louis.landry@joomla.org>
  26. * @package Joomla.Framework
  27. * @subpackage FileSystem
  28. * @since 1.5
  29. */
  30. class JArchiveTar extends JObject
  31. {
  32. /**
  33. * Tar file types.
  34. * @var array
  35. */
  36. var $_types = array (
  37. 0x0 => 'Unix file',
  38. 0x30 => 'File',
  39. 0x31 => 'Link',
  40. 0x32 => 'Symbolic link',
  41. 0x33 => 'Character special file',
  42. 0x34 => 'Block special file',
  43. 0x35 => 'Directory',
  44. 0x36 => 'FIFO special file',
  45. 0x37 => 'Contiguous file'
  46. );
  47. /**
  48. * Tar file flags.
  49. * @var array
  50. */
  51. var $_flags = array (
  52. 'FTEXT' => 0x01,
  53. 'FHCRC' => 0x02,
  54. 'FEXTRA' => 0x04,
  55. 'FNAME' => 0x08,
  56. 'FCOMMENT' => 0x10
  57. );
  58. /**
  59. * Tar file data buffer
  60. * @var string
  61. */
  62. var $_data = null;
  63. /**
  64. * Tar file metadata array
  65. * @var array
  66. */
  67. var $_metadata = null;
  68. /**
  69. * Extract a ZIP compressed file to a given path
  70. *
  71. * @access public
  72. * @param string $archive Path to ZIP archive to extract
  73. * @param string $destination Path to extract archive into
  74. * @param array $options Extraction options [unused]
  75. * @return boolean True if successful
  76. * @since 1.5
  77. */
  78. function extract($archive, $destination, $options = array ())
  79. {
  80. // Initialize variables
  81. $this->_data = null;
  82. $this->_metadata = null;
  83. if (!$this->_data = JFile::read($archive))
  84. {
  85. $this->set('error.message', 'Unable to read archive');
  86. return JError::raiseWarning(100, $this->get('error.message'));
  87. }
  88. if (!$this->_getTarInfo($this->_data))
  89. {
  90. return JError::raiseWarning(100, $this->get('error.message'));
  91. }
  92. for ($i=0,$n=count($this->_metadata);$i<$n;$i++)
  93. {
  94. $type = strtolower( $this->_metadata[$i]['type'] );
  95. if ($type == 'file' || $type == 'unix file')
  96. {
  97. $buffer = $this->_metadata[$i]['data'];
  98. $path = JPath::clean($destination.DS.$this->_metadata[$i]['name']);
  99. // Make sure the destination folder exists
  100. if (!JFolder::create(dirname($path)))
  101. {
  102. $this->set('error.message', 'Unable to create destination');
  103. return JError::raiseWarning(100, $this->get('error.message'));
  104. }
  105. if (JFile::write($path, $buffer) === false)
  106. {
  107. $this->set('error.message', 'Unable to write entry');
  108. return JError::raiseWarning(100, $this->get('error.message'));
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114. /**
  115. * Get the list of files/data from a Tar archive buffer.
  116. *
  117. * @access private
  118. * @param string $data The Tar archive buffer.
  119. * @return array Archive metadata array
  120. * <pre>
  121. * KEY: Position in the array
  122. * VALUES: 'attr' -- File attributes
  123. * 'data' -- Raw file contents
  124. * 'date' -- File modification time
  125. * 'name' -- Filename
  126. * 'size' -- Original file size
  127. * 'type' -- File type
  128. * </pre>
  129. * @since 1.5
  130. */
  131. function _getTarInfo(& $data)
  132. {
  133. $position = 0;
  134. $return_array = array ();
  135. while ($position < strlen($data))
  136. {
  137. $info = @ unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", substr($data, $position));
  138. if (!$info) {
  139. $this->set('error.message', 'Unable to decompress data');
  140. return false;
  141. }
  142. $position += 512;
  143. $contents = substr($data, $position, octdec($info['size']));
  144. $position += ceil(octdec($info['size']) / 512) * 512;
  145. if ($info['filename']) {
  146. $file = array (
  147. 'attr' => null,
  148. 'data' => null,
  149. 'date' => octdec($info['mtime']
  150. ), 'name' => trim($info['filename']), 'size' => octdec($info['size']), 'type' => isset ($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null);
  151. if (($info['typeflag'] == 0) || ($info['typeflag'] == 0x30) || ($info['typeflag'] == 0x35)) {
  152. /* File or folder. */
  153. $file['data'] = $contents;
  154. $mode = hexdec(substr($info['mode'], 4, 3));
  155. $file['attr'] = (($info['typeflag'] == 0x35) ? 'd' : '-') .
  156. (($mode & 0x400) ? 'r' : '-') .
  157. (($mode & 0x200) ? 'w' : '-') .
  158. (($mode & 0x100) ? 'x' : '-') .
  159. (($mode & 0x040) ? 'r' : '-') .
  160. (($mode & 0x020) ? 'w' : '-') .
  161. (($mode & 0x010) ? 'x' : '-') .
  162. (($mode & 0x004) ? 'r' : '-') .
  163. (($mode & 0x002) ? 'w' : '-') .
  164. (($mode & 0x001) ? 'x' : '-');
  165. } else {
  166. /* Some other type. */
  167. }
  168. $return_array[] = $file;
  169. }
  170. }
  171. $this->_metadata = $return_array;
  172. return true;
  173. }
  174. }