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