/libraries/joomla/filesystem/archive.php

https://github.com/mathc/joomla-cms · PHP · 192 lines · 120 code · 33 blank · 39 comment · 22 complexity · 76de67f3d744a127287e6416c40d20cc MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage FileSystem
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * An Archive handling class
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage FileSystem
  15. * @since 11.1
  16. */
  17. class JArchive
  18. {
  19. /**
  20. * Extract an archive file to a directory.
  21. *
  22. * @param string $archivename The name of the archive file
  23. * @param string $extractdir Directory to unpack into
  24. *
  25. * @return boolean True for success
  26. * @since 11.1
  27. */
  28. public static function extract($archivename, $extractdir)
  29. {
  30. jimport('joomla.filesystem.file');
  31. jimport('joomla.filesystem.folder');
  32. $untar = false;
  33. $result = false;
  34. $ext = JFile::getExt(strtolower($archivename));
  35. // Check if a tar is embedded...gzip/bzip2 can just be plain files!
  36. if (JFile::getExt(JFile::stripExt(strtolower($archivename))) == 'tar') {
  37. $untar = true;
  38. }
  39. switch ($ext)
  40. {
  41. case 'zip':
  42. $adapter = JArchive::getAdapter('zip');
  43. if ($adapter) {
  44. $result = $adapter->extract($archivename, $extractdir);
  45. }
  46. break;
  47. case 'tar':
  48. $adapter = JArchive::getAdapter('tar');
  49. if ($adapter) {
  50. $result = $adapter->extract($archivename, $extractdir);
  51. }
  52. break;
  53. case 'tgz':
  54. // This format is a tarball gzip'd
  55. $untar = true;
  56. case 'gz':
  57. case 'gzip':
  58. // This may just be an individual file (e.g. sql script)
  59. $adapter = JArchive::getAdapter('gzip');
  60. if ($adapter) {
  61. $config = JFactory::getConfig();
  62. $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip');
  63. $gzresult = $adapter->extract($archivename, $tmpfname);
  64. if (JError::isError($gzresult)) {
  65. @unlink($tmpfname);
  66. return false;
  67. }
  68. if ($untar) {
  69. // Try to untar the file
  70. $tadapter = JArchive::getAdapter('tar');
  71. if ($tadapter) {
  72. $result = $tadapter->extract($tmpfname, $extractdir);
  73. }
  74. }
  75. else {
  76. $path = JPath::clean($extractdir);
  77. JFolder::create($path);
  78. $result = JFile::copy(
  79. $tmpfname,
  80. $path . '/' . JFile::stripExt(JFile::getName(strtolower($archivename))), null, 1
  81. );
  82. }
  83. @unlink($tmpfname);
  84. }
  85. break;
  86. case 'tbz2' :
  87. // This format is a tarball bzip2'd
  88. $untar = true;
  89. case 'bz2' :
  90. case 'bzip2':
  91. // This may just be an individual file (e.g. sql script)
  92. $adapter = JArchive::getAdapter('bzip2');
  93. if ($adapter) {
  94. $config = JFactory::getConfig();
  95. $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2');
  96. $bzresult = $adapter->extract($archivename, $tmpfname);
  97. if (JError::isError($bzresult)) {
  98. @unlink($tmpfname);
  99. return false;
  100. }
  101. if ($untar) {
  102. // Try to untar the file
  103. $tadapter = JArchive::getAdapter('tar');
  104. if ($tadapter) {
  105. $result = $tadapter->extract($tmpfname, $extractdir);
  106. }
  107. }
  108. else {
  109. $path = JPath::clean($extractdir);
  110. JFolder::create($path);
  111. $result = JFile::copy(
  112. $tmpfname,
  113. $path . '/' . JFile::stripExt(JFile::getName(strtolower($archivename))), null, 1
  114. );
  115. }
  116. @unlink($tmpfname);
  117. }
  118. break;
  119. default:
  120. JError::raiseWarning(10, JText::_('JLIB_FILESYSTEM_UNKNOWNARCHIVETYPE'));
  121. return false;
  122. break;
  123. }
  124. if (! $result || JError::isError($result)) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * Get a file compression adapter.
  131. *
  132. * @param string $type The type of adapter (bzip2|gzip|tar|zip).
  133. *
  134. * @return object JObject
  135. * @since 11.1
  136. */
  137. public static function getAdapter($type)
  138. {
  139. static $adapters;
  140. if (!isset($adapters)) {
  141. $adapters = array();
  142. }
  143. if (!isset($adapters[$type])) {
  144. // Try to load the adapter object
  145. $class = 'JArchive'.ucfirst($type);
  146. if (!class_exists($class)) {
  147. $path = dirname(__FILE__) . '/archive/' . strtolower($type).'.php';
  148. if (file_exists($path)) {
  149. require_once $path;
  150. }
  151. else {
  152. JError::raiseError(500, JText::_('JLIB_FILESYSTEM_UNABLE_TO_LOAD_ARCHIVE'));
  153. }
  154. }
  155. $adapters[$type] = new $class;
  156. }
  157. return $adapters[$type];
  158. }
  159. }