prefix}*` * tables, never arbitrary ones. Row (INSERT) entries are executed as a single * statement so user data that happens to contain ";\n" is never mis-split. * * @package AI_SiteArk * Copyright (c) 2026 AtoZ INFOWAY INC. * Licensed under the GNU General Public License, version 2 or later. */ namespace AtoZ\SiteArk\Restore; defined( 'ABSPATH' ) || exit; /** * Class DB_Importer */ class DB_Importer { /** * Execute one DB archive entry. * * @param string $kind 'create' | 'rows'. * @param string $table Original table name (from the archive header). * @param string $sql Decoded SQL for the entry. * @param string $old_prefix Backup's table prefix. * @param string $new_prefix This site's table prefix. * @return bool Whether the entry was executed. */ public static function execute_entry( $kind, $table, $sql, $old_prefix, $new_prefix ) { global $wpdb; // Views / triggers / events restore after their base tables (they appear // last in the archive). Each is prefix-scoped and DEFINER-stripped so a // crafted archive can only ever create objects on this site's own tables. if ( in_array( $kind, array( 'view', 'trigger', 'event' ), true ) ) { return self::execute_object( $kind, $table, $sql, $old_prefix, $new_prefix ); } $new_table = self::rewrite_table( $table, $old_prefix, $new_prefix ); // Safety scope: only ever touch this site's own prefixed tables. if ( '' === $new_table || 0 !== strpos( $new_table, $new_prefix ) ) { return false; } // Rewrite the exact backticked table identifier (precise; never touches data). if ( $table !== $new_table ) { $sql = str_replace( '`' . $table . '`', '`' . $new_table . '`', $sql ); } if ( 'create' === $kind ) { // DROP + CREATE — no user data, so ";\n" splitting is safe. Each // statement must be a DROP/CREATE of exactly the scoped table, so a // crafted archive can't smuggle extra SQL into a create entry. $drop = 'DROP TABLE IF EXISTS `' . $new_table . '`'; $create = 'CREATE TABLE `' . $new_table . '`'; foreach ( array_filter( array_map( 'trim', explode( ";\n", $sql ) ) ) as $stmt ) { if ( 0 !== stripos( $stmt, $drop ) && 0 !== stripos( $stmt, $create ) ) { continue; // Reject anything that isn't this table's DROP/CREATE. } self::run_query( $stmt ); } return true; } // Row inserts: one statement, executed whole. $stmt = rtrim( trim( $sql ), ';' ); if ( '' !== $stmt ) { self::run_query( $stmt ); } return true; } /** * Restore a view / trigger / event. Prefix-scoped and shape-guarded: the object * (or, for a trigger, its target table) must resolve to this site's own prefix, * and the statement must actually be a CREATE of that object kind — so a crafted * archive can't smuggle arbitrary SQL through a meta entry. * * @param string $kind view|trigger|event. * @param string $ident View/event name, or a trigger's target table. * @param string $sql CREATE statement (DEFINER already stripped at export). * @param string $old_prefix Backup prefix. * @param string $new_prefix This site's prefix. * @return bool Whether it was executed. */ private static function execute_object( $kind, $ident, $sql, $old_prefix, $new_prefix ) { $sql = trim( (string) $sql ); if ( 0 !== stripos( $sql, 'CREATE' ) ) { return false; // Only CREATE statements. } // Rewrite backticked prefix on identifiers (object name + referenced tables). if ( '' !== $old_prefix && $old_prefix !== $new_prefix ) { $sql = str_replace( '`' . $old_prefix, '`' . $new_prefix, $sql ); } if ( 'trigger' === $kind ) { $new_table = self::rewrite_table( $ident, $old_prefix, $new_prefix ); if ( '' === $new_table || 0 !== strpos( $new_table, $new_prefix ) ) { return false; // Trigger must fire on one of our own tables. } if ( false === stripos( $sql, 'TRIGGER' ) || false === strpos( $sql, 'ON `' . $new_table . '`' ) ) { return false; // Body must be a trigger on exactly that table. } if ( ! preg_match( '/TRIGGER\s+`((?:[^`]|``)+)`/i', $sql, $m ) ) { return false; } $name = str_replace( '`', '', $m[1] ); self::run_query( 'DROP TRIGGER IF EXISTS `' . $name . '`' ); self::run_query( $sql ); return true; } // View or event: the object name itself must be prefixed. $new_name = self::rewrite_table( $ident, $old_prefix, $new_prefix ); if ( '' === $new_name || 0 !== strpos( $new_name, $new_prefix ) ) { return false; } $word = ( 'view' === $kind ) ? 'VIEW' : 'EVENT'; if ( false === strpos( $sql, $word . ' `' . $new_name . '`' ) ) { return false; // Must create exactly this prefixed object. } self::run_query( 'DROP ' . $word . ' IF EXISTS `' . $new_name . '`' ); self::run_query( $sql ); return true; } /** * Run one statement, retrying a few times on a transient error (deadlock, * lock-wait timeout, dropped connection). * * @param string $stmt SQL statement. * @return void */ private static function run_query( $stmt ) { global $wpdb; for ( $i = 0; $i < 3; $i++ ) { $wpdb->last_error = ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- executes a statement from the backup archive being restored (import), by design. $wpdb->query( $stmt ); $err = strtolower( (string) $wpdb->last_error ); if ( '' === $err ) { return; } $transient = false; foreach ( array( 'deadlock', 'lock wait timeout', 'try restarting transaction', 'server has gone away', 'lost connection' ) as $needle ) { if ( false !== strpos( $err, $needle ) ) { $transient = true; break; } } if ( ! $transient ) { return; } usleep( 150000 * ( $i + 1 ) ); } } /** * Map an old-prefix table name onto the new prefix. * * @param string $table Table name. * @param string $old_prefix Old prefix. * @param string $new_prefix New prefix. * @return string */ private static function rewrite_table( $table, $old_prefix, $new_prefix ) { $table = str_replace( '`', '', (string) $table ); if ( $old_prefix !== $new_prefix && '' !== $old_prefix && 0 === strpos( $table, $old_prefix ) ) { return $new_prefix . substr( $table, strlen( $old_prefix ) ); } return $table; } }