PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 2.0.3
JetBackup – Backup, Restore & Migrate v2.0.3
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / com / core / functions.php
backup / com / core Last commit date
backup 3 years ago database 3 years ago exception 3 years ago extension 3 years ago notice 3 years ago restore 3 years ago schedule 3 years ago storage 3 years ago SGBoot.php 3 years ago SGConfig.php 3 years ago functions.php 3 years ago
functions.php
1080 lines
1 <?php
2
3 function backupGuardGetSiteUrl()
4 {
5 if (SG_ENV_ADAPTER == SG_ENV_WORDPRESS) {
6 return get_site_url();
7 } else {
8 return sprintf(
9 "%s://%s%s",
10 isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
11 $_SERVER['SERVER_NAME'],
12 $_SERVER['REQUEST_URI']
13 );
14 }
15 }
16
17 function backupGuardGetCapabilities()
18 {
19 switch (SG_PRODUCT_IDENTIFIER) {
20 case 'backup-guard-wp-platinum':
21 return BACKUP_GUARD_CAPABILITIES_PLATINUM;
22 case 'backup-guard-wp-gold':
23 return BACKUP_GUARD_CAPABILITIES_GOLD;
24 case 'backup-guard-wp-silver':
25 return BACKUP_GUARD_CAPABILITIES_SILVER;
26 case 'backup-guard-wp-free':
27 return BACKUP_GUARD_CAPABILITIES_FREE;
28 }
29 }
30
31 function convertToReadableSize($size)
32 {
33 if (!$size) {
34 return '0';
35 }
36
37 $base = log($size) / log(1000);
38 $suffix = array("", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
39 $fBase = floor($base);
40
41 return round(pow(1000, $base - floor($base)), 1) . $suffix[$fBase];
42 }
43
44 function backupGuardConvertDateTimezone($dateTime, $currentTimezone = false, $dateFormat = "Y-m-d H:i:s", $timeZone = SG_DEFAULT_TIMEZONE)
45 {
46 if ($currentTimezone) {
47 $getCurrentTimezone = SGConfig::get('SG_TIMEZONE', true);
48
49 if ($getCurrentTimezone) {
50 $timeZone = $getCurrentTimezone;
51 }
52 }
53
54 $newDateTime = new DateTime($dateTime);
55 $newDateTime->setTimezone(new DateTimeZone($timeZone));
56
57 return $newDateTime->format($dateFormat);
58 }
59
60 function backupGuardCeliDateTimezone($time)
61 {
62
63 $currentDateTime = date('Y-m-d H', $time);
64
65 $celiCurrentDateTime = $currentDateTime . ':00:00';
66
67 return date('Y-m-d H:i:s', strtotime($celiCurrentDateTime));
68 }
69
70 function backupGuardConvertDateTimezoneToUTC($dateTime, $timezone = 'UTC')
71 {
72 $getCurrentTimezone = SGConfig::get('SG_TIMEZONE', true);
73 if ($getCurrentTimezone) {
74 $timezone = $getCurrentTimezone;
75 }
76
77 $newDateTime = new DateTime($dateTime, new DateTimeZone($timezone));
78 $newDateTime->setTimezone(new DateTimeZone("UTC"));
79 $dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");
80
81 return $dateTimeUTC;
82 }
83
84 function backupGuardRemoveSlashes($value)
85 {
86 if (SG_ENV_ADAPTER == SG_ENV_WORDPRESS) {
87 return wp_unslash($value);
88 } else {
89 if (is_array($value)) {
90 return array_map('stripslashes', $value);
91 }
92
93 return stripslashes($value);
94 }
95 }
96
97 function backupGuardSanitizeTextField($value)
98 {
99 if (SG_ENV_ADAPTER == SG_ENV_WORDPRESS) {
100 if (is_array($value)) {
101 return array_map('sanitize_text_field', $value);
102 }
103
104 return sanitize_text_field($value);
105 } else {
106 if (is_array($value)) {
107 return array_map('strip_tags', $value);
108 }
109
110 return strip_tags($value);
111 }
112 }
113
114 function backupGuardIsMultisite()
115 {
116 if (SG_ENV_ADAPTER == SG_ENV_WORDPRESS) {
117 return defined('BG_IS_MULTISITE') ? BG_IS_MULTISITE : is_multisite();
118 } else {
119 return false;
120 }
121 }
122
123 function backupGuardGetFilenameOptions($options)
124 {
125 $selectedPaths = explode(',', $options['SG_BACKUP_FILE_PATHS']);
126 $pathsToExclude = explode(',', $options['SG_BACKUP_FILE_PATHS_EXCLUDE']);
127
128 $opt = '';
129
130 if (SG_ENV_ADAPTER == SG_ENV_WORDPRESS) {
131 $opt .= 'opt(';
132
133 if ($options['SG_BACKUP_TYPE'] == SG_BACKUP_TYPE_CUSTOM) {
134 if ($options['SG_ACTION_BACKUP_DATABASE_AVAILABLE']) {
135 $opt .= 'db_';
136 }
137
138 if ($options['SG_ACTION_BACKUP_FILES_AVAILABLE']) {
139 if (in_array('wp-content', $selectedPaths)) {
140 $opt .= 'wpc_';
141 }
142 if (!in_array('wp-content/plugins', $pathsToExclude)) {
143 $opt .= 'plg_';
144 }
145 if (!in_array('wp-content/themes', $pathsToExclude)) {
146 $opt .= 'thm_';
147 }
148 if (!in_array('wp-content/uploads', $pathsToExclude)) {
149 $opt .= 'upl_';
150 }
151 }
152 } else {
153 $opt .= 'full';
154 }
155
156 $opt = trim($opt, "_");
157 $opt .= ')_';
158 }
159
160 return $opt;
161 }
162
163 function backupGuardGenerateToken()
164 {
165 return md5(time());
166 }
167
168 // Parse a URL and return its components
169 function backupGuardParseUrl($url)
170 {
171 $urlComponents = parse_url($url);
172 $domain = $urlComponents['host'];
173 $port = '';
174
175 if (isset($urlComponents['port']) && strlen($urlComponents['port'])) {
176 $port = ":" . $urlComponents['port'];
177 }
178
179 $domain = preg_replace("/(www|\dww|w\dw|ww\d)\./", "", $domain);
180
181 $path = "";
182 if (isset($urlComponents['path'])) {
183 $path = $urlComponents['path'];
184 }
185
186 return $domain . $port . $path;
187 }
188
189 function backupGuardIsReloadEnabled()
190 {
191 // Check if reloads option is turned on
192 return SGConfig::get('SG_BACKUP_WITH_RELOADINGS') ? true : false;
193 }
194
195 function backupGuardGetBackupOptions($options)
196 {
197 $backupOptions = array(
198 'SG_BACKUP_UPLOAD_TO_STORAGES' => '',
199 'SG_BACKUP_FILE_PATHS_EXCLUDE' => '',
200 'SG_BACKUP_FILE_PATHS' => ''
201 );
202
203 if (isset($options['sg-custom-backup-name']) && $options['sg-custom-backup-name']) {
204 SGConfig::set("SG_CUSTOM_BACKUP_NAME", $options['sg-custom-backup-name']);
205 } else {
206 SGConfig::set("SG_CUSTOM_BACKUP_NAME", '');
207 }
208
209 //If background mode
210 $isBackgroundMode = !empty($options['backgroundMode']) ? 1 : 0;
211
212 if ($isBackgroundMode) {
213 $backupOptions['SG_BACKUP_IN_BACKGROUND_MODE'] = $isBackgroundMode;
214 }
215
216 //If cloud backup
217 if (!empty($options['backupCloud']) && count($options['backupStorages'])) {
218 $clouds = $options['backupStorages'];
219 $backupOptions['SG_BACKUP_UPLOAD_TO_STORAGES'] = implode(',', $clouds);
220 }
221
222 $backupOptions['SG_BACKUP_TYPE'] = $options['backupType'];
223
224 if ($options['backupType'] == SG_BACKUP_TYPE_FULL) {
225 $backupOptions['SG_ACTION_BACKUP_DATABASE_AVAILABLE'] = 1;
226 $backupOptions['SG_ACTION_BACKUP_FILES_AVAILABLE'] = 1;
227 $backupOptions['SG_BACKUP_FILE_PATHS_EXCLUDE'] = SG_BACKUP_FILE_PATHS_EXCLUDE;
228 $backupOptions['SG_BACKUP_FILE_PATHS'] = 'wp-content';
229 } else if ($options['backupType'] == SG_BACKUP_TYPE_CUSTOM) {
230 //If database backup
231 $isDatabaseBackup = !empty($options['backupDatabase']) ? 1 : 0;
232 $backupOptions['SG_ACTION_BACKUP_DATABASE_AVAILABLE'] = $isDatabaseBackup;
233
234 //If db backup
235 if ($options['backupDBType']) {
236 $tablesToBackup = implode(',', $options['table']);
237 $backupOptions['SG_BACKUP_TABLES_TO_BACKUP'] = $tablesToBackup;
238 }
239
240 //If files backup
241 if (!empty($options['backupFiles']) && count($options['directory'])) {
242 $backupFiles = explode(',', SG_BACKUP_FILE_PATHS);
243 $filesToExclude = @array_diff($backupFiles, $options['directory']);
244
245 if (in_array('wp-content', $options['directory'])) {
246 $options['directory'] = array('wp-content');
247 } else {
248 $filesToExclude = array_diff($filesToExclude, array('wp-content'));
249 }
250
251 $filesToExclude = implode(',', $filesToExclude);
252 if (strlen($filesToExclude)) {
253 $filesToExclude = ',' . $filesToExclude;
254 }
255
256 $backupOptions['SG_BACKUP_FILE_PATHS_EXCLUDE'] = SG_BACKUP_FILE_PATHS_EXCLUDE . $filesToExclude;
257 $options['directory'] = backupGuardSanitizeTextField($options['directory']);
258 $backupOptions['SG_BACKUP_FILE_PATHS'] = implode(',', $options['directory']);
259 $backupOptions['SG_ACTION_BACKUP_FILES_AVAILABLE'] = 1;
260 } else {
261 $backupOptions['SG_ACTION_BACKUP_FILES_AVAILABLE'] = 0;
262 $backupOptions['SG_BACKUP_FILE_PATHS'] = 0;
263 }
264 }
265
266 return $backupOptions;
267 }
268
269 function backupGuardLoadStateData()
270 {
271 if (file_exists(SG_BACKUP_DIRECTORY . SG_STATE_FILE_NAME)) {
272 $sgState = new SGState();
273 $stateFile = file_get_contents(SG_BACKUP_DIRECTORY . SG_STATE_FILE_NAME);
274 $sgState = $sgState->factory($stateFile);
275
276 return $sgState;
277 }
278
279 return false;
280 }
281
282 function backupGuardValidateApiCall($token)
283 {
284 if (empty($token)) {
285 exit;
286 }
287
288 $statePath = SG_BACKUP_DIRECTORY . SG_STATE_FILE_NAME;
289
290 if (!file_exists($statePath)) {
291 exit;
292 }
293
294 $state = file_get_contents($statePath);
295 $state = json_decode($state, true);
296 $stateToken = $state['token'];
297
298 if ($stateToken != $token) {
299 exit;
300 }
301
302 return true;
303 }
304
305 function backupGuardScanBackupsDirectory($path)
306 {
307 $backups = scandir($path);
308 $backupFolders = array();
309 foreach ($backups as $backup) {
310 if ($backup == "." || $backup == "..") {
311 continue;
312 }
313
314 if (is_dir($path . $backup)) {
315 $backupFolders[$backup] = filemtime($path . $backup);
316 }
317 }
318 // Sort(from low to high) backups by creation date
319 asort($backupFolders);
320
321 return $backupFolders;
322 }
323
324 function backupGuardSymlinksCleanup($dir)
325 {
326 if (is_dir($dir)) {
327 $objects = scandir($dir);
328 foreach ($objects as $object) {
329 if ($object == "." || $object == "..") {
330 continue;
331 }
332
333 if (filetype($dir . $object) != "dir") {
334 @unlink($dir . $object);
335 } else {
336 backupGuardSymlinksCleanup($dir . $object . '/');
337 @rmdir($dir . $object);
338 }
339 }
340 } else if (file_exists($dir)) {
341 @unlink($dir);
342 }
343
344 return;
345 }
346
347 function backupGuardRealFilesize($filename)
348 {
349 $fp = fopen($filename, 'r');
350 $return = false;
351 if (is_resource($fp)) {
352 if (PHP_INT_SIZE < 8) { // 32 bit
353 if (0 === fseek($fp, 0, SEEK_END)) {
354 $return = 0.0;
355 $step = 0x7FFFFFFF;
356 while ($step > 0) {
357 if (0 === fseek($fp, -$step, SEEK_CUR)) {
358 $return += floatval($step);
359 } else {
360 $step >>= 1;
361 }
362 }
363 }
364 } else if (0 === fseek($fp, 0, SEEK_END)) { // 64 bit
365 $return = ftell($fp);
366 }
367 }
368
369 return $return;
370 }
371
372 function backupGuardFormattedDuration($startTs, $endTs)
373 {
374 $result = '';
375 $seconds = $endTs - $startTs;
376
377 if ($seconds < 1) {
378 return '0 seconds';
379 }
380
381 $days = intval(intval($seconds) / (3600 * 24));
382 if ($days > 0) {
383 $result .= $days . (($days > 1) ? ' days ' : ' day ');
384 }
385
386 $hours = intval(intval($seconds) / 3600) % 24;
387 if ($hours > 0) {
388 $result .= $hours . (($hours > 1) ? ' hours ' : ' hour ');
389 }
390
391 $minutes = intval(intval($seconds) / 60) % 60;
392 if ($minutes > 0) {
393 $result .= $minutes . (($minutes > 1) ? ' minutes ' : ' minute ');
394 }
395
396 $seconds = intval($seconds) % 60;
397 if ($seconds > 0) {
398 $result .= $seconds . (($seconds > 1) ? ' seconds' : ' second');
399 }
400
401 return $result;
402 }
403
404 function backupGuardDeleteDirectory($dirName)
405 {
406 $dirHandle = null;
407 if (is_dir($dirName)) {
408 $dirHandle = opendir($dirName);
409 }
410
411 if (!$dirHandle) {
412 return false;
413 }
414
415 while ($file = readdir($dirHandle)) {
416 if ($file != "." && $file != "..") {
417 if (!is_dir($dirName . "/" . $file)) {
418 @unlink($dirName . "/" . $file);
419 } else {
420 backupGuardDeleteDirectory($dirName . '/' . $file);
421 }
422 }
423 }
424
425 closedir($dirHandle);
426
427 return @rmdir($dirName);
428 }
429
430 function backupGuardMakeSymlinkFolder($filename)
431 {
432 $filename = backupGuardRemoveSlashes($filename);
433
434 $downloaddir = SG_SYMLINK_PATH;
435
436 if (!file_exists($downloaddir)) {
437 mkdir($downloaddir, 0777);
438 }
439
440 $letters = 'abcdefghijklmnopqrstuvwxyz';
441 srand((double) microtime() * 1000000);
442 $string = '';
443
444 for ($i = 1; $i <= rand(4, 12); $i++) {
445 $q = rand(1, 24);
446 $string = $string . $letters[$q];
447 }
448
449 $handle = opendir($downloaddir);
450 while ($dir = readdir($handle)) {
451 if ($dir == "." || $dir == "..") {
452 continue;
453 }
454
455 if (is_dir($downloaddir . $dir)) {
456 @unlink($downloaddir . $dir . "/" . $filename);
457 @rmdir($downloaddir . $dir);
458 }
459 }
460
461 closedir($handle);
462 mkdir($downloaddir . $string, 0777);
463
464 return $string;
465 }
466
467 function backupGuardDownloadFile($file, $type = 'application/octet-stream')
468 {
469 if (ob_get_level()) {
470 ob_end_clean();
471 }
472
473 $file = backupGuardRemoveSlashes($file);
474 if (file_exists($file)) {
475 header('Content-Description: File Transfer');
476 header('Content-Type: ' . $type);
477 header('Content-Disposition: attachment; filename="' . basename($file) . '";');
478 header('Expires: 0');
479 header('Cache-Control: must-revalidate');
480 header('Pragma: public');
481 header('Content-Length: ' . filesize($file));
482 readfile($file);
483 }
484
485 exit;
486 }
487
488 function backupGuardDownloadViaPhp($backupName, $fileName)
489 {
490 $str = backupGuardMakeSymlinkFolder($fileName);
491 @copy(SG_BACKUP_DIRECTORY . $backupName . '/' . $fileName, SG_SYMLINK_PATH . $str . '/' . $fileName);
492
493 if (file_exists(SG_SYMLINK_PATH . $str . '/' . $fileName)) {
494 $remoteGet = wp_remote_get(SG_SYMLINK_URL . $str . '/' . $fileName);
495 if (!is_wp_error($remoteGet)) {
496 $content = wp_remote_retrieve_body($remoteGet);
497 header('Pragma: public');
498 header('Expires: 0');
499 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
500 header('Cache-Control: private', false);
501 header('Content-Type: application/octet-stream');
502 header('Content-Disposition: attachment; filename=' . $fileName . ';');
503 header('Content-Transfer-Encoding: binary');
504 echo $content;
505 exit;
506 }
507 }
508 }
509
510 function backupGuardDownloadFileViaFunction($safeDir, $fileName, $type)
511 {
512 $downloadDir = SG_SYMLINK_PATH;
513 $downloadURL = SG_SYMLINK_URL;
514
515 $safeDir = backupGuardRemoveSlashes($safeDir);
516 $string = backupGuardMakeSymlinkFolder($fileName);
517
518 $target = $safeDir . $fileName;
519 $link = $downloadDir . $string . '/' . $fileName;
520
521 if ($type == BACKUP_GUARD_DOWNLOAD_MODE_LINK) {
522 $res = @link($target, $link);
523 $name = 'link';
524 } else {
525 $res = @symlink($target, $link);
526 $name = 'symlink';
527 }
528
529 if ($res) {
530 header('Content-Description: File Transfer');
531 header('Content-Type: application/octet-stream');
532 header('Content-Disposition: attachment;filename="' . $fileName . '"');
533 header('Content-Transfer-Encoding: binary');
534 header("Location: " . $downloadURL . $string . "/" . $fileName);
535 } else {
536 wp_die(_backupGuardT(ucfirst($name) . " / shortcut creation failed! Seems your server configurations don't allow $name creation, so we're unable to provide you the direct download url. You can download your backup using any FTP client. All backups and related stuff we locate '/wp-content/uploads/".SG_BACKUP_FOLDER_NAME."' directory. If you need this functionality, you should check out your server configurations and make sure you don't have any limitation related to $name creation.", true));
537 }
538 exit;
539 }
540
541 function backupGuardDownloadFileSymlink($safedir, $filename)
542 {
543 $downloaddir = SG_SYMLINK_PATH;
544 $downloadURL = SG_SYMLINK_URL;
545
546 $safedir = backupGuardRemoveSlashes($safedir);
547 $string = backupGuardMakeSymlinkFolder($filename);
548
549 $res = @symlink($safedir . $filename, $downloaddir . $string . "/" . $filename);
550 if ($res) {
551 header('Content-Description: File Transfer');
552 header('Content-Type: application/octet-stream');
553 header('Content-Disposition: attachment;filename="' . $filename . '"');
554 header('Content-Transfer-Encoding: binary');
555 header("Location: " . $downloadURL . $string . "/" . $filename);
556 } else {
557 wp_die(_backupGuardT("Symlink / shortcut creation failed! Seems your server configurations don't allow symlink creation, so we're unable to provide you the direct download url. You can download your backup using any FTP client. All backups and related stuff we locate '/wp-content/uploads/".SG_BACKUP_FOLDER_NAME."' directory. If you need this functionality, you should check out your server configurations and make sure you don't have any limitation related to symlink creation.", true));
558 }
559 exit;
560 }
561
562 function backupGuardDownloadFileLink($safedir, $filename)
563 {
564 $downloaddir = SG_SYMLINK_PATH;
565 $downloadURL = SG_SYMLINK_URL;
566
567 $safedir = backupGuardRemoveSlashes($safedir);
568 $string = backupGuardMakeSymlinkFolder($filename);
569
570 $res = @link($safedir . $filename, $downloaddir . $string . "/" . $filename);
571 if ($res) {
572 header('Content-Description: File Transfer');
573 header('Content-Type: application/octet-stream');
574 header('Content-Disposition: attachment;filename="' . $filename . '"');
575 header('Content-Transfer-Encoding: binary');
576 header("Location: " . $downloadURL . $string . "/" . $filename);
577 } else {
578 wp_die(_backupGuardT("Link / shortcut creation failed! Seems your server configurations don't allow link creation, so we're unable to provide you the direct download url. You can download your backup using any FTP client. All backups and related stuff we locate '/wp-content/uploads/".SG_BACKUP_FOLDER_NAME."' directory. If you need this functionality, you should check out your server configurations and make sure you don't have any limitation related to link creation.", true));
579 }
580 exit;
581 }
582
583 function backupGuardGetCurrentUrlScheme()
584 {
585 return (is_ssl()) ? 'https' : 'http';
586 }
587
588 function backupGuardGetCurrentUrlHost()
589 {
590 if (defined('SG_SITE_URL')) {
591 $url = SG_SITE_URL;
592 } else {
593 $url = get_site_url();
594 }
595
596 $parseUrl = parse_url($url);
597
598 return $parseUrl['host'];
599 }
600
601 function backupGuardValidateLicense()
602 {
603 $pluginCapabilities = backupGuardGetCapabilities();
604 if ($pluginCapabilities == BACKUP_GUARD_CAPABILITIES_FREE) {
605 return true;
606 }
607
608 include_once SG_LIB_PATH . 'SGAuthClient.php';
609 include_once SG_LIB_PATH . 'BackupGuard/License.php';
610
611 $nextCheck = (int) SGConfig::get('SG_LOCAL_KEY_NEXT_CHECK_TS', true);
612
613 if ($nextCheck < time()) {
614 BackupGuard\License::retrieveLocalKey();
615 }
616
617 $auth = SGAuthClient::getInstance();
618
619 try {
620 BackupGuard\License::checkLocalKey();
621 } catch (Exception $e) {
622 backup_guard_login_page();
623
624 return false;
625 }
626
627 return true;
628 }
629
630 //returns true if string $haystack ends with string $needle or $needle is an empty string
631 function backupGuardStringEndsWith($haystack, $needle)
632 {
633 $length = strlen($needle);
634
635 return $length === 0 || (substr($haystack, -$length) === $needle);
636 }
637
638 //returns true if string $haystack starts with string $needle
639 function backupGuardStringStartsWith($haystack, $needle)
640 {
641 $length = strlen($needle);
642
643 return (substr($haystack, 0, $length) === $needle);
644 }
645
646 function backupGuardGetDbTables()
647 {
648 $sgdb = SGDatabase::getInstance();
649 $tables = $sgdb->query("SHOW TABLES");
650 $tablesKey = 'Tables_in_' . strtolower(SG_DB_NAME);
651 $tableNames = array();
652 $customTablesToExclude = !empty(SGConfig::get('SG_TABLES_TO_EXCLUDE')) ? str_replace(' ', '', SGConfig::get('SG_TABLES_TO_EXCLUDE')) : '';
653
654 $tablesToExclude = explode(',', $customTablesToExclude);
655 foreach ($tables as $table) :
656 $tableName = $table[$tablesKey];
657 if ($tableName != SG_ACTION_TABLE_NAME && $tableName != SG_CONFIG_TABLE_NAME && $tableName != SG_SCHEDULE_TABLE_NAME) {
658 array_push(
659 $tableNames,
660 array(
661 'name' => $tableName,
662 'current' => backupGuardStringStartsWith($tableName, SG_ENV_DB_PREFIX) ? 'true' : 'false',
663 'disabled' => in_array($tableName, $tablesToExclude) ? 'disabled' : ''
664 )
665 );
666 }
667 endforeach;
668 usort(
669 $tableNames,
670 function ($name1, $name2) {
671 if (backupGuardStringStartsWith($name1['name'], SG_ENV_DB_PREFIX)) {
672 if (backupGuardStringStartsWith($name2['name'], SG_ENV_DB_PREFIX)) {
673 return 0;
674 }
675
676 return -1;
677 }
678
679 return 1;
680 }
681 );
682
683 return $tableNames;
684 }
685
686 function backupGuardGetBackupTablesHTML($defaultChecked = false)
687 {
688 $tables = backupGuardGetDbTables();
689 ?>
690
691 <div class="checkbox">
692 <label for="custom-backupdb-chbx">
693 <input type="checkbox" class="sg-custom-option" name="backupDatabase"
694 id="custom-backupdb-chbx" <?php echo $defaultChecked ? 'checked' : '' ?>>
695 <span class="sg-checkbox-label-text"><?php _backupGuardT('Backup database'); ?></span>
696 </label>
697 <div class="col-md-12 sg-checkbox sg-backup-db-options">
698 <div class="checkbox">
699 <label for="custombackupdbfull-radio" class="sg-backup-db-mode"
700 title="<?php _backupGuardT('Backup all tables found in the database') ?>">
701 <input type="radio" name="backupDBType" id="custombackupdbfull-radio" value="0" checked>
702 <?php _backupGuardT('Full'); ?>
703 </label>
704 <label for="custombackupdbcurent-radio" class="sg-backup-db-mode"
705 title="<?php echo _backupGuardT('Backup tables related to the current WordPress installation. Only tables with', true) . ' ' . SG_ENV_DB_PREFIX . ' ' . _backupGuardT('will be backed up', true) ?>">
706 <input type="radio" name="backupDBType" id="custombackupdbcurent-radio" value="1">
707 <?php _backupGuardT('Only WordPress'); ?>
708 </label>
709 <label for="custombackupdbcustom-radio" class="sg-backup-db-mode"
710 title="<?php _backupGuardT('Select tables you want to include in your backup') ?>">
711 <input type="radio" name="backupDBType" id="custombackupdbcustom-radio" value="2">
712 <?php _backupGuardT('Custom'); ?>
713 </label>
714 <!--Tables-->
715 <div class="col-md-12 sg-custom-backup-tables">
716 <?php foreach ($tables as $table) : ?>
717 <div class="checkbox">
718 <label for="<?php echo esc_attr($table['name']) ?>">
719 <input
720 type="checkbox"
721 name="table[]"
722 current="<?php echo esc_attr($table['current']) ?>"
723 <?php echo esc_attr($table['disabled']) ?>
724 id="<?php echo esc_attr($table['name']) ?>"
725 value="<?php echo esc_attr($table['name']); ?>"
726 >
727 <span class="sg-checkbox-label-text"><?php echo basename(esc_html($table['name'])); ?></span>
728 <?php if ($table['disabled']) { ?>
729 <span class="sg-disableText"><?php _backupGuardT('(excluded from settings)') ?></span>
730 <?php } ?>
731 </label>
732 </div>
733 <?php endforeach; ?>
734 </div>
735 </div>
736 </div>
737
738 </div>
739
740 <?php
741 }
742
743 function backupGuardIsAccountGold()
744 {
745 return strpos("gold", SG_PRODUCT_IDENTIFIER) !== false;
746 }
747
748 function backupGuardGetProductName()
749 {
750 $name = '';
751 switch (SG_PRODUCT_IDENTIFIER) {
752 case 'backup-guard-wp-silver':
753 $name = 'Solo';
754 break;
755 case 'backup-guard-wp-platinum':
756 $name = 'Pro';
757 break;
758 case 'backup-guard-wp-gold':
759 $name = 'Admin';
760 break;
761 case 'backup-guard-wp-free':
762 $name = 'Free';
763 break;
764 }
765
766 return $name;
767 }
768
769 function backupGuardGetFileSelectiveRestore()
770 {
771 ?>
772 <div class="col-md-12 sg-checkbox sg-restore-files-options">
773 <div class="checkbox">
774 <label for="restorefilesfull-radio" class="sg-restore-files-mode">
775 <input type="radio" name="restoreFilesType" checked id="restorefilesfull-radio" value="0">
776 <?php _backupGuardT('Full'); ?>
777 </label>
778
779 <label for="restorefilescustom-radio" class="sg-restore-files-mode">
780 <input type="radio" name="restoreFilesType" id="restorefilescustom-radio" value="1">
781 <?php _backupGuardT('Custom'); ?>
782 </label>
783 <!--Files-->
784 <div class="col-md-12 sg-file-selective-restore">
785 <div id="fileSystemTreeContainer"></div>
786 </div>
787 </div>
788 </div>
789 <?php
790 }
791
792 function checkAllMissedTables()
793 {
794 $sgdb = SGDatabase::getInstance();
795 $allTables = array(SG_CONFIG_TABLE_NAME, SG_SCHEDULE_TABLE_NAME, SG_ACTION_TABLE_NAME);
796 $status = true;
797
798 foreach ($allTables as $table) {
799 $query = $sgdb->query(
800 "SELECT count(*) as isExists
801 FROM information_schema.TABLES
802 WHERE (TABLE_SCHEMA = '" . DB_NAME . "') AND (TABLE_NAME = '$table')"
803 );
804
805 if (empty($query[0]['isExists'])) {
806 $status = false;
807 }
808 }
809
810 return $status;
811 }
812
813 function backupGuardIncludeFile($filePath)
814 {
815 if (file_exists($filePath)) {
816 include_once $filePath;
817 }
818 }
819
820 function getCloudUploadDefaultMaxChunkSize()
821 {
822 $memory = (int) SGBoot::$memoryLimit;
823 $uploadSize = 1;
824
825 if ($memory <= 128) {
826 $uploadSize = 4;
827 } else if ($memory > 128 && $memory <= 256) {
828 $uploadSize = 8;
829 } else if ($memory > 256 && $memory <= 512) {
830 $uploadSize = 16;
831 } else if ($memory > 512) {
832 $uploadSize = 32;
833 }
834
835 return $uploadSize;
836 }
837
838 function getCloudUploadChunkSize()
839 {
840 $cloudUploadDefaultChunkSize = (int) getCloudUploadDefaultMaxChunkSize();
841 $savedCloudUploadChunkSize = (int) SGConfig::get('SG_BACKUP_CLOUD_UPLOAD_CHUNK_SIZE');
842
843 return ($savedCloudUploadChunkSize ? $savedCloudUploadChunkSize : $cloudUploadDefaultChunkSize);
844 }
845
846 function backupGuardCheckOS()
847 {
848 $os = strtoupper(substr(PHP_OS, 0, 3));
849
850 if ($os === 'WIN') {
851 return 'windows';
852 } else if ($os === 'LIN') {
853 return 'linux';
854 }
855
856 return 'other';
857 }
858
859 function backupGuardCheckDownloadMode()
860 {
861 $system = backupGuardCheckOS();
862 $link = false;
863 $symlink = false;
864
865 if (!file_exists(SG_SYMLINK_PATH)) {
866 mkdir(SG_SYMLINK_PATH);
867 }
868
869 backupGuardRemoveDownloadTmpFiles();
870
871 $testFile = fopen(SG_SYMLINK_PATH . 'test.log', 'w');
872
873 if (!$testFile) {
874 return BACKUP_GUARD_DOWNLOAD_MODE_PHP;
875 }
876
877 if (function_exists('link')) {
878 $link = @link(SG_SYMLINK_PATH . 'test.log', SG_SYMLINK_PATH . 'link.log');
879 }
880
881 if (function_exists('symlink')) {
882 $symlink = @symlink(SG_SYMLINK_PATH . 'test.log', SG_SYMLINK_PATH . 'symlink.log');
883 }
884
885 backupGuardRemoveDownloadTmpFiles();
886
887 if ($system == 'windows') {
888 if ($symlink) {
889 return BACKUP_GUARD_DOWNLOAD_MODE_SYMLINK;
890 }
891 } else {
892 if ($link) {
893 return BACKUP_GUARD_DOWNLOAD_MODE_LINK;
894 } elseif ($symlink) {
895 return BACKUP_GUARD_DOWNLOAD_MODE_SYMLINK;
896 }
897 }
898
899 return BACKUP_GUARD_DOWNLOAD_MODE_PHP;
900 }
901
902 function backupGuardRemoveDownloadTmpFiles()
903 {
904 if (file_exists(SG_SYMLINK_PATH . 'test.log')) {
905 @unlink(SG_SYMLINK_PATH . 'test.log');
906 }
907
908 if (file_exists(SG_SYMLINK_PATH . 'link.log')) {
909 @unlink(SG_SYMLINK_PATH . 'link.log');
910 }
911
912 if (file_exists(SG_SYMLINK_PATH . 'symlink.log') || is_link(SG_SYMLINK_PATH . 'symlink.log')) {
913 @unlink(SG_SYMLINK_PATH . 'symlink.log');
914 }
915 }
916
917 function backupGuardMigrateDownloadMode()
918 {
919 $downloadModeRow = SGConfig::get('SG_DOWNLOAD_MODE', true);
920
921 if (!is_null($downloadModeRow)) {
922 return true;
923 }
924
925 $downloadMode = BACKUP_GUARD_DOWNLOAD_MODE_PHP;
926 $downloadViaPhp = SGConfig::get('SG_DOWNLOAD_VIA_PHP', true);
927
928 if ($downloadViaPhp != BACKUP_GUARD_DOWNLOAD_MODE_PHP) {
929 $downloadMode = backupGuardCheckDownloadMode();
930 }
931
932 SGConfig::set('SG_DOWNLOAD_MODE', $downloadMode);
933
934 return true;
935 }
936
937 function addLiteSpeedHtaccessModule()
938 {
939 $server = '';
940 if (isset($_SERVER['SERVER_SOFTWARE'])) {
941 $server = strtolower($_SERVER['SERVER_SOFTWARE']);
942 }
943
944 if (strpos($server, 'litespeed') !== false) {
945 $htaccessFile = ABSPATH . '.htaccess';
946 $htaccessContent = '';
947
948 if (is_readable($htaccessFile)) {
949 $htaccessContent = @file_get_contents($htaccessFile);
950 if (!$htaccessContent) {
951 $htaccessContent = '';
952 }
953 }
954
955 if (!$htaccessContent || !preg_match('/noabort/i', $htaccessContent)) {
956 $liteSpeedTemplate = file_get_contents(SG_HTACCESS_TEMPLATES_PATH . 'liteSpeed.php');
957 $result = file_put_contents($htaccessFile, "\n" . $liteSpeedTemplate, FILE_APPEND);
958
959 if ($result) {
960 return true;
961 }
962 }
963 }
964
965 return false;
966 }
967
968 function removeLiteSpeedHtaccessModule()
969 {
970 $htaccessFile = ABSPATH . '.htaccess';
971 $htaccessContent = file_get_contents($htaccessFile);
972
973 $result = preg_replace('/(# LITESPEED START[\s\S]+?# LITESPEED END)/', '', $htaccessContent);
974
975 if ($result) {
976 $change = file_put_contents($htaccessFile, $result);
977
978 if ($change) {
979 return true;
980 }
981 }
982
983 return false;
984 }
985
986 function getAllTimezones()
987 {
988 static $regions = array(
989 DateTimeZone::AFRICA,
990 DateTimeZone::AMERICA,
991 DateTimeZone::ANTARCTICA,
992 DateTimeZone::ASIA,
993 DateTimeZone::ATLANTIC,
994 DateTimeZone::AUSTRALIA,
995 DateTimeZone::EUROPE,
996 DateTimeZone::INDIAN,
997 DateTimeZone::PACIFIC,
998 );
999
1000 $timezones = array();
1001 foreach ($regions as $region) {
1002 $timezones = array_merge($timezones, DateTimeZone::listIdentifiers($region));
1003 }
1004
1005 $timezoneOffsets = array();
1006 foreach ($timezones as $timezone) {
1007 $tz = new DateTimeZone($timezone);
1008 $timezoneOffsets[$timezone] = $tz->getOffset(new DateTime());
1009 }
1010
1011 asort($timezoneOffsets);
1012
1013 $timezoneList = array();
1014 foreach ($timezoneOffsets as $timezone => $offset) {
1015 $offsetPrefix = $offset < 0 ? '-' : '+';
1016 $offsetFormatted = gmdate('H:i', abs($offset));
1017 $offsetFormattedOnlyHour = gmdate('H', abs($offset));
1018
1019 $prettyOffset = "UTC$offsetPrefix$offsetFormatted";
1020
1021 $timezoneList[$timezone] = ["($prettyOffset) $timezone", "$offsetPrefix$offsetFormattedOnlyHour"];
1022 }
1023
1024 return $timezoneList;
1025 }
1026
1027 function backupGuardIsMaintenanceMode()
1028 {
1029 if (file_exists(ABSPATH . '.maintenance')) {
1030 return true;
1031 }
1032
1033 return false;
1034 }
1035
1036 function backupGuardDiskFreeSize($dir)
1037 {
1038 if (function_exists('disk_free_space')) {
1039 return convertToReadableSize(@disk_free_space($dir));
1040 }
1041
1042 return 0;
1043 }
1044
1045 function prepareBackupDir()
1046 {
1047 $backupPath = SGConfig::get('SG_BACKUP_DIRECTORY');
1048 $backupOldPath = SGConfig::get('SG_BACKUP_OLD_DIRECTORY');
1049
1050 if (!is_dir($backupPath)) {
1051 // rename old backups folder to new one
1052 if (is_dir($backupOldPath)) {
1053 @rename($backupOldPath, $backupPath);
1054 }
1055
1056 if (!is_dir($backupPath) && !@mkdir($backupPath)) {
1057 throw new SGExceptionMethodNotAllowed('Cannot create folder: ' . $backupPath);
1058 }
1059
1060 if (!@file_put_contents($backupPath . '.htaccess', 'deny from all')) {
1061 throw new SGExceptionMethodNotAllowed('Cannot create htaccess file');
1062 }
1063
1064 if (!@file_put_contents($backupPath . 'index.php', "<?php\n// Silence is golden")) {
1065 throw new SGExceptionMethodNotAllowed('Cannot create index file');
1066 }
1067 }
1068
1069 //check permissions of backups directory
1070 if (!is_writable($backupPath)) {
1071 throw new SGExceptionForbidden('Permission denied. Directory is not writable: ' . $backupPath);
1072 }
1073 }
1074
1075 function checkMinimumRequirements()
1076 {
1077 if (!function_exists('gzdeflate')) {
1078 throw new SGExceptionNotFound('ZLib extension is not loaded.');
1079 }
1080 }