public static function export()
 {
     // Set default handlers
     set_error_handler(array('Ai1wm_Error', 'error_handler'));
     set_exception_handler(array('Ai1wm_Error', 'exception_handler'));
     // Get options
     if (isset($_POST['options']) && ($options = $_POST['options'])) {
         $storage = new StorageArea();
         // Export archive
         $model = new Ai1wm_Export();
         $file = $model->export($storage, $options);
         // Send the file to the user
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header(sprintf('Content-Disposition: attachment; filename=%s-%s.%s', Ai1wm_Export::EXPORT_ARCHIVE_NAME, time(), 'zip'));
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
         header('Pragma: public');
         header('Content-Length: ' . filesize($file->getAs('string')));
         // Clear output buffering and read file content
         if (ob_get_length() > 0) {
             @ob_end_clean();
         }
         readfile($file->getAs('string'));
         $storage->flush();
         exit;
     }
 }
 public static function export()
 {
     // Set default handlers
     set_error_handler(array('Ai1wm_Error', 'error_handler'));
     set_exception_handler(array('Ai1wm_Error', 'exception_handler'));
     // Get options
     if (isset($_POST['options']) && ($options = $_POST['options'])) {
         // Log options
         Ai1wm_Logger::debug(AI1WM_EXPORT_OPTIONS, $options);
         // Export site
         $model = new Ai1wm_Export($options);
         $file = $model->export();
         // Send the file to the user
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename=' . self::filename());
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
         header('Pragma: public');
         header('Content-Length: ' . $file->getSize());
         // Clear output buffering and read file content
         while (@ob_end_clean()) {
         }
         // Load file content
         $handle = fopen($file->getName(), 'rb');
         while (!feof($handle)) {
             echo fread($handle, 8192);
         }
         fclose($handle);
         // Flush storage
         StorageArea::getInstance()->flush();
         exit;
     }
 }
Example #3
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);
    }