| 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 |
* FOLDER Backup export rule. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
class VBOBackupExportRuleFolder extends VBOBackupExportRule |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The target used by the system to restore the files in their position. |
| 23 |
* It is possible to pass a PHP constant so that the backup receiver will |
| 24 |
* be able to fetch the path at runtime. |
| 25 |
* |
| 26 |
* It is possible to receive an array in order to support both constants |
| 27 |
* and relative paths. In example, the first element of the array could be |
| 28 |
* a constant and the second one the remaining path of the destination. |
| 29 |
* |
| 30 |
* @var string|array |
| 31 |
*/ |
| 32 |
protected $destination; |
| 33 |
|
| 34 |
/** |
| 35 |
* The base (relative) path of the files. This is useful in case of |
| 36 |
* recursive elements, because we need to strip the relative path |
| 37 |
* used within the archive. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $relativePath; |
| 42 |
|
| 43 |
/** |
| 44 |
* An array containing the relative path of all the copied files. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
protected $files; |
| 49 |
|
| 50 |
/** |
| 51 |
* Flag used to check whether during the import the path of the files to copy |
| 52 |
* should be preserved or whether only the name should be used. |
| 53 |
* |
| 54 |
* @var boolean |
| 55 |
*/ |
| 56 |
protected $recursive; |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the rules instructions. |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function getData() |
| 64 |
{ |
| 65 |
// make sure there's at least a file to copy |
| 66 |
if ($this->files) |
| 67 |
{ |
| 68 |
return [ |
| 69 |
'destination' => $this->destination, |
| 70 |
'relativePath' => $this->relativePath, |
| 71 |
'recursive' => $this->recursive, |
| 72 |
'files' => $this->files, |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
// do not import empty files |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Configures the rule to work according to the specified data. |
| 82 |
* |
| 83 |
* @param array $data An associative array holding the instructions |
| 84 |
* for the folder to copy. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
protected function setup($data) |
| 89 |
{ |
| 90 |
// make sure the source path is specified |
| 91 |
if (empty($data['source'])) |
| 92 |
{ |
| 93 |
throw new Exception('Missing folder source', 500); |
| 94 |
} |
| 95 |
|
| 96 |
// make sure the destrination path is specified |
| 97 |
if (empty($data['destination'])) |
| 98 |
{ |
| 99 |
throw new Exception('Missing folder destination', 500); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
// remove trailing directory separator |
| 104 |
$data['destination'] = preg_replace("/[\/\\\\]+$/", '', $data['destination']); |
| 105 |
} |
| 106 |
|
| 107 |
// register relative path |
| 108 |
$this->relativePath = $data['destination']; |
| 109 |
|
| 110 |
if (empty($data['target'])) |
| 111 |
{ |
| 112 |
// final target equals to source path |
| 113 |
$data['target'] = $data['source']; |
| 114 |
} |
| 115 |
|
| 116 |
// check if we should preserve the path defined by the rule during the import |
| 117 |
$this->recursive = isset($data['recursive']) ? (bool) $data['recursive'] : false; |
| 118 |
|
| 119 |
// set destination path to use while restoring the backup |
| 120 |
$this->destination = $data['target']; |
| 121 |
|
| 122 |
if (JFile::exists($data['source'])) |
| 123 |
{ |
| 124 |
// file given, use it directly |
| 125 |
$files = $data['source']; |
| 126 |
|
| 127 |
// go up by one level to properly copy the file |
| 128 |
$data['source'] = dirname($data['source']); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
// ignore the following files while copying |
| 133 |
$exclude = ['.svn', 'CVS', '.DS_Store', '__MACOSX', 'index.html']; |
| 134 |
|
| 135 |
// scan the given folder |
| 136 |
$files = JFolder::files($data['source'], '.', $this->recursive, $fullPath = true, $exclude); |
| 137 |
|
| 138 |
if ($files === false) |
| 139 |
{ |
| 140 |
throw new Exception(sprintf('Reading source failed: %s', $data['source']), 500); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// iterate all files stored within the given folder |
| 145 |
foreach ((array) $files as $file) |
| 146 |
{ |
| 147 |
// get rid of base path from file |
| 148 |
$rel = str_replace($data['source'], '', $file); |
| 149 |
$rel = preg_replace("/^[\/\\\\]+/", '', $rel); |
| 150 |
|
| 151 |
// build relative destination path |
| 152 |
$dest = $data['destination'] . DIRECTORY_SEPARATOR . $rel; |
| 153 |
|
| 154 |
// append file to archive |
| 155 |
if (!$this->archive->addFile($file, $dest)) |
| 156 |
{ |
| 157 |
throw new Exception(sprintf('Cannot copy file: %s', $file), 500); |
| 158 |
} |
| 159 |
|
| 160 |
// register copied file |
| 161 |
$this->files[] = $dest; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|