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