PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.2
VikBooking Hotel Booking Engine & PMS v1.7.2
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 / model / backup.php

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

358 lines 7.9 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 * VikBooking backup model.
16 *
17 * @since 1.5
18 */
19 class VBOModelBackup extends JObject
20 {
21 /**
22 * Basic item loading implementation.
23 *
24 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
25 * If not set the instance property value is used.
26 *
27 * @return mixed The record object on success, null otherwise.
28 */
29 public function getItem($pk)
30 {
31 // check if we have a file path or a name
32 if (!JFile::exists($pk))
33 {
34 // fetch folder in which the backup are stored
35 $folder = VBOFactory::getConfig()->get('backupfolder');
36
37 if (!$folder)
38 {
39 // folder not specified, use the temporary folder
40 $folder = JFactory::getApplication()->get('tmp_path');
41 }
42
43 // build file path
44 $pk = rtrim($folder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $pk;
45
46 if (!JFile::exists($pk))
47 {
48 // backup not found
49 return null;
50 }
51 }
52
53 // get running platform
54 $platform = VBOFactory::getPlatform();
55
56 $backup = new stdClass;
57
58 $backup->name = basename($pk);
59 $backup->path = $pk;
60 $backup->url = $platform->getUri()->getUrlFromPath($pk);
61 $backup->timestamp = filemtime($pk);
62 $backup->date = JFactory::getDate($backup->timestamp)->format('Y-m-d H:i:s');
63 $backup->size = filesize($pk);
64
65 if (!$backup->url)
66 {
67 // use the administrator task for a direct download
68 $url = $platform->getUri()->addCSRF('index.php?option=com_vikbooking&task=backup.download&cid[]=' . $backup->name);
69 $backup->url = $platform->getUri()->admin($url, false);
70 }
71
72 $backup->type = new stdClass;
73
74 if (preg_match("/backup_/i", $backup->name))
75 {
76 // try to fetch the backup type from the name
77 $chunks = preg_split("/backup_/i", $backup->name);
78 $chunks = preg_split("/_/", array_pop($chunks));
79 // remove date from chunks
80 array_pop($chunks);
81
82 $backup->type->id = implode('_', $chunks);
83
84 // try to fetch the matching export type
85 $type = $this->getExportTypes($backup->type->id);
86
87 if ($type)
88 {
89 $backup->type->name = $type->getName();
90 }
91 else
92 {
93 $backup->type->name = $backup->type->id;
94 }
95 }
96 else
97 {
98 // we are not dealing with a backup file
99 $backup->type->id = 'custom';
100 $backup->type->name = 'custom';
101 }
102
103 return $backup;
104 }
105
106 /**
107 * Entirely rewrite save method because the backup files
108 * do not use database tables.
109 *
110 * @param mixed $data Either an array or an object of data to save.
111 *
112 * @return mixed The ID of the record on success, false otherwise.
113 */
114 public function save($data)
115 {
116 // wrap in a registry for a better ease of use
117 $data = new JRegistry($data);
118
119 $config = VBOFactory::getConfig();
120
121 if ($data->get('action', 'create') === 'create')
122 {
123 // get requested type
124 $type = $data->get('type');
125
126 if (!$type)
127 {
128 // type not specified, use the default one
129 $type = $config->get('backuptype', 'full');
130 }
131
132 $options = [];
133
134 // get requested folder
135 $options['folder'] = $data->get('folder');
136
137 if (!$options['folder'])
138 {
139 // folder not specified, use the default one
140 $options['folder'] = $config->get('backupfolder', null);
141 }
142
143 if ($filename = $data->get('filename'))
144 {
145 // use the given filename
146 $options['filename'] = $filename;
147 }
148
149 if ($prefix = $data->get('prefix'))
150 {
151 // use the given price
152 $options['prefix'] = $prefix;
153 }
154
155 try
156 {
157 // create a new backup
158 $archive = VBOBackupManager::create($type, $options);
159 }
160 catch (Exception $e)
161 {
162 // register error and abort the saving process
163 $this->setError($e);
164 return false;
165 }
166 }
167 else
168 {
169 $file = $data->get('file');
170
171 // get requested folder
172 $dest = $data->get('folder');
173
174 if (!$dest)
175 {
176 // folder not specified, use the default one
177 $dest = $config->get('backupfolder', null);
178 }
179
180 if (!$dest)
181 {
182 // use temporary folder if not specified
183 $dest = JFactory::getApplication()->get('tmp_path');
184 }
185
186 try
187 {
188 // upload archive
189 $resp = VikBooking::uploadFileFromRequest((array)$file, $dest, "/application\/[a-zA-Z0-9\-_.]*\bzip\b/");
190 }
191 catch (Exception $e)
192 {
193 // catch error and register it within the model
194 $this->setError($e);
195
196 return false;
197 }
198
199 // extract file type from path
200 $filetype = pathinfo($resp->path, PATHINFO_EXTENSION);
201
202 // build safe name
203 $safeName = 'backup_uploaded_' . JFactory::getDate()->format('Y-m-d H:i:s') . '.' . $filetype;
204
205 // rename the file so that the system will be able to load it
206 if (!rename($resp->path, dirname($resp->path) . DIRECTORY_SEPARATOR . $safeName))
207 {
208 // it was not possible to rename the file
209 JFile::delete($resp->path);
210 $this->setError(sprintf('Cannot rename [%s] into [%s]', $resp->name, $safeName));
211 return false;
212 }
213
214 // register archive name
215 $archive = $safeName;
216 }
217
218 return $archive;
219 }
220
221 /**
222 * Restores an existing backup.
223 *
224 * @param string $path Either the name of the path of the archive.
225 *
226 * @return boolean True on success, false otherwise.
227 */
228 public function restore($path)
229 {
230 // make sure the archive exists
231 $archive = $this->getItem($path);
232
233 if (!$archive)
234 {
235 // the specified backup doesn't exist
236 $this->setError(sprintf('Backup [%s] not found', $path));
237 return false;
238 }
239
240 try
241 {
242 // restore the system with the backup data
243 VBOBackupManager::restore($archive->path);
244 }
245 catch (Exception $e)
246 {
247 // an error occurred
248 $this->setError($e);
249 return false;
250 }
251
252 return true;
253 }
254
255 /**
256 * Extend delete implementation to delete any related records
257 * stored within a separated table.
258 *
259 * @param mixed $ids Either the record ID or a list of records.
260 *
261 * @return boolean True on success, false otherwise.
262 */
263 public function delete($ids)
264 {
265 $ids = (array) $ids;
266
267 $result = false;
268
269 foreach ($ids as $id)
270 {
271 // fetch backup details
272 $item = $this->getItem($id);
273
274 if ($item)
275 {
276 // delete the backup file
277 $result = JFile::delete($item->path) || $result;
278 }
279 }
280
281 return $result;
282 }
283
284 /**
285 * Moves the existing backup archives into the new specified folder.
286 *
287 * @param string $path The new folder used to host the archives.
288 *
289 * @return boolean True on success, false otherwise.
290 */
291 public function moveArchives($path)
292 {
293 // get currently set folder
294 $current = VBOFactory::getConfig()->get('backupfolder');
295
296 if (!$current)
297 {
298 // folder not specified, use the default one
299 $current = JFactory::getApplication()->get('tmp_path');
300 }
301
302 // load all backup archives
303 $files = JFolder::files($current, 'backup_', $recurse = false, $fullpath = true);
304
305 $path = rtrim($path, DIRECTORY_SEPARATOR);
306
307 if (!JFolder::exists($path))
308 {
309 // attempt to create the folder if missing
310 JFolder::create($path);
311 }
312
313 $status = true;
314
315 foreach ($files as $file)
316 {
317 // create new path
318 $newFile = $path . DIRECTORY_SEPARATOR . basename($file);
319 // try to rename the archive
320 if (!rename($file, $newFile))
321 {
322 $this->setError(sprintf('Unable to move [%s] into [%s]', $file, $newFile));
323 $status = false;
324 }
325 }
326
327 return $status;
328 }
329
330 /**
331 * Returns a list of supported export types.
332 *
333 * @param string|null $id When provided, only the matching type will be returned.
334 *
335 * @return array|object|null
336 */
337 public function getExportTypes($id = null)
338 {
339 // the export types will be fetched only once
340 $types = VBOBackupManager::getExportTypes();
341
342 if (!$id)
343 {
344 // return all the types
345 return $types;
346 }
347
348 if ($id && isset($types[$id]))
349 {
350 // return the searched type
351 return $types[$id];
352 }
353
354 // type not found
355 return null;
356 }
357 }
358