Plugin Directory

Changeset 3663734


Ignore:
Timestamp:
08/24/2026 03:02:46 PM (3 months ago)
Author:
haseebabidofficial
Message:

Issues fixed. Updated version uploaded and tested.

Location:
haseebvault-backup-migration/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • haseebvault-backup-migration/trunk/assets/js/admin.js

    r3662954 r3663734  
    6161    locked: false,
    6262    terminal: false,
     63    redirectUrl: '',
    6364  };
    6465  const stageLabels = {
    … …  
    100101    if (cancelButton) {
    101102      cancelButton.hidden = !canCancel && !runner.terminal;
    102       cancelButton.textContent = runner.terminal ? 'Close' : 'Cancel';
     103      cancelButton.textContent = runner.terminal && runner.redirectUrl ? 'OK' : (runner.terminal ? 'Close' : 'Cancel');
    103104    }
    104105  }
    … …  
    137138    runner.kind = '';
    138139    runner.jobId = '';
     140    runner.redirectUrl = '';
    139141    if (progress) progress.hidden = true;
    140142  }
    … …  
    171173    runner.running = false;
    172174    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      }
    177183    } else if (job.status === 'failed') {
    178184      markTerminal(job, true);
    … …  
    376382  if (cancelButton) cancelButton.addEventListener('click', async () => {
    377383    if (runner.terminal) {
     384      if (runner.redirectUrl) {
     385        window.location.assign(runner.redirectUrl);
     386        return;
     387      }
    378388      closeProgress();
    379389      return;
  • haseebvault-backup-migration/trunk/haseebvault.php

    r3662954 r3663734  
    33 * Plugin Name: HaseebVault - Backup & Migration
    44 * Description: Secure WordPress backup, migration, import, and export with resumable processing, integrity checks, and safe URL replacement.
    5  * Version:     1.1.6
     5 * Version:     1.1.7
    66 * Requires at least: 6.2
    77 * Requires PHP: 7.4
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'HASEEBVAULT_VERSION', '1.1.6' );
     20define( 'HASEEBVAULT_VERSION', '1.1.7' );
    2121define( 'HASEEBVAULT_FORMAT_VERSION', '1.0' );
    2222define( 'HASEEBVAULT_FILE', __FILE__ );
  • haseebvault-backup-migration/trunk/includes/class-database-restorer.php

    r3662954 r3663734  
    5454                                }
    5555                                $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                                }
    5664                                $sql = $this->map_prefix( $line, $state['source_prefix'], $state['destination_prefix'] );
    5765                                if ( 0 === stripos( $sql, 'INSERT INTO ' ) ) {
    … …  
    7785                        'complete' => $eof,
    7886                );
     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 );
    79103        }
    80104
    … …  
    123147                                        array(
    124148                                                '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.
    126150                                        ),
    127151                                        array( '%d', '%s' )
    … …  
    131155                                        array(
    132156                                                '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.
    135159                                        ),
    136160                                        array( '%d', '%s', '%s' )
  • haseebvault-backup-migration/trunk/includes/class-restore.php

    r3662954 r3663734  
    4141                        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' ) );
    4242                }
    43                 if ( 'RESTORE' !== strtoupper( trim( (string) ( $request['confirmation'] ?? '' ) ) ) ) {
     43                if ( 'IMPORT' !== strtoupper( trim( (string) ( $request['confirmation'] ?? '' ) ) ) ) {
    4444                        throw new RuntimeException( esc_html__( 'Type IMPORT to confirm this operation.', 'haseebvault-backup-migration' ) );
    4545                }
    … …  
    6868                $restore_request = array(
    6969                        'backup_id'    => $backup['backup_id'],
    70                         'confirmation' => 'RESTORE',
     70                        'confirmation' => 'IMPORT',
    7171                        'mode'         => $mode,
    7272                        'components'   => array_values( $selected ),
    … …  
    102102                        'old_url'            => untrailingslashit( esc_url_raw( $request['old_url'] ) ),
    103103                        'new_url'            => untrailingslashit( esc_url_raw( $request['new_url'] ) ),
     104                        'new_site_url'       => $this->destination_site_url( $request['new_url'], $backup['manifest'] ),
    104105                        'replace_guid'       => ! empty( $request['replace_guid'] ),
    105106                        'source_prefix'      => (string) ( $backup['manifest']['database_prefix'] ?? $wpdb->prefix ),
    … …  
    112113                        'administrator'      => $this->snapshot_administrator( $user_id ),
    113114                        '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() ),
    116121                        ),
     122                        'source_urls'        => $this->source_urls( $request, $backup['manifest'] ),
    117123                        'started_at'         => time(),
    118124                );
    … …  
    209215
    210216                                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 ) ) {
    212218                                                $job = $this->jobs->touch_state( $job_id, $state, 'finalize', 96 );
    213219                                                break;
    … …  
    218224                                        $result   = $this->replacer->process( $state );
    219225                                        $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 );
    220230                                        $stage    = $result['complete'] ? 'finalize' : 'replace_urls';
    221231                                        $total    = max( 1, count( $state['replace_tables'] ) );
    … …  
    341351                        return in_array( 'other', $components, true );
    342352                }
    343                 return in_array( 'WordPress', $components, true );
     353                return in_array( 'wordpress', $components, true );
    344354        }
    345355
    … …  
    419429
    420430        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                }
    425445                $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;
    426505        }
    427506
    … …  
    430509                        if ( ! empty( $state['new_url'] ) ) {
    431510                                update_option( 'home', $state['new_url'] );
    432                                 update_option( 'siteurl', $state['new_url'] );
     511                                update_option( 'siteurl', $state['new_site_url'] ?? $state['new_url'] );
    433512                        }
    434513                        wp_cache_flush();
  • haseebvault-backup-migration/trunk/includes/class-url-replacer.php

    r3662954 r3663734  
    6767                                                continue;
    6868                                        }
    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 ) ) {
    7271                                                continue;
    7372                                        }
    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                                        }
    7580                                        if ( $replaced !== $value ) {
    7681                                                $changes[ $column ] = $replaced;
    … …  
    113118                        return $value;
    114119                }
    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 ) ) {
    117122                        return $value;
    118123                }
    … …  
    120125                        $unserialized = @unserialize( $value, array( 'allowed_classes' => false ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
    121126                        if ( false !== $unserialized || 'b:0;' === $value ) {
    122                                 return serialize( self::replace_recursive( $unserialized, $old, $new ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
     127                                return serialize( self::replace_recursive( $unserialized, $pairs ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
    123128                        }
    124129                }
    … …  
    127132                        $decoded = json_decode( $value, true );
    128133                        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 ) {
    136141                if ( is_string( $value ) ) {
    137                         return str_replace( $old, $new, $value );
     142                        return strtr( $value, $pairs );
    138143                }
    139144                if ( is_array( $value ) ) {
    140145                        $replaced = array();
    141146                        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 );
    144149                        }
    145150                        return $replaced;
    … …  
    150155                                        continue;
    151156                                }
    152                                 $value->{$key} = self::replace_recursive( $item, $old, $new );
     157                                $value->{$key} = self::replace_recursive( $item, $pairs );
    153158                        }
    154159                }
    155160                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;
    156210        }
    157211
  • haseebvault-backup-migration/trunk/readme.txt

    r3662954 r3663734  
    55Tested up to: 7.0
    66Requires PHP: 7.4
    7 Stable tag: 1.1.6
     7Stable tag: 1.1.7
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    9696== Changelog ==
    9797
     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
    98106= 1.1.6 =
    99107* Removed the fixed directory-depth assumption from the archive recovery regression test bootstrap.
  • haseebvault-backup-migration/trunk/tests/test-url-replacer.php

    r3662954 r3663734  
    1010function wp_json_encode( $value, $flags = 0 ) {
    1111        return json_encode( $value, $flags ); }
     12function untrailingslashit( $value ) {
     13        return rtrim( $value, '/\\' );
     14}
     15function 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}
    1218require_once dirname( __DIR__ ) . '/includes/class-url-replacer.php';
    1319use HaseebVault\URL_Replacer;
    … …  
    2733        'Gutenberg content ' . $old . '/page',
    2834        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',
    2939);
    3040foreach ( $cases as $case ) {
    … …  
    3242        $normalized = str_replace( '\\/', '/', $result );
    3343        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        );
    3552        if ( is_serialized( $case ) ) {
    3653                assert( false !== @unserialize( $result, array( 'allowed_classes' => false ) ) );
Note: See TracChangeset for help on using the changeset viewer.