PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.8
VikBooking Hotel Booking Engine & PMS v1.6.8
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
vikbooking / admin / helpers / src / backup / manager.php

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

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