diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:39:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:39:39 +0000 |
commit | 8ca6cc32b2c789a3149861159ad258f2cb9491e3 (patch) | |
tree | 2492de6f1528dd44eaa169a5c1555026d9cb75ec /library/vendor/Zend/Filter/Compress/Rar.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-upstream.tar.xz icingaweb2-upstream.zip |
Adding upstream version 2.11.4.upstream/2.11.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | library/vendor/Zend/Filter/Compress/Rar.php | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/library/vendor/Zend/Filter/Compress/Rar.php b/library/vendor/Zend/Filter/Compress/Rar.php new file mode 100644 index 0000000..7a76af7 --- /dev/null +++ b/library/vendor/Zend/Filter/Compress/Rar.php @@ -0,0 +1,243 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + * @version $Id$ + */ + +/** + * @see Zend_Filter_Compress_CompressAbstract + */ + +/** + * Compression adapter for Rar + * + * @category Zend + * @package Zend_Filter + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ +class Zend_Filter_Compress_Rar extends Zend_Filter_Compress_CompressAbstract +{ + /** + * Compression Options + * array( + * 'callback' => Callback for compression + * 'archive' => Archive to use + * 'password' => Password to use + * 'target' => Target to write the files to + * ) + * + * @var array + */ + protected $_options = array( + 'callback' => null, + 'archive' => null, + 'password' => null, + 'target' => '.', + ); + + /** + * Class constructor + * + * @param array $options (Optional) Options to set + */ + public function __construct($options = null) + { + if (!extension_loaded('rar')) { + throw new Zend_Filter_Exception('This filter needs the rar extension'); + } + parent::__construct($options); + } + + /** + * Returns the set callback for compression + * + * @return string + */ + public function getCallback() + { + return $this->_options['callback']; + } + + /** + * Sets the callback to use + * + * @param string $callback + * @return Zend_Filter_Compress_Rar + */ + public function setCallback($callback) + { + if (!is_callable($callback)) { + throw new Zend_Filter_Exception('Callback can not be accessed'); + } + + $this->_options['callback'] = $callback; + return $this; + } + + /** + * Returns the set archive + * + * @return string + */ + public function getArchive() + { + return $this->_options['archive']; + } + + /** + * Sets the archive to use for de-/compression + * + * @param string $archive Archive to use + * @return Zend_Filter_Compress_Rar + */ + public function setArchive($archive) + { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive); + $this->_options['archive'] = (string) $archive; + + return $this; + } + + /** + * Returns the set password + * + * @return string + */ + public function getPassword() + { + return $this->_options['password']; + } + + /** + * Sets the password to use + * + * @param string $password + * @return Zend_Filter_Compress_Rar + */ + public function setPassword($password) + { + $this->_options['password'] = (string) $password; + return $this; + } + + /** + * Returns the set targetpath + * + * @return string + */ + public function getTarget() + { + return $this->_options['target']; + } + + /** + * Sets the targetpath to use + * + * @param string $target + * @return Zend_Filter_Compress_Rar + */ + public function setTarget($target) + { + if (!file_exists(dirname($target))) { + throw new Zend_Filter_Exception("The directory '$target' does not exist"); + } + + $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target); + $this->_options['target'] = (string) $target; + return $this; + } + + /** + * Compresses the given content + * + * @param string|array $content + * @return string + */ + public function compress($content) + { + $callback = $this->getCallback(); + if ($callback === null) { + throw new Zend_Filter_Exception('No compression callback available'); + } + + $options = $this->getOptions(); + unset($options['callback']); + + $result = call_user_func($callback, $options, $content); + if ($result !== true) { + throw new Zend_Filter_Exception('Error compressing the RAR Archive'); + } + + return $this->getArchive(); + } + + /** + * Decompresses the given content + * + * @param string $content + * @return boolean + */ + public function decompress($content) + { + $archive = $this->getArchive(); + if (file_exists($content)) { + $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content)); + } elseif (empty($archive) || !file_exists($archive)) { + throw new Zend_Filter_Exception('RAR Archive not found'); + } + + $password = $this->getPassword(); + if ($password !== null) { + $archive = rar_open($archive, $password); + } else { + $archive = rar_open($archive); + } + + if (!$archive) { + throw new Zend_Filter_Exception("Error opening the RAR Archive"); + } + + $target = $this->getTarget(); + if (!is_dir($target)) { + $target = dirname($target); + } + + $filelist = rar_list($archive); + if (!$filelist) { + throw new Zend_Filter_Exception("Error reading the RAR Archive"); + } + + foreach($filelist as $file) { + $file->extract($target); + } + + rar_close($archive); + return true; + } + + /** + * Returns the adapter name + * + * @return string + */ + public function toString() + { + return 'Rar'; + } +} |