xref: /dokuwiki/vendor/splitbrain/php-archive/src/Zip.php (revision 54333642ddf417b6543d8f53709e39da6473bfe6)
1<?php
2
3namespace splitbrain\PHPArchive;
4
5/**
6 * Class Zip
7 *
8 * Creates or extracts Zip archives
9 *
10 * for specs see http://www.pkware.com/appnote
11 *
12 * @author  Andreas Gohr <andi@splitbrain.org>
13 * @package splitbrain\PHPArchive
14 * @license MIT
15 */
16class Zip extends Archive
17{
18
19    protected $file = '';
20    protected $fh;
21    protected $memory = '';
22    protected $closed = true;
23    protected $writeaccess = false;
24    protected $ctrl_dir;
25    protected $complevel = 9;
26
27    /**
28     * Set the compression level.
29     *
30     * Compression Type is ignored for ZIP
31     *
32     * You can call this function before adding each file to set differen compression levels
33     * for each file.
34     *
35     * @param int $level Compression level (0 to 9)
36     * @param int $type  Type of compression to use ignored for ZIP
37     * @return mixed
38     */
39    public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO)
40    {
41        $this->complevel = $level;
42    }
43
44    /**
45     * Open an existing ZIP file for reading
46     *
47     * @param string $file
48     * @throws ArchiveIOException
49     */
50    public function open($file)
51    {
52        $this->file = $file;
53        $this->fh   = @fopen($this->file, 'rb');
54        if (!$this->fh) {
55            throw new ArchiveIOException('Could not open file for reading: '.$this->file);
56        }
57        $this->closed = false;
58    }
59
60    /**
61     * Read the contents of a ZIP archive
62     *
63     * This function lists the files stored in the archive, and returns an indexed array of FileInfo objects
64     *
65     * The archive is closed afer reading the contents, for API compatibility with TAR files
66     * Reopen the file with open() again if you want to do additional operations
67     *
68     * @throws ArchiveIOException
69     * @return FileInfo[]
70     */
71    public function contents()
72    {
73        if ($this->closed || !$this->file) {
74            throw new ArchiveIOException('Can not read from a closed archive');
75        }
76
77        $result = array();
78
79        $centd = $this->readCentralDir();
80
81        @rewind($this->fh);
82        @fseek($this->fh, $centd['offset']);
83
84        for ($i = 0; $i < $centd['entries']; $i++) {
85            $result[] = $this->header2fileinfo($this->readCentralFileHeader());
86        }
87
88        $this->close();
89        return $result;
90    }
91
92    /**
93     * Extract an existing ZIP archive
94     *
95     * The $strip parameter allows you to strip a certain number of path components from the filenames
96     * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
97     * an integer is passed as $strip.
98     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
99     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
100     *
101     * By default this will extract all files found in the archive. You can restrict the output using the $include
102     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
103     * $include is set only files that match this expression will be extracted. Files that match the $exclude
104     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
105     * stripped filenames as described above.
106     *
107     * @param string     $outdir  the target directory for extracting
108     * @param int|string $strip   either the number of path components or a fixed prefix to strip
109     * @param string     $exclude a regular expression of files to exclude
110     * @param string     $include a regular expression of files to include
111     * @throws ArchiveIOException
112     * @return FileInfo[]
113     */
114    function extract($outdir, $strip = '', $exclude = '', $include = '')
115    {
116        if ($this->closed || !$this->file) {
117            throw new ArchiveIOException('Can not read from a closed archive');
118        }
119
120        $outdir = rtrim($outdir, '/');
121        @mkdir($outdir, 0777, true);
122
123        $extracted = array();
124
125        $cdir      = $this->readCentralDir();
126        $pos_entry = $cdir['offset']; // begin of the central file directory
127
128        for ($i = 0; $i < $cdir['entries']; $i++) {
129            // read file header
130            @fseek($this->fh, $pos_entry);
131            $header          = $this->readCentralFileHeader();
132            $header['index'] = $i;
133            $pos_entry       = ftell($this->fh); // position of the next file in central file directory
134            fseek($this->fh, $header['offset']); // seek to beginning of file header
135            $header   = $this->readFileHeader($header);
136            $fileinfo = $this->header2fileinfo($header);
137
138            // apply strip rules
139            $fileinfo->strip($strip);
140
141            // skip unwanted files
142            if (!strlen($fileinfo->getPath()) || !$fileinfo->match($include, $exclude)) {
143                continue;
144            }
145
146            $extracted[] = $fileinfo;
147
148            // create output directory
149            $output    = $outdir.'/'.$fileinfo->getPath();
150            $directory = ($header['folder']) ? $output : dirname($output);
151            @mkdir($directory, 0777, true);
152
153            // nothing more to do for directories
154            if ($fileinfo->getIsdir()) {
155                continue;
156            }
157
158            // compressed files are written to temporary .gz file first
159            if ($header['compression'] == 0) {
160                $extractto = $output;
161            } else {
162                $extractto = $output.'.gz';
163            }
164
165            // open file for writing
166            $fp = fopen($extractto, "wb");
167            if (!$fp) {
168                throw new ArchiveIOException('Could not open file for writing: '.$extractto);
169            }
170
171            // prepend compression header
172            if ($header['compression'] != 0) {
173                $binary_data = pack(
174                    'va1a1Va1a1',
175                    0x8b1f,
176                    chr($header['compression']),
177                    chr(0x00),
178                    time(),
179                    chr(0x00),
180                    chr(3)
181                );
182                fwrite($fp, $binary_data, 10);
183            }
184
185            // read the file and store it on disk
186            $size = $header['compressed_size'];
187            while ($size != 0) {
188                $read_size   = ($size < 2048 ? $size : 2048);
189                $buffer      = fread($this->fh, $read_size);
190                $binary_data = pack('a'.$read_size, $buffer);
191                fwrite($fp, $binary_data, $read_size);
192                $size -= $read_size;
193            }
194
195            // finalize compressed file
196            if ($header['compression'] != 0) {
197                $binary_data = pack('VV', $header['crc'], $header['size']);
198                fwrite($fp, $binary_data, 8);
199            }
200
201            // close file
202            fclose($fp);
203
204            // unpack compressed file
205            if ($header['compression'] != 0) {
206                $gzp = @gzopen($extractto, 'rb');
207                if (!$gzp) {
208                    @unlink($extractto);
209                    throw new ArchiveIOException('Failed file extracting. gzip support missing?');
210                }
211                $fp = @fopen($output, 'wb');
212                if (!$fp) {
213                    throw new ArchiveIOException('Could not open file for writing: '.$extractto);
214                }
215
216                $size = $header['size'];
217                while ($size != 0) {
218                    $read_size   = ($size < 2048 ? $size : 2048);
219                    $buffer      = gzread($gzp, $read_size);
220                    $binary_data = pack('a'.$read_size, $buffer);
221                    @fwrite($fp, $binary_data, $read_size);
222                    $size -= $read_size;
223                }
224                fclose($fp);
225                gzclose($gzp);
226                unlink($extractto); // remove temporary gz file
227            }
228
229            touch($output, $fileinfo->getMtime());
230            //FIXME what about permissions?
231        }
232
233        $this->close();
234        return $extracted;
235    }
236
237    /**
238     * Create a new ZIP file
239     *
240     * If $file is empty, the zip file will be created in memory
241     *
242     * @param string $file
243     * @throws ArchiveIOException
244     */
245    public function create($file = '')
246    {
247        $this->file   = $file;
248        $this->memory = '';
249        $this->fh     = 0;
250
251        if ($this->file) {
252            $this->fh = @fopen($this->file, 'wb');
253
254            if (!$this->fh) {
255                throw new ArchiveIOException('Could not open file for writing: '.$this->file);
256            }
257        }
258        $this->writeaccess = true;
259        $this->closed      = false;
260        $this->ctrl_dir    = array();
261    }
262
263    /**
264     * Add a file to the current ZIP archive using an existing file in the filesystem
265     *
266     * @param string          $file     path to the original file
267     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
268     * @throws ArchiveIOException
269     */
270
271    /**
272     * Add a file to the current archive using an existing file in the filesystem
273     *
274     * @param string          $file     path to the original file
275     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
276     * @throws ArchiveIOException
277     */
278    public function addFile($file, $fileinfo = '')
279    {
280        if (is_string($fileinfo)) {
281            $fileinfo = FileInfo::fromPath($file, $fileinfo);
282        }
283
284        if ($this->closed) {
285            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
286        }
287
288        $data = @file_get_contents($file);
289        if ($data === false) {
290            throw new ArchiveIOException('Could not open file for reading: '.$file);
291        }
292
293        // FIXME could we stream writing compressed data? gzwrite on a fopen handle?
294        $this->addData($fileinfo, $data);
295    }
296
297    /**
298     * Add a file to the current TAR archive using the given $data as content
299     *
300     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
301     * @param string          $data     binary content of the file to add
302     * @throws ArchiveIOException
303     */
304    public function addData($fileinfo, $data)
305    {
306        if (is_string($fileinfo)) {
307            $fileinfo = new FileInfo($fileinfo);
308        }
309
310        if ($this->closed) {
311            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
312        }
313
314        // prepare info and compress data
315        $size     = strlen($data);
316        $crc      = crc32($data);
317        if ($this->complevel) {
318            $data = gzcompress($data, $this->complevel);
319            $data = substr($data, 2, -4); // strip compression headers
320        }
321        $csize  = strlen($data);
322        $offset = $this->dataOffset();
323        $name   = $fileinfo->getPath();
324        $time   = $fileinfo->getMtime();
325
326        // write local file header
327        $this->writebytes($this->makeLocalFileHeader(
328            $time,
329            $crc,
330            $size,
331            $csize,
332            $name,
333            (bool) $this->complevel
334        ));
335
336        // we store no encryption header
337
338        // write data
339        $this->writebytes($data);
340
341        // we store no data descriptor
342
343        // add info to central file directory
344        $this->ctrl_dir[] = $this->makeCentralFileRecord(
345            $offset,
346            $time,
347            $crc,
348            $size,
349            $csize,
350            $name,
351            (bool) $this->complevel
352        );
353    }
354
355    /**
356     * Add the closing footer to the archive if in write mode, close all file handles
357     *
358     * After a call to this function no more data can be added to the archive, for
359     * read access no reading is allowed anymore
360     */
361    public function close()
362    {
363        if ($this->closed) {
364            return;
365        } // we did this already
366
367        if ($this->writeaccess) {
368            // write central directory
369            $offset = $this->dataOffset();
370            $ctrldir = join('', $this->ctrl_dir);
371            $this->writebytes($ctrldir);
372
373            // write end of central directory record
374            $this->writebytes("\x50\x4b\x05\x06"); // end of central dir signature
375            $this->writebytes(pack('v', 0)); // number of this disk
376            $this->writebytes(pack('v', 0)); // number of the disk with the start of the central directory
377            $this->writebytes(pack('v',
378                count($this->ctrl_dir))); // total number of entries in the central directory on this disk
379            $this->writebytes(pack('v', count($this->ctrl_dir))); // total number of entries in the central directory
380            $this->writebytes(pack('V', strlen($ctrldir))); // size of the central directory
381            $this->writebytes(pack('V',
382                $offset)); // offset of start of central directory with respect to the starting disk number
383            $this->writebytes(pack('v', 0)); // .ZIP file comment length
384
385            $this->ctrl_dir = array();
386        }
387
388        // close file handles
389        if ($this->file) {
390            fclose($this->fh);
391            $this->file = '';
392            $this->fh   = 0;
393        }
394
395        $this->writeaccess = false;
396        $this->closed      = true;
397    }
398
399    /**
400     * Returns the created in-memory archive data
401     *
402     * This implicitly calls close() on the Archive
403     */
404    public function getArchive()
405    {
406        $this->close();
407
408        return $this->memory;
409    }
410
411    /**
412     * Save the created in-memory archive data
413     *
414     * Note: It's more memory effective to specify the filename in the create() function and
415     * let the library work on the new file directly.
416     *
417     * @param     $file
418     * @throws ArchiveIOException
419     */
420    public function save($file)
421    {
422        if (!file_put_contents($file, $this->getArchive())) {
423            throw new ArchiveIOException('Could not write to file: '.$file);
424        }
425    }
426
427    /**
428     * Read the central directory
429     *
430     * This key-value list contains general information about the ZIP file
431     *
432     * @return array
433     */
434    protected function readCentralDir()
435    {
436        $size = filesize($this->file);
437        if ($size < 277) {
438            $maximum_size = $size;
439        } else {
440            $maximum_size = 277;
441        }
442
443        @fseek($this->fh, $size - $maximum_size);
444        $pos   = ftell($this->fh);
445        $bytes = 0x00000000;
446
447        while ($pos < $size) {
448            $byte  = @fread($this->fh, 1);
449            $bytes = (($bytes << 8) & 0xFFFFFFFF) | ord($byte);
450            if ($bytes == 0x504b0506) {
451                break;
452            }
453            $pos++;
454        }
455
456        $data = unpack(
457            'vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',
458            fread($this->fh, 18)
459        );
460
461        if ($data['comment_size'] != 0) {
462            $centd['comment'] = fread($this->fh, $data['comment_size']);
463        } else {
464            $centd['comment'] = '';
465        }
466        $centd['entries']      = $data['entries'];
467        $centd['disk_entries'] = $data['disk_entries'];
468        $centd['offset']       = $data['offset'];
469        $centd['disk_start']   = $data['disk_start'];
470        $centd['size']         = $data['size'];
471        $centd['disk']         = $data['disk'];
472        return $centd;
473    }
474
475    /**
476     * Read the next central file header
477     *
478     * Assumes the current file pointer is pointing at the right position
479     *
480     * @return array
481     */
482    protected function readCentralFileHeader()
483    {
484        $binary_data = fread($this->fh, 46);
485        $header      = unpack(
486            'vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset',
487            $binary_data
488        );
489
490        if ($header['filename_len'] != 0) {
491            $header['filename'] = fread($this->fh, $header['filename_len']);
492        } else {
493            $header['filename'] = '';
494        }
495
496        if ($header['extra_len'] != 0) {
497            $header['extra'] = fread($this->fh, $header['extra_len']);
498        } else {
499            $header['extra'] = '';
500        }
501
502        if ($header['comment_len'] != 0) {
503            $header['comment'] = fread($this->fh, $header['comment_len']);
504        } else {
505            $header['comment'] = '';
506        }
507
508        $header['mtime']           = $this->makeUnixTime($header['mdate'], $header['mtime']);
509        $header['stored_filename'] = $header['filename'];
510        $header['status']          = 'ok';
511        if (substr($header['filename'], -1) == '/') {
512            $header['external'] = 0x41FF0010;
513        }
514        $header['folder'] = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0;
515
516        return $header;
517    }
518
519    /**
520     * Reads the local file header
521     *
522     * This header precedes each individual file inside the zip file. Assumes the current file pointer is pointing at
523     * the right position already. Enhances the given central header with the data found at the local header.
524     *
525     * @param array $header the central file header read previously (see above)
526     * @return array
527     */
528    protected function readFileHeader($header)
529    {
530        $binary_data = fread($this->fh, 30);
531        $data        = unpack(
532            'vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len',
533            $binary_data
534        );
535
536        $header['filename'] = fread($this->fh, $data['filename_len']);
537        if ($data['extra_len'] != 0) {
538            $header['extra'] = fread($this->fh, $data['extra_len']);
539        } else {
540            $header['extra'] = '';
541        }
542
543        $header['compression'] = $data['compression'];
544        foreach (array(
545                     'size',
546                     'compressed_size',
547                     'crc'
548                 ) as $hd) { // On ODT files, these headers are 0. Keep the previous value.
549            if ($data[$hd] != 0) {
550                $header[$hd] = $data[$hd];
551            }
552        }
553        $header['flag']  = $data['flag'];
554        $header['mtime'] = $this->makeUnixTime($data['mdate'], $data['mtime']);
555
556        $header['stored_filename'] = $header['filename'];
557        $header['status']          = "ok";
558        $header['folder']          = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0;
559        return $header;
560    }
561
562    /**
563     * Create fileinfo object from header data
564     *
565     * @param $header
566     * @return FileInfo
567     */
568    protected function header2fileinfo($header)
569    {
570        $fileinfo = new FileInfo();
571        $fileinfo->setPath($header['filename']);
572        $fileinfo->setSize($header['size']);
573        $fileinfo->setCompressedSize($header['compressed_size']);
574        $fileinfo->setMtime($header['mtime']);
575        $fileinfo->setComment($header['comment']);
576        $fileinfo->setIsdir($header['external'] == 0x41FF0010 || $header['external'] == 16);
577        return $fileinfo;
578    }
579
580    /**
581     * Write to the open filepointer or memory
582     *
583     * @param string $data
584     * @throws ArchiveIOException
585     * @return int number of bytes written
586     */
587    protected function writebytes($data)
588    {
589        if (!$this->file) {
590            $this->memory .= $data;
591            $written = strlen($data);
592        } else {
593            $written = @fwrite($this->fh, $data);
594        }
595        if ($written === false) {
596            throw new ArchiveIOException('Failed to write to archive stream');
597        }
598        return $written;
599    }
600
601    /**
602     * Current data pointer position
603     *
604     * @fixme might need a -1
605     * @return int
606     */
607    protected function dataOffset()
608    {
609        if ($this->file) {
610            return ftell($this->fh);
611        } else {
612            return strlen($this->memory);
613        }
614    }
615
616    /**
617     * Create a DOS timestamp from a UNIX timestamp
618     *
619     * DOS timestamps start at 1980-01-01, earlier UNIX stamps will be set to this date
620     *
621     * @param $time
622     * @return int
623     */
624    protected function makeDosTime($time)
625    {
626        $timearray = getdate($time);
627        if ($timearray['year'] < 1980) {
628            $timearray['year']    = 1980;
629            $timearray['mon']     = 1;
630            $timearray['mday']    = 1;
631            $timearray['hours']   = 0;
632            $timearray['minutes'] = 0;
633            $timearray['seconds'] = 0;
634        }
635        return (($timearray['year'] - 1980) << 25) |
636        ($timearray['mon'] << 21) |
637        ($timearray['mday'] << 16) |
638        ($timearray['hours'] << 11) |
639        ($timearray['minutes'] << 5) |
640        ($timearray['seconds'] >> 1);
641    }
642
643    /**
644     * Create a UNIX timestamp from a DOS timestamp
645     *
646     * @param $mdate
647     * @param $mtime
648     * @return int
649     */
650    protected function makeUnixTime($mdate = null, $mtime = null)
651    {
652        if ($mdate && $mtime) {
653            $year = (($mdate & 0xFE00) >> 9) + 1980;
654            $month = ($mdate & 0x01E0) >> 5;
655            $day = $mdate & 0x001F;
656
657            $hour = ($mtime & 0xF800) >> 11;
658            $minute = ($mtime & 0x07E0) >> 5;
659            $seconde = ($mtime & 0x001F) << 1;
660
661            $mtime = mktime($hour, $minute, $seconde, $month, $day, $year);
662        } else {
663            $mtime = time();
664        }
665
666        return $mtime;
667    }
668
669    /**
670     * Returns a local file header for the given data
671     *
672     * @param int $offset location of the local header
673     * @param int $ts unix timestamp
674     * @param int $crc CRC32 checksum of the uncompressed data
675     * @param int $len length of the uncompressed data
676     * @param int $clen length of the compressed data
677     * @param string $name file name
678     * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen
679     * @return string
680     */
681    protected function makeCentralFileRecord($offset, $ts, $crc, $len, $clen, $name, $comp = null)
682    {
683        if(is_null($comp)) $comp = $len != $clen;
684        $comp = $comp ? 8 : 0;
685        $dtime = dechex($this->makeDosTime($ts));
686
687        $header = "\x50\x4b\x01\x02"; // central file header signature
688        $header .= pack('v', 14); // version made by - VFAT
689        $header .= pack('v', 20); // version needed to extract - 2.0
690        $header .= pack('v', 0); // general purpose flag - no flags set
691        $header .= pack('v', $comp); // compression method - deflate|none
692        $header .= pack(
693            'H*',
694            $dtime[6] . $dtime[7] .
695            $dtime[4] . $dtime[5] .
696            $dtime[2] . $dtime[3] .
697            $dtime[0] . $dtime[1]
698        ); //  last mod file time and date
699        $header .= pack('V', $crc); // crc-32
700        $header .= pack('V', $clen); // compressed size
701        $header .= pack('V', $len); // uncompressed size
702        $header .= pack('v', strlen($name)); // file name length
703        $header .= pack('v', 0); // extra field length
704        $header .= pack('v', 0); // file comment length
705        $header .= pack('v', 0); // disk number start
706        $header .= pack('v', 0); // internal file attributes
707        $header .= pack('V', 0); // external file attributes  @todo was 0x32!?
708        $header .= pack('V', $offset); // relative offset of local header
709        $header .= $name; // file name
710
711        return $header;
712    }
713
714    /**
715     * Returns a local file header for the given data
716     *
717     * @param int $ts unix timestamp
718     * @param int $crc CRC32 checksum of the uncompressed data
719     * @param int $len length of the uncompressed data
720     * @param int $clen length of the compressed data
721     * @param string $name file name
722     * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen
723     * @return string
724     */
725    protected function makeLocalFileHeader($ts, $crc, $len, $clen, $name, $comp = null)
726    {
727        if(is_null($comp)) $comp = $len != $clen;
728        $comp = $comp ? 8 : 0;
729        $dtime = dechex($this->makeDosTime($ts));
730
731        $header = "\x50\x4b\x03\x04"; //  local file header signature
732        $header .= pack('v', 20); // version needed to extract - 2.0
733        $header .= pack('v', 0); // general purpose flag - no flags set
734        $header .= pack('v', $comp); // compression method - deflate|none
735        $header .= pack(
736            'H*',
737            $dtime[6] . $dtime[7] .
738            $dtime[4] . $dtime[5] .
739            $dtime[2] . $dtime[3] .
740            $dtime[0] . $dtime[1]
741        ); //  last mod file time and date
742        $header .= pack('V', $crc); // crc-32
743        $header .= pack('V', $clen); // compressed size
744        $header .= pack('V', $len); // uncompressed size
745        $header .= pack('v', strlen($name)); // file name length
746        $header .= pack('v', 0); // extra field length
747        $header .= $name;
748        return $header;
749    }
750}
751