xref: /dokuwiki/vendor/splitbrain/php-archive/src/Tar.php (revision 6cb05674d342c24351edf98467e681cbaabf9d0b)
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    {
91605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
92605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
93605f8e8dSAndreas Gohr        }
94605f8e8dSAndreas Gohr
95605f8e8dSAndreas Gohr        $result = array();
96605f8e8dSAndreas Gohr        while ($read = $this->readbytes(512)) {
97605f8e8dSAndreas Gohr            $header = $this->parseHeader($read);
98605f8e8dSAndreas Gohr            if (!is_array($header)) {
99605f8e8dSAndreas Gohr                continue;
100605f8e8dSAndreas Gohr            }
101605f8e8dSAndreas Gohr
102605f8e8dSAndreas Gohr            $this->skipbytes(ceil($header['size'] / 512) * 512);
103605f8e8dSAndreas Gohr            $result[] = $this->header2fileinfo($header);
104605f8e8dSAndreas Gohr        }
105605f8e8dSAndreas Gohr
106605f8e8dSAndreas Gohr        $this->close();
107605f8e8dSAndreas Gohr        return $result;
108605f8e8dSAndreas Gohr    }
109605f8e8dSAndreas Gohr
110605f8e8dSAndreas Gohr    /**
111605f8e8dSAndreas Gohr     * Extract an existing TAR archive
112605f8e8dSAndreas Gohr     *
113605f8e8dSAndreas Gohr     * The $strip parameter allows you to strip a certain number of path components from the filenames
114605f8e8dSAndreas Gohr     * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
115605f8e8dSAndreas Gohr     * an integer is passed as $strip.
116605f8e8dSAndreas Gohr     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
117605f8e8dSAndreas Gohr     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
118605f8e8dSAndreas Gohr     *
119605f8e8dSAndreas Gohr     * By default this will extract all files found in the archive. You can restrict the output using the $include
120605f8e8dSAndreas Gohr     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
121605f8e8dSAndreas Gohr     * $include is set only files that match this expression will be extracted. Files that match the $exclude
122605f8e8dSAndreas Gohr     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
123605f8e8dSAndreas Gohr     * stripped filenames as described above.
124605f8e8dSAndreas Gohr     *
125605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
126605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
127605f8e8dSAndreas Gohr     *
128605f8e8dSAndreas Gohr     * @param string $outdir the target directory for extracting
129605f8e8dSAndreas Gohr     * @param int|string $strip either the number of path components or a fixed prefix to strip
130605f8e8dSAndreas Gohr     * @param string $exclude a regular expression of files to exclude
131605f8e8dSAndreas Gohr     * @param string $include a regular expression of files to include
132605f8e8dSAndreas Gohr     * @throws ArchiveIOException
133e43cd7e1SAndreas Gohr     * @throws ArchiveCorruptedException
134605f8e8dSAndreas Gohr     * @return FileInfo[]
135605f8e8dSAndreas Gohr     */
136605f8e8dSAndreas Gohr    public function extract($outdir, $strip = '', $exclude = '', $include = '')
137605f8e8dSAndreas Gohr    {
138605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
139605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
140605f8e8dSAndreas Gohr        }
141605f8e8dSAndreas Gohr
142605f8e8dSAndreas Gohr        $outdir = rtrim($outdir, '/');
143605f8e8dSAndreas Gohr        @mkdir($outdir, 0777, true);
144605f8e8dSAndreas Gohr        if (!is_dir($outdir)) {
145605f8e8dSAndreas Gohr            throw new ArchiveIOException("Could not create directory '$outdir'");
146605f8e8dSAndreas Gohr        }
147605f8e8dSAndreas Gohr
148605f8e8dSAndreas Gohr        $extracted = array();
149605f8e8dSAndreas Gohr        while ($dat = $this->readbytes(512)) {
150605f8e8dSAndreas Gohr            // read the file header
151605f8e8dSAndreas Gohr            $header = $this->parseHeader($dat);
152605f8e8dSAndreas Gohr            if (!is_array($header)) {
153605f8e8dSAndreas Gohr                continue;
154605f8e8dSAndreas Gohr            }
155605f8e8dSAndreas Gohr            $fileinfo = $this->header2fileinfo($header);
156605f8e8dSAndreas Gohr
157605f8e8dSAndreas Gohr            // apply strip rules
158605f8e8dSAndreas Gohr            $fileinfo->strip($strip);
159605f8e8dSAndreas Gohr
160605f8e8dSAndreas Gohr            // skip unwanted files
161a3bfbb3cSAndreas Gohr            if (!strlen($fileinfo->getPath()) || !$fileinfo->matchExpression($include, $exclude)) {
162605f8e8dSAndreas Gohr                $this->skipbytes(ceil($header['size'] / 512) * 512);
163605f8e8dSAndreas Gohr                continue;
164605f8e8dSAndreas Gohr            }
165605f8e8dSAndreas Gohr
166605f8e8dSAndreas Gohr            // create output directory
167605f8e8dSAndreas Gohr            $output    = $outdir.'/'.$fileinfo->getPath();
168605f8e8dSAndreas Gohr            $directory = ($fileinfo->getIsdir()) ? $output : dirname($output);
169605f8e8dSAndreas Gohr            @mkdir($directory, 0777, true);
170605f8e8dSAndreas Gohr
171605f8e8dSAndreas Gohr            // extract data
172605f8e8dSAndreas Gohr            if (!$fileinfo->getIsdir()) {
173ddb94cf0SAndreas Gohr                $fp = @fopen($output, "wb");
174605f8e8dSAndreas Gohr                if (!$fp) {
175605f8e8dSAndreas Gohr                    throw new ArchiveIOException('Could not open file for writing: '.$output);
176605f8e8dSAndreas Gohr                }
177605f8e8dSAndreas Gohr
178605f8e8dSAndreas Gohr                $size = floor($header['size'] / 512);
179605f8e8dSAndreas Gohr                for ($i = 0; $i < $size; $i++) {
180605f8e8dSAndreas Gohr                    fwrite($fp, $this->readbytes(512), 512);
181605f8e8dSAndreas Gohr                }
182605f8e8dSAndreas Gohr                if (($header['size'] % 512) != 0) {
183605f8e8dSAndreas Gohr                    fwrite($fp, $this->readbytes(512), $header['size'] % 512);
184605f8e8dSAndreas Gohr                }
185605f8e8dSAndreas Gohr
186605f8e8dSAndreas Gohr                fclose($fp);
187e43cd7e1SAndreas Gohr                @touch($output, $fileinfo->getMtime());
188e43cd7e1SAndreas Gohr                @chmod($output, $fileinfo->getMode());
189605f8e8dSAndreas Gohr            } else {
190605f8e8dSAndreas Gohr                $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories
191605f8e8dSAndreas Gohr            }
192605f8e8dSAndreas Gohr
193e43cd7e1SAndreas Gohr            if(is_callable($this->callback)) {
194e43cd7e1SAndreas Gohr                call_user_func($this->callback, $fileinfo);
195e43cd7e1SAndreas Gohr            }
196605f8e8dSAndreas Gohr            $extracted[] = $fileinfo;
197605f8e8dSAndreas Gohr        }
198605f8e8dSAndreas Gohr
199605f8e8dSAndreas Gohr        $this->close();
200605f8e8dSAndreas Gohr        return $extracted;
201605f8e8dSAndreas Gohr    }
202605f8e8dSAndreas Gohr
203605f8e8dSAndreas Gohr    /**
204605f8e8dSAndreas Gohr     * Create a new TAR file
205605f8e8dSAndreas Gohr     *
206605f8e8dSAndreas Gohr     * If $file is empty, the tar file will be created in memory
207605f8e8dSAndreas Gohr     *
208605f8e8dSAndreas Gohr     * @param string $file
209605f8e8dSAndreas Gohr     * @throws ArchiveIOException
210e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
211605f8e8dSAndreas Gohr     */
212605f8e8dSAndreas Gohr    public function create($file = '')
213605f8e8dSAndreas Gohr    {
214605f8e8dSAndreas Gohr        $this->file   = $file;
215605f8e8dSAndreas Gohr        $this->memory = '';
216605f8e8dSAndreas Gohr        $this->fh     = 0;
217605f8e8dSAndreas Gohr
218605f8e8dSAndreas Gohr        if ($this->file) {
219605f8e8dSAndreas Gohr            // determine compression
220605f8e8dSAndreas Gohr            if ($this->comptype == Archive::COMPRESS_AUTO) {
221605f8e8dSAndreas Gohr                $this->setCompression($this->complevel, $this->filetype($file));
222605f8e8dSAndreas Gohr            }
223605f8e8dSAndreas Gohr
224605f8e8dSAndreas Gohr            if ($this->comptype === Archive::COMPRESS_GZIP) {
225605f8e8dSAndreas Gohr                $this->fh = @gzopen($this->file, 'wb'.$this->complevel);
226605f8e8dSAndreas Gohr            } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
227605f8e8dSAndreas Gohr                $this->fh = @bzopen($this->file, 'w');
228605f8e8dSAndreas Gohr            } else {
229605f8e8dSAndreas Gohr                $this->fh = @fopen($this->file, 'wb');
230605f8e8dSAndreas Gohr            }
231605f8e8dSAndreas Gohr
232605f8e8dSAndreas Gohr            if (!$this->fh) {
233605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for writing: '.$this->file);
234605f8e8dSAndreas Gohr            }
235605f8e8dSAndreas Gohr        }
236605f8e8dSAndreas Gohr        $this->writeaccess = true;
237605f8e8dSAndreas Gohr        $this->closed      = false;
238605f8e8dSAndreas Gohr    }
239605f8e8dSAndreas Gohr
240605f8e8dSAndreas Gohr    /**
241605f8e8dSAndreas Gohr     * Add a file to the current TAR archive using an existing file in the filesystem
242605f8e8dSAndreas Gohr     *
243605f8e8dSAndreas Gohr     * @param string $file path to the original file
244605f8e8dSAndreas 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
24536113441SAndreas Gohr     * @throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted
24636113441SAndreas Gohr     * @throws ArchiveIOException there was trouble reading the given file, it was not added
247e43cd7e1SAndreas Gohr     * @throws FileInfoException trouble reading file info, it was not added
248605f8e8dSAndreas Gohr     */
249605f8e8dSAndreas Gohr    public function addFile($file, $fileinfo = '')
250605f8e8dSAndreas Gohr    {
251605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
252605f8e8dSAndreas Gohr            $fileinfo = FileInfo::fromPath($file, $fileinfo);
253605f8e8dSAndreas Gohr        }
254605f8e8dSAndreas Gohr
255605f8e8dSAndreas Gohr        if ($this->closed) {
256605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
257605f8e8dSAndreas Gohr        }
258605f8e8dSAndreas Gohr
259*6cb05674SAndreas Gohr        // create file header
260*6cb05674SAndreas Gohr        $this->writeFileHeader($fileinfo);
261*6cb05674SAndreas Gohr
262*6cb05674SAndreas Gohr        // write data, but only if we have data to write.
263*6cb05674SAndreas Gohr        // note: on Windows fopen() on a directory will fail, so we prevent
264*6cb05674SAndreas Gohr        // errors on Windows by testing if we have data to write.
265*6cb05674SAndreas Gohr        if (!$fileinfo->getIsdir() && $fileinfo->getSize() > 0) {
266*6cb05674SAndreas Gohr            $read = 0;
267ddb94cf0SAndreas Gohr            $fp = @fopen($file, 'rb');
268605f8e8dSAndreas Gohr            if (!$fp) {
269605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for reading: ' . $file);
270605f8e8dSAndreas Gohr            }
271605f8e8dSAndreas Gohr            while (!feof($fp)) {
272605f8e8dSAndreas Gohr                $data = fread($fp, 512);
27336113441SAndreas Gohr                $read += strlen($data);
274605f8e8dSAndreas Gohr                if ($data === false) {
275605f8e8dSAndreas Gohr                    break;
276605f8e8dSAndreas Gohr                }
277605f8e8dSAndreas Gohr                if ($data === '') {
278605f8e8dSAndreas Gohr                    break;
279605f8e8dSAndreas Gohr                }
280605f8e8dSAndreas Gohr                $packed = pack("a512", $data);
281605f8e8dSAndreas Gohr                $this->writebytes($packed);
282605f8e8dSAndreas Gohr            }
283605f8e8dSAndreas Gohr            fclose($fp);
28436113441SAndreas Gohr
28536113441SAndreas Gohr            if ($read != $fileinfo->getSize()) {
28636113441SAndreas Gohr                $this->close();
28736113441SAndreas Gohr                throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
28836113441SAndreas Gohr            }
289*6cb05674SAndreas Gohr        }
290e43cd7e1SAndreas Gohr
291e43cd7e1SAndreas Gohr        if(is_callable($this->callback)) {
292e43cd7e1SAndreas Gohr            call_user_func($this->callback, $fileinfo);
293e43cd7e1SAndreas Gohr        }
294605f8e8dSAndreas Gohr    }
295605f8e8dSAndreas Gohr
296605f8e8dSAndreas Gohr    /**
297605f8e8dSAndreas Gohr     * Add a file to the current TAR archive using the given $data as content
298605f8e8dSAndreas Gohr     *
299605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
300605f8e8dSAndreas Gohr     * @param string          $data     binary content of the file to add
301605f8e8dSAndreas Gohr     * @throws ArchiveIOException
302605f8e8dSAndreas Gohr     */
303605f8e8dSAndreas Gohr    public function addData($fileinfo, $data)
304605f8e8dSAndreas Gohr    {
305605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
306605f8e8dSAndreas Gohr            $fileinfo = new FileInfo($fileinfo);
307605f8e8dSAndreas Gohr        }
308605f8e8dSAndreas Gohr
309605f8e8dSAndreas Gohr        if ($this->closed) {
310605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
311605f8e8dSAndreas Gohr        }
312605f8e8dSAndreas Gohr
313605f8e8dSAndreas Gohr        $len = strlen($data);
314605f8e8dSAndreas Gohr        $fileinfo->setSize($len);
315605f8e8dSAndreas Gohr        $this->writeFileHeader($fileinfo);
316605f8e8dSAndreas Gohr
317605f8e8dSAndreas Gohr        for ($s = 0; $s < $len; $s += 512) {
318605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", substr($data, $s, 512)));
319605f8e8dSAndreas Gohr        }
320e43cd7e1SAndreas Gohr
321e43cd7e1SAndreas Gohr        if (is_callable($this->callback)) {
322e43cd7e1SAndreas Gohr            call_user_func($this->callback, $fileinfo);
323e43cd7e1SAndreas Gohr        }
324605f8e8dSAndreas Gohr    }
325605f8e8dSAndreas Gohr
326605f8e8dSAndreas Gohr    /**
327605f8e8dSAndreas Gohr     * Add the closing footer to the archive if in write mode, close all file handles
328605f8e8dSAndreas Gohr     *
329605f8e8dSAndreas Gohr     * After a call to this function no more data can be added to the archive, for
330605f8e8dSAndreas Gohr     * read access no reading is allowed anymore
331605f8e8dSAndreas Gohr     *
332605f8e8dSAndreas Gohr     * "Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which
333605f8e8dSAndreas Gohr     * consists of two 512 blocks of zero bytes"
334605f8e8dSAndreas Gohr     *
335605f8e8dSAndreas Gohr     * @link http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134
336e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
337605f8e8dSAndreas Gohr     */
338605f8e8dSAndreas Gohr    public function close()
339605f8e8dSAndreas Gohr    {
340605f8e8dSAndreas Gohr        if ($this->closed) {
341605f8e8dSAndreas Gohr            return;
342605f8e8dSAndreas Gohr        } // we did this already
343605f8e8dSAndreas Gohr
344605f8e8dSAndreas Gohr        // write footer
345605f8e8dSAndreas Gohr        if ($this->writeaccess) {
346605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", ""));
347605f8e8dSAndreas Gohr            $this->writebytes(pack("a512", ""));
348605f8e8dSAndreas Gohr        }
349605f8e8dSAndreas Gohr
350605f8e8dSAndreas Gohr        // close file handles
351605f8e8dSAndreas Gohr        if ($this->file) {
352605f8e8dSAndreas Gohr            if ($this->comptype === Archive::COMPRESS_GZIP) {
353605f8e8dSAndreas Gohr                gzclose($this->fh);
354605f8e8dSAndreas Gohr            } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
355605f8e8dSAndreas Gohr                bzclose($this->fh);
356605f8e8dSAndreas Gohr            } else {
357605f8e8dSAndreas Gohr                fclose($this->fh);
358605f8e8dSAndreas Gohr            }
359605f8e8dSAndreas Gohr
360605f8e8dSAndreas Gohr            $this->file = '';
361605f8e8dSAndreas Gohr            $this->fh   = 0;
362605f8e8dSAndreas Gohr        }
363605f8e8dSAndreas Gohr
364605f8e8dSAndreas Gohr        $this->writeaccess = false;
365605f8e8dSAndreas Gohr        $this->closed      = true;
366605f8e8dSAndreas Gohr    }
367605f8e8dSAndreas Gohr
368605f8e8dSAndreas Gohr    /**
369605f8e8dSAndreas Gohr     * Returns the created in-memory archive data
370605f8e8dSAndreas Gohr     *
371605f8e8dSAndreas Gohr     * This implicitly calls close() on the Archive
372e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
373605f8e8dSAndreas Gohr     */
374605f8e8dSAndreas Gohr    public function getArchive()
375605f8e8dSAndreas Gohr    {
376605f8e8dSAndreas Gohr        $this->close();
377605f8e8dSAndreas Gohr
378605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_AUTO) {
379605f8e8dSAndreas Gohr            $this->comptype = Archive::COMPRESS_NONE;
380605f8e8dSAndreas Gohr        }
381605f8e8dSAndreas Gohr
382605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
383dd7064d9SAndreas Gohr            return gzencode($this->memory, $this->complevel);
384605f8e8dSAndreas Gohr        }
385605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_BZIP) {
386605f8e8dSAndreas Gohr            return bzcompress($this->memory);
387605f8e8dSAndreas Gohr        }
388605f8e8dSAndreas Gohr        return $this->memory;
389605f8e8dSAndreas Gohr    }
390605f8e8dSAndreas Gohr
391605f8e8dSAndreas Gohr    /**
392605f8e8dSAndreas Gohr     * Save the created in-memory archive data
393605f8e8dSAndreas Gohr     *
394605f8e8dSAndreas Gohr     * Note: It more memory effective to specify the filename in the create() function and
395605f8e8dSAndreas Gohr     * let the library work on the new file directly.
396605f8e8dSAndreas Gohr     *
397605f8e8dSAndreas Gohr     * @param string $file
398605f8e8dSAndreas Gohr     * @throws ArchiveIOException
399e43cd7e1SAndreas Gohr     * @throws ArchiveIllegalCompressionException
400605f8e8dSAndreas Gohr     */
401605f8e8dSAndreas Gohr    public function save($file)
402605f8e8dSAndreas Gohr    {
403605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_AUTO) {
404530d6729SAndreas Gohr            $this->setCompression($this->complevel, $this->filetype($file));
405605f8e8dSAndreas Gohr        }
406605f8e8dSAndreas Gohr
407ddb94cf0SAndreas Gohr        if (!@file_put_contents($file, $this->getArchive())) {
408605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not write to file: '.$file);
409605f8e8dSAndreas Gohr        }
410605f8e8dSAndreas Gohr    }
411605f8e8dSAndreas Gohr
412605f8e8dSAndreas Gohr    /**
413605f8e8dSAndreas Gohr     * Read from the open file pointer
414605f8e8dSAndreas Gohr     *
415605f8e8dSAndreas Gohr     * @param int $length bytes to read
416605f8e8dSAndreas Gohr     * @return string
417605f8e8dSAndreas Gohr     */
418605f8e8dSAndreas Gohr    protected function readbytes($length)
419605f8e8dSAndreas Gohr    {
420605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
421605f8e8dSAndreas Gohr            return @gzread($this->fh, $length);
422605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
423605f8e8dSAndreas Gohr            return @bzread($this->fh, $length);
424605f8e8dSAndreas Gohr        } else {
425605f8e8dSAndreas Gohr            return @fread($this->fh, $length);
426605f8e8dSAndreas Gohr        }
427605f8e8dSAndreas Gohr    }
428605f8e8dSAndreas Gohr
429605f8e8dSAndreas Gohr    /**
430605f8e8dSAndreas Gohr     * Write to the open filepointer or memory
431605f8e8dSAndreas Gohr     *
432605f8e8dSAndreas Gohr     * @param string $data
433605f8e8dSAndreas Gohr     * @throws ArchiveIOException
434605f8e8dSAndreas Gohr     * @return int number of bytes written
435605f8e8dSAndreas Gohr     */
436605f8e8dSAndreas Gohr    protected function writebytes($data)
437605f8e8dSAndreas Gohr    {
438605f8e8dSAndreas Gohr        if (!$this->file) {
439605f8e8dSAndreas Gohr            $this->memory .= $data;
440605f8e8dSAndreas Gohr            $written = strlen($data);
441605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_GZIP) {
442605f8e8dSAndreas Gohr            $written = @gzwrite($this->fh, $data);
443605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
444605f8e8dSAndreas Gohr            $written = @bzwrite($this->fh, $data);
445605f8e8dSAndreas Gohr        } else {
446605f8e8dSAndreas Gohr            $written = @fwrite($this->fh, $data);
447605f8e8dSAndreas Gohr        }
448605f8e8dSAndreas Gohr        if ($written === false) {
449605f8e8dSAndreas Gohr            throw new ArchiveIOException('Failed to write to archive stream');
450605f8e8dSAndreas Gohr        }
451605f8e8dSAndreas Gohr        return $written;
452605f8e8dSAndreas Gohr    }
453605f8e8dSAndreas Gohr
454605f8e8dSAndreas Gohr    /**
455605f8e8dSAndreas Gohr     * Skip forward in the open file pointer
456605f8e8dSAndreas Gohr     *
457605f8e8dSAndreas Gohr     * This is basically a wrapper around seek() (and a workaround for bzip2)
458605f8e8dSAndreas Gohr     *
459605f8e8dSAndreas Gohr     * @param int $bytes seek to this position
460605f8e8dSAndreas Gohr     */
461ddb94cf0SAndreas Gohr    protected function skipbytes($bytes)
462605f8e8dSAndreas Gohr    {
463605f8e8dSAndreas Gohr        if ($this->comptype === Archive::COMPRESS_GZIP) {
464605f8e8dSAndreas Gohr            @gzseek($this->fh, $bytes, SEEK_CUR);
465605f8e8dSAndreas Gohr        } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
466605f8e8dSAndreas Gohr            // there is no seek in bzip2, we simply read on
467530d6729SAndreas Gohr            // bzread allows to read a max of 8kb at once
468530d6729SAndreas Gohr            while($bytes) {
469530d6729SAndreas Gohr                $toread = min(8192, $bytes);
470530d6729SAndreas Gohr                @bzread($this->fh, $toread);
471530d6729SAndreas Gohr                $bytes -= $toread;
472530d6729SAndreas Gohr            }
473605f8e8dSAndreas Gohr        } else {
474605f8e8dSAndreas Gohr            @fseek($this->fh, $bytes, SEEK_CUR);
475605f8e8dSAndreas Gohr        }
476605f8e8dSAndreas Gohr    }
477605f8e8dSAndreas Gohr
478605f8e8dSAndreas Gohr    /**
479e43cd7e1SAndreas Gohr     * Write the given file meta data as header
480605f8e8dSAndreas Gohr     *
481605f8e8dSAndreas Gohr     * @param FileInfo $fileinfo
482e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
483605f8e8dSAndreas Gohr     */
484605f8e8dSAndreas Gohr    protected function writeFileHeader(FileInfo $fileinfo)
485605f8e8dSAndreas Gohr    {
486605f8e8dSAndreas Gohr        $this->writeRawFileHeader(
487605f8e8dSAndreas Gohr            $fileinfo->getPath(),
488605f8e8dSAndreas Gohr            $fileinfo->getUid(),
489605f8e8dSAndreas Gohr            $fileinfo->getGid(),
490605f8e8dSAndreas Gohr            $fileinfo->getMode(),
491605f8e8dSAndreas Gohr            $fileinfo->getSize(),
492605f8e8dSAndreas Gohr            $fileinfo->getMtime(),
493605f8e8dSAndreas Gohr            $fileinfo->getIsdir() ? '5' : '0'
494605f8e8dSAndreas Gohr        );
495605f8e8dSAndreas Gohr    }
496605f8e8dSAndreas Gohr
497605f8e8dSAndreas Gohr    /**
498605f8e8dSAndreas Gohr     * Write a file header to the stream
499605f8e8dSAndreas Gohr     *
500605f8e8dSAndreas Gohr     * @param string $name
501605f8e8dSAndreas Gohr     * @param int $uid
502605f8e8dSAndreas Gohr     * @param int $gid
503605f8e8dSAndreas Gohr     * @param int $perm
504605f8e8dSAndreas Gohr     * @param int $size
505605f8e8dSAndreas Gohr     * @param int $mtime
506605f8e8dSAndreas Gohr     * @param string $typeflag Set to '5' for directories
507e43cd7e1SAndreas Gohr     * @throws ArchiveIOException
508605f8e8dSAndreas Gohr     */
509605f8e8dSAndreas Gohr    protected function writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '')
510605f8e8dSAndreas Gohr    {
511605f8e8dSAndreas Gohr        // handle filename length restrictions
512605f8e8dSAndreas Gohr        $prefix  = '';
513605f8e8dSAndreas Gohr        $namelen = strlen($name);
514605f8e8dSAndreas Gohr        if ($namelen > 100) {
515605f8e8dSAndreas Gohr            $file = basename($name);
516605f8e8dSAndreas Gohr            $dir  = dirname($name);
517605f8e8dSAndreas Gohr            if (strlen($file) > 100 || strlen($dir) > 155) {
518605f8e8dSAndreas Gohr                // we're still too large, let's use GNU longlink
519605f8e8dSAndreas Gohr                $this->writeRawFileHeader('././@LongLink', 0, 0, 0, $namelen, 0, 'L');
520605f8e8dSAndreas Gohr                for ($s = 0; $s < $namelen; $s += 512) {
521605f8e8dSAndreas Gohr                    $this->writebytes(pack("a512", substr($name, $s, 512)));
522605f8e8dSAndreas Gohr                }
523605f8e8dSAndreas Gohr                $name = substr($name, 0, 100); // cut off name
524605f8e8dSAndreas Gohr            } else {
525605f8e8dSAndreas Gohr                // we're fine when splitting, use POSIX ustar
526605f8e8dSAndreas Gohr                $prefix = $dir;
527605f8e8dSAndreas Gohr                $name   = $file;
528605f8e8dSAndreas Gohr            }
529605f8e8dSAndreas Gohr        }
530605f8e8dSAndreas Gohr
531605f8e8dSAndreas Gohr        // values are needed in octal
532605f8e8dSAndreas Gohr        $uid   = sprintf("%6s ", decoct($uid));
533605f8e8dSAndreas Gohr        $gid   = sprintf("%6s ", decoct($gid));
534605f8e8dSAndreas Gohr        $perm  = sprintf("%6s ", decoct($perm));
535605f8e8dSAndreas Gohr        $size  = sprintf("%11s ", decoct($size));
536605f8e8dSAndreas Gohr        $mtime = sprintf("%11s", decoct($mtime));
537605f8e8dSAndreas Gohr
538605f8e8dSAndreas Gohr        $data_first = pack("a100a8a8a8a12A12", $name, $perm, $uid, $gid, $size, $mtime);
539605f8e8dSAndreas Gohr        $data_last  = pack("a1a100a6a2a32a32a8a8a155a12", $typeflag, '', 'ustar', '', '', '', '', '', $prefix, "");
540605f8e8dSAndreas Gohr
541605f8e8dSAndreas Gohr        for ($i = 0, $chks = 0; $i < 148; $i++) {
542605f8e8dSAndreas Gohr            $chks += ord($data_first[$i]);
543605f8e8dSAndreas Gohr        }
544605f8e8dSAndreas Gohr
545605f8e8dSAndreas Gohr        for ($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++) {
546605f8e8dSAndreas Gohr            $chks += ord($data_last[$j]);
547605f8e8dSAndreas Gohr        }
548605f8e8dSAndreas Gohr
549605f8e8dSAndreas Gohr        $this->writebytes($data_first);
550605f8e8dSAndreas Gohr
551605f8e8dSAndreas Gohr        $chks = pack("a8", sprintf("%6s ", decoct($chks)));
552605f8e8dSAndreas Gohr        $this->writebytes($chks.$data_last);
553605f8e8dSAndreas Gohr    }
554605f8e8dSAndreas Gohr
555605f8e8dSAndreas Gohr    /**
556605f8e8dSAndreas Gohr     * Decode the given tar file header
557605f8e8dSAndreas Gohr     *
558530d6729SAndreas Gohr     * @param string $block a 512 byte block containing the header data
559530d6729SAndreas Gohr     * @return array|false returns false when this was a null block
560530d6729SAndreas Gohr     * @throws ArchiveCorruptedException
561605f8e8dSAndreas Gohr     */
562605f8e8dSAndreas Gohr    protected function parseHeader($block)
563605f8e8dSAndreas Gohr    {
564605f8e8dSAndreas Gohr        if (!$block || strlen($block) != 512) {
565530d6729SAndreas Gohr            throw new ArchiveCorruptedException('Unexpected length of header');
566605f8e8dSAndreas Gohr        }
567605f8e8dSAndreas Gohr
568530d6729SAndreas Gohr        // null byte blocks are ignored
569530d6729SAndreas Gohr        if(trim($block) === '') return false;
570530d6729SAndreas Gohr
571605f8e8dSAndreas Gohr        for ($i = 0, $chks = 0; $i < 148; $i++) {
572605f8e8dSAndreas Gohr            $chks += ord($block[$i]);
573605f8e8dSAndreas Gohr        }
574605f8e8dSAndreas Gohr
575605f8e8dSAndreas Gohr        for ($i = 156, $chks += 256; $i < 512; $i++) {
576605f8e8dSAndreas Gohr            $chks += ord($block[$i]);
577605f8e8dSAndreas Gohr        }
578605f8e8dSAndreas Gohr
579605f8e8dSAndreas Gohr        $header = @unpack(
580605f8e8dSAndreas Gohr            "a100filename/a8perm/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix",
581605f8e8dSAndreas Gohr            $block
582605f8e8dSAndreas Gohr        );
583605f8e8dSAndreas Gohr        if (!$header) {
584530d6729SAndreas Gohr            throw new ArchiveCorruptedException('Failed to parse header');
585605f8e8dSAndreas Gohr        }
586605f8e8dSAndreas Gohr
587605f8e8dSAndreas Gohr        $return['checksum'] = OctDec(trim($header['checksum']));
588605f8e8dSAndreas Gohr        if ($return['checksum'] != $chks) {
589a3bfbb3cSAndreas Gohr            throw new ArchiveCorruptedException('Header does not match its checksum');
590605f8e8dSAndreas Gohr        }
591605f8e8dSAndreas Gohr
592605f8e8dSAndreas Gohr        $return['filename'] = trim($header['filename']);
593605f8e8dSAndreas Gohr        $return['perm']     = OctDec(trim($header['perm']));
594605f8e8dSAndreas Gohr        $return['uid']      = OctDec(trim($header['uid']));
595605f8e8dSAndreas Gohr        $return['gid']      = OctDec(trim($header['gid']));
596605f8e8dSAndreas Gohr        $return['size']     = OctDec(trim($header['size']));
597605f8e8dSAndreas Gohr        $return['mtime']    = OctDec(trim($header['mtime']));
598605f8e8dSAndreas Gohr        $return['typeflag'] = $header['typeflag'];
599605f8e8dSAndreas Gohr        $return['link']     = trim($header['link']);
600605f8e8dSAndreas Gohr        $return['uname']    = trim($header['uname']);
601605f8e8dSAndreas Gohr        $return['gname']    = trim($header['gname']);
602605f8e8dSAndreas Gohr
603605f8e8dSAndreas Gohr        // Handle ustar Posix compliant path prefixes
604605f8e8dSAndreas Gohr        if (trim($header['prefix'])) {
605605f8e8dSAndreas Gohr            $return['filename'] = trim($header['prefix']).'/'.$return['filename'];
606605f8e8dSAndreas Gohr        }
607605f8e8dSAndreas Gohr
608605f8e8dSAndreas Gohr        // Handle Long-Link entries from GNU Tar
609605f8e8dSAndreas Gohr        if ($return['typeflag'] == 'L') {
610605f8e8dSAndreas Gohr            // following data block(s) is the filename
61136113441SAndreas Gohr            $filename = trim($this->readbytes(ceil($return['size'] / 512) * 512));
612605f8e8dSAndreas Gohr            // next block is the real header
613605f8e8dSAndreas Gohr            $block  = $this->readbytes(512);
614605f8e8dSAndreas Gohr            $return = $this->parseHeader($block);
615605f8e8dSAndreas Gohr            // overwrite the filename
616605f8e8dSAndreas Gohr            $return['filename'] = $filename;
617605f8e8dSAndreas Gohr        }
618605f8e8dSAndreas Gohr
619605f8e8dSAndreas Gohr        return $return;
620605f8e8dSAndreas Gohr    }
621605f8e8dSAndreas Gohr
622605f8e8dSAndreas Gohr    /**
623605f8e8dSAndreas Gohr     * Creates a FileInfo object from the given parsed header
624605f8e8dSAndreas Gohr     *
625605f8e8dSAndreas Gohr     * @param $header
626605f8e8dSAndreas Gohr     * @return FileInfo
627605f8e8dSAndreas Gohr     */
628605f8e8dSAndreas Gohr    protected function header2fileinfo($header)
629605f8e8dSAndreas Gohr    {
630605f8e8dSAndreas Gohr        $fileinfo = new FileInfo();
631605f8e8dSAndreas Gohr        $fileinfo->setPath($header['filename']);
632605f8e8dSAndreas Gohr        $fileinfo->setMode($header['perm']);
633605f8e8dSAndreas Gohr        $fileinfo->setUid($header['uid']);
634605f8e8dSAndreas Gohr        $fileinfo->setGid($header['gid']);
635605f8e8dSAndreas Gohr        $fileinfo->setSize($header['size']);
636605f8e8dSAndreas Gohr        $fileinfo->setMtime($header['mtime']);
637605f8e8dSAndreas Gohr        $fileinfo->setOwner($header['uname']);
638605f8e8dSAndreas Gohr        $fileinfo->setGroup($header['gname']);
639605f8e8dSAndreas Gohr        $fileinfo->setIsdir((bool) $header['typeflag']);
640605f8e8dSAndreas Gohr
641605f8e8dSAndreas Gohr        return $fileinfo;
642605f8e8dSAndreas Gohr    }
643605f8e8dSAndreas Gohr
644605f8e8dSAndreas Gohr    /**
645605f8e8dSAndreas Gohr     * Checks if the given compression type is available and throws an exception if not
646605f8e8dSAndreas Gohr     *
647605f8e8dSAndreas Gohr     * @param $comptype
648605f8e8dSAndreas Gohr     * @throws ArchiveIllegalCompressionException
649605f8e8dSAndreas Gohr     */
650605f8e8dSAndreas Gohr    protected function compressioncheck($comptype)
651605f8e8dSAndreas Gohr    {
652605f8e8dSAndreas Gohr        if ($comptype === Archive::COMPRESS_GZIP && !function_exists('gzopen')) {
653605f8e8dSAndreas Gohr            throw new ArchiveIllegalCompressionException('No gzip support available');
654605f8e8dSAndreas Gohr        }
655605f8e8dSAndreas Gohr
656605f8e8dSAndreas Gohr        if ($comptype === Archive::COMPRESS_BZIP && !function_exists('bzopen')) {
657605f8e8dSAndreas Gohr            throw new ArchiveIllegalCompressionException('No bzip2 support available');
658605f8e8dSAndreas Gohr        }
659605f8e8dSAndreas Gohr    }
660605f8e8dSAndreas Gohr
661605f8e8dSAndreas Gohr    /**
662530d6729SAndreas Gohr     * Guesses the wanted compression from the given file
663530d6729SAndreas Gohr     *
664530d6729SAndreas Gohr     * Uses magic bytes for existing files, the file extension otherwise
665605f8e8dSAndreas Gohr     *
666605f8e8dSAndreas Gohr     * You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere
667605f8e8dSAndreas Gohr     *
668605f8e8dSAndreas Gohr     * @param string $file
669605f8e8dSAndreas Gohr     * @return int
670605f8e8dSAndreas Gohr     */
671605f8e8dSAndreas Gohr    public function filetype($file)
672605f8e8dSAndreas Gohr    {
673530d6729SAndreas Gohr        // for existing files, try to read the magic bytes
674530d6729SAndreas Gohr        if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
675ddb94cf0SAndreas Gohr            $fh = @fopen($file, 'rb');
676530d6729SAndreas Gohr            if(!$fh) return false;
677530d6729SAndreas Gohr            $magic = fread($fh, 5);
678530d6729SAndreas Gohr            fclose($fh);
679530d6729SAndreas Gohr
680530d6729SAndreas Gohr            if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP;
681530d6729SAndreas Gohr            if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP;
682530d6729SAndreas Gohr        }
683530d6729SAndreas Gohr
684530d6729SAndreas Gohr        // otherwise rely on file name
685605f8e8dSAndreas Gohr        $file = strtolower($file);
686605f8e8dSAndreas Gohr        if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') {
687530d6729SAndreas Gohr            return Archive::COMPRESS_GZIP;
688605f8e8dSAndreas Gohr        } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
689530d6729SAndreas Gohr            return Archive::COMPRESS_BZIP;
690605f8e8dSAndreas Gohr        }
691530d6729SAndreas Gohr
692530d6729SAndreas Gohr        return Archive::COMPRESS_NONE;
693605f8e8dSAndreas Gohr    }
694e43cd7e1SAndreas Gohr
695605f8e8dSAndreas Gohr}
696