/** * Export database in SQL format * * @param StorageArea $storage Storage instance * @param array $options Export settings * @return StorageFile StorageFile instance */ public function prepare_database(StorageArea $storage, array $options = array()) { global $wpdb; $file = new Ai1wm_File(); $database_file = $storage->makeFile(); // Set include tables $includeTables = array(); if (isset($options['include-tables'])) { $includeTables = $options['include-tables']; } // Set exclude tables $excludeTables = array(); if (isset($options['exclude-tables'])) { $excludeTables = $options['exclude-tables']; } // Set no table data $noTableData = false; if (isset($options['no-table-data'])) { $noTableData = true; } $clauses = array(); // Spam comments if (isset($options['export-spam-comments'])) { $clauses[$wpdb->comments] = ' WHERE comment_approved != "spam" ORDER BY comment_ID '; $clauses[$wpdb->commentmeta] = sprintf(' INNER JOIN `%1$s` ON `%1$s`.comment_ID = `%2$s`.comment_id AND `%1$s`.comment_approved != \'spam\' ORDER BY `%2$s`.meta_id ', $wpdb->comments, $wpdb->commentmeta); } // Post revisions if (isset($options['export-revisions'])) { $clauses[$wpdb->posts] = ' WHERE post_type != "revision" ORDER BY ID '; } // No table data, but leave Administrator account unchanged if ($noTableData) { $clauses = array(); $clauses[$wpdb->options] = ' ORDER BY option_id ASC '; $clauses[$wpdb->users] = ' WHERE id = 1 '; $clauses[$wpdb->usermeta] = ' WHERE user_id = 1 '; } // Make connection try { $db = MysqlDumpFactory::makeMysqlDump(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, class_exists('PDO') && in_array('mysql', PDO::getAvailableDrivers())); $db->getConnection(); } catch (Exception $e) { // Use "old" mysql adapter $db = MysqlDumpFactory::makeMysqlDump(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, false); } // Set dump options $db->setFileName($database_file->getAs('string'))->setIncludeTables($includeTables)->setExcludeTables($excludeTables)->setNoTableData($noTableData)->setOldTablePrefix($wpdb->prefix)->setNewTablePrefix(AI1WM_TABLE_PREFIX)->setQueryClauses($clauses); // Export database into a file $db->export(); // Replace Old/New Values if (isset($options['replace']) && ($replace = $options['replace'])) { $old_values = array(); $new_values = array(); for ($i = 0; $i < count($replace['old-value']); $i++) { if (!empty($replace['old-value'][$i]) && !empty($replace['new-value'][$i]) && $replace['old-value'][$i] != $replace['new-value'][$i]) { $old_values[] = $replace['old-value'][$i]; $new_values[] = $replace['new-value'][$i]; } } // Do String Replacement if ($old_values && $new_values) { $database_file = $file->str_replace_file($storage, $database_file, $old_values, $new_values); // Do find and replace $database_file = $file->preg_replace_file($storage, $database_file, '/s:(\\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");/'); } } return $database_file; }
/** * Import archive file (database, media, package.json) * * @param array $input_file Upload file parameters * @param array $options Additional upload settings * @return array List of messages */ public function import($input_file, $options = array()) { global $wpdb; $errors = array(); if (empty($input_file['error'])) { try { $storage = new StorageArea(); // Flush storage directory if ($options['chunk'] === 0) { StorageDirectory::flush(AI1WM_STORAGE_PATH, array('.gitignore')); } // Partial file path $upload_file = $storage->makeFile($options['name'])->getAs('string'); // Open partial file $out = fopen($upload_file, $options['chunk'] == 0 ? 'wb' : 'ab'); if ($out) { // Read binary input stream and append it to temp file $in = fopen($input_file['tmp_name'], 'rb'); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } fclose($in); fclose($out); // Remove temporary uploaded file unlink($input_file['tmp_name']); } else { $errors[] = sprintf(_('Site could not be imported!<br /> Please make sure that storage directory <strong>%s</strong> has read and write permissions.'), AI1WM_STORAGE_PATH); // Clear storage $storage->flush(); } } catch (Exception $e) { $errors[] = sprintf(_('Site could not be imported!<br /> Please make sure that storage directory <strong>%s</strong> has read and write permissions.'), AI1WM_STORAGE_PATH); // Clear storage $storage->flush(); } // Check if file has been uploaded if (empty($errors) && (!$options['chunks'] || $options['chunk'] == $options['chunks'] - 1)) { // Create temporary directory $extract_to = $storage->makeDirectory()->getAs('string'); // Extract archive to a temporary directory try { try { $archive = ZipFactory::makeZipArchiver($upload_file, !class_exists('ZipArchive')); $archive->extractTo($extract_to); $archive->close(); } catch (Exception $e) { $archive = ZipFactory::makeZipArchiver($upload_file, true); $archive->extractTo($extract_to); $archive->close(); } } catch (Exception $e) { $errors[] = _('Archive file is broken or is not compatible with "All In One WP Migration" plugin! Please verify your archive file.'); } if (empty($errors)) { // Verify whether this archive is valid if ($this->is_valid($extract_to)) { // Enable maintenance mode $this->maintenance_mode(true); // Parse package config file $config = $this->parse_package($extract_to . Ai1wm_Export::EXPORT_PACKAGE_NAME); // Database import if (is_file($extract_to . Ai1wm_Export::EXPORT_DATABASE_NAME)) { // Backup database $model = new Ai1wm_Export(); $database_file = $model->prepare_database($storage); try { $db = MysqlDumpFactory::makeMysqlDump(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, class_exists('PDO') && in_array('mysql', PDO::getAvailableDrivers())); $db->getConnection(); } catch (Exception $e) { // Use "old" mysql adapter $db = MysqlDumpFactory::makeMysqlDump(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, false); } // Flush database $db->flush(); $old_values = array(); $new_values = array(); // Get Site URL if (isset($config['SiteURL']) && $config['SiteURL'] != site_url()) { $old_values[] = $config['SiteURL']; $new_values[] = site_url(); } // Get Home URL if (isset($config['HomeURL']) && $config['HomeURL'] != home_url()) { $old_values[] = $config['HomeURL']; $new_values[] = home_url(); } // Get Domain if (isset($config['Domain']) && $config['Domain'] != ($domain = parse_url(home_url(), PHP_URL_HOST))) { $old_values[] = $config['Domain']; $new_values[] = $domain; } $file = new Ai1wm_File(); $database_file = $storage->makeFile(Ai1wm_Export::EXPORT_DATABASE_NAME, $extract_to); // Replace Old/New Values if ($old_values && $new_values) { $database_file = $file->str_replace_file($storage, $database_file, $old_values, $new_values); $database_file = $file->preg_replace_file($storage, $database_file, '/s:(\\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");/'); } // Import database $db->setOldTablePrefix(AI1WM_TABLE_PREFIX)->setNewTablePrefix($wpdb->prefix)->import($database_file->getAs('string')); } // Media import if (is_dir($extract_to . Ai1wm_Export::EXPORT_MEDIA_NAME)) { // Media base directory $upload_dir = wp_upload_dir(); $upload_basedir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR; if (!is_dir($upload_basedir)) { mkdir($upload_basedir); } // Backup media files $backup_media_to = $storage->makeDirectory()->getAs('string'); StorageDirectory::copy($upload_basedir, $backup_media_to); // Flush media files StorageDirectory::flush($upload_basedir); // Import media files StorageDirectory::copy($extract_to . Ai1wm_Export::EXPORT_MEDIA_NAME, $upload_basedir); } // Themes import if (is_dir($extract_to . Ai1wm_Export::EXPORT_THEMES_NAME)) { // Themes base directory $themes_dir = get_theme_root(); $themes_basedir = $themes_dir . DIRECTORY_SEPARATOR; if (!is_dir($themes_basedir)) { mkdir($themes_basedir); } // Backup themes files $backup_themes_to = $storage->makeDirectory()->getAs('string'); StorageDirectory::copy($themes_basedir, $backup_themes_to); // Flush themes files StorageDirectory::flush($themes_basedir); // Import themes files StorageDirectory::copy($extract_to . Ai1wm_Export::EXPORT_THEMES_NAME, $themes_basedir); } // Plugins import if (is_dir($extract_to . Ai1wm_Export::EXPORT_PLUGINS_NAME)) { // Backup plugin files $backup_plugins_to = $storage->makeDirectory()->getAs('string'); StorageDirectory::copy(WP_PLUGIN_DIR, $backup_plugins_to, array(AI1WM_PLUGIN_NAME)); // Flush plugin files StorageDirectory::flush(WP_PLUGIN_DIR, array(AI1WM_PLUGIN_NAME)); // Import plugin files StorageDirectory::copy($extract_to . Ai1wm_Export::EXPORT_PLUGINS_NAME, WP_PLUGIN_DIR); } // Disable maintenance mode $this->maintenance_mode(false); } else { $errors[] = _('File is not compatible with "All In One WP Migration" plugin! Please verify your archive file.'); } } // Clear storage $storage->flush(); } } else { $errors[] = $this->code_to_message($input_file['error']); } return array('errors' => $errors); }