xref: /dokuwiki/vendor/splitbrain/php-archive/src/Tar.php (revision 2afbbbaeea08091e19cadcd631ed59a224ff0d59)
1605f8e8dSAndreas Gohr<?php
2605f8e8dSAndreas Gohr
3605f8e8dSAndreas Gohrnamespace splitbrain\PHPArchive;
4605f8e8dSAndreas Gohr
5605f8e8dSAndreas Gohr/**
6605f8e8dSAndreas Gohr * Class Tar
7605f8e8dSAndreas Gohr *
8605f8e8dSAndreas Gohr * Creates or extracts Tar archives. Supports gz and bzip compression
9605f8e8dSAndreas Gohr *
10605f8e8dSAndreas Gohr * Long pathnames (>100 chars) are supported in POSIX ustar and GNU longlink formats.
11605f8e8dSAndreas Gohr *
12605f8e8dSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
13605f8e8dSAndreas Gohr * @package splitbrain\PHPArchive
14605f8e8dSAndreas Gohr * @license MIT
15605f8e8dSAndreas Gohr */
16605f8e8dSAndreas Gohrclass Tar extends Archive
17605f8e8dSAndreas Gohr{
18605f8e8dSAndreas Gohr
19605f8e8dSAndreas Gohr    protected $file = '';
20605f8e8dSAndreas Gohr    protected $comptype = Archive::COMPRESS_AUTO;
21605f8e8dSAndreas Gohr    protected $complevel = 9;
22605f8e8dSAndreas Gohr    protected $fh;
23605f8e8dSAndreas Gohr    protected $memory = '';
24605f8e8dSAndreas Gohr    protected $closed = true;
25605f8e8dSAndreas Gohr    protected $writeaccess = false;
26605f8e8dSAndreas Gohr
27605f8e8dSAndreas Gohr    /**
28605f8e8dSAndreas Gohr     * Sets the compression to use
29605f8e8dSAndreas Gohr     *
30605f8e8dSAndreas Gohr     * @param int $level Compression level (0 to 9)
31605f8e8dSAndreas Gohr     * @param int $type Type of compression to use (use COMPRESS_* constants)
32e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
33605f8e8dSAndreas Gohr     */
34605f8e8dSAndreas Gohr    public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO)
35605f8e8dSAndreas Gohr    {
36605f8e8dSAndreas Gohr        $this->compressioncheck($type);
37e43cd7e1SAndreas Gohr        if ($level < -1 || $level > 9) {
38e43cd7e1SAndreas Gohr            throw new ArchiveIllegalCompressionException('Compression level should be between -1 and 9');
39e43cd7e1SAndreas Gohr        }
40605f8e8dSAndreas Gohr        $this->comptype  = $type;
41605f8e8dSAndreas Gohr        $this->complevel = $level;
42530d6729SAndreas Gohr        if($level == 0) $this->comptype = Archive::COMPRESS_NONE;
43530d6729SAndreas Gohr        if($type == Archive::COMPRESS_NONE) $this->complevel = 0;
44605f8e8dSAndreas Gohr    }
45605f8e8dSAndreas Gohr
46605f8e8dSAndreas Gohr    /**
47605f8e8dSAndreas Gohr     * Open an existing TAR file for reading
48605f8e8dSAndreas Gohr     *
49605f8e8dSAndreas Gohr     * @param string $file
50605f8e8dSAndreas Gohr     * @throws ArchiveIOException
51e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
52605f8e8dSAndreas Gohr     */
53605f8e8dSAndreas Gohr    public function open($file)
54605f8e8dSAndreas Gohr    {
55605f8e8dSAndreas Gohr        $this->file = $file;
56605f8e8dSAndreas Gohr
57605f8e8dSAndreas Gohr        // update compression to mach file
58605f8e8dSAndreas Gohr        if ($this->comptype == Tar::COMPRESS_AUTO) {
59605f8e8dSAndreas Gohr            $this->setCompression($this->complevel, $this->filetype($file));
60605f8e8dSAndreas Gohr        }
61605f8e8dSAndreas Gohr
62605f8e8dSAndreas Gohr        // open file handles
63605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
64605f8e8dSAndreas Gohr            $this->fh = @gzopen($this->file, 'rb');
65605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
66605f8e8dSAndreas Gohr            $this->fh = @bzopen($this->file, 'r');
67605f8e8dSAndreas Gohr        } else {
68605f8e8dSAndreas Gohr            $this->fh = @fopen($this->file, 'rb');
69605f8e8dSAndreas Gohr        }
70605f8e8dSAndreas Gohr
71605f8e8dSAndreas Gohr        if (!$this->fh) {
72605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not open file for reading: '.$this->file);
73605f8e8dSAndreas Gohr        }
74605f8e8dSAndreas Gohr        $this->closed = false;
75605f8e8dSAndreas Gohr    }
76605f8e8dSAndreas Gohr
77605f8e8dSAndreas Gohr    /**
78605f8e8dSAndreas Gohr     * Read the contents of a TAR archive
79605f8e8dSAndreas Gohr     *
80605f8e8dSAndreas Gohr     * This function lists the files stored in the archive
81605f8e8dSAndreas Gohr     *
82605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
83605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
84605f8e8dSAndreas Gohr     *
85605f8e8dSAndreas Gohr     * @throws ArchiveIOException
86e43cd7e1SAndreas Gohr     * @throws ArchiveCorruptedException
87605f8e8dSAndreas Gohr     * @returns FileInfo[]
88605f8e8dSAndreas Gohr     */
89605f8e8dSAndreas Gohr    public function contents()
90605f8e8dSAndreas Gohr    {
91*2afbbbaeSAndreas Gohr        $result = array();
92*2afbbbaeSAndreas Gohr
93*2afbbbaeSAndreas Gohr        foreach ($this->yieldContents() as $fileinfo) {
94*2afbbbaeSAndreas Gohr            $result[] = $fileinfo;
95*2afbbbaeSAndreas Gohr        }
96*2afbbbaeSAndreas Gohr
97*2afbbbaeSAndreas Gohr        return $result;
98*2afbbbaeSAndreas Gohr    }
99*2afbbbaeSAndreas Gohr
100*2afbbbaeSAndreas Gohr    /**
101*2afbbbaeSAndreas Gohr     * Read the contents of a TAR archive and return each entry using yield
102*2afbbbaeSAndreas Gohr     * for memory efficiency.
103*2afbbbaeSAndreas Gohr     *
104*2afbbbaeSAndreas Gohr     * @see contents()
105*2afbbbaeSAndreas Gohr     * @throws ArchiveIOException
106*2afbbbaeSAndreas Gohr     * @throws ArchiveCorruptedException
107*2afbbbaeSAndreas Gohr     * @returns FileInfo[]
108*2afbbbaeSAndreas Gohr     */
109*2afbbbaeSAndreas Gohr    public function yieldContents()
110*2afbbbaeSAndreas Gohr    {
111605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
112605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
113605f8e8dSAndreas Gohr        }
114605f8e8dSAndreas Gohr
115605f8e8dSAndreas Gohr        while ($read = $this->readbytes(512)) {
116605f8e8dSAndreas Gohr            $header = $this->parseHeader($read);
117605f8e8dSAndreas Gohr            if (!is_array($header)) {
118605f8e8dSAndreas Gohr                continue;
119605f8e8dSAndreas Gohr            }
120605f8e8dSAndreas Gohr
121605f8e8dSAndreas Gohr            $this->skipbytes(ceil($header['size'] / 512) * 512);
122*2afbbbaeSAndreas Gohr            yield $this->header2fileinfo($header);
123605f8e8dSAndreas Gohr        }
124605f8e8dSAndreas Gohr
125605f8e8dSAndreas Gohr        $this->close();
126*2afbbbaeSAndreas Gohr
127605f8e8dSAndreas Gohr    }
128605f8e8dSAndreas Gohr
129605f8e8dSAndreas Gohr    /**
130605f8e8dSAndreas Gohr     * Extract an existing TAR archive
131605f8e8dSAndreas Gohr     *
132605f8e8dSAndreas Gohr     * The $strip parameter allows you to strip a certain number of path components from the filenames
133605f8e8dSAndreas Gohr     * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
134605f8e8dSAndreas Gohr     * an integer is passed as $strip.
135605f8e8dSAndreas Gohr     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
136605f8e8dSAndreas Gohr     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
137605f8e8dSAndreas Gohr     *
138605f8e8dSAndreas Gohr     * By default this will extract all files found in the archive. You can restrict the output using the $include
139605f8e8dSAndreas Gohr     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
140605f8e8dSAndreas Gohr     * $include is set only files that match this expression will be extracted. Files that match the $exclude
141605f8e8dSAndreas Gohr     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
142605f8e8dSAndreas Gohr     * stripped filenames as described above.
143605f8e8dSAndreas Gohr     *
144605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
145605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
146605f8e8dSAndreas Gohr     *
147605f8e8dSAndreas Gohr     * @param string $outdir the target directory for extracting
148605f8e8dSAndreas Gohr     * @param int|string $strip either the number of path components or a fixed prefix to strip
149605f8e8dSAndreas Gohr     * @param string $exclude a regular expression of files to exclude
150605f8e8dSAndreas Gohr     * @param string $include a regular expression of files to include
151605f8e8dSAndreas Gohr     * @throws ArchiveIOException
152e43cd7e1SAndreas Gohr     * @throws ArchiveCorruptedException
153605f8e8dSAndreas Gohr     * @return FileInfo[]
154605f8e8dSAndreas Gohr     */
155605f8e8dSAndreas Gohr    public function extract($outdir, $strip = '', $exclude = '', $include = '')
156605f8e8dSAndreas Gohr    {
157605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
158605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
159605f8e8dSAndreas Gohr        }
160605f8e8dSAndreas Gohr
161605f8e8dSAndreas Gohr        $outdir = rtrim($outdir, '/');
162605f8e8dSAndreas Gohr        @mkdir($outdir, 0777, true);
163605f8e8dSAndreas Gohr        if (!is_dir($outdir)) {
164605f8e8dSAndreas Gohr            throw new ArchiveIOException("Could not create directory '$outdir'");
165605f8e8dSAndreas Gohr        }
166605f8e8dSAndreas Gohr
167605f8e8dSAndreas Gohr        $extracted = array();
168605f8e8dSAndreas Gohr        while ($dat = $this->readbytes(512)) {
169605f8e8dSAndreas Gohr            // read the file header
170605f8e8dSAndreas Gohr            $header = $this->parseHeader($dat);
171605f8e8dSAndreas Gohr            if (!is_array($header)) {
172605f8e8dSAndreas Gohr                continue;
173605f8e8dSAndreas Gohr            }
174605f8e8dSAndreas Gohr            $fileinfo = $this->header2fileinfo($header);
175605f8e8dSAndreas Gohr
176605f8e8dSAndreas Gohr            // apply strip rules
177605f8e8dSAndreas Gohr            $fileinfo->strip($strip);
178605f8e8dSAndreas Gohr
179605f8e8dSAndreas Gohr            // skip unwanted files
180a3bfbb3cSAndreas Gohr            if (!strlen($fileinfo->getPath()) || !$fileinfo->matchExpression($include, $exclude)) {
181605f8e8dSAndreas Gohr                $this->skipbytes(ceil($header['size'] / 512) * 512);
182605f8e8dSAndreas Gohr                continue;
183605f8e8dSAndreas Gohr            }
184605f8e8dSAndreas Gohr
185605f8e8dSAndreas Gohr            // create output directory
186605f8e8dSAndreas Gohr            $output    = $outdir.'/'.$fileinfo->getPath();
187605f8e8dSAndreas Gohr            $directory = ($fileinfo->getIsdir()) ? $output : dirname($output);
188*2afbbbaeSAndreas Gohr            if (!file_exists($directory)) {
189*2afbbbaeSAndreas Gohr                mkdir($directory, 0777, true);
190*2afbbbaeSAndreas Gohr            }
191605f8e8dSAndreas Gohr
192605f8e8dSAndreas Gohr            // extract data
193605f8e8dSAndreas Gohr            if (!$fileinfo->getIsdir()) {
194ddb94cf0SAndreas Gohr                $fp = @fopen($output, "wb");
195605f8e8dSAndreas Gohr                if (!$fp) {
196605f8e8dSAndreas Gohr                    throw new ArchiveIOException('Could not open file for writing: '.$output);
197605f8e8dSAndreas Gohr                }
198605f8e8dSAndreas Gohr
199605f8e8dSAndreas Gohr                $size = floor($header['size'] / 512);
200605f8e8dSAndreas Gohr                for ($i = 0; $i < $size; $i++) {
201605f8e8dSAndreas Gohr                    fwrite($fp, $this->readbytes(512), 512);
202605f8e8dSAndreas Gohr                }
203605f8e8dSAndreas Gohr                if (($header['size'] % 512) != 0) {
204605f8e8dSAndreas Gohr                    fwrite($fp, $this->readbytes(512), $header['size'] % 512);
205605f8e8dSAndreas Gohr                }
206605f8e8dSAndreas Gohr
207605f8e8dSAndreas Gohr                fclose($fp);
208e43cd7e1SAndreas Gohr                @touch($output, $fileinfo->getMtime());
209e43cd7e1SAndreas Gohr                @chmod($output, $fileinfo->getMode());
210605f8e8dSAndreas Gohr            } else {
211605f8e8dSAndreas Gohr                $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories
212605f8e8dSAndreas Gohr            }
213605f8e8dSAndreas Gohr
214e43cd7e1SAndreas Gohr            if(is_callable($this->callback)) {
215e43cd7e1SAndreas Gohr                call_user_func($this->callback, $fileinfo);
216e43cd7e1SAndreas Gohr            }
217605f8e8dSAndreas Gohr            $extracted[] = $fileinfo;
218605f8e8dSAndreas Gohr        }
219605f8e8dSAndreas Gohr
220605f8e8dSAndreas Gohr        $this->close();
221605f8e8dSAndreas Gohr        return $extracted;
222605f8e8dSAndreas Gohr    }
223605f8e8dSAndreas Gohr
224605f8e8dSAndreas Gohr    /**
225605f8e8dSAndreas Gohr     * Create a new TAR file
226605f8e8dSAndreas Gohr     *
227605f8e8dSAndreas Gohr     * If $file is empty, the tar file will be created in memory
228605f8e8dSAndreas Gohr     *
229605f8e8dSAndreas Gohr     * @param string $file
230605f8e8dSAndreas Gohr     * @throws ArchiveIOException
231e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
232605f8e8dSAndreas Gohr     */
233605f8e8dSAndreas Gohr    public function create($file = '')
234605f8e8dSAndreas Gohr    {
235605f8e8dSAndreas Gohr        $this->file   = $file;
236605f8e8dSAndreas Gohr        $this->memory = '';
237605f8e8dSAndreas Gohr        $this->fh     = 0;
238605f8e8dSAndreas Gohr
239605f8e8dSAndreas Gohr        if ($this->file) {
240605f8e8dSAndreas Gohr            // determine compression
241605f8e8dSAndreas Gohr            if ($this->comptype == Archive::COMPRESS_AUTO) {
242605f8e8dSAndreas Gohr                $this->setCompression($this->complevel, $this->filetype($file));
243605f8e8dSAndreas Gohr            }
244605f8e8dSAndreas Gohr
245605f8e8dSAndreas Gohr            if ($this->comptype === Archive::COMPRESS_GZIP) {
246605f8e8dSAndreas Gohr                $this->fh = @gzopen($this->file, 'wb'.$this->complevel);
247605f8e8dSAndreas Gohr            } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
248605f8e8dSAndreas Gohr                $this->fh = @bzopen($this->file, 'w');
249605f8e8dSAndreas Gohr            } else {
250605f8e8dSAndreas Gohr                $this->fh = @fopen($this->file, 'wb');
251605f8e8dSAndreas Gohr            }
252605f8e8dSAndreas Gohr
253605f8e8dSAndreas Gohr            if (!$this->fh) {
254605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for writing: '.$this->file);
255605f8e8dSAndreas Gohr            }
256605f8e8dSAndreas Gohr        }
257605f8e8dSAndreas Gohr        $this->writeaccess = true;
258605f8e8dSAndreas Gohr        $this->closed      = false;
259605f8e8dSAndreas Gohr    }
260605f8e8dSAndreas Gohr
261605f8e8dSAndreas Gohr    /**
262605f8e8dSAndreas Gohr     * Add a file to the current TAR archive using an existing file in the filesystem
263605f8e8dSAndreas Gohr     *
264605f8e8dSAndreas Gohr     * @param string $file path to the original file
265605f8e8dSAndreas Gohr     * @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
26636113441SAndreas Gohr     * @throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted
26736113441SAndreas Gohr     * @throws ArchiveIOException there was trouble reading the given file, it was not added
268e43cd7e1SAndreas Gohr     * @throws FileInfoException trouble reading file info, it was not added
269605f8e8dSAndreas Gohr     */
270605f8e8dSAndreas Gohr    public function addFile($file, $fileinfo = '')
271605f8e8dSAndreas Gohr    {
272605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
273605f8e8dSAndreas Gohr            $fileinfo = FileInfo::fromPath($file, $fileinfo);
274605f8e8dSAndreas Gohr        }
275605f8e8dSAndreas Gohr
276605f8e8dSAndreas Gohr        if ($this->closed) {
277605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
278605f8e8dSAndreas Gohr        }
279605f8e8dSAndreas Gohr
2806cb05674SAndreas Gohr        // create file header
2816cb05674SAndreas Gohr        $this->writeFileHeader($fileinfo);
2826cb05674SAndreas Gohr
2836cb05674SAndreas Gohr        // write data, but only if we have data to write.
2846cb05674SAndreas Gohr        // note: on Windows fopen() on a directory will fail, so we prevent
2856cb05674SAndreas Gohr        // errors on Windows by testing if we have data to write.
2866cb05674SAndreas Gohr        if (!$fileinfo->getIsdir() && $fileinfo->getSize() > 0) {
2876cb05674SAndreas Gohr            $read = 0;
288ddb94cf0SAndreas Gohr            $fp = @fopen($file, 'rb');
289605f8e8dSAndreas Gohr            if (!$fp) {
290605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for reading: ' . $file);
291605f8e8dSAndreas Gohr            }
292605f8e8dSAndreas Gohr            while (!feof($fp)) {
293605f8e8dSAndreas Gohr                $data = fread($fp, 512);
29436113441SAndreas Gohr                $read += strlen($data);
295605f8e8dSAndreas Gohr                if ($data === false) {
296605f8e8dSAndreas Gohr                    break;
297605f8e8dSAndreas Gohr                }
298605f8e8dSAndreas Gohr                if ($data === '') {
299605f8e8dSAndreas Gohr                    break;
300605f8e8dSAndreas Gohr                }
301605f8e8dSAndreas Gohr                $packed = pack("a512", $data);
302605f8e8dSAndreas Gohr                $this->writebytes($packed);
303605f8e8dSAndreas Gohr            }
304605f8e8dSAndreas Gohr            fclose($fp);
30536113441SAndreas Gohr
30636113441SAndreas Gohr            if ($read != $fileinfo->getSize()) {
30736113441SAndreas Gohr                $this->close();
30836113441SAndreas Gohr                throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
30936113441SAndreas Gohr            }
3106cb05674SAndreas Gohr        }
311e43cd7e1SAndreas Gohr
312e43cd7e1SAndreas Gohr        if(is_callable($this->callback)) {
313e43cd7e1SAndreas Gohr            call_user_func($this->callback, $fileinfo);
314e43cd7e1SAndreas Gohr        }
315605f8e8dSAndreas Gohr    }
316605f8e8dSAndreas Gohr
317605f8e8dSAndreas Gohr    /**
318605f8e8dSAndreas Gohr     * Add a file to the current TAR archive using the given $data as content
319605f8e8dSAndreas Gohr     *
320605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
321605f8e8dSAndreas Gohr     * @param string          $data     binary content of the file to add
322605f8e8dSAndreas Gohr     * @throws ArchiveIOException
323605f8e8dSAndreas Gohr     */
324605f8e8dSAndreas Gohr    public function addData($fileinfo, $data)
325605f8e8dSAndreas Gohr    {
326605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
327605f8e8dSAndreas Gohr            $fileinfo = new FileInfo($fileinfo);
328605f8e8dSAndreas Gohr        }
329605f8e8dSAndreas Gohr
330605f8e8dSAndreas Gohr        if ($this->closed) {
331605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
332605f8e8dSAndreas Gohr        }
333605f8e8dSAndreas Gohr
334605f8e8dSAndreas Gohr        $len = strlen($data);
335605f8e8dSAndreas Gohr        $fileinfo->setSize($len);
336605f8e8dSAndreas Gohr        $this->writeFileHeader($fileinfo);
337605f8e8dSAndreas Gohr
338605f8e8dSAndreas Gohr        for ($s = 0; $s < $len; $s += 512) {
339605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", substr($data, $s, 512)));
340605f8e8dSAndreas Gohr        }
341e43cd7e1SAndreas Gohr
342e43cd7e1SAndreas Gohr        if (is_callable($this->callback)) {
343e43cd7e1SAndreas Gohr            call_user_func($this->callback, $fileinfo);
344e43cd7e1SAndreas Gohr        }
345605f8e8dSAndreas Gohr    }
346605f8e8dSAndreas Gohr
347605f8e8dSAndreas Gohr    /**
348605f8e8dSAndreas Gohr     * Add the closing footer to the archive if in write mode, close all file handles
349605f8e8dSAndreas Gohr     *
350605f8e8dSAndreas Gohr     * After a call to this function no more data can be added to the archive, for
351605f8e8dSAndreas Gohr     * read access no reading is allowed anymore
352605f8e8dSAndreas Gohr     *
353605f8e8dSAndreas Gohr     * "Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which
354605f8e8dSAndreas Gohr     * consists of two 512 blocks of zero bytes"
355605f8e8dSAndreas Gohr     *
356605f8e8dSAndreas Gohr     * @link http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134
357e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
358605f8e8dSAndreas Gohr     */
359605f8e8dSAndreas Gohr    public function close()
360605f8e8dSAndreas Gohr    {
361605f8e8dSAndreas Gohr        if ($this->closed) {
362605f8e8dSAndreas Gohr            return;
363605f8e8dSAndreas Gohr        } // we did this already
364605f8e8dSAndreas Gohr
365605f8e8dSAndreas Gohr        // write footer
366605f8e8dSAndreas Gohr        if ($this->writeaccess) {
367605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", ""));
368605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", ""));
369605f8e8dSAndreas Gohr        }
370605f8e8dSAndreas Gohr
371605f8e8dSAndreas Gohr        // close file handles
372605f8e8dSAndreas Gohr        if ($this->file) {
373605f8e8dSAndreas Gohr            if ($this->comptype === Archive::COMPRESS_GZIP) {
374605f8e8dSAndreas Gohr                gzclose($this->fh);
375605f8e8dSAndreas Gohr            } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
376605f8e8dSAndreas Gohr                bzclose($this->fh);
377605f8e8dSAndreas Gohr            } else {
378605f8e8dSAndreas Gohr                fclose($this->fh);
379605f8e8dSAndreas Gohr            }
380605f8e8dSAndreas Gohr
381605f8e8dSAndreas Gohr            $this->file = '';
382605f8e8dSAndreas Gohr            $this->fh   = 0;
383605f8e8dSAndreas Gohr        }
384605f8e8dSAndreas Gohr
385605f8e8dSAndreas Gohr        $this->writeaccess = false;
386605f8e8dSAndreas Gohr        $this->closed      = true;
387605f8e8dSAndreas Gohr    }
388605f8e8dSAndreas Gohr
389605f8e8dSAndreas Gohr    /**
390605f8e8dSAndreas Gohr     * Returns the created in-memory archive data
391605f8e8dSAndreas Gohr     *
392605f8e8dSAndreas Gohr     * This implicitly calls close() on the Archive
393e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
394605f8e8dSAndreas Gohr     */
395605f8e8dSAndreas Gohr    public function getArchive()
396605f8e8dSAndreas Gohr    {
397605f8e8dSAndreas Gohr        $this->close();
398605f8e8dSAndreas Gohr
399605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_AUTO) {
400605f8e8dSAndreas Gohr            $this->comptype = Archive::COMPRESS_NONE;
401605f8e8dSAndreas Gohr        }
402605f8e8dSAndreas Gohr
403605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
404dd7064d9SAndreas Gohr            return gzencode($this->memory, $this->complevel);
405605f8e8dSAndreas Gohr        }
406605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_BZIP) {
407605f8e8dSAndreas Gohr            return bzcompress($this->memory);
408605f8e8dSAndreas Gohr        }
409605f8e8dSAndreas Gohr        return $this->memory;
410605f8e8dSAndreas Gohr    }
411605f8e8dSAndreas Gohr
412605f8e8dSAndreas Gohr    /**
413605f8e8dSAndreas Gohr     * Save the created in-memory archive data
414605f8e8dSAndreas Gohr     *
415605f8e8dSAndreas Gohr     * Note: It more memory effective to specify the filename in the create() function and
416605f8e8dSAndreas Gohr     * let the library work on the new file directly.
417605f8e8dSAndreas Gohr     *
418605f8e8dSAndreas Gohr     * @param string $file
419605f8e8dSAndreas Gohr     * @throws ArchiveIOException
420e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
421605f8e8dSAndreas Gohr     */
422605f8e8dSAndreas Gohr    public function save($file)
423605f8e8dSAndreas Gohr    {
424605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_AUTO) {
425530d6729SAndreas Gohr            $this->setCompression($this->complevel, $this->filetype($file));
426605f8e8dSAndreas Gohr        }
427605f8e8dSAndreas Gohr
428ddb94cf0SAndreas Gohr        if (!@file_put_contents($file, $this->getArchive())) {
429605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not write to file: '.$file);
430605f8e8dSAndreas Gohr        }
431605f8e8dSAndreas Gohr    }
432605f8e8dSAndreas Gohr
433605f8e8dSAndreas Gohr    /**
434605f8e8dSAndreas Gohr     * Read from the open file pointer
435605f8e8dSAndreas Gohr     *
436605f8e8dSAndreas Gohr     * @param int $length bytes to read
437605f8e8dSAndreas Gohr     * @return string
438605f8e8dSAndreas Gohr     */
439605f8e8dSAndreas Gohr    protected function readbytes($length)
440605f8e8dSAndreas Gohr    {
441605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
442605f8e8dSAndreas Gohr            return @gzread($this->fh, $length);
443605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
444605f8e8dSAndreas Gohr            return @bzread($this->fh, $length);
445605f8e8dSAndreas Gohr        } else {
446605f8e8dSAndreas Gohr            return @fread($this->fh, $length);
447605f8e8dSAndreas Gohr        }
448605f8e8dSAndreas Gohr    }
449605f8e8dSAndreas Gohr
450605f8e8dSAndreas Gohr    /**
451605f8e8dSAndreas Gohr     * Write to the open filepointer or memory
452605f8e8dSAndreas Gohr     *
453605f8e8dSAndreas Gohr     * @param string $data
454605f8e8dSAndreas Gohr     * @throws ArchiveIOException
455605f8e8dSAndreas Gohr     * @return int number of bytes written
456605f8e8dSAndreas Gohr     */
457605f8e8dSAndreas Gohr    protected function writebytes($data)
458605f8e8dSAndreas Gohr    {
459605f8e8dSAndreas Gohr        if (!$this->file) {
460605f8e8dSAndreas Gohr            $this->memory .= $data;
461605f8e8dSAndreas Gohr            $written = strlen($data);
462605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_GZIP) {
463605f8e8dSAndreas Gohr            $written = @gzwrite($this->fh, $data);
464605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
465605f8e8dSAndreas Gohr            $written = @bzwrite($this->fh, $data);
466605f8e8dSAndreas Gohr        } else {
467605f8e8dSAndreas Gohr            $written = @fwrite($this->fh, $data);
468605f8e8dSAndreas Gohr        }
469605f8e8dSAndreas Gohr        if ($written === false) {
470605f8e8dSAndreas Gohr            throw new ArchiveIOException('Failed to write to archive stream');
471605f8e8dSAndreas Gohr        }
472605f8e8dSAndreas Gohr        return $written;
473605f8e8dSAndreas Gohr    }
474605f8e8dSAndreas Gohr
475605f8e8dSAndreas Gohr    /**
476605f8e8dSAndreas Gohr     * Skip forward in the open file pointer
477605f8e8dSAndreas Gohr     *
478605f8e8dSAndreas Gohr     * This is basically a wrapper around seek() (and a workaround for bzip2)
479605f8e8dSAndreas Gohr     *
480605f8e8dSAndreas Gohr     * @param int $bytes seek to this position
481605f8e8dSAndreas Gohr     */
482ddb94cf0SAndreas Gohr    protected function skipbytes($bytes)
483605f8e8dSAndreas Gohr    {
484605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
485605f8e8dSAndreas Gohr            @gzseek($this->fh, $bytes, SEEK_CUR);
486605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
487605f8e8dSAndreas Gohr            // there is no seek in bzip2, we simply read on
488530d6729SAndreas Gohr            // bzread allows to read a max of 8kb at once
489530d6729SAndreas Gohr            while($bytes) {
490530d6729SAndreas Gohr                $toread = min(8192, $bytes);
491530d6729SAndreas Gohr                @bzread($this->fh, $toread);
492530d6729SAndreas Gohr                $bytes -= $toread;
493530d6729SAndreas Gohr            }
494605f8e8dSAndreas Gohr        } else {
495605f8e8dSAndreas Gohr            @fseek($this->fh, $bytes, SEEK_CUR);
496605f8e8dSAndreas Gohr        }
497605f8e8dSAndreas Gohr    }
498605f8e8dSAndreas Gohr
499605f8e8dSAndreas Gohr    /**
500e43cd7e1SAndreas Gohr     * Write the given file meta data as header
501605f8e8dSAndreas Gohr     *
502605f8e8dSAndreas Gohr     * @param FileInfo $fileinfo
503e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
504605f8e8dSAndreas Gohr     */
505605f8e8dSAndreas Gohr    protected function writeFileHeader(FileInfo $fileinfo)
506605f8e8dSAndreas Gohr    {
507605f8e8dSAndreas Gohr        $this->writeRawFileHeader(
508605f8e8dSAndreas Gohr            $fileinfo->getPath(),
509605f8e8dSAndreas Gohr            $fileinfo->getUid(),
510605f8e8dSAndreas Gohr            $fileinfo->getGid(),
511605f8e8dSAndreas Gohr            $fileinfo->getMode(),
512605f8e8dSAndreas Gohr            $fileinfo->getSize(),
513605f8e8dSAndreas Gohr            $fileinfo->getMtime(),
514605f8e8dSAndreas Gohr            $fileinfo->getIsdir() ? '5' : '0'
515605f8e8dSAndreas Gohr        );
516605f8e8dSAndreas Gohr    }
517605f8e8dSAndreas Gohr
518605f8e8dSAndreas Gohr    /**
519605f8e8dSAndreas Gohr     * Write a file header to the stream
520605f8e8dSAndreas Gohr     *
521605f8e8dSAndreas Gohr     * @param string $name
522605f8e8dSAndreas Gohr     * @param int $uid
523605f8e8dSAndreas Gohr     * @param int $gid
524605f8e8dSAndreas Gohr     * @param int $perm
525605f8e8dSAndreas Gohr     * @param int $size
526605f8e8dSAndreas Gohr     * @param int $mtime
527605f8e8dSAndreas Gohr     * @param string $typeflag Set to '5' for directories
528e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
529605f8e8dSAndreas Gohr     */
530605f8e8dSAndreas Gohr    protected function writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '')
531605f8e8dSAndreas Gohr    {
532605f8e8dSAndreas Gohr        // handle filename length restrictions
533605f8e8dSAndreas Gohr        $prefix  = '';
534605f8e8dSAndreas Gohr        $namelen = strlen($name);
535605f8e8dSAndreas Gohr        if ($namelen > 100) {
536605f8e8dSAndreas Gohr            $file = basename($name);
537605f8e8dSAndreas Gohr            $dir  = dirname($name);
538605f8e8dSAndreas Gohr            if (strlen($file) > 100 || strlen($dir) > 155) {
539605f8e8dSAndreas Gohr                // we're still too large, let's use GNU longlink
540605f8e8dSAndreas Gohr                $this->writeRawFileHeader('././@LongLink', 0, 0, 0, $namelen, 0, 'L');
541605f8e8dSAndreas Gohr                for ($s = 0; $s < $namelen; $s += 512) {
542605f8e8dSAndreas Gohr                    $this->writebytes(pack("a512", substr($name, $s, 512)));
543605f8e8dSAndreas Gohr                }
544605f8e8dSAndreas Gohr                $name = substr($name, 0, 100); // cut off name
545605f8e8dSAndreas Gohr            } else {
546605f8e8dSAndreas Gohr                // we're fine when splitting, use POSIX ustar
547605f8e8dSAndreas Gohr                $prefix = $dir;
548605f8e8dSAndreas Gohr                $name   = $file;
549605f8e8dSAndreas Gohr            }
550605f8e8dSAndreas Gohr        }
551605f8e8dSAndreas Gohr
552605f8e8dSAndreas Gohr        // values are needed in octal
553605f8e8dSAndreas Gohr        $uid   = sprintf("%6s ", decoct($uid));
554605f8e8dSAndreas Gohr        $gid   = sprintf("%6s ", decoct($gid));
555605f8e8dSAndreas Gohr        $perm  = sprintf("%6s ", decoct($perm));
556605f8e8dSAndreas Gohr        $size  = sprintf("%11s ", decoct($size));
557605f8e8dSAndreas Gohr        $mtime = sprintf("%11s", decoct($mtime));
558605f8e8dSAndreas Gohr
559605f8e8dSAndreas Gohr        $data_first = pack("a100a8a8a8a12A12", $name, $perm, $uid, $gid, $size, $mtime);
560605f8e8dSAndreas Gohr        $data_last  = pack("a1a100a6a2a32a32a8a8a155a12", $typeflag, '', 'ustar', '', '', '', '', '', $prefix, "");
561605f8e8dSAndreas Gohr
562605f8e8dSAndreas Gohr        for ($i = 0, $chks = 0; $i < 148; $i++) {
563605f8e8dSAndreas Gohr            $chks += ord($data_first[$i]);
564605f8e8dSAndreas Gohr        }
565605f8e8dSAndreas Gohr
566605f8e8dSAndreas Gohr        for ($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++) {
567605f8e8dSAndreas Gohr            $chks += ord($data_last[$j]);
568605f8e8dSAndreas Gohr        }
569605f8e8dSAndreas Gohr
570605f8e8dSAndreas Gohr        $this->writebytes($data_first);
571605f8e8dSAndreas Gohr
572605f8e8dSAndreas Gohr        $chks = pack("a8", sprintf("%6s ", decoct($chks)));
573605f8e8dSAndreas Gohr        $this->writebytes($chks.$data_last);
574605f8e8dSAndreas Gohr    }
575605f8e8dSAndreas Gohr
576605f8e8dSAndreas Gohr    /**
577605f8e8dSAndreas Gohr     * Decode the given tar file header
578605f8e8dSAndreas Gohr     *
579530d6729SAndreas Gohr     * @param string $block a 512 byte block containing the header data
580530d6729SAndreas Gohr     * @return array|false returns false when this was a null block
581530d6729SAndreas Gohr     * @throws ArchiveCorruptedException
582605f8e8dSAndreas Gohr     */
583605f8e8dSAndreas Gohr    protected function parseHeader($block)
584605f8e8dSAndreas Gohr    {
585605f8e8dSAndreas Gohr        if (!$block || strlen($block) != 512) {
586530d6729SAndreas Gohr            throw new ArchiveCorruptedException('Unexpected length of header');
587605f8e8dSAndreas Gohr        }
588605f8e8dSAndreas Gohr
589530d6729SAndreas Gohr        // null byte blocks are ignored
590530d6729SAndreas Gohr        if(trim($block) === '') return false;
591530d6729SAndreas Gohr
592605f8e8dSAndreas Gohr        for ($i = 0, $chks = 0; $i < 148; $i++) {
593605f8e8dSAndreas Gohr            $chks += ord($block[$i]);
594605f8e8dSAndreas Gohr        }
595605f8e8dSAndreas Gohr
596605f8e8dSAndreas Gohr        for ($i = 156, $chks += 256; $i < 512; $i++) {
597605f8e8dSAndreas Gohr            $chks += ord($block[$i]);
598605f8e8dSAndreas Gohr        }
599605f8e8dSAndreas Gohr
600605f8e8dSAndreas Gohr        $header = @unpack(
601605f8e8dSAndreas Gohr            "a100filename/a8perm/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix",
602605f8e8dSAndreas Gohr            $block
603605f8e8dSAndreas Gohr        );
604605f8e8dSAndreas Gohr        if (!$header) {
605530d6729SAndreas Gohr            throw new ArchiveCorruptedException('Failed to parse header');
606605f8e8dSAndreas Gohr        }
607605f8e8dSAndreas Gohr
608605f8e8dSAndreas Gohr        $return['checksum'] = OctDec(trim($header['checksum']));
609605f8e8dSAndreas Gohr        if ($return['checksum'] != $chks) {
610a3bfbb3cSAndreas Gohr            throw new ArchiveCorruptedException('Header does not match its checksum');
611605f8e8dSAndreas Gohr        }
612605f8e8dSAndreas Gohr
613605f8e8dSAndreas Gohr        $return['filename'] = trim($header['filename']);
614605f8e8dSAndreas Gohr        $return['perm']     = OctDec(trim($header['perm']));
615605f8e8dSAndreas Gohr        $return['uid']      = OctDec(trim($header['uid']));
616605f8e8dSAndreas Gohr        $return['gid']      = OctDec(trim($header['gid']));
617605f8e8dSAndreas Gohr        $return['size']     = OctDec(trim($header['size']));
618605f8e8dSAndreas Gohr        $return['mtime']    = OctDec(trim($header['mtime']));
619605f8e8dSAndreas Gohr        $return['typeflag'] = $header['typeflag'];
620605f8e8dSAndreas Gohr        $return['link']     = trim($header['link']);
621605f8e8dSAndreas Gohr        $return['uname']    = trim($header['uname']);
622605f8e8dSAndreas Gohr        $return['gname']    = trim($header['gname']);
623605f8e8dSAndreas Gohr
624605f8e8dSAndreas Gohr        // Handle ustar Posix compliant path prefixes
625605f8e8dSAndreas Gohr        if (trim($header['prefix'])) {
626605f8e8dSAndreas Gohr            $return['filename'] = trim($header['prefix']).'/'.$return['filename'];
627605f8e8dSAndreas Gohr        }
628605f8e8dSAndreas Gohr
629605f8e8dSAndreas Gohr        // Handle Long-Link entries from GNU Tar
630605f8e8dSAndreas Gohr        if ($return['typeflag'] == 'L') {
631605f8e8dSAndreas Gohr            // following data block(s) is the filename
63236113441SAndreas Gohr            $filename = trim($this->readbytes(ceil($return['size'] / 512) * 512));
633605f8e8dSAndreas Gohr            // next block is the real header
634605f8e8dSAndreas Gohr            $block  = $this->readbytes(512);
635605f8e8dSAndreas Gohr            $return = $this->parseHeader($block);
636605f8e8dSAndreas Gohr            // overwrite the filename
637605f8e8dSAndreas Gohr            $return['filename'] = $filename;
638605f8e8dSAndreas Gohr        }
639605f8e8dSAndreas Gohr
640605f8e8dSAndreas Gohr        return $return;
641605f8e8dSAndreas Gohr    }
642605f8e8dSAndreas Gohr
643605f8e8dSAndreas Gohr    /**
644605f8e8dSAndreas Gohr     * Creates a FileInfo object from the given parsed header
645605f8e8dSAndreas Gohr     *
646605f8e8dSAndreas Gohr     * @param $header
647605f8e8dSAndreas Gohr     * @return FileInfo
648605f8e8dSAndreas Gohr     */
649605f8e8dSAndreas Gohr    protected function header2fileinfo($header)
650605f8e8dSAndreas Gohr    {
651605f8e8dSAndreas Gohr        $fileinfo = new FileInfo();
652605f8e8dSAndreas Gohr        $fileinfo->setPath($header['filename']);
653605f8e8dSAndreas Gohr        $fileinfo->setMode($header['perm']);
654605f8e8dSAndreas Gohr        $fileinfo->setUid($header['uid']);
655605f8e8dSAndreas Gohr        $fileinfo->setGid($header['gid']);
656605f8e8dSAndreas Gohr        $fileinfo->setSize($header['size']);
657605f8e8dSAndreas Gohr        $fileinfo->setMtime($header['mtime']);
658605f8e8dSAndreas Gohr        $fileinfo->setOwner($header['uname']);
659605f8e8dSAndreas Gohr        $fileinfo->setGroup($header['gname']);
660605f8e8dSAndreas Gohr        $fileinfo->setIsdir((bool) $header['typeflag']);
661605f8e8dSAndreas Gohr
662605f8e8dSAndreas Gohr        return $fileinfo;
663605f8e8dSAndreas Gohr    }
664605f8e8dSAndreas Gohr
665605f8e8dSAndreas Gohr    /**
666605f8e8dSAndreas Gohr     * Checks if the given compression type is available and throws an exception if not
667605f8e8dSAndreas Gohr     *
668605f8e8dSAndreas Gohr     * @param $comptype
669605f8e8dSAndreas Gohr     * @throws ArchiveIllegalCompressionException
670605f8e8dSAndreas Gohr     */
671605f8e8dSAndreas Gohr    protected function compressioncheck($comptype)
672605f8e8dSAndreas Gohr    {
673605f8e8dSAndreas Gohr        if ($comptype === Archive::COMPRESS_GZIP && !function_exists('gzopen')) {
674605f8e8dSAndreas Gohr            throw new ArchiveIllegalCompressionException('No gzip support available');
675605f8e8dSAndreas Gohr        }
676605f8e8dSAndreas Gohr
677605f8e8dSAndreas Gohr        if ($comptype === Archive::COMPRESS_BZIP && !function_exists('bzopen')) {
678605f8e8dSAndreas Gohr            throw new ArchiveIllegalCompressionException('No bzip2 support available');
679605f8e8dSAndreas Gohr        }
680605f8e8dSAndreas Gohr    }
681605f8e8dSAndreas Gohr
682605f8e8dSAndreas Gohr    /**
683530d6729SAndreas Gohr     * Guesses the wanted compression from the given file
684530d6729SAndreas Gohr     *
685530d6729SAndreas Gohr     * Uses magic bytes for existing files, the file extension otherwise
686605f8e8dSAndreas Gohr     *
687605f8e8dSAndreas Gohr     * You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere
688605f8e8dSAndreas Gohr     *
689605f8e8dSAndreas Gohr     * @param string $file
690605f8e8dSAndreas Gohr     * @return int
691605f8e8dSAndreas Gohr     */
692605f8e8dSAndreas Gohr    public function filetype($file)
693605f8e8dSAndreas Gohr    {
694530d6729SAndreas Gohr        // for existing files, try to read the magic bytes
695530d6729SAndreas Gohr        if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
696ddb94cf0SAndreas Gohr            $fh = @fopen($file, 'rb');
697530d6729SAndreas Gohr            if(!$fh) return false;
698530d6729SAndreas Gohr            $magic = fread($fh, 5);
699530d6729SAndreas Gohr            fclose($fh);
700530d6729SAndreas Gohr
701530d6729SAndreas Gohr            if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP;
702530d6729SAndreas Gohr            if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP;
703530d6729SAndreas Gohr        }
704530d6729SAndreas Gohr
705530d6729SAndreas Gohr        // otherwise rely on file name
706605f8e8dSAndreas Gohr        $file = strtolower($file);
707605f8e8dSAndreas Gohr        if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') {
708530d6729SAndreas Gohr            return Archive::COMPRESS_GZIP;
709605f8e8dSAndreas Gohr        } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
710530d6729SAndreas Gohr            return Archive::COMPRESS_BZIP;
711605f8e8dSAndreas Gohr        }
712530d6729SAndreas Gohr
713530d6729SAndreas Gohr        return Archive::COMPRESS_NONE;
714605f8e8dSAndreas Gohr    }
715e43cd7e1SAndreas Gohr
716605f8e8dSAndreas Gohr}
717