summaryrefslogtreecommitdiff
path: root/inc/TarLib.class.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2012-11-04 20:19:52 +0100
committerAndreas Gohr <andi@splitbrain.org>2012-11-04 20:19:52 +0100
commit383dc9895eac5f1b8ee2a938bfff4a1482228817 (patch)
treea6af696a4ecd6259628f2885e23eadb637603b30 /inc/TarLib.class.php
parentecd445c000e4e54bf7228890848222312cffd3e3 (diff)
parent2005b6b650f2523cb58a005961a55a6f099c70c3 (diff)
downloadrpg-383dc9895eac5f1b8ee2a938bfff4a1482228817.tar.gz
rpg-383dc9895eac5f1b8ee2a938bfff4a1482228817.tar.bz2
Merge branch 'master' into future
* master: (45 commits) TarLib code cleanup TarLib: fixed appending in non-dynamic mode fixed third method of adding files in TarLib fix lone zero block in TarLib created archives fix use of constructor in TarLib Slovak language update Korean language update Latvian language update removed redundant variables in tpl_include_page() (because of 3ff8773b) added cut off points for mobile devices as parameters to style.ini Corrected typo: ruke -> rule Persian language update Spanish language update russian language update Kazach language update correctly check hash parameter in media dispatcher FS#2648 avoid broken browser_uid on IE Removed acronyms for "Perl" and "PERL" as Perl is not an acronym. See http://learn.perl.org/faq/perlfaq1.html#Whats-the-difference-between-perl-and-Perl- Made striplangs.php executable release preparations ...
Diffstat (limited to 'inc/TarLib.class.php')
-rw-r--r--inc/TarLib.class.php420
1 files changed, 273 insertions, 147 deletions
diff --git a/inc/TarLib.class.php b/inc/TarLib.class.php
index 126604cd1..e04c47cb8 100644
--- a/inc/TarLib.class.php
+++ b/inc/TarLib.class.php
@@ -13,7 +13,6 @@
* @author Christopher Smith <chris@jalakai.co.uk>
*/
-
/**
* Those constants represent the compression method to use.
* COMPRESS_GZIP is used for the GZIP compression; COMPRESS_BZIP for
@@ -68,15 +67,15 @@ class TarLib {
var $_result;
var $_initerror;
- const COMPRESS_GZIP = 1;
- const COMPRESS_BZIP = 2;
- const COMPRESS_AUTO = 3;
- const COMPRESS_NONE = 0;
- const TARLIB_VERSION = '1.2';
- const FULL_ARCHIVE = -1;
- const ARCHIVE_DYNAMIC = 0;
+ const COMPRESS_GZIP = 1;
+ const COMPRESS_BZIP = 2;
+ const COMPRESS_AUTO = 3;
+ const COMPRESS_NONE = 0;
+ const TARLIB_VERSION = '1.2';
+ const FULL_ARCHIVE = -1;
+ const ARCHIVE_DYNAMIC = 0;
const ARCHIVE_RENAMECOMP = 5;
- const COMPRESS_DETECT = -1;
+ const COMPRESS_DETECT = -1;
/**
* constructor, initialize the class
@@ -108,21 +107,21 @@ class TarLib {
* represent the GZIP or BZIP compression level. 1 produce fast compression,
* and 9 produce smaller files. See the RFC 1952 for more infos.
*/
- function __construct($p_filen = TarLib::ARCHIVE_DYNAMIC , $p_comptype = TarLib::COMPRESS_AUTO, $p_complevel = 9) {
+ function __construct($p_filen = TarLib::ARCHIVE_DYNAMIC, $p_comptype = TarLib::COMPRESS_AUTO, $p_complevel = 9) {
$this->_initerror = 0;
- $this->_nomf = $p_filen;
- $flag=0;
- if($p_comptype && $p_comptype % 5 == 0){
+ $this->_nomf = $p_filen;
+ $flag = 0;
+ if($p_comptype && $p_comptype % 5 == 0) {
$p_comptype /= TarLib::ARCHIVE_RENAMECOMP;
- $flag=1;
+ $flag = 1;
}
if($p_complevel > 0 && $p_complevel <= 9) $this->_compzlevel = $p_complevel;
- else $p_complevel = 9;
+ else $this->_compzlevel = 9;
if($p_comptype == TarLib::COMPRESS_DETECT) {
- if(strtolower(substr($p_filen,-3)) == '.gz') $p_comptype = TarLib::COMPRESS_GZIP;
- elseif(strtolower(substr($p_filen,-4)) == '.bz2') $p_comptype = TarLib::COMPRESS_BZIP;
+ if(strtolower(substr($p_filen, -3)) == '.gz') $p_comptype = TarLib::COMPRESS_GZIP;
+ elseif(strtolower(substr($p_filen, -4)) == '.bz2') $p_comptype = TarLib::COMPRESS_BZIP;
else $p_comptype = TarLib::COMPRESS_NONE;
}
@@ -152,7 +151,7 @@ class TarLib {
if($this->_initerror < 0) $this->_comptype = TarLib::COMPRESS_NONE;
- if($flag) $this->_nomf.= '.'.$this->getCompression(1);
+ if($flag) $this->_nomf .= '.'.$this->getCompression(1);
$this->_result = true;
}
@@ -162,9 +161,9 @@ class TarLib {
* This function does exactly the same as TarLib (constructor), except it
* returns a status code.
*/
- function setArchive($p_name='', $p_comp = TarLib::COMPRESS_AUTO, $p_level=9) {
+ function setArchive($p_name = '', $p_comp = TarLib::COMPRESS_AUTO, $p_level = 9) {
$this->_CompTar();
- $this->TarLib($p_name, $p_comp, $p_level);
+ $this->__construct($p_name, $p_comp, $p_level);
return $this->_result;
}
@@ -186,7 +185,7 @@ class TarLib {
* MaxgTar Constants
*/
function getCompression($ext = false) {
- $exts = Array('tar','tar.gz','tar.bz2');
+ $exts = Array('tar', 'tar.gz', 'tar.bz2');
if($ext) return $exts[$this->_comptype];
return $this->_comptype;
}
@@ -263,8 +262,10 @@ class TarLib {
if(!$archive && !$this->_memdat) return -10;
if(!$name) $name = utf8_basename($this->_nomf);
- if($archive){ if(!file_exists($archive)) return -11; }
- else $decoded = $this->getDynamicArchive();
+ if($archive) {
+ if(!file_exists($archive)) return -11;
+ }
+ $decoded = $this->getDynamicArchive();
if($headers) {
header('Content-Type: application/x-gtar');
@@ -274,10 +275,10 @@ class TarLib {
}
if($archive) {
- $fp = @fopen($archive,'rb');
+ $fp = @fopen($archive, 'rb');
if(!$fp) return -4;
- while(!feof($fp)) echo fread($fp,2048);
+ while(!feof($fp)) echo fread($fp, 2048);
} else {
echo $decoded;
}
@@ -311,10 +312,10 @@ class TarLib {
* permission in octal mode (prefixed with a 0) that will be given on each
* extracted file.
*/
- function Extract($p_what = TarLib::FULL_ARCHIVE, $p_to = '.', $p_remdir='', $p_mode = 0755) {
+ function Extract($p_what = TarLib::FULL_ARCHIVE, $p_to = '.', $p_remdir = '', $p_mode = 0755) {
if(!$this->_OpenRead()) return -4;
// if(!@is_dir($p_to)) if(!@mkdir($p_to, 0777)) return -8; --CS
- if(!@is_dir($p_to)) if(!$this->_dirApp($p_to)) return -8; //--CS (route through correct dir fn)
+ if(!@is_dir($p_to)) if(!$this->_dirApp($p_to)) return -8; //--CS (route through correct dir fn)
$ok = $this->_extractList($p_to, $p_what, $p_remdir, $p_mode);
$this->_CompTar();
@@ -327,21 +328,20 @@ class TarLib {
*
* This function will attempt to create a new archive with global headers
* then add the given files into. If the archive is a real file, the
- * contents are written directly into the file, if it is a dynamic archive
+ * contents are written directly into the file. If it is a dynamic archive,
* contents are only stored in memory. This function should not be used to
* add files to an existing archive, you should use Add() instead.
*
- * The FileList actually supports three different modes :
+ * The FileList actually supports three different modes:
*
* - You can pass a string containing filenames separated by pipes '|'.
- * In this case the file are read from the webserver filesystem and the
- * root folder is the folder where the script using the MaxgTar is called.
+ * In this case thes file are read from the filesystem and the root folder
+ * is the folder running script located. NOT RECOMMENDED
*
- * - You can also give a unidimensional indexed array containing the
- * filenames. The behaviour for the content reading is the same that a
- * '|'ed string.
+ * - You can also give an indexed array containing the filenames. The
+ * behaviour for the content reading is the same as above.
*
- * - The more useful usage is to pass bidimensional arrays, where the
+ * - You can pass an array of arrays. For each file use an array where the
* first element contains the filename and the second contains the file
* contents. You can even add empty folders to the package if the filename
* has a leading '/'. Once again, have a look at the exemples to understand
@@ -357,15 +357,15 @@ class TarLib {
* to the file you store. Note also that the RemovePath is applied before the
* AddPath is added, so it HAS a sense to use both parameters together.
*/
- function Create($p_filelist,$p_add='',$p_rem='') {
+ function Create($p_filelist, $p_add = '', $p_rem = '') {
if(!$fl = $this->_fetchFilelist($p_filelist)) return -5;
if(!$this->_OpenWrite()) return -6;
- $ok = $this->_addFileList($fl,$p_add,$p_rem);
+ $ok = $this->_addFileList($fl, $p_add, $p_rem);
- if($ok){
+ if($ok) {
$this->_writeFooter();
- }else{
+ } else {
$this->_CompTar();
@unlink($this->_nomf);
}
@@ -376,18 +376,26 @@ class TarLib {
/**
* Add files to an existing package.
*
- * This function does exactly the same than Create() exept it
- * will append the given files at the end of the archive. Please not the is
- * safe to call Add() on a newly created archive whereas the
- * contrary will fail !
+ * This function does exactly the same as Create() exept it
+ * will append the given files at the end of the archive.
+ *
+ * Note: This is only supported for dynamic in memory files and uncompressed
+ * tar files
*
* This function returns a status code, you can use TarErrorStr() on
* it to get the human-readable description of the error.
*/
function Add($p_filelist, $p_add = '', $p_rem = '') {
- if (($this->_nomf != TarLib::ARCHIVE_DYNAMIC && @is_file($this->_nomf)) ||
- ($this->_nomf == TarLib::ARCHIVE_DYNAMIC && !$this->_memdat)){
- return $this->Create($p_filelist, $p_add, $p_rem);
+ if($this->_nomf !== TarLib::ARCHIVE_DYNAMIC &&
+ $this->_comptype !== TarLib::COMPRESS_NONE
+ ) {
+ return -12;
+ }
+
+ if(($this->_nomf !== TarLib::ARCHIVE_DYNAMIC && !$this->_fp) ||
+ ($this->_nomf === TarLib::ARCHIVE_DYNAMIC && !$this->_memdat)
+ ) {
+ return $this->Create($p_filelist, $p_add, $p_rem);
}
if(!$fl = $this->_fetchFilelist($p_filelist)) return -5;
@@ -419,15 +427,15 @@ class TarLib {
$result = Array();
- while ($dat = $this->_read(512)) {
+ while($dat = $this->_read(512)) {
$dat = $this->_readHeader($dat);
if(!is_array($dat)) continue;
- $this->_seek(ceil($dat['size']/512)*512,1);
+ $this->_seek(ceil($dat['size'] / 512) * 512, 1);
$result[] = $dat;
}
- return $result;
+ return $result;
}
/**
@@ -439,35 +447,48 @@ class TarLib {
*/
function TarErrorStr($i) {
$ecodes = Array(
- 1 => true,
- 0 => "Undocumented error",
- -1 => "Can't use COMPRESS_GZIP compression : ZLIB extensions are not loaded !",
- -2 => "Can't use COMPRESS_BZIP compression : BZ2 extensions are not loaded !",
- -3 => "You must set a archive file to read the contents !",
- -4 => "Can't open the archive file for read !",
- -5 => "Invalide file list !",
- -6 => "Can't open the archive in write mode !",
- -7 => "There is no ARCHIVE_DYNAMIC to write !",
- -8 => "Can't create the directory to extract files !",
- -9 => "Please pass a archive name to send if you made created an ARCHIVE_DYNAMIC !",
- -10 => "You didn't pass an archive filename and there is no stored ARCHIVE_DYNAMIC !",
- -11 => "Given archive doesn't exist !"
- );
+ 1 => true,
+ 0 => "Undocumented error",
+ -1 => "Can't use COMPRESS_GZIP compression : ZLIB extensions are not loaded !",
+ -2 => "Can't use COMPRESS_BZIP compression : BZ2 extensions are not loaded !",
+ -3 => "You must set a archive file to read the contents !",
+ -4 => "Can't open the archive file for read !",
+ -5 => "Invalide file list !",
+ -6 => "Can't open the archive in write mode !",
+ -7 => "There is no ARCHIVE_DYNAMIC to write !",
+ -8 => "Can't create the directory to extract files !",
+ -9 => "Please pass a archive name to send if you made created an ARCHIVE_DYNAMIC !",
+ -10 => "You didn't pass an archive filename and there is no stored ARCHIVE_DYNAMIC !",
+ -11 => "Given archive doesn't exist !",
+ -12 => "Appending not supported for compressed files"
+ );
return isset($ecodes[$i]) ? $ecodes[$i] : $ecodes[0];
}
- function _seek($p_flen, $tell=0) {
+ /**
+ * Seek in the data stream
+ *
+ * @todo probably broken for bzip tars
+ * @param int $p_flen seek to this position
+ * @param bool $tell seek from current position?
+ */
+ function _seek($p_flen, $tell = false) {
if($this->_nomf === TarLib::ARCHIVE_DYNAMIC)
- $this->_memdat=substr($this->_memdat,0,($tell ? strlen($this->_memdat) : 0) + $p_flen);
+ $this->_memdat = substr($this->_memdat, 0, ($tell ? strlen($this->_memdat) : 0) + $p_flen);
elseif($this->_comptype == TarLib::COMPRESS_GZIP)
- @gzseek($this->_fp, ($tell ? @gztell($this->_fp) : 0)+$p_flen);
+ @gzseek($this->_fp, ($tell ? @gztell($this->_fp) : 0) + $p_flen);
elseif($this->_comptype == TarLib::COMPRESS_BZIP)
- @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0)+$p_flen);
+ @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0) + $p_flen);
else
- @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0)+$p_flen);
+ @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0) + $p_flen);
}
+ /**
+ * Open the archive for reading
+ *
+ * @return bool true if succesfull
+ */
function _OpenRead() {
if($this->_comptype == TarLib::COMPRESS_GZIP)
$this->_fp = @gzopen($this->_nomf, 'rb');
@@ -479,6 +500,12 @@ class TarLib {
return ($this->_fp ? true : false);
}
+ /**
+ * Open the archive for writing
+ *
+ * @param string $add filemode
+ * @return bool true on success
+ */
function _OpenWrite($add = 'w') {
if($this->_nomf === TarLib::ARCHIVE_DYNAMIC) return true;
@@ -492,6 +519,9 @@ class TarLib {
return ($this->_fp ? true : false);
}
+ /**
+ * Closes the open file pointer
+ */
function _CompTar() {
if($this->_nomf === TarLib::ARCHIVE_DYNAMIC || !$this->_fp) return;
@@ -500,27 +530,46 @@ class TarLib {
else @fclose($this->_fp);
}
+ /**
+ * Read from the open file pointer
+ *
+ * @param int $p_len bytes to read
+ * @return string
+ */
function _read($p_len) {
if($this->_comptype == TarLib::COMPRESS_GZIP)
- return @gzread($this->_fp,$p_len);
+ return @gzread($this->_fp, $p_len);
elseif($this->_comptype == TarLib::COMPRESS_BZIP)
- return @bzread($this->_fp,$p_len);
+ return @bzread($this->_fp, $p_len);
else
- return @fread($this->_fp,$p_len);
+ return @fread($this->_fp, $p_len);
}
+ /**
+ * Write to the open filepointer or memory
+ *
+ * @param string $p_data
+ * @return int
+ */
function _write($p_data) {
- if($this->_nomf === TarLib::ARCHIVE_DYNAMIC) $this->_memdat .= $p_data;
- elseif($this->_comptype == TarLib::COMPRESS_GZIP)
- return @gzwrite($this->_fp,$p_data);
-
- elseif($this->_comptype == TarLib::COMPRESS_BZIP)
- return @bzwrite($this->_fp,$p_data);
-
- else
- return @fwrite($this->_fp,$p_data);
+ if($this->_nomf === TarLib::ARCHIVE_DYNAMIC) {
+ $this->_memdat .= $p_data;
+ return strlen($p_data);
+ } elseif($this->_comptype == TarLib::COMPRESS_GZIP) {
+ return @gzwrite($this->_fp, $p_data);
+ } elseif($this->_comptype == TarLib::COMPRESS_BZIP) {
+ return @bzwrite($this->_fp, $p_data);
+ } else {
+ return @fwrite($this->_fp, $p_data);
+ }
}
+ /**
+ * Compress given data according to the set compression method
+ *
+ * @param $p_dat
+ * @return string
+ */
function _encode($p_dat) {
if($this->_comptype == TarLib::COMPRESS_GZIP)
return gzencode($p_dat, $this->_compzlevel);
@@ -529,55 +578,77 @@ class TarLib {
else return $p_dat;
}
+ /**
+ * Decode the given tar file header
+ *
+ * @param $p_dat
+ * @return array|bool
+ */
function _readHeader($p_dat) {
- if (!$p_dat || strlen($p_dat) != 512) return false;
+ if(!$p_dat || strlen($p_dat) != 512) return false;
- for ($i=0, $chks=0; $i<148; $i++)
+ for($i = 0, $chks = 0; $i < 148; $i++)
$chks += ord($p_dat[$i]);
- for ($i=156,$chks+=256; $i<512; $i++)
+ for($i = 156, $chks += 256; $i < 512; $i++)
$chks += ord($p_dat[$i]);
$headers = @unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $p_dat);
if(!$headers) return false;
$return['checksum'] = OctDec(trim($headers['checksum']));
- if ($return['checksum'] != $chks) return false;
+ if($return['checksum'] != $chks) return false;
$return['filename'] = trim($headers['filename']);
- $return['mode'] = OctDec(trim($headers['mode']));
- $return['uid'] = OctDec(trim($headers['uid']));
- $return['gid'] = OctDec(trim($headers['gid']));
- $return['size'] = OctDec(trim($headers['size']));
- $return['mtime'] = OctDec(trim($headers['mtime']));
+ $return['mode'] = OctDec(trim($headers['mode']));
+ $return['uid'] = OctDec(trim($headers['uid']));
+ $return['gid'] = OctDec(trim($headers['gid']));
+ $return['size'] = OctDec(trim($headers['size']));
+ $return['mtime'] = OctDec(trim($headers['mtime']));
$return['typeflag'] = $headers['typeflag'];
- $return['link'] = trim($headers['link']);
- $return['uname'] = trim($headers['uname']);
- $return['gname'] = trim($headers['gname']);
+ $return['link'] = trim($headers['link']);
+ $return['uname'] = trim($headers['uname']);
+ $return['gname'] = trim($headers['gname']);
return $return;
}
+ /**
+ * Builds a normalized file list
+ *
+ * @todo remove string support, use saner format
+ *
+ * @param $p_filelist
+ * @return array|bool
+ */
function _fetchFilelist($p_filelist) {
if(!$p_filelist || (is_array($p_filelist) && !@count($p_filelist))) return false;
if(is_string($p_filelist)) {
- $p_filelist = explode('|',$p_filelist);
+ $p_filelist = explode('|', $p_filelist);
if(!is_array($p_filelist)) $p_filelist = Array($p_filelist);
}
return $p_filelist;
}
+ /**
+ * Adds files given as file list
+ *
+ * @param array $p_fl
+ * @param string $p_addir
+ * @param string $p_remdir
+ * @return bool
+ */
function _addFileList($p_fl, $p_addir, $p_remdir) {
foreach($p_fl as $file) {
- if(($file == $this->_nomf && $this->_nomf != TarLib::ARCHIVE_DYNAMIC) || !$file || (!file_exists($file) && !is_array($file)))
+ if(($file == $this->_nomf && $this->_nomf !== TarLib::ARCHIVE_DYNAMIC) || !$file || (!is_array($file) && !file_exists($file)))
continue;
- if (!$this->_addFile($file, $p_addir, $p_remdir))
+ if(!$this->_addFile($file, $p_addir, $p_remdir))
continue;
- if (@is_dir($file)) {
+ if(@is_dir($file)) {
$d = @opendir($file);
if(!$d) continue;
@@ -592,26 +663,36 @@ class TarLib {
}
closedir($d);
- unset($tmplist,$f);
+ unset($tmplist, $f);
}
}
return true;
}
+ /**
+ * Adds a single file
+ *
+ * @param array|string $p_fn
+ * @param string $p_addir
+ * @param string $p_remdir
+ * @return bool
+ */
function _addFile($p_fn, $p_addir = '', $p_remdir = '') {
+ $data = false;
if(is_array($p_fn)) list($p_fn, $data) = $p_fn;
$sname = $p_fn;
if($p_remdir) {
- if(substr($p_remdir,-1) != '/') $p_remdir .= "/";
+ if(substr($p_remdir, -1) != '/') $p_remdir .= "/";
if(substr($sname, 0, strlen($p_remdir)) == $p_remdir)
$sname = substr($sname, strlen($p_remdir));
}
- if($p_addir) $sname = $p_addir.(substr($p_addir,-1) == '/' ? '' : "/").$sname;
+ if($p_addir) $sname = $p_addir.(substr($p_addir, -1) == '/' ? '' : "/").$sname;
- if(strlen($sname) > 99) return;
+ // FIXME ustar should support up 256 chars
+ if(strlen($sname) > 99) return false;
if(@is_dir($p_fn)) {
if(!$this->_writeFileHeader($p_fn, $sname)) return false;
@@ -625,14 +706,14 @@ class TarLib {
if(!$data) {
while(!feof($fp)) {
- $packed = pack("a512", fread($fp,512));
+ $packed = pack("a512", fread($fp, 512));
$this->_write($packed);
}
fclose($fp);
} else {
$len = strlen($data);
- for($s = 0; $s < $len; $s += 512){
- $this->_write(pack("a512",substr($data,$s,512)));
+ for($s = 0; $s < $len; $s += 512) {
+ $this->_write(pack("a512", substr($data, $s, 512)));
}
}
}
@@ -640,53 +721,70 @@ class TarLib {
return true;
}
- function _writeFileHeader($p_file, $p_sname, $p_data=false) {
+ /**
+ * Write the header for a file in the TAR archive
+ *
+ * @param string $p_file
+ * @param string $p_sname
+ * @param bool $p_data
+ * @return bool
+ */
+ function _writeFileHeader($p_file, $p_sname, $p_data = false) {
if(!$p_data) {
- if (!$p_sname) $p_sname = $p_file;
+ if(!$p_sname) $p_sname = $p_file;
$p_sname = $this->_pathTrans($p_sname);
$h_info = stat($p_file);
- $h[0] = sprintf("%6s ", DecOct($h_info[4]));
- $h[] = sprintf("%6s ", DecOct($h_info[5]));
- $h[] = sprintf("%6s ", DecOct(fileperms($p_file)));
+ $h[0] = sprintf("%6s ", DecOct($h_info[4]));
+ $h[] = sprintf("%6s ", DecOct($h_info[5]));
+ $h[] = sprintf("%6s ", DecOct(fileperms($p_file)));
clearstatcache();
$h[] = sprintf("%11s ", DecOct(filesize($p_file)));
$h[] = sprintf("%11s", DecOct(filemtime($p_file)));
$dir = @is_dir($p_file) ? '5' : '';
} else {
- $dir = '';
+ $dir = '';
$p_data = sprintf("%11s ", DecOct($p_data));
- $time = sprintf("%11s ", DecOct(time()));
- $h = Array(" 0 "," 0 "," 40777 ",$p_data,$time);
+ $time = sprintf("%11s ", DecOct(time()));
+ $h = Array(" 0 ", " 0 ", " 40777 ", $p_data, $time);
}
$data_first = pack("a100a8a8a8a12A12", $p_sname, $h[2], $h[0], $h[1], $h[3], $h[4]);
- $data_last = pack("a1a100a6a2a32a32a8a8a155a12", $dir, '', '', '', '', '', '', '', '', "");
+ $data_last = pack("a1a100a6a2a32a32a8a8a155a12", $dir, '', '', '', '', '', '', '', '', "");
- for ($i=0,$chks=0; $i<148; $i++)
+ for($i = 0, $chks = 0; $i < 148; $i++)
$chks += ord($data_first[$i]);
- for ($i=156, $chks+=256, $j=0; $i<512; $i++, $j++)
+ for($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++)
$chks += ord($data_last[$j]);
$this->_write($data_first);
- $chks = pack("a8",sprintf("%6s ", DecOct($chks)));
+ $chks = pack("a8", sprintf("%6s ", DecOct($chks)));
$this->_write($chks.$data_last);
return true;
}
- function _append($p_filelist, $p_addir="", $p_remdir="") {
+ /**
+ * Append the given files to the already open archive
+ *
+ * @param array $p_filelist
+ * @param string $p_addir
+ * @param string $p_remdir
+ * @return bool|int
+ */
+ function _append($p_filelist, $p_addir = "", $p_remdir = "") {
if(!$this->_fp) if(!$this->_OpenWrite('a')) return -6;
- if($this->_nomf == TarLib::ARCHIVE_DYNAMIC) {
- $s = strlen($this->_memdat);
- $this->_memdat = substr($this->_memdat,0,-512);
+ if($this->_nomf === TarLib::ARCHIVE_DYNAMIC) {
+ $this->_memdat = substr($this->_memdat, 0, -512 * 2); // remove footer
} else {
+ clearstatcache();
$s = filesize($this->_nomf);
- $this->_seek($s-512);
+
+ $this->_seek($s - (512 * 2)); // remove footer
}
$ok = $this->_addFileList($p_filelist, $p_addir, $p_remdir);
@@ -695,48 +793,70 @@ class TarLib {
return $ok;
}
+ /**
+ * Cleans up a path and removes relative parts
+ *
+ * @param string $p_dir
+ * @return string
+ */
function _pathTrans($p_dir) {
- if ($p_dir) {
+ $r = '';
+ if($p_dir) {
$subf = explode("/", $p_dir);
- $r='';
- for ($i=count($subf)-1; $i>=0; $i--) {
- if ($subf[$i] == ".") {
+ for($i = count($subf) - 1; $i >= 0; $i--) {
+ if($subf[$i] == ".") {
# do nothing
- } elseif ($subf[$i] == "..") {
+ } elseif($subf[$i] == "..") {
$i--;
- } elseif (!$subf[$i] && $i!=count($subf)-1 && $i) {
+ } elseif(!$subf[$i] && $i != count($subf) - 1 && $i) {
# do nothing
} else {
- $r = $subf[$i].($i!=(count($subf)-1) ? "/".$r : "");
+ $r = $subf[$i].($i != (count($subf) - 1) ? "/".$r : "");
}
}
}
return $r;
}
+ /**
+ * Add the closing footer to the archive
+ *
+ * Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which
+ * consists of two 512 blocks of zero bytes
+ *
+ * @link http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134
+ */
function _writeFooter() {
$this->_write(pack("a512", ""));
+ $this->_write(pack("a512", ""));
}
+ /**
+ * @param $p_to
+ * @param $p_files
+ * @param $p_remdir
+ * @param int $p_mode
+ * @return array|bool|int|string
+ */
function _extractList($p_to, $p_files, $p_remdir, $p_mode = 0755) {
- if (!$p_to || ($p_to[0]!="/"&&substr($p_to,0,3)!="../"&&substr($p_to,1,3)!=":\\"&&substr($p_to,1,2)!=":/")) /*" // <- PHP Coder bug */
+ if(!$p_to || ($p_to[0] != "/" && substr($p_to, 0, 3) != "../" && substr($p_to, 1, 3) != ":\\" && substr($p_to, 1, 2) != ":/")) /*" // <- PHP Coder bug */
$p_to = "./$p_to";
- if ($p_remdir && substr($p_remdir,-1)!='/') $p_remdir .= '/';
+ if($p_remdir && substr($p_remdir, -1) != '/') $p_remdir .= '/';
$p_remdirs = strlen($p_remdir);
while($dat = $this->_read(512)) {
$headers = $this->_readHeader($dat);
if(!$headers['filename']) continue;
- if($p_files == -1 || $p_files[0] == -1){
+ if($p_files == -1 || $p_files[0] == -1) {
$extract = true;
} else {
$extract = false;
foreach($p_files as $f) {
- if(substr($f,-1) == "/") {
- if((strlen($headers['filename']) > strlen($f)) && (substr($headers['filename'],0,strlen($f))==$f)) {
+ if(substr($f, -1) == "/") {
+ if((strlen($headers['filename']) > strlen($f)) && (substr($headers['filename'], 0, strlen($f)) == $f)) {
$extract = true;
break;
}
@@ -747,15 +867,15 @@ class TarLib {
}
}
- if ($extract) {
+ if($extract) {
$det[] = $headers;
- if ($p_remdir && substr($headers['filename'],0,$p_remdirs)==$p_remdir)
- $headers['filename'] = substr($headers['filename'],$p_remdirs);
+ if($p_remdir && substr($headers['filename'], 0, $p_remdirs) == $p_remdir)
+ $headers['filename'] = substr($headers['filename'], $p_remdirs);
- if($headers['filename'].'/' == $p_remdir && $headers['typeflag']=='5') continue;
+ if($headers['filename'].'/' == $p_remdir && $headers['typeflag'] == '5') continue;
- if ($p_to != "./" && $p_to != "/") {
- while($p_to{-1}=="/") $p_to = substr($p_to,0,-1);
+ if($p_to != "./" && $p_to != "/") {
+ while($p_to{-1} == "/") $p_to = substr($p_to, 0, -1);
if($headers['filename']{0} == "/")
$headers['filename'] = $p_to.$headers['filename'];
@@ -763,29 +883,35 @@ class TarLib {
$headers['filename'] = $p_to."/".$headers['filename'];
}
- $ok = $this->_dirApp($headers['typeflag']=="5" ? $headers['filename'] : dirname($headers['filename']));
+ $ok = $this->_dirApp($headers['typeflag'] == "5" ? $headers['filename'] : dirname($headers['filename']));
if($ok < 0) return $ok;
- if (!$headers['typeflag']) {
- if (!$fp = @fopen($headers['filename'], "wb")) return -6;
- $n = floor($headers['size']/512);
+ if(!$headers['typeflag']) {
+ if(!$fp = @fopen($headers['filename'], "wb")) return -6;
+ $n = floor($headers['size'] / 512);
- for ($i=0; $i<$n; $i++){
- fwrite($fp, $this->_read(512),512);
+ for($i = 0; $i < $n; $i++) {
+ fwrite($fp, $this->_read(512), 512);
}
- if (($headers['size'] % 512) != 0) fwrite($fp, $this->_read(512), $headers['size'] % 512);
+ if(($headers['size'] % 512) != 0) fwrite($fp, $this->_read(512), $headers['size'] % 512);
fclose($fp);
touch($headers['filename'], $headers['mtime']);
chmod($headers['filename'], $p_mode);
} else {
- $this->_seek(ceil($headers['size']/512)*512,1);
+ $this->_seek(ceil($headers['size'] / 512) * 512, 1);
}
- }else $this->_seek(ceil($headers['size']/512)*512,1);
+ } else $this->_seek(ceil($headers['size'] / 512) * 512, 1);
}
return $det;
}
+ /**
+ * Create a directory hierarchy in filesystem
+ *
+ * @param string $d
+ * @return bool
+ */
function _dirApp($d) {
// map to dokuwiki function (its more robust)
return io_mkdir_p($d);