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