PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.10
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.10
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / features.php

features.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.10, at includes/features.php

1,089 lines 33.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Anonimization code
4 add_filter('wpdbbkp_process_db_fields', 'bkpforwp_anonimize_database', 10, 3);
5 add_action('wp_ajax_wpdbbkp_check_extract_status', 'wpdbbkp_check_extract_status');
6 function wpdbbkp_check_extract_status(){
7 if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
8 wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
9 return;
10 }
11 $get_progress = get_transient('wpdbbkp_track_progress');
12 if($get_progress==false){
13 wp_send_json_success(['success'=>1,'message'=>esc_html__('Starring Extraction Process', 'wpdbbkp')]);
14 }else{
15 if($get_progress=='Process Completed'){
16 delete_transient('wpdbbkp_track_progress');
17 }
18 wp_send_json_success(['success'=>1,'message'=>$get_progress]);
19 }
20 }
21
22 add_action('wp_ajax_wpdbbkp_upload_site_chunk', 'wpdbbkp_upload_site_chunk');
23 function wpdbbkp_upload_site_chunk() {
24 if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
25 wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
26 return;
27 }
28 if (!isset($_FILES['file']) || !isset($_POST['fileName']) || !isset($_POST['offset'])) {
29 wp_send_json_error("Invalid request.");
30 }
31
32 $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp';
33 if (!file_exists($upload_dir)) {
34 mkdir($upload_dir, 0755, true);
35 }
36
37 $file_name = sanitize_file_name($_POST['fileName']);
38 $file_path = $upload_dir . '/' . $file_name;
39
40 // Append chunk to the file
41 file_put_contents($file_path, file_get_contents($_FILES['file']['tmp_name']), FILE_APPEND);
42
43 wp_send_json_success(esc_html__('Chunk uploaded successfully.', 'wpdbbkp'));
44 }
45 add_action('wp_ajax_wpdbbkp_extract_uploaded_site', 'wpdbbkp_extract_uploaded_site');
46 function wpdbbkp_extract_uploaded_site() {
47
48 try {
49
50 if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
51
52 wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
53
54 return;
55
56 }
57
58 ini_set('max_execution_time', 0); // No time limit
59
60 set_time_limit(0);
61
62 error_log("AJAX extraction started");
63
64
65
66 if (!isset($_POST['fileName'])) {
67
68 throw new Exception("No file provided.");
69
70 }
71
72
73
74 $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp';
75
76 $backup_file = $upload_dir . '/' . sanitize_file_name($_POST['fileName']);
77
78
79
80 if (!file_exists($backup_file)) {
81
82 throw new Exception("Backup file not found.");
83
84 }
85
86
87
88 $extract_path = $upload_dir . '/extracted/';
89
90 $zip = new ZipArchive();
91
92 if ($zip->open($backup_file) !== TRUE) {
93
94 throw new Exception("Failed to open the ZIP archive.");
95
96 }
97
98
99
100 if (!file_exists($extract_path)) {
101
102 mkdir($extract_path, 0755, true);
103
104 }
105
106
107
108 error_log("ZIP contains " . $zip->numFiles . " files");
109
110
111
112
113
114 $root_folder = '';
115
116 for ($i = 0; $i < $zip->numFiles; $i++) {
117
118 $entry = $zip->getNameIndex($i);
119
120 if (preg_match('#^(.*?)/wp-content/#', $entry, $matches)) {
121
122 $root_folder = $matches[1] . '/';
123
124 break;
125
126 }
127
128 }
129
130 error_log("Detected root folder: $root_folder");
131
132
133
134
135
136 for ($i = 0; $i < $zip->numFiles; $i++) {
137
138 $entry = $zip->getNameIndex($i);
139
140 $normalized_entry = str_replace($root_folder, '', $entry);
141
142
143
144 if (preg_match('#^wp-content/plugins/#', $normalized_entry) ||
145
146 preg_match('#^wp-content/themes/#', $normalized_entry) ||
147
148 (pathinfo($normalized_entry, PATHINFO_EXTENSION) === 'sql' && substr_count($normalized_entry, '/') === 0)
149
150 ) {
151
152 if ($zip->extractTo($extract_path, $entry)) {
153
154 set_transient('wpdbbkp_track_progress', $entry, 3600);
155
156 error_log("Successfully extracted: $entry");
157
158
159
160 } else {
161
162 error_log("Extraction failed: $entry");
163
164 }
165
166 }
167
168 }
169
170
171
172 $zip->close();
173
174 set_transient('wpdbbkp_track_progress', 'Extraction of files completed. Now taking live plugins & themes', 3600);
175
176 $plugins_path = $extract_path . '/wp-content/plugins/';
177
178 $themes_path = $extract_path . '/wp-content/themes/';
179
180
181
182 // Move extracted files to wp-content
183
184 $ignore_plugins = ['wp-database-backup'];
185
186
187
188 if (file_exists($plugins_path)) {
189
190 wpdbbkp_move_extracted_files($plugins_path, WP_CONTENT_DIR . '/plugins/', $ignore_plugins);
191
192 } else {
193
194 error_log("Plugins directory not found in extracted folder.");
195
196 }
197
198
199
200 if (file_exists($themes_path)) {
201
202 wpdbbkp_move_extracted_files($themes_path, WP_CONTENT_DIR . '/themes/');
203
204 } else {
205
206 error_log("Themes directory not found in extracted folder OR extraction failed.");
207
208 }
209
210
211
212 // Process SQL file if found
213
214 $sql_file = wpdbbkp_find_sql_file($extract_path);
215
216 if ($sql_file) {
217
218 set_transient('wpdbbkp_track_progress', 'Migrating Database...', 3600);
219
220 $wp_config_prefix = wpdbbkp_get_wp_config_table_prefix();
221 error_log("Sql File .".$sql_file);
222 wpdbbkp_update_sql_table_prefix($sql_file);
223
224 wpdbbkp_restore_database($sql_file);
225
226 } else {
227
228 error_log("No SQL file found in extracted folder.");
229
230 }
231
232
233
234 error_log("Extraction completed successfully");
235
236 $wpdbbkp_folder = WP_CONTENT_DIR . '/uploads/wpdbbkp';
237 if (file_exists($wpdbbkp_folder)) {
238 wpdbbkp_delete_folder($wpdbbkp_folder);
239 error_log("Deleted wpdbbkp folder after successful migration.");
240 }
241 set_transient('wpdbbkp_track_progress', 'Process Completed', 3600);
242 wp_send_json_success(esc_html__('Plugins, Themes, Database & Table Prefix Updated!', 'wpdbbkp'));
243
244
245
246 } catch (Exception $e) {
247
248 error_log("Error: " . $e->getMessage());
249
250 wp_send_json_error($e->getMessage());
251
252 }
253
254 }
255
256 function wpdbbkp_delete_folder($folder_path) {
257 if (!is_dir($folder_path)) {
258 return;
259 }
260
261 $files = array_diff(scandir($folder_path), array('.', '..'));
262
263 foreach ($files as $file) {
264 $file_path = $folder_path . DIRECTORY_SEPARATOR . $file;
265 if (is_dir($file_path)) {
266 wpdbbkp_delete_folder($file_path);
267 } else {
268 set_transient('wpdbbkp_track_progress', 'Cleaning : '.$file_path, 3600);
269 unlink($file_path);
270 }
271 }
272
273 rmdir($folder_path);
274 }
275
276
277
278
279 function wpdbbkp_find_sql_file($directory) {
280
281 $files = scandir($directory);
282
283 foreach ($files as $file) {
284
285 if (pathinfo($file, PATHINFO_EXTENSION) === 'sql') {
286
287 return $directory . '/' . $file;
288
289 }
290
291 }
292
293 return false;
294
295 }
296
297
298
299 function wpdbbkp_get_wp_config_table_prefix() {
300
301 $config_file = ABSPATH . 'wp-config.php';
302 error_log("[ERROR] Config Path: $config_file");
303 if (!file_exists($config_file)) {
304
305 return 'wp_';
306
307 }
308
309 $config_contents = file_get_contents($config_file);
310
311 if (preg_match("/\$table_prefix\s*=\s*'([^']+)'/", $config_contents, $matches)) {
312
313 return $matches[1];
314
315 }
316
317 return 'wp_';
318
319 }
320 function wpdbbkp_update_sql_table_prefix($sql_file) {
321 if (!file_exists($sql_file)) {
322 die("❌ ERROR: SQL file not found: $sql_file\n");
323 }
324
325 $sql_content = file_get_contents($sql_file);
326
327 // Find wp-config.php
328 $wp_config_path = find_wp_config();
329 if (!$wp_config_path) {
330 die("❌ ERROR: wp-config.php not found!\n");
331 }
332
333 // Extract table prefix from wp-config.php
334 $config_content = file_get_contents($wp_config_path);
335 if (preg_match("/\\\$table_prefix\s*=\s*['\"]([^'\"]+)['\"]\s*;/", $config_content, $matches)) {
336 $new_prefix = $matches[1];
337 echo "�
338 SUCCESS: Extracted new prefix from wp-config.php: '$new_prefix'\n";
339 } else {
340 die(" ERROR: Could not extract table prefix from wp-config.php!\n");
341 }
342
343 // Detect old prefix from the SQL file
344 if (preg_match("/(CREATE TABLE|INSERT INTO|ALTER TABLE|UPDATE|DELETE FROM)\s+[`']?([a-zA-Z0-9]+?_)/i", $sql_content, $matches)) {
345 $old_prefix = $matches[2];
346 echo "�
347 SUCCESS: Found old prefix in SQL file: '$old_prefix'\n";
348 } else {
349 die("❌ ERROR: No table prefix found in the SQL file!\n");
350 }
351
352 // Prevent replacing if old prefix is same as new prefix
353 if ($old_prefix === $new_prefix) {
354 die("ℹ️ INFO: Old and new prefixes are the same ('$old_prefix'), no changes needed.\n");
355 }
356
357 // Replace all occurrences of the old prefix with the new one
358 $sql_content = preg_replace("/\b" . preg_quote($old_prefix, '/') . "/i", $new_prefix, $sql_content);
359
360 // Write the updated content back to the SQL file
361 file_put_contents($sql_file, $sql_content);
362
363 echo "�
364 SUCCESS: Table prefix updated from '$old_prefix' to '$new_prefix' in $sql_file!\n";
365 }
366
367 /**
368 * Finds the root wp-config.php file by checking parent directories.
369 */
370 function find_wp_config() {
371 $dir = __DIR__;
372
373 while ($dir !== dirname($dir)) { // Keep going up until the root
374 $config_path = $dir . '/wp-config.php';
375 if (file_exists($config_path)) {
376 return $config_path;
377 }
378 $dir = dirname($dir);
379 }
380
381 return false; // wp-config.php not found
382 }
383
384
385
386
387
388
389
390
391 function wpdbbkp_move_extracted_files($source, $destination, $ignore = []) {
392 if (!file_exists($source)) return;
393
394 if (!file_exists($destination)) {
395 mkdir($destination, 0755, true);
396 }
397
398 $files = array_diff(scandir($source), array('.', '..'));
399 foreach ($files as $file) {
400 $srcFile = rtrim($source, '/') . '/' . $file;
401 $destFile = rtrim($destination, '/') . '/' . $file;
402
403 if (in_array($file, $ignore)) {
404 continue;
405 }
406
407 set_transient('wpdbbkp_track_progress', 'Copying: ' . $destFile, 3600);
408
409 if (is_dir($srcFile)) {
410 if (file_exists($destFile)) {
411 wpdbbkp_delete_directory($destFile);
412 }
413 rename($srcFile, $destFile);
414 } else {
415 if (file_exists($destFile)) {
416 unlink($destFile);
417 }
418 rename($srcFile, $destFile);
419 }
420 }
421 }
422
423 function wpdbbkp_delete_directory($dir) {
424 if (!is_dir($dir)) {
425 return unlink($dir);
426 }
427
428 $files = array_diff(scandir($dir), array('.', '..'));
429 foreach ($files as $file) {
430 $filePath = $dir . DIRECTORY_SEPARATOR . $file;
431 if (is_dir($filePath)) {
432 wpdbbkp_delete_directory($filePath);
433 } else {
434 unlink($filePath);
435 }
436 }
437
438 return rmdir($dir);
439 }
440
441
442 function wpdbbkp_restore_database($sql_file) {
443
444 global $wpdb;
445
446 $new_site_url = get_site_url();
447
448
449
450 if (!file_exists($sql_file)) {
451
452 error_log("[ERROR] SQL file not found: $sql_file");
453
454 return false;
455
456 }
457
458
459
460 $sql_content = file_get_contents($sql_file);
461
462 if (!$sql_content) {
463
464 error_log("[ERROR] Failed to read SQL file: $sql_file");
465
466 return false;
467
468 }
469
470
471
472 // Fetch old site URL from wp_options before updating
473
474 $old_site_url = $wpdb->get_var("SELECT option_value FROM {$wpdb->options} WHERE option_name = 'siteurl'");
475
476
477
478 if (!$old_site_url) {
479
480 error_log("[ERROR] Failed to fetch old site URL.");
481
482 return false;
483
484 }
485
486
487
488 error_log("[INFO] Old Site URL detected: $old_site_url");
489
490
491
492 // Split SQL statements properly
493
494 $queries = preg_split('/;\s*\n/', $sql_content, -1, PREG_SPLIT_NO_EMPTY);
495
496
497
498 // Tables to exclude (e.g., Users and Usermeta for security reasons)
499
500 $excluded_tables = ['users','usermeta','options'];
501
502
503
504 $wpdb->query('SET foreign_key_checks = 0'); // Disable FK checks
505
506 $wpdb->query('START TRANSACTION'); // Start transaction
507
508
509
510 try {
511
512 foreach ($queries as $query) {
513
514 $query = trim($query);
515
516 if (empty($query)) continue;
517
518 $query = str_replace('options','option_tmp',$query);
519
520 // Check for CREATE TABLE and extract table name
521
522 if (preg_match('/CREATE TABLE `([^`]*)`/', $query, $matches)) {
523
524 $table_name = $matches[1];
525
526
527
528 // Skip excluded tables
529
530 foreach ($excluded_tables as $excluded) {
531
532 if (strpos($table_name, $excluded) !== false) {
533
534 error_log("[SKIPPED] Table excluded: $table_name");
535
536 continue 2;
537
538 }
539
540 }
541
542
543
544 // Drop existing table before restoring
545
546 $wpdb->query("DROP TABLE IF EXISTS `$table_name`");
547
548 error_log("[DROPPED] Table: $table_name");
549
550 }
551
552
553
554 // Skip execution of queries for excluded tables
555
556 foreach ($excluded_tables as $excluded) {
557
558 if (strpos($query, $excluded) !== false) {
559
560 error_log("[SKIPPED] Query contains excluded table reference.");
561
562 continue 2;
563
564 }
565
566 }
567
568
569
570 // Execute the query
571
572 $result = $wpdb->query($query);
573
574 if ($result === false) {
575
576 throw new Exception("[ERROR] Failed to execute query: " . $wpdb->last_error);
577
578 }
579
580 }
581
582
583
584
585
586
587
588 // **Find and replace old URLs in all tables**
589
590 $tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
591
592 foreach ($tables as $table) {
593
594 $table_name = $table[0];
595
596
597
598 // Skip excluded tables
599
600 foreach ($excluded_tables as $excluded) {
601
602 if (strpos($table_name, $excluded) !== false) {
603
604 error_log("[SKIPPED] URL Replacement in: $table_name");
605
606 continue 2;
607
608 }
609
610 }
611
612
613
614 // Get all columns for the table
615
616 $columns = $wpdb->get_results("SHOW COLUMNS FROM `$table_name`", ARRAY_A);
617
618 foreach ($columns as $column) {
619
620 $column_name = $column['Field'];
621
622
623
624 // Update all occurrences of old URL in text-based columns
625
626 $wpdb->query(
627
628 $wpdb->prepare(
629
630 "UPDATE `$table_name` SET `$column_name` = REPLACE(`$column_name`, %s, %s) WHERE `$column_name` LIKE %s",
631
632 $old_site_url,
633
634 $new_site_url,
635
636 '%' . $wpdb->esc_like($old_site_url) . '%'
637
638 )
639
640 );
641
642 }
643
644 }
645
646
647
648 $blogname = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'blogname'");
649
650 if ($blogname) {
651
652 $wpdb->query(
653
654 $wpdb->prepare(
655
656 "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'blogname'",
657
658 $blogname
659
660 )
661
662 );
663
664 error_log("[SUCCESS] Updated wp_options with blogname from option_tmp");
665
666 }
667 $active_plugins = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'active_plugins'");
668
669 if ($active_plugins) {
670
671 $wpdb->query(
672
673 $wpdb->prepare(
674
675 "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'active_plugins'",
676
677 $active_plugins
678
679 )
680
681 );
682
683 error_log("[SUCCESS] Updated wp_options with active_plugins from option_tmp");
684
685 }
686
687 $wpdb->query('COMMIT'); // Commit transaction
688
689 error_log("[SUCCESS] Database restoration and URL replacement completed from: $sql_file");
690
691 return true;
692
693
694
695 } catch (Exception $e) {
696
697 $wpdb->query('ROLLBACK'); // Rollback in case of an error
698
699 error_log($e->getMessage());
700
701 return false;
702
703 } finally {
704
705 $wpdb->query('SET foreign_key_checks = 1'); // Re-enable FK checks
706
707 }
708
709 }
710 function bkpforwp_anonimize_database($value, $table, $column)
711 {
712 $enable_anonymization = get_option('bkpforwp_enable_anonymization', false);
713 $anonymization_type = get_option('bkpforwp_anonymization_type', false);
714 $enable_backup_encryption = get_option('bkpforwp_enable_backup_encryption', false);
715 $anonymization_pass = get_option('bkpforwp_anonymization_pass', '');
716
717
718 if (isset($enable_anonymization) && $enable_anonymization == 1) {
719 global $wpdb;
720 $bkpforwp_process_table = array($wpdb->prefix . 'options', $wpdb->prefix . 'users', $wpdb->prefix . 'usermeta', $wpdb->prefix . 'wc_customer_lookup', $wpdb->prefix . 'edd_customers', $wpdb->prefix . 'edd_customermeta');
721 $bkpforwp_process_cols = array('mailserver_pass', 'mailserver_login', 'user_email', 'email', 'user_url', 'nickname', 'name', 'twitter', 'facebook', 'instagram', 'phone', 'mobile', 'address', 'city', 'zip', 'pincode', 'user_login', 'postcode', 'state', 'user_ip', 'ip_address');
722
723 //Masking Logic
724 if (isset($anonymization_type) && $anonymization_type == 'masked_data') {
725 if (in_array($table, $bkpforwp_process_table)) {
726 $check_str = implode(',', $bkpforwp_process_cols);
727 if (stripos($check_str, $column) !== false) {
728 return str_replace($value, str_repeat('*', strlen($value)), $value);
729 }
730 }
731 }
732 //FakeData Logic
733
734 if (isset($anonymization_type) && $anonymization_type == 'fake_data') {
735 if (function_exists('wp_privacy_anonymize_data')) {
736 $bkpforwp_process_email = implode(',', array('email', 'user_email'));
737 $bkpforwp_process_url = implode(',', array('url', 'user_url', 'twitter', 'facebook', 'instagram'));
738 $bkpforwp_process_ip = implode(',', array('user_ip', 'ip_address'));
739 $bkpforwp_process_text = implode(',', array('nickname', 'name', 'address', 'phone', 'mobile', 'city', 'zip', 'pincode', 'user_login', 'postcode', 'state'));
740
741 if (in_array($table, $bkpforwp_process_table)) {
742
743 //For email
744 if (stripos($bkpforwp_process_email, $column) !== false) {
745 return str_replace($value, wp_privacy_anonymize_data('email', $value), $value);
746 }
747
748 if (stripos($bkpforwp_process_url, $column) !== false) {
749 return str_replace($value, wp_privacy_anonymize_data('url', $value), $value);
750 }
751
752 if (stripos($bkpforwp_process_ip, $column) !== false) {
753 return str_replace($value, wp_privacy_anonymize_data('ip', $value), $value);
754 }
755
756 if (stripos($bkpforwp_process_text, $column) !== false) {
757 return str_replace($value, wp_privacy_anonymize_data('text', $value), $value);
758 }
759
760 }
761
762 return $value;
763
764 } else {
765 if (in_array($table, $bkpforwp_process_table)) {
766 $check_str = implode(',', $bkpforwp_process_cols);
767 if (stripos($check_str, $column) !== false) {
768 return str_replace($value, str_repeat('*', strlen($value)), $value);
769 }
770 }
771 }
772
773 }
774
775 if (isset($anonymization_type) && $anonymization_type == 'encrypted_data' && !empty($anonymization_pass)) {
776 require_once 'class-symmetric-encryption.php';
777
778 if (in_array($table, $bkpforwp_process_table)) {
779 $check_str = implode(',', $bkpforwp_process_cols);
780 if (stripos($check_str, $column) !== false) {
781 $enc_pass = $anonymization_pass;
782 $encryption = new SymmetricEncryption();
783 return str_replace($value, '<==>' . $encryption->encrypt($value, $enc_pass, $enc_pass) . '<==>', $value);
784 }
785
786 }
787
788 }
789
790 }
791 return $value;
792 }
793
794 add_filter('wpdbbkp_sql_query_restore', 'bkpforwp_sql_query_restore', 1);
795 function bkpforwp_sql_query_restore($sql_query)
796 {
797 $anonymization_type = get_option('bkpforwp_anonymization_type', false);
798 $anonymization_pass = get_option('bkpforwp_anonymization_pass', '');
799 if (isset($anonymization_type) && $anonymization_type == 'encrypted_data' && !empty($anonymization_pass)) {
800
801 $pattern = '/<==>(.*?)<==>/i';
802 return preg_replace_callback($pattern, 'bkpforwp_sql_restore_replace', $sql_query);
803 }
804 return $sql_query;
805 }
806
807 function bkpforwp_sql_restore_replace($matches)
808 {
809 $anonymization_pass = get_option('bkpforwp_anonymization_pass', '');
810 $enc_pass = isset($anonymization_pass) ? $anonymization_pass : false;
811 if ($enc_pass) {
812 require_once 'class-symmetric-encryption.php';
813 $encryption = new SymmetricEncryption();
814 return $encryption->decrypt($matches[0], $enc_pass, $enc_pass);
815 }
816 return $matches[0];
817 }
818
819 add_action('wpdbbkp_database_backup_options', 'bkpforwp_database_backup_options');
820 function bkpforwp_database_backup_options()
821 {
822 $settings = get_option('wp_db_backup_options');
823 $autobackup_days = isset($settings['autobackup_days']) ? implode(',', $settings['autobackup_days']) : ',';
824 $autobackup_time = isset($settings['autobackup_time']) ? $settings['autobackup_time'] : '';
825 $autobackup_date = isset($settings['autobackup_date']) ? $settings['autobackup_date'] : '';
826 ?>
827
828
829 <div class="row form-group autobackup_frequency_pro" style="display:none"><label
830 class="col-sm-12 autobackup_daily_pro">We will automatically backup at 00:00 AM daily. <b><a
831 href="javascript:modify_backup_frequency();">Change Back Frequency Timings</a></b></label></div>
832 <div class="row form-group autobackup_frequency_pro" style="display:none"><label
833 class="col-sm-12 autobackup_weekly_pro">We will automatically backup every Sunday on weekly basis. <b><a
834 href="javascript:modify_backup_frequency();">Change Back Frequency Timings</a></b></label></div>
835 <div class="row form-group autobackup_frequency_pro" style="display:none"><label
836 class="col-sm-12 autobackup_monthly_pro">We will automatically backup on 1st on Monday on monthly basis. <b><a
837 href="javascript:modify_backup_frequency();">Change Back Frequency Timings</a></b></label></div>
838
839
840 <div class="row form-group autobackup_days database_autobackup" style="display:none">
841 <label class="col-sm-3" for="autobackup_days"><?php esc_html_e('Database Backup Days', 'wpdbbkp'); ?></label>
842 <div class="col-sm-9">
843 <select id="autobackup_days" class="form-control bkpforwp_multiselect"
844 name="wp_db_backup_options[autobackup_days][]" multiple>
845 <option value="Mon" <?php if (strpos($autobackup_days, 'Mon') !== false) {
846 echo 'selected';
847 } ?>>
848 <?php esc_html_e('Monday', 'wpdbbkp'); ?></option>
849 <option value="Tue" <?php if (strpos($autobackup_days, 'Tue') !== false) {
850 echo 'selected';
851 } ?>>
852 <?php esc_html_e('Tuesday', 'wpdbbkp'); ?></option>
853 <option value="Wed" <?php if (strpos($autobackup_days, 'Wed') !== false) {
854 echo 'selected';
855 } ?>>
856 <?php esc_html_e('Wednesday', 'wpdbbkp'); ?></option>
857 <option value="Thu" <?php if (strpos($autobackup_days, 'Thu') !== false) {
858 echo 'selected';
859 } ?>>
860 <?php esc_html_e('Thursday', 'wpdbbkp'); ?></option>
861 <option value="Fri" <?php if (strpos($autobackup_days, 'Fri') !== false) {
862 echo 'selected';
863 } ?>>
864 <?php esc_html_e('Friday', 'wpdbbkp'); ?></option>
865 <option value="Sat" <?php if (strpos($autobackup_days, 'Sat') !== false) {
866 echo 'selected';
867 } ?>>
868 <?php esc_html_e('Saturday', 'wpdbbkp'); ?></option>
869 <option value="Sun" <?php if (strpos($autobackup_days, 'Sun') !== false) {
870 echo 'selected';
871 } ?>>
872 <?php esc_html_e('Sunday', 'wpdbbkp'); ?></option>
873 </select>
874 </div>
875 </div>
876 <div class="row form-group autobackup_date database_autobackup" style="display:none">
877 <label class="col-sm-3" for="autobackup_date"><?php esc_html_e('Database Backup Date', 'wpdbbkp'); ?></label>
878 <div class="col-sm-9">
879 <input type="date" id="autobackup_date" value="<?php echo esc_attr($autobackup_date); ?>"
880 class="form-control bkpforwp_multiselect" name="wp_db_backup_options[autobackup_date]">
881 </div>
882 </div>
883 <div class="row form-group autobackup_time database_autobackup" style="display:none">
884 <label class="col-sm-3" for="autobackup_time"><?php esc_html_e('Database Backup Time', 'wpdbbkp'); ?></label>
885 <div class="col-sm-9">
886 <input type="time" id="autobackup_time" value="<?php echo esc_attr($autobackup_time); ?>"
887 class="form-control bkpforwp_multiselect" name="wp_db_backup_options[autobackup_time]">
888 </div>
889 </div>
890
891 <?php
892 }
893
894 add_action('wpdbbkp_full_backup_options', 'bkpforwp_full_backup_options');
895 function bkpforwp_full_backup_options()
896 {
897
898 $settings = get_option('wp_db_backup_options');
899 $autobackup_days = isset($settings['autobackup_full_days']) ? implode(',', $settings['autobackup_full_days']) : ',';
900 $autobackup_time = isset($settings['autobackup_full_time']) ? $settings['autobackup_full_time'] : '';
901 $autobackup_date = isset($settings['autobackup_full_date']) ? $settings['autobackup_full_date'] : '';
902 $autobackup_date = isset($settings['autobackup_full_date']) ? $settings['autobackup_full_date'] : '';
903 $senable_exact_backup_time = get_option('bkpforwp_enable_exact_backup_time', false);
904 if ($senable_exact_backup_time) {
905 ?>
906 <div class="row form-group autobackup_full_days full_autobackup" style="display:none">
907 <label class="col-sm-3" for="autobackup_full_days"><?php esc_html_e('Full Backup Days', 'wpdbbkp'); ?></label>
908 <div class="col-sm-9">
909 <select id="autobackup_full_days" class="form-control bkpforwp_multiselect"
910 name="wp_db_backup_options[autobackup_full_days][]" multiple>
911 <option value="Mon" <?php if (strpos($autobackup_days, 'Mon') !== false) {
912 echo 'selected';
913 } ?>>
914 <?php esc_html_e('Monday', 'wpdbbkp'); ?></option>
915 <option value="Tue" <?php if (strpos($autobackup_days, 'Tue') !== false) {
916 echo 'selected';
917 } ?>>
918 <?php esc_html_e('Tuesday', 'wpdbbkp'); ?></option>
919 <option value="Wed" <?php if (strpos($autobackup_days, 'Wed') !== false) {
920 echo 'selected';
921 } ?>>
922 <?php esc_html_e('Wednesday', 'wpdbbkp'); ?></option>
923 <option value="Thu" <?php if (strpos($autobackup_days, 'Thu') !== false) {
924 echo 'selected';
925 } ?>>
926 <?php esc_html_e('Thursday', 'wpdbbkp'); ?></option>
927 <option value="Fri" <?php if (strpos($autobackup_days, 'Fri') !== false) {
928 echo 'selected';
929 } ?>>
930 <?php esc_html_e('Friday', 'wpdbbkp'); ?></option>
931 <option value="Sat" <?php if (strpos($autobackup_days, 'Sat') !== false) {
932 echo 'selected';
933 } ?>>
934 <?php esc_html_e('Saturday', 'wpdbbkp'); ?></option>
935 <option value="Sun" <?php if (strpos($autobackup_days, 'Sun') !== false) {
936 echo 'selected';
937 } ?>>
938 <?php esc_html_e('Sunday', 'wpdbbkp'); ?></option>
939 </select>
940 </div>
941 </div>
942 <div class="row form-group autobackup_full_date full_autobackup" style="display:none">
943 <label class="col-sm-3" for="autobackup_full_date"><?php esc_html_e('Full Backup Date', 'wpdbbkp'); ?></label>
944 <div class="col-sm-9">
945 <input type="date" id="autobackup_full_date" value="<?php echo esc_attr($autobackup_date); ?>" class="form-control"
946 name="wp_db_backup_options[autobackup_full_date]">
947 </div>
948 </div>
949 <div class="row form-group autobackup_full_time full_autobackup" style="display:none">
950 <label class="col-sm-3" for="autobackup_full_time"><?php esc_html_e('Full Backup Time', 'wpdbbkp'); ?></label>
951 <div class="col-sm-9">
952 <input type="time" id="autobackup_full_time" value="<?php echo esc_attr($autobackup_time); ?>" class="form-control"
953 name="wp_db_backup_options[autobackup_full_time]">
954 </div>
955 </div>
956 <?php
957 }
958 }
959
960 add_filter('wpdbbkp_fullback_cron_condition', 'bkpforwp_fullback_cron_condition');
961 function bkpforwp_fullback_cron_condition($value)
962 {
963 $options_settings = get_option('wp_db_backup_options', false);
964
965 $senable_exact_backup_time = get_option('bkpforwp_enable_exact_backup_time', false);
966 if (!$senable_exact_backup_time) {
967 return $value;
968 }
969 if (wp_doing_cron() && $options_settings && isset($options_settings['enable_autobackups']) && $options_settings['enable_autobackups'] == 1 && isset($options_settings['full_autobackup_frequency'])) {
970 if ($options_settings['full_autobackup_frequency'] == 'daily' && isset($options_settings['autobackup_full_time']) && $options_settings['autobackup_full_time']) {
971 if ($options_settings['autobackup_full_time'] < gmdate("H:i") || $options_settings['autobackup_full_time'] > gmdate("H:i", strtotime('+30 minutes', gmdate("H:i")))) {
972 $value = false;
973 }
974 }
975 if ($options_settings['full_autobackup_frequency'] == 'weekly' && isset($options_settings['autobackup_full_time']) && $options_settings['autobackup_full_time'] && isset($options_settings['autobackup_full_days'])) {
976 $current_day = gmdate('M');
977 $current_time = gmdate('H:i');
978 $allowed_days = $options_settings['autobackup_full_days'];
979 if (!in_array($current_day, $allowed_days) || ($options_settings['autobackup_full_time'] < $current_time) || $options_settings['autobackup_full_time'] > gmdate("H:i", strtotime('+30 minutes', $current_time))) {
980 $value = false;
981 }
982 }
983 if ($options_settings['full_autobackup_frequency'] == 'monthly' && isset($options_settings['autobackup_full_time']) && $options_settings['autobackup_full_time'] && isset($options_settings['autobackup_full_date'])) {
984 $current_date = gmdate('d');
985 $current_time = gmdate('H:i');
986 $allowed_date = gmdate('d', strtotime($options_settings['autobackup_full_date']));
987 if (($allowed_date != $current_date) || ($options_settings['autobackup_full_time'] < $current_time || $options_settings['autobackup_full_time'] > gmdate("H:i", strtotime('+30 minutes', $current_time)))) {
988 $value = false;
989 }
990 }
991 }
992 return $value;
993 }
994
995 add_filter('wpdbbkp_dbback_cron_condition', 'bkpforwp_dbback_cron_condition');
996 function bkpforwp_dbback_cron_condition($value)
997 {
998 $options_settings = get_option('wp_db_backup_options', false);
999 if (wp_doing_cron() && $options_settings && isset($options_settings['enable_autobackups']) && $options_settings['enable_autobackups'] == 1 && isset($options_settings['autobackup_frequency'])) {
1000 if ($options_settings['autobackup_frequency'] == 'daily' && isset($options_settings['autobackup_time'])) {
1001 if ($options_settings['autobackup_time'] < gmdate("H:i") || $options_settings['autobackup_time'] > gmdate("H:i", strtotime('+30 minutes', gmdate("H:i")))) {
1002 $value = false;
1003 }
1004 }
1005 if ($options_settings['autobackup_frequency'] == 'weekly' && isset($options_settings['autobackup_time']) && isset($options_settings['autobackup_days'])) {
1006 $current_day = gmdate('M');
1007 $current_time = gmdate('H:i');
1008 $allowed_days = $options_settings['autobackup_days'];
1009 if (!in_array($current_day, $allowed_days) || ($options_settings['autobackup_time'] < $current_time || $options_settings['autobackup_time'] > gmdate("H:i", strtotime('+30 minutes', $current_time)))) {
1010 $value = false;
1011 }
1012 }
1013 if ($options_settings['autobackup_frequency'] == 'monthly' && isset($options_settings['autobackup_time']) && isset($options_settings['autobackup_date'])) {
1014 $current_date = gmdate('d');
1015 $current_time = gmdate('H:i');
1016 $allowed_date = gmdate('d', strtotime($options_settings['autobackup_date']));
1017 if (($allowed_date != $current_date) || ($options_settings['autobackup_time'] < $current_time || $options_settings['autobackup_time'] > gmdate("H:i", strtotime('+30 minutes', $current_time)))) {
1018 $value = false;
1019 }
1020 }
1021 }
1022 return $value;
1023 }
1024
1025 add_filter('wpdbbkp_dbback_cron_frequency', 'bkpforwp_dbback_cron_frequency');
1026
1027 function bkpforwp_dbback_cron_frequency($value)
1028 {
1029 if (wp_doing_cron()) {
1030 $options = get_option('wp_db_backup_options');
1031 if (isset($options['autobackup_full_time']) && !empty($options['autobackup_full_time'])) {
1032 $value = 'thirty_minutes';
1033 }
1034 }
1035 return $value;
1036 }
1037
1038 /**
1039 * Function to force the new .htaccess file to fix the backup folder protection
1040 */
1041 function wpdbbkp_fix_htaccess_on_update()
1042 {
1043 static $wpdbbkp_htaccess_fix = false;
1044
1045 if (!$wpdbbkp_htaccess_fix && version_compare(WPDB_VERSION, '7.4', '>=')) {
1046 $wpdbbkp_htaccess_fix = true;
1047 $option_name = 'wpdbbkp_htaccess_fix';
1048 if (get_option($option_name, false)) {
1049 return; // Exit if already fixed
1050 }
1051
1052 // Initialize WP Filesystem
1053 global $wp_filesystem;
1054
1055 if (!function_exists('WP_Filesystem')) {
1056 require_once ABSPATH . 'wp-admin/includes/file.php';
1057 }
1058
1059 if (!WP_Filesystem()) {
1060 return;
1061 }
1062 // Define the .htaccess content
1063 $htaccess_content = "
1064 # Disable public access to this folder
1065 <IfModule mod_authz_core.c>
1066 Require all denied
1067 </IfModule>
1068
1069 <IfModule !mod_authz_core.c>
1070 Deny from all
1071 </IfModule>
1072 ";
1073
1074 $path_info = wp_upload_dir();
1075 $backup_folder = $path_info['basedir'] . '/' . WPDB_BACKUPS_DIR . '/';
1076 $htaccess_file = trailingslashit($backup_folder) . '.htaccess';
1077
1078 if ($wp_filesystem->exists($htaccess_file)) {
1079 $wp_filesystem->delete($htaccess_file);
1080 }
1081
1082 if (!$wp_filesystem->put_contents($htaccess_file, $htaccess_content, FS_CHMOD_FILE)) {
1083 return;
1084 }
1085 update_option($option_name, time(), false);
1086 }
1087 }
1088
1089 add_action('admin_init', 'wpdbbkp_fix_htaccess_on_update');