PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / league / flysystem-ziparchive / src / ZipArchiveAdapter.php

ZipArchiveAdapter.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/league/flysystem-ziparchive/src/ZipArchiveAdapter.php

321 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace League\Flysystem\ZipArchive;
4
5 use League\Flysystem\Adapter\AbstractAdapter;
6 use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
7 use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait;
8 use League\Flysystem\Adapter\Polyfill\StreamedWritingTrait;
9 use League\Flysystem\Config;
10 use League\Flysystem\Util;
11 use LogicException;
12 use ZipArchive;
13
14 class ZipArchiveAdapter extends AbstractAdapter
15 {
16 use StreamedWritingTrait;
17 use StreamedCopyTrait;
18 use NotSupportingVisibilityTrait;
19
20 /**
21 * @var array
22 */
23 protected static $resultMap = [
24 'size' => 'size',
25 'mtime' => 'timestamp',
26 'name' => 'path',
27 ];
28
29 /**
30 * @var ZipArchive
31 */
32 protected $archive;
33
34 /**
35 * @param $location
36 * @param ZipArchive $archive
37 * @param string|null $prefix
38 */
39 public function __construct($location, ZipArchive $archive = null, $prefix = null)
40 {
41 $this->setArchive($archive ?: new ZipArchive());
42 $this->openArchive($location);
43 $this->setPathPrefix($prefix);
44 }
45
46 /**
47 * Re-open an archive to ensure persistence.
48 */
49 protected function reopenArchive()
50 {
51 $path = $this->archive->filename;
52 $this->archive->close();
53 $this->openArchive($path);
54 }
55
56 /**
57 * ZipArchive setter.
58 *
59 * @param ZipArchive $archive
60 */
61 public function setArchive(ZipArchive $archive)
62 {
63 $this->archive = $archive;
64 }
65
66 /**
67 * Get the used ZipArchive.
68 *
69 * @return ZipArchive
70 */
71 public function getArchive()
72 {
73 return $this->archive;
74 }
75
76 /**
77 * Open a zip file.
78 *
79 * @param $location
80 */
81 public function openArchive($location)
82 {
83 $location = str_replace('/', DIRECTORY_SEPARATOR, $location);
84
85 if (($response = $this->archive->open($location, ZipArchive::CREATE)) !== true) {
86 throw new LogicException('Could not open zip archive at:'.$location.', error: '.$response);
87 }
88 }
89
90 /**
91 * {@inheritdoc}
92 */
93 public function write($path, $contents, Config $config)
94 {
95 $location = $this->applyPathPrefix($path);
96 $dirname = Util::dirname($path);
97
98 if (! empty($dirname) && ! $this->has($dirname)) {
99 $this->createDir($dirname, $config);
100 }
101
102 if (! $this->archive->addFromString($location, $contents)) {
103 return false;
104 }
105
106 $result = compact('path', 'contents');
107
108 if ($config && $config->get('visibility')) {
109 throw new LogicException(get_class($this).' does not support visibility settings.');
110 }
111
112 return $result;
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function update($path, $contents, Config $config)
119 {
120 $this->delete($path);
121
122 return $this->write($path, $contents, $config);
123 }
124
125 /**
126 * {@inheritdoc}
127 */
128 public function rename($path, $newpath)
129 {
130 $source = $this->applyPathPrefix($path);
131 $destination = $this->applyPathPrefix($newpath);
132
133 return $this->archive->renameName($source, $destination);
134 }
135
136 /**
137 * {@inheritdoc}
138 */
139 public function delete($path)
140 {
141 $location = $this->applyPathPrefix($path);
142
143 return $this->archive->deleteName($location);
144 }
145
146 /**
147 * {@inheritdoc}
148 */
149 public function deleteDir($dirname)
150 {
151 // This is needed to ensure the right number of
152 // files are set to the $numFiles property.
153 $this->reopenArchive();
154
155 $location = $this->applyPathPrefix($dirname);
156 $path = Util::normalizePrefix($location, '/');
157 $length = strlen($path);
158
159 for ($i = 0; $i < $this->archive->numFiles; $i++) {
160 $info = $this->archive->statIndex($i);
161
162 if (substr($info['name'], 0, $length) === $path) {
163 $this->archive->deleteIndex($i);
164 }
165 }
166
167 return $this->archive->deleteName($dirname);
168 }
169
170 /**
171 * {@inheritdoc}
172 */
173 public function createDir($dirname, Config $config)
174 {
175 if (! $this->has($dirname)) {
176 $location = $this->applyPathPrefix($dirname);
177 $this->archive->addEmptyDir($location);
178 }
179
180 return ['path' => $dirname];
181 }
182
183 /**
184 * {@inheritdoc}
185 */
186 public function has($path)
187 {
188 return $this->getMetadata($path);
189 }
190
191 /**
192 * {@inheritdoc}
193 */
194 public function read($path)
195 {
196 $this->reopenArchive();
197 $location = $this->applyPathPrefix($path);
198
199 if (! $contents = $this->archive->getFromName($location)) {
200 return false;
201 }
202
203 return compact('contents');
204 }
205
206 /**
207 * {@inheritdoc}
208 */
209 public function readStream($path)
210 {
211 $this->reopenArchive();
212 $location = $this->applyPathPrefix($path);
213
214 if (! $stream = $this->archive->getStream($location)) {
215 return false;
216 }
217
218 return compact('stream');
219 }
220
221 /**
222 * {@inheritdoc}
223 */
224 public function listContents($dirname = '', $recursive = false)
225 {
226 $result = [];
227
228 // This is needed to ensure the right number of
229 // files are set to the $numFiles property.
230 $this->reopenArchive();
231
232 for ($i = 0; $i < $this->archive->numFiles; $i++) {
233 if ($info = $this->archive->statIndex($i)) {
234 $result[] = $info;
235 }
236 }
237
238 $pathPrefix = $this->getPathPrefix();
239 $prefixLength = strlen($pathPrefix);
240
241 return array_filter(array_map(function ($item) use ($pathPrefix, $prefixLength) {
242 if ($pathPrefix && (substr($item['name'], 0, $prefixLength) !== $pathPrefix || $item['name'] === $pathPrefix)) {
243 return false;
244 }
245
246 return $this->normalizeObject($item);
247 }, $result));
248 }
249
250 /**
251 * {@inheritdoc}
252 */
253 public function getMetadata($path)
254 {
255 $location = $this->applyPathPrefix($path);
256
257 if (! $info = $this->archive->statName($location)) {
258
259 // Check if $location is a directory.
260 if (! $info = $this->archive->statName($location . '/')) {
261 return false;
262 }
263 }
264
265 return $this->normalizeObject($info);
266 }
267
268 /**
269 * Normalize a zip response array.
270 *
271 * @param array $object
272 *
273 * @return array
274 */
275 protected function normalizeObject(array $object)
276 {
277 if (substr($object['name'], -1) === '/') {
278 return [
279 'path' => $this->removePathPrefix(trim($object['name'], '/')),
280 'type' => 'dir',
281 ];
282 }
283
284 $result = ['type' => 'file'];
285 $normalised = Util::map($object, static::$resultMap);
286 $normalised['path'] = $this->removePathPrefix($normalised['path']);
287
288 return array_merge($result, $normalised);
289 }
290
291 /**
292 * {@inheritdoc}
293 */
294 public function getSize($path)
295 {
296 return $this->getMetadata($path);
297 }
298
299 /**
300 * {@inheritdoc}
301 */
302 public function getMimetype($path)
303 {
304 if (! $data = $this->read($path)) {
305 return false;
306 }
307
308 $data['mimetype'] = Util::guessMimeType($path, $data['contents']);
309
310 return $data;
311 }
312
313 /**
314 * {@inheritdoc}
315 */
316 public function getTimestamp($path)
317 {
318 return $this->getMetadata($path);
319 }
320 }
321