public function export(FW_Backup_Interface_Feedback $feedback)
 {
     $db = new FW_Backup_Export_Database();
     $fs = new FW_Backup_Export_File_System();
     $zip_file = sprintf('%s/backup-full-%s.zip', get_temp_dir(), date('Y_m_d-H_i_s'));
     $tmp_file = array();
     try {
         touch($zip_file);
         $zip = new ZipArchive();
         if ($zip->open($zip_file) !== true) {
             throw new FW_Backup_Exception(__('Could not create .zip file', 'fw'));
         }
         $fs->append_zip($zip, ABSPATH, '', $feedback);
         $zip->addFile($tmp_file[] = $db->export_sql($feedback), 'database.txt');
         $feedback->set_task(__('Compressing files...', 'fw'));
         $zip->close();
     } catch (FW_Backup_Exception $exception) {
         unset($zip);
         unlink($zip_file);
     }
     array_map('unlink', $tmp_file);
     if (isset($exception)) {
         throw $exception;
     }
     return $zip_file;
 }
 public function export(FW_Backup_Interface_Feedback $feedback)
 {
     /**
      * @var wpdb $wpdb
      */
     global $wpdb;
     $db = new FW_Backup_Export_Database();
     $fs = new FW_Backup_Export_File_System();
     $zip_file = sprintf('%s/backup-demo-install-%s.zip', get_temp_dir(), date('Y_m_d-H_i_s'));
     $tmp_file = array();
     try {
         touch($zip_file);
         $zip = new ZipArchive();
         if ($zip->open($zip_file) !== true) {
             throw new FW_Backup_Exception(__('Could not create .zip file', 'fw'));
         }
         $upload_dir = wp_upload_dir();
         fw_set_db_extension_data($this->backup()->get_name(), 'wp_upload_dir', $upload_dir);
         $upload_dir = $upload_dir['basedir'];
         $stylesheet_dir = get_stylesheet_directory();
         $template_dir = get_template_directory();
         // Do not put auto-install directory which comes with theme into archive
         $exclude = array();
         if ($a = $this->backup()->get_auto_install_dir()) {
             $exclude[] = $a;
         }
         $fs->append_zip($zip, $template_dir, basename($template_dir) . '/', $feedback, $exclude);
         if ($stylesheet_dir != $template_dir) {
             $fs->append_zip($zip, $stylesheet_dir, basename($stylesheet_dir) . '/', $feedback, $exclude);
         }
         $fs->append_zip($zip, $upload_dir, basename($template_dir) . '/auto-install/uploads/', $feedback);
         $options_where = "WHERE option_name NOT LIKE 'fw_backup.%%' AND option_name NOT IN ('ftp_credentials', 'mailserver_url', 'mailserver_login', 'mailserver_pass', 'mailserver_port', 'admin_email')";
         $exclude_table = array($wpdb->users);
         $zip->addFile($tmp_file[] = $db->export_sql($feedback, $options_where, $exclude_table), basename($template_dir) . '/auto-install/database.txt');
         $feedback->set_task(__('Compressing files...', 'fw'));
         $zip->close();
     } catch (FW_Backup_Exception $exception) {
         unset($zip);
         unlink($zip_file);
     }
     array_map('unlink', $tmp_file);
     if (isset($exception)) {
         throw $exception;
     }
     return $zip_file;
 }
 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();
 }
 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();
 }