public function run_with_initialized_file_system() { $fs = new FW_Backup_Helper_File_System(); $db = new FW_Backup_Helper_Database(); $auto_install_dir = $this->backup()->get_auto_install_dir(); $upload_dir = wp_upload_dir(); $upload_dir = $upload_dir['basedir']; try { // Do Full Backup before Auto Install if (apply_filters('fw_ext_backup_do_backup_on_demo_content_install', true)) { $this->backup()->action()->do_backup_background_cron('cron_full'); } // Replace uploads directory $t = $fs->replace($fs->map($upload_dir), $fs->map("{$auto_install_dir}/uploads")); // Move backup directory from trashed dir into new upload dir if ($t) { $fs->move_existing("{$t}/backup", $fs->map("{$upload_dir}/backup")); // Remove trashed dir because we made Full Backup of the site $fs->rmdir($t); } if (file_exists("{$auto_install_dir}/database.txt")) { $file_with_sql = "{$auto_install_dir}/database.txt"; } else { $file_with_sql = "{$auto_install_dir}/database.sql"; } $db->import($file_with_sql, true, true, true); do_action('fw_ext_backup_after_import_demo_content'); return array('success' => true); } catch (FW_Backup_Exception $exception) { return array('success' => false, 'data' => array('message' => $exception->getMessage())); } }
public function run() { $fs = new FW_Backup_Helper_File_System(); $db = new FW_Backup_Helper_Database(); $auto_install_dir = $this->backup()->get_auto_install_dir(); $upload_dir = wp_upload_dir(); $upload_dir = $upload_dir['basedir']; // This forces *Restore* page to be opened event if // request_filesystem_credentials is not required. In the // latter case JavaScript will submit the page automatically // which opens up a *Restore in Progress* popup. if ($_SERVER['REQUEST_METHOD'] != 'POST') { throw new FW_Backup_Exception_Method_Not_Allowed(); } $restore = new FW_Backup_Process_Restore(); $restore->request_filesystem_credentials(); try { $fs->check_permissions(); // Do Full Backup before Auto Install $this->backup()->action()->do_backup_background_cron('cron_full'); // Replace uploads directory $t = $fs->replace($fs->map($upload_dir), $fs->map("{$auto_install_dir}/uploads")); // Move backup directory from trashed dir into new upload dir if ($t) { $fs->move_existing("{$t}/backup", $fs->map("{$upload_dir}/backup")); // Remove trashed dir because we made Full Backup of the site $fs->rmdir($t); } if (file_exists("{$auto_install_dir}/database.txt")) { $file_with_sql = "{$auto_install_dir}/database.txt"; } else { $file_with_sql = "{$auto_install_dir}/database.sql"; } $db->import($file_with_sql, true, true, true); } catch (FW_Backup_Exception $exception) { FW_Flash_Messages::add('auto-install', $exception->getMessage(), 'error'); // otherwise flash messages wont show wp_redirect($this->backup()->action()->url_backup_auto_install_page()); exit; } // get rid of update notifications wp_redirect(admin_url('update-core.php?force-check=1&auto-install-redirect=' . esc_url(admin_url()))); exit; }
public function import_fp($fp, $keep_users_tables = false, $fix_foreign_database = false, $keep_options = false) { /** * @var wpdb $wpdb */ global $wpdb; $helper = new FW_Backup_Helper_Database(); $exporter = new FW_Backup_Export_Database(); /** * fixme: all options should have bool for wp_option autoload | array( 'option_name' => (bool)autoload ) */ $option_list = array($wpdb->prefix . 'user_roles', 'siteurl', 'blogname', 'blog_charset', 'blogdescription', 'admin_email', 'mailserver_url', 'mailserver_login', 'mailserver_pass', 'mailserver_port', 'ftp_credentials', 'use_ssl', 'template', 'stylesheet', 'current_theme', 'WPLANG'); $option_list = apply_filters('fw_ext_backup_import_skip_options', $option_list); // Preserve some options $before = array_map('get_option', $option_list); $before = array_combine($option_list, $before); // Preserve Backup History and Backup Settings $history = $exporter->export_history(); $settings = $exporter->export_settings(); // Import database (preserve user related tables) // ============================================== if ($keep_users_tables) { $foreign_prefix = $exporter->import_fp($fp, array($wpdb->users, $wpdb->usermeta)); } else { $foreign_prefix = $exporter->import_fp($fp); } wp_cache_flush(); FW_Cache::clear(); $fw_extensions_data = get_option('fw_extensions', array()); if (!empty($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl']) && $fix_foreign_database) { $wp_upload_dir = wp_upload_dir(); // Fix database if ($fix_foreign_database) { $helper->fix_foreign_database(array(fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl']) => fw_get_url_without_scheme($wp_upload_dir['baseurl']), str_replace('/', '\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme($fw_extensions_data[$this->backup()->get_name()]['wp_upload_dir']['baseurl'] . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($wp_upload_dir['baseurl'] . '/')))); } } // Restore Backup History and Settings $exporter->import_history($history); $exporter->import_settings($settings); // Fix database if ($fix_foreign_database) { $uploadDir = wp_upload_dir(); $uploadOld = fw_get_url_without_scheme(site_url() . '/wp-content/uploads/'); $uploadNew = fw_get_url_without_scheme($uploadDir['baseurl'] . '/'); $helper->fix_foreign_database(array($uploadOld => $uploadNew, str_replace('/', '\\/', $uploadOld) => str_replace('/', '\\/', $uploadNew), str_replace('/', '\\\\/', $uploadOld) => str_replace('/', '\\\\/', $uploadNew), str_replace('/', '\\\\/', $uploadOld) => str_replace('/', '\\\\/', $uploadNew), site_url() => $before['siteurl'], site_url() . '/' => $before['siteurl'] . '/', fw_get_url_without_scheme(site_url() . '/') => fw_get_url_without_scheme($before['siteurl'] . '/'), str_replace('/', '\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/')))); $helper->fix_wp_options($foreign_prefix); } wp_cache_flush(); FW_Cache::clear(); // Restore options if ($keep_options) { // WP keeps stylesheet settings in theme_mods_{stylesheet} option, // that means that if stylesheet option has different value in dump file and in database // new theme_mods_{stylesheet} should be rename to old theme_mods_{stylesheet} $stylesheet = get_option('stylesheet'); if ($before['stylesheet'] != $stylesheet) { $theme_mods_before = 'theme_mods_' . $before['stylesheet']; $theme_mods_after = 'theme_mods_' . $stylesheet; $query = $wpdb->prepare("\n\t\t\t\t\tDELETE FROM\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tWHERE\n\t\t\t\t\t option_name = %s\n\t\t\t\t", $theme_mods_before); $wpdb->query($query); $query = $wpdb->prepare("\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tSET\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t\tWHERE\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t", $theme_mods_before, $theme_mods_after); $wpdb->query($query); } // Restore all saved options array_map('update_option', array_keys($before), $before); } // Actualize settings $this->backup()->cron()->reschedule(); wp_cache_flush(); FW_Cache::clear(); }
/** * @internal */ public function _query($headers, FW_Backup_Helper_Database $db, $stmt) { $_translate_table_prefix = FW_Backup_Callable::make(array($this, '_translate_table_prefix'), $headers); $sql = preg_replace_callback('/^(CREATE TABLE|INSERT INTO) `(\\w+)`/m', $_translate_table_prefix, $stmt); $db->query($sql); }
public function import_fp($fp, $keep_users_table = false, $fix_foreign_database = false, $keep_options = false) { /** * @var wpdb $wpdb */ global $wpdb; $helper = new FW_Backup_Helper_Database(); $exporter = new FW_Backup_Export_Database(); $option_list = array($wpdb->prefix . 'user_roles', 'siteurl', 'blogname', 'blog_charset', 'blogdescription', 'admin_email', 'mailserver_url', 'mailserver_login', 'mailserver_pass', 'mailserver_port', 'ftp_credentials', 'use_ssl', 'template', 'stylesheet', 'current_theme'); // Preserve some options $before = array_map('get_option', $option_list); $before = array_combine($option_list, $before); // Preserve Backup History and Backup Settings $history = $exporter->export_history(); $settings = $exporter->export_settings(); // Import database (preserve user related tables) // ============================================== if ($keep_users_table) { $foreign_prefix = $exporter->import_fp($fp, array($wpdb->users)); } else { $foreign_prefix = $exporter->import_fp($fp); } wp_cache_flush(); // Fix database if ($fix_foreign_database) { $helper->fix_foreign_database(array(site_url() => $before['siteurl'], site_url() . '/' => $before['siteurl'] . '/', fw_get_url_without_scheme(site_url() . '/') => fw_get_url_without_scheme($before['siteurl'] . '/'), str_replace('/', '\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/')), str_replace('/', '\\\\/', fw_get_url_without_scheme(site_url() . '/')) => str_replace('/', '\\\\/', fw_get_url_without_scheme($before['siteurl'] . '/'))), $foreign_prefix); } wp_cache_flush(); // Restore Backup History and Settings $exporter->import_history($history); $exporter->import_settings($settings); // Restore options if ($keep_options) { // WP keeps stylesheet settings in theme_mods_{stylesheet} option, // that means that if stylesheet option has different value in dump file and in database // new theme_mods_{stylesheet} should be rename to old theme_mods_{stylesheet} $stylesheet = get_option('stylesheet'); if ($before['stylesheet'] != $stylesheet) { $theme_mods_before = 'theme_mods_' . $before['stylesheet']; $theme_mods_after = 'theme_mods_' . $stylesheet; $query = $wpdb->prepare("\n\t\t\t\t\tDELETE FROM\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tWHERE\n\t\t\t\t\t option_name = %s\n\t\t\t\t", $theme_mods_before); $wpdb->query($query); $query = $wpdb->prepare("\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\t{$wpdb->options}\n\t\t\t\t\tSET\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t\tWHERE\n\t\t\t\t\t\toption_name = %s\n\t\t\t\t", $theme_mods_before, $theme_mods_after); $wpdb->query($query); } // Restore all saved options array_map('update_option', array_keys($before), $before); } // Actualize settings $this->backup()->cron()->reschedule(); wp_cache_flush(); }
public function run($post_id) { /** * @var WP_Filesystem_Base $wp_filesystem */ global $wp_filesystem; set_time_limit(0); $fs = new FW_Backup_Helper_File_System(); $db = new FW_Backup_Helper_Database(); $backup_info = $this->backup()->get_backup_info($post_id); if (!$backup_info || !$backup_info->is_completed()) { throw new FW_Backup_Exception('Cannot restore from incomplete backup'); } $storage = $this->backup()->get_storage($backup_info->get_storage(), $backup_info->get_cron_job()); // Ensure that storage layer is workable (e.g. was configured properly) $storage->ping(new FW_Backup_Feedback_Void()); if ($backup_info->has_fs()) { $this->request_filesystem_credentials(); } // This forces *Restore* page to be opened event if // request_filesystem_credentials is not required. In the // latter case JavaScript will submit the page automatically // which opens up a *Restore in Progress* popup. if ($_SERVER['REQUEST_METHOD'] != 'POST') { throw new FW_Backup_Exception_Method_Not_Allowed(); } if ($backup_info->has_fs()) { if ($wp_filesystem->abspath() == '/') { throw new FW_Backup_Exception('WordPress was located at root directory (/). Restoration for this scenario was not implemented.'); } $fs->check_permissions(); } if ($backup_info->has_db()) { $db->check_permissions(); } // 2) Do restore try { $f = new FW_Backup_Feedback_Void(); $zip_file = $storage->fetch($backup_info->get_storage_file(), $f); // Try to open it as .zip archive $zip = new ZipArchive(); if ($zip->open($zip_file) !== true) { throw new FW_Backup_Exception('Cannot open .zip file'); } // Restore files (should come before database: rename wordpress dir can fail) if ($backup_info->has_fs()) { $wp = rtrim($wp_filesystem->abspath(), '/'); $wp_new = rtrim(dirname($wp), '/') . '/' . uniqid(); $wp_backup = $wp . '-' . date('Y_m_d-H_i_s', current_time('timestamp')); $backup_rel = substr($this->backup()->get_backup_dir(), strlen(ABSPATH)); // Extract archive into temporary location unzip_file($zip_file, $wp_new); // Get rid of database.txt file, if any if ($wp_filesystem->exists("{$wp_new}/database.txt")) { $wp_filesystem->delete("{$wp_new}/database.txt"); } // In imported backup files wp-config.php can contain database // credentials which cannot work on this host. Also, database prefix // can be different from the value in original wp-config.php if ($backup_info->is_imported()) { $wp_filesystem->copy("{$wp}/wp-config.php", "{$wp_new}/wp-config.php", true); } // Replace WordPress dir by version from backup $move = true && $wp_filesystem->move($wp, $wp_backup) && $wp_filesystem->move($wp_new, $wp) && $wp_filesystem->move("{$wp_backup}/{$backup_rel}", "{$wp}/{$backup_rel}"); if (!$move) { throw new FW_Backup_Exception('Replacing wordpress dir by version from backup failed'); } } // Restore database if ($backup_info->has_db()) { $fp_db = $zip->getStream('database.txt'); $db->import_fp($fp_db, false, $backup_info->is_imported(), false); fclose($fp_db); } } catch (FW_Backup_Exception $exception) { } unset($zip); if (isset($zip_file)) { unlink($zip_file); } if (isset($exception)) { throw $exception; } }