PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
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 1.7.3 All 36 releases
vikbooking / admin / helpers / src / backup / manager.php

manager.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/backup/manager.php

311 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Backups director class.
16 *
17 * @since 1.5
18 */
19 class VBOBackupManager
20 {
21 /**
22 * Indicates the minimum required version while creating a
23 * new backup on Joomla. This value should be changed everytime
24 * something in the database structure is altered.
25 *
26 * @var string
27 *
28 * @see This should MATCH the E4jConnect constants for the Trial.
29 */
30 const MINIMUM_REQUIRED_VERSION_JOOMLA = '1.18.6';
31
32 /**
33 * Indicates the minimum required version while creating a
34 * new backup on WordPress. This value should be changed everytime
35 * something in the database structure is altered.
36 *
37 * @var string
38 *
39 * @see This should MATCH the E4jConnect constants for the Trial.
40 */
41 const MINIMUM_REQUIRED_VERSION_WORDPRESS = '1.8.6';
42
43 /**
44 * An associative array containing the supported export types, where the
45 * key is equals to the type ID and the value is the type instance.
46 *
47 * @var array
48 */
49 protected static $exportTypes = null;
50
51 /**
52 * Creates a new backup.
53 *
54 * @param string $type The type of backup to execute.
55 * @param array $options A configuration array.
56 * - folder string The path in which the archive should be saved.
57 * if not specified, the system temporary path will be used.
58 * - filename string An optional filename to use for the archive. If not specified
59 * the filename will be equals to the current time.
60 * - prefix string An optional prefix to prepend to the filename.
61 *
62 * @return string The path of the backup (a ZIP archive).
63 *
64 * @throws Exception
65 */
66 public static function create($type, array $options = [])
67 {
68 // ignore the maximum execution time
69 set_time_limit(0);
70
71 $app = JFactory::getApplication();
72
73 if (empty($options['folder']))
74 {
75 // before starting the export, make sure the temporary folder is supported
76 $options['folder'] = $app->get('tmp_path');
77
78 if (!$options['folder'] || !JFolder::exists($options['folder']))
79 {
80 throw new Exception('The temporary folder seems to be not set', 500);
81 }
82
83 // remove trailing directory separator
84 $options['folder'] = preg_replace("/[\/\\\\]$/", '', $options['folder']);
85 }
86
87 if (empty($options['filename']))
88 {
89 // use the current date and time as file name
90 $options['filename'] = 'backup_' . $type . '_' . JFactory::getDate()->format('Y-m-d H-i-s');
91 }
92
93 if (!empty($options['prefix']))
94 {
95 // include a prefix before the file name
96 $options['filename'] = $options['prefix'] . $options['filename'];
97 }
98
99 // build archive path
100 $path = $options['folder'] . DIRECTORY_SEPARATOR . $options['filename'];
101
102 // create backup export director
103 $director = new VBOBackupExportDirector($path);
104
105 // set the manifest version equals to the minimum required one
106 $director->setVersion(static::MINIMUM_REQUIRED_VERSION_JOOMLA, 'joomla');
107 $director->setVersion(static::MINIMUM_REQUIRED_VERSION_WORDPRESS, 'wordpress');
108
109 // fetch all the supported export types
110 $exportTypes = static::getExportTypes();
111
112 // check whether the requested support type exists
113 if (!isset($exportTypes[$type]))
114 {
115 // type not found
116 throw new Exception(sprintf('Cannot import [%s] export type', $type), 404);
117 }
118
119 // get export type instance
120 $handler = $exportTypes[$type];
121
122 $error = null;
123
124 try
125 {
126 // build the installers manifest
127 $handler->build($director);
128
129 /**
130 * Trigger event to allow third party plugins to extend the backup feature.
131 * This hook is useful to include third-party tables and files into the
132 * backup archive.
133 *
134 * It is possible to attach a database table into the backup by using:
135 * $director->attachRule('sqlfile', '#__extensions');
136 *
137 * @param string $type The type of backup to execute.
138 * @param VBOBackupExportDirector $director The instance used to manage the backup.
139 * @param array $options An array of options.
140 *
141 * @return void
142 *
143 * @since 1.5
144 */
145 $app->triggerEvent('onBuildBackupVikBooking', [$type, $director, $options]);
146
147 // compress the archive and obtain the full path
148 $archivePath = $director->compress();
149 }
150 catch (Exception $e)
151 {
152 // catch any error
153 $error = $e;
154 }
155
156 // always delete archive folder
157 JFolder::delete($path);
158
159 if ($error)
160 {
161 // in case of error, propagate it only after cleaning the dump
162 throw $error;
163 }
164
165 return $archivePath;
166 }
167
168 /**
169 * Restores an existing backup.
170 *
171 * @param string $path The path of the backup to restore.
172 *
173 * @return void
174 *
175 * @throws Exception
176 */
177 public static function restore($path)
178 {
179 // ignore the maximum execution time
180 set_time_limit(0);
181
182 // make sure the archive exists
183 if (!JFile::exists($path))
184 {
185 // unable to find the specified archive
186 throw new Exception(sprintf('Backup [%s] not found', $path), 404);
187 }
188
189 // create a unique extraction folder
190 $extractdir = dirname($path) . DIRECTORY_SEPARATOR . uniqid();
191
192 // extract the given backup
193 $status = VBOArchiveFactory::extract($path, $extractdir);
194
195 if (!$status)
196 {
197 // cannot extract the archive (422 Unprocessable Content)
198 throw new Exception(sprintf('Unable to extract [%s] into [%s]', $path, $extractdir), 422);
199 }
200
201 // create backup import director
202 $director = new VBOBackupImportDirector($extractdir);
203
204 // set the manifest version equals to the minimum required one, according to the CMS in use
205 if (VBOPlatformDetection::isWordPress())
206 {
207 $director->setVersion(static::MINIMUM_REQUIRED_VERSION_WORDPRESS);
208 }
209 else
210 {
211 $director->setVersion(static::MINIMUM_REQUIRED_VERSION_JOOMLA);
212 }
213
214 $error = null;
215
216 try
217 {
218 // process the backup
219 $director->process();
220 }
221 catch (Exception $e)
222 {
223 $error = $e;
224 }
225
226 // always delete extracted folder
227 JFolder::delete($extractdir);
228
229 if ($error)
230 {
231 // in case of error, propagate it only after cleaning the dump
232 throw $error;
233 }
234 }
235
236 /**
237 * Returns a list of supported export types.
238 *
239 * @return array
240 */
241 public static function getExportTypes()
242 {
243 if (!is_null(static::$exportTypes))
244 {
245 // export types already fetched
246 return static::$exportTypes;
247 }
248
249 // register default include paths
250 $includePaths = [
251 dirname(__FILE__) . DIRECTORY_SEPARATOR . 'export' . DIRECTORY_SEPARATOR . 'type',
252 ];
253
254 /**
255 * Trigger event to allow third party plugins to register additional include paths,
256 * from which the system can load other backup handlers.
257 *
258 * @return mixed An array of include paths or a string.
259 *
260 * @since 1.5
261 */
262 $paths = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadBackupExportTypesVikBooking');
263
264 // merge returned paths with the existing ones
265 foreach ($paths as $path)
266 {
267 if (is_string($path))
268 {
269 $includePaths[] = $path;
270 }
271 else if (is_array($path))
272 {
273 $includePaths = array_merge($includePaths, $path);
274 }
275 }
276
277 static::$exportTypes = [];
278
279 // iterate include paths to fetch all the supported export types
280 foreach ($includePaths as $path)
281 {
282 // get all PHP files inside the folder
283 $files = JFolder::files($path, '\.php$', $recurse = false, $fullpath = true);
284
285 // iterate all PHP files
286 foreach ($files as $file)
287 {
288 // get file name without extension
289 $type = basename($file, '.php');
290
291 // load the file
292 require_once $file;
293
294 // build class name
295 $classname = 'VBOBackupExportType' . ucfirst($type);
296
297 // check whether the class exists
298 if (!class_exists($classname))
299 {
300 throw new Exception(sprintf('Cannot find [%s] export type class', $classname), 404);
301 }
302
303 // instantiate and register export type handler
304 static::$exportTypes[$type] = new $classname();
305 }
306 }
307
308 return static::$exportTypes;
309 }
310 }
311