/**
  * {@inheritdoc}
  * @param array $args
  * * zip - file path
  * * dir - where the zip file will be extract
  */
 public function execute(array $args, array $state = array())
 {
     if (!isset($args['zip'])) {
         return new WP_Error('no_zip', __('Zip file not specified', 'fw'));
     } else {
         $args['zip'] = fw_fix_path($args['zip']);
     }
     if (!isset($args['dir'])) {
         return new WP_Error('no_dir', __('Destination dir not specified', 'fw'));
     } else {
         $args['dir'] = fw_fix_path($args['dir']);
     }
     if (empty($state)) {
         if (!fw_ext_backups_is_dir_empty($args['dir'])) {
             return new WP_Error('destination_not_empty', __('Destination dir is not empty', 'fw'));
         }
         $state = array('entry' => '', 'extracted_files' => 0);
     }
     wp_cache_flush();
     FW_Cache::clear();
     if (is_wp_error($extract_result = fw_ext_backups_unzip_partial($args['zip'], $args['dir'], $state['entry']))) {
         return $extract_result;
     } else {
         if ($extract_result['finished']) {
             return true;
         } else {
             $state['entry'] = $extract_result['last_entry'];
             $state['extracted_files'] += $extract_result['extracted_files'];
             return $state;
         }
     }
 }
 /**
  * {@inheritdoc}
  * @param array $args
  * * source_dir - everything from this directory will be added in zip
  * * destination_dir - where the zip file will be created
  *
  * Warning!
  *  Zip can't be executed in steps, it will execute way too long,
  *  because it is impossible to update a zip file, every time you add a file to zip,
  *  a new temp copy of original zip is created with new modifications, it is compressed,
  *  and the original zip is replaced. So when the zip will grow in size,
  *  just adding a single file, will take a very long time.
  */
 public function execute(array $args, array $state = array())
 {
     if (!isset($args['source_dir'])) {
         return new WP_Error('no_source_dir', __('Source dir not specified', 'fw'));
     } else {
         $args['source_dir'] = fw_fix_path($args['source_dir']);
     }
     if (!isset($args['destination_dir'])) {
         return new WP_Error('no_destination_dir', __('Destination dir not specified', 'fw'));
     } else {
         $args['destination_dir'] = fw_fix_path($args['destination_dir']);
     }
     if (!class_exists('ZipArchive')) {
         return new WP_Error('zip_ext_missing', __('Zip extension missing', 'fw'));
     }
     $zip_path = $args['source_dir'] . '/' . implode('-', array('fw-backup', date('Y_m_d-H_i_s'), fw_ext('backups')->manifest->get_version())) . '.zip';
     $zip = new ZipArchive();
     if (false === ($zip_error_code = $zip->open($zip_path, ZipArchive::CREATE))) {
         return new WP_Error('cannot_open_zip', sprintf(__('Cannot open zip (Error code: %s)', 'fw'), $zip_error_code));
     }
     $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($args['source_dir']), RecursiveIteratorIterator::LEAVES_ONLY);
     foreach ($files as $name => $file) {
         if (!$file->isDir()) {
             // Skip directories (they would be added automatically)
             if (($file_path = $file->getRealPath()) !== $zip_path) {
                 $zip->addFile($file_path, substr(fw_fix_path($file_path), strlen($args['source_dir']) + 1));
             }
         }
     }
     wp_cache_flush();
     FW_Cache::clear();
     // Zip archive will be created only after closing object
     if (!$zip->close()) {
         return new WP_Error('cannot_close_zip', __('Cannot close the zip file', 'fw'));
     }
     if (!rename($zip_path, $args['destination_dir'] . '/' . basename($zip_path))) {
         return new WP_Error('cannot_move_zip', __('Cannot move zip in destination dir', 'fw'));
     }
     return true;
 }
 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();
 }