| @@ -1,0 +1,96 @@ | ||
| 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 | + * Archives helper class. | |
| 16 | + * | |
| 17 | + * @since 1.5 | |
| 18 | + */ | |
| 19 | +abstract class VBOArchiveFactory | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Extracts the file from a package. | |
| 23 | + * | |
| 24 | + * @param string $source The path of the package. | |
| 25 | + * @param string $destination The folder in which the files should be extracted. | |
| 26 | + * | |
| 27 | + * @return bool True on success, false otherwise. | |
| 28 | + * | |
| 29 | + * @throws Exception | |
| 30 | + */ | |
| 31 | + public static function extract($source, $destination) | |
| 32 | + { | |
| 33 | + return static::getInstance($source)->extract($source, $destination); | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Compresses the specified folder into an archive. | |
| 38 | + * | |
| 39 | + * @param string $source The path of the folder to compress. | |
| 40 | + * @param string $destination The path of the resulting archive. | |
| 41 | + * | |
| 42 | + * @return bool True on success, false otherwise. | |
| 43 | + * | |
| 44 | + * @throws Exception | |
| 45 | + */ | |
| 46 | + public static function compress($source, $destination) | |
| 47 | + { | |
| 48 | + return static::getInstance($destination)->compress($source, $destination); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Downloads the specified archive. | |
| 53 | + * | |
| 54 | + * @param string $source The path of the folder to compress. | |
| 55 | + * | |
| 56 | + * @return void | |
| 57 | + * | |
| 58 | + * @throws Exception | |
| 59 | + */ | |
| 60 | + public static function download($source) | |
| 61 | + { | |
| 62 | + static::getInstance($source)->download($source); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Returns a new instance of the specified archive handler. | |
| 67 | + * | |
| 68 | + * @param string $type Either the archive type or a path, from which | |
| 69 | + * the file type will be extracted. | |
| 70 | + * | |
| 71 | + * @return VBOArchiveType | |
| 72 | + * | |
| 73 | + * @throws Exception | |
| 74 | + */ | |
| 75 | + protected static function getInstance($type) | |
| 76 | + { | |
| 77 | + // check if we have a path | |
| 78 | + if (preg_match("/[\/\\\\]/", $type)) | |
| 79 | + { | |
| 80 | + // extract file type from path | |
| 81 | + $type = pathinfo($type, PATHINFO_EXTENSION); | |
| 82 | + } | |
| 83 | + | |
| 84 | + switch (strtolower($type)) | |
| 85 | + { | |
| 86 | + case 'zip': | |
| 87 | + | |
| 88 | + // create a new ZIP archive instance | |
| 89 | + return new VBOArchiveTypeZip; | |
| 90 | + | |
| 91 | + default: | |
| 92 | + // invalid archive type | |
| 93 | + throw new Exception(sprintf('Archive [%s] not supported', $type), 500); | |
| 94 | + } | |
| 95 | + } | |
| 96 | +} | |