File indexing completed on 2025-01-19 05:21:07

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 Gzip (ZLib)
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_Gz extends Zend_Filter_Compress_CompressAbstract
0036 {
0037     /**
0038      * Compression Options
0039      * array(
0040      *     'level'    => Compression level 0-9
0041      *     'mode'     => Compression mode, can be 'compress', 'deflate'
0042      *     'archive'  => Archive to use
0043      * )
0044      *
0045      * @var array
0046      */
0047     protected $_options = array(
0048         'level'   => 9,
0049         'mode'    => 'compress',
0050         'archive' => null,
0051     );
0052 
0053     /**
0054      * Class constructor
0055      *
0056      * @param array|Zend_Config|null $options (Optional) Options to set
0057      */
0058     public function __construct($options = null)
0059     {
0060         if (!extension_loaded('zlib')) {
0061             // require_once 'Zend/Filter/Exception.php';
0062             throw new Zend_Filter_Exception('This filter needs the zlib extension');
0063         }
0064         parent::__construct($options);
0065     }
0066 
0067     /**
0068      * Returns the set compression level
0069      *
0070      * @return integer
0071      */
0072     public function getLevel()
0073     {
0074         return $this->_options['level'];
0075     }
0076 
0077     /**
0078      * Sets a new compression level
0079      *
0080      * @param integer $level
0081      * @return Zend_Filter_Compress_Gz
0082      */
0083     public function setLevel($level)
0084     {
0085         if (($level < 0) || ($level > 9)) {
0086             // require_once 'Zend/Filter/Exception.php';
0087             throw new Zend_Filter_Exception('Level must be between 0 and 9');
0088         }
0089 
0090         $this->_options['level'] = (int) $level;
0091         return $this;
0092     }
0093 
0094     /**
0095      * Returns the set compression mode
0096      *
0097      * @return string
0098      */
0099     public function getMode()
0100     {
0101         return $this->_options['mode'];
0102     }
0103 
0104     /**
0105      * Sets a new compression mode
0106      *
0107      * @param string $mode Supported are 'compress', 'deflate' and 'file'
0108      */
0109     public function setMode($mode)
0110     {
0111         if (($mode != 'compress') && ($mode != 'deflate')) {
0112             // require_once 'Zend/Filter/Exception.php';
0113             throw new Zend_Filter_Exception('Given compression mode not supported');
0114         }
0115 
0116         $this->_options['mode'] = $mode;
0117         return $this;
0118     }
0119 
0120     /**
0121      * Returns the set archive
0122      *
0123      * @return string
0124      */
0125     public function getArchive()
0126     {
0127         return $this->_options['archive'];
0128     }
0129 
0130     /**
0131      * Sets the archive to use for de-/compression
0132      *
0133      * @param string $archive Archive to use
0134      * @return Zend_Filter_Compress_Gz
0135      */
0136     public function setArchive($archive)
0137     {
0138         $this->_options['archive'] = (string) $archive;
0139         return $this;
0140     }
0141 
0142     /**
0143      * Compresses the given content
0144      *
0145      * @param  string $content
0146      * @return string
0147      */
0148     public function compress($content)
0149     {
0150         $archive = $this->getArchive();
0151         if (!empty($archive)) {
0152             $file = gzopen($archive, 'w' . $this->getLevel());
0153             if (!$file) {
0154                 // require_once 'Zend/Filter/Exception.php';
0155                 throw new Zend_Filter_Exception("Error opening the archive '" . $this->_options['archive'] . "'");
0156             }
0157 
0158             gzwrite($file, $content);
0159             gzclose($file);
0160             $compressed = true;
0161         } else if ($this->_options['mode'] == 'deflate') {
0162             $compressed = gzdeflate($content, $this->getLevel());
0163         } else {
0164             $compressed = gzcompress($content, $this->getLevel());
0165         }
0166 
0167         if (!$compressed) {
0168             // require_once 'Zend/Filter/Exception.php';
0169             throw new Zend_Filter_Exception('Error during compression');
0170         }
0171 
0172         return $compressed;
0173     }
0174 
0175     /**
0176      * Decompresses the given content
0177      *
0178      * @param  string $content
0179      * @return string
0180      */
0181     public function decompress($content)
0182     {
0183         $archive = $this->getArchive();
0184         $mode    = $this->getMode();
0185         if (@file_exists($content)) {
0186             $archive = $content;
0187         }
0188 
0189         if (@file_exists($archive)) {
0190             $handler = fopen($archive, "rb");
0191             if (!$handler) {
0192                 // require_once 'Zend/Filter/Exception.php';
0193                 throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
0194             }
0195 
0196             fseek($handler, -4, SEEK_END);
0197             $packet = fread($handler, 4);
0198             $bytes  = unpack("V", $packet);
0199             $size   = end($bytes);
0200             fclose($handler);
0201 
0202             $file       = gzopen($archive, 'r');
0203             $compressed = gzread($file, $size);
0204             gzclose($file);
0205         } else if ($mode == 'deflate') {
0206             $compressed = gzinflate($content);
0207         } else {
0208             $compressed = gzuncompress($content);
0209         }
0210 
0211         if (!$compressed) {
0212             // require_once 'Zend/Filter/Exception.php';
0213             throw new Zend_Filter_Exception('Error during compression');
0214         }
0215 
0216         return $compressed;
0217     }
0218 
0219     /**
0220      * Returns the adapter name
0221      *
0222      * @return string
0223      */
0224     public function toString()
0225     {
0226         return 'Gz';
0227     }
0228 }