Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.37% covered (success)
98.37%
121 / 123
92.31% covered (success)
92.31%
12 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZipArchive
98.37% covered (success)
98.37%
121 / 123
92.31% covered (success)
92.31%
12 / 13
44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 __call
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 open
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 close
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 extractTo
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getFromName
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 pclzipAddFile
91.67% covered (success)
91.67%
22 / 24
0.00% covered (danger)
0.00%
0 / 1
5.01
 pclzipAddFromString
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 pclzipExtractTo
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 pclzipGetFromName
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 pclzipGetNameIndex
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 pclzipLocateName
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 addEmptyDir
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
19namespace PhpOffice\PhpWord\Shared;
20
21use PclZip;
22use PhpOffice\PhpWord\Exception\Exception;
23use PhpOffice\PhpWord\Settings;
24use 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 */
40class 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 bool|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        $filenamePartsBaseName = pathinfo($filename, PATHINFO_BASENAME);
249        $filenamePartsDirName = pathinfo($filename, PATHINFO_DIRNAME);
250        $localnamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
251        $localnamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);
252
253        // To Rename the file while adding it to the zip we
254        //   need to create a temp file with the correct name
255        $tempFile = false;
256        if ($filenamePartsBaseName != $localnamePartsBaseName) {
257            $tempFile = true; // temp file created
258            $temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName;
259            copy($filename, $temppath);
260            $filename = $temppath;
261            $filenamePartsDirName = pathinfo($temppath, PATHINFO_DIRNAME);
262        }
263
264        $pathRemoved = $filenamePartsDirName;
265        $pathAdded = $localnamePartsDirName;
266
267        if (!$this->usePclzip) {
268            $pathAdded = $pathAdded . '/' . ltrim(str_replace('\\', '/', substr($filename, strlen($pathRemoved))), '/');
269            //$res = $zip->addFile($filename, $pathAdded);
270            $res = $zip->addFromString($pathAdded, file_get_contents($filename));       // addFile can't use subfolders in some cases
271        } else {
272            $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
273        }
274
275        if ($tempFile) {
276            // Remove temp file, if created
277            unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName);
278        }
279
280        return $res != 0;
281    }
282
283    /**
284     * Add a new file to the zip archive from a string of raw data (emulate \ZipArchive).
285     *
286     * @param string $localname Directory/Name of the file to add to the zip archive
287     * @param string $contents String of data to add to the zip archive
288     *
289     * @return bool
290     */
291    public function pclzipAddFromString($localname, $contents)
292    {
293        /** @var PclZip $zip Type hint */
294        $zip = $this->zip;
295        $filenamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
296        $filenamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);
297
298        // Write $contents to a temp file
299        $handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName, 'wb');
300        if ($handle) {
301            fwrite($handle, $contents);
302            fclose($handle);
303        }
304
305        // Add temp file to zip
306        $filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName;
307        $pathRemoved = $this->tempDir;
308        $pathAdded = $filenamePartsDirName;
309
310        $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
311
312        // Remove temp file
313        @unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName);
314
315        return $res != 0;
316    }
317
318    /**
319     * Extract the archive contents (emulate \ZipArchive).
320     *
321     * @param string $destination
322     * @param array|string $entries
323     *
324     * @return bool
325     *
326     * @since 0.10.0
327     */
328    public function pclzipExtractTo($destination, $entries = null)
329    {
330        /** @var PclZip $zip Type hint */
331        $zip = $this->zip;
332
333        // Extract all files
334        if (null === $entries) {
335            $result = $zip->extract(PCLZIP_OPT_PATH, $destination);
336
337            return $result > 0;
338        }
339
340        // Extract by entries
341        if (!is_array($entries)) {
342            $entries = [$entries];
343        }
344        foreach ($entries as $entry) {
345            $entryIndex = $this->locateName($entry);
346            $result = $zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination);
347            if ($result <= 0) {
348                return false;
349            }
350        }
351
352        return true;
353    }
354
355    /**
356     * Extract file from archive by given file name (emulate \ZipArchive).
357     *
358     * @param  string $filename Filename for the file in zip archive
359     *
360     * @return string $contents File string contents
361     */
362    public function pclzipGetFromName($filename)
363    {
364        /** @var PclZip $zip Type hint */
365        $zip = $this->zip;
366        $listIndex = $this->pclzipLocateName($filename);
367        $contents = false;
368
369        if ($listIndex !== false) {
370            $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
371        } else {
372            $filename = substr($filename, 1);
373            $listIndex = $this->pclzipLocateName($filename);
374            $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
375        }
376        if (is_array($extracted) && count($extracted) != 0) {
377            $contents = $extracted[0]['content'];
378        }
379
380        return $contents;
381    }
382
383    /**
384     * Returns the name of an entry using its index (emulate \ZipArchive).
385     *
386     * @param int $index
387     *
388     * @return bool|string
389     *
390     * @since 0.10.0
391     */
392    public function pclzipGetNameIndex($index)
393    {
394        /** @var PclZip $zip Type hint */
395        $zip = $this->zip;
396        $list = $zip->listContent();
397        if (isset($list[$index])) {
398            return $list[$index]['filename'];
399        }
400
401        return false;
402    }
403
404    /**
405     * Returns the index of the entry in the archive (emulate \ZipArchive).
406     *
407     * @param string $filename Filename for the file in zip archive
408     *
409     * @return false|int
410     */
411    public function pclzipLocateName($filename)
412    {
413        /** @var PclZip $zip Type hint */
414        $zip = $this->zip;
415        $list = $zip->listContent();
416        $listCount = count($list);
417        $listIndex = -1;
418        for ($i = 0; $i < $listCount; ++$i) {
419            if (strtolower($list[$i]['filename']) == strtolower($filename) ||
420                strtolower($list[$i]['stored_filename']) == strtolower($filename)) {
421                $listIndex = $i;
422
423                break;
424            }
425        }
426
427        return ($listIndex > -1) ? $listIndex : false;
428    }
429
430    /**
431     * Add an empty directory to the zip archive (emulate \ZipArchive).
432     *
433     * @param string $dirname Directory name to add to the zip archive
434     */
435    public function addEmptyDir(string $dirname): bool
436    {
437        // Create a directory entry by adding an empty file with trailing slash
438        return $this->addFromString(rtrim($dirname, '/') . '/', '');
439    }
440}