PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filesystem/archive.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 189 lines | 140 code | 11 blank | 38 comment | 23 complexity | 6346b4b9cb3260b62d2253ee5cf3aeb6 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: archive.php 14401 2010-01-26 14:10:00Z louis $
  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. defined('JPATH_BASE') or die();
  15. /**
  16. * An Archive handling class
  17. *
  18. * @static
  19. * @package Joomla.Framework
  20. * @subpackage FileSystem
  21. * @since 1.5
  22. */
  23. class JArchive
  24. {
  25. /**
  26. * @param string The name of the archive file
  27. * @param string Directory to unpack into
  28. * @return boolean True for success
  29. */
  30. function extract( $archivename, $extractdir)
  31. {
  32. jimport('joomla.filesystem.file');
  33. jimport('joomla.filesystem.folder');
  34. $untar = false;
  35. $result = false;
  36. $ext = JFile::getExt(strtolower($archivename));
  37. // check if a tar is embedded...gzip/bzip2 can just be plain files!
  38. if (JFile::getExt(JFile::stripExt(strtolower($archivename))) == 'tar') {
  39. $untar = true;
  40. }
  41. switch ($ext)
  42. {
  43. case 'zip':
  44. $adapter =& JArchive::getAdapter('zip');
  45. if ($adapter) {
  46. $result = $adapter->extract($archivename, $extractdir);
  47. }
  48. break;
  49. case 'tar':
  50. $adapter =& JArchive::getAdapter('tar');
  51. if ($adapter) {
  52. $result = $adapter->extract($archivename, $extractdir);
  53. }
  54. break;
  55. case 'tgz' :
  56. $untar = true; // This format is a tarball gzip'd
  57. case 'gz' : // This may just be an individual file (e.g. sql script)
  58. case 'gzip' :
  59. $adapter =& JArchive::getAdapter('gzip');
  60. if ($adapter)
  61. {
  62. $config =& JFactory::getConfig();
  63. $tmpfname = $config->getValue('config.tmp_path').DS.uniqid('gzip');
  64. $gzresult = $adapter->extract($archivename, $tmpfname);
  65. if (JError::isError($gzresult))
  66. {
  67. @unlink($tmpfname);
  68. return false;
  69. }
  70. if($untar)
  71. {
  72. // Try to untar the file
  73. $tadapter =& JArchive::getAdapter('tar');
  74. if ($tadapter) {
  75. $result = $tadapter->extract($tmpfname, $extractdir);
  76. }
  77. }
  78. else
  79. {
  80. $path = JPath::clean($extractdir);
  81. JFolder::create($path);
  82. $result = JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  83. }
  84. @unlink($tmpfname);
  85. }
  86. break;
  87. case 'tbz2' :
  88. $untar = true; // This format is a tarball bzip2'd
  89. case 'bz2' : // This may just be an individual file (e.g. sql script)
  90. case 'bzip2':
  91. $adapter =& JArchive::getAdapter('bzip2');
  92. if ($adapter)
  93. {
  94. $config =& JFactory::getConfig();
  95. $tmpfname = $config->getValue('config.tmp_path').DS.uniqid('bzip2');
  96. $bzresult = $adapter->extract($archivename, $tmpfname);
  97. if (JError::isError($bzresult))
  98. {
  99. @unlink($tmpfname);
  100. return false;
  101. }
  102. if ($untar)
  103. {
  104. // Try to untar the file
  105. $tadapter =& JArchive::getAdapter('tar');
  106. if ($tadapter) {
  107. $result = $tadapter->extract($tmpfname, $extractdir);
  108. }
  109. }
  110. else
  111. {
  112. $path = JPath::clean($extractdir);
  113. JFolder::create($path);
  114. $result = JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  115. }
  116. @unlink($tmpfname);
  117. }
  118. break;
  119. default:
  120. JError::raiseWarning(10, JText::_('UNKNOWNARCHIVETYPE'));
  121. return false;
  122. break;
  123. }
  124. if (! $result || JError::isError($result)) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. function &getAdapter($type)
  130. {
  131. static $adapters;
  132. if (!isset($adapters)) {
  133. $adapters = array();
  134. }
  135. if (!isset($adapters[$type]))
  136. {
  137. // Try to load the adapter object
  138. $class = 'JArchive'.ucfirst($type);
  139. if (!class_exists($class))
  140. {
  141. $path = dirname(__FILE__).DS.'archive'.DS.strtolower($type).'.php';
  142. if (file_exists($path)) {
  143. require_once($path);
  144. } else {
  145. JError::raiseError(500,JText::_('Unable to load archive'));
  146. }
  147. }
  148. $adapters[$type] = new $class();
  149. }
  150. return $adapters[$type];
  151. }
  152. /**
  153. * @param string The name of the archive
  154. * @param mixed The name of a single file or an array of files
  155. * @param string The compression for the archive
  156. * @param string Path to add within the archive
  157. * @param string Path to remove within the archive
  158. * @param boolean Automatically append the extension for the archive
  159. * @param boolean Remove for source files
  160. */
  161. function create($archive, $files, $compress = 'tar', $addPath = '', $removePath = '', $autoExt = false, $cleanUp = false)
  162. {
  163. jimport( 'pear.archive_tar.Archive_Tar' );
  164. if (is_string($files)) {
  165. $files = array ($files);
  166. }
  167. if ($autoExt) {
  168. $archive .= '.'.$compress;
  169. }
  170. $tar = new Archive_Tar( $archive, $compress );
  171. $tar->setErrorHandling(PEAR_ERROR_PRINT);
  172. $tar->createModify( $files, $addPath, $removePath );
  173. if ($cleanUp) {
  174. JFile::delete( $files );
  175. }
  176. return $tar;
  177. }
  178. }