Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rar.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Filter/Compress/CompressAbstract.php';
26 
36 {
48  protected $_options = array(
49  'callback' => null,
50  'archive' => null,
51  'password' => null,
52  'target' => '.',
53  );
54 
60  public function __construct($options = null)
61  {
62  if (!extension_loaded('rar')) {
63  #require_once 'Zend/Filter/Exception.php';
64  throw new Zend_Filter_Exception('This filter needs the rar extension');
65  }
66  parent::__construct($options);
67  }
68 
74  public function getCallback()
75  {
76  return $this->_options['callback'];
77  }
78 
85  public function setCallback($callback)
86  {
87  if (!is_callable($callback)) {
88  #require_once 'Zend/Filter/Exception.php';
89  throw new Zend_Filter_Exception('Callback can not be accessed');
90  }
91 
92  $this->_options['callback'] = $callback;
93  return $this;
94  }
95 
101  public function getArchive()
102  {
103  return $this->_options['archive'];
104  }
105 
112  public function setArchive($archive)
113  {
114  $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
115  $this->_options['archive'] = (string) $archive;
116 
117  return $this;
118  }
119 
125  public function getPassword()
126  {
127  return $this->_options['password'];
128  }
129 
136  public function setPassword($password)
137  {
138  $this->_options['password'] = (string) $password;
139  return $this;
140  }
141 
147  public function getTarget()
148  {
149  return $this->_options['target'];
150  }
151 
158  public function setTarget($target)
159  {
160  if (!file_exists(dirname($target))) {
161  #require_once 'Zend/Filter/Exception.php';
162  throw new Zend_Filter_Exception("The directory '$target' does not exist");
163  }
164 
165  $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
166  $this->_options['target'] = (string) $target;
167  return $this;
168  }
169 
176  public function compress($content)
177  {
178  $callback = $this->getCallback();
179  if ($callback === null) {
180  #require_once 'Zend/Filter/Exception.php';
181  throw new Zend_Filter_Exception('No compression callback available');
182  }
183 
184  $options = $this->getOptions();
185  unset($options['callback']);
186 
187  $result = call_user_func($callback, $options, $content);
188  if ($result !== true) {
189  #require_once 'Zend/Filter/Exception.php';
190  throw new Zend_Filter_Exception('Error compressing the RAR Archive');
191  }
192 
193  return $this->getArchive();
194  }
195 
202  public function decompress($content)
203  {
204  $archive = $this->getArchive();
205  if (file_exists($content)) {
206  $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
207  } elseif (empty($archive) || !file_exists($archive)) {
208  #require_once 'Zend/Filter/Exception.php';
209  throw new Zend_Filter_Exception('RAR Archive not found');
210  }
211 
212  $password = $this->getPassword();
213  if ($password !== null) {
214  $archive = rar_open($archive, $password);
215  } else {
216  $archive = rar_open($archive);
217  }
218 
219  if (!$archive) {
220  #require_once 'Zend/Filter/Exception.php';
221  throw new Zend_Filter_Exception("Error opening the RAR Archive");
222  }
223 
224  $target = $this->getTarget();
225  if (!is_dir($target)) {
226  $target = dirname($target);
227  }
228 
229  $filelist = rar_list($archive);
230  if (!$filelist) {
231  #require_once 'Zend/Filter/Exception.php';
232  throw new Zend_Filter_Exception("Error reading the RAR Archive");
233  }
234 
235  foreach($filelist as $file) {
236  $file->extract($target);
237  }
238 
239  rar_close($archive);
240  return true;
241  }
242 
248  public function toString()
249  {
250  return 'Rar';
251  }
252 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setPassword($password)
Definition: Rar.php:136
compress($content)
Definition: Rar.php:176
$target
Definition: skip.phtml:8
setCallback($callback)
Definition: Rar.php:85
decompress($content)
Definition: Rar.php:202
__construct($options=null)
Definition: Rar.php:60
setTarget($target)
Definition: Rar.php:158
setArchive($archive)
Definition: Rar.php:112