| @@ -1,0 +1,99 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage core | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Defines an abstract adapter able to extract the archives by using the | |
| 16 | + * native tools provided by Joomla! CMS. | |
| 17 | + * | |
| 18 | + * @since 1.5 | |
| 19 | + */ | |
| 20 | +abstract class VBOArchiveAdapter implements VBOArchiveType | |
| 21 | +{ | |
| 22 | + /** | |
| 23 | + * Extracts the file from a package. | |
| 24 | + * | |
| 25 | + * @param string $source The path of the package. | |
| 26 | + * @param string $destination The folder in which the files should be extracted. | |
| 27 | + * | |
| 28 | + * @return bool True on success, false otherwise. | |
| 29 | + */ | |
| 30 | + public function extract($source, $destination) | |
| 31 | + { | |
| 32 | + if (!class_exists('JArchive')) | |
| 33 | + { | |
| 34 | + // get temporary path | |
| 35 | + $tmp_path = JFactory::getApplication()->get('tmp_path'); | |
| 36 | + | |
| 37 | + // instantiate archive class | |
| 38 | + $archive = new Joomla\Archive\Archive(array('tmp_path' => $tmp_path)); | |
| 39 | + | |
| 40 | + // extract the archive | |
| 41 | + return $archive->extract($source, $destination); | |
| 42 | + } | |
| 43 | + | |
| 44 | + // backward compatibility | |
| 45 | + return JArchive::extract($source, $destination); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Downloads the specified archive. | |
| 50 | + * | |
| 51 | + * @param string $source The path of the folder to compress. | |
| 52 | + * | |
| 53 | + * @return void | |
| 54 | + * | |
| 55 | + * @throws Exception | |
| 56 | + */ | |
| 57 | + public function download($source) | |
| 58 | + { | |
| 59 | + if (!JFile::exists($source)) | |
| 60 | + { | |
| 61 | + // the selected archive doesn't exist | |
| 62 | + throw new Exception(sprintf('Archive [%s] not found', $source), 404); | |
| 63 | + } | |
| 64 | + | |
| 65 | + $app = JFactory::getApplication(); | |
| 66 | + | |
| 67 | + // prepare headers | |
| 68 | + $app->setHeader('Content-Disposition', 'attachment; filename=' . basename($source)); | |
| 69 | + $app->setHeader('Content-Length', filesize($source)); | |
| 70 | + | |
| 71 | + // send headers | |
| 72 | + $app->sendHeaders(); | |
| 73 | + | |
| 74 | + // use fopen to properly download large files | |
| 75 | + $handle = fopen($source, 'rb'); | |
| 76 | + | |
| 77 | + // read 1MB per cycle | |
| 78 | + $chunk_size = 1024 * 1024; | |
| 79 | + | |
| 80 | + while (!feof($handle)) | |
| 81 | + { | |
| 82 | + echo fread($handle, $chunk_size); | |
| 83 | + ob_flush(); | |
| 84 | + flush(); | |
| 85 | + } | |
| 86 | + | |
| 87 | + fclose($handle); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Compresses the specified folder into an archive. | |
| 92 | + * | |
| 93 | + * @param string $source The path of the folder to compress. | |
| 94 | + * @param string $destination The path of the resulting archive. | |
| 95 | + * | |
| 96 | + * @return bool True on success, false otherwise. | |
| 97 | + */ | |
| 98 | + abstract public function compress($source, $destination); | |
| 99 | +} | |