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{ 18*9520a435SAndreas Gohr const READ_CHUNK_SIZE = 1048576; // 1MB 19605f8e8dSAndreas Gohr 20605f8e8dSAndreas Gohr protected $file = ''; 21605f8e8dSAndreas Gohr protected $comptype = Archive::COMPRESS_AUTO; 22605f8e8dSAndreas Gohr protected $complevel = 9; 23605f8e8dSAndreas Gohr protected $fh; 24605f8e8dSAndreas Gohr protected $memory = ''; 25605f8e8dSAndreas Gohr protected $closed = true; 26605f8e8dSAndreas Gohr protected $writeaccess = false; 27*9520a435SAndreas Gohr protected $position = 0; 28*9520a435SAndreas Gohr protected $contentUntil = 0; 29*9520a435SAndreas Gohr protected $skipUntil = 0; 30605f8e8dSAndreas Gohr 31605f8e8dSAndreas Gohr /** 32605f8e8dSAndreas Gohr * Sets the compression to use 33605f8e8dSAndreas Gohr * 34605f8e8dSAndreas Gohr * @param int $level Compression level (0 to 9) 35605f8e8dSAndreas Gohr * @param int $type Type of compression to use (use COMPRESS_* constants) 36e43cd7e1SAndreas Gohr * @throws ArchiveIllegalCompressionException 37605f8e8dSAndreas Gohr */ 38605f8e8dSAndreas Gohr public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO) 39605f8e8dSAndreas Gohr { 40605f8e8dSAndreas Gohr $this->compressioncheck($type); 41e43cd7e1SAndreas Gohr if ($level < -1 || $level > 9) { 42e43cd7e1SAndreas Gohr throw new ArchiveIllegalCompressionException('Compression level should be between -1 and 9'); 43e43cd7e1SAndreas Gohr } 44605f8e8dSAndreas Gohr $this->comptype = $type; 45605f8e8dSAndreas Gohr $this->complevel = $level; 46530d6729SAndreas Gohr if($level == 0) $this->comptype = Archive::COMPRESS_NONE; 47530d6729SAndreas Gohr if($type == Archive::COMPRESS_NONE) $this->complevel = 0; 48605f8e8dSAndreas Gohr } 49605f8e8dSAndreas Gohr 50605f8e8dSAndreas Gohr /** 51605f8e8dSAndreas Gohr * Open an existing TAR file for reading 52605f8e8dSAndreas Gohr * 53605f8e8dSAndreas Gohr * @param string $file 54605f8e8dSAndreas Gohr * @throws ArchiveIOException 55e43cd7e1SAndreas Gohr * @throws ArchiveIllegalCompressionException 56605f8e8dSAndreas Gohr */ 57605f8e8dSAndreas Gohr public function open($file) 58605f8e8dSAndreas Gohr { 59605f8e8dSAndreas Gohr $this->file = $file; 60605f8e8dSAndreas Gohr 61605f8e8dSAndreas Gohr // update compression to mach file 62605f8e8dSAndreas Gohr if ($this->comptype == Tar::COMPRESS_AUTO) { 63605f8e8dSAndreas Gohr $this->setCompression($this->complevel, $this->filetype($file)); 64605f8e8dSAndreas Gohr } 65605f8e8dSAndreas Gohr 66605f8e8dSAndreas Gohr // open file handles 67605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 68605f8e8dSAndreas Gohr $this->fh = @gzopen($this->file, 'rb'); 69605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 70605f8e8dSAndreas Gohr $this->fh = @bzopen($this->file, 'r'); 71605f8e8dSAndreas Gohr } else { 72605f8e8dSAndreas Gohr $this->fh = @fopen($this->file, 'rb'); 73605f8e8dSAndreas Gohr } 74605f8e8dSAndreas Gohr 75605f8e8dSAndreas Gohr if (!$this->fh) { 76605f8e8dSAndreas Gohr throw new ArchiveIOException('Could not open file for reading: '.$this->file); 77605f8e8dSAndreas Gohr } 78605f8e8dSAndreas Gohr $this->closed = false; 79*9520a435SAndreas Gohr $this->position = 0; 80605f8e8dSAndreas Gohr } 81605f8e8dSAndreas Gohr 82605f8e8dSAndreas Gohr /** 83605f8e8dSAndreas Gohr * Read the contents of a TAR archive 84605f8e8dSAndreas Gohr * 85605f8e8dSAndreas Gohr * This function lists the files stored in the archive 86605f8e8dSAndreas Gohr * 87605f8e8dSAndreas Gohr * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams. 88605f8e8dSAndreas Gohr * Reopen the file with open() again if you want to do additional operations 89605f8e8dSAndreas Gohr * 90605f8e8dSAndreas Gohr * @throws ArchiveIOException 91e43cd7e1SAndreas Gohr * @throws ArchiveCorruptedException 92605f8e8dSAndreas Gohr * @returns FileInfo[] 93605f8e8dSAndreas Gohr */ 94605f8e8dSAndreas Gohr public function contents() 95605f8e8dSAndreas Gohr { 962afbbbaeSAndreas Gohr $result = array(); 972afbbbaeSAndreas Gohr 982afbbbaeSAndreas Gohr foreach ($this->yieldContents() as $fileinfo) { 992afbbbaeSAndreas Gohr $result[] = $fileinfo; 1002afbbbaeSAndreas Gohr } 1012afbbbaeSAndreas Gohr 1022afbbbaeSAndreas Gohr return $result; 1032afbbbaeSAndreas Gohr } 1042afbbbaeSAndreas Gohr 1052afbbbaeSAndreas Gohr /** 1062afbbbaeSAndreas Gohr * Read the contents of a TAR archive and return each entry using yield 1072afbbbaeSAndreas Gohr * for memory efficiency. 1082afbbbaeSAndreas Gohr * 1092afbbbaeSAndreas Gohr * @see contents() 1102afbbbaeSAndreas Gohr * @throws ArchiveIOException 1112afbbbaeSAndreas Gohr * @throws ArchiveCorruptedException 1122afbbbaeSAndreas Gohr * @returns FileInfo[] 1132afbbbaeSAndreas Gohr */ 1142afbbbaeSAndreas Gohr public function yieldContents() 1152afbbbaeSAndreas Gohr { 116605f8e8dSAndreas Gohr if ($this->closed || !$this->file) { 117605f8e8dSAndreas Gohr throw new ArchiveIOException('Can not read from a closed archive'); 118605f8e8dSAndreas Gohr } 119605f8e8dSAndreas Gohr 120605f8e8dSAndreas Gohr while ($read = $this->readbytes(512)) { 121605f8e8dSAndreas Gohr $header = $this->parseHeader($read); 122605f8e8dSAndreas Gohr if (!is_array($header)) { 123605f8e8dSAndreas Gohr continue; 124605f8e8dSAndreas Gohr } 125605f8e8dSAndreas Gohr 126*9520a435SAndreas Gohr $this->contentUntil = $this->position + $header['size']; 127*9520a435SAndreas Gohr $this->skipUntil = $this->position + ceil($header['size'] / 512) * 512; 128*9520a435SAndreas Gohr 1292afbbbaeSAndreas Gohr yield $this->header2fileinfo($header); 130*9520a435SAndreas Gohr 131*9520a435SAndreas Gohr $skip = $this->skipUntil - $this->position; 132*9520a435SAndreas Gohr if ($skip > 0) { 133*9520a435SAndreas Gohr $this->skipbytes($skip); 134*9520a435SAndreas Gohr } 135605f8e8dSAndreas Gohr } 136605f8e8dSAndreas Gohr 137605f8e8dSAndreas Gohr $this->close(); 138*9520a435SAndreas Gohr } 1392afbbbaeSAndreas Gohr 140*9520a435SAndreas Gohr /** 141*9520a435SAndreas Gohr * Reads content of a current archive entry. 142*9520a435SAndreas Gohr * 143*9520a435SAndreas Gohr * Works only when iterating trough the archive using the generator returned 144*9520a435SAndreas Gohr * by the yieldContents(). 145*9520a435SAndreas Gohr * 146*9520a435SAndreas Gohr * @param int $length maximum number of bytes to read 147*9520a435SAndreas Gohr * 148*9520a435SAndreas Gohr * @return string 149*9520a435SAndreas Gohr */ 150*9520a435SAndreas Gohr public function readCurrentEntry($length = PHP_INT_MAX) 151*9520a435SAndreas Gohr { 152*9520a435SAndreas Gohr $length = (int) min($length, $this->contentUntil - $this->position); 153*9520a435SAndreas Gohr if ($length === 0) { 154*9520a435SAndreas Gohr return ''; 155*9520a435SAndreas Gohr } 156*9520a435SAndreas Gohr return $this->readbytes($length); 157605f8e8dSAndreas Gohr } 158605f8e8dSAndreas Gohr 159605f8e8dSAndreas Gohr /** 160605f8e8dSAndreas Gohr * Extract an existing TAR archive 161605f8e8dSAndreas Gohr * 162605f8e8dSAndreas Gohr * The $strip parameter allows you to strip a certain number of path components from the filenames 163605f8e8dSAndreas Gohr * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when 164605f8e8dSAndreas Gohr * an integer is passed as $strip. 165605f8e8dSAndreas Gohr * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix, 166605f8e8dSAndreas Gohr * the prefix will be stripped. It is recommended to give prefixes with a trailing slash. 167605f8e8dSAndreas Gohr * 168605f8e8dSAndreas Gohr * By default this will extract all files found in the archive. You can restrict the output using the $include 169605f8e8dSAndreas Gohr * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If 170605f8e8dSAndreas Gohr * $include is set only files that match this expression will be extracted. Files that match the $exclude 171605f8e8dSAndreas Gohr * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against 172605f8e8dSAndreas Gohr * stripped filenames as described above. 173605f8e8dSAndreas Gohr * 174605f8e8dSAndreas Gohr * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams. 175605f8e8dSAndreas Gohr * Reopen the file with open() again if you want to do additional operations 176605f8e8dSAndreas Gohr * 177605f8e8dSAndreas Gohr * @param string $outdir the target directory for extracting 178605f8e8dSAndreas Gohr * @param int|string $strip either the number of path components or a fixed prefix to strip 179605f8e8dSAndreas Gohr * @param string $exclude a regular expression of files to exclude 180605f8e8dSAndreas Gohr * @param string $include a regular expression of files to include 181605f8e8dSAndreas Gohr * @throws ArchiveIOException 182e43cd7e1SAndreas Gohr * @throws ArchiveCorruptedException 183605f8e8dSAndreas Gohr * @return FileInfo[] 184605f8e8dSAndreas Gohr */ 185605f8e8dSAndreas Gohr public function extract($outdir, $strip = '', $exclude = '', $include = '') 186605f8e8dSAndreas Gohr { 187605f8e8dSAndreas Gohr if ($this->closed || !$this->file) { 188605f8e8dSAndreas Gohr throw new ArchiveIOException('Can not read from a closed archive'); 189605f8e8dSAndreas Gohr } 190605f8e8dSAndreas Gohr 191605f8e8dSAndreas Gohr $outdir = rtrim($outdir, '/'); 192605f8e8dSAndreas Gohr @mkdir($outdir, 0777, true); 193605f8e8dSAndreas Gohr if (!is_dir($outdir)) { 194605f8e8dSAndreas Gohr throw new ArchiveIOException("Could not create directory '$outdir'"); 195605f8e8dSAndreas Gohr } 196605f8e8dSAndreas Gohr 197605f8e8dSAndreas Gohr $extracted = array(); 198605f8e8dSAndreas Gohr while ($dat = $this->readbytes(512)) { 199605f8e8dSAndreas Gohr // read the file header 200605f8e8dSAndreas Gohr $header = $this->parseHeader($dat); 201605f8e8dSAndreas Gohr if (!is_array($header)) { 202605f8e8dSAndreas Gohr continue; 203605f8e8dSAndreas Gohr } 204605f8e8dSAndreas Gohr $fileinfo = $this->header2fileinfo($header); 205605f8e8dSAndreas Gohr 206605f8e8dSAndreas Gohr // apply strip rules 207605f8e8dSAndreas Gohr $fileinfo->strip($strip); 208605f8e8dSAndreas Gohr 209605f8e8dSAndreas Gohr // skip unwanted files 210a3bfbb3cSAndreas Gohr if (!strlen($fileinfo->getPath()) || !$fileinfo->matchExpression($include, $exclude)) { 211605f8e8dSAndreas Gohr $this->skipbytes(ceil($header['size'] / 512) * 512); 212605f8e8dSAndreas Gohr continue; 213605f8e8dSAndreas Gohr } 214605f8e8dSAndreas Gohr 215605f8e8dSAndreas Gohr // create output directory 216605f8e8dSAndreas Gohr $output = $outdir.'/'.$fileinfo->getPath(); 217605f8e8dSAndreas Gohr $directory = ($fileinfo->getIsdir()) ? $output : dirname($output); 2182afbbbaeSAndreas Gohr if (!file_exists($directory)) { 2192afbbbaeSAndreas Gohr mkdir($directory, 0777, true); 2202afbbbaeSAndreas Gohr } 221605f8e8dSAndreas Gohr 222605f8e8dSAndreas Gohr // extract data 223605f8e8dSAndreas Gohr if (!$fileinfo->getIsdir()) { 224ddb94cf0SAndreas Gohr $fp = @fopen($output, "wb"); 225605f8e8dSAndreas Gohr if (!$fp) { 226605f8e8dSAndreas Gohr throw new ArchiveIOException('Could not open file for writing: '.$output); 227605f8e8dSAndreas Gohr } 228605f8e8dSAndreas Gohr 229605f8e8dSAndreas Gohr $size = floor($header['size'] / 512); 230605f8e8dSAndreas Gohr for ($i = 0; $i < $size; $i++) { 231605f8e8dSAndreas Gohr fwrite($fp, $this->readbytes(512), 512); 232605f8e8dSAndreas Gohr } 233605f8e8dSAndreas Gohr if (($header['size'] % 512) != 0) { 234605f8e8dSAndreas Gohr fwrite($fp, $this->readbytes(512), $header['size'] % 512); 235605f8e8dSAndreas Gohr } 236605f8e8dSAndreas Gohr 237605f8e8dSAndreas Gohr fclose($fp); 238e43cd7e1SAndreas Gohr @touch($output, $fileinfo->getMtime()); 239e43cd7e1SAndreas Gohr @chmod($output, $fileinfo->getMode()); 240605f8e8dSAndreas Gohr } else { 241605f8e8dSAndreas Gohr $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories 242605f8e8dSAndreas Gohr } 243605f8e8dSAndreas Gohr 244e43cd7e1SAndreas Gohr if(is_callable($this->callback)) { 245e43cd7e1SAndreas Gohr call_user_func($this->callback, $fileinfo); 246e43cd7e1SAndreas Gohr } 247605f8e8dSAndreas Gohr $extracted[] = $fileinfo; 248605f8e8dSAndreas Gohr } 249605f8e8dSAndreas Gohr 250605f8e8dSAndreas Gohr $this->close(); 251605f8e8dSAndreas Gohr return $extracted; 252605f8e8dSAndreas Gohr } 253605f8e8dSAndreas Gohr 254605f8e8dSAndreas Gohr /** 255605f8e8dSAndreas Gohr * Create a new TAR file 256605f8e8dSAndreas Gohr * 257605f8e8dSAndreas Gohr * If $file is empty, the tar file will be created in memory 258605f8e8dSAndreas Gohr * 259605f8e8dSAndreas Gohr * @param string $file 260605f8e8dSAndreas Gohr * @throws ArchiveIOException 261e43cd7e1SAndreas Gohr * @throws ArchiveIllegalCompressionException 262605f8e8dSAndreas Gohr */ 263605f8e8dSAndreas Gohr public function create($file = '') 264605f8e8dSAndreas Gohr { 265605f8e8dSAndreas Gohr $this->file = $file; 266605f8e8dSAndreas Gohr $this->memory = ''; 267605f8e8dSAndreas Gohr $this->fh = 0; 268605f8e8dSAndreas Gohr 269605f8e8dSAndreas Gohr if ($this->file) { 270605f8e8dSAndreas Gohr // determine compression 271605f8e8dSAndreas Gohr if ($this->comptype == Archive::COMPRESS_AUTO) { 272605f8e8dSAndreas Gohr $this->setCompression($this->complevel, $this->filetype($file)); 273605f8e8dSAndreas Gohr } 274605f8e8dSAndreas Gohr 275605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 276605f8e8dSAndreas Gohr $this->fh = @gzopen($this->file, 'wb'.$this->complevel); 277605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 278605f8e8dSAndreas Gohr $this->fh = @bzopen($this->file, 'w'); 279605f8e8dSAndreas Gohr } else { 280605f8e8dSAndreas Gohr $this->fh = @fopen($this->file, 'wb'); 281605f8e8dSAndreas Gohr } 282605f8e8dSAndreas Gohr 283605f8e8dSAndreas Gohr if (!$this->fh) { 284605f8e8dSAndreas Gohr throw new ArchiveIOException('Could not open file for writing: '.$this->file); 285605f8e8dSAndreas Gohr } 286605f8e8dSAndreas Gohr } 287605f8e8dSAndreas Gohr $this->writeaccess = true; 288605f8e8dSAndreas Gohr $this->closed = false; 289605f8e8dSAndreas Gohr } 290605f8e8dSAndreas Gohr 291605f8e8dSAndreas Gohr /** 292605f8e8dSAndreas Gohr * Add a file to the current TAR archive using an existing file in the filesystem 293605f8e8dSAndreas Gohr * 294605f8e8dSAndreas Gohr * @param string $file path to the original file 295605f8e8dSAndreas 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 29636113441SAndreas Gohr * @throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted 29736113441SAndreas Gohr * @throws ArchiveIOException there was trouble reading the given file, it was not added 298e43cd7e1SAndreas Gohr * @throws FileInfoException trouble reading file info, it was not added 299605f8e8dSAndreas Gohr */ 300605f8e8dSAndreas Gohr public function addFile($file, $fileinfo = '') 301605f8e8dSAndreas Gohr { 302605f8e8dSAndreas Gohr if (is_string($fileinfo)) { 303605f8e8dSAndreas Gohr $fileinfo = FileInfo::fromPath($file, $fileinfo); 304605f8e8dSAndreas Gohr } 305605f8e8dSAndreas Gohr 306605f8e8dSAndreas Gohr if ($this->closed) { 307605f8e8dSAndreas Gohr throw new ArchiveIOException('Archive has been closed, files can no longer be added'); 308605f8e8dSAndreas Gohr } 309605f8e8dSAndreas Gohr 3106cb05674SAndreas Gohr // create file header 3116cb05674SAndreas Gohr $this->writeFileHeader($fileinfo); 3126cb05674SAndreas Gohr 3136cb05674SAndreas Gohr // write data, but only if we have data to write. 3146cb05674SAndreas Gohr // note: on Windows fopen() on a directory will fail, so we prevent 3156cb05674SAndreas Gohr // errors on Windows by testing if we have data to write. 3166cb05674SAndreas Gohr if (!$fileinfo->getIsdir() && $fileinfo->getSize() > 0) { 3176cb05674SAndreas Gohr $read = 0; 318ddb94cf0SAndreas Gohr $fp = @fopen($file, 'rb'); 319605f8e8dSAndreas Gohr if (!$fp) { 320605f8e8dSAndreas Gohr throw new ArchiveIOException('Could not open file for reading: ' . $file); 321605f8e8dSAndreas Gohr } 322605f8e8dSAndreas Gohr while (!feof($fp)) { 323*9520a435SAndreas Gohr // for performance reasons read bigger chunks at once 324*9520a435SAndreas Gohr $data = fread($fp, self::READ_CHUNK_SIZE); 325605f8e8dSAndreas Gohr if ($data === false) { 326605f8e8dSAndreas Gohr break; 327605f8e8dSAndreas Gohr } 328605f8e8dSAndreas Gohr if ($data === '') { 329605f8e8dSAndreas Gohr break; 330605f8e8dSAndreas Gohr } 331*9520a435SAndreas Gohr $dataLen = strlen($data); 332*9520a435SAndreas Gohr $read += $dataLen; 333*9520a435SAndreas Gohr // how much of data read fully fills 512-byte blocks? 334*9520a435SAndreas Gohr $passLen = ($dataLen >> 9) << 9; 335*9520a435SAndreas Gohr if ($passLen === $dataLen) { 336*9520a435SAndreas Gohr // all - just write the data 337*9520a435SAndreas Gohr $this->writebytes($data); 338*9520a435SAndreas Gohr } else { 339*9520a435SAndreas Gohr // directly write what fills 512-byte blocks fully 340*9520a435SAndreas Gohr $this->writebytes(substr($data, 0, $passLen)); 341*9520a435SAndreas Gohr // pad the reminder to 512 bytes 342*9520a435SAndreas Gohr $this->writebytes(pack("a512", substr($data, $passLen))); 343*9520a435SAndreas Gohr } 344605f8e8dSAndreas Gohr } 345605f8e8dSAndreas Gohr fclose($fp); 34636113441SAndreas Gohr 34736113441SAndreas Gohr if ($read != $fileinfo->getSize()) { 34836113441SAndreas Gohr $this->close(); 34936113441SAndreas Gohr throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize()); 35036113441SAndreas Gohr } 3516cb05674SAndreas Gohr } 352e43cd7e1SAndreas Gohr 353e43cd7e1SAndreas Gohr if(is_callable($this->callback)) { 354e43cd7e1SAndreas Gohr call_user_func($this->callback, $fileinfo); 355e43cd7e1SAndreas Gohr } 356605f8e8dSAndreas Gohr } 357605f8e8dSAndreas Gohr 358605f8e8dSAndreas Gohr /** 359605f8e8dSAndreas Gohr * Add a file to the current TAR archive using the given $data as content 360605f8e8dSAndreas Gohr * 361605f8e8dSAndreas Gohr * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data 362605f8e8dSAndreas Gohr * @param string $data binary content of the file to add 363605f8e8dSAndreas Gohr * @throws ArchiveIOException 364605f8e8dSAndreas Gohr */ 365605f8e8dSAndreas Gohr public function addData($fileinfo, $data) 366605f8e8dSAndreas Gohr { 367605f8e8dSAndreas Gohr if (is_string($fileinfo)) { 368605f8e8dSAndreas Gohr $fileinfo = new FileInfo($fileinfo); 369605f8e8dSAndreas Gohr } 370605f8e8dSAndreas Gohr 371605f8e8dSAndreas Gohr if ($this->closed) { 372605f8e8dSAndreas Gohr throw new ArchiveIOException('Archive has been closed, files can no longer be added'); 373605f8e8dSAndreas Gohr } 374605f8e8dSAndreas Gohr 375605f8e8dSAndreas Gohr $len = strlen($data); 376605f8e8dSAndreas Gohr $fileinfo->setSize($len); 377605f8e8dSAndreas Gohr $this->writeFileHeader($fileinfo); 378605f8e8dSAndreas Gohr 379*9520a435SAndreas Gohr // write directly everything but the last block which needs padding 380*9520a435SAndreas Gohr $passLen = ($len >> 9) << 9; 381*9520a435SAndreas Gohr $this->writebytes(substr($data, 0, $passLen)); 382*9520a435SAndreas Gohr if ($passLen < $len) { 383*9520a435SAndreas Gohr $this->writebytes(pack("a512", substr($data, $passLen, 512))); 384605f8e8dSAndreas Gohr } 385e43cd7e1SAndreas Gohr 386e43cd7e1SAndreas Gohr if (is_callable($this->callback)) { 387e43cd7e1SAndreas Gohr call_user_func($this->callback, $fileinfo); 388e43cd7e1SAndreas Gohr } 389605f8e8dSAndreas Gohr } 390605f8e8dSAndreas Gohr 391605f8e8dSAndreas Gohr /** 392605f8e8dSAndreas Gohr * Add the closing footer to the archive if in write mode, close all file handles 393605f8e8dSAndreas Gohr * 394605f8e8dSAndreas Gohr * After a call to this function no more data can be added to the archive, for 395605f8e8dSAndreas Gohr * read access no reading is allowed anymore 396605f8e8dSAndreas Gohr * 397605f8e8dSAndreas Gohr * "Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which 398605f8e8dSAndreas Gohr * consists of two 512 blocks of zero bytes" 399605f8e8dSAndreas Gohr * 400605f8e8dSAndreas Gohr * @link http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134 401e43cd7e1SAndreas Gohr * @throws ArchiveIOException 402605f8e8dSAndreas Gohr */ 403605f8e8dSAndreas Gohr public function close() 404605f8e8dSAndreas Gohr { 405605f8e8dSAndreas Gohr if ($this->closed) { 406605f8e8dSAndreas Gohr return; 407605f8e8dSAndreas Gohr } // we did this already 408605f8e8dSAndreas Gohr 409605f8e8dSAndreas Gohr // write footer 410605f8e8dSAndreas Gohr if ($this->writeaccess) { 411605f8e8dSAndreas Gohr $this->writebytes(pack("a512", "")); 412605f8e8dSAndreas Gohr $this->writebytes(pack("a512", "")); 413605f8e8dSAndreas Gohr } 414605f8e8dSAndreas Gohr 415605f8e8dSAndreas Gohr // close file handles 416605f8e8dSAndreas Gohr if ($this->file) { 417605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 418605f8e8dSAndreas Gohr gzclose($this->fh); 419605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 420605f8e8dSAndreas Gohr bzclose($this->fh); 421605f8e8dSAndreas Gohr } else { 422605f8e8dSAndreas Gohr fclose($this->fh); 423605f8e8dSAndreas Gohr } 424605f8e8dSAndreas Gohr 425605f8e8dSAndreas Gohr $this->file = ''; 426605f8e8dSAndreas Gohr $this->fh = 0; 427605f8e8dSAndreas Gohr } 428605f8e8dSAndreas Gohr 429605f8e8dSAndreas Gohr $this->writeaccess = false; 430605f8e8dSAndreas Gohr $this->closed = true; 431605f8e8dSAndreas Gohr } 432605f8e8dSAndreas Gohr 433605f8e8dSAndreas Gohr /** 434605f8e8dSAndreas Gohr * Returns the created in-memory archive data 435605f8e8dSAndreas Gohr * 436605f8e8dSAndreas Gohr * This implicitly calls close() on the Archive 437e43cd7e1SAndreas Gohr * @throws ArchiveIOException 438605f8e8dSAndreas Gohr */ 439605f8e8dSAndreas Gohr public function getArchive() 440605f8e8dSAndreas Gohr { 441605f8e8dSAndreas Gohr $this->close(); 442605f8e8dSAndreas Gohr 443605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_AUTO) { 444605f8e8dSAndreas Gohr $this->comptype = Archive::COMPRESS_NONE; 445605f8e8dSAndreas Gohr } 446605f8e8dSAndreas Gohr 447605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 448dd7064d9SAndreas Gohr return gzencode($this->memory, $this->complevel); 449605f8e8dSAndreas Gohr } 450605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_BZIP) { 451605f8e8dSAndreas Gohr return bzcompress($this->memory); 452605f8e8dSAndreas Gohr } 453605f8e8dSAndreas Gohr return $this->memory; 454605f8e8dSAndreas Gohr } 455605f8e8dSAndreas Gohr 456605f8e8dSAndreas Gohr /** 457605f8e8dSAndreas Gohr * Save the created in-memory archive data 458605f8e8dSAndreas Gohr * 459605f8e8dSAndreas Gohr * Note: It more memory effective to specify the filename in the create() function and 460605f8e8dSAndreas Gohr * let the library work on the new file directly. 461605f8e8dSAndreas Gohr * 462605f8e8dSAndreas Gohr * @param string $file 463605f8e8dSAndreas Gohr * @throws ArchiveIOException 464e43cd7e1SAndreas Gohr * @throws ArchiveIllegalCompressionException 465605f8e8dSAndreas Gohr */ 466605f8e8dSAndreas Gohr public function save($file) 467605f8e8dSAndreas Gohr { 468605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_AUTO) { 469530d6729SAndreas Gohr $this->setCompression($this->complevel, $this->filetype($file)); 470605f8e8dSAndreas Gohr } 471605f8e8dSAndreas Gohr 472ddb94cf0SAndreas Gohr if (!@file_put_contents($file, $this->getArchive())) { 473605f8e8dSAndreas Gohr throw new ArchiveIOException('Could not write to file: '.$file); 474605f8e8dSAndreas Gohr } 475605f8e8dSAndreas Gohr } 476605f8e8dSAndreas Gohr 477605f8e8dSAndreas Gohr /** 478605f8e8dSAndreas Gohr * Read from the open file pointer 479605f8e8dSAndreas Gohr * 480605f8e8dSAndreas Gohr * @param int $length bytes to read 481605f8e8dSAndreas Gohr * @return string 482605f8e8dSAndreas Gohr */ 483605f8e8dSAndreas Gohr protected function readbytes($length) 484605f8e8dSAndreas Gohr { 485605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 486*9520a435SAndreas Gohr $ret = @gzread($this->fh, $length); 487605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 488*9520a435SAndreas Gohr $ret = @bzread($this->fh, $length); 489605f8e8dSAndreas Gohr } else { 490*9520a435SAndreas Gohr $ret = @fread($this->fh, $length); 491605f8e8dSAndreas Gohr } 492*9520a435SAndreas Gohr $this->position += strlen($ret); 493*9520a435SAndreas Gohr return $ret; 494605f8e8dSAndreas Gohr } 495605f8e8dSAndreas Gohr 496605f8e8dSAndreas Gohr /** 497605f8e8dSAndreas Gohr * Write to the open filepointer or memory 498605f8e8dSAndreas Gohr * 499605f8e8dSAndreas Gohr * @param string $data 500605f8e8dSAndreas Gohr * @throws ArchiveIOException 501605f8e8dSAndreas Gohr * @return int number of bytes written 502605f8e8dSAndreas Gohr */ 503605f8e8dSAndreas Gohr protected function writebytes($data) 504605f8e8dSAndreas Gohr { 505605f8e8dSAndreas Gohr if (!$this->file) { 506605f8e8dSAndreas Gohr $this->memory .= $data; 507605f8e8dSAndreas Gohr $written = strlen($data); 508605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_GZIP) { 509605f8e8dSAndreas Gohr $written = @gzwrite($this->fh, $data); 510605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 511605f8e8dSAndreas Gohr $written = @bzwrite($this->fh, $data); 512605f8e8dSAndreas Gohr } else { 513605f8e8dSAndreas Gohr $written = @fwrite($this->fh, $data); 514605f8e8dSAndreas Gohr } 515605f8e8dSAndreas Gohr if ($written === false) { 516605f8e8dSAndreas Gohr throw new ArchiveIOException('Failed to write to archive stream'); 517605f8e8dSAndreas Gohr } 518605f8e8dSAndreas Gohr return $written; 519605f8e8dSAndreas Gohr } 520605f8e8dSAndreas Gohr 521605f8e8dSAndreas Gohr /** 522605f8e8dSAndreas Gohr * Skip forward in the open file pointer 523605f8e8dSAndreas Gohr * 524605f8e8dSAndreas Gohr * This is basically a wrapper around seek() (and a workaround for bzip2) 525605f8e8dSAndreas Gohr * 526605f8e8dSAndreas Gohr * @param int $bytes seek to this position 527605f8e8dSAndreas Gohr */ 528ddb94cf0SAndreas Gohr protected function skipbytes($bytes) 529605f8e8dSAndreas Gohr { 530605f8e8dSAndreas Gohr if ($this->comptype === Archive::COMPRESS_GZIP) { 531605f8e8dSAndreas Gohr @gzseek($this->fh, $bytes, SEEK_CUR); 532605f8e8dSAndreas Gohr } elseif ($this->comptype === Archive::COMPRESS_BZIP) { 533605f8e8dSAndreas Gohr // there is no seek in bzip2, we simply read on 534530d6729SAndreas Gohr // bzread allows to read a max of 8kb at once 535530d6729SAndreas Gohr while($bytes) { 536530d6729SAndreas Gohr $toread = min(8192, $bytes); 537530d6729SAndreas Gohr @bzread($this->fh, $toread); 538530d6729SAndreas Gohr $bytes -= $toread; 539530d6729SAndreas Gohr } 540605f8e8dSAndreas Gohr } else { 541605f8e8dSAndreas Gohr @fseek($this->fh, $bytes, SEEK_CUR); 542605f8e8dSAndreas Gohr } 543*9520a435SAndreas Gohr $this->position += $bytes; 544605f8e8dSAndreas Gohr } 545605f8e8dSAndreas Gohr 546605f8e8dSAndreas Gohr /** 547e43cd7e1SAndreas Gohr * Write the given file meta data as header 548605f8e8dSAndreas Gohr * 549605f8e8dSAndreas Gohr * @param FileInfo $fileinfo 550e43cd7e1SAndreas Gohr * @throws ArchiveIOException 551605f8e8dSAndreas Gohr */ 552605f8e8dSAndreas Gohr protected function writeFileHeader(FileInfo $fileinfo) 553605f8e8dSAndreas Gohr { 554605f8e8dSAndreas Gohr $this->writeRawFileHeader( 555605f8e8dSAndreas Gohr $fileinfo->getPath(), 556605f8e8dSAndreas Gohr $fileinfo->getUid(), 557605f8e8dSAndreas Gohr $fileinfo->getGid(), 558605f8e8dSAndreas Gohr $fileinfo->getMode(), 559605f8e8dSAndreas Gohr $fileinfo->getSize(), 560605f8e8dSAndreas Gohr $fileinfo->getMtime(), 561605f8e8dSAndreas Gohr $fileinfo->getIsdir() ? '5' : '0' 562605f8e8dSAndreas Gohr ); 563605f8e8dSAndreas Gohr } 564605f8e8dSAndreas Gohr 565605f8e8dSAndreas Gohr /** 566605f8e8dSAndreas Gohr * Write a file header to the stream 567605f8e8dSAndreas Gohr * 568605f8e8dSAndreas Gohr * @param string $name 569605f8e8dSAndreas Gohr * @param int $uid 570605f8e8dSAndreas Gohr * @param int $gid 571605f8e8dSAndreas Gohr * @param int $perm 572605f8e8dSAndreas Gohr * @param int $size 573605f8e8dSAndreas Gohr * @param int $mtime 574605f8e8dSAndreas Gohr * @param string $typeflag Set to '5' for directories 575e43cd7e1SAndreas Gohr * @throws ArchiveIOException 576605f8e8dSAndreas Gohr */ 577605f8e8dSAndreas Gohr protected function writeRawFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '') 578605f8e8dSAndreas Gohr { 579605f8e8dSAndreas Gohr // handle filename length restrictions 580605f8e8dSAndreas Gohr $prefix = ''; 581605f8e8dSAndreas Gohr $namelen = strlen($name); 582605f8e8dSAndreas Gohr if ($namelen > 100) { 583605f8e8dSAndreas Gohr $file = basename($name); 584605f8e8dSAndreas Gohr $dir = dirname($name); 585605f8e8dSAndreas Gohr if (strlen($file) > 100 || strlen($dir) > 155) { 586605f8e8dSAndreas Gohr // we're still too large, let's use GNU longlink 587605f8e8dSAndreas Gohr $this->writeRawFileHeader('././@LongLink', 0, 0, 0, $namelen, 0, 'L'); 588605f8e8dSAndreas Gohr for ($s = 0; $s < $namelen; $s += 512) { 589605f8e8dSAndreas Gohr $this->writebytes(pack("a512", substr($name, $s, 512))); 590605f8e8dSAndreas Gohr } 591605f8e8dSAndreas Gohr $name = substr($name, 0, 100); // cut off name 592605f8e8dSAndreas Gohr } else { 593605f8e8dSAndreas Gohr // we're fine when splitting, use POSIX ustar 594605f8e8dSAndreas Gohr $prefix = $dir; 595605f8e8dSAndreas Gohr $name = $file; 596605f8e8dSAndreas Gohr } 597605f8e8dSAndreas Gohr } 598605f8e8dSAndreas Gohr 599605f8e8dSAndreas Gohr // values are needed in octal 600605f8e8dSAndreas Gohr $uid = sprintf("%6s ", decoct($uid)); 601605f8e8dSAndreas Gohr $gid = sprintf("%6s ", decoct($gid)); 602605f8e8dSAndreas Gohr $perm = sprintf("%6s ", decoct($perm)); 603*9520a435SAndreas Gohr $size = self::numberEncode($size, 12); 604*9520a435SAndreas Gohr $mtime = self::numberEncode($size, 12); 605605f8e8dSAndreas Gohr 606605f8e8dSAndreas Gohr $data_first = pack("a100a8a8a8a12A12", $name, $perm, $uid, $gid, $size, $mtime); 607605f8e8dSAndreas Gohr $data_last = pack("a1a100a6a2a32a32a8a8a155a12", $typeflag, '', 'ustar', '', '', '', '', '', $prefix, ""); 608605f8e8dSAndreas Gohr 609605f8e8dSAndreas Gohr for ($i = 0, $chks = 0; $i < 148; $i++) { 610605f8e8dSAndreas Gohr $chks += ord($data_first[$i]); 611605f8e8dSAndreas Gohr } 612605f8e8dSAndreas Gohr 613605f8e8dSAndreas Gohr for ($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++) { 614605f8e8dSAndreas Gohr $chks += ord($data_last[$j]); 615605f8e8dSAndreas Gohr } 616605f8e8dSAndreas Gohr 617605f8e8dSAndreas Gohr $this->writebytes($data_first); 618605f8e8dSAndreas Gohr 619605f8e8dSAndreas Gohr $chks = pack("a8", sprintf("%6s ", decoct($chks))); 620605f8e8dSAndreas Gohr $this->writebytes($chks.$data_last); 621605f8e8dSAndreas Gohr } 622605f8e8dSAndreas Gohr 623605f8e8dSAndreas Gohr /** 624605f8e8dSAndreas Gohr * Decode the given tar file header 625605f8e8dSAndreas Gohr * 626530d6729SAndreas Gohr * @param string $block a 512 byte block containing the header data 627530d6729SAndreas Gohr * @return array|false returns false when this was a null block 628530d6729SAndreas Gohr * @throws ArchiveCorruptedException 629605f8e8dSAndreas Gohr */ 630605f8e8dSAndreas Gohr protected function parseHeader($block) 631605f8e8dSAndreas Gohr { 632605f8e8dSAndreas Gohr if (!$block || strlen($block) != 512) { 633530d6729SAndreas Gohr throw new ArchiveCorruptedException('Unexpected length of header'); 634605f8e8dSAndreas Gohr } 635605f8e8dSAndreas Gohr 636530d6729SAndreas Gohr // null byte blocks are ignored 637530d6729SAndreas Gohr if(trim($block) === '') return false; 638530d6729SAndreas Gohr 639605f8e8dSAndreas Gohr for ($i = 0, $chks = 0; $i < 148; $i++) { 640605f8e8dSAndreas Gohr $chks += ord($block[$i]); 641605f8e8dSAndreas Gohr } 642605f8e8dSAndreas Gohr 643605f8e8dSAndreas Gohr for ($i = 156, $chks += 256; $i < 512; $i++) { 644605f8e8dSAndreas Gohr $chks += ord($block[$i]); 645605f8e8dSAndreas Gohr } 646605f8e8dSAndreas Gohr 647605f8e8dSAndreas Gohr $header = @unpack( 648605f8e8dSAndreas Gohr "a100filename/a8perm/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", 649605f8e8dSAndreas Gohr $block 650605f8e8dSAndreas Gohr ); 651605f8e8dSAndreas Gohr if (!$header) { 652530d6729SAndreas Gohr throw new ArchiveCorruptedException('Failed to parse header'); 653605f8e8dSAndreas Gohr } 654605f8e8dSAndreas Gohr 655605f8e8dSAndreas Gohr $return['checksum'] = OctDec(trim($header['checksum'])); 656605f8e8dSAndreas Gohr if ($return['checksum'] != $chks) { 657a3bfbb3cSAndreas Gohr throw new ArchiveCorruptedException('Header does not match its checksum'); 658605f8e8dSAndreas Gohr } 659605f8e8dSAndreas Gohr 660605f8e8dSAndreas Gohr $return['filename'] = trim($header['filename']); 661605f8e8dSAndreas Gohr $return['perm'] = OctDec(trim($header['perm'])); 662605f8e8dSAndreas Gohr $return['uid'] = OctDec(trim($header['uid'])); 663605f8e8dSAndreas Gohr $return['gid'] = OctDec(trim($header['gid'])); 664*9520a435SAndreas Gohr $return['size'] = self::numberDecode($header['size']); 665*9520a435SAndreas Gohr $return['mtime'] = self::numberDecode($header['mtime']); 666605f8e8dSAndreas Gohr $return['typeflag'] = $header['typeflag']; 667605f8e8dSAndreas Gohr $return['link'] = trim($header['link']); 668605f8e8dSAndreas Gohr $return['uname'] = trim($header['uname']); 669605f8e8dSAndreas Gohr $return['gname'] = trim($header['gname']); 670605f8e8dSAndreas Gohr 671605f8e8dSAndreas Gohr // Handle ustar Posix compliant path prefixes 672605f8e8dSAndreas Gohr if (trim($header['prefix'])) { 673605f8e8dSAndreas Gohr $return['filename'] = trim($header['prefix']).'/'.$return['filename']; 674605f8e8dSAndreas Gohr } 675605f8e8dSAndreas Gohr 676605f8e8dSAndreas Gohr // Handle Long-Link entries from GNU Tar 677605f8e8dSAndreas Gohr if ($return['typeflag'] == 'L') { 678605f8e8dSAndreas Gohr // following data block(s) is the filename 67936113441SAndreas Gohr $filename = trim($this->readbytes(ceil($return['size'] / 512) * 512)); 680605f8e8dSAndreas Gohr // next block is the real header 681605f8e8dSAndreas Gohr $block = $this->readbytes(512); 682605f8e8dSAndreas Gohr $return = $this->parseHeader($block); 683605f8e8dSAndreas Gohr // overwrite the filename 684605f8e8dSAndreas Gohr $return['filename'] = $filename; 685605f8e8dSAndreas Gohr } 686605f8e8dSAndreas Gohr 687605f8e8dSAndreas Gohr return $return; 688605f8e8dSAndreas Gohr } 689605f8e8dSAndreas Gohr 690605f8e8dSAndreas Gohr /** 691605f8e8dSAndreas Gohr * Creates a FileInfo object from the given parsed header 692605f8e8dSAndreas Gohr * 693605f8e8dSAndreas Gohr * @param $header 694605f8e8dSAndreas Gohr * @return FileInfo 695605f8e8dSAndreas Gohr */ 696605f8e8dSAndreas Gohr protected function header2fileinfo($header) 697605f8e8dSAndreas Gohr { 698605f8e8dSAndreas Gohr $fileinfo = new FileInfo(); 699605f8e8dSAndreas Gohr $fileinfo->setPath($header['filename']); 700605f8e8dSAndreas Gohr $fileinfo->setMode($header['perm']); 701605f8e8dSAndreas Gohr $fileinfo->setUid($header['uid']); 702605f8e8dSAndreas Gohr $fileinfo->setGid($header['gid']); 703605f8e8dSAndreas Gohr $fileinfo->setSize($header['size']); 704605f8e8dSAndreas Gohr $fileinfo->setMtime($header['mtime']); 705605f8e8dSAndreas Gohr $fileinfo->setOwner($header['uname']); 706605f8e8dSAndreas Gohr $fileinfo->setGroup($header['gname']); 707605f8e8dSAndreas Gohr $fileinfo->setIsdir((bool) $header['typeflag']); 708605f8e8dSAndreas Gohr 709605f8e8dSAndreas Gohr return $fileinfo; 710605f8e8dSAndreas Gohr } 711605f8e8dSAndreas Gohr 712605f8e8dSAndreas Gohr /** 713605f8e8dSAndreas Gohr * Checks if the given compression type is available and throws an exception if not 714605f8e8dSAndreas Gohr * 715605f8e8dSAndreas Gohr * @param $comptype 716605f8e8dSAndreas Gohr * @throws ArchiveIllegalCompressionException 717605f8e8dSAndreas Gohr */ 718605f8e8dSAndreas Gohr protected function compressioncheck($comptype) 719605f8e8dSAndreas Gohr { 720605f8e8dSAndreas Gohr if ($comptype === Archive::COMPRESS_GZIP && !function_exists('gzopen')) { 721605f8e8dSAndreas Gohr throw new ArchiveIllegalCompressionException('No gzip support available'); 722605f8e8dSAndreas Gohr } 723605f8e8dSAndreas Gohr 724605f8e8dSAndreas Gohr if ($comptype === Archive::COMPRESS_BZIP && !function_exists('bzopen')) { 725605f8e8dSAndreas Gohr throw new ArchiveIllegalCompressionException('No bzip2 support available'); 726605f8e8dSAndreas Gohr } 727605f8e8dSAndreas Gohr } 728605f8e8dSAndreas Gohr 729605f8e8dSAndreas Gohr /** 730530d6729SAndreas Gohr * Guesses the wanted compression from the given file 731530d6729SAndreas Gohr * 732530d6729SAndreas Gohr * Uses magic bytes for existing files, the file extension otherwise 733605f8e8dSAndreas Gohr * 734605f8e8dSAndreas Gohr * You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere 735605f8e8dSAndreas Gohr * 736605f8e8dSAndreas Gohr * @param string $file 737605f8e8dSAndreas Gohr * @return int 738605f8e8dSAndreas Gohr */ 739605f8e8dSAndreas Gohr public function filetype($file) 740605f8e8dSAndreas Gohr { 741530d6729SAndreas Gohr // for existing files, try to read the magic bytes 742530d6729SAndreas Gohr if(file_exists($file) && is_readable($file) && filesize($file) > 5) { 743ddb94cf0SAndreas Gohr $fh = @fopen($file, 'rb'); 744530d6729SAndreas Gohr if(!$fh) return false; 745530d6729SAndreas Gohr $magic = fread($fh, 5); 746530d6729SAndreas Gohr fclose($fh); 747530d6729SAndreas Gohr 748530d6729SAndreas Gohr if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP; 749530d6729SAndreas Gohr if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP; 750530d6729SAndreas Gohr } 751530d6729SAndreas Gohr 752530d6729SAndreas Gohr // otherwise rely on file name 753605f8e8dSAndreas Gohr $file = strtolower($file); 754605f8e8dSAndreas Gohr if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') { 755530d6729SAndreas Gohr return Archive::COMPRESS_GZIP; 756605f8e8dSAndreas Gohr } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') { 757530d6729SAndreas Gohr return Archive::COMPRESS_BZIP; 758605f8e8dSAndreas Gohr } 759530d6729SAndreas Gohr 760530d6729SAndreas Gohr return Archive::COMPRESS_NONE; 761605f8e8dSAndreas Gohr } 762e43cd7e1SAndreas Gohr 763*9520a435SAndreas Gohr /** 764*9520a435SAndreas Gohr * Decodes numeric values according to the 765*9520a435SAndreas Gohr * https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions 766*9520a435SAndreas Gohr * (basically with support for big numbers) 767*9520a435SAndreas Gohr * 768*9520a435SAndreas Gohr * @param string $field 769*9520a435SAndreas Gohr * $return int 770*9520a435SAndreas Gohr */ 771*9520a435SAndreas Gohr static public function numberDecode($field) 772*9520a435SAndreas Gohr { 773*9520a435SAndreas Gohr $firstByte = ord(substr($field, 0, 1)); 774*9520a435SAndreas Gohr if ($firstByte === 255) { 775*9520a435SAndreas Gohr $value = -1 << (8 * strlen($field)); 776*9520a435SAndreas Gohr $shift = 0; 777*9520a435SAndreas Gohr for ($i = strlen($field) - 1; $i >= 0; $i--) { 778*9520a435SAndreas Gohr $value += ord(substr($field, $i, 1)) << $shift; 779*9520a435SAndreas Gohr $shift += 8; 780605f8e8dSAndreas Gohr } 781*9520a435SAndreas Gohr } elseif ($firstByte === 128) { 782*9520a435SAndreas Gohr $value = 0; 783*9520a435SAndreas Gohr $shift = 0; 784*9520a435SAndreas Gohr for ($i = strlen($field) - 1; $i > 0; $i--) { 785*9520a435SAndreas Gohr $value += ord(substr($field, $i, 1)) << $shift; 786*9520a435SAndreas Gohr $shift += 8; 787*9520a435SAndreas Gohr } 788*9520a435SAndreas Gohr } else { 789*9520a435SAndreas Gohr $value = octdec(trim($field)); 790*9520a435SAndreas Gohr } 791*9520a435SAndreas Gohr return $value; 792*9520a435SAndreas Gohr } 793*9520a435SAndreas Gohr 794*9520a435SAndreas Gohr /** 795*9520a435SAndreas Gohr * Encodes numeric values according to the 796*9520a435SAndreas Gohr * https://www.gnu.org/software/tar/manual/html_node/Extensions.html#Extensions 797*9520a435SAndreas Gohr * (basically with support for big numbers) 798*9520a435SAndreas Gohr * 799*9520a435SAndreas Gohr * @param int $value 800*9520a435SAndreas Gohr * @param int $length field length 801*9520a435SAndreas Gohr * @return string 802*9520a435SAndreas Gohr */ 803*9520a435SAndreas Gohr static public function numberEncode($value, $length) 804*9520a435SAndreas Gohr { 805*9520a435SAndreas Gohr // old implementations leave last byte empty 806*9520a435SAndreas Gohr // octal encoding encodes three bits per byte 807*9520a435SAndreas Gohr $maxValue = 1 << (($length - 1) * 3); 808*9520a435SAndreas Gohr if ($value < 0) { 809*9520a435SAndreas Gohr // PHP already stores integers as 2's complement 810*9520a435SAndreas Gohr $value = pack(PHP_INT_SIZE === 8 ? 'J' : 'N', (int) $value); 811*9520a435SAndreas Gohr $encoded = str_repeat(chr(255), max(1, $length - PHP_INT_SIZE)); 812*9520a435SAndreas Gohr $encoded .= substr($value, max(0, PHP_INT_SIZE - $length + 1)); 813*9520a435SAndreas Gohr } elseif ($value >= $maxValue) { 814*9520a435SAndreas Gohr $value = pack(PHP_INT_SIZE === 8 ? 'J' : 'N', (int) $value); 815*9520a435SAndreas Gohr $encoded = chr(128) . str_repeat(chr(0), max(0, $length - PHP_INT_SIZE - 1)); 816*9520a435SAndreas Gohr $encoded .= substr($value, max(0, PHP_INT_SIZE - $length + 1)); 817*9520a435SAndreas Gohr } else { 818*9520a435SAndreas Gohr $encoded = sprintf("%" . ($length - 1) . "s ", decoct($value)); 819*9520a435SAndreas Gohr } 820*9520a435SAndreas Gohr return $encoded; 821*9520a435SAndreas Gohr } 822*9520a435SAndreas Gohr} 823*9520a435SAndreas Gohr 824