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