/vendor/zendframework/zend-filter/src/Compress/Rar.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 230 lines · 110 code · 27 blank · 93 comment · 11 complexity · c6756b4c0e4a2e940ad9ffebeb68b1b4 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Filter\Compress;
  10. use Zend\Filter\Exception;
  11. /**
  12. * Compression adapter for Rar
  13. */
  14. class Rar extends AbstractCompressionAlgorithm
  15. {
  16. /**
  17. * Compression Options
  18. * array(
  19. * 'callback' => Callback for compression
  20. * 'archive' => Archive to use
  21. * 'password' => Password to use
  22. * 'target' => Target to write the files to
  23. * )
  24. *
  25. * @var array
  26. */
  27. protected $options = array(
  28. 'callback' => null,
  29. 'archive' => null,
  30. 'password' => null,
  31. 'target' => '.',
  32. );
  33. /**
  34. * Class constructor
  35. *
  36. * @param array $options (Optional) Options to set
  37. * @throws Exception\ExtensionNotLoadedException if rar extension not loaded
  38. */
  39. public function __construct($options = null)
  40. {
  41. if (!extension_loaded('rar')) {
  42. throw new Exception\ExtensionNotLoadedException('This filter needs the rar extension');
  43. }
  44. parent::__construct($options);
  45. }
  46. /**
  47. * Returns the set callback for compression
  48. *
  49. * @return string
  50. */
  51. public function getCallback()
  52. {
  53. return $this->options['callback'];
  54. }
  55. /**
  56. * Sets the callback to use
  57. *
  58. * @param string $callback
  59. * @return self
  60. * @throws Exception\InvalidArgumentException if invalid callback provided
  61. */
  62. public function setCallback($callback)
  63. {
  64. if (!is_callable($callback)) {
  65. throw new Exception\InvalidArgumentException('Invalid callback provided');
  66. }
  67. $this->options['callback'] = $callback;
  68. return $this;
  69. }
  70. /**
  71. * Returns the set archive
  72. *
  73. * @return string
  74. */
  75. public function getArchive()
  76. {
  77. return $this->options['archive'];
  78. }
  79. /**
  80. * Sets the archive to use for de-/compression
  81. *
  82. * @param string $archive Archive to use
  83. * @return self
  84. */
  85. public function setArchive($archive)
  86. {
  87. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
  88. $this->options['archive'] = (string) $archive;
  89. return $this;
  90. }
  91. /**
  92. * Returns the set password
  93. *
  94. * @return string
  95. */
  96. public function getPassword()
  97. {
  98. return $this->options['password'];
  99. }
  100. /**
  101. * Sets the password to use
  102. *
  103. * @param string $password
  104. * @return self
  105. */
  106. public function setPassword($password)
  107. {
  108. $this->options['password'] = (string) $password;
  109. return $this;
  110. }
  111. /**
  112. * Returns the set targetpath
  113. *
  114. * @return string
  115. */
  116. public function getTarget()
  117. {
  118. return $this->options['target'];
  119. }
  120. /**
  121. * Sets the targetpath to use
  122. *
  123. * @param string $target
  124. * @return self
  125. * @throws Exception\InvalidArgumentException if specified target directory does not exist
  126. */
  127. public function setTarget($target)
  128. {
  129. if (!file_exists(dirname($target))) {
  130. throw new Exception\InvalidArgumentException("The directory '$target' does not exist");
  131. }
  132. $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, (string) $target);
  133. $this->options['target'] = $target;
  134. return $this;
  135. }
  136. /**
  137. * Compresses the given content
  138. *
  139. * @param string|array $content
  140. * @return string
  141. * @throws Exception\RuntimeException if no callback available, or error during compression
  142. */
  143. public function compress($content)
  144. {
  145. $callback = $this->getCallback();
  146. if ($callback === null) {
  147. throw new Exception\RuntimeException('No compression callback available');
  148. }
  149. $options = $this->getOptions();
  150. unset($options['callback']);
  151. $result = call_user_func($callback, $options, $content);
  152. if ($result !== true) {
  153. throw new Exception\RuntimeException('Error compressing the RAR Archive');
  154. }
  155. return $this->getArchive();
  156. }
  157. /**
  158. * Decompresses the given content
  159. *
  160. * @param string $content
  161. * @return bool
  162. * @throws Exception\RuntimeException if archive not found, cannot be opened,
  163. * or error during decompression
  164. */
  165. public function decompress($content)
  166. {
  167. if (!file_exists($content)) {
  168. throw new Exception\RuntimeException('RAR Archive not found');
  169. }
  170. $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
  171. $password = $this->getPassword();
  172. if ($password !== null) {
  173. $archive = rar_open($archive, $password);
  174. } else {
  175. $archive = rar_open($archive);
  176. }
  177. if (!$archive) {
  178. throw new Exception\RuntimeException("Error opening the RAR Archive");
  179. }
  180. $target = $this->getTarget();
  181. if (!is_dir($target)) {
  182. $target = dirname($target);
  183. }
  184. $filelist = rar_list($archive);
  185. if (!$filelist) {
  186. throw new Exception\RuntimeException("Error reading the RAR Archive");
  187. }
  188. foreach ($filelist as $file) {
  189. $file->extract($target);
  190. }
  191. rar_close($archive);
  192. return true;
  193. }
  194. /**
  195. * Returns the adapter name
  196. *
  197. * @return string
  198. */
  199. public function toString()
  200. {
  201. return 'Rar';
  202. }
  203. }