Changeset 3663734
- Timestamp:
- 08/24/2026 03:02:46 PM (3 months ago)
- Location:
- haseebvault-backup-migration/trunk
- Files:
-
- 7 edited
-
assets/js/admin.js (modified) (5 diffs)
-
haseebvault.php (modified) (2 diffs)
-
includes/class-database-restorer.php (modified) (4 diffs)
-
includes/class-restore.php (modified) (9 diffs)
-
includes/class-url-replacer.php (modified) (5 diffs)
-
readme.txt (modified) (2 diffs)
-
tests/test-url-replacer.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
haseebvault-backup-migration/trunk/assets/js/admin.js
r3662954 r3663734 61 61 locked: false, 62 62 terminal: false, 63 redirectUrl: '', 63 64 }; 64 65 const stageLabels = { … … 100 101 if (cancelButton) { 101 102 cancelButton.hidden = !canCancel && !runner.terminal; 102 cancelButton.textContent = runner.terminal ? 'Close' : 'Cancel';103 cancelButton.textContent = runner.terminal && runner.redirectUrl ? 'OK' : (runner.terminal ? 'Close' : 'Cancel'); 103 104 } 104 105 } … … 137 138 runner.kind = ''; 138 139 runner.jobId = ''; 140 runner.redirectUrl = ''; 139 141 if (progress) progress.hidden = true; 140 142 } … … 171 173 runner.running = false; 172 174 if (job.status === 'completed') { 173 toast(job.message); 174 lockPage(false); 175 if (job.type === 'restore') window.location.assign(cfg.urls.login); 176 else if (job.type === 'backup') window.location.assign(cfg.urls.list); 175 if (job.type === 'restore') { 176 runner.redirectUrl = cfg.urls.login; 177 markTerminal(job); 178 } else if (job.type === 'backup') { 179 toast(job.message); 180 lockPage(false); 181 window.location.assign(cfg.urls.list); 182 } 177 183 } else if (job.status === 'failed') { 178 184 markTerminal(job, true); … … 376 382 if (cancelButton) cancelButton.addEventListener('click', async () => { 377 383 if (runner.terminal) { 384 if (runner.redirectUrl) { 385 window.location.assign(runner.redirectUrl); 386 return; 387 } 378 388 closeProgress(); 379 389 return; -
haseebvault-backup-migration/trunk/haseebvault.php
r3662954 r3663734 3 3 * Plugin Name: HaseebVault - Backup & Migration 4 4 * Description: Secure WordPress backup, migration, import, and export with resumable processing, integrity checks, and safe URL replacement. 5 * Version: 1.1. 65 * Version: 1.1.7 6 6 * Requires at least: 6.2 7 7 * Requires PHP: 7.4 … … 18 18 defined( 'ABSPATH' ) || exit; 19 19 20 define( 'HASEEBVAULT_VERSION', '1.1. 6' );20 define( 'HASEEBVAULT_VERSION', '1.1.7' ); 21 21 define( 'HASEEBVAULT_FORMAT_VERSION', '1.0' ); 22 22 define( 'HASEEBVAULT_FILE', __FILE__ ); -
haseebvault-backup-migration/trunk/includes/class-database-restorer.php
r3662954 r3663734 54 54 } 55 55 $this->validate_statement( $line, $state['source_prefix'] ); 56 // Keep WordPress bootstrap tables available between resumable requests. 57 // Their imported rows are still applied below, but dropping one at a batch 58 // boundary would invalidate the current login before the next AJAX call. 59 if ( $this->is_runtime_table_ddl( $line, $state['source_prefix'] ) ) { 60 ++$state['db_restore_queries']; 61 ++$processed; 62 continue; 63 } 56 64 $sql = $this->map_prefix( $line, $state['source_prefix'], $state['destination_prefix'] ); 57 65 if ( 0 === stripos( $sql, 'INSERT INTO ' ) ) { … … 77 85 'complete' => $eof, 78 86 ); 87 } 88 89 /** 90 * Detect destructive schema statements for tables required to authenticate 91 * and bootstrap WordPress while an import is still in progress. 92 * 93 * @param string $sql Validated export statement. 94 * @param string $source_prefix Source database prefix. 95 * @return bool 96 */ 97 private function is_runtime_table_ddl( $sql, $source_prefix ) { 98 $prefix = preg_quote( $source_prefix, '/' ); 99 if ( ! preg_match( '/^(?:DROP TABLE IF EXISTS|CREATE TABLE) `' . $prefix . '([A-Za-z0-9_$-]+)`/i', $sql, $matches ) ) { 100 return false; 101 } 102 return in_array( strtolower( $matches[1] ), array( 'options', 'users', 'usermeta' ), true ); 79 103 } 80 104 … … 123 147 array( 124 148 'user_id' => (int) $user['ID'], 125 'meta_key' => $meta['meta_key'], 149 'meta_key' => $meta['meta_key'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Deletes one indexed key for the preserved administrator only. 126 150 ), 127 151 array( '%d', '%s' ) … … 131 155 array( 132 156 'user_id' => (int) $user['ID'], 133 'meta_key' => $meta['meta_key'], 134 'meta_value' => $meta['meta_value'], 157 'meta_key' => $meta['meta_key'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Restores one bounded administrator key. 158 'meta_value' => $meta['meta_value'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Restores the matching bounded administrator value. 135 159 ), 136 160 array( '%d', '%s', '%s' ) -
haseebvault-backup-migration/trunk/includes/class-restore.php
r3662954 r3663734 41 41 throw new RuntimeException( esc_html__( 'HaseebVault 1.1 blocks import and migration on WordPress Multisite because network imports are not yet safely supported.', 'haseebvault-backup-migration' ) ); 42 42 } 43 if ( ' RESTORE' !== strtoupper( trim( (string) ( $request['confirmation'] ?? '' ) ) ) ) {43 if ( 'IMPORT' !== strtoupper( trim( (string) ( $request['confirmation'] ?? '' ) ) ) ) { 44 44 throw new RuntimeException( esc_html__( 'Type IMPORT to confirm this operation.', 'haseebvault-backup-migration' ) ); 45 45 } … … 68 68 $restore_request = array( 69 69 'backup_id' => $backup['backup_id'], 70 'confirmation' => ' RESTORE',70 'confirmation' => 'IMPORT', 71 71 'mode' => $mode, 72 72 'components' => array_values( $selected ), … … 102 102 'old_url' => untrailingslashit( esc_url_raw( $request['old_url'] ) ), 103 103 'new_url' => untrailingslashit( esc_url_raw( $request['new_url'] ) ), 104 'new_site_url' => $this->destination_site_url( $request['new_url'], $backup['manifest'] ), 104 105 'replace_guid' => ! empty( $request['replace_guid'] ), 105 106 'source_prefix' => (string) ( $backup['manifest']['database_prefix'] ?? $wpdb->prefix ), … … 112 113 'administrator' => $this->snapshot_administrator( $user_id ), 113 114 'runtime_options' => array( 114 'settings' => get_option( 'haseebvault_settings', array() ), 115 'db_version' => get_option( 'haseebvault_db_version', HASEEBVAULT_VERSION ), 115 'settings' => get_option( 'haseebvault_settings', array() ), 116 'db_version' => get_option( 'haseebvault_db_version', HASEEBVAULT_VERSION ), 117 'home' => get_option( 'home', home_url() ), 118 'siteurl' => get_option( 'siteurl', site_url() ), 119 'active_plugins' => get_option( 'active_plugins', array() ), 120 'user_roles' => get_option( $wpdb->prefix . 'user_roles', array() ), 116 121 ), 122 'source_urls' => $this->source_urls( $request, $backup['manifest'] ), 117 123 'started_at' => time(), 118 124 ); … … 209 215 210 216 case 'replace_urls': 211 if ( ! in_array( 'database', $state['components'], true ) || empty( $state['old_url'] ) || empty( $state['new_url'] ) || $state['old_url'] === $state['new_url']) {217 if ( ! in_array( 'database', $state['components'], true ) || ! $this->needs_url_replacement( $state ) ) { 212 218 $job = $this->jobs->touch_state( $job_id, $state, 'finalize', 96 ); 213 219 break; … … 218 224 $result = $this->replacer->process( $state ); 219 225 $state = $result['state']; 226 // URL replacement may touch home/siteurl before all tables are scanned. 227 // Keep the current cookie origin until the final response is ready. 228 wp_cache_flush(); 229 $this->preserve_runtime_options( $state, $job_id ); 220 230 $stage = $result['complete'] ? 'finalize' : 'replace_urls'; 221 231 $total = max( 1, count( $state['replace_tables'] ) ); … … 341 351 return in_array( 'other', $components, true ); 342 352 } 343 return in_array( ' WordPress', $components, true );353 return in_array( 'wordpress', $components, true ); 344 354 } 345 355 … … 419 429 420 430 private function preserve_runtime_options( array $state, $job_id ) { 421 if ( isset( $state['runtime_options']['settings'] ) ) { 422 update_option( 'haseebvault_settings', $state['runtime_options']['settings'], false ); 423 } 424 update_option( 'haseebvault_db_version', $state['runtime_options']['db_version'] ?? HASEEBVAULT_VERSION, false ); 431 global $wpdb; 432 $runtime = (array) ( $state['runtime_options'] ?? array() ); 433 if ( array_key_exists( 'settings', $runtime ) ) { 434 update_option( 'haseebvault_settings', $runtime['settings'], false ); 435 } 436 update_option( 'haseebvault_db_version', $runtime['db_version'] ?? HASEEBVAULT_VERSION, false ); 437 foreach ( array( 'home', 'siteurl', 'active_plugins' ) as $option_name ) { 438 if ( array_key_exists( $option_name, $runtime ) ) { 439 update_option( $option_name, $runtime[ $option_name ], false ); 440 } 441 } 442 if ( array_key_exists( 'user_roles', $runtime ) ) { 443 update_option( $wpdb->prefix . 'user_roles', $runtime['user_roles'], false ); 444 } 425 445 $this->lock->force( $job_id, 'restore' ); 446 } 447 448 /** 449 * Collect every source site URL recorded by the package. WordPress can use 450 * different home and site URLs, so both must participate in migration. 451 * 452 * @param array $request Verified import request. 453 * @param array $manifest Package manifest. 454 * @return array 455 */ 456 private function source_urls( array $request, array $manifest ) { 457 $urls = array( 458 $request['old_url'] ?? '', 459 $manifest['home_url'] ?? '', 460 $manifest['site_url'] ?? '', 461 ); 462 $urls = array_map( 463 static function ( $url ) { 464 return untrailingslashit( esc_url_raw( $url ) ); 465 }, 466 $urls 467 ); 468 return array_values( array_unique( array_filter( $urls ) ) ); 469 } 470 471 /** 472 * Determine whether at least one recorded source URL differs from the target. 473 * 474 * @param array $state Import state. 475 * @return bool 476 */ 477 private function needs_url_replacement( array $state ) { 478 if ( empty( $state['new_url'] ) ) { 479 return false; 480 } 481 $sources = ! empty( $state['source_urls'] ) ? (array) $state['source_urls'] : array( $state['old_url'] ?? '' ); 482 foreach ( $sources as $source ) { 483 if ( $source && untrailingslashit( $source ) !== untrailingslashit( $state['new_url'] ) ) { 484 return true; 485 } 486 } 487 return false; 488 } 489 490 /** 491 * Preserve a WordPress-in-a-subdirectory suffix during domain migration. 492 * 493 * @param string $new_home Destination home URL. 494 * @param array $manifest Package manifest. 495 * @return string 496 */ 497 private function destination_site_url( $new_home, array $manifest ) { 498 $new_home = untrailingslashit( esc_url_raw( $new_home ) ); 499 $source_home = untrailingslashit( esc_url_raw( $manifest['home_url'] ?? '' ) ); 500 $source_site = untrailingslashit( esc_url_raw( $manifest['site_url'] ?? '' ) ); 501 if ( $source_home && 0 === strpos( $source_site, $source_home . '/' ) ) { 502 return $new_home . substr( $source_site, strlen( $source_home ) ); 503 } 504 return $new_home; 426 505 } 427 506 … … 430 509 if ( ! empty( $state['new_url'] ) ) { 431 510 update_option( 'home', $state['new_url'] ); 432 update_option( 'siteurl', $state['new_ url'] );511 update_option( 'siteurl', $state['new_site_url'] ?? $state['new_url'] ); 433 512 } 434 513 wp_cache_flush(); -
haseebvault-backup-migration/trunk/includes/class-url-replacer.php
r3662954 r3663734 67 67 continue; 68 68 } 69 $value = $row[ $column ]; 70 $escaped_old = str_replace( '/', '\\/', $state['old_url'] ); 71 if ( ! is_string( $value ) || ( false === strpos( $value, $state['old_url'] ) && false === strpos( $value, $escaped_old ) ) ) { 69 $value = $row[ $column ]; 70 if ( ! is_string( $value ) ) { 72 71 continue; 73 72 } 74 $replaced = self::replace_value( $value, $state['old_url'], $state['new_url'] ); 73 $replaced = $value; 74 $sources = ! empty( $state['source_urls'] ) ? (array) $state['source_urls'] : array( $state['old_url'] ); 75 foreach ( $sources as $source ) { 76 if ( $source && untrailingslashit( $source ) !== untrailingslashit( $state['new_url'] ) ) { 77 $replaced = self::replace_value( $replaced, $source, $state['new_url'] ); 78 } 79 } 75 80 if ( $replaced !== $value ) { 76 81 $changes[ $column ] = $replaced; … … 113 118 return $value; 114 119 } 115 $ escaped_old = str_replace( '/', '\\/', $old);116 if ( false === strpos( $value, $old ) && false === strpos( $value, $escaped_old) ) {120 $pairs = self::replacement_pairs( untrailingslashit( $old ), untrailingslashit( $new ) ); 121 if ( ! self::contains_candidate( $value, $pairs ) ) { 117 122 return $value; 118 123 } … … 120 125 $unserialized = @unserialize( $value, array( 'allowed_classes' => false ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize 121 126 if ( false !== $unserialized || 'b:0;' === $value ) { 122 return serialize( self::replace_recursive( $unserialized, $ old, $new) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize127 return serialize( self::replace_recursive( $unserialized, $pairs ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize 123 128 } 124 129 } … … 127 132 $decoded = json_decode( $value, true ); 128 133 if ( JSON_ERROR_NONE === json_last_error() ) { 129 return wp_json_encode( self::replace_recursive( $decoded, $ old, $new), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );130 } 131 } 132 return str _replace( $old, $new, $value);133 } 134 135 private static function replace_recursive( $value, $old, $new) {134 return wp_json_encode( self::replace_recursive( $decoded, $pairs ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); 135 } 136 } 137 return strtr( $value, $pairs ); 138 } 139 140 private static function replace_recursive( $value, array $pairs ) { 136 141 if ( is_string( $value ) ) { 137 return str _replace( $old, $new, $value);142 return strtr( $value, $pairs ); 138 143 } 139 144 if ( is_array( $value ) ) { 140 145 $replaced = array(); 141 146 foreach ( $value as $key => $item ) { 142 $new_key = is_string( $key ) ? str _replace( $old, $new, $key) : $key;143 $replaced[ $new_key ] = self::replace_recursive( $item, $ old, $new);147 $new_key = is_string( $key ) ? strtr( $key, $pairs ) : $key; 148 $replaced[ $new_key ] = self::replace_recursive( $item, $pairs ); 144 149 } 145 150 return $replaced; … … 150 155 continue; 151 156 } 152 $value->{$key} = self::replace_recursive( $item, $ old, $new);157 $value->{$key} = self::replace_recursive( $item, $pairs ); 153 158 } 154 159 } 155 160 return $value; 161 } 162 163 /** 164 * Build safe representations of a full source URL that can occur in option, 165 * post-builder, JSON, HTML, and encoded values. 166 * 167 * @param string $old Source URL. 168 * @param string $new Destination URL. 169 * @return array 170 */ 171 private static function replacement_pairs( $old, $new ) { 172 $pairs = array(); 173 self::add_pair( $pairs, $old, $new ); 174 self::add_pair( $pairs, str_replace( '/', '\\/', $old ), str_replace( '/', '\\/', $new ) ); 175 self::add_pair( $pairs, rawurlencode( $old ), rawurlencode( $new ) ); 176 self::add_pair( $pairs, urlencode( $old ), urlencode( $new ) ); 177 self::add_pair( $pairs, str_replace( '/', '/', $old ), str_replace( '/', '/', $new ) ); 178 179 $old_scheme = wp_parse_url( $old, PHP_URL_SCHEME ); 180 $new_scheme = wp_parse_url( $new, PHP_URL_SCHEME ); 181 if ( in_array( $old_scheme, array( 'http', 'https' ), true ) ) { 182 $old_suffix = substr( $old, strlen( $old_scheme ) + 1 ); 183 $new_suffix = $new_scheme ? substr( $new, strlen( $new_scheme ) + 1 ) : $new; 184 self::add_pair( $pairs, ( 'http' === $old_scheme ? 'https' : 'http' ) . ':' . $old_suffix, $new ); 185 self::add_pair( $pairs, $old_suffix, $new_suffix ); 186 } 187 uksort( 188 $pairs, 189 static function ( $left, $right ) { 190 return strlen( $right ) <=> strlen( $left ); 191 } 192 ); 193 return $pairs; 194 } 195 196 private static function add_pair( array &$pairs, $old, $new ) { 197 if ( '' !== $old && $old !== $new ) { 198 $pairs[ $old ] = $new; 199 } 200 } 201 202 private static function contains_candidate( $value, array $pairs ) { 203 foreach ( $pairs as $old => $new ) { 204 unset( $new ); 205 if ( false !== strpos( $value, $old ) ) { 206 return true; 207 } 208 } 209 return false; 156 210 } 157 211 -
haseebvault-backup-migration/trunk/readme.txt
r3662954 r3663734 5 5 Tested up to: 7.0 6 6 Requires PHP: 7.4 7 Stable tag: 1.1. 67 Stable tag: 1.1.7 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 96 96 == Changelog == 97 97 98 = 1.1.7 = 99 * Kept WordPress authentication, administrator roles, active plugins, and site URL options stable throughout resumable database imports. 100 * Prevented critical users, usermeta, and options tables from disappearing at database batch boundaries. 101 * Expanded serialized and JSON-safe migration replacement to home/site URLs, HTTP/HTTPS variants, escaped URLs, encoded URLs, and scheme-relative URLs. 102 * Added an import-success confirmation popup that redirects to login only after the administrator clicks OK. 103 * Fixed selective WordPress core-file imports by using the correct component identifier. 104 * Matched the backend destructive-action confirmation to the Import interface. 105 98 106 = 1.1.6 = 99 107 * Removed the fixed directory-depth assumption from the archive recovery regression test bootstrap. -
haseebvault-backup-migration/trunk/tests/test-url-replacer.php
r3662954 r3663734 10 10 function wp_json_encode( $value, $flags = 0 ) { 11 11 return json_encode( $value, $flags ); } 12 function untrailingslashit( $value ) { 13 return rtrim( $value, '/\\' ); 14 } 15 function wp_parse_url( $url, $component = -1 ) { 16 return parse_url( $url, $component ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- Standalone test double for WordPress core's wp_parse_url(). 17 } 12 18 require_once dirname( __DIR__ ) . '/includes/class-url-replacer.php'; 13 19 use HaseebVault\URL_Replacer; … … 27 33 'Gutenberg content ' . $old . '/page', 28 34 serialize( array( 'widget' => array( 'link' => $old . '/shop' ) ) ), 35 'Alternate scheme http://oldsite.com/account', 36 'Escaped https:\/\/oldsite.com\/api', 37 'Encoded ' . rawurlencode( $old . '/checkout' ), 38 'Scheme relative //oldsite.com/media.jpg', 29 39 ); 30 40 foreach ( $cases as $case ) { … … 32 42 $normalized = str_replace( '\\/', '/', $result ); 33 43 assert( false === strpos( $normalized, $old ) ); 34 assert( false !== strpos( $normalized, $new ) ); 44 assert( false === strpos( $normalized, 'http://oldsite.com' ) ); 45 assert( false === strpos( $normalized, rawurlencode( $old ) ) ); 46 assert( false === strpos( $normalized, '//oldsite.com' ) ); 47 assert( 48 false !== strpos( $normalized, $new ) || 49 false !== strpos( $normalized, rawurlencode( $new ) ) || 50 false !== strpos( $normalized, '//new.example' ) 51 ); 35 52 if ( is_serialized( $case ) ) { 36 53 assert( false !== @unserialize( $result, array( 'allowed_classes' => false ) ) );
Note:
See TracChangeset
for help on using the changeset viewer.