[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/archive/src/ -> Archive.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Archive Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Archive;
  10  
  11  use Joomla\Archive\Exception\UnknownArchiveException;
  12  use Joomla\Archive\Exception\UnsupportedArchiveException;
  13  use Joomla\Filesystem\File;
  14  use Joomla\Filesystem\Folder;
  15  
  16  /**
  17   * An Archive handling class
  18   *
  19   * @since  1.0
  20   */
  21  class Archive
  22  {
  23      /**
  24       * The array of instantiated archive adapters.
  25       *
  26       * @var    ExtractableInterface[]
  27       * @since  1.0
  28       */
  29      protected $adapters = [];
  30  
  31      /**
  32       * Holds the options array.
  33       *
  34       * @var    array|\ArrayAccess
  35       * @since  1.0
  36       */
  37      public $options = [];
  38  
  39      /**
  40       * Create a new Archive object.
  41       *
  42       * @param   array|\ArrayAccess  $options  An array of options
  43       *
  44       * @since   1.0
  45       * @throws  \InvalidArgumentException
  46       */
  47  	public function __construct($options = [])
  48      {
  49          if (!\is_array($options) && !($options instanceof \ArrayAccess))
  50          {
  51              throw new \InvalidArgumentException(
  52                  'The options param must be an array or implement the ArrayAccess interface.'
  53              );
  54          }
  55  
  56          // Make sure we have a tmp directory.
  57          isset($options['tmp_path']) || $options['tmp_path'] = realpath(sys_get_temp_dir());
  58  
  59          $this->options = $options;
  60      }
  61  
  62      /**
  63       * Extract an archive file to a directory.
  64       *
  65       * @param   string  $archivename  The name of the archive file
  66       * @param   string  $extractdir   Directory to unpack into
  67       *
  68       * @return  boolean  True for success
  69       *
  70       * @since   1.0
  71       * @throws  UnknownArchiveException if the archive type is not supported
  72       */
  73  	public function extract($archivename, $extractdir)
  74      {
  75          $ext      = pathinfo($archivename, \PATHINFO_EXTENSION);
  76          $path     = pathinfo($archivename, \PATHINFO_DIRNAME);
  77          $filename = pathinfo($archivename, \PATHINFO_FILENAME);
  78  
  79          switch (strtolower($ext))
  80          {
  81              case 'zip':
  82                  $result = $this->getAdapter('zip')->extract($archivename, $extractdir);
  83  
  84                  break;
  85  
  86              case 'tar':
  87                  $result = $this->getAdapter('tar')->extract($archivename, $extractdir);
  88  
  89                  break;
  90  
  91              case 'tgz':
  92              case 'gz':
  93              case 'gzip':
  94                  // This may just be an individual file (e.g. sql script)
  95                  $tmpfname = $this->options['tmp_path'] . '/' . uniqid('gzip');
  96  
  97                  try
  98                  {
  99                      $this->getAdapter('gzip')->extract($archivename, $tmpfname);
 100                  }
 101                  catch (\RuntimeException $exception)
 102                  {
 103                      @unlink($tmpfname);
 104  
 105                      return false;
 106                  }
 107  
 108                  if ($ext === 'tgz' || stripos($filename, '.tar') !== false)
 109                  {
 110                      $result = $this->getAdapter('tar')->extract($tmpfname, $extractdir);
 111                  }
 112                  else
 113                  {
 114                      Folder::create($extractdir);
 115                      $result = File::copy($tmpfname, $extractdir . '/' . $filename, null, false);
 116                  }
 117  
 118                  @unlink($tmpfname);
 119  
 120                  break;
 121  
 122              case 'tbz2':
 123              case 'bz2':
 124              case 'bzip2':
 125                  // This may just be an individual file (e.g. sql script)
 126                  $tmpfname = $this->options['tmp_path'] . '/' . uniqid('bzip2');
 127  
 128                  try
 129                  {
 130                      $this->getAdapter('bzip2')->extract($archivename, $tmpfname);
 131                  }
 132                  catch (\RuntimeException $exception)
 133                  {
 134                      @unlink($tmpfname);
 135  
 136                      return false;
 137                  }
 138  
 139                  if ($ext === 'tbz2' || stripos($filename, '.tar') !== false)
 140                  {
 141                      $result = $this->getAdapter('tar')->extract($tmpfname, $extractdir);
 142                  }
 143                  else
 144                  {
 145                      Folder::create($extractdir);
 146                      $result = File::copy($tmpfname, $extractdir . '/' . $filename, null, false);
 147                  }
 148  
 149                  @unlink($tmpfname);
 150  
 151                  break;
 152  
 153              default:
 154                  throw new UnknownArchiveException(sprintf('Unsupported archive type: %s', $ext));
 155          }
 156  
 157          return $result;
 158      }
 159  
 160      /**
 161       * Method to override the provided adapter with your own implementation.
 162       *
 163       * @param   string   $type      Name of the adapter to set.
 164       * @param   string   $class     FQCN of your class which implements ExtractableInterface.
 165       * @param   boolean  $override  True to force override the adapter type.
 166       *
 167       * @return  $this
 168       *
 169       * @since   1.0
 170       * @throws  UnsupportedArchiveException if the adapter type is not supported
 171       */
 172  	public function setAdapter($type, $class, $override = true)
 173      {
 174          if ($override || !isset($this->adapters[$type]))
 175          {
 176              if (!\is_object($class) && !class_exists($class))
 177              {
 178                  throw new UnsupportedArchiveException($type, sprintf('Archive adapter "%s" (class "%s") not found.', $type, $class));
 179              }
 180  
 181              if (!$class::isSupported())
 182              {
 183                  throw new UnsupportedArchiveException($type, sprintf('Archive adapter "%s" (class "%s") not supported.', $type, $class));
 184              }
 185  
 186              $object = new $class($this->options);
 187  
 188              if (!($object instanceof ExtractableInterface))
 189              {
 190                  throw new UnsupportedArchiveException(
 191                      $type,
 192                      sprintf(
 193                          'The provided adapter "%s" (class "%s") must implement %s',
 194                          $type,
 195                          $class,
 196                          ExtractableInterface::class
 197                      )
 198                  );
 199              }
 200  
 201              $this->adapters[$type] = $object;
 202          }
 203  
 204          return $this;
 205      }
 206  
 207      /**
 208       * Get a file compression adapter.
 209       *
 210       * @param   string  $type  The type of adapter (bzip2|gzip|tar|zip).
 211       *
 212       * @return  ExtractableInterface  Adapter for the requested type
 213       *
 214       * @since   1.0
 215       */
 216  	public function getAdapter($type)
 217      {
 218          $type = strtolower($type);
 219  
 220          if (!isset($this->adapters[$type]))
 221          {
 222              // Try to load the adapter object
 223              $this->setAdapter($type, __NAMESPACE__ . '\\' . ucfirst($type));
 224          }
 225  
 226          return $this->adapters[$type];
 227      }
 228  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer