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;
     }
 }
 /**
  * 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);
     }
 }
 /**
  * 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;
 }