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 / director.php
vikappointments / site / helpers / libraries / backup / export Last commit date
rule 1 month ago type 1 month ago archive.php 1 month ago director.php 1 month ago rule.php 1 month ago type.php 1 month ago
director.php
231 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.archive');
15 VAPLoader::import('libraries.backup.export.rule');
16
17 /**
18 * Wraps the instructions used to create a backup.
19 *
20 * @since 1.7.1
21 */
22 class VAPBackupExportDirector
23 {
24 /**
25 * An array of export rules.
26 *
27 * @var VAPBackupExportRule[]
28 */
29 private $rules = [];
30
31 /**
32 * The instance used to manage the archive.
33 *
34 * @var VAPBackupExportArchive
35 */
36 private $archive;
37
38 /**
39 * The version to use for the backup manifest.
40 *
41 * @var string
42 */
43 private $version;
44
45 /**
46 * A lookup used to register the compatible version for each CMS.
47 *
48 * @var array
49 */
50 private $platforms = [];
51
52 /**
53 * Class constructor.
54 *
55 * @param string $path The archive path.
56 */
57 public function __construct($path)
58 {
59 // init archive manager
60 $this->archive = new VAPBackupExportArchive($path);
61 }
62
63 /**
64 * Sets the version of the manifest.
65 *
66 * @param string $version
67 *
68 * @return self This object to support chaining.
69 */
70 public function setVersion($version, $platform = null)
71 {
72 if (is_null($platform))
73 {
74 // register generic version
75 $this->version = $version;
76 }
77 else
78 {
79 // register platform version
80 $this->platforms[$platform] = $version;
81 }
82
83 return $this;
84 }
85
86 /**
87 * Creates a new export rule.
88 *
89 * @param string $rule The identifier of the rule to create.
90 * @param mixed $data The instructions used for the rule setup.
91 *
92 * @return self This object to support chaining.
93 *
94 * @throws Exception
95 */
96 public function createRule($rule, $data)
97 {
98 // attempt to load the export rule
99 if (!VAPLoader::import('libraries.backup.export.rule.' . $rule))
100 {
101 // rule not found
102 throw new Exception(sprintf('Cannot import [%s] export rule', $rule), 404);
103 }
104
105 // build class name
106 $classname = preg_replace("/_/", ' ', $rule);
107 $classname = preg_replace("/\s+/", '', ucwords($classname));
108 $classname = 'VAPBackupExportRule' . $classname;
109
110 // make sure the rule class exists
111 if (!class_exists($classname))
112 {
113 // class not found
114 throw new Exception(sprintf('Cannot find [%s] export rule class', $classname), 404);
115 }
116
117 // attach the rule
118 return $this->attachRule(new $classname($this->archive, $data));
119 }
120
121 /**
122 * Attaches the specified rule as export instruction.
123 *
124 * @param VAPBackupExportRule $rule The rule to attach.
125 *
126 * @return self This object to support chaining.
127 */
128 public function attachRule(VAPBackupExportRule $rule)
129 {
130 // register rule only if there is some data to export
131 if ($rule->getData())
132 {
133 $this->rules[] = $rule;
134 }
135
136 return $this;
137 }
138
139 /**
140 * Returns an array of registered installer rules.
141 *
142 * @return VAPBackupExportRule[]
143 */
144 public function getRules()
145 {
146 return $this->rules;
147 }
148
149 /**
150 * Compresses the archive.
151 *
152 * @return string The archive path.
153 */
154 public function compress()
155 {
156 // create manifest
157 $manifest = $this->createManifest();
158
159 if (defined('JSON_PRETTY_PRINT'))
160 {
161 $flag = JSON_PRETTY_PRINT;
162 }
163 else
164 {
165 $flag = 0;
166 }
167
168 // try to encode the manifest in JSON format
169 $json = json_encode($manifest, $flag);
170
171 if ($json === false)
172 {
173 // an error has occurred while trying to encode the manifest file in JSON format
174 throw new UnexpectedValueException('Failed to encode the manifest file. Error: ' . json_last_error() . '.', 500);
175 }
176
177 // add manifest file into the root of the archive
178 $this->archive->addBuffer($json, 'manifest.json');
179
180 // complete the backup process by creating the archive
181 return $this->archive->compress();
182 }
183
184 /**
185 * Creates the backup manifest.
186 *
187 * @return object The backup manifest to be encoded in JSON format.
188 */
189 protected function createManifest()
190 {
191 // before to compress the archive, we need to create the installation manifest
192 $manifest = new stdClass;
193 $manifest->title = basename($this->archive->getPath());
194 $manifest->version = $this->version ?: VIKAPPOINTMENTS_SOFTWARE_VERSION;
195 $manifest->application = 'Vik Appointments';
196 $manifest->signature = md5($manifest->application . ' ' . $manifest->version);
197 $manifest->langtag = '*';
198 $manifest->dateCreated = JFactory::getDate()->toSql();
199 $manifest->installers = $this->getRules();
200
201 if ($this->platforms)
202 {
203 $manifest->platforms = new stdClass;
204
205 foreach ($this->platforms as $id => $version)
206 {
207 $manifest->platforms->{$id} = new stdClass;
208 $manifest->platforms->{$id}->version = $version;
209 $manifest->platforms->{$id}->signature = md5($manifest->application . ' ' . $id . ' ' . $version);
210 }
211 }
212
213 $dispatcher = VAPFactory::getEventDispatcher();
214
215 /**
216 * Trigger event to allow third party plugins to manipulate the backup manifest.
217 * Fires just before performing the compression of the archive.
218 *
219 * @param object $manifest The backup manifest.
220 * @param VAPBackupExportArchive $archive The instance used to manage the archive.
221 *
222 * @return void
223 *
224 * @since 1.7.1
225 */
226 $dispatcher->trigger('onCreateBackupManifestVikAppointments', [$manifest, $this->archive]);
227
228 return $manifest;
229 }
230 }
231