예제 #1
0
 /**
  * Export archive file (database, media, package.json)
  *
  * @param  StorageArea $storage Storage instance
  * @param  array       $options Export settings
  * @return StorageFile          StorageFile instance
  */
 public function export(StorageArea $storage, array $options = array())
 {
     global $wp_version;
     $options['plugin_version'] = AI1WM_VERSION;
     $options['wp_version'] = $wp_version;
     $options['php_version'] = phpversion();
     $options['php_uname'] = php_uname();
     $options['max_execution_time'] = ini_get('max_execution_time');
     $options['memory_limit'] = ini_get('memory_limit');
     $options['memory_get_peak_usage'] = memory_get_peak_usage();
     $options['memory_get_usage'] = memory_get_usage();
     $options['ZipArchive'] = class_exists('ZipArchive') ? 1 : 0;
     $options['ZLIB_installed'] = function_exists('gzopen') ? 1 : 0;
     $options['PDO_available'] = class_exists('PDO') ? 1 : 0;
     $options['home_url'] = home_url();
     // Export last options
     update_option(self::EXPORT_LAST_OPTIONS, $options);
     // Flush storage directory
     StorageDirectory::flush(AI1WM_STORAGE_PATH, array('.gitignore'));
     // Create output file
     $output_file = $storage->makeFile();
     // Make archive
     try {
         $archive = ZipFactory::makeZipArchiver($output_file->getAs('resource'), !class_exists('ZipArchive'), true);
     } catch (Exception $e) {
         $archive = ZipFactory::makeZipArchiver($output_file->getAs('resource'), true, true);
     }
     // Should we export database?
     if (!isset($options['export-database'])) {
         // Prepare database file
         $database_file = $this->prepare_database($storage, $options);
         // Add database to archive
         $archive->addFile($database_file->getAs('resource'), self::EXPORT_DATABASE_NAME);
     }
     // Should we export media?
     if (!isset($options['export-media'])) {
         $archive->addDir($this->prepare_media($options), self::EXPORT_MEDIA_NAME);
     }
     // Should we export themes?
     if (!isset($options['export-themes'])) {
         $archive->addDir($this->prepare_themes($options), self::EXPORT_THEMES_NAME);
     }
     // Should we export plugins?
     if (!isset($options['export-plugins'])) {
         if ($include = $this->get_plugins(array(AI1WM_PLUGIN_NAME))) {
             $archive->addDir($this->prepare_plugins($options), self::EXPORT_PLUGINS_NAME, $include);
         }
     }
     // Add package
     $archive->addFromString(self::EXPORT_PACKAGE_NAME, $this->prepare_package($options));
     return $output_file;
 }
예제 #2
0
    /**
     * 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);
    }