| @@ -1,0 +1,121 @@ | ||
| 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 | + * Backup Folder import rule. | |
| 16 | + * | |
| 17 | + * @since 1.5 | |
| 18 | + */ | |
| 19 | +class VBOBackupImportRuleFolder extends VBOBackupImportRule | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Executes the backup import command. | |
| 23 | + * | |
| 24 | + * @param mixed $data The import rule instructions. | |
| 25 | + * | |
| 26 | + * @return void | |
| 27 | + */ | |
| 28 | + public function execute($data) | |
| 29 | + { | |
| 30 | + if (empty($data->destination)) | |
| 31 | + { | |
| 32 | + // destination path is missing | |
| 33 | + throw new Exception('Invalid Folder import rule, missing destination path', 404); | |
| 34 | + } | |
| 35 | + | |
| 36 | + if (!isset($data->files)) | |
| 37 | + { | |
| 38 | + // source files are missing | |
| 39 | + throw new Exception('Invalid Folder import rule, missing source files', 404); | |
| 40 | + } | |
| 41 | + | |
| 42 | + // support the array notation for the destination | |
| 43 | + if (is_array($data->destination)) | |
| 44 | + { | |
| 45 | + // removed first element from destination | |
| 46 | + $fixed = $data->destination; | |
| 47 | + $data->destination = array_shift($fixed); | |
| 48 | + } | |
| 49 | + else | |
| 50 | + { | |
| 51 | + $fixed = null; | |
| 52 | + } | |
| 53 | + | |
| 54 | + // check if we have a constant | |
| 55 | + if (defined($data->destination)) | |
| 56 | + { | |
| 57 | + // use the path defined by the plugin | |
| 58 | + $destination = constant($data->destination); | |
| 59 | + } | |
| 60 | + else | |
| 61 | + { | |
| 62 | + // use a path relative to the system (according to the platform in use) | |
| 63 | + $destination = JPath::clean((VBOPlatformDetection::isWordPress() ? ABSPATH : JPATH_SITE) . '/' . $data->destination); | |
| 64 | + } | |
| 65 | + | |
| 66 | + if ($fixed) | |
| 67 | + { | |
| 68 | + // re-append fixed path to destination | |
| 69 | + $destination = JPath::clean($destination . '/' . implode('/', $fixed)); | |
| 70 | + } | |
| 71 | + | |
| 72 | + // iterate all source files | |
| 73 | + foreach ((array) $data->files as $file) | |
| 74 | + { | |
| 75 | + // build source path | |
| 76 | + $src = JPath::clean($this->path . '/' . $file); | |
| 77 | + | |
| 78 | + // make sure the source file exists | |
| 79 | + if (!JFile::exists($src)) | |
| 80 | + { | |
| 81 | + // source file is missing | |
| 82 | + throw new Exception(sprintf('File to copy [%s] not found', $src), 404); | |
| 83 | + } | |
| 84 | + | |
| 85 | + if (!empty($data->full)) | |
| 86 | + { | |
| 87 | + // use the full relative path | |
| 88 | + $rel = $file; | |
| 89 | + } | |
| 90 | + else if (!empty($data->recursive) && !empty($data->relativePath)) | |
| 91 | + { | |
| 92 | + // get rid of the relative path | |
| 93 | + $rel = substr($file, strlen($data->relativePath . '/')); | |
| 94 | + } | |
| 95 | + else | |
| 96 | + { | |
| 97 | + // use only the file name | |
| 98 | + $rel = basename($file); | |
| 99 | + } | |
| 100 | + | |
| 101 | + // build full destination path | |
| 102 | + $fd = JPath::clean($destination . '/' . $rel); | |
| 103 | + | |
| 104 | + // get path of parent folder | |
| 105 | + $parentDir = dirname($fd); | |
| 106 | + | |
| 107 | + // make sure the destination folder exists, otherwise create it first | |
| 108 | + if (!JFolder::exists($parentDir)) | |
| 109 | + { | |
| 110 | + JFolder::create($parentDir); | |
| 111 | + } | |
| 112 | + | |
| 113 | + // try to copy the file | |
| 114 | + if (!JFile::copy($src, $fd)) | |
| 115 | + { | |
| 116 | + // unable to perform file copy | |
| 117 | + throw new Exception(sprintf('Unable to copy [%s] into [%s]', $src, $fd), 500); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + } | |
| 121 | +} | |