Skip to content
Snippets Groups Projects
system.tar.inc 84.8 KiB
Newer Older
Chris Gross's avatar
Chris Gross committed
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * File::CSV
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 1997-2008,
 * Vincent Blavet <vincent@phpconcept.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
Chris Gross's avatar
Chris Gross committed
 * @category  File_Formats
 * @package   Archive_Tar
 * @author    Vincent Blavet <vincent@phpconcept.net>
 * @copyright 1997-2010 The Authors
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   CVS: $Id$
 * @link      http://pear.php.net/package/Archive_Tar
 */

 /**
 * Note on Drupal 8 porting.
Chris Gross's avatar
Chris Gross committed
 * This file origin is Tar.php, release 1.4.5 (stable) with some code
 * from PEAR.php, release 1.10.5 (stable) both at http://pear.php.net.
Chris Gross's avatar
Chris Gross committed
 * To simplify future porting from pear of this file, you should not
 * do cosmetic or other non significant changes to this file.
 * The following changes have been done:
 *  Added namespace Drupal\Core\Archiver.
 *  Removed require_once 'PEAR.php'.
 *  Added defintion of OS_WINDOWS taken from PEAR.php.
 *  Renamed class to ArchiveTar.
 *  Removed extends PEAR from class.
 *  Removed call parent:: __construct().
 *  Changed PEAR::loadExtension($extname) to this->loadExtension($extname).
 *  Added function loadExtension() taken from PEAR.php.
 *  Changed all calls of unlink() to drupal_unlink().
 *  Changed $this->error_object = &$this->raiseError($p_message)
 *  to throw new \Exception($p_message).
 */

 /**
 * Note on Drupal 7 backporting from Drupal 8.
 * File origin is core/lib/Drupal/Core/Archiver/ArchiveTar.php from Drupal 8.
 * The following changes have been done:
 *  Removed namespace Drupal\Core\Archiver.
 *  Renamed class to Archive_Tar.
 *  Changed \Exception to Exception.
Chris Gross's avatar
Chris Gross committed
 */

Chris Gross's avatar
Chris Gross committed

// Drupal removal require_once 'PEAR.php'.

// Drupal addition OS_WINDOWS as defined in PEAR.php.
if (substr(PHP_OS, 0, 3) == 'WIN') {
    define('OS_WINDOWS', true);
} else {
    define('OS_WINDOWS', false);
}

define('ARCHIVE_TAR_ATT_SEPARATOR', 90001);
define('ARCHIVE_TAR_END_BLOCK', pack("a512", ''));

if (!function_exists('gzopen') && function_exists('gzopen64')) {
    function gzopen($filename, $mode, $use_include_path = 0)
    {
        return gzopen64($filename, $mode, $use_include_path);
    }
}

if (!function_exists('gztell') && function_exists('gztell64')) {
    function gztell($zp)
    {
        return gztell64($zp);
    }
}

if (!function_exists('gzseek') && function_exists('gzseek64')) {
    function gzseek($zp, $offset, $whence = SEEK_SET)
    {
        return gzseek64($zp, $offset, $whence);
    }
}
Chris Gross's avatar
Chris Gross committed

/**
Chris Gross's avatar
Chris Gross committed
 * Creates a (compressed) Tar archive
 *
 * @package Archive_Tar
 * @author  Vincent Blavet <vincent@phpconcept.net>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version $Revision$
 */
// Drupal change class Archive_Tar extends PEAR.
class Archive_Tar
Chris Gross's avatar
Chris Gross committed
{
    /**
Chris Gross's avatar
Chris Gross committed
     * @var string Name of the Tar
     */
    public $_tarname = '';
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var boolean if true, the Tar file will be gzipped
     */
    public $_compress = false;
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var string Type of compression : 'none', 'gz', 'bz2' or 'lzma2'
     */
    public $_compress_type = 'none';
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var string Explode separator
     */
    public $_separator = ' ';
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var file descriptor
     */
    public $_file = 0;
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var string Local Tar name of a remote Tar (http:// or ftp://)
     */
    public $_temp_tarname = '';
Chris Gross's avatar
Chris Gross committed

    /**
Chris Gross's avatar
Chris Gross committed
     * @var string regular expression for ignoring files or directories
     */
    public $_ignore_regexp = '';

    /**
     * @var object PEAR_Error object
     */
    public $error_object = null;

    /**
     * Archive_Tar Class constructor. This flavour of the constructor only
     * declare a new Archive_Tar object, identifying it by the name of the
     * tar file.
     * If the compress argument is set the tar will be read or created as a
     * gzip or bz2 compressed TAR file.
     *
     * @param string $p_tarname The name of the tar archive to create
     * @param string $p_compress can be null, 'gz', 'bz2' or 'lzma2'. This
     *               parameter indicates if gzip, bz2 or lzma2 compression
     *               is required.  For compatibility reason the
     *               boolean value 'true' means 'gz'.
     *
     * @return bool
     */
    public function __construct($p_tarname, $p_compress = null)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        // Drupal removal parent::__construct().

Chris Gross's avatar
Chris Gross committed
        $this->_compress = false;
        $this->_compress_type = 'none';
        if (($p_compress === null) || ($p_compress == '')) {
            if (@file_exists($p_tarname)) {
                if ($fp = @fopen($p_tarname, "rb")) {
                    // look for gzip magic cookie
                    $data = fread($fp, 2);
                    fclose($fp);
                    if ($data == "\37\213") {
                        $this->_compress = true;
                        $this->_compress_type = 'gz';
Chris Gross's avatar
Chris Gross committed
                        // No sure it's enought for a magic code ....
Chris Gross's avatar
Chris Gross committed
                    } elseif ($data == "BZ") {
                        $this->_compress = true;
                        $this->_compress_type = 'bz2';
Chris Gross's avatar
Chris Gross committed
                    } elseif (file_get_contents($p_tarname, false, null, 1, 4) == '7zXZ') {
                        $this->_compress = true;
                        $this->_compress_type = 'lzma2';
Chris Gross's avatar
Chris Gross committed
                    }
                }
            } else {
                // probably a remote file or some file accessible
                // through a stream interface
                if (substr($p_tarname, -2) == 'gz') {
                    $this->_compress = true;
                    $this->_compress_type = 'gz';
                } elseif ((substr($p_tarname, -3) == 'bz2') ||
Chris Gross's avatar
Chris Gross committed
                    (substr($p_tarname, -2) == 'bz')
                ) {
Chris Gross's avatar
Chris Gross committed
                    $this->_compress = true;
                    $this->_compress_type = 'bz2';
Chris Gross's avatar
Chris Gross committed
                } else {
                    if (substr($p_tarname, -2) == 'xz') {
                        $this->_compress = true;
                        $this->_compress_type = 'lzma2';
                    }
Chris Gross's avatar
Chris Gross committed
                }
            }
        } else {
            if (($p_compress === true) || ($p_compress == 'gz')) {
                $this->_compress = true;
                $this->_compress_type = 'gz';
            } else {
Chris Gross's avatar
Chris Gross committed
                if ($p_compress == 'bz2') {
                    $this->_compress = true;
                    $this->_compress_type = 'bz2';
                } else {
                    if ($p_compress == 'lzma2') {
                        $this->_compress = true;
                        $this->_compress_type = 'lzma2';
                    } else {
                        $this->_error(
                            "Unsupported compression type '$p_compress'\n" .
                            "Supported types are 'gz', 'bz2' and 'lzma2'.\n"
                        );
                        return false;
                    }
                }
Chris Gross's avatar
Chris Gross committed
            }
        }
        $this->_tarname = $p_tarname;
Chris Gross's avatar
Chris Gross committed
        if ($this->_compress) { // assert zlib or bz2 or xz extension support
            if ($this->_compress_type == 'gz') {
Chris Gross's avatar
Chris Gross committed
                $extname = 'zlib';
Chris Gross's avatar
Chris Gross committed
            } else {
                if ($this->_compress_type == 'bz2') {
                    $extname = 'bz2';
                } else {
                    if ($this->_compress_type == 'lzma2') {
                        $extname = 'xz';
                    }
                }
            }
Chris Gross's avatar
Chris Gross committed

            if (!extension_loaded($extname)) {
Chris Gross's avatar
Chris Gross committed
                // Drupal change PEAR::loadExtension($extname).
Chris Gross's avatar
Chris Gross committed
                $this->loadExtension($extname);
            }
            if (!extension_loaded($extname)) {
Chris Gross's avatar
Chris Gross committed
                $this->_error(
                    "The extension '$extname' couldn't be found.\n" .
                    "Please make sure your version of PHP was built " .
                    "with '$extname' support.\n"
                );
Chris Gross's avatar
Chris Gross committed
                return false;
            }
        }


        if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) {
            $this->_fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" .
                   "a8checksum/a1typeflag/a100link/a6magic/a2version/" .
                   "a32uname/a32gname/a8devmajor/a8devminor/a131prefix";
        } else {
            $this->_fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" .
                   "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" .
                   "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix";
        }


Chris Gross's avatar
Chris Gross committed
    }

Chris Gross's avatar
Chris Gross committed
    public function __destruct()
    {
        $this->_close();
        // ----- Look for a local copy to delete
        if ($this->_temp_tarname != '') {
            @drupal_unlink($this->_temp_tarname);
        }
    }

    // Drupal addition from PEAR.php.
Chris Gross's avatar
Chris Gross committed
    /**
    * OS independent PHP extension load. Remember to take care
    * on the correct extension name for case sensitive OSes.
    *
    * @param string $ext The extension name
    * @return bool Success or not on the dl() call
    */
    public static function loadExtension($ext)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        if (extension_loaded($ext)) {
            return true;
        }
Chris Gross's avatar
Chris Gross committed

Chris Gross's avatar
Chris Gross committed
        // if either returns true dl() will produce a FATAL error, stop that
        if (
            function_exists('dl') === false ||
            ini_get('enable_dl') != 1
Chris Gross's avatar
Chris Gross committed
        ) {
            return false;
        }
Chris Gross's avatar
Chris Gross committed

Chris Gross's avatar
Chris Gross committed
        if (OS_WINDOWS) {
            $suffix = '.dll';
        } elseif (PHP_OS == 'HP-UX') {
            $suffix = '.sl';
        } elseif (PHP_OS == 'AIX') {
            $suffix = '.a';
        } elseif (PHP_OS == 'OSX') {
            $suffix = '.bundle';
        } else {
            $suffix = '.so';
Chris Gross's avatar
Chris Gross committed
        }

Chris Gross's avatar
Chris Gross committed
        return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
Chris Gross's avatar
Chris Gross committed
     * This method creates the archive file and add the files / directories
     * that are listed in $p_filelist.
     * If a file with the same name exist and is writable, it is replaced
     * by the new tar.
     * The method return false and a PEAR error text.
     * The $p_filelist parameter can be an array of string, each string
     * representing a filename or a directory name with their path if
     * needed. It can also be a single string with names separated by a
     * single blank.
     * For each directory added in the archive, the files and
     * sub-directories are also added.
     * See also createModify() method for more details.
     *
     * @param array $p_filelist An array of filenames and directory names, or a
     *              single string with names separated by a single
     *              blank space.
     *
     * @return true on success, false on error.
     * @see    createModify()
     */
    public function create($p_filelist)
Chris Gross's avatar
Chris Gross committed
    {
        return $this->createModify($p_filelist, '', '');
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method add the files / directories that are listed in $p_filelist in
     * the archive. If the archive does not exist it is created.
     * The method return false and a PEAR error text.
     * The files and directories listed are only added at the end of the archive,
     * even if a file with the same name is already archived.
     * See also createModify() method for more details.
     *
     * @param array $p_filelist An array of filenames and directory names, or a
     *              single string with names separated by a single
     *              blank space.
     *
     * @return true on success, false on error.
     * @see    createModify()
     * @access public
     */
    public function add($p_filelist)
Chris Gross's avatar
Chris Gross committed
    {
        return $this->addModify($p_filelist, '', '');
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @param string $p_path
     * @param bool $p_preserve
     * @return bool
     */
    public function extract($p_path = '', $p_preserve = false)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        return $this->extractModify($p_path, '', $p_preserve);
Chris Gross's avatar
Chris Gross committed
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @return array|int
     */
    public function listContent()
Chris Gross's avatar
Chris Gross committed
    {
        $v_list_detail = array();

        if ($this->_openRead()) {
            if (!$this->_extractList('', $v_list_detail, "list", '', '')) {
                unset($v_list_detail);
                $v_list_detail = 0;
            }
            $this->_close();
        }

        return $v_list_detail;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method creates the archive file and add the files / directories
     * that are listed in $p_filelist.
     * If the file already exists and is writable, it is replaced by the
     * new tar. It is a create and not an add. If the file exists and is
     * read-only or is a directory it is not replaced. The method return
     * false and a PEAR error text.
     * The $p_filelist parameter can be an array of string, each string
     * representing a filename or a directory name with their path if
     * needed. It can also be a single string with names separated by a
     * single blank.
     * The path indicated in $p_remove_dir will be removed from the
     * memorized path of each file / directory listed when this path
     * exists. By default nothing is removed (empty path '')
     * The path indicated in $p_add_dir will be added at the beginning of
     * the memorized path of each file / directory listed. However it can
     * be set to empty ''. The adding of a path is done after the removing
     * of path.
     * The path add/remove ability enables the user to prepare an archive
     * for extraction in a different path than the origin files are.
     * See also addModify() method for file adding properties.
     *
     * @param array $p_filelist An array of filenames and directory names,
     *                             or a single string with names separated by
     *                             a single blank space.
     * @param string $p_add_dir A string which contains a path to be added
     *                             to the memorized path of each element in
     *                             the list.
     * @param string $p_remove_dir A string which contains a path to be
     *                             removed from the memorized path of each
     *                             element in the list, when relevant.
     *
     * @return boolean true on success, false on error.
     * @see addModify()
     */
    public function createModify($p_filelist, $p_add_dir, $p_remove_dir = '')
Chris Gross's avatar
Chris Gross committed
    {
        $v_result = true;

Chris Gross's avatar
Chris Gross committed
        if (!$this->_openWrite()) {
Chris Gross's avatar
Chris Gross committed
            return false;
Chris Gross's avatar
Chris Gross committed
        }
Chris Gross's avatar
Chris Gross committed

        if ($p_filelist != '') {
Chris Gross's avatar
Chris Gross committed
            if (is_array($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
                $v_list = $p_filelist;
Chris Gross's avatar
Chris Gross committed
            } elseif (is_string($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
                $v_list = explode($this->_separator, $p_filelist);
Chris Gross's avatar
Chris Gross committed
            } else {
Chris Gross's avatar
Chris Gross committed
                $this->_cleanFile();
                $this->_error('Invalid file list');
                return false;
            }

            $v_result = $this->_addList($v_list, $p_add_dir, $p_remove_dir);
        }

        if ($v_result) {
            $this->_writeFooter();
            $this->_close();
Chris Gross's avatar
Chris Gross committed
        } else {
Chris Gross's avatar
Chris Gross committed
            $this->_cleanFile();
Chris Gross's avatar
Chris Gross committed
        }
Chris Gross's avatar
Chris Gross committed

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method add the files / directories listed in $p_filelist at the
     * end of the existing archive. If the archive does not yet exists it
     * is created.
     * The $p_filelist parameter can be an array of string, each string
     * representing a filename or a directory name with their path if
     * needed. It can also be a single string with names separated by a
     * single blank.
     * The path indicated in $p_remove_dir will be removed from the
     * memorized path of each file / directory listed when this path
     * exists. By default nothing is removed (empty path '')
     * The path indicated in $p_add_dir will be added at the beginning of
     * the memorized path of each file / directory listed. However it can
     * be set to empty ''. The adding of a path is done after the removing
     * of path.
     * The path add/remove ability enables the user to prepare an archive
     * for extraction in a different path than the origin files are.
     * If a file/dir is already in the archive it will only be added at the
     * end of the archive. There is no update of the existing archived
     * file/dir. However while extracting the archive, the last file will
     * replace the first one. This results in a none optimization of the
     * archive size.
     * If a file/dir does not exist the file/dir is ignored. However an
     * error text is send to PEAR error.
     * If a file/dir is not readable the file/dir is ignored. However an
     * error text is send to PEAR error.
     *
     * @param array $p_filelist An array of filenames and directory
     *                             names, or a single string with names
     *                             separated by a single blank space.
     * @param string $p_add_dir A string which contains a path to be
     *                             added to the memorized path of each
     *                             element in the list.
     * @param string $p_remove_dir A string which contains a path to be
     *                             removed from the memorized path of
     *                             each element in the list, when
     *                             relevant.
     *
     * @return true on success, false on error.
     */
    public function addModify($p_filelist, $p_add_dir, $p_remove_dir = '')
Chris Gross's avatar
Chris Gross committed
    {
        $v_result = true;

Chris Gross's avatar
Chris Gross committed
        if (!$this->_isArchive()) {
            $v_result = $this->createModify(
                $p_filelist,
                $p_add_dir,
                $p_remove_dir
            );
        } else {
            if (is_array($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
                $v_list = $p_filelist;
Chris Gross's avatar
Chris Gross committed
            } elseif (is_string($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
                $v_list = explode($this->_separator, $p_filelist);
Chris Gross's avatar
Chris Gross committed
            } else {
Chris Gross's avatar
Chris Gross committed
                $this->_error('Invalid file list');
                return false;
            }

            $v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
        }

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method add a single string as a file at the
     * end of the existing archive. If the archive does not yet exists it
     * is created.
     *
     * @param string $p_filename A string which contains the full
     *                           filename path that will be associated
     *                           with the string.
     * @param string $p_string The content of the file added in
     *                           the archive.
     * @param bool|int $p_datetime A custom date/time (unix timestamp)
     *                           for the file (optional).
     * @param array $p_params An array of optional params:
     *                               stamp => the datetime (replaces
     *                                   datetime above if it exists)
     *                               mode => the permissions on the
     *                                   file (600 by default)
     *                               type => is this a link?  See the
     *                                   tar specification for details.
     *                                   (default = regular file)
     *                               uid => the user ID of the file
     *                                   (default = 0 = root)
     *                               gid => the group ID of the file
     *                                   (default = 0 = root)
     *
     * @return true on success, false on error.
     */
    public function addString($p_filename, $p_string, $p_datetime = false, $p_params = array())
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        $p_stamp = @$p_params["stamp"] ? $p_params["stamp"] : ($p_datetime ? $p_datetime : time());
        $p_mode = @$p_params["mode"] ? $p_params["mode"] : 0600;
        $p_type = @$p_params["type"] ? $p_params["type"] : "";
        $p_uid = @$p_params["uid"] ? $p_params["uid"] : "";
        $p_gid = @$p_params["gid"] ? $p_params["gid"] : "";
Chris Gross's avatar
Chris Gross committed
        $v_result = true;

        if (!$this->_isArchive()) {
            if (!$this->_openWrite()) {
                return false;
            }
            $this->_close();
        }

Chris Gross's avatar
Chris Gross committed
        if (!$this->_openAppend()) {
Chris Gross's avatar
Chris Gross committed
            return false;
Chris Gross's avatar
Chris Gross committed
        }
Chris Gross's avatar
Chris Gross committed

        // Need to check the get back to the temporary file ? ....
Chris Gross's avatar
Chris Gross committed
        $v_result = $this->_addString($p_filename, $p_string, $p_datetime, $p_params);
Chris Gross's avatar
Chris Gross committed

        $this->_writeFooter();

        $this->_close();

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method extract all the content of the archive in the directory
     * indicated by $p_path. When relevant the memorized path of the
     * files/dir can be modified by removing the $p_remove_path path at the
     * beginning of the file/dir path.
     * While extracting a file, if the directory path does not exists it is
     * created.
     * While extracting a file, if the file already exists it is replaced
     * without looking for last modification date.
     * While extracting a file, if the file already exists and is write
     * protected, the extraction is aborted.
     * While extracting a file, if a directory with the same name already
     * exists, the extraction is aborted.
     * While extracting a directory, if a file with the same name already
     * exists, the extraction is aborted.
     * While extracting a file/directory if the destination directory exist
     * and is write protected, or does not exist but can not be created,
     * the extraction is aborted.
     * If after extraction an extracted file does not show the correct
     * stored file size, the extraction is aborted.
     * When the extraction is aborted, a PEAR error text is set and false
     * is returned. However the result can be a partial extraction that may
     * need to be manually cleaned.
     *
     * @param string $p_path The path of the directory where the
     *                               files/dir need to by extracted.
     * @param string $p_remove_path Part of the memorized path that can be
     *                               removed if present at the beginning of
     *                               the file/dir path.
     * @param boolean $p_preserve Preserve user/group ownership of files
     *
     * @return boolean true on success, false on error.
     * @see    extractList()
     */
    public function extractModify($p_path, $p_remove_path, $p_preserve = false)
Chris Gross's avatar
Chris Gross committed
    {
        $v_result = true;
        $v_list_detail = array();

        if ($v_result = $this->_openRead()) {
Chris Gross's avatar
Chris Gross committed
            $v_result = $this->_extractList(
                $p_path,
                $v_list_detail,
                "complete",
                0,
                $p_remove_path,
                $p_preserve
            );
Chris Gross's avatar
Chris Gross committed
            $this->_close();
        }

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method extract from the archive one file identified by $p_filename.
     * The return value is a string with the file content, or NULL on error.
     *
     * @param string $p_filename The path of the file to extract in a string.
     *
     * @return a string with the file content or NULL.
     */
    public function extractInString($p_filename)
Chris Gross's avatar
Chris Gross committed
    {
        if ($this->_openRead()) {
            $v_result = $this->_extractInString($p_filename);
            $this->_close();
        } else {
Chris Gross's avatar
Chris Gross committed
            $v_result = null;
Chris Gross's avatar
Chris Gross committed
        }

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method extract from the archive only the files indicated in the
     * $p_filelist. These files are extracted in the current directory or
     * in the directory indicated by the optional $p_path parameter.
     * If indicated the $p_remove_path can be used in the same way as it is
     * used in extractModify() method.
     *
     * @param array $p_filelist An array of filenames and directory names,
     *                               or a single string with names separated
     *                               by a single blank space.
     * @param string $p_path The path of the directory where the
     *                               files/dir need to by extracted.
     * @param string $p_remove_path Part of the memorized path that can be
     *                               removed if present at the beginning of
     *                               the file/dir path.
     * @param boolean $p_preserve Preserve user/group ownership of files
     *
     * @return true on success, false on error.
     * @see    extractModify()
     */
    public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_preserve = false)
Chris Gross's avatar
Chris Gross committed
    {
        $v_result = true;
        $v_list_detail = array();

Chris Gross's avatar
Chris Gross committed
        if (is_array($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
            $v_list = $p_filelist;
Chris Gross's avatar
Chris Gross committed
        } elseif (is_string($p_filelist)) {
Chris Gross's avatar
Chris Gross committed
            $v_list = explode($this->_separator, $p_filelist);
Chris Gross's avatar
Chris Gross committed
        } else {
Chris Gross's avatar
Chris Gross committed
            $this->_error('Invalid string list');
            return false;
        }

        if ($v_result = $this->_openRead()) {
Chris Gross's avatar
Chris Gross committed
            $v_result = $this->_extractList(
                $p_path,
                $v_list_detail,
                "partial",
                $v_list,
                $p_remove_path,
                $p_preserve
            );
Chris Gross's avatar
Chris Gross committed
            $this->_close();
        }

        return $v_result;
    }

    /**
Chris Gross's avatar
Chris Gross committed
     * This method set specific attributes of the archive. It uses a variable
     * list of parameters, in the format attribute code + attribute values :
     * $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
     *
     * @return true on success, false on error.
     */
    public function setAttribute()
Chris Gross's avatar
Chris Gross committed
    {
        $v_result = true;

        // ----- Get the number of variable list of arguments
        if (($v_size = func_num_args()) == 0) {
            return true;
        }

        // ----- Get the arguments
        $v_att_list = func_get_args();
Chris Gross's avatar
Chris Gross committed

        // ----- Read the attributes
Chris Gross's avatar
Chris Gross committed
        $i = 0;
        while ($i < $v_size) {
Chris Gross's avatar
Chris Gross committed

            // ----- Look for next option
            switch ($v_att_list[$i]) {
                // ----- Look for options that request a string value
                case ARCHIVE_TAR_ATT_SEPARATOR :
                    // ----- Check the number of parameters
Chris Gross's avatar
Chris Gross committed
                    if (($i + 1) >= $v_size) {
                        $this->_error(
                            'Invalid number of parameters for '
                            . 'attribute ARCHIVE_TAR_ATT_SEPARATOR'
                        );
Chris Gross's avatar
Chris Gross committed
                        return false;
                    }

                    // ----- Get the value
Chris Gross's avatar
Chris Gross committed
                    $this->_separator = $v_att_list[$i + 1];
Chris Gross's avatar
Chris Gross committed
                    $i++;
Chris Gross's avatar
Chris Gross committed
                    break;
Chris Gross's avatar
Chris Gross committed

                default :
Chris Gross's avatar
Chris Gross committed
                    $this->_error('Unknown attribute code ' . $v_att_list[$i] . '');
Chris Gross's avatar
Chris Gross committed
                    return false;
            }

            // ----- Next attribute
            $i++;
        }

        return $v_result;
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * This method sets the regular expression for ignoring files and directories
     * at import, for example:
     * $arch->setIgnoreRegexp("#CVS|\.svn#");
     *
     * @param string $regexp regular expression defining which files or directories to ignore
     */
    public function setIgnoreRegexp($regexp)
    {
        $this->_ignore_regexp = $regexp;
    }

    /**
     * This method sets the regular expression for ignoring all files and directories
     * matching the filenames in the array list at import, for example:
     * $arch->setIgnoreList(array('CVS', '.svn', 'bin/tool'));
     *
     * @param array $list a list of file or directory names to ignore
     *
     * @access public
     */
    public function setIgnoreList($list)
    {
        $regexp = str_replace(array('#', '.', '^', '$'), array('\#', '\.', '\^', '\$'), $list);
        $regexp = '#/' . join('$|/', $list) . '#';
        $this->setIgnoreRegexp($regexp);
    }

    /**
     * @param string $p_message
     */
    public function _error($p_message)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        // Drupal change $this->error_object = $this->raiseError($p_message).
Chris Gross's avatar
Chris Gross committed
        throw new Exception($p_message);
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @param string $p_message
     */
    public function _warning($p_message)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        // Drupal change $this->error_object = $this->raiseError($p_message).
Chris Gross's avatar
Chris Gross committed
        throw new Exception($p_message);
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @param string $p_filename
     * @return bool
     */
    public function _isArchive($p_filename = null)
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        if ($p_filename == null) {
Chris Gross's avatar
Chris Gross committed
            $p_filename = $this->_tarname;
        }
        clearstatcache();
        return @is_file($p_filename) && !@is_link($p_filename);
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @return bool
     */
    public function _openWrite()
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        if ($this->_compress_type == 'gz' && function_exists('gzopen')) {
Chris Gross's avatar
Chris Gross committed
            $this->_file = @gzopen($this->_tarname, "wb9");
Chris Gross's avatar
Chris Gross committed
        } else {
            if ($this->_compress_type == 'bz2' && function_exists('bzopen')) {
                $this->_file = @bzopen($this->_tarname, "w");
            } else {
                if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) {
                    $this->_file = @xzopen($this->_tarname, 'w');
                } else {
                    if ($this->_compress_type == 'none') {
                        $this->_file = @fopen($this->_tarname, "wb");
                    } else {
                        $this->_error(
                            'Unknown or missing compression type ('
                            . $this->_compress_type . ')'
                        );
                        return false;
                    }
                }
            }
        }
Chris Gross's avatar
Chris Gross committed

        if ($this->_file == 0) {
Chris Gross's avatar
Chris Gross committed
            $this->_error(
                'Unable to open in write mode \''
                . $this->_tarname . '\''
            );
Chris Gross's avatar
Chris Gross committed
            return false;
        }

        return true;
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @return bool
     */
    public function _openRead()
Chris Gross's avatar
Chris Gross committed
    {
        if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {

Chris Gross's avatar
Chris Gross committed
            // ----- Look if a local copy need to be done
            if ($this->_temp_tarname == '') {
                $this->_temp_tarname = uniqid('tar') . '.tmp';
                if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
                    $this->_error(
                        'Unable to open in read mode \''
                        . $this->_tarname . '\''
                    );
                    $this->_temp_tarname = '';
                    return false;
                }
                if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
                    $this->_error(
                        'Unable to open in write mode \''
                        . $this->_temp_tarname . '\''
                    );
                    $this->_temp_tarname = '';
                    return false;
                }
                while ($v_data = @fread($v_file_from, 1024)) {
                    @fwrite($v_file_to, $v_data);
                }
                @fclose($v_file_from);
                @fclose($v_file_to);
            }
Chris Gross's avatar
Chris Gross committed

Chris Gross's avatar
Chris Gross committed
            // ----- File to open if the local copy
            $v_filename = $this->_temp_tarname;
        } else {
            // ----- File to open if the normal Tar file
Chris Gross's avatar
Chris Gross committed

Chris Gross's avatar
Chris Gross committed
            $v_filename = $this->_tarname;
        }
Chris Gross's avatar
Chris Gross committed

Chris Gross's avatar
Chris Gross committed
        if ($this->_compress_type == 'gz' && function_exists('gzopen')) {
Chris Gross's avatar
Chris Gross committed
            $this->_file = @gzopen($v_filename, "rb");
Chris Gross's avatar
Chris Gross committed
        } else {
            if ($this->_compress_type == 'bz2' && function_exists('bzopen')) {
                $this->_file = @bzopen($v_filename, "r");
            } else {
                if ($this->_compress_type == 'lzma2' && function_exists('xzopen')) {
                    $this->_file = @xzopen($v_filename, "r");
                } else {
                    if ($this->_compress_type == 'none') {
                        $this->_file = @fopen($v_filename, "rb");
                    } else {
                        $this->_error(
                            'Unknown or missing compression type ('
                            . $this->_compress_type . ')'
                        );
                        return false;
                    }
                }
            }
        }
Chris Gross's avatar
Chris Gross committed

        if ($this->_file == 0) {
Chris Gross's avatar
Chris Gross committed
            $this->_error('Unable to open in read mode \'' . $v_filename . '\'');
Chris Gross's avatar
Chris Gross committed
            return false;
        }

        return true;
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @return bool
     */
    public function _openReadWrite()
Chris Gross's avatar
Chris Gross committed
    {
Chris Gross's avatar
Chris Gross committed
        if ($this->_compress_type == 'gz') {
Chris Gross's avatar
Chris Gross committed
            $this->_file = @gzopen($this->_tarname, "r+b");
Chris Gross's avatar
Chris Gross committed
        } else {
            if ($this->_compress_type == 'bz2') {
                $this->_error(
                    'Unable to open bz2 in read/write mode \''
                    . $this->_tarname . '\' (limitation of bz2 extension)'
                );
                return false;
            } else {
                if ($this->_compress_type == 'lzma2') {
                    $this->_error(
                        'Unable to open lzma2 in read/write mode \''
                        . $this->_tarname . '\' (limitation of lzma2 extension)'
                    );
                    return false;
                } else {
                    if ($this->_compress_type == 'none') {
                        $this->_file = @fopen($this->_tarname, "r+b");
                    } else {
                        $this->_error(
                            'Unknown or missing compression type ('
                            . $this->_compress_type . ')'
                        );
                        return false;
                    }
                }
            }
        }
Chris Gross's avatar
Chris Gross committed

        if ($this->_file == 0) {
Chris Gross's avatar
Chris Gross committed
            $this->_error(
                'Unable to open in read/write mode \''
                . $this->_tarname . '\''
            );
Chris Gross's avatar
Chris Gross committed
            return false;
        }

        return true;
    }

Chris Gross's avatar
Chris Gross committed
    /**
     * @return bool
     */
    public function _close()
Chris Gross's avatar
Chris Gross committed
    {
        //if (isset($this->_file)) {
        if (is_resource($this->_file)) {
Chris Gross's avatar
Chris Gross committed
            if ($this->_compress_type == 'gz') {
Chris Gross's avatar
Chris Gross committed
                @gzclose($this->_file);
Chris Gross's avatar
Chris Gross committed
            } else {
                if ($this->_compress_type == 'bz2') {
                    @bzclose($this->_file);
                } else {
                    if ($this->_compress_type == 'lzma2') {
                        @xzclose($this->_file);
                    } else {
                        if ($this->_compress_type == 'none') {
                            @fclose($this->_file);
                        } else {
                            $this->_error(
                                'Unknown or missing compression type ('
                                . $this->_compress_type . ')'