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