/** * extract() * * Extract backup zip file. * * @return array True if the extraction was a success OR skipping of extraction is set. */ function extract_files() { if ( true === pb_backupbuddy::$options['skip_files'] ) { // Option to skip all file updating / extracting. pb_backupbuddy::status( 'message', 'Skipped extracting files based on debugging options.' ); return true; } else { pb_backupbuddy::set_greedy_script_limits(); pb_backupbuddy::status( 'message', 'Unzipping into `' . ABSPATH . '`' ); $backup_archive = ABSPATH . pb_backupbuddy::$options['file']; $destination_directory = ABSPATH; // Set compatibility mode if defined in advanced options. $compatibility_mode = false; // Default to no compatibility mode. if ( pb_backupbuddy::$options['force_compatibility_medium'] != false ) { $compatibility_mode = 'ziparchive'; } elseif ( pb_backupbuddy::$options['force_compatibility_slow'] != false ) { $compatibility_mode = 'pclzip'; } // Zip & Unzip library setup. require_once( ABSPATH . 'importbuddy/lib/zipbuddy/zipbuddy.php' ); $_zipbuddy = new pluginbuddy_zipbuddy( ABSPATH, '', 'unzip' ); // Extract zip file & verify it worked. if ( true !== ( $result = $_zipbuddy->unzip( $backup_archive, $destination_directory, $compatibility_mode ) ) ) { pb_backupbuddy::status( 'error', 'Failed unzipping archive.' ); pb_backupbuddy::alert( 'Failed unzipping archive.', true ); return false; } else { // Reported success; verify extraction. $_backupdata_file = ABSPATH . 'wp-content/uploads/temp_' . pb_backupbuddy::$options['zip_id'] . '/backupbuddy_dat.php'; // Full backup dat file location $_backupdata_file_dbonly = ABSPATH . 'backupbuddy_dat.php'; // DB only dat file location $_backupdata_file_new = ABSPATH . 'wp-content/uploads/backupbuddy_temp/' . pb_backupbuddy::$options['zip_id'] . '/backupbuddy_dat.php'; // Full backup dat file location if ( !file_exists( $_backupdata_file ) && !file_exists( $_backupdata_file_dbonly ) && !file_exists( $_backupdata_file_new ) ) { pb_backupbuddy::status( 'error', 'Error #9004: Key files missing.', 'The unzip process reported success but the backup data file, backupbuddy_dat.php was not found in the extracted files. The unzip process either failed (most likely) or the zip file is not a proper BackupBuddy backup.' ); pb_backupbuddy::alert( 'Error: Key files missing. The unzip process reported success but the backup data file, backupbuddy_dat.php was not found in the extracted files. The unzip process either failed (most likely) or the zip file is not a proper BackupBuddy backup.', true, '9004' ); return false; } pb_backupbuddy::status( 'details', 'Success extracting Zip File "' . ABSPATH . pb_backupbuddy::$options['file'] . '" into "' . ABSPATH . '".' ); return true; } } }
$this->status('message', 'Skipping file extraction based on advanced settings.'); $result = true; } else { // Set compatibility mode if defined in advanced options. $compatibility_mode = false; // Default to no compatibility mode. if (isset($this->advanced_options['force_compatibility_medium']) && $this->advanced_options['force_compatibility_medium'] == "true") { $compatibility_mode = 'ziparchive'; } elseif (isset($this->advanced_options['force_compatibility_slow']) && $this->advanced_options['force_compatibility_slow'] == "true") { $compatibility_mode = 'pclzip'; } // Zip & Unzip library setup. require_once $this->_pluginPath . '/lib/zipbuddy/zipbuddy.php'; $_zipbuddy = new pluginbuddy_zipbuddy(ABSPATH, '', 'unzip'); $_zipbuddy->set_status_callback(array(&$this, 'status')); $result = $_zipbuddy->unzip($backup_archive, $destination_directory, $compatibility_mode); } echo '<script type="text/javascript">jQuery("#pb_importbuddy_working").hide();</script>'; flush(); // Extract zip file & verify it worked. if (true !== $result) { $this->status('error', 'Failed unzipping archive.'); $this->alert('Failed unzipping archive.'); $failed = true; } else { // Reported success; verify extraction. if (!file_exists($destination_directory . 'wp-config.php')) { $this->status('error', 'Error #9004: Key files missing.', 'The unzip process reported success but the backup data file, backupbuddy_dat.php was not found in the extracted files. The unzip process either failed (most likely) or the zip file is not a proper BackupBuddy backup.'); $this->alert('Error: Key files missing.', 'The unzip process reported success but the backup data file, backupbuddy_dat.php was not found in the extracted files. The unzip process either failed (most likely) or the zip file is not a proper BackupBuddy backup.', '9004'); return false; }
function ajax_site_export() { $this->_parent->set_greedy_script_limits(); global $wpdb, $current_site, $current_blog; $blog_id = absint($current_blog->blog_id); check_ajax_referer('export-site'); $return_args = array('completion' => 0, 'message' => '', 'errors' => false); require_once $this->_parent->_pluginPath . '/lib/zipbuddy/zipbuddy.php'; $step = absint($_POST['step']); $zip_id = sanitize_text_field($_POST['zip_id']); $upload_dir = wp_upload_dir(); $original_upload_base_dir = $upload_dir['basedir']; $extract_files_to = $original_upload_base_dir . '/' . $zip_id; switch ($step) { case 1: //Step 1 - Download a copy of WordPress $wp_url = 'http://wordpress.org/latest.zip'; $wp_file = download_url($wp_url); if (is_wp_error($wp_file)) { $return_args['errors'] = true; $return_args['message'] = $wp_file->get_error_message(); $this->log('MS-MS Step 1 - ' . $wp_file->get_error_message(), 'error'); } else { $return_args['message'] = __('WordPress Successfully Downloaded - Extracting WordPress to a Temporary Directory', 'it-l10n-backupbuddy'); $return_args['completion'] = 5; $return_args['wp_file'] = $wp_file; } break; case 2: //Step 2 - Extract WP into a separate directory $wp_file = file_exists($_POST['wp_file']) ? $_POST['wp_file'] : false; if (!$wp_file) { $return_args['errors'] = true; $return_args['message'] = __('WordPress file could not be located', 'it-l10n-backupbuddy'); $this->log('MS-MS Step 2 - ' . __('WordPress file could not be located', 'it-l10n-backupbuddy'), 'error'); } else { $return_args['debug'] = $extract_files_to; $return_args['message'] = __('WordPress extracted - Creating installation file', 'it-l10n-backupbuddy'); $return_args['completion'] = 15; $zipbuddy = new pluginbuddy_zipbuddy($extract_files_to); ob_start(); //todo - upload to wp-contennt //when merging, update extract_files to use new importbuddy version of zip / unzip functionality $zipbuddy->unzip($wp_file, $extract_files_to); $return_args['debug'] = ob_get_clean(); unlink($wp_file); } break; case 3: //Step 3 - Create new WP-Config File $wp_config_path = $extract_files_to . '/wordpress/'; if (!file_exists($wp_config_path)) { $return_args['errors'] = true; $return_args['message'] = __('Temporary WordPress installation not found', 'it-l10n-backupbuddy'); $return_args['debug'] = $wp_config_path; $this->log('MS-MS Step 3 - ' . __('Temporary WordPress installation file could not be located', 'it-l10n-backupbuddy') . $wp_config_path, 'error'); } else { $to_file = "<?php\n"; $to_file .= sprintf("define( 'DB_NAME', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_USER', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_PASSWORD', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_HOST', '%s' );\n", ''); $charset = defined('DB_CHARSET') ? DB_CHARSET : ''; $collate = defined('DB_COLLATE') ? DB_COLLATE : ''; $to_file .= sprintf("define( 'DB_CHARSET', '%s' );\n", $charset); $to_file .= sprintf("define( 'DB_COLLATE', '%s' );\n", $collate); //Attempt to remotely retrieve salts $salts = wp_remote_get('https://api.wordpress.org/secret-key/1.1/salt/'); if (!is_wp_error($salts)) { $to_file .= wp_remote_retrieve_body($salts) . "\n"; } $to_file .= sprintf("define( 'WPLANG', '%s' );\n", WPLANG); $to_file .= sprintf('$table_prefix = \'%s\';' . "\n", 'bbms' . $blog_id . '_'); $to_file .= "if ( !defined('ABSPATH') ) { \n\tdefine('ABSPATH', dirname(__FILE__) . '/'); }"; $to_file .= "/** Sets up WordPress vars and included files. */\n\n\t\t\t\t\t\trequire_once(ABSPATH . 'wp-settings.php');"; $to_file .= "\n?>"; $wp_config_path .= 'wp-config.php'; //Create the file, save, and close $file_handle = fopen($wp_config_path, 'w'); fwrite($file_handle, $to_file); fclose($file_handle); //Prepare the response $return_args['debug'] = $extract_files_to; $return_args['message'] = __('Installation file created - Copying Over Plugins.', 'it-l10n-backupbuddy'); $return_args['completion'] = 25; } break; case 4: //Step 4 - Copy over plugins //Move over plugins $plugin_items = get_transient($zip_id); //Populate $items_to_copy for all plugins to copy over if (is_array($plugin_items)) { $items_to_copy = array(); //Get content directories by using this plugin as a base $content_dir = $dropin_plugins_dir = dirname(dirname(dirname(rtrim(plugin_dir_path(__FILE__), '/')))); $mu_plugins_dir = $content_dir . '/mu-plugins'; $plugins_dir = $content_dir . '/plugins'; //Get the special plugins (mu, dropins, network activated) foreach ($plugin_items as $type => $plugins) { foreach ($plugins as $plugin) { if ($type == 'mu') { $items_to_copy[$plugin] = $mu_plugins_dir . '/' . $plugin; } elseif ($type == 'dropin') { $items_to_copy[$plugin] = $dropin_plugins_dir . '/' . $plugin; } elseif ($type == 'network' || $type == 'site') { //Determine if we're a folder-based plugin, or a file-based plugin (such as hello.php) $plugin_path = dirname($plugins_dir . '/' . $plugin); if (basename($plugin_path) == 'plugins') { $plugin_path = $plugins_dir . '/' . $plugin; } $items_to_copy[basename($plugin_path)] = $plugin_path; } } //end foreach $plugins } //end foreach special plugins //Copy the files over $wp_dir = ''; if (count($items_to_copy) > 0) { $wp_dir = $extract_files_to . '/wordpress/'; $wp_plugin_dir = $wp_dir . '/wp-content/plugins/'; foreach ($items_to_copy as $file => $original_destination) { if (file_exists($original_destination) && file_exists($wp_plugin_dir)) { $this->copy($original_destination, $wp_plugin_dir . $file); } } } //Prepare the response $return_args['debug'] = $wp_dir; $return_args['message'] = __('Plugins copied over. Now copying over the active theme.', 'it-l10n-backupbuddy'); $return_args['completion'] = 50; } else { //Nothing has technically failed at this point - There just aren't any plugins to copy over $return_args['message'] = __('Plugins copied over. Now copying over the active theme.', 'it-l10n-backupbuddy'); $return_args['completion'] = 50; $this->log('MS-MS Step 4 - ' . __('No plugins to copy over', 'it-l10n-backupbuddy'), 'error'); } break; case 5: //Step 5 - Copy over themes $current_theme = current_theme_info(); $template_dir = $current_theme->template_dir; $stylesheet_dir = $current_theme->stylesheet_dir; //If $template_dir and $stylesheet_dir don't match, that means we have a child theme and need to copy over the parent also $items_to_copy = array(); $items_to_copy[basename($template_dir)] = $template_dir; if ($template_dir != $stylesheet_dir) { $items_to_copy[basename($stylesheet_dir)] = $stylesheet_dir; } //Copy the files over if (count($items_to_copy) > 0) { $wp_dir = $extract_files_to . '/wordpress/'; $wp_theme_dir = $wp_dir . '/wp-content/themes/'; foreach ($items_to_copy as $file => $original_destination) { if (file_exists($original_destination) && file_exists($wp_theme_dir)) { $this->copy($original_destination, $wp_theme_dir . $file); } } } $return_args['debug'] = $wp_dir; $return_args['message'] = __('Theme has been copied over. Now copying over media files.', 'it-l10n-backupbuddy'); $return_args['completion'] = 60; break; case 6: //Step 6 - Copy over media/upload files $upload_dir = wp_upload_dir(); $original_upload_base_dir = $upload_dir['basedir']; $destination_upload_base_dir = $extract_files_to . '/wordpress/wp-content/uploads'; $this->copy($original_upload_base_dir, $destination_upload_base_dir, array('ignore_files' => array($zip_id))); $return_args['debug'] = $destination_upload_base_dir; $return_args['message'] = __('Media has been copied over. Now preparing the export.', 'it-l10n-backupbuddy'); $return_args['completion'] = 70; break; case 7: //Step 7 - Create Users Table //Get users of current site global $wpdb, $current_blog; $user_args = array('blog_id' => $current_blog->blog_id); $users = get_users($user_args); //Copy over the user and usermeta tables $found_tables = array($wpdb->users, $wpdb->usermeta); $user_tablename = $usermeta_tablename = ''; $sql_to_execute = array(); if ($found_tables) { foreach ($found_tables as $index => $tablename) { $new_table = ''; if (strstr($tablename, 'users')) { $new_table = $user_tablename = $wpdb->prefix . 'users'; } if (strstr($tablename, 'usermeta')) { $new_table = $usermeta_tablename = $wpdb->prefix . 'usermeta'; } $sql_to_execute[] = sprintf('CREATE TABLE %1$s LIKE %2$s', $new_table, $tablename); $sql_to_execute[] = sprintf('INSERT %1$s SELECT * FROM %2$s', $new_table, $tablename); //Couldn't use $wpdb->prepare here because sql doesn't like quotes around the tablenames } } //Tables have been created, now execute a query to remove the users and user data that doesn't matter $users_to_capture = array(); if ($users) { foreach ($users as $user) { array_push($users_to_capture, $user->ID); } } $users_to_capture = implode(',', $users_to_capture); $sql_to_execute[] = sprintf("DELETE from %s WHERE ID NOT IN( %s )", $user_tablename, $users_to_capture); $sql_to_execute[] = sprintf("DELETE from %s WHERE user_id NOT IN( %s )", $usermeta_tablename, $users_to_capture); //Execute queries foreach ($sql_to_execute as $sql) { $wpdb->query($sql); } //Return the response $return_args['message'] = __('Building the export file and cleaning up.', 'it-l10n-backupbuddy'); $return_args['completion'] = 80; break; case 8: //Step 8 - Backup global $current_site, $wpdb; require_once $this->_parent->_pluginPath . '/classes/backup.php'; $backup_directory = $extract_files_to . '/wordpress/'; $temp_directory = $backup_directory . '/wp-content/uploads/backupbuddy_temp/'; $prefix = $wpdb->prefix; $pluginbuddy_backup = new pluginbuddy_backupbuddy_backup($this->_parent); //Get a list of tables to backup $query = "SHOW TABLES LIKE '{$prefix}%'"; $results = $wpdb->get_results($query, ARRAY_A); $tables_to_ignore = array($prefix . 'blogs', $prefix . 'blog_versions', $prefix . 'site', $prefix . 'sitemeta', $prefix . 'registration_log', $prefix . 'signups', $prefix . 'sitecategories'); $list_of_tables = array(); foreach ($results as $results_key => $table_array) { foreach ($table_array as $key => $tablename) { if (preg_match("/^{$prefix}(?!\\d+)/", $tablename) && !in_array($tablename, $tables_to_ignore)) { array_push($list_of_tables, $tablename); } } } //Do the database dump $backup_serial = $pluginbuddy_backup->rand_string(10); $dat_directory = $temp_directory . '/' . $backup_serial . '/'; $backup = array('backup_time' => time(), 'serial' => $backup_serial, 'backup_type' => 'full', 'temp_directory' => $dat_directory, 'backup_mode' => '2'); $options = array('include_tables' => array(), 'temp_directory' => $dat_directory, 'high_security' => '1', 'backup_nonwp_tables' => '0'); //Create the temp directory $this->_parent->mkdir_recursive($dat_directory); $pluginbuddy_backup->anti_directory_browsing($temp_directory); //Create the dat file $pluginbuddy_backup->backup_create_dat_file($backup, $options, true); //Create the database dump $pluginbuddy_backup->backup_create_database_dump($backup, $options, $list_of_tables, true); //Archive the file require_once $this->_parent->_pluginPath . '/lib/zipbuddy/zipbuddy.php'; $zipbuddy = new pluginbuddy_zipbuddy($backup_directory); $archive_directory = $this->_parent->_options['backup_directory']; $this->_parent->mkdir_recursive($archive_directory); $pluginbuddy_backup->anti_directory_browsing($archive_directory); $archive_file = $archive_directory . 'backup-' . $this->_parent->backup_prefix() . '-' . str_replace('-', '_', date('Y-m-d')) . '-' . $backup_serial . '.zip'; $zipbuddy->add_directory_to_zip($archive_file, $backup_directory, true); //Return the response $archive_file_url = str_replace(ABSPATH, '', $archive_file); $archive_file_url = site_url($archive_file_url); $file_args = sprintf('<a href="%s">%s</a>', $archive_file_url, __('Download Now', 'it-l10n-backupbuddy')); $return_args['message'] = sprintf(__('You\'re done! The export file is ready for download. %s', 'it-l10n-backupbuddy'), $file_args); $return_args['completion'] = 100; //Cleanup $this->_parent->delete_directory_recursive($extract_files_to); break; } //end switch die(json_encode($return_args)); }