File indexing completed on 2024-12-22 05:36:41

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Compress_CompressAbstract
0024  */
0025 // require_once 'Zend/Filter/Compress/CompressAbstract.php';
0026 
0027 /**
0028  * Compression adapter for Tar
0029  *
0030  * @category   Zend
0031  * @package    Zend_Filter
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Filter_Compress_Tar extends Zend_Filter_Compress_CompressAbstract
0036 {
0037     /**
0038      * Compression Options
0039      * array(
0040      *     'archive'  => Archive to use
0041      *     'target'   => Target to write the files to
0042      * )
0043      *
0044      * @var array
0045      */
0046     protected $_options = array(
0047         'archive'  => null,
0048         'target'   => '.',
0049         'mode'     => null,
0050     );
0051 
0052     /**
0053      * Class constructor
0054      *
0055      * @param array $options (Optional) Options to set
0056      */
0057     public function __construct($options = null)
0058     {
0059         if (!class_exists('Archive_Tar')) {
0060             // require_once 'Zend/Loader.php';
0061             try {
0062                 Zend_Loader::loadClass('Archive_Tar');
0063             } catch (Zend_Exception $e) {
0064                 // require_once 'Zend/Filter/Exception.php';
0065                 throw new Zend_Filter_Exception('This filter needs PEARs Archive_Tar', 0, $e);
0066             }
0067         }
0068 
0069         parent::__construct($options);
0070     }
0071 
0072     /**
0073      * Returns the set archive
0074      *
0075      * @return string
0076      */
0077     public function getArchive()
0078     {
0079         return $this->_options['archive'];
0080     }
0081 
0082     /**
0083      * Sets the archive to use for de-/compression
0084      *
0085      * @param string $archive Archive to use
0086      * @return Zend_Filter_Compress_Tar
0087      */
0088     public function setArchive($archive)
0089     {
0090         $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
0091         $this->_options['archive'] = (string) $archive;
0092 
0093         return $this;
0094     }
0095 
0096     /**
0097      * Returns the set targetpath
0098      *
0099      * @return string
0100      */
0101     public function getTarget()
0102     {
0103         return $this->_options['target'];
0104     }
0105 
0106     /**
0107      * Sets the targetpath to use
0108      *
0109      * @param string $target
0110      * @return Zend_Filter_Compress_Tar
0111      */
0112     public function setTarget($target)
0113     {
0114         if (!file_exists(dirname($target))) {
0115             // require_once 'Zend/Filter/Exception.php';
0116             throw new Zend_Filter_Exception("The directory '$target' does not exist");
0117         }
0118 
0119         $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
0120         $this->_options['target'] = (string) $target;
0121         return $this;
0122     }
0123 
0124     /**
0125      * Returns the set compression mode
0126      */
0127     public function getMode()
0128     {
0129         return $this->_options['mode'];
0130     }
0131 
0132     /**
0133      * Compression mode to use
0134      * Eighter Gz or Bz2
0135      *
0136      * @param string $mode
0137      */
0138     public function setMode($mode)
0139     {
0140         $mode = ucfirst(strtolower($mode));
0141         if (($mode != 'Bz2') && ($mode != 'Gz')) {
0142             // require_once 'Zend/Filter/Exception.php';
0143             throw new Zend_Filter_Exception("The mode '$mode' is unknown");
0144         }
0145 
0146         if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
0147             // require_once 'Zend/Filter/Exception.php';
0148             throw new Zend_Filter_Exception('This mode needs the bz2 extension');
0149         }
0150 
0151         if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
0152             // require_once 'Zend/Filter/Exception.php';
0153             throw new Zend_Filter_Exception('This mode needs the zlib extension');
0154         }
0155     }
0156 
0157     /**
0158      * Compresses the given content
0159      *
0160      * @param  string $content
0161      * @return string
0162      */
0163     public function compress($content)
0164     {
0165         $archive = new Archive_Tar($this->getArchive(), $this->getMode());
0166         if (!file_exists($content)) {
0167             $file = $this->getTarget();
0168             if (is_dir($file)) {
0169                 $file .= DIRECTORY_SEPARATOR . "tar.tmp";
0170             }
0171 
0172             $result = file_put_contents($file, $content);
0173             if ($result === false) {
0174                 // require_once 'Zend/Filter/Exception.php';
0175                 throw new Zend_Filter_Exception('Error creating the temporary file');
0176             }
0177 
0178             $content = $file;
0179         }
0180 
0181         if (is_dir($content)) {
0182             // collect all file infos
0183             foreach (new RecursiveIteratorIterator(
0184                         new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
0185                         RecursiveIteratorIterator::SELF_FIRST
0186                     ) as $directory => $info
0187             ) {
0188                 if ($info->isFile()) {
0189                     $file[] = $directory;
0190                 }
0191             }
0192 
0193             $content = $file;
0194         }
0195 
0196         $result  = $archive->create($content);
0197         if ($result === false) {
0198             // require_once 'Zend/Filter/Exception.php';
0199             throw new Zend_Filter_Exception('Error creating the Tar archive');
0200         }
0201 
0202         return $this->getArchive();
0203     }
0204 
0205     /**
0206      * Decompresses the given content
0207      *
0208      * @param  string $content
0209      * @return boolean
0210      */
0211     public function decompress($content)
0212     {
0213         $archive = $this->getArchive();
0214         if (file_exists($content)) {
0215             $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
0216         } elseif (empty($archive) || !file_exists($archive)) {
0217             // require_once 'Zend/Filter/Exception.php';
0218             throw new Zend_Filter_Exception('Tar Archive not found');
0219         }
0220 
0221         $archive = new Archive_Tar($archive, $this->getMode());
0222         $target  = $this->getTarget();
0223         if (!is_dir($target)) {
0224             $target = dirname($target);
0225         }
0226 
0227         $result = $archive->extract($target);
0228         if ($result === false) {
0229             // require_once 'Zend/Filter/Exception.php';
0230             throw new Zend_Filter_Exception('Error while extracting the Tar archive');
0231         }
0232 
0233         return true;
0234     }
0235 
0236     /**
0237      * Returns the adapter name
0238      *
0239      * @return string
0240      */
0241     public function toString()
0242     {
0243         return 'Tar';
0244     }
0245 }