| @@ -1,780 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - exit; | |
| 4 | -} | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Log backup/restore messages when WP_DEBUG_LOG is enabled. | |
| 8 | - * | |
| 9 | - * @param string $message Log message. | |
| 10 | - */ | |
| 11 | -function wpdbbkp_log( $message ) { | |
| 12 | - if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 13 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional backup operation logging. | |
| 14 | - error_log( $message ); | |
| 15 | - } | |
| 16 | -} | |
| 17 | - | |
| 18 | 3 | // Anonimization code |
| 19 | 4 | add_filter('wpdbbkp_process_db_fields', 'bkpforwp_anonimize_database', 10, 3); |
| 20 | -add_action('wp_ajax_wpdbbkp_check_extract_status', 'wpdbbkp_check_extract_status'); | |
| 21 | -function wpdbbkp_check_extract_status(){ | |
| 22 | - if ( ! isset( $_POST['wpdbbkp_admin_security_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['wpdbbkp_admin_security_nonce'] ), 'wpdbbkp_ajax_check_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce. | |
| 23 | - wp_send_json_success( esc_html__( 'Invalid nonce check.', 'wpdbbkp' ) ); | |
| 24 | - return; | |
| 25 | - } | |
| 26 | - $get_progress = get_transient('wpdbbkp_track_progress'); | |
| 27 | - if($get_progress==false){ | |
| 28 | - wp_send_json_success(['success'=>1,'message'=>esc_html__('Starring Extraction Process', 'wpdbbkp')]); | |
| 29 | - }else{ | |
| 30 | - if($get_progress=='Process Completed'){ | |
| 31 | - delete_transient('wpdbbkp_track_progress'); | |
| 32 | - } | |
| 33 | - wp_send_json_success(['success'=>1,'message'=>$get_progress]); | |
| 34 | - } | |
| 35 | -} | |
| 36 | 5 | |
| 37 | -add_action('wp_ajax_wpdbbkp_upload_site_chunk', 'wpdbbkp_upload_site_chunk'); | |
| 38 | -function wpdbbkp_upload_site_chunk() { | |
| 39 | - if ( ! isset( $_POST['wpdbbkp_admin_security_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['wpdbbkp_admin_security_nonce'] ), 'wpdbbkp_ajax_check_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce. | |
| 40 | - wp_send_json_success( esc_html__( 'Invalid nonce check.', 'wpdbbkp' ) ); | |
| 41 | - return; | |
| 42 | - } | |
| 43 | - if ( ! isset( $_FILES['file'], $_POST['fileName'], $_POST['offset'] ) ) { | |
| 44 | - wp_send_json_error( esc_html__( 'Invalid request.', 'wpdbbkp' ) ); | |
| 45 | - } | |
| 46 | - | |
| 47 | - $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp'; | |
| 48 | - wp_mkdir_p( $upload_dir ); | |
| 49 | - | |
| 50 | - $file_name = sanitize_file_name( wp_unslash( $_POST['fileName'] ) ); | |
| 51 | - $file_path = $upload_dir . '/' . $file_name; | |
| 52 | - | |
| 53 | - $chunk_tmp = isset( $_FILES['file']['tmp_name'] ) ? $_FILES['file']['tmp_name'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated with is_uploaded_file(). | |
| 54 | - | |
| 55 | - if ( empty( $chunk_tmp ) || ! is_uploaded_file( $chunk_tmp ) ) { | |
| 56 | - wp_send_json_error( esc_html__( 'Invalid upload.', 'wpdbbkp' ) ); | |
| 57 | - } | |
| 58 | - | |
| 59 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- Appending validated upload chunk. | |
| 60 | - file_put_contents( $file_path, file_get_contents( $chunk_tmp ), FILE_APPEND ); | |
| 61 | - | |
| 62 | - wp_send_json_success(esc_html__('Chunk uploaded successfully.', 'wpdbbkp')); | |
| 63 | -} | |
| 64 | -add_action('wp_ajax_wpdbbkp_extract_uploaded_site', 'wpdbbkp_extract_uploaded_site'); | |
| 65 | -function wpdbbkp_extract_uploaded_site() { | |
| 66 | - | |
| 67 | - try { | |
| 68 | - | |
| 69 | - if ( ! isset( $_POST['wpdbbkp_admin_security_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['wpdbbkp_admin_security_nonce'] ), 'wpdbbkp_ajax_check_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce. | |
| 70 | - | |
| 71 | - wp_send_json_success( esc_html__( 'Invalid nonce check.', 'wpdbbkp' ) ); | |
| 72 | - | |
| 73 | - return; | |
| 74 | - | |
| 75 | - } | |
| 76 | - | |
| 77 | - // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Required for large backup extraction. | |
| 78 | - ini_set( 'max_execution_time', '0' ); | |
| 79 | - | |
| 80 | - // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Required for large backup extraction. | |
| 81 | - set_time_limit( 0 ); | |
| 82 | - | |
| 83 | - wpdbbkp_log( 'AJAX extraction started' ); | |
| 84 | - | |
| 85 | - | |
| 86 | - | |
| 87 | - if (!isset($_POST['fileName'])) { | |
| 88 | - | |
| 89 | - throw new Exception("No file provided."); | |
| 90 | - | |
| 91 | - } | |
| 92 | - | |
| 93 | - | |
| 94 | - | |
| 95 | - $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp'; | |
| 96 | - | |
| 97 | - $backup_file = $upload_dir . '/' . sanitize_file_name( wp_unslash( $_POST['fileName'] ) ); | |
| 98 | - | |
| 99 | - | |
| 100 | - | |
| 101 | - if (!file_exists($backup_file)) { | |
| 102 | - | |
| 103 | - throw new Exception("Backup file not found."); | |
| 104 | - | |
| 105 | - } | |
| 106 | - | |
| 107 | - | |
| 108 | - | |
| 109 | - $extract_path = $upload_dir . '/extracted/'; | |
| 110 | - | |
| 111 | - $zip = new ZipArchive(); | |
| 112 | - | |
| 113 | - if ($zip->open($backup_file) !== TRUE) { | |
| 114 | - | |
| 115 | - throw new Exception("Failed to open the ZIP archive."); | |
| 116 | - | |
| 117 | - } | |
| 118 | - | |
| 119 | - | |
| 120 | - | |
| 121 | - wp_mkdir_p( $extract_path ); | |
| 122 | - | |
| 123 | - | |
| 124 | - | |
| 125 | - wpdbbkp_log("ZIP contains " . $zip->numFiles . " files"); | |
| 126 | - | |
| 127 | - | |
| 128 | - | |
| 129 | - | |
| 130 | - | |
| 131 | - $root_folder = ''; | |
| 132 | - | |
| 133 | - for ($i = 0; $i < $zip->numFiles; $i++) { | |
| 134 | - | |
| 135 | - $entry = $zip->getNameIndex($i); | |
| 136 | - | |
| 137 | - if (preg_match('#^(.*?)/wp-content/#', $entry, $matches)) { | |
| 138 | - | |
| 139 | - $root_folder = $matches[1] . '/'; | |
| 140 | - | |
| 141 | - break; | |
| 142 | - | |
| 143 | - } | |
| 144 | - | |
| 145 | - } | |
| 146 | - | |
| 147 | - wpdbbkp_log("Detected root folder: $root_folder"); | |
| 148 | - | |
| 149 | - | |
| 150 | - | |
| 151 | - | |
| 152 | - | |
| 153 | - for ($i = 0; $i < $zip->numFiles; $i++) { | |
| 154 | - | |
| 155 | - $entry = $zip->getNameIndex($i); | |
| 156 | - | |
| 157 | - $normalized_entry = str_replace($root_folder, '', $entry); | |
| 158 | - | |
| 159 | - | |
| 160 | - | |
| 161 | - if (preg_match('#^wp-content/plugins/#', $normalized_entry) || | |
| 162 | - | |
| 163 | - preg_match('#^wp-content/themes/#', $normalized_entry) || | |
| 164 | - | |
| 165 | - (pathinfo($normalized_entry, PATHINFO_EXTENSION) === 'sql' && substr_count($normalized_entry, '/') === 0) | |
| 166 | - | |
| 167 | - ) { | |
| 168 | - | |
| 169 | - if ($zip->extractTo($extract_path, $entry)) { | |
| 170 | - | |
| 171 | - set_transient('wpdbbkp_track_progress', $entry, 3600); | |
| 172 | - | |
| 173 | - wpdbbkp_log("Successfully extracted: $entry"); | |
| 174 | - | |
| 175 | - | |
| 176 | - | |
| 177 | - } else { | |
| 178 | - | |
| 179 | - wpdbbkp_log("Extraction failed: $entry"); | |
| 180 | - | |
| 181 | - } | |
| 182 | - | |
| 183 | - } | |
| 184 | - | |
| 185 | - } | |
| 186 | - | |
| 187 | - | |
| 188 | - | |
| 189 | - $zip->close(); | |
| 190 | - | |
| 191 | - set_transient('wpdbbkp_track_progress', 'Extraction of files completed. Now taking live plugins & themes', 3600); | |
| 192 | - | |
| 193 | - $plugins_path = $extract_path . '/wp-content/plugins/'; | |
| 194 | - | |
| 195 | - $themes_path = $extract_path . '/wp-content/themes/'; | |
| 196 | - | |
| 197 | - | |
| 198 | - | |
| 199 | - // Move extracted files to wp-content | |
| 200 | - | |
| 201 | - $ignore_plugins = ['wp-database-backup']; | |
| 202 | - | |
| 203 | - | |
| 204 | - | |
| 205 | - if (file_exists($plugins_path)) { | |
| 206 | - | |
| 207 | - wpdbbkp_move_extracted_files($plugins_path, WP_CONTENT_DIR . '/plugins/', $ignore_plugins); | |
| 208 | - | |
| 209 | - } else { | |
| 210 | - | |
| 211 | - wpdbbkp_log("Plugins directory not found in extracted folder."); | |
| 212 | - | |
| 213 | - } | |
| 214 | - | |
| 215 | - | |
| 216 | - | |
| 217 | - if (file_exists($themes_path)) { | |
| 218 | - | |
| 219 | - wpdbbkp_move_extracted_files($themes_path, WP_CONTENT_DIR . '/themes/'); | |
| 220 | - | |
| 221 | - } else { | |
| 222 | - | |
| 223 | - wpdbbkp_log("Themes directory not found in extracted folder OR extraction failed."); | |
| 224 | - | |
| 225 | - } | |
| 226 | - | |
| 227 | - | |
| 228 | - | |
| 229 | - // Process SQL file if found | |
| 230 | - | |
| 231 | - $sql_file = wpdbbkp_find_sql_file($extract_path); | |
| 232 | - | |
| 233 | - if ($sql_file) { | |
| 234 | - | |
| 235 | - set_transient('wpdbbkp_track_progress', 'Migrating Database...', 3600); | |
| 236 | - | |
| 237 | - $wp_config_prefix = wpdbbkp_get_wp_config_table_prefix(); | |
| 238 | - wpdbbkp_log("Sql File .".$sql_file); | |
| 239 | - wpdbbkp_update_sql_table_prefix($sql_file); | |
| 240 | - | |
| 241 | - wpdbbkp_restore_database($sql_file); | |
| 242 | - | |
| 243 | - } else { | |
| 244 | - | |
| 245 | - wpdbbkp_log("No SQL file found in extracted folder."); | |
| 246 | - | |
| 247 | - } | |
| 248 | - | |
| 249 | - | |
| 250 | - | |
| 251 | - wpdbbkp_log("Extraction completed successfully"); | |
| 252 | - | |
| 253 | - $wpdbbkp_folder = WP_CONTENT_DIR . '/uploads/wpdbbkp'; | |
| 254 | - if (file_exists($wpdbbkp_folder)) { | |
| 255 | - wpdbbkp_delete_folder($wpdbbkp_folder); | |
| 256 | - wpdbbkp_log("Deleted wpdbbkp folder after successful migration."); | |
| 257 | - } | |
| 258 | - set_transient('wpdbbkp_track_progress', 'Process Completed', 3600); | |
| 259 | - wp_send_json_success(esc_html__('Plugins, Themes, Database & Table Prefix Updated!', 'wpdbbkp')); | |
| 260 | - | |
| 261 | - | |
| 262 | - | |
| 263 | - } catch (Exception $e) { | |
| 264 | - | |
| 265 | - wpdbbkp_log("Error: " . $e->getMessage()); | |
| 266 | - | |
| 267 | - wp_send_json_error($e->getMessage()); | |
| 268 | - | |
| 269 | - } | |
| 270 | - | |
| 271 | -} | |
| 272 | - | |
| 273 | -/** | |
| 274 | - * Initialize WP_Filesystem API. | |
| 275 | - * | |
| 276 | - * @return bool | |
| 277 | - */ | |
| 278 | -function wpdbbkp_init_filesystem() { | |
| 279 | - global $wp_filesystem; | |
| 280 | - if ( ! empty( $wp_filesystem ) ) { | |
| 281 | - return true; | |
| 282 | - } | |
| 283 | - if ( ! function_exists( 'WP_Filesystem' ) ) { | |
| 284 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 285 | - } | |
| 286 | - return WP_Filesystem(); | |
| 287 | -} | |
| 288 | - | |
| 289 | -function wpdbbkp_delete_folder( $folder_path ) { | |
| 290 | - if ( ! is_dir( $folder_path ) ) { | |
| 291 | - return; | |
| 292 | - } | |
| 293 | - | |
| 294 | - if ( ! wpdbbkp_init_filesystem() ) { | |
| 295 | - return; | |
| 296 | - } | |
| 297 | - | |
| 298 | - global $wp_filesystem; | |
| 299 | - | |
| 300 | - $files = array_diff( scandir( $folder_path ), array( '.', '..' ) ); | |
| 301 | - | |
| 302 | - foreach ( $files as $file ) { | |
| 303 | - $file_path = $folder_path . DIRECTORY_SEPARATOR . $file; | |
| 304 | - if ( is_dir( $file_path ) ) { | |
| 305 | - wpdbbkp_delete_folder( $file_path ); | |
| 306 | - } else { | |
| 307 | - set_transient( 'wpdbbkp_track_progress', 'Cleaning : ' . $file_path, 3600 ); | |
| 308 | - $wp_filesystem->delete( $file_path ); | |
| 309 | - } | |
| 310 | - } | |
| 311 | - | |
| 312 | - $wp_filesystem->rmdir( $folder_path ); | |
| 313 | -} | |
| 314 | - | |
| 315 | - | |
| 316 | - | |
| 317 | - | |
| 318 | -function wpdbbkp_find_sql_file($directory) { | |
| 319 | - | |
| 320 | - $files = scandir($directory); | |
| 321 | - | |
| 322 | - foreach ($files as $file) { | |
| 323 | - | |
| 324 | - if (pathinfo($file, PATHINFO_EXTENSION) === 'sql') { | |
| 325 | - | |
| 326 | - return $directory . '/' . $file; | |
| 327 | - | |
| 328 | - } | |
| 329 | - | |
| 330 | - } | |
| 331 | - | |
| 332 | - return false; | |
| 333 | - | |
| 334 | -} | |
| 335 | - | |
| 336 | - | |
| 337 | - | |
| 338 | -function wpdbbkp_get_wp_config_table_prefix() { | |
| 339 | - | |
| 340 | - $config_file = ABSPATH . 'wp-config.php'; | |
| 341 | - wpdbbkp_log("[ERROR] Config Path: $config_file"); | |
| 342 | - if (!file_exists($config_file)) { | |
| 343 | - | |
| 344 | - return 'wp_'; | |
| 345 | - | |
| 346 | - } | |
| 347 | - | |
| 348 | - $config_contents = file_get_contents($config_file); | |
| 349 | - | |
| 350 | - if (preg_match("/\$table_prefix\s*=\s*'([^']+)'/", $config_contents, $matches)) { | |
| 351 | - | |
| 352 | - return $matches[1]; | |
| 353 | - | |
| 354 | - } | |
| 355 | - | |
| 356 | - return 'wp_'; | |
| 357 | - | |
| 358 | -} | |
| 359 | -function wpdbbkp_update_sql_table_prefix($sql_file) { | |
| 360 | - if (!file_exists($sql_file)) { | |
| 361 | - die( esc_html( sprintf( | |
| 362 | - /* translators: %s: path to the SQL file */ | |
| 363 | - __( 'ERROR: SQL file not found: %s', 'wpdbbkp' ), | |
| 364 | - $sql_file | |
| 365 | - ) ) ); | |
| 366 | - } | |
| 367 | - | |
| 368 | - $sql_content = file_get_contents($sql_file); | |
| 369 | - | |
| 370 | - // Find wp-config.php | |
| 371 | - $wp_config_path = wpdbbkp_find_wp_config(); | |
| 372 | - if (!$wp_config_path) { | |
| 373 | - die(esc_html__("❌ ERROR: wp-config.php not found!\n", 'wpdbbkp')); | |
| 374 | - } | |
| 375 | - | |
| 376 | - // Extract table prefix from wp-config.php | |
| 377 | - $config_content = file_get_contents($wp_config_path); | |
| 378 | - if (preg_match("/\\\$table_prefix\s*=\s*['\"]([^'\"]+)['\"]\s*;/", $config_content, $matches)) { | |
| 379 | - $new_prefix = $matches[1]; | |
| 380 | - echo esc_html( sprintf( | |
| 381 | - /* translators: %s: table prefix from wp-config.php */ | |
| 382 | - __( 'SUCCESS: Extracted new prefix from wp-config.php: %s', 'wpdbbkp' ), | |
| 383 | - $new_prefix | |
| 384 | - ) ) . "\n"; | |
| 385 | - } else { | |
| 386 | - die(esc_html__("❌ ERROR: Could not extract table prefix from wp-config.php!\n", 'wpdbbkp')); | |
| 387 | - } | |
| 388 | - | |
| 389 | - // Detect old prefix from the SQL file | |
| 390 | - if (preg_match("/(CREATE TABLE|INSERT INTO|ALTER TABLE|UPDATE|DELETE FROM)\s+[`']?([a-zA-Z0-9]+?_)/i", $sql_content, $matches)) { | |
| 391 | - $old_prefix = $matches[2]; | |
| 392 | - echo esc_html( sprintf( | |
| 393 | - /* translators: %s: table prefix found in the SQL file */ | |
| 394 | - __( 'SUCCESS: Found old prefix in SQL file: %s', 'wpdbbkp' ), | |
| 395 | - $old_prefix | |
| 396 | - ) ) . "\n"; | |
| 397 | - } else { | |
| 398 | - die(esc_html__("❌ ERROR: No table prefix found in the SQL file!\n", 'wpdbbkp')); | |
| 399 | - } | |
| 400 | - | |
| 401 | - // Prevent replacing if old prefix is same as new prefix | |
| 402 | - if ($old_prefix === $new_prefix) { | |
| 403 | - die( esc_html( sprintf( | |
| 404 | - /* translators: %s: table prefix */ | |
| 405 | - __( 'INFO: Old and new prefixes are the same (%s), no changes needed.', 'wpdbbkp' ), | |
| 406 | - $old_prefix | |
| 407 | - ) ) ); | |
| 408 | - } | |
| 409 | - | |
| 410 | - // Replace all occurrences of the old prefix with the new one | |
| 411 | - $sql_content = preg_replace("/\b" . preg_quote($old_prefix, '/') . "/i", $new_prefix, $sql_content); | |
| 412 | - | |
| 413 | - // Write the updated content back to the SQL file | |
| 414 | - file_put_contents($sql_file, $sql_content); | |
| 415 | - | |
| 416 | - echo esc_html( sprintf( | |
| 417 | - /* translators: 1: old table prefix, 2: new table prefix, 3: path to SQL file */ | |
| 418 | - __( 'SUCCESS: Table prefix updated from %1$s to %2$s in %3$s!', 'wpdbbkp' ), | |
| 419 | - $old_prefix, | |
| 420 | - $new_prefix, | |
| 421 | - $sql_file | |
| 422 | - ) ) . "\n"; | |
| 423 | -} | |
| 424 | - | |
| 425 | -/** | |
| 426 | -* Finds the root wp-config.php file by checking parent directories. | |
| 427 | -*/ | |
| 428 | -function wpdbbkp_find_wp_config() { | |
| 429 | - $dir = __DIR__; | |
| 430 | - | |
| 431 | - while ($dir !== dirname($dir)) { // Keep going up until the root | |
| 432 | - $config_path = $dir . '/wp-config.php'; | |
| 433 | - if (file_exists($config_path)) { | |
| 434 | - return $config_path; | |
| 435 | - } | |
| 436 | - $dir = dirname($dir); | |
| 437 | - } | |
| 438 | - | |
| 439 | - return false; // wp-config.php not found | |
| 440 | -} | |
| 441 | - | |
| 442 | - | |
| 443 | - | |
| 444 | - | |
| 445 | - | |
| 446 | - | |
| 447 | - | |
| 448 | - | |
| 449 | -function wpdbbkp_move_extracted_files( $source, $destination, $ignore = array() ) { | |
| 450 | - if ( ! file_exists( $source ) || ! wpdbbkp_init_filesystem() ) { | |
| 451 | - return; | |
| 452 | - } | |
| 453 | - | |
| 454 | - global $wp_filesystem; | |
| 455 | - | |
| 456 | - wp_mkdir_p( $destination ); | |
| 457 | - | |
| 458 | - $files = array_diff( scandir( $source ), array( '.', '..' ) ); | |
| 459 | - foreach ( $files as $file ) { | |
| 460 | - $src_file = rtrim( $source, '/' ) . '/' . $file; | |
| 461 | - $dest_file = rtrim( $destination, '/' ) . '/' . $file; | |
| 462 | - | |
| 463 | - if ( in_array( $file, $ignore, true ) ) { | |
| 464 | - continue; | |
| 465 | - } | |
| 466 | - | |
| 467 | - set_transient( 'wpdbbkp_track_progress', 'Copying: ' . $dest_file, 3600 ); | |
| 468 | - | |
| 469 | - if ( is_dir( $src_file ) ) { | |
| 470 | - if ( $wp_filesystem->exists( $dest_file ) ) { | |
| 471 | - wpdbbkp_delete_directory( $dest_file ); | |
| 472 | - } | |
| 473 | - $wp_filesystem->move( $src_file, $dest_file, true ); | |
| 474 | - } else { | |
| 475 | - if ( $wp_filesystem->exists( $dest_file ) ) { | |
| 476 | - $wp_filesystem->delete( $dest_file ); | |
| 477 | - } | |
| 478 | - $wp_filesystem->move( $src_file, $dest_file, true ); | |
| 479 | - } | |
| 480 | - } | |
| 481 | -} | |
| 482 | - | |
| 483 | -function wpdbbkp_delete_directory( $dir ) { | |
| 484 | - if ( ! wpdbbkp_init_filesystem() ) { | |
| 485 | - return false; | |
| 486 | - } | |
| 487 | - | |
| 488 | - global $wp_filesystem; | |
| 489 | - | |
| 490 | - if ( ! $wp_filesystem->exists( $dir ) ) { | |
| 491 | - return false; | |
| 492 | - } | |
| 493 | - | |
| 494 | - if ( ! $wp_filesystem->is_dir( $dir ) ) { | |
| 495 | - return $wp_filesystem->delete( $dir ); | |
| 496 | - } | |
| 497 | - | |
| 498 | - $files = array_diff( scandir( $dir ), array( '.', '..' ) ); | |
| 499 | - foreach ( $files as $file ) { | |
| 500 | - $file_path = $dir . DIRECTORY_SEPARATOR . $file; | |
| 501 | - if ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 502 | - wpdbbkp_delete_directory( $file_path ); | |
| 503 | - } else { | |
| 504 | - $wp_filesystem->delete( $file_path ); | |
| 505 | - } | |
| 506 | - } | |
| 507 | - | |
| 508 | - return $wp_filesystem->rmdir( $dir ); | |
| 509 | -} | |
| 510 | - | |
| 511 | - | |
| 512 | -function wpdbbkp_restore_database($sql_file) { | |
| 513 | - | |
| 514 | - global $wpdb; | |
| 515 | - | |
| 516 | - $new_site_url = get_site_url(); | |
| 517 | - | |
| 518 | - | |
| 519 | - | |
| 520 | - if (!file_exists($sql_file)) { | |
| 521 | - | |
| 522 | - wpdbbkp_log("[ERROR] SQL file not found: $sql_file"); | |
| 523 | - | |
| 524 | - return false; | |
| 525 | - | |
| 526 | - } | |
| 527 | - | |
| 528 | - | |
| 529 | - | |
| 530 | - $sql_content = file_get_contents($sql_file); | |
| 531 | - | |
| 532 | - if (!$sql_content) { | |
| 533 | - | |
| 534 | - wpdbbkp_log("[ERROR] Failed to read SQL file: $sql_file"); | |
| 535 | - | |
| 536 | - return false; | |
| 537 | - | |
| 538 | - } | |
| 539 | - | |
| 540 | - | |
| 541 | - | |
| 542 | - // phpcs:disable WordPress.DB.DirectDatabaseQuery -- Direct $wpdb access required for SQL backup restore. | |
| 543 | - // Fetch old site URL from wp_options before updating | |
| 544 | - | |
| 545 | - $old_site_url = $wpdb->get_var("SELECT option_value FROM {$wpdb->options} WHERE option_name = 'siteurl'"); | |
| 546 | - | |
| 547 | - | |
| 548 | - | |
| 549 | - if (!$old_site_url) { | |
| 550 | - | |
| 551 | - wpdbbkp_log("[ERROR] Failed to fetch old site URL."); | |
| 552 | - | |
| 553 | - return false; | |
| 554 | - | |
| 555 | - } | |
| 556 | - | |
| 557 | - | |
| 558 | - | |
| 559 | - wpdbbkp_log("[INFO] Old Site URL detected: $old_site_url"); | |
| 560 | - | |
| 561 | - | |
| 562 | - | |
| 563 | - // Split SQL statements properly | |
| 564 | - | |
| 565 | - $queries = preg_split('/;\s*\n/', $sql_content, -1, PREG_SPLIT_NO_EMPTY); | |
| 566 | - | |
| 567 | - | |
| 568 | - | |
| 569 | - // Tables to exclude (e.g., Users and Usermeta for security reasons) | |
| 570 | - | |
| 571 | - $excluded_tables = ['users','usermeta','options']; | |
| 572 | - | |
| 573 | - | |
| 574 | - | |
| 575 | - $wpdb->query('SET foreign_key_checks = 0'); // Disable FK checks | |
| 576 | - | |
| 577 | - $wpdb->query('START TRANSACTION'); // Start transaction | |
| 578 | - | |
| 579 | - | |
| 580 | - | |
| 581 | - try { | |
| 582 | - | |
| 583 | - foreach ($queries as $query) { | |
| 584 | - | |
| 585 | - $query = trim($query); | |
| 586 | - | |
| 587 | - if (empty($query)) continue; | |
| 588 | - | |
| 589 | - $query = str_replace('options','option_tmp',$query); | |
| 590 | - | |
| 591 | - // Check for CREATE TABLE and extract table name | |
| 592 | - | |
| 593 | - if (preg_match('/CREATE TABLE `([^`]*)`/', $query, $matches)) { | |
| 594 | - | |
| 595 | - $table_name = $matches[1]; | |
| 596 | - | |
| 597 | - | |
| 598 | - | |
| 599 | - // Skip excluded tables | |
| 600 | - | |
| 601 | - foreach ($excluded_tables as $excluded) { | |
| 602 | - | |
| 603 | - if (strpos($table_name, $excluded) !== false) { | |
| 604 | - | |
| 605 | - wpdbbkp_log("[SKIPPED] Table excluded: $table_name"); | |
| 606 | - | |
| 607 | - continue 2; | |
| 608 | - | |
| 609 | - } | |
| 610 | - | |
| 611 | - } | |
| 612 | - | |
| 613 | - | |
| 614 | - | |
| 615 | - // Drop existing table before restoring | |
| 616 | - | |
| 617 | - $wpdb->query( 'DROP TABLE IF EXISTS `' . esc_sql( $table_name ) . '`' ); | |
| 618 | - | |
| 619 | - wpdbbkp_log("[DROPPED] Table: $table_name"); | |
| 620 | - | |
| 621 | - } | |
| 622 | - | |
| 623 | - | |
| 624 | - | |
| 625 | - // Skip execution of queries for excluded tables | |
| 626 | - | |
| 627 | - foreach ($excluded_tables as $excluded) { | |
| 628 | - | |
| 629 | - if (strpos($query, $excluded) !== false) { | |
| 630 | - | |
| 631 | - wpdbbkp_log("[SKIPPED] Query contains excluded table reference."); | |
| 632 | - | |
| 633 | - continue 2; | |
| 634 | - | |
| 635 | - } | |
| 636 | - | |
| 637 | - } | |
| 638 | - | |
| 639 | - | |
| 640 | - | |
| 641 | - // Execute the query (dynamic SQL from backup; prepare cannot cover arbitrary statements). | |
| 642 | - | |
| 643 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Restoring statements from trusted backup file. | |
| 644 | - $result = $wpdb->query( $query ); | |
| 645 | - | |
| 646 | - if ($result === false) { | |
| 647 | - | |
| 648 | - throw new Exception("[ERROR] Failed to execute query: " . $wpdb->last_error); | |
| 649 | - | |
| 650 | - } | |
| 651 | - | |
| 652 | - } | |
| 653 | - | |
| 654 | - | |
| 655 | - | |
| 656 | - | |
| 657 | - | |
| 658 | - | |
| 659 | - | |
| 660 | - // **Find and replace old URLs in all tables** | |
| 661 | - | |
| 662 | - $tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); | |
| 663 | - | |
| 664 | - foreach ($tables as $table) { | |
| 665 | - | |
| 666 | - $table_name = esc_sql( $table[0] ); | |
| 667 | - | |
| 668 | - | |
| 669 | - | |
| 670 | - // Skip excluded tables | |
| 671 | - | |
| 672 | - foreach ($excluded_tables as $excluded) { | |
| 673 | - | |
| 674 | - if (strpos($table_name, $excluded) !== false) { | |
| 675 | - | |
| 676 | - wpdbbkp_log("[SKIPPED] URL Replacement in: $table_name"); | |
| 677 | - | |
| 678 | - continue 2; | |
| 679 | - | |
| 680 | - } | |
| 681 | - | |
| 682 | - } | |
| 683 | - | |
| 684 | - | |
| 685 | - | |
| 686 | - // Get all columns for the table | |
| 687 | - | |
| 688 | - // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name escaped with esc_sql(). | |
| 689 | - $columns = $wpdb->get_results( "SHOW COLUMNS FROM `{$table_name}`", ARRAY_A ); | |
| 690 | - | |
| 691 | - foreach ($columns as $column) { | |
| 692 | - | |
| 693 | - $column_name = esc_sql( $column['Field'] ); | |
| 694 | - | |
| 695 | - | |
| 696 | - | |
| 697 | - // Update all occurrences of old URL in text-based columns. | |
| 698 | - $wpdb->query( | |
| 699 | - $wpdb->prepare( | |
| 700 | - "UPDATE `{$table_name}` SET `{$column_name}` = REPLACE(`{$column_name}`, %s, %s) WHERE `{$column_name}` LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table and column identifiers escaped with esc_sql(). | |
| 701 | - $old_site_url, | |
| 702 | - $new_site_url, | |
| 703 | - '%' . $wpdb->esc_like( $old_site_url ) . '%' | |
| 704 | - ) | |
| 705 | - ); | |
| 706 | - | |
| 707 | - } | |
| 708 | - | |
| 709 | - } | |
| 710 | - | |
| 711 | - | |
| 712 | - | |
| 713 | - $blogname = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'blogname'"); | |
| 714 | - | |
| 715 | - if ($blogname) { | |
| 716 | - | |
| 717 | - $wpdb->query( | |
| 718 | - | |
| 719 | - $wpdb->prepare( | |
| 720 | - | |
| 721 | - "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'blogname'", | |
| 722 | - | |
| 723 | - $blogname | |
| 724 | - | |
| 725 | - ) | |
| 726 | - | |
| 727 | - ); | |
| 728 | - | |
| 729 | - wpdbbkp_log("[SUCCESS] Updated wp_options with blogname from option_tmp"); | |
| 730 | - | |
| 731 | - } | |
| 732 | - $active_plugins = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'active_plugins'"); | |
| 733 | - | |
| 734 | - if ($active_plugins) { | |
| 735 | - | |
| 736 | - $wpdb->query( | |
| 737 | - | |
| 738 | - $wpdb->prepare( | |
| 739 | - | |
| 740 | - "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'active_plugins'", | |
| 741 | - | |
| 742 | - $active_plugins | |
| 743 | - | |
| 744 | - ) | |
| 745 | - | |
| 746 | - ); | |
| 747 | - | |
| 748 | - wpdbbkp_log("[SUCCESS] Updated wp_options with active_plugins from option_tmp"); | |
| 749 | - | |
| 750 | - } | |
| 751 | - | |
| 752 | - $wpdb->query('COMMIT'); // Commit transaction | |
| 753 | - | |
| 754 | - wpdbbkp_log("[SUCCESS] Database restoration and URL replacement completed from: $sql_file"); | |
| 755 | - | |
| 756 | - return true; | |
| 757 | - | |
| 758 | - | |
| 759 | - | |
| 760 | - } catch (Exception $e) { | |
| 761 | - | |
| 762 | - $wpdb->query('ROLLBACK'); // Rollback in case of an error | |
| 763 | - | |
| 764 | - wpdbbkp_log($e->getMessage()); | |
| 765 | - | |
| 766 | - return false; | |
| 767 | - | |
| 768 | - } finally { | |
| 769 | - | |
| 770 | - $wpdb->query('SET foreign_key_checks = 1'); // Re-enable FK checks | |
| 771 | - | |
| 772 | - } | |
| 773 | - | |
| 774 | - // phpcs:enable WordPress.DB.DirectDatabaseQuery | |
| 775 | - | |
| 776 | -} | |
| 777 | 6 | function bkpforwp_anonimize_database($value, $table, $column) |
| 778 | 7 | { |
| 779 | 8 | $enable_anonymization = get_option('bkpforwp_enable_anonymization', false); |
| 780 | 9 | $anonymization_type = get_option('bkpforwp_anonymization_type', false); |
| @@ -845,9 +74,9 @@ | ||
| 845 | 74 | if (in_array($table, $bkpforwp_process_table)) { |
| 846 | 75 | $check_str = implode(',', $bkpforwp_process_cols); |
| 847 | 76 | if (stripos($check_str, $column) !== false) { |
| 848 | 77 | $enc_pass = $anonymization_pass; |
| 849 | - $encryption = new WPDBBackupSymmetricEncryption(); | |
| 78 | + $encryption = new SymmetricEncryption(); | |
| 850 | 79 | return str_replace($value, '<==>' . $encryption->encrypt($value, $enc_pass, $enc_pass) . '<==>', $value); |
| 851 | 80 | } |
| 852 | 81 | |
| 853 | 82 | } |
| @@ -876,9 +105,9 @@ | ||
| 876 | 105 | $anonymization_pass = get_option('bkpforwp_anonymization_pass', ''); |
| 877 | 106 | $enc_pass = isset($anonymization_pass) ? $anonymization_pass : false; |
| 878 | 107 | if ($enc_pass) { |
| 879 | 108 | require_once 'class-symmetric-encryption.php'; |
| 880 | - $encryption = new WPDBBackupSymmetricEncryption(); | |
| 109 | + $encryption = new SymmetricEncryption(); | |
| 881 | 110 | return $encryption->decrypt($matches[0], $enc_pass, $enc_pass); |
| 882 | 111 | } |
| 883 | 112 | return $matches[0]; |
| 884 | 113 | } |