Chris@18: Chris@18: * All rights reserved. Chris@18: * Chris@18: * Redistribution and use in source and binary forms, with or without Chris@18: * modification, are permitted provided that the following conditions are met: Chris@18: * Chris@18: * * Redistributions of source code must retain the above copyright notice, Chris@18: * this list of conditions and the following disclaimer. Chris@18: * * Redistributions in binary form must reproduce the above copyright Chris@18: * notice, this list of conditions and the following disclaimer in the Chris@18: * documentation and/or other materials provided with the distribution. Chris@18: * Chris@18: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@18: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@18: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE Chris@18: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE Chris@18: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL Chris@18: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR Chris@18: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER Chris@18: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, Chris@18: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE Chris@18: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@18: * Chris@18: * @category File_Formats Chris@18: * @package Archive_Tar Chris@18: * @author Vincent Blavet Chris@18: * @copyright 1997-2010 The Authors Chris@18: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @version CVS: $Id$ Chris@18: * @link http://pear.php.net/package/Archive_Tar Chris@18: */ Chris@18: Chris@18: // If the PEAR class cannot be loaded via the autoloader, Chris@18: // then try to require_once it from the PHP include path. Chris@18: if (!class_exists('PEAR')) { Chris@18: require_once 'PEAR.php'; Chris@18: } Chris@18: Chris@18: define('ARCHIVE_TAR_ATT_SEPARATOR', 90001); Chris@18: define('ARCHIVE_TAR_END_BLOCK', pack("a512", '')); Chris@18: Chris@18: if (!function_exists('gzopen') && function_exists('gzopen64')) { Chris@18: function gzopen($filename, $mode, $use_include_path = 0) Chris@18: { Chris@18: return gzopen64($filename, $mode, $use_include_path); Chris@18: } Chris@18: } Chris@18: Chris@18: if (!function_exists('gztell') && function_exists('gztell64')) { Chris@18: function gztell($zp) Chris@18: { Chris@18: return gztell64($zp); Chris@18: } Chris@18: } Chris@18: Chris@18: if (!function_exists('gzseek') && function_exists('gzseek64')) { Chris@18: function gzseek($zp, $offset, $whence = SEEK_SET) Chris@18: { Chris@18: return gzseek64($zp, $offset, $whence); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Creates a (compressed) Tar archive Chris@18: * Chris@18: * @package Archive_Tar Chris@18: * @author Vincent Blavet Chris@18: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @version $Revision$ Chris@18: */ Chris@18: class Archive_Tar extends PEAR Chris@18: { Chris@18: /** Chris@18: * @var string Name of the Tar Chris@18: */ Chris@18: public $_tarname = ''; Chris@18: Chris@18: /** Chris@18: * @var boolean if true, the Tar file will be gzipped Chris@18: */ Chris@18: public $_compress = false; Chris@18: Chris@18: /** Chris@18: * @var string Type of compression : 'none', 'gz', 'bz2' or 'lzma2' Chris@18: */ Chris@18: public $_compress_type = 'none'; Chris@18: Chris@18: /** Chris@18: * @var string Explode separator Chris@18: */ Chris@18: public $_separator = ' '; Chris@18: Chris@18: /** Chris@18: * @var file descriptor Chris@18: */ Chris@18: public $_file = 0; Chris@18: Chris@18: /** Chris@18: * @var string Local Tar name of a remote Tar (http:// or ftp://) Chris@18: */ Chris@18: public $_temp_tarname = ''; Chris@18: Chris@18: /** Chris@18: * @var string regular expression for ignoring files or directories Chris@18: */ Chris@18: public $_ignore_regexp = ''; Chris@18: Chris@18: /** Chris@18: * @var object PEAR_Error object Chris@18: */ Chris@18: public $error_object = null; Chris@18: Chris@18: /** Chris@18: * Format for data extraction Chris@18: * Chris@18: * @var string Chris@18: */ Chris@18: public $_fmt = ''; Chris@18: Chris@18: /** Chris@18: * @var int Length of the read buffer in bytes Chris@18: */ Chris@18: protected $buffer_length; Chris@18: Chris@18: /** Chris@18: * Archive_Tar Class constructor. This flavour of the constructor only Chris@18: * declare a new Archive_Tar object, identifying it by the name of the Chris@18: * tar file. Chris@18: * If the compress argument is set the tar will be read or created as a Chris@18: * gzip or bz2 compressed TAR file. Chris@18: * Chris@18: * @param string $p_tarname The name of the tar archive to create Chris@18: * @param string $p_compress can be null, 'gz', 'bz2' or 'lzma2'. This Chris@18: * parameter indicates if gzip, bz2 or lzma2 compression Chris@18: * is required. For compatibility reason the Chris@18: * boolean value 'true' means 'gz'. Chris@18: * @param int $buffer_length Length of the read buffer in bytes Chris@18: * Chris@18: * @return bool Chris@18: */ Chris@18: public function __construct($p_tarname, $p_compress = null, $buffer_length = 512) Chris@18: { Chris@18: parent::__construct(); Chris@18: Chris@18: $this->_compress = false; Chris@18: $this->_compress_type = 'none'; Chris@18: if (($p_compress === null) || ($p_compress == '')) { Chris@18: if (@file_exists($p_tarname)) { Chris@18: if ($fp = @fopen($p_tarname, "rb")) { Chris@18: // look for gzip magic cookie Chris@18: $data = fread($fp, 2); Chris@18: fclose($fp); Chris@18: if ($data == "\37\213") { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'gz'; Chris@18: // No sure it's enought for a magic code .... Chris@18: } elseif ($data == "BZ") { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'bz2'; Chris@18: } elseif (file_get_contents($p_tarname, false, null, 1, 4) == '7zXZ') { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'lzma2'; Chris@18: } Chris@18: } Chris@18: } else { Chris@18: // probably a remote file or some file accessible Chris@18: // through a stream interface Chris@18: if (substr($p_tarname, -2) == 'gz') { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'gz'; Chris@18: } elseif ((substr($p_tarname, -3) == 'bz2') || Chris@18: (substr($p_tarname, -2) == 'bz') Chris@18: ) { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'bz2'; Chris@18: } else { Chris@18: if (substr($p_tarname, -2) == 'xz') { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'lzma2'; Chris@18: } Chris@18: } Chris@18: } Chris@18: } else { Chris@18: if (($p_compress === true) || ($p_compress == 'gz')) { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'gz'; Chris@18: } else { Chris@18: if ($p_compress == 'bz2') { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'bz2'; Chris@18: } else { Chris@18: if ($p_compress == 'lzma2') { Chris@18: $this->_compress = true; Chris@18: $this->_compress_type = 'lzma2'; Chris@18: } else { Chris@18: $this->_error( Chris@18: "Unsupported compression type '$p_compress'\n" . Chris@18: "Supported types are 'gz', 'bz2' and 'lzma2'.\n" Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: $this->_tarname = $p_tarname; Chris@18: if ($this->_compress) { // assert zlib or bz2 or xz extension support Chris@18: if ($this->_compress_type == 'gz') { Chris@18: $extname = 'zlib'; Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: $extname = 'bz2'; Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: $extname = 'xz'; Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: if (!extension_loaded($extname)) { Chris@18: PEAR::loadExtension($extname); Chris@18: } Chris@18: if (!extension_loaded($extname)) { Chris@18: $this->_error( Chris@18: "The extension '$extname' couldn't be found.\n" . Chris@18: "Please make sure your version of PHP was built " . Chris@18: "with '$extname' support.\n" Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: Chris@18: Chris@18: if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) { Chris@18: $this->_fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" . Chris@18: "a8checksum/a1typeflag/a100link/a6magic/a2version/" . Chris@18: "a32uname/a32gname/a8devmajor/a8devminor/a131prefix"; Chris@18: } else { Chris@18: $this->_fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" . Chris@18: "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" . Chris@18: "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix"; Chris@18: } Chris@18: Chris@18: Chris@18: $this->buffer_length = $buffer_length; Chris@18: } Chris@18: Chris@18: public function __destruct() Chris@18: { Chris@18: $this->_close(); Chris@18: // ----- Look for a local copy to delete Chris@18: if ($this->_temp_tarname != '') { Chris@18: @unlink($this->_temp_tarname); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method creates the archive file and add the files / directories Chris@18: * that are listed in $p_filelist. Chris@18: * If a file with the same name exist and is writable, it is replaced Chris@18: * by the new tar. Chris@18: * The method return false and a PEAR error text. Chris@18: * The $p_filelist parameter can be an array of string, each string Chris@18: * representing a filename or a directory name with their path if Chris@18: * needed. It can also be a single string with names separated by a Chris@18: * single blank. Chris@18: * For each directory added in the archive, the files and Chris@18: * sub-directories are also added. Chris@18: * See also createModify() method for more details. Chris@18: * Chris@18: * @param array $p_filelist An array of filenames and directory names, or a Chris@18: * single string with names separated by a single Chris@18: * blank space. Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: * @see createModify() Chris@18: */ Chris@18: public function create($p_filelist) Chris@18: { Chris@18: return $this->createModify($p_filelist, '', ''); Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method add the files / directories that are listed in $p_filelist in Chris@18: * the archive. If the archive does not exist it is created. Chris@18: * The method return false and a PEAR error text. Chris@18: * The files and directories listed are only added at the end of the archive, Chris@18: * even if a file with the same name is already archived. Chris@18: * See also createModify() method for more details. Chris@18: * Chris@18: * @param array $p_filelist An array of filenames and directory names, or a Chris@18: * single string with names separated by a single Chris@18: * blank space. Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: * @see createModify() Chris@18: * @access public Chris@18: */ Chris@18: public function add($p_filelist) Chris@18: { Chris@18: return $this->addModify($p_filelist, '', ''); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_path Chris@18: * @param bool $p_preserve Chris@18: * @return bool Chris@18: */ Chris@18: public function extract($p_path = '', $p_preserve = false) Chris@18: { Chris@18: return $this->extractModify($p_path, '', $p_preserve); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return array|int Chris@18: */ Chris@18: public function listContent() Chris@18: { Chris@18: $v_list_detail = array(); Chris@18: Chris@18: if ($this->_openRead()) { Chris@18: if (!$this->_extractList('', $v_list_detail, "list", '', '')) { Chris@18: unset($v_list_detail); Chris@18: $v_list_detail = 0; Chris@18: } Chris@18: $this->_close(); Chris@18: } Chris@18: Chris@18: return $v_list_detail; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method creates the archive file and add the files / directories Chris@18: * that are listed in $p_filelist. Chris@18: * If the file already exists and is writable, it is replaced by the Chris@18: * new tar. It is a create and not an add. If the file exists and is Chris@18: * read-only or is a directory it is not replaced. The method return Chris@18: * false and a PEAR error text. Chris@18: * The $p_filelist parameter can be an array of string, each string Chris@18: * representing a filename or a directory name with their path if Chris@18: * needed. It can also be a single string with names separated by a Chris@18: * single blank. Chris@18: * The path indicated in $p_remove_dir will be removed from the Chris@18: * memorized path of each file / directory listed when this path Chris@18: * exists. By default nothing is removed (empty path '') Chris@18: * The path indicated in $p_add_dir will be added at the beginning of Chris@18: * the memorized path of each file / directory listed. However it can Chris@18: * be set to empty ''. The adding of a path is done after the removing Chris@18: * of path. Chris@18: * The path add/remove ability enables the user to prepare an archive Chris@18: * for extraction in a different path than the origin files are. Chris@18: * See also addModify() method for file adding properties. Chris@18: * Chris@18: * @param array $p_filelist An array of filenames and directory names, Chris@18: * or a single string with names separated by Chris@18: * a single blank space. Chris@18: * @param string $p_add_dir A string which contains a path to be added Chris@18: * to the memorized path of each element in Chris@18: * the list. Chris@18: * @param string $p_remove_dir A string which contains a path to be Chris@18: * removed from the memorized path of each Chris@18: * element in the list, when relevant. Chris@18: * Chris@18: * @return boolean true on success, false on error. Chris@18: * @see addModify() Chris@18: */ Chris@18: public function createModify($p_filelist, $p_add_dir, $p_remove_dir = '') Chris@18: { Chris@18: $v_result = true; Chris@18: Chris@18: if (!$this->_openWrite()) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($p_filelist != '') { Chris@18: if (is_array($p_filelist)) { Chris@18: $v_list = $p_filelist; Chris@18: } elseif (is_string($p_filelist)) { Chris@18: $v_list = explode($this->_separator, $p_filelist); Chris@18: } else { Chris@18: $this->_cleanFile(); Chris@18: $this->_error('Invalid file list'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir); Chris@18: } Chris@18: Chris@18: if ($v_result) { Chris@18: $this->_writeFooter(); Chris@18: $this->_close(); Chris@18: } else { Chris@18: $this->_cleanFile(); Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method add the files / directories listed in $p_filelist at the Chris@18: * end of the existing archive. If the archive does not yet exists it Chris@18: * is created. Chris@18: * The $p_filelist parameter can be an array of string, each string Chris@18: * representing a filename or a directory name with their path if Chris@18: * needed. It can also be a single string with names separated by a Chris@18: * single blank. Chris@18: * The path indicated in $p_remove_dir will be removed from the Chris@18: * memorized path of each file / directory listed when this path Chris@18: * exists. By default nothing is removed (empty path '') Chris@18: * The path indicated in $p_add_dir will be added at the beginning of Chris@18: * the memorized path of each file / directory listed. However it can Chris@18: * be set to empty ''. The adding of a path is done after the removing Chris@18: * of path. Chris@18: * The path add/remove ability enables the user to prepare an archive Chris@18: * for extraction in a different path than the origin files are. Chris@18: * If a file/dir is already in the archive it will only be added at the Chris@18: * end of the archive. There is no update of the existing archived Chris@18: * file/dir. However while extracting the archive, the last file will Chris@18: * replace the first one. This results in a none optimization of the Chris@18: * archive size. Chris@18: * If a file/dir does not exist the file/dir is ignored. However an Chris@18: * error text is send to PEAR error. Chris@18: * If a file/dir is not readable the file/dir is ignored. However an Chris@18: * error text is send to PEAR error. Chris@18: * Chris@18: * @param array $p_filelist An array of filenames and directory Chris@18: * names, or a single string with names Chris@18: * separated by a single blank space. Chris@18: * @param string $p_add_dir A string which contains a path to be Chris@18: * added to the memorized path of each Chris@18: * element in the list. Chris@18: * @param string $p_remove_dir A string which contains a path to be Chris@18: * removed from the memorized path of Chris@18: * each element in the list, when Chris@18: * relevant. Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: */ Chris@18: public function addModify($p_filelist, $p_add_dir, $p_remove_dir = '') Chris@18: { Chris@18: $v_result = true; Chris@18: Chris@18: if (!$this->_isArchive()) { Chris@18: $v_result = $this->createModify( Chris@18: $p_filelist, Chris@18: $p_add_dir, Chris@18: $p_remove_dir Chris@18: ); Chris@18: } else { Chris@18: if (is_array($p_filelist)) { Chris@18: $v_list = $p_filelist; Chris@18: } elseif (is_string($p_filelist)) { Chris@18: $v_list = explode($this->_separator, $p_filelist); Chris@18: } else { Chris@18: $this->_error('Invalid file list'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir); Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method add a single string as a file at the Chris@18: * end of the existing archive. If the archive does not yet exists it Chris@18: * is created. Chris@18: * Chris@18: * @param string $p_filename A string which contains the full Chris@18: * filename path that will be associated Chris@18: * with the string. Chris@18: * @param string $p_string The content of the file added in Chris@18: * the archive. Chris@18: * @param bool|int $p_datetime A custom date/time (unix timestamp) Chris@18: * for the file (optional). Chris@18: * @param array $p_params An array of optional params: Chris@18: * stamp => the datetime (replaces Chris@18: * datetime above if it exists) Chris@18: * mode => the permissions on the Chris@18: * file (600 by default) Chris@18: * type => is this a link? See the Chris@18: * tar specification for details. Chris@18: * (default = regular file) Chris@18: * uid => the user ID of the file Chris@18: * (default = 0 = root) Chris@18: * gid => the group ID of the file Chris@18: * (default = 0 = root) Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: */ Chris@18: public function addString($p_filename, $p_string, $p_datetime = false, $p_params = array()) Chris@18: { Chris@18: $p_stamp = @$p_params["stamp"] ? $p_params["stamp"] : ($p_datetime ? $p_datetime : time()); Chris@18: $p_mode = @$p_params["mode"] ? $p_params["mode"] : 0600; Chris@18: $p_type = @$p_params["type"] ? $p_params["type"] : ""; Chris@18: $p_uid = @$p_params["uid"] ? $p_params["uid"] : ""; Chris@18: $p_gid = @$p_params["gid"] ? $p_params["gid"] : ""; Chris@18: $v_result = true; Chris@18: Chris@18: if (!$this->_isArchive()) { Chris@18: if (!$this->_openWrite()) { Chris@18: return false; Chris@18: } Chris@18: $this->_close(); Chris@18: } Chris@18: Chris@18: if (!$this->_openAppend()) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: // Need to check the get back to the temporary file ? .... Chris@18: $v_result = $this->_addString($p_filename, $p_string, $p_datetime, $p_params); Chris@18: Chris@18: $this->_writeFooter(); Chris@18: Chris@18: $this->_close(); Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method extract all the content of the archive in the directory Chris@18: * indicated by $p_path. When relevant the memorized path of the Chris@18: * files/dir can be modified by removing the $p_remove_path path at the Chris@18: * beginning of the file/dir path. Chris@18: * While extracting a file, if the directory path does not exists it is Chris@18: * created. Chris@18: * While extracting a file, if the file already exists it is replaced Chris@18: * without looking for last modification date. Chris@18: * While extracting a file, if the file already exists and is write Chris@18: * protected, the extraction is aborted. Chris@18: * While extracting a file, if a directory with the same name already Chris@18: * exists, the extraction is aborted. Chris@18: * While extracting a directory, if a file with the same name already Chris@18: * exists, the extraction is aborted. Chris@18: * While extracting a file/directory if the destination directory exist Chris@18: * and is write protected, or does not exist but can not be created, Chris@18: * the extraction is aborted. Chris@18: * If after extraction an extracted file does not show the correct Chris@18: * stored file size, the extraction is aborted. Chris@18: * When the extraction is aborted, a PEAR error text is set and false Chris@18: * is returned. However the result can be a partial extraction that may Chris@18: * need to be manually cleaned. Chris@18: * Chris@18: * @param string $p_path The path of the directory where the Chris@18: * files/dir need to by extracted. Chris@18: * @param string $p_remove_path Part of the memorized path that can be Chris@18: * removed if present at the beginning of Chris@18: * the file/dir path. Chris@18: * @param boolean $p_preserve Preserve user/group ownership of files Chris@18: * Chris@18: * @return boolean true on success, false on error. Chris@18: * @see extractList() Chris@18: */ Chris@18: public function extractModify($p_path, $p_remove_path, $p_preserve = false) Chris@18: { Chris@18: $v_result = true; Chris@18: $v_list_detail = array(); Chris@18: Chris@18: if ($v_result = $this->_openRead()) { Chris@18: $v_result = $this->_extractList( Chris@18: $p_path, Chris@18: $v_list_detail, Chris@18: "complete", Chris@18: 0, Chris@18: $p_remove_path, Chris@18: $p_preserve Chris@18: ); Chris@18: $this->_close(); Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method extract from the archive one file identified by $p_filename. Chris@18: * The return value is a string with the file content, or NULL on error. Chris@18: * Chris@18: * @param string $p_filename The path of the file to extract in a string. Chris@18: * Chris@18: * @return a string with the file content or NULL. Chris@18: */ Chris@18: public function extractInString($p_filename) Chris@18: { Chris@18: if ($this->_openRead()) { Chris@18: $v_result = $this->_extractInString($p_filename); Chris@18: $this->_close(); Chris@18: } else { Chris@18: $v_result = null; Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method extract from the archive only the files indicated in the Chris@18: * $p_filelist. These files are extracted in the current directory or Chris@18: * in the directory indicated by the optional $p_path parameter. Chris@18: * If indicated the $p_remove_path can be used in the same way as it is Chris@18: * used in extractModify() method. Chris@18: * Chris@18: * @param array $p_filelist An array of filenames and directory names, Chris@18: * or a single string with names separated Chris@18: * by a single blank space. Chris@18: * @param string $p_path The path of the directory where the Chris@18: * files/dir need to by extracted. Chris@18: * @param string $p_remove_path Part of the memorized path that can be Chris@18: * removed if present at the beginning of Chris@18: * the file/dir path. Chris@18: * @param boolean $p_preserve Preserve user/group ownership of files Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: * @see extractModify() Chris@18: */ Chris@18: public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_preserve = false) Chris@18: { Chris@18: $v_result = true; Chris@18: $v_list_detail = array(); Chris@18: Chris@18: if (is_array($p_filelist)) { Chris@18: $v_list = $p_filelist; Chris@18: } elseif (is_string($p_filelist)) { Chris@18: $v_list = explode($this->_separator, $p_filelist); Chris@18: } else { Chris@18: $this->_error('Invalid string list'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($v_result = $this->_openRead()) { Chris@18: $v_result = $this->_extractList( Chris@18: $p_path, Chris@18: $v_list_detail, Chris@18: "partial", Chris@18: $v_list, Chris@18: $p_remove_path, Chris@18: $p_preserve Chris@18: ); Chris@18: $this->_close(); Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method set specific attributes of the archive. It uses a variable Chris@18: * list of parameters, in the format attribute code + attribute values : Chris@18: * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ','); Chris@18: * Chris@18: * @return true on success, false on error. Chris@18: */ Chris@18: public function setAttribute() Chris@18: { Chris@18: $v_result = true; Chris@18: Chris@18: // ----- Get the number of variable list of arguments Chris@18: if (($v_size = func_num_args()) == 0) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: // ----- Get the arguments Chris@18: $v_att_list = func_get_args(); Chris@18: Chris@18: // ----- Read the attributes Chris@18: $i = 0; Chris@18: while ($i < $v_size) { Chris@18: Chris@18: // ----- Look for next option Chris@18: switch ($v_att_list[$i]) { Chris@18: // ----- Look for options that request a string value Chris@18: case ARCHIVE_TAR_ATT_SEPARATOR : Chris@18: // ----- Check the number of parameters Chris@18: if (($i + 1) >= $v_size) { Chris@18: $this->_error( Chris@18: 'Invalid number of parameters for ' Chris@18: . 'attribute ARCHIVE_TAR_ATT_SEPARATOR' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: // ----- Get the value Chris@18: $this->_separator = $v_att_list[$i + 1]; Chris@18: $i++; Chris@18: break; Chris@18: Chris@18: default : Chris@18: $this->_error('Unknown attribute code ' . $v_att_list[$i] . ''); Chris@18: return false; Chris@18: } Chris@18: Chris@18: // ----- Next attribute Chris@18: $i++; Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method sets the regular expression for ignoring files and directories Chris@18: * at import, for example: Chris@18: * $arch->setIgnoreRegexp("#CVS|\.svn#"); Chris@18: * Chris@18: * @param string $regexp regular expression defining which files or directories to ignore Chris@18: */ Chris@18: public function setIgnoreRegexp($regexp) Chris@18: { Chris@18: $this->_ignore_regexp = $regexp; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method sets the regular expression for ignoring all files and directories Chris@18: * matching the filenames in the array list at import, for example: Chris@18: * $arch->setIgnoreList(array('CVS', '.svn', 'bin/tool')); Chris@18: * Chris@18: * @param array $list a list of file or directory names to ignore Chris@18: * Chris@18: * @access public Chris@18: */ Chris@18: public function setIgnoreList($list) Chris@18: { Chris@18: $regexp = str_replace(array('#', '.', '^', '$'), array('\#', '\.', '\^', '\$'), $list); Chris@18: $regexp = '#/' . join('$|/', $list) . '#'; Chris@18: $this->setIgnoreRegexp($regexp); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_message Chris@18: */ Chris@18: public function _error($p_message) Chris@18: { Chris@18: $this->error_object = $this->raiseError($p_message); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_message Chris@18: */ Chris@18: public function _warning($p_message) Chris@18: { Chris@18: $this->error_object = $this->raiseError($p_message); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @return bool Chris@18: */ Chris@18: public function _isArchive($p_filename = null) Chris@18: { Chris@18: if ($p_filename == null) { Chris@18: $p_filename = $this->_tarname; Chris@18: } Chris@18: clearstatcache(); Chris@18: return @is_file($p_filename) && !@is_link($p_filename); Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _openWrite() Chris@18: { Chris@18: if ($this->_compress_type == 'gz' && function_exists('gzopen')) { Chris@18: $this->_file = @gzopen($this->_tarname, "wb9"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2' && function_exists('bzopen')) { Chris@18: $this->_file = @bzopen($this->_tarname, "w"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) { Chris@18: $this->_file = @xzopen($this->_tarname, 'w'); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: $this->_file = @fopen($this->_tarname, "wb"); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: if ($this->_file == 0) { Chris@18: $this->_error( Chris@18: 'Unable to open in write mode \'' Chris@18: . $this->_tarname . '\'' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _openRead() Chris@18: { Chris@18: if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') { Chris@18: Chris@18: // ----- Look if a local copy need to be done Chris@18: if ($this->_temp_tarname == '') { Chris@18: $this->_temp_tarname = uniqid('tar') . '.tmp'; Chris@18: if (!$v_file_from = @fopen($this->_tarname, 'rb')) { Chris@18: $this->_error( Chris@18: 'Unable to open in read mode \'' Chris@18: . $this->_tarname . '\'' Chris@18: ); Chris@18: $this->_temp_tarname = ''; Chris@18: return false; Chris@18: } Chris@18: if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) { Chris@18: $this->_error( Chris@18: 'Unable to open in write mode \'' Chris@18: . $this->_temp_tarname . '\'' Chris@18: ); Chris@18: $this->_temp_tarname = ''; Chris@18: return false; Chris@18: } Chris@18: while ($v_data = @fread($v_file_from, 1024)) { Chris@18: @fwrite($v_file_to, $v_data); Chris@18: } Chris@18: @fclose($v_file_from); Chris@18: @fclose($v_file_to); Chris@18: } Chris@18: Chris@18: // ----- File to open if the local copy Chris@18: $v_filename = $this->_temp_tarname; Chris@18: } else { Chris@18: // ----- File to open if the normal Tar file Chris@18: Chris@18: $v_filename = $this->_tarname; Chris@18: } Chris@18: Chris@18: if ($this->_compress_type == 'gz' && function_exists('gzopen')) { Chris@18: $this->_file = @gzopen($v_filename, "rb"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2' && function_exists('bzopen')) { Chris@18: $this->_file = @bzopen($v_filename, "r"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) { Chris@18: $this->_file = @xzopen($v_filename, "r"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: $this->_file = @fopen($v_filename, "rb"); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: if ($this->_file == 0) { Chris@18: $this->_error('Unable to open in read mode \'' . $v_filename . '\''); Chris@18: return false; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _openReadWrite() Chris@18: { Chris@18: if ($this->_compress_type == 'gz') { Chris@18: $this->_file = @gzopen($this->_tarname, "r+b"); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: $this->_error( Chris@18: 'Unable to open bz2 in read/write mode \'' Chris@18: . $this->_tarname . '\' (limitation of bz2 extension)' Chris@18: ); Chris@18: return false; Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: $this->_error( Chris@18: 'Unable to open lzma2 in read/write mode \'' Chris@18: . $this->_tarname . '\' (limitation of lzma2 extension)' Chris@18: ); Chris@18: return false; Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: $this->_file = @fopen($this->_tarname, "r+b"); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: if ($this->_file == 0) { Chris@18: $this->_error( Chris@18: 'Unable to open in read/write mode \'' Chris@18: . $this->_tarname . '\'' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _close() Chris@18: { Chris@18: //if (isset($this->_file)) { Chris@18: if (is_resource($this->_file)) { Chris@18: if ($this->_compress_type == 'gz') { Chris@18: @gzclose($this->_file); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: @bzclose($this->_file); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: @xzclose($this->_file); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: @fclose($this->_file); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: $this->_file = 0; Chris@18: } Chris@18: Chris@18: // ----- Look if a local copy need to be erase Chris@18: // Note that it might be interesting to keep the url for a time : ToDo Chris@18: if ($this->_temp_tarname != '') { Chris@18: @unlink($this->_temp_tarname); Chris@18: $this->_temp_tarname = ''; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _cleanFile() Chris@18: { Chris@18: $this->_close(); Chris@18: Chris@18: // ----- Look for a local copy Chris@18: if ($this->_temp_tarname != '') { Chris@18: // ----- Remove the local copy but not the remote tarname Chris@18: @unlink($this->_temp_tarname); Chris@18: $this->_temp_tarname = ''; Chris@18: } else { Chris@18: // ----- Remove the local tarname file Chris@18: @unlink($this->_tarname); Chris@18: } Chris@18: $this->_tarname = ''; Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param mixed $p_binary_data Chris@18: * @param integer $p_len Chris@18: * @return bool Chris@18: */ Chris@18: public function _writeBlock($p_binary_data, $p_len = null) Chris@18: { Chris@18: if (is_resource($this->_file)) { Chris@18: if ($p_len === null) { Chris@18: if ($this->_compress_type == 'gz') { Chris@18: @gzputs($this->_file, $p_binary_data); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: @bzwrite($this->_file, $p_binary_data); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: @xzwrite($this->_file, $p_binary_data); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: @fputs($this->_file, $p_binary_data); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } else { Chris@18: if ($this->_compress_type == 'gz') { Chris@18: @gzputs($this->_file, $p_binary_data, $p_len); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: @bzwrite($this->_file, $p_binary_data, $p_len); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: @xzwrite($this->_file, $p_binary_data, $p_len); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: @fputs($this->_file, $p_binary_data, $p_len); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return null|string Chris@18: */ Chris@18: public function _readBlock() Chris@18: { Chris@18: $v_block = null; Chris@18: if (is_resource($this->_file)) { Chris@18: if ($this->_compress_type == 'gz') { Chris@18: $v_block = @gzread($this->_file, 512); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: $v_block = @bzread($this->_file, 512); Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: $v_block = @xzread($this->_file, 512); Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: $v_block = @fread($this->_file, 512); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: return $v_block; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param null $p_len Chris@18: * @return bool Chris@18: */ Chris@18: public function _jumpBlock($p_len = null) Chris@18: { Chris@18: if (is_resource($this->_file)) { Chris@18: if ($p_len === null) { Chris@18: $p_len = 1; Chris@18: } Chris@18: Chris@18: if ($this->_compress_type == 'gz') { Chris@18: @gzseek($this->_file, gztell($this->_file) + ($p_len * 512)); Chris@18: } else { Chris@18: if ($this->_compress_type == 'bz2') { Chris@18: // ----- Replace missing bztell() and bzseek() Chris@18: for ($i = 0; $i < $p_len; $i++) { Chris@18: $this->_readBlock(); Chris@18: } Chris@18: } else { Chris@18: if ($this->_compress_type == 'lzma2') { Chris@18: // ----- Replace missing xztell() and xzseek() Chris@18: for ($i = 0; $i < $p_len; $i++) { Chris@18: $this->_readBlock(); Chris@18: } Chris@18: } else { Chris@18: if ($this->_compress_type == 'none') { Chris@18: @fseek($this->_file, $p_len * 512, SEEK_CUR); Chris@18: } else { Chris@18: $this->_error( Chris@18: 'Unknown or missing compression type (' Chris@18: . $this->_compress_type . ')' Chris@18: ); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _writeFooter() Chris@18: { Chris@18: if (is_resource($this->_file)) { Chris@18: // ----- Write the last 0 filled block for end of archive Chris@18: $v_binary_data = pack('a1024', ''); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param array $p_list Chris@18: * @param string $p_add_dir Chris@18: * @param string $p_remove_dir Chris@18: * @return bool Chris@18: */ Chris@18: public function _addList($p_list, $p_add_dir, $p_remove_dir) Chris@18: { Chris@18: $v_result = true; Chris@18: $v_header = array(); Chris@18: Chris@18: // ----- Remove potential windows directory separator Chris@18: $p_add_dir = $this->_translateWinPath($p_add_dir); Chris@18: $p_remove_dir = $this->_translateWinPath($p_remove_dir, false); Chris@18: Chris@18: if (!$this->_file) { Chris@18: $this->_error('Invalid file descriptor'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (sizeof($p_list) == 0) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: foreach ($p_list as $v_filename) { Chris@18: if (!$v_result) { Chris@18: break; Chris@18: } Chris@18: Chris@18: // ----- Skip the current tar name Chris@18: if ($v_filename == $this->_tarname) { Chris@18: continue; Chris@18: } Chris@18: Chris@18: if ($v_filename == '') { Chris@18: continue; Chris@18: } Chris@18: Chris@18: // ----- ignore files and directories matching the ignore regular expression Chris@18: if ($this->_ignore_regexp && preg_match($this->_ignore_regexp, '/' . $v_filename)) { Chris@18: $this->_warning("File '$v_filename' ignored"); Chris@18: continue; Chris@18: } Chris@18: Chris@18: if (!file_exists($v_filename) && !is_link($v_filename)) { Chris@18: $this->_warning("File '$v_filename' does not exist"); Chris@18: continue; Chris@18: } Chris@18: Chris@18: // ----- Add the file or directory header Chris@18: if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir)) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (@is_dir($v_filename) && !@is_link($v_filename)) { Chris@18: if (!($p_hdir = opendir($v_filename))) { Chris@18: $this->_warning("Directory '$v_filename' can not be read"); Chris@18: continue; Chris@18: } Chris@18: while (false !== ($p_hitem = readdir($p_hdir))) { Chris@18: if (($p_hitem != '.') && ($p_hitem != '..')) { Chris@18: if ($v_filename != ".") { Chris@18: $p_temp_list[0] = $v_filename . '/' . $p_hitem; Chris@18: } else { Chris@18: $p_temp_list[0] = $p_hitem; Chris@18: } Chris@18: Chris@18: $v_result = $this->_addList( Chris@18: $p_temp_list, Chris@18: $p_add_dir, Chris@18: $p_remove_dir Chris@18: ); Chris@18: } Chris@18: } Chris@18: Chris@18: unset($p_temp_list); Chris@18: unset($p_hdir); Chris@18: unset($p_hitem); Chris@18: } Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @param mixed $p_header Chris@18: * @param string $p_add_dir Chris@18: * @param string $p_remove_dir Chris@18: * @param null $v_stored_filename Chris@18: * @return bool Chris@18: */ Chris@18: public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $v_stored_filename = null) Chris@18: { Chris@18: if (!$this->_file) { Chris@18: $this->_error('Invalid file descriptor'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($p_filename == '') { Chris@18: $this->_error('Invalid file name'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (is_null($v_stored_filename)) { Chris@18: // ----- Calculate the stored filename Chris@18: $p_filename = $this->_translateWinPath($p_filename, false); Chris@18: $v_stored_filename = $p_filename; Chris@18: Chris@18: if (strcmp($p_filename, $p_remove_dir) == 0) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: if ($p_remove_dir != '') { Chris@18: if (substr($p_remove_dir, -1) != '/') { Chris@18: $p_remove_dir .= '/'; Chris@18: } Chris@18: Chris@18: if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir) { Chris@18: $v_stored_filename = substr($p_filename, strlen($p_remove_dir)); Chris@18: } Chris@18: } Chris@18: Chris@18: $v_stored_filename = $this->_translateWinPath($v_stored_filename); Chris@18: if ($p_add_dir != '') { Chris@18: if (substr($p_add_dir, -1) == '/') { Chris@18: $v_stored_filename = $p_add_dir . $v_stored_filename; Chris@18: } else { Chris@18: $v_stored_filename = $p_add_dir . '/' . $v_stored_filename; Chris@18: } Chris@18: } Chris@18: Chris@18: $v_stored_filename = $this->_pathReduction($v_stored_filename); Chris@18: } Chris@18: Chris@18: if ($this->_isArchive($p_filename)) { Chris@18: if (($v_file = @fopen($p_filename, "rb")) == 0) { Chris@18: $this->_warning( Chris@18: "Unable to open file '" . $p_filename Chris@18: . "' in binary read mode" Chris@18: ); Chris@18: return true; Chris@18: } Chris@18: Chris@18: if (!$this->_writeHeader($p_filename, $v_stored_filename)) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: while (($v_buffer = fread($v_file, $this->buffer_length)) != '') { Chris@18: $buffer_length = strlen("$v_buffer"); Chris@18: if ($buffer_length != $this->buffer_length) { Chris@18: $pack_size = ((int)($buffer_length / 512) + 1) * 512; Chris@18: $pack_format = sprintf('a%d', $pack_size); Chris@18: } else { Chris@18: $pack_format = sprintf('a%d', $this->buffer_length); Chris@18: } Chris@18: $v_binary_data = pack($pack_format, "$v_buffer"); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: fclose($v_file); Chris@18: } else { Chris@18: // ----- Only header for dir Chris@18: if (!$this->_writeHeader($p_filename, $v_stored_filename)) { Chris@18: return false; Chris@18: } Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @param string $p_string Chris@18: * @param bool $p_datetime Chris@18: * @param array $p_params Chris@18: * @return bool Chris@18: */ Chris@18: public function _addString($p_filename, $p_string, $p_datetime = false, $p_params = array()) Chris@18: { Chris@18: $p_stamp = @$p_params["stamp"] ? $p_params["stamp"] : ($p_datetime ? $p_datetime : time()); Chris@18: $p_mode = @$p_params["mode"] ? $p_params["mode"] : 0600; Chris@18: $p_type = @$p_params["type"] ? $p_params["type"] : ""; Chris@18: $p_uid = @$p_params["uid"] ? $p_params["uid"] : 0; Chris@18: $p_gid = @$p_params["gid"] ? $p_params["gid"] : 0; Chris@18: if (!$this->_file) { Chris@18: $this->_error('Invalid file descriptor'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($p_filename == '') { Chris@18: $this->_error('Invalid file name'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: // ----- Calculate the stored filename Chris@18: $p_filename = $this->_translateWinPath($p_filename, false); Chris@18: Chris@18: // ----- If datetime is not specified, set current time Chris@18: if ($p_datetime === false) { Chris@18: $p_datetime = time(); Chris@18: } Chris@18: Chris@18: if (!$this->_writeHeaderBlock( Chris@18: $p_filename, Chris@18: strlen($p_string), Chris@18: $p_stamp, Chris@18: $p_mode, Chris@18: $p_type, Chris@18: $p_uid, Chris@18: $p_gid Chris@18: ) Chris@18: ) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: $i = 0; Chris@18: while (($v_buffer = substr($p_string, (($i++) * 512), 512)) != '') { Chris@18: $v_binary_data = pack("a512", $v_buffer); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @param string $p_stored_filename Chris@18: * @return bool Chris@18: */ Chris@18: public function _writeHeader($p_filename, $p_stored_filename) Chris@18: { Chris@18: if ($p_stored_filename == '') { Chris@18: $p_stored_filename = $p_filename; Chris@18: } Chris@18: Chris@18: $v_reduced_filename = $this->_pathReduction($p_stored_filename); Chris@18: Chris@18: if (strlen($v_reduced_filename) > 99) { Chris@18: if (!$this->_writeLongHeader($v_reduced_filename, false)) { Chris@18: return false; Chris@18: } Chris@18: } Chris@18: Chris@18: $v_linkname = ''; Chris@18: if (@is_link($p_filename)) { Chris@18: $v_linkname = readlink($p_filename); Chris@18: } Chris@18: Chris@18: if (strlen($v_linkname) > 99) { Chris@18: if (!$this->_writeLongHeader($v_linkname, true)) { Chris@18: return false; Chris@18: } Chris@18: } Chris@18: Chris@18: $v_info = lstat($p_filename); Chris@18: $v_uid = sprintf("%07s", DecOct($v_info[4])); Chris@18: $v_gid = sprintf("%07s", DecOct($v_info[5])); Chris@18: $v_perms = sprintf("%07s", DecOct($v_info['mode'] & 000777)); Chris@18: $v_mtime = sprintf("%011s", DecOct($v_info['mtime'])); Chris@18: Chris@18: if (@is_link($p_filename)) { Chris@18: $v_typeflag = '2'; Chris@18: $v_size = sprintf("%011s", DecOct(0)); Chris@18: } elseif (@is_dir($p_filename)) { Chris@18: $v_typeflag = "5"; Chris@18: $v_size = sprintf("%011s", DecOct(0)); Chris@18: } else { Chris@18: $v_typeflag = '0'; Chris@18: clearstatcache(); Chris@18: $v_size = sprintf("%011s", DecOct($v_info['size'])); Chris@18: } Chris@18: Chris@18: $v_magic = 'ustar '; Chris@18: $v_version = ' '; Chris@18: Chris@18: if (function_exists('posix_getpwuid')) { Chris@18: $userinfo = posix_getpwuid($v_info[4]); Chris@18: $groupinfo = posix_getgrgid($v_info[5]); Chris@18: Chris@18: $v_uname = $userinfo['name']; Chris@18: $v_gname = $groupinfo['name']; Chris@18: } else { Chris@18: $v_uname = ''; Chris@18: $v_gname = ''; Chris@18: } Chris@18: Chris@18: $v_devmajor = ''; Chris@18: $v_devminor = ''; Chris@18: $v_prefix = ''; Chris@18: Chris@18: $v_binary_data_first = pack( Chris@18: "a100a8a8a8a12a12", Chris@18: $v_reduced_filename, Chris@18: $v_perms, Chris@18: $v_uid, Chris@18: $v_gid, Chris@18: $v_size, Chris@18: $v_mtime Chris@18: ); Chris@18: $v_binary_data_last = pack( Chris@18: "a1a100a6a2a32a32a8a8a155a12", Chris@18: $v_typeflag, Chris@18: $v_linkname, Chris@18: $v_magic, Chris@18: $v_version, Chris@18: $v_uname, Chris@18: $v_gname, Chris@18: $v_devmajor, Chris@18: $v_devminor, Chris@18: $v_prefix, Chris@18: '' Chris@18: ); Chris@18: Chris@18: // ----- Calculate the checksum Chris@18: $v_checksum = 0; Chris@18: // ..... First part of the header Chris@18: for ($i = 0; $i < 148; $i++) { Chris@18: $v_checksum += ord(substr($v_binary_data_first, $i, 1)); Chris@18: } Chris@18: // ..... Ignore the checksum value and replace it by ' ' (space) Chris@18: for ($i = 148; $i < 156; $i++) { Chris@18: $v_checksum += ord(' '); Chris@18: } Chris@18: // ..... Last part of the header Chris@18: for ($i = 156, $j = 0; $i < 512; $i++, $j++) { Chris@18: $v_checksum += ord(substr($v_binary_data_last, $j, 1)); Chris@18: } Chris@18: Chris@18: // ----- Write the first 148 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_first, 148); Chris@18: Chris@18: // ----- Write the calculated checksum Chris@18: $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum)); Chris@18: $v_binary_data = pack("a8", $v_checksum); Chris@18: $this->_writeBlock($v_binary_data, 8); Chris@18: Chris@18: // ----- Write the last 356 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_last, 356); Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @param int $p_size Chris@18: * @param int $p_mtime Chris@18: * @param int $p_perms Chris@18: * @param string $p_type Chris@18: * @param int $p_uid Chris@18: * @param int $p_gid Chris@18: * @return bool Chris@18: */ Chris@18: public function _writeHeaderBlock( Chris@18: $p_filename, Chris@18: $p_size, Chris@18: $p_mtime = 0, Chris@18: $p_perms = 0, Chris@18: $p_type = '', Chris@18: $p_uid = 0, Chris@18: $p_gid = 0 Chris@18: ) Chris@18: { Chris@18: $p_filename = $this->_pathReduction($p_filename); Chris@18: Chris@18: if (strlen($p_filename) > 99) { Chris@18: if (!$this->_writeLongHeader($p_filename, false)) { Chris@18: return false; Chris@18: } Chris@18: } Chris@18: Chris@18: if ($p_type == "5") { Chris@18: $v_size = sprintf("%011s", DecOct(0)); Chris@18: } else { Chris@18: $v_size = sprintf("%011s", DecOct($p_size)); Chris@18: } Chris@18: Chris@18: $v_uid = sprintf("%07s", DecOct($p_uid)); Chris@18: $v_gid = sprintf("%07s", DecOct($p_gid)); Chris@18: $v_perms = sprintf("%07s", DecOct($p_perms & 000777)); Chris@18: Chris@18: $v_mtime = sprintf("%11s", DecOct($p_mtime)); Chris@18: Chris@18: $v_linkname = ''; Chris@18: Chris@18: $v_magic = 'ustar '; Chris@18: Chris@18: $v_version = ' '; Chris@18: Chris@18: if (function_exists('posix_getpwuid')) { Chris@18: $userinfo = posix_getpwuid($p_uid); Chris@18: $groupinfo = posix_getgrgid($p_gid); Chris@18: Chris@18: $v_uname = $userinfo['name']; Chris@18: $v_gname = $groupinfo['name']; Chris@18: } else { Chris@18: $v_uname = ''; Chris@18: $v_gname = ''; Chris@18: } Chris@18: Chris@18: $v_devmajor = ''; Chris@18: Chris@18: $v_devminor = ''; Chris@18: Chris@18: $v_prefix = ''; Chris@18: Chris@18: $v_binary_data_first = pack( Chris@18: "a100a8a8a8a12A12", Chris@18: $p_filename, Chris@18: $v_perms, Chris@18: $v_uid, Chris@18: $v_gid, Chris@18: $v_size, Chris@18: $v_mtime Chris@18: ); Chris@18: $v_binary_data_last = pack( Chris@18: "a1a100a6a2a32a32a8a8a155a12", Chris@18: $p_type, Chris@18: $v_linkname, Chris@18: $v_magic, Chris@18: $v_version, Chris@18: $v_uname, Chris@18: $v_gname, Chris@18: $v_devmajor, Chris@18: $v_devminor, Chris@18: $v_prefix, Chris@18: '' Chris@18: ); Chris@18: Chris@18: // ----- Calculate the checksum Chris@18: $v_checksum = 0; Chris@18: // ..... First part of the header Chris@18: for ($i = 0; $i < 148; $i++) { Chris@18: $v_checksum += ord(substr($v_binary_data_first, $i, 1)); Chris@18: } Chris@18: // ..... Ignore the checksum value and replace it by ' ' (space) Chris@18: for ($i = 148; $i < 156; $i++) { Chris@18: $v_checksum += ord(' '); Chris@18: } Chris@18: // ..... Last part of the header Chris@18: for ($i = 156, $j = 0; $i < 512; $i++, $j++) { Chris@18: $v_checksum += ord(substr($v_binary_data_last, $j, 1)); Chris@18: } Chris@18: Chris@18: // ----- Write the first 148 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_first, 148); Chris@18: Chris@18: // ----- Write the calculated checksum Chris@18: $v_checksum = sprintf("%06s ", DecOct($v_checksum)); Chris@18: $v_binary_data = pack("a8", $v_checksum); Chris@18: $this->_writeBlock($v_binary_data, 8); Chris@18: Chris@18: // ----- Write the last 356 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_last, 356); Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_filename Chris@18: * @return bool Chris@18: */ Chris@18: public function _writeLongHeader($p_filename, $is_link = false) Chris@18: { Chris@18: $v_uid = sprintf("%07s", 0); Chris@18: $v_gid = sprintf("%07s", 0); Chris@18: $v_perms = sprintf("%07s", 0); Chris@18: $v_size = sprintf("%'011s", DecOct(strlen($p_filename))); Chris@18: $v_mtime = sprintf("%011s", 0); Chris@18: $v_typeflag = ($is_link ? 'K' : 'L'); Chris@18: $v_linkname = ''; Chris@18: $v_magic = 'ustar '; Chris@18: $v_version = ' '; Chris@18: $v_uname = ''; Chris@18: $v_gname = ''; Chris@18: $v_devmajor = ''; Chris@18: $v_devminor = ''; Chris@18: $v_prefix = ''; Chris@18: Chris@18: $v_binary_data_first = pack( Chris@18: "a100a8a8a8a12a12", Chris@18: '././@LongLink', Chris@18: $v_perms, Chris@18: $v_uid, Chris@18: $v_gid, Chris@18: $v_size, Chris@18: $v_mtime Chris@18: ); Chris@18: $v_binary_data_last = pack( Chris@18: "a1a100a6a2a32a32a8a8a155a12", Chris@18: $v_typeflag, Chris@18: $v_linkname, Chris@18: $v_magic, Chris@18: $v_version, Chris@18: $v_uname, Chris@18: $v_gname, Chris@18: $v_devmajor, Chris@18: $v_devminor, Chris@18: $v_prefix, Chris@18: '' Chris@18: ); Chris@18: Chris@18: // ----- Calculate the checksum Chris@18: $v_checksum = 0; Chris@18: // ..... First part of the header Chris@18: for ($i = 0; $i < 148; $i++) { Chris@18: $v_checksum += ord(substr($v_binary_data_first, $i, 1)); Chris@18: } Chris@18: // ..... Ignore the checksum value and replace it by ' ' (space) Chris@18: for ($i = 148; $i < 156; $i++) { Chris@18: $v_checksum += ord(' '); Chris@18: } Chris@18: // ..... Last part of the header Chris@18: for ($i = 156, $j = 0; $i < 512; $i++, $j++) { Chris@18: $v_checksum += ord(substr($v_binary_data_last, $j, 1)); Chris@18: } Chris@18: Chris@18: // ----- Write the first 148 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_first, 148); Chris@18: Chris@18: // ----- Write the calculated checksum Chris@18: $v_checksum = sprintf("%06s\0 ", DecOct($v_checksum)); Chris@18: $v_binary_data = pack("a8", $v_checksum); Chris@18: $this->_writeBlock($v_binary_data, 8); Chris@18: Chris@18: // ----- Write the last 356 bytes of the header in the archive Chris@18: $this->_writeBlock($v_binary_data_last, 356); Chris@18: Chris@18: // ----- Write the filename as content of the block Chris@18: $i = 0; Chris@18: while (($v_buffer = substr($p_filename, (($i++) * 512), 512)) != '') { Chris@18: $v_binary_data = pack("a512", "$v_buffer"); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param mixed $v_binary_data Chris@18: * @param mixed $v_header Chris@18: * @return bool Chris@18: */ Chris@18: public function _readHeader($v_binary_data, &$v_header) Chris@18: { Chris@18: if (strlen($v_binary_data) == 0) { Chris@18: $v_header['filename'] = ''; Chris@18: return true; Chris@18: } Chris@18: Chris@18: if (strlen($v_binary_data) != 512) { Chris@18: $v_header['filename'] = ''; Chris@18: $this->_error('Invalid block size : ' . strlen($v_binary_data)); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (!is_array($v_header)) { Chris@18: $v_header = array(); Chris@18: } Chris@18: // ----- Calculate the checksum Chris@18: $v_checksum = 0; Chris@18: // ..... First part of the header Chris@18: $v_binary_split = str_split($v_binary_data); Chris@18: $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 0, 148))); Chris@18: $v_checksum += array_sum(array_map('ord', array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',))); Chris@18: $v_checksum += array_sum(array_map('ord', array_slice($v_binary_split, 156, 512))); Chris@18: Chris@18: Chris@18: $v_data = unpack($this->_fmt, $v_binary_data); Chris@18: Chris@18: if (strlen($v_data["prefix"]) > 0) { Chris@18: $v_data["filename"] = "$v_data[prefix]/$v_data[filename]"; Chris@18: } Chris@18: Chris@18: // ----- Extract the checksum Chris@18: $v_header['checksum'] = OctDec(trim($v_data['checksum'])); Chris@18: if ($v_header['checksum'] != $v_checksum) { Chris@18: $v_header['filename'] = ''; Chris@18: Chris@18: // ----- Look for last block (empty block) Chris@18: if (($v_checksum == 256) && ($v_header['checksum'] == 0)) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: $this->_error( Chris@18: 'Invalid checksum for file "' . $v_data['filename'] Chris@18: . '" : ' . $v_checksum . ' calculated, ' Chris@18: . $v_header['checksum'] . ' expected' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: // ----- Extract the properties Chris@18: $v_header['filename'] = rtrim($v_data['filename'], "\0"); Chris@18: if ($this->_maliciousFilename($v_header['filename'])) { Chris@18: $this->_error( Chris@18: 'Malicious .tar detected, file "' . $v_header['filename'] . Chris@18: '" will not install in desired directory tree' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: $v_header['mode'] = OctDec(trim($v_data['mode'])); Chris@18: $v_header['uid'] = OctDec(trim($v_data['uid'])); Chris@18: $v_header['gid'] = OctDec(trim($v_data['gid'])); Chris@18: $v_header['size'] = $this->_tarRecToSize($v_data['size']); Chris@18: $v_header['mtime'] = OctDec(trim($v_data['mtime'])); Chris@18: if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { Chris@18: $v_header['size'] = 0; Chris@18: } Chris@18: $v_header['link'] = trim($v_data['link']); Chris@18: /* ----- All these fields are removed form the header because Chris@18: they do not carry interesting info Chris@18: $v_header[magic] = trim($v_data[magic]); Chris@18: $v_header[version] = trim($v_data[version]); Chris@18: $v_header[uname] = trim($v_data[uname]); Chris@18: $v_header[gname] = trim($v_data[gname]); Chris@18: $v_header[devmajor] = trim($v_data[devmajor]); Chris@18: $v_header[devminor] = trim($v_data[devminor]); Chris@18: */ Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Convert Tar record size to actual size Chris@18: * Chris@18: * @param string $tar_size Chris@18: * @return size of tar record in bytes Chris@18: */ Chris@18: private function _tarRecToSize($tar_size) Chris@18: { Chris@18: /* Chris@18: * First byte of size has a special meaning if bit 7 is set. Chris@18: * Chris@18: * Bit 7 indicates base-256 encoding if set. Chris@18: * Bit 6 is the sign bit. Chris@18: * Bits 5:0 are most significant value bits. Chris@18: */ Chris@18: $ch = ord($tar_size[0]); Chris@18: if ($ch & 0x80) { Chris@18: // Full 12-bytes record is required. Chris@18: $rec_str = $tar_size . "\x00"; Chris@18: Chris@18: $size = ($ch & 0x40) ? -1 : 0; Chris@18: $size = ($size << 6) | ($ch & 0x3f); Chris@18: Chris@18: for ($num_ch = 1; $num_ch < 12; ++$num_ch) { Chris@18: $size = ($size * 256) + ord($rec_str[$num_ch]); Chris@18: } Chris@18: Chris@18: return $size; Chris@18: Chris@18: } else { Chris@18: return OctDec(trim($tar_size)); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Detect and report a malicious file name Chris@18: * Chris@18: * @param string $file Chris@18: * Chris@18: * @return bool Chris@18: */ Chris@18: private function _maliciousFilename($file) Chris@18: { Chris@18: if (strpos($file, 'phar://') === 0) { Chris@18: return true; Chris@18: } Chris@18: if (strpos($file, '../') !== false || strpos($file, '..\\') !== false) { Chris@18: return true; Chris@18: } Chris@18: return false; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param $v_header Chris@18: * @return bool Chris@18: */ Chris@18: public function _readLongHeader(&$v_header) Chris@18: { Chris@18: $v_filename = ''; Chris@18: $v_filesize = $v_header['size']; Chris@18: $n = floor($v_header['size'] / 512); Chris@18: for ($i = 0; $i < $n; $i++) { Chris@18: $v_content = $this->_readBlock(); Chris@18: $v_filename .= $v_content; Chris@18: } Chris@18: if (($v_header['size'] % 512) != 0) { Chris@18: $v_content = $this->_readBlock(); Chris@18: $v_filename .= $v_content; Chris@18: } Chris@18: Chris@18: // ----- Read the next header Chris@18: $v_binary_data = $this->_readBlock(); Chris@18: Chris@18: if (!$this->_readHeader($v_binary_data, $v_header)) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: $v_filename = rtrim(substr($v_filename, 0, $v_filesize), "\0"); Chris@18: $v_header['filename'] = $v_filename; Chris@18: if ($this->_maliciousFilename($v_filename)) { Chris@18: $this->_error( Chris@18: 'Malicious .tar detected, file "' . $v_filename . Chris@18: '" will not install in desired directory tree' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method extract from the archive one file identified by $p_filename. Chris@18: * The return value is a string with the file content, or null on error. Chris@18: * Chris@18: * @param string $p_filename The path of the file to extract in a string. Chris@18: * Chris@18: * @return a string with the file content or null. Chris@18: */ Chris@18: private function _extractInString($p_filename) Chris@18: { Chris@18: $v_result_str = ""; Chris@18: Chris@18: while (strlen($v_binary_data = $this->_readBlock()) != 0) { Chris@18: if (!$this->_readHeader($v_binary_data, $v_header)) { Chris@18: return null; Chris@18: } Chris@18: Chris@18: if ($v_header['filename'] == '') { Chris@18: continue; Chris@18: } Chris@18: Chris@18: switch ($v_header['typeflag']) { Chris@18: case 'L': Chris@18: { Chris@18: if (!$this->_readLongHeader($v_header)) { Chris@18: return null; Chris@18: } Chris@18: } Chris@18: break; Chris@18: Chris@18: case 'K': Chris@18: { Chris@18: $v_link_header = $v_header; Chris@18: if (!$this->_readLongHeader($v_link_header)) { Chris@18: return null; Chris@18: } Chris@18: $v_header['link'] = $v_link_header['filename']; Chris@18: } Chris@18: break; Chris@18: } Chris@18: Chris@18: if ($v_header['filename'] == $p_filename) { Chris@18: if ($v_header['typeflag'] == "5") { Chris@18: $this->_error( Chris@18: 'Unable to extract in string a directory ' Chris@18: . 'entry {' . $v_header['filename'] . '}' Chris@18: ); Chris@18: return null; Chris@18: } else { Chris@18: $n = floor($v_header['size'] / 512); Chris@18: for ($i = 0; $i < $n; $i++) { Chris@18: $v_result_str .= $this->_readBlock(); Chris@18: } Chris@18: if (($v_header['size'] % 512) != 0) { Chris@18: $v_content = $this->_readBlock(); Chris@18: $v_result_str .= substr( Chris@18: $v_content, Chris@18: 0, Chris@18: ($v_header['size'] % 512) Chris@18: ); Chris@18: } Chris@18: return $v_result_str; Chris@18: } Chris@18: } else { Chris@18: $this->_jumpBlock(ceil(($v_header['size'] / 512))); Chris@18: } Chris@18: } Chris@18: Chris@18: return null; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param string $p_path Chris@18: * @param string $p_list_detail Chris@18: * @param string $p_mode Chris@18: * @param string $p_file_list Chris@18: * @param string $p_remove_path Chris@18: * @param bool $p_preserve Chris@18: * @return bool Chris@18: */ Chris@18: public function _extractList( Chris@18: $p_path, Chris@18: &$p_list_detail, Chris@18: $p_mode, Chris@18: $p_file_list, Chris@18: $p_remove_path, Chris@18: $p_preserve = false Chris@18: ) Chris@18: { Chris@18: $v_result = true; Chris@18: $v_nb = 0; Chris@18: $v_extract_all = true; Chris@18: $v_listing = false; Chris@18: Chris@18: $p_path = $this->_translateWinPath($p_path, false); Chris@18: if ($p_path == '' || (substr($p_path, 0, 1) != '/' Chris@18: && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':')) Chris@18: ) { Chris@18: $p_path = "./" . $p_path; Chris@18: } Chris@18: $p_remove_path = $this->_translateWinPath($p_remove_path); Chris@18: Chris@18: // ----- Look for path to remove format (should end by /) Chris@18: if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) { Chris@18: $p_remove_path .= '/'; Chris@18: } Chris@18: $p_remove_path_size = strlen($p_remove_path); Chris@18: Chris@18: switch ($p_mode) { Chris@18: case "complete" : Chris@18: $v_extract_all = true; Chris@18: $v_listing = false; Chris@18: break; Chris@18: case "partial" : Chris@18: $v_extract_all = false; Chris@18: $v_listing = false; Chris@18: break; Chris@18: case "list" : Chris@18: $v_extract_all = false; Chris@18: $v_listing = true; Chris@18: break; Chris@18: default : Chris@18: $this->_error('Invalid extract mode (' . $p_mode . ')'); Chris@18: return false; Chris@18: } Chris@18: Chris@18: clearstatcache(); Chris@18: Chris@18: while (strlen($v_binary_data = $this->_readBlock()) != 0) { Chris@18: $v_extract_file = false; Chris@18: $v_extraction_stopped = 0; Chris@18: Chris@18: if (!$this->_readHeader($v_binary_data, $v_header)) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($v_header['filename'] == '') { Chris@18: continue; Chris@18: } Chris@18: Chris@18: switch ($v_header['typeflag']) { Chris@18: case 'L': Chris@18: { Chris@18: if (!$this->_readLongHeader($v_header)) { Chris@18: return null; Chris@18: } Chris@18: } Chris@18: break; Chris@18: Chris@18: case 'K': Chris@18: { Chris@18: $v_link_header = $v_header; Chris@18: if (!$this->_readLongHeader($v_link_header)) { Chris@18: return null; Chris@18: } Chris@18: $v_header['link'] = $v_link_header['filename']; Chris@18: } Chris@18: break; Chris@18: } Chris@18: Chris@18: // ignore extended / pax headers Chris@18: if ($v_header['typeflag'] == 'x' || $v_header['typeflag'] == 'g') { Chris@18: $this->_jumpBlock(ceil(($v_header['size'] / 512))); Chris@18: continue; Chris@18: } Chris@18: Chris@18: if ((!$v_extract_all) && (is_array($p_file_list))) { Chris@18: // ----- By default no unzip if the file is not found Chris@18: $v_extract_file = false; Chris@18: Chris@18: for ($i = 0; $i < sizeof($p_file_list); $i++) { Chris@18: // ----- Look if it is a directory Chris@18: if (substr($p_file_list[$i], -1) == '/') { Chris@18: // ----- Look if the directory is in the filename path Chris@18: if ((strlen($v_header['filename']) > strlen($p_file_list[$i])) Chris@18: && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) Chris@18: == $p_file_list[$i]) Chris@18: ) { Chris@18: $v_extract_file = true; Chris@18: break; Chris@18: } Chris@18: } // ----- It is a file, so compare the file names Chris@18: elseif ($p_file_list[$i] == $v_header['filename']) { Chris@18: $v_extract_file = true; Chris@18: break; Chris@18: } Chris@18: } Chris@18: } else { Chris@18: $v_extract_file = true; Chris@18: } Chris@18: Chris@18: // ----- Look if this file need to be extracted Chris@18: if (($v_extract_file) && (!$v_listing)) { Chris@18: if (($p_remove_path != '') Chris@18: && (substr($v_header['filename'] . '/', 0, $p_remove_path_size) Chris@18: == $p_remove_path) Chris@18: ) { Chris@18: $v_header['filename'] = substr( Chris@18: $v_header['filename'], Chris@18: $p_remove_path_size Chris@18: ); Chris@18: if ($v_header['filename'] == '') { Chris@18: continue; Chris@18: } Chris@18: } Chris@18: if (($p_path != './') && ($p_path != '/')) { Chris@18: while (substr($p_path, -1) == '/') { Chris@18: $p_path = substr($p_path, 0, strlen($p_path) - 1); Chris@18: } Chris@18: Chris@18: if (substr($v_header['filename'], 0, 1) == '/') { Chris@18: $v_header['filename'] = $p_path . $v_header['filename']; Chris@18: } else { Chris@18: $v_header['filename'] = $p_path . '/' . $v_header['filename']; Chris@18: } Chris@18: } Chris@18: if (file_exists($v_header['filename'])) { Chris@18: if ((@is_dir($v_header['filename'])) Chris@18: && ($v_header['typeflag'] == '') Chris@18: ) { Chris@18: $this->_error( Chris@18: 'File ' . $v_header['filename'] Chris@18: . ' already exists as a directory' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: if (($this->_isArchive($v_header['filename'])) Chris@18: && ($v_header['typeflag'] == "5") Chris@18: ) { Chris@18: $this->_error( Chris@18: 'Directory ' . $v_header['filename'] Chris@18: . ' already exists as a file' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: if (!is_writeable($v_header['filename'])) { Chris@18: $this->_error( Chris@18: 'File ' . $v_header['filename'] Chris@18: . ' already exists and is write protected' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: if (filemtime($v_header['filename']) > $v_header['mtime']) { Chris@18: // To be completed : An error or silent no replace ? Chris@18: } Chris@18: } // ----- Check the directory availability and create it if necessary Chris@18: elseif (($v_result Chris@18: = $this->_dirCheck( Chris@18: ($v_header['typeflag'] == "5" Chris@18: ? $v_header['filename'] Chris@18: : dirname($v_header['filename'])) Chris@18: )) != 1 Chris@18: ) { Chris@18: $this->_error('Unable to create path for ' . $v_header['filename']); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($v_extract_file) { Chris@18: if ($v_header['typeflag'] == "5") { Chris@18: if (!@file_exists($v_header['filename'])) { Chris@18: if (!@mkdir($v_header['filename'], 0777)) { Chris@18: $this->_error( Chris@18: 'Unable to create directory {' Chris@18: . $v_header['filename'] . '}' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } elseif ($v_header['typeflag'] == "2") { Chris@18: if (@file_exists($v_header['filename'])) { Chris@18: @unlink($v_header['filename']); Chris@18: } Chris@18: if (!@symlink($v_header['link'], $v_header['filename'])) { Chris@18: $this->_error( Chris@18: 'Unable to extract symbolic link {' Chris@18: . $v_header['filename'] . '}' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } else { Chris@18: if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) { Chris@18: $this->_error( Chris@18: 'Error while opening {' . $v_header['filename'] Chris@18: . '} in write binary mode' Chris@18: ); Chris@18: return false; Chris@18: } else { Chris@18: $n = floor($v_header['size'] / 512); Chris@18: for ($i = 0; $i < $n; $i++) { Chris@18: $v_content = $this->_readBlock(); Chris@18: fwrite($v_dest_file, $v_content, 512); Chris@18: } Chris@18: if (($v_header['size'] % 512) != 0) { Chris@18: $v_content = $this->_readBlock(); Chris@18: fwrite($v_dest_file, $v_content, ($v_header['size'] % 512)); Chris@18: } Chris@18: Chris@18: @fclose($v_dest_file); Chris@18: Chris@18: if ($p_preserve) { Chris@18: @chown($v_header['filename'], $v_header['uid']); Chris@18: @chgrp($v_header['filename'], $v_header['gid']); Chris@18: } Chris@18: Chris@18: // ----- Change the file mode, mtime Chris@18: @touch($v_header['filename'], $v_header['mtime']); Chris@18: if ($v_header['mode'] & 0111) { Chris@18: // make file executable, obey umask Chris@18: $mode = fileperms($v_header['filename']) | (~umask() & 0111); Chris@18: @chmod($v_header['filename'], $mode); Chris@18: } Chris@18: } Chris@18: Chris@18: // ----- Check the file size Chris@18: clearstatcache(); Chris@18: if (!is_file($v_header['filename'])) { Chris@18: $this->_error( Chris@18: 'Extracted file ' . $v_header['filename'] Chris@18: . 'does not exist. Archive may be corrupted.' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: $filesize = filesize($v_header['filename']); Chris@18: if ($filesize != $v_header['size']) { Chris@18: $this->_error( Chris@18: 'Extracted file ' . $v_header['filename'] Chris@18: . ' does not have the correct file size \'' Chris@18: . $filesize Chris@18: . '\' (' . $v_header['size'] Chris@18: . ' expected). Archive may be corrupted.' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: } Chris@18: } else { Chris@18: $this->_jumpBlock(ceil(($v_header['size'] / 512))); Chris@18: } Chris@18: } else { Chris@18: $this->_jumpBlock(ceil(($v_header['size'] / 512))); Chris@18: } Chris@18: Chris@18: /* TBC : Seems to be unused ... Chris@18: if ($this->_compress) Chris@18: $v_end_of_file = @gzeof($this->_file); Chris@18: else Chris@18: $v_end_of_file = @feof($this->_file); Chris@18: */ Chris@18: Chris@18: if ($v_listing || $v_extract_file || $v_extraction_stopped) { Chris@18: // ----- Log extracted files Chris@18: if (($v_file_dir = dirname($v_header['filename'])) Chris@18: == $v_header['filename'] Chris@18: ) { Chris@18: $v_file_dir = ''; Chris@18: } Chris@18: if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) { Chris@18: $v_file_dir = '/'; Chris@18: } Chris@18: Chris@18: $p_list_detail[$v_nb++] = $v_header; Chris@18: if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) { Chris@18: return true; Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @return bool Chris@18: */ Chris@18: public function _openAppend() Chris@18: { Chris@18: if (filesize($this->_tarname) == 0) { Chris@18: return $this->_openWrite(); Chris@18: } Chris@18: Chris@18: if ($this->_compress) { Chris@18: $this->_close(); Chris@18: Chris@18: if (!@rename($this->_tarname, $this->_tarname . ".tmp")) { Chris@18: $this->_error( Chris@18: 'Error while renaming \'' . $this->_tarname Chris@18: . '\' to temporary file \'' . $this->_tarname Chris@18: . '.tmp\'' Chris@18: ); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($this->_compress_type == 'gz') { Chris@18: $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb"); Chris@18: } elseif ($this->_compress_type == 'bz2') { Chris@18: $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r"); Chris@18: } elseif ($this->_compress_type == 'lzma2') { Chris@18: $v_temp_tar = @xzopen($this->_tarname . ".tmp", "r"); Chris@18: } Chris@18: Chris@18: Chris@18: if ($v_temp_tar == 0) { Chris@18: $this->_error( Chris@18: 'Unable to open file \'' . $this->_tarname Chris@18: . '.tmp\' in binary read mode' Chris@18: ); Chris@18: @rename($this->_tarname . ".tmp", $this->_tarname); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (!$this->_openWrite()) { Chris@18: @rename($this->_tarname . ".tmp", $this->_tarname); Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($this->_compress_type == 'gz') { Chris@18: $end_blocks = 0; Chris@18: Chris@18: while (!@gzeof($v_temp_tar)) { Chris@18: $v_buffer = @gzread($v_temp_tar, 512); Chris@18: if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) { Chris@18: $end_blocks++; Chris@18: // do not copy end blocks, we will re-make them Chris@18: // after appending Chris@18: continue; Chris@18: } elseif ($end_blocks > 0) { Chris@18: for ($i = 0; $i < $end_blocks; $i++) { Chris@18: $this->_writeBlock(ARCHIVE_TAR_END_BLOCK); Chris@18: } Chris@18: $end_blocks = 0; Chris@18: } Chris@18: $v_binary_data = pack("a512", $v_buffer); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: @gzclose($v_temp_tar); Chris@18: } elseif ($this->_compress_type == 'bz2') { Chris@18: $end_blocks = 0; Chris@18: Chris@18: while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) { Chris@18: if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) { Chris@18: $end_blocks++; Chris@18: // do not copy end blocks, we will re-make them Chris@18: // after appending Chris@18: continue; Chris@18: } elseif ($end_blocks > 0) { Chris@18: for ($i = 0; $i < $end_blocks; $i++) { Chris@18: $this->_writeBlock(ARCHIVE_TAR_END_BLOCK); Chris@18: } Chris@18: $end_blocks = 0; Chris@18: } Chris@18: $v_binary_data = pack("a512", $v_buffer); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: @bzclose($v_temp_tar); Chris@18: } elseif ($this->_compress_type == 'lzma2') { Chris@18: $end_blocks = 0; Chris@18: Chris@18: while (strlen($v_buffer = @xzread($v_temp_tar, 512)) > 0) { Chris@18: if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) { Chris@18: $end_blocks++; Chris@18: // do not copy end blocks, we will re-make them Chris@18: // after appending Chris@18: continue; Chris@18: } elseif ($end_blocks > 0) { Chris@18: for ($i = 0; $i < $end_blocks; $i++) { Chris@18: $this->_writeBlock(ARCHIVE_TAR_END_BLOCK); Chris@18: } Chris@18: $end_blocks = 0; Chris@18: } Chris@18: $v_binary_data = pack("a512", $v_buffer); Chris@18: $this->_writeBlock($v_binary_data); Chris@18: } Chris@18: Chris@18: @xzclose($v_temp_tar); Chris@18: } Chris@18: Chris@18: if (!@unlink($this->_tarname . ".tmp")) { Chris@18: $this->_error( Chris@18: 'Error while deleting temporary file \'' Chris@18: . $this->_tarname . '.tmp\'' Chris@18: ); Chris@18: } Chris@18: } else { Chris@18: // ----- For not compressed tar, just add files before the last Chris@18: // one or two 512 bytes block Chris@18: if (!$this->_openReadWrite()) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: clearstatcache(); Chris@18: $v_size = filesize($this->_tarname); Chris@18: Chris@18: // We might have zero, one or two end blocks. Chris@18: // The standard is two, but we should try to handle Chris@18: // other cases. Chris@18: fseek($this->_file, $v_size - 1024); Chris@18: if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) { Chris@18: fseek($this->_file, $v_size - 1024); Chris@18: } elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) { Chris@18: fseek($this->_file, $v_size - 512); Chris@18: } Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param $p_filelist Chris@18: * @param string $p_add_dir Chris@18: * @param string $p_remove_dir Chris@18: * @return bool Chris@18: */ Chris@18: public function _append($p_filelist, $p_add_dir = '', $p_remove_dir = '') Chris@18: { Chris@18: if (!$this->_openAppend()) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir)) { Chris@18: $this->_writeFooter(); Chris@18: } Chris@18: Chris@18: $this->_close(); Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Check if a directory exists and create it (including parent Chris@18: * dirs) if not. Chris@18: * Chris@18: * @param string $p_dir directory to check Chris@18: * Chris@18: * @return bool true if the directory exists or was created Chris@18: */ Chris@18: public function _dirCheck($p_dir) Chris@18: { Chris@18: clearstatcache(); Chris@18: if ((@is_dir($p_dir)) || ($p_dir == '')) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: $p_parent_dir = dirname($p_dir); Chris@18: Chris@18: if (($p_parent_dir != $p_dir) && Chris@18: ($p_parent_dir != '') && Chris@18: (!$this->_dirCheck($p_parent_dir)) Chris@18: ) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (!@mkdir($p_dir, 0777)) { Chris@18: $this->_error("Unable to create directory '$p_dir'"); Chris@18: return false; Chris@18: } Chris@18: Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Compress path by changing for example "/dir/foo/../bar" to "/dir/bar", Chris@18: * rand emove double slashes. Chris@18: * Chris@18: * @param string $p_dir path to reduce Chris@18: * Chris@18: * @return string reduced path Chris@18: */ Chris@18: private function _pathReduction($p_dir) Chris@18: { Chris@18: $v_result = ''; Chris@18: Chris@18: // ----- Look for not empty path Chris@18: if ($p_dir != '') { Chris@18: // ----- Explode path by directory names Chris@18: $v_list = explode('/', $p_dir); Chris@18: Chris@18: // ----- Study directories from last to first Chris@18: for ($i = sizeof($v_list) - 1; $i >= 0; $i--) { Chris@18: // ----- Look for current path Chris@18: if ($v_list[$i] == ".") { Chris@18: // ----- Ignore this directory Chris@18: // Should be the first $i=0, but no check is done Chris@18: } else { Chris@18: if ($v_list[$i] == "..") { Chris@18: // ----- Ignore it and ignore the $i-1 Chris@18: $i--; Chris@18: } else { Chris@18: if (($v_list[$i] == '') Chris@18: && ($i != (sizeof($v_list) - 1)) Chris@18: && ($i != 0) Chris@18: ) { Chris@18: // ----- Ignore only the double '//' in path, Chris@18: // but not the first and last / Chris@18: } else { Chris@18: $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? '/' Chris@18: . $v_result : ''); Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: if (defined('OS_WINDOWS') && OS_WINDOWS) { Chris@18: $v_result = strtr($v_result, '\\', '/'); Chris@18: } Chris@18: Chris@18: return $v_result; Chris@18: } Chris@18: Chris@18: /** Chris@18: * @param $p_path Chris@18: * @param bool $p_remove_disk_letter Chris@18: * @return string Chris@18: */ Chris@18: public function _translateWinPath($p_path, $p_remove_disk_letter = true) Chris@18: { Chris@18: if (defined('OS_WINDOWS') && OS_WINDOWS) { Chris@18: // ----- Look for potential disk letter Chris@18: if (($p_remove_disk_letter) Chris@18: && (($v_position = strpos($p_path, ':')) != false) Chris@18: ) { Chris@18: $p_path = substr($p_path, $v_position + 1); Chris@18: } Chris@18: // ----- Change potential windows directory separator Chris@18: if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) { Chris@18: $p_path = strtr($p_path, '\\', '/'); Chris@18: } Chris@18: } Chris@18: return $p_path; Chris@18: } Chris@18: }