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 Rar 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_Rar extends Zend_Filter_Compress_CompressAbstract 0036 { 0037 /** 0038 * Compression Options 0039 * array( 0040 * 'callback' => Callback for compression 0041 * 'archive' => Archive to use 0042 * 'password' => Password to use 0043 * 'target' => Target to write the files to 0044 * ) 0045 * 0046 * @var array 0047 */ 0048 protected $_options = array( 0049 'callback' => null, 0050 'archive' => null, 0051 'password' => null, 0052 'target' => '.', 0053 ); 0054 0055 /** 0056 * Class constructor 0057 * 0058 * @param array $options (Optional) Options to set 0059 */ 0060 public function __construct($options = null) 0061 { 0062 if (!extension_loaded('rar')) { 0063 // require_once 'Zend/Filter/Exception.php'; 0064 throw new Zend_Filter_Exception('This filter needs the rar extension'); 0065 } 0066 parent::__construct($options); 0067 } 0068 0069 /** 0070 * Returns the set callback for compression 0071 * 0072 * @return string 0073 */ 0074 public function getCallback() 0075 { 0076 return $this->_options['callback']; 0077 } 0078 0079 /** 0080 * Sets the callback to use 0081 * 0082 * @param string $callback 0083 * @return Zend_Filter_Compress_Rar 0084 */ 0085 public function setCallback($callback) 0086 { 0087 if (!is_callable($callback)) { 0088 // require_once 'Zend/Filter/Exception.php'; 0089 throw new Zend_Filter_Exception('Callback can not be accessed'); 0090 } 0091 0092 $this->_options['callback'] = $callback; 0093 return $this; 0094 } 0095 0096 /** 0097 * Returns the set archive 0098 * 0099 * @return string 0100 */ 0101 public function getArchive() 0102 { 0103 return $this->_options['archive']; 0104 } 0105 0106 /** 0107 * Sets the archive to use for de-/compression 0108 * 0109 * @param string $archive Archive to use 0110 * @return Zend_Filter_Compress_Rar 0111 */ 0112 public function setArchive($archive) 0113 { 0114 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive); 0115 $this->_options['archive'] = (string) $archive; 0116 0117 return $this; 0118 } 0119 0120 /** 0121 * Returns the set password 0122 * 0123 * @return string 0124 */ 0125 public function getPassword() 0126 { 0127 return $this->_options['password']; 0128 } 0129 0130 /** 0131 * Sets the password to use 0132 * 0133 * @param string $password 0134 * @return Zend_Filter_Compress_Rar 0135 */ 0136 public function setPassword($password) 0137 { 0138 $this->_options['password'] = (string) $password; 0139 return $this; 0140 } 0141 0142 /** 0143 * Returns the set targetpath 0144 * 0145 * @return string 0146 */ 0147 public function getTarget() 0148 { 0149 return $this->_options['target']; 0150 } 0151 0152 /** 0153 * Sets the targetpath to use 0154 * 0155 * @param string $target 0156 * @return Zend_Filter_Compress_Rar 0157 */ 0158 public function setTarget($target) 0159 { 0160 if (!file_exists(dirname($target))) { 0161 // require_once 'Zend/Filter/Exception.php'; 0162 throw new Zend_Filter_Exception("The directory '$target' does not exist"); 0163 } 0164 0165 $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target); 0166 $this->_options['target'] = (string) $target; 0167 return $this; 0168 } 0169 0170 /** 0171 * Compresses the given content 0172 * 0173 * @param string|array $content 0174 * @return string 0175 */ 0176 public function compress($content) 0177 { 0178 $callback = $this->getCallback(); 0179 if ($callback === null) { 0180 // require_once 'Zend/Filter/Exception.php'; 0181 throw new Zend_Filter_Exception('No compression callback available'); 0182 } 0183 0184 $options = $this->getOptions(); 0185 unset($options['callback']); 0186 0187 $result = call_user_func($callback, $options, $content); 0188 if ($result !== true) { 0189 // require_once 'Zend/Filter/Exception.php'; 0190 throw new Zend_Filter_Exception('Error compressing the RAR Archive'); 0191 } 0192 0193 return $this->getArchive(); 0194 } 0195 0196 /** 0197 * Decompresses the given content 0198 * 0199 * @param string $content 0200 * @return boolean 0201 */ 0202 public function decompress($content) 0203 { 0204 $archive = $this->getArchive(); 0205 if (file_exists($content)) { 0206 $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); 0207 } elseif (empty($archive) || !file_exists($archive)) { 0208 // require_once 'Zend/Filter/Exception.php'; 0209 throw new Zend_Filter_Exception('RAR Archive not found'); 0210 } 0211 0212 $password = $this->getPassword(); 0213 if ($password !== null) { 0214 $archive = rar_open($archive, $password); 0215 } else { 0216 $archive = rar_open($archive); 0217 } 0218 0219 if (!$archive) { 0220 // require_once 'Zend/Filter/Exception.php'; 0221 throw new Zend_Filter_Exception("Error opening the RAR Archive"); 0222 } 0223 0224 $target = $this->getTarget(); 0225 if (!is_dir($target)) { 0226 $target = dirname($target); 0227 } 0228 0229 $filelist = rar_list($archive); 0230 if (!$filelist) { 0231 // require_once 'Zend/Filter/Exception.php'; 0232 throw new Zend_Filter_Exception("Error reading the RAR Archive"); 0233 } 0234 0235 foreach($filelist as $file) { 0236 $file->extract($target); 0237 } 0238 0239 rar_close($archive); 0240 return true; 0241 } 0242 0243 /** 0244 * Returns the adapter name 0245 * 0246 * @return string 0247 */ 0248 public function toString() 0249 { 0250 return 'Rar'; 0251 } 0252 }