Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
archive.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Archive
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 jimport('joomla.filesystem.file');
13 jimport('joomla.filesystem.folder');
14 
15 /**
16  * An Archive handling class
17  *
18  * @package Joomla.Platform
19  * @subpackage Archive
20  * @since 11.1
21  */
22 class JArchive
23 {
24  /**
25  * @var array The array of instantiated archive adapters.
26  * @since 12.1
27  */
28  protected static $adapters = array();
29 
30  /**
31  * Extract an archive file to a directory.
32  *
33  * @param string $archivename The name of the archive file
34  * @param string $extractdir Directory to unpack into
35  *
36  * @return boolean True for success
37  *
38  * @since 11.1
39  * @throws InvalidArgumentException
40  */
41  public static function extract($archivename, $extractdir)
42  {
43  $untar = false;
44  $result = false;
45  $ext = JFile::getExt(strtolower($archivename));
46 
47  // Check if a tar is embedded...gzip/bzip2 can just be plain files!
48  if (JFile::getExt(JFile::stripExt(strtolower($archivename))) == 'tar')
49  {
50  $untar = true;
51  }
52 
53  switch ($ext)
54  {
55  case 'zip':
56  $adapter = self::getAdapter('zip');
57 
58  if ($adapter)
59  {
60  $result = $adapter->extract($archivename, $extractdir);
61  }
62  break;
63 
64  case 'tar':
65  $adapter = self::getAdapter('tar');
66 
67  if ($adapter)
68  {
69  $result = $adapter->extract($archivename, $extractdir);
70  }
71  break;
72 
73  case 'tgz':
74  // This format is a tarball gzip'd
75  $untar = true;
76 
77  case 'gz':
78  case 'gzip':
79  // This may just be an individual file (e.g. sql script)
80  $adapter = self::getAdapter('gzip');
81 
82  if ($adapter)
83  {
84  $config = JFactory::getConfig();
85  $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip');
86  $gzresult = $adapter->extract($archivename, $tmpfname);
87 
88  if ($gzresult instanceof Exception)
89  {
90  @unlink($tmpfname);
91 
92  return false;
93  }
94 
95  if ($untar)
96  {
97  // Try to untar the file
98  $tadapter = self::getAdapter('tar');
99 
100  if ($tadapter)
101  {
102  $result = $tadapter->extract($tmpfname, $extractdir);
103  }
104  }
105  else
106  {
107  $path = JPath::clean($extractdir);
108  JFolder::create($path);
109  $result = JFile::copy($tmpfname, $path . '/' . JFile::stripExt(basename(strtolower($archivename))), null, 1);
110  }
111 
112  @unlink($tmpfname);
113  }
114  break;
115 
116  case 'tbz2':
117  // This format is a tarball bzip2'd
118  $untar = true;
119 
120  case 'bz2':
121  case 'bzip2':
122  // This may just be an individual file (e.g. sql script)
123  $adapter = self::getAdapter('bzip2');
124 
125  if ($adapter)
126  {
127  $config = JFactory::getConfig();
128  $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2');
129  $bzresult = $adapter->extract($archivename, $tmpfname);
130 
131  if ($bzresult instanceof Exception)
132  {
133  @unlink($tmpfname);
134 
135  return false;
136  }
137 
138  if ($untar)
139  {
140  // Try to untar the file
141  $tadapter = self::getAdapter('tar');
142 
143  if ($tadapter)
144  {
145  $result = $tadapter->extract($tmpfname, $extractdir);
146  }
147  }
148  else
149  {
150  $path = JPath::clean($extractdir);
151  JFolder::create($path);
152  $result = JFile::copy($tmpfname, $path . '/' . JFile::stripExt(basename(strtolower($archivename))), null, 1);
153  }
154 
155  @unlink($tmpfname);
156  }
157  break;
158 
159  default:
160  throw new InvalidArgumentException('Unknown Archive Type');
161  }
162 
163  if (!$result || $result instanceof Exception)
164  {
165  return false;
166  }
167 
168  return true;
169  }
170 
171  /**
172  * Get a file compression adapter.
173  *
174  * @param string $type The type of adapter (bzip2|gzip|tar|zip).
175  *
176  * @return JArchiveExtractable Adapter for the requested type
177  *
178  * @since 11.1
179  * @throws UnexpectedValueException
180  */
181  public static function getAdapter($type)
182  {
183  if (!isset(self::$adapters[$type]))
184  {
185  // Try to load the adapter object
186  $class = 'JArchive' . ucfirst($type);
187 
188  if (!class_exists($class))
189  {
190  throw new UnexpectedValueException('Unable to load archive', 500);
191  }
192 
193  self::$adapters[$type] = new $class;
194  }
195 
196  return self::$adapters[$type];
197  }
198 }