public static function index()
 {
     try {
         $storage = new StorageArea();
         $is_accessible = $storage->makeFile();
         $storage->flush();
     } catch (Exception $e) {
         $is_accessible = false;
     }
     Ai1wm_Template::render('import/index', array('is_accessible' => $is_accessible));
 }
 public static function index()
 {
     try {
         $storage = new StorageArea();
         $is_accessible = $storage->makeFile();
         $storage->flush();
     } catch (Exception $e) {
         $is_accessible = false;
     }
     // Message
     $message = new Ai1wm_Message();
     $is_closed = $message->is_closed(Ai1wm_Message::MESSAGE_INFO);
     Ai1wm_Template::render('export/index', array('list_plugins' => get_plugins(), 'is_accessible' => $is_accessible, 'is_closed' => $is_closed));
 }
 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;
     }
 }
示例#4
0
 public static function getInstance()
 {
     if (self::$instance === null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
示例#5
0
 /**
  * Replace a file, line by line with the regex pattern and then writes the
  * output to a new file.
  *
  * @param  StorageArea $storage Storage instance
  * @param  StorageFile $file    StorageFile instance
  * @param  string      $pattern Find and replace pattern
  * @return StorageFile          StorageFile instance
  */
 public function preg_replace_file(StorageArea $storage, StorageFile $file, $pattern)
 {
     $new_file = $storage->makeFile();
     $current_file = $file->getAs('resource');
     // Set file handle to the beginning of the file
     rewind($current_file);
     while (!feof($current_file)) {
         $line = stream_get_line($current_file, 1000000, "\n");
         // Append new line at the end of the line
         if (strlen($line) < 1000000 && !feof($current_file)) {
             $line .= "\n";
         }
         $replaced = $this->_preg_replace($line, $pattern);
         if (false === fwrite($new_file->getAs('resource'), $replaced)) {
             wp_die('Writting to a file failed! Probably, there is no more free space left?', 'Out of disk space');
         }
     }
     return $new_file;
 }
 /**
  * Export site
  *
  * @return StorageFile
  */
 public function export()
 {
     $storage = StorageArea::getInstance();
     // Enable maintenance mode
     Ai1wm_Maintenance::enable();
     // Create export file
     $export_file = $storage->makeFile();
     // Make archive file
     try {
         $zip = ZipFactory::makeZipArchiver($export_file->getName(), !class_exists('ZipArchive'), true);
     } catch (Exception $e) {
         $zip = ZipFactory::makeZipArchiver($export_file->getName(), true, true);
     }
     // Package
     if ($this->should_export_package()) {
         $service = new Ai1wm_Service_Package($this->options);
         $zip->addFromString(AI1WM_PACKAGE_NAME, $service->export());
     }
     // Database
     if ($this->should_export_database()) {
         $service = new Ai1wm_Service_Database($this->options);
         // Add database to archive
         $zip->addFile($service->export(), AI1WM_DATABASE_NAME);
     }
     // Media
     if ($this->should_export_media()) {
         $service = new Ai1wm_Service_Media($this->options);
         // Add media to archive
         $zip->addDir($service->export(), AI1WM_MEDIA_NAME);
         // Sites (Network mode)
         $service = new Ai1wm_Service_Sites($this->options);
         if ($sites = $service->export()) {
             // Add sites to archive
             $zip->addDir($sites, AI1WM_SITES_NAME);
         }
     }
     // Themes
     if ($this->should_export_themes()) {
         $service = new Ai1wm_Service_Themes($this->options);
         // Add themes to archive
         $zip->addDir($service->export(), AI1WM_THEMES_NAME);
     }
     // Plugins
     if ($this->should_export_plugins()) {
         $service = new Ai1wm_Service_Plugins($this->options);
         // Add plugins to archive
         if ($plugins = $service->get_installed_plugins()) {
             $zip->addDir($service->export(), AI1WM_PLUGINS_NAME, $plugins);
         }
     }
     // Disable maintenance mode
     Ai1wm_Maintenance::disable();
     return $export_file;
 }
 /**
  * Import themes
  *
  * @return void
  */
 public function import()
 {
     $storage = StorageArea::getInstance();
     // Themes directory
     $themes_dir = get_theme_root();
     if (!is_dir($themes_dir)) {
         mkdir($themes_dir);
     }
     // Backup themes files
     $backup_themes_to = $storage->makeDirectory();
     StorageUtility::copy($themes_dir, $backup_themes_to->getName());
     // Flush themes files
     StorageUtility::flush($themes_dir);
     // Import themes files
     StorageUtility::copy($storage->getRootPath() . AI1WM_THEMES_NAME, $themes_dir);
 }
 /**
  * Import plugins
  *
  * @return void
  */
 public function import()
 {
     $storage = StorageArea::getInstance();
     // Plugins directory
     $plugins_dir = WP_PLUGIN_DIR;
     if (!is_dir($plugins_dir)) {
         mkdir($plugins_dir);
     }
     // Backup plugin files
     $backup_plugins_to = $storage->makeDirectory();
     StorageUtility::copy($plugins_dir, $backup_plugins_to->getName(), array(AI1WM_PLUGIN_NAME));
     // Flush plugin files
     StorageUtility::flush($plugins_dir, array(AI1WM_PLUGIN_NAME));
     // Import plugin files
     StorageUtility::copy($storage->getRootPath() . AI1WM_PLUGINS_NAME, $plugins_dir);
 }
 /**
  * Import package configuration
  *
  * @return array
  */
 public function import()
 {
     global $wp_version;
     // Get config file
     $data = file_get_contents(StorageArea::getInstance()->getRootPath() . AI1WM_PACKAGE_NAME);
     // Parse config file
     $config = json_decode($data, true);
     // Add plugin version
     if (!isset($config['Plugin']['Version'])) {
         $config['Plugin']['Version'] = AI1WM_VERSION;
     }
     // Add wordpress version
     if (!isset($config['WordPress']['Version'])) {
         $config['WordPress']['Version'] = $wp_version;
     }
     return $config;
 }
 /**
  * Import media
  *
  * @return void
  */
 public function import()
 {
     $storage = StorageArea::getInstance();
     // Media directory
     $upload_dir = wp_upload_dir();
     $upload_basedir = $upload_dir['basedir'];
     if (!is_dir($upload_basedir)) {
         mkdir($upload_basedir);
     }
     // Backup media files
     $backup_media_to = $storage->makeDirectory();
     StorageUtility::copy($upload_basedir, $backup_media_to->getName());
     // Flush media files
     StorageUtility::flush($upload_basedir);
     // Import media files
     StorageUtility::copy($storage->getRootPath() . AI1WM_MEDIA_NAME, $upload_basedir);
 }
 /**
  * Import sites (Network mode)
  *
  * @return void
  */
 public function import()
 {
     global $wp_version;
     $storage = StorageArea::getInstance();
     if (version_compare($wp_version, '3.5', '<')) {
         // Blogs.dir directory
         $blogs_dir = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . AI1WM_BLOGS_NAME;
         if (!is_dir($blogs_dir)) {
             mkdir($blogs_dir);
         }
         // Backup blogs.dir files
         $backup_blogs_to = $storage->makeDirectory();
         StorageUtility::copy($blogs_dir, $backup_blogs_to->getName());
         // Flush blogs.dir files
         StorageUtility::flush($blogs_dir);
         // Import blogs.dir files
         StorageUtility::copy($storage->getRootPath() . AI1WM_SITES_NAME, $blogs_dir);
     } else {
         // Media directory
         $upload_dir = wp_upload_dir();
         $upload_basedir = $upload_dir['basedir'];
         if (!is_dir($upload_basedir)) {
             mkdir($upload_basedir);
         }
         // Sites directory
         $sites_dir = $upload_basedir . DIRECTORY_SEPARATOR . AI1WM_SITES_NAME;
         if (!is_dir($sites_dir)) {
             mkdir($sites_dir);
         }
         // Backup sites files
         $backup_sites_to = $storage->makeDirectory();
         StorageUtility::copy($sites_dir, $backup_sites_to->getName());
         // Flush sites files
         StorageUtility::flush($sites_dir);
         // Import sites files
         StorageUtility::copy($storage->getRootPath() . AI1WM_SITES_NAME, $sites_dir);
     }
 }
示例#12
0
    /**
     * 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;
    }
示例#13
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);
    }
 /**
  * Should import plugins?
  *
  * @return boolean
  */
 public function should_import_plugins()
 {
     return is_dir(StorageArea::getInstance()->getRootPath() . AI1WM_PLUGINS_NAME);
 }
 /**
  * Export database
  *
  * @return string
  */
 public function export()
 {
     global $wpdb;
     $database_file = StorageArea::getInstance()->makeFile();
     // Set include tables
     $include_tables = array();
     if (isset($this->options['include-tables'])) {
         $include_tables = $this->options['include-tables'];
     }
     // Set exclude tables
     $exclude_tables = array();
     if (isset($this->options['exclude-tables'])) {
         $exclude_tables = $this->options['exclude-tables'];
     }
     $clauses = array();
     // Spam comments
     if (isset($this->options['export-spam-comments'])) {
         $clauses[$wpdb->comments] = " WHERE comment_approved != 'spam' ";
         $clauses[$wpdb->commentmeta] = sprintf(" WHERE comment_id IN ( SELECT comment_ID FROM `%s` WHERE comment_approved != 'spam' ) ", $wpdb->comments);
     }
     // Post revisions
     if (isset($this->options['export-revisions'])) {
         $clauses[$wpdb->posts] = " WHERE post_type != 'revision' ";
     }
     // No table data, but leave Admin account
     $no_table_data = isset($this->options['no-table-data']);
     if ($no_table_data) {
         $clauses = array();
         $clauses[$wpdb->users] = ' WHERE id = 1 ';
         $clauses[$wpdb->usermeta] = ' WHERE user_id = 1 ';
     }
     // Find and replace
     $old_values = array();
     $new_values = array();
     if (isset($this->options['replace']) && ($replace = $this->options['replace'])) {
         for ($i = 0; $i < count($replace['old-value']); $i++) {
             if (isset($replace['old-value'][$i]) && isset($replace['new-value'][$i])) {
                 $old_values[] = $replace['old-value'][$i];
                 $new_values[] = $replace['new-value'][$i];
             }
         }
     }
     // Set dump options
     $this->connection->setFileName($database_file->getName())->setIncludeTables($include_tables)->setExcludeTables($exclude_tables)->setNoTableData($no_table_data)->setOldTablePrefix($wpdb->prefix)->setNewTablePrefix(AI1WM_TABLE_PREFIX)->setOldReplaceValues($old_values)->setNewReplaceValues($new_values)->setQueryClauses($clauses);
     // Export database
     $this->connection->export();
     return $database_file->getName();
 }
 public static function upload($options)
 {
     $storage = StorageArea::getInstance();
     // Partial upload file
     $partial_file = $storage->makeFile($options['import']['file']);
     // Upload file
     if (isset($_FILES['upload-file'])) {
         // Has any upload error?
         if (empty($_FILES['upload-file']['error'])) {
             // Flush storage
             if ($options['chunk'] === 0) {
                 $storage->flush();
             }
             // Open partial file
             $out = fopen($partial_file->getName(), $options['chunk'] == 0 ? 'wb' : 'ab');
             if ($out) {
                 // Read binary input stream and append it to temp file
                 $in = fopen($_FILES['upload-file']['tmp_name'], 'rb');
                 if ($in) {
                     while ($buff = fread($in, 4096)) {
                         fwrite($out, $buff);
                     }
                 }
                 fclose($in);
                 fclose($out);
                 // Remove temporary uploaded file
                 unlink($_FILES['upload-file']['tmp_name']);
             } else {
                 throw new Ai1wm_Import_Exception(sprintf(_('Site could not be imported!<br />' . 'Please make sure that storage directory <strong>%s</strong> has read and write permissions.'), AI1WM_STORAGE_PATH));
                 // Flush storage
                 $storage->flush();
             }
         } else {
             throw new Ai1wm_Import_Exception(sprintf(_('Site could not be imported!<br />' . 'Please contact ServMask Support and report the following error code: %d'), $_FILES['upload-file']['error']));
         }
     }
     // Upload completed?
     if (!$options['chunks'] || $options['chunk'] == $options['chunks'] - 1) {
         return $partial_file;
     }
     exit;
 }