Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.32% |
117 / 119 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
ZipArchive | |
98.32% |
117 / 119 |
|
91.67% |
11 / 12 |
43 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
__call | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
open | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
close | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
extractTo | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getFromName | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
pclzipAddFile | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
5.02 | |||
pclzipAddFromString | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
pclzipExtractTo | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
pclzipGetFromName | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
pclzipGetNameIndex | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
pclzipLocateName | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
5 | * word processing documents. |
6 | * |
7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
8 | * General Public License version 3 as published by the Free Software Foundation. |
9 | * |
10 | * For the full copyright and license information, please read the LICENSE |
11 | * file that was distributed with this source code. For the full list of |
12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
13 | * |
14 | * @see https://github.com/PHPOffice/PHPWord |
15 | * |
16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
17 | */ |
18 | |
19 | namespace PhpOffice\PhpWord\Shared; |
20 | |
21 | use PclZip; |
22 | use PhpOffice\PhpWord\Exception\Exception; |
23 | use PhpOffice\PhpWord\Settings; |
24 | use Throwable; |
25 | |
26 | /** |
27 | * ZipArchive wrapper. |
28 | * |
29 | * Wraps zip archive functionality of PHP ZipArchive and PCLZip. PHP ZipArchive |
30 | * properties and methods are bypassed and used as the model for the PCLZip |
31 | * emulation. Only needed PHP ZipArchive features are implemented. |
32 | * |
33 | * @method bool addFile(string $filename, string $localname = null) |
34 | * @method bool addFromString(string $localname, string $contents) |
35 | * @method false|string getNameIndex(int $index) |
36 | * @method false|int locateName(string $name) |
37 | * |
38 | * @since 0.10.0 |
39 | */ |
40 | class ZipArchive |
41 | { |
42 | /** @const int Flags for open method */ |
43 | const CREATE = 1; // Emulate \ZipArchive::CREATE |
44 | const OVERWRITE = 8; // Emulate \ZipArchive::OVERWRITE |
45 | |
46 | /** |
47 | * Number of files (emulate ZipArchive::$numFiles). |
48 | * |
49 | * @var int |
50 | */ |
51 | public $numFiles = 0; |
52 | |
53 | /** |
54 | * Archive filename (emulate ZipArchive::$filename). |
55 | * |
56 | * @var string |
57 | */ |
58 | public $filename; |
59 | |
60 | /** |
61 | * Temporary storage directory. |
62 | * |
63 | * @var string |
64 | */ |
65 | private $tempDir; |
66 | |
67 | /** |
68 | * Internal zip archive object. |
69 | * |
70 | * @var PclZip|\ZipArchive |
71 | */ |
72 | private $zip; |
73 | |
74 | /** |
75 | * Use PCLZip (default behaviour). |
76 | * |
77 | * @var bool |
78 | */ |
79 | private $usePclzip = true; |
80 | |
81 | /** |
82 | * Create new instance. |
83 | */ |
84 | public function __construct() |
85 | { |
86 | $this->usePclzip = (Settings::getZipClass() != 'ZipArchive'); |
87 | if ($this->usePclzip) { |
88 | if (!defined('PCLZIP_TEMPORARY_DIR')) { |
89 | define('PCLZIP_TEMPORARY_DIR', Settings::getTempDir() . '/'); |
90 | } |
91 | require_once 'PCLZip/pclzip.lib.php'; |
92 | } |
93 | } |
94 | |
95 | /** |
96 | * Catch function calls: pass to ZipArchive or PCLZip. |
97 | * |
98 | * `call_user_func_array` can only used for public function, hence the `public` in all `pcl...` methods |
99 | * |
100 | * @param mixed $function |
101 | * @param mixed $args |
102 | * |
103 | * @return mixed |
104 | */ |
105 | public function __call($function, $args) |
106 | { |
107 | // Set object and function |
108 | $zipFunction = $function; |
109 | if (!$this->usePclzip) { |
110 | $zipObject = $this->zip; |
111 | } else { |
112 | $zipObject = $this; |
113 | $zipFunction = "pclzip{$zipFunction}"; |
114 | } |
115 | |
116 | // Run function |
117 | $result = false; |
118 | if (method_exists($zipObject, $zipFunction)) { |
119 | $result = @call_user_func_array([$zipObject, $zipFunction], $args); |
120 | } |
121 | |
122 | return $result; |
123 | } |
124 | |
125 | /** |
126 | * Open a new zip archive. |
127 | * |
128 | * @param string $filename The file name of the ZIP archive to open |
129 | * @param int $flags The mode to use to open the archive |
130 | * |
131 | * @return bool |
132 | */ |
133 | public function open($filename, $flags = null) |
134 | { |
135 | $result = true; |
136 | $this->filename = $filename; |
137 | $this->tempDir = Settings::getTempDir(); |
138 | |
139 | if (!$this->usePclzip) { |
140 | $zip = new \ZipArchive(); |
141 | |
142 | // PHP 8.1 compat - passing null as second arg to \ZipArchive::open() is deprecated |
143 | // passing 0 achieves the same behaviour |
144 | if ($flags === null) { |
145 | $flags = 0; |
146 | } |
147 | |
148 | $result = $zip->open($this->filename, $flags); |
149 | |
150 | // Scrutizer will report the property numFiles does not exist |
151 | // See https://github.com/scrutinizer-ci/php-analyzer/issues/190 |
152 | $this->numFiles = $zip->numFiles; |
153 | } else { |
154 | $zip = new PclZip($this->filename); |
155 | $zipContent = $zip->listContent(); |
156 | $this->numFiles = is_array($zipContent) ? count($zipContent) : 0; |
157 | } |
158 | $this->zip = $zip; |
159 | |
160 | return $result; |
161 | } |
162 | |
163 | /** |
164 | * Close the active archive. |
165 | * |
166 | * @return bool |
167 | */ |
168 | public function close() |
169 | { |
170 | if (!$this->usePclzip) { |
171 | try { |
172 | $result = @$this->zip->close(); |
173 | } catch (Throwable $e) { |
174 | $result = false; |
175 | } |
176 | if ($result === false) { |
177 | throw new Exception("Could not close zip file {$this->filename}: "); |
178 | } |
179 | } |
180 | |
181 | return true; |
182 | } |
183 | |
184 | /** |
185 | * Extract the archive contents (emulate \ZipArchive). |
186 | * |
187 | * @param string $destination |
188 | * @param array|string $entries |
189 | * |
190 | * @return bool |
191 | * |
192 | * @since 0.10.0 |
193 | */ |
194 | public function extractTo($destination, $entries = null) |
195 | { |
196 | if (!is_dir($destination)) { |
197 | return false; |
198 | } |
199 | |
200 | if (!$this->usePclzip) { |
201 | return $this->zip->extractTo($destination, $entries); |
202 | } |
203 | |
204 | return $this->pclzipExtractTo($destination, $entries); |
205 | } |
206 | |
207 | /** |
208 | * Extract file from archive by given file name (emulate \ZipArchive). |
209 | * |
210 | * @param string $filename Filename for the file in zip archive |
211 | * |
212 | * @return string $contents File string contents |
213 | */ |
214 | public function getFromName($filename) |
215 | { |
216 | if (!$this->usePclzip) { |
217 | $contents = $this->zip->getFromName($filename); |
218 | if ($contents === false) { |
219 | $filename = substr($filename, 1); |
220 | $contents = $this->zip->getFromName($filename); |
221 | } |
222 | } else { |
223 | $contents = $this->pclzipGetFromName($filename); |
224 | } |
225 | |
226 | return $contents; |
227 | } |
228 | |
229 | /** |
230 | * Add a new file to the zip archive (emulate \ZipArchive). |
231 | * |
232 | * @param string $filename Directory/Name of the file to add to the zip archive |
233 | * @param string $localname Directory/Name of the file added to the zip |
234 | * |
235 | * @return bool |
236 | */ |
237 | public function pclzipAddFile($filename, $localname = null) |
238 | { |
239 | /** @var PclZip $zip Type hint */ |
240 | $zip = $this->zip; |
241 | |
242 | // Bugfix GH-261 https://github.com/PHPOffice/PHPWord/pull/261 |
243 | $realpathFilename = realpath($filename); |
244 | if ($realpathFilename !== false) { |
245 | $filename = $realpathFilename; |
246 | } |
247 | |
248 | $filenameParts = pathinfo($filename); |
249 | $localnameParts = pathinfo($localname); |
250 | |
251 | // To Rename the file while adding it to the zip we |
252 | // need to create a temp file with the correct name |
253 | $tempFile = false; |
254 | if ($filenameParts['basename'] != $localnameParts['basename']) { |
255 | $tempFile = true; // temp file created |
256 | $temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename']; |
257 | copy($filename, $temppath); |
258 | $filename = $temppath; |
259 | $filenameParts = pathinfo($temppath); |
260 | } |
261 | |
262 | $pathRemoved = $filenameParts['dirname']; |
263 | $pathAdded = $localnameParts['dirname']; |
264 | |
265 | if (!$this->usePclzip) { |
266 | $pathAdded = $pathAdded . '/' . ltrim(str_replace('\\', '/', substr($filename, strlen($pathRemoved))), '/'); |
267 | //$res = $zip->addFile($filename, $pathAdded); |
268 | $res = $zip->addFromString($pathAdded, file_get_contents($filename)); // addFile can't use subfolders in some cases |
269 | } else { |
270 | $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded); |
271 | } |
272 | |
273 | if ($tempFile) { |
274 | // Remove temp file, if created |
275 | unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename']); |
276 | } |
277 | |
278 | return $res != 0; |
279 | } |
280 | |
281 | /** |
282 | * Add a new file to the zip archive from a string of raw data (emulate \ZipArchive). |
283 | * |
284 | * @param string $localname Directory/Name of the file to add to the zip archive |
285 | * @param string $contents String of data to add to the zip archive |
286 | * |
287 | * @return bool |
288 | */ |
289 | public function pclzipAddFromString($localname, $contents) |
290 | { |
291 | /** @var PclZip $zip Type hint */ |
292 | $zip = $this->zip; |
293 | $filenameParts = pathinfo($localname); |
294 | |
295 | // Write $contents to a temp file |
296 | $handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'], 'wb'); |
297 | if ($handle) { |
298 | fwrite($handle, $contents); |
299 | fclose($handle); |
300 | } |
301 | |
302 | // Add temp file to zip |
303 | $filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']; |
304 | $pathRemoved = $this->tempDir; |
305 | $pathAdded = $filenameParts['dirname']; |
306 | |
307 | $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded); |
308 | |
309 | // Remove temp file |
310 | @unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']); |
311 | |
312 | return $res != 0; |
313 | } |
314 | |
315 | /** |
316 | * Extract the archive contents (emulate \ZipArchive). |
317 | * |
318 | * @param string $destination |
319 | * @param array|string $entries |
320 | * |
321 | * @return bool |
322 | * |
323 | * @since 0.10.0 |
324 | */ |
325 | public function pclzipExtractTo($destination, $entries = null) |
326 | { |
327 | /** @var PclZip $zip Type hint */ |
328 | $zip = $this->zip; |
329 | |
330 | // Extract all files |
331 | if (null === $entries) { |
332 | $result = $zip->extract(PCLZIP_OPT_PATH, $destination); |
333 | |
334 | return $result > 0; |
335 | } |
336 | |
337 | // Extract by entries |
338 | if (!is_array($entries)) { |
339 | $entries = [$entries]; |
340 | } |
341 | foreach ($entries as $entry) { |
342 | $entryIndex = $this->locateName($entry); |
343 | $result = $zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination); |
344 | if ($result <= 0) { |
345 | return false; |
346 | } |
347 | } |
348 | |
349 | return true; |
350 | } |
351 | |
352 | /** |
353 | * Extract file from archive by given file name (emulate \ZipArchive). |
354 | * |
355 | * @param string $filename Filename for the file in zip archive |
356 | * |
357 | * @return string $contents File string contents |
358 | */ |
359 | public function pclzipGetFromName($filename) |
360 | { |
361 | /** @var PclZip $zip Type hint */ |
362 | $zip = $this->zip; |
363 | $listIndex = $this->pclzipLocateName($filename); |
364 | $contents = false; |
365 | |
366 | if ($listIndex !== false) { |
367 | $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); |
368 | } else { |
369 | $filename = substr($filename, 1); |
370 | $listIndex = $this->pclzipLocateName($filename); |
371 | $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); |
372 | } |
373 | if ((is_array($extracted)) && ($extracted != 0)) { |
374 | $contents = $extracted[0]['content']; |
375 | } |
376 | |
377 | return $contents; |
378 | } |
379 | |
380 | /** |
381 | * Returns the name of an entry using its index (emulate \ZipArchive). |
382 | * |
383 | * @param int $index |
384 | * |
385 | * @return bool|string |
386 | * |
387 | * @since 0.10.0 |
388 | */ |
389 | public function pclzipGetNameIndex($index) |
390 | { |
391 | /** @var PclZip $zip Type hint */ |
392 | $zip = $this->zip; |
393 | $list = $zip->listContent(); |
394 | if (isset($list[$index])) { |
395 | return $list[$index]['filename']; |
396 | } |
397 | |
398 | return false; |
399 | } |
400 | |
401 | /** |
402 | * Returns the index of the entry in the archive (emulate \ZipArchive). |
403 | * |
404 | * @param string $filename Filename for the file in zip archive |
405 | * |
406 | * @return false|int |
407 | */ |
408 | public function pclzipLocateName($filename) |
409 | { |
410 | /** @var PclZip $zip Type hint */ |
411 | $zip = $this->zip; |
412 | $list = $zip->listContent(); |
413 | $listCount = count($list); |
414 | $listIndex = -1; |
415 | for ($i = 0; $i < $listCount; ++$i) { |
416 | if (strtolower($list[$i]['filename']) == strtolower($filename) || |
417 | strtolower($list[$i]['stored_filename']) == strtolower($filename)) { |
418 | $listIndex = $i; |
419 | |
420 | break; |
421 | } |
422 | } |
423 | |
424 | return ($listIndex > -1) ? $listIndex : false; |
425 | } |
426 | } |