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