adapter.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 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 | VAPLoader::import('libraries.archive.type'); |
| 15 | |
| 16 | /** |
| 17 | * Defines an abstract adapter able to extract the archives by using the |
| 18 | * native tools provided by Joomla! CMS. |
| 19 | * |
| 20 | * @since 1.7.1 |
| 21 | */ |
| 22 | abstract class VAPArchiveAdapter implements VAPArchiveType |
| 23 | { |
| 24 | /** |
| 25 | * Extracts the file from a package. |
| 26 | * |
| 27 | * @param string $source The path of the package. |
| 28 | * @param string $destination The folder in which the files should be extracted. |
| 29 | * |
| 30 | * @return bool True on success, false otherwise. |
| 31 | */ |
| 32 | public function extract($source, $destination) |
| 33 | { |
| 34 | if (!class_exists('JArchive')) |
| 35 | { |
| 36 | // get temporary path |
| 37 | $tmp_path = JFactory::getApplication()->get('tmp_path'); |
| 38 | |
| 39 | // instantiate archive class |
| 40 | $archive = new Joomla\Archive\Archive(array('tmp_path' => $tmp_path)); |
| 41 | |
| 42 | // extract the archive |
| 43 | return $archive->extract($source, $destination); |
| 44 | } |
| 45 | |
| 46 | // backward compatibility |
| 47 | return JArchive::extract($source, $destination); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Downloads the specified archive. |
| 52 | * |
| 53 | * @param string $source The path of the folder to compress. |
| 54 | * |
| 55 | * @return void |
| 56 | * |
| 57 | * @since 1.7.1 |
| 58 | * |
| 59 | * @throws Exception |
| 60 | */ |
| 61 | public function download($source) |
| 62 | { |
| 63 | if (!JFile::exists($source)) |
| 64 | { |
| 65 | // the selected archive doesn't exist |
| 66 | throw new Exception(sprintf('Archive [%s] not found', $source), 404); |
| 67 | } |
| 68 | |
| 69 | $app = JFactory::getApplication(); |
| 70 | |
| 71 | // prepare headers |
| 72 | $app->setHeader('Content-Disposition', 'attachment; filename=' . basename($source)); |
| 73 | $app->setHeader('Content-Length', filesize($source)); |
| 74 | |
| 75 | // send headers |
| 76 | $app->sendHeaders(); |
| 77 | |
| 78 | // use fopen to properly download large files |
| 79 | $handle = fopen($source, 'rb'); |
| 80 | |
| 81 | // read 1MB per cycle |
| 82 | $chunk_size = 1024 * 1024; |
| 83 | |
| 84 | while (!feof($handle)) |
| 85 | { |
| 86 | echo fread($handle, $chunk_size); |
| 87 | ob_flush(); |
| 88 | flush(); |
| 89 | } |
| 90 | |
| 91 | fclose($handle); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Compresses the specified folder into an archive. |
| 96 | * |
| 97 | * @param string $source The path of the folder to compress. |
| 98 | * @param string $destination The path of the resulting archive. |
| 99 | * |
| 100 | * @return bool True on success, false otherwise. |
| 101 | */ |
| 102 | abstract public function compress($source, $destination); |
| 103 | } |
| 104 |