public static function execute( $params ) {

		// Set shutdown handler
		@register_shutdown_function( 'Ai1wm_Import_Done::shutdown' );

		// Check multisite.json file
		if ( true === is_file( ai1wm_multisite_path( $params ) ) ) {

			// Read multisite.json file
			$handle = fopen( ai1wm_multisite_path( $params ), 'r' );
			if ( $handle === false ) {
				throw new Ai1wm_Import_Exception( __( 'Unable to read multisite.json file', AI1WM_PLUGIN_NAME ) );
			}

			// Parse multisite.json file
			$multisite = fread( $handle, filesize( ai1wm_multisite_path( $params ) ) );
			$multisite = json_decode( $multisite );

			// Close handle
			fclose( $handle );

			// Activate plugins
			if ( isset( $multisite->Plugins ) && ( $active_sitewide_plugins = $multisite->Plugins ) ) {
				activate_plugins( $active_sitewide_plugins, null, is_multisite() );
			}
		}

		// Set the new MS files rewriting
		if ( get_site_option( AI1WM_MS_FILES_REWRITING ) ) {
			update_site_option( AI1WM_MS_FILES_REWRITING, 0 );
		}

		// Open the archive file for reading
		$archive = new Ai1wm_Extractor( ai1wm_archive_path( $params ) );

		// Unpack must-use plugins
		$archive->extract_by_files_array( WP_CONTENT_DIR, array( AI1WM_MUPLUGINS_NAME ) );

		// Close the archive file
		$archive->close();

		// Load must-use plugins
		foreach ( wp_get_mu_plugins() as $mu_plugin ) {
			include_once( $mu_plugin );
		}

		return $params;
	}
 public static function execute($params)
 {
     // Set progress
     Ai1wm_Status::info(__('Activating plugins...', AI1WM_PLUGIN_NAME));
     // Open the archive file for reading
     $archive = new Ai1wm_Extractor(ai1wm_archive_path($params));
     // Include WordPress files
     $include_files = array_keys(_get_dropins());
     // Include mu-plugins files
     $include_files = array_merge($include_files, array(AI1WM_MUPLUGINS_NAME));
     // Unpack WordPress files and mu-plugins files
     $archive->extract_by_files_array(WP_CONTENT_DIR, $include_files);
     // Close the archive file
     $archive->close();
     // Set progress
     Ai1wm_Status::info(__('Done activating plugins...', AI1WM_PLUGIN_NAME));
     return $params;
 }
	public static function execute( $params ) {

		// Set progress
		Ai1wm_Status::info( __( 'Retrieving a list of all WordPress files...', AI1WM_PLUGIN_NAME ) );

		// Open the archive file for reading
		$archive = new Ai1wm_Extractor( ai1wm_archive_path( $params ) );

		// Get total files
		$params['total_files'] = $archive->get_total_files();

		// Get total size
		$params['total_size'] = $archive->get_total_size();

		// Close the archive file
		$archive->close();

		// Set progress
		Ai1wm_Status::info( __( 'Done retrieving a list of all WordPress files.', AI1WM_PLUGIN_NAME ) );

		return $params;
	}
 /**
  * Add content files and directories
  *
  * @return void
  */
 public function content()
 {
     // Total and processed files
     $total = Ai1wm_Status::get('total');
     $processed = Ai1wm_Status::get('processed');
     $progress = (int) ($processed / $total * 100) or $progress = 4;
     // Set progress
     Ai1wm_Status::set(array('type' => 'info', 'message' => sprintf(__('Restoring %d files...<br />%d%% complete', AI1WM_PLUGIN_NAME), $total, $progress)));
     // Start time
     $start = microtime(true);
     // Flag to hold if all files have been processed
     $completed = true;
     // Open the archive file for reading
     $archive = new Ai1wm_Extractor($this->storage()->archive());
     // Set the file pointer to the one that we have saved
     $archive->set_file_pointer(null, $this->pointer());
     while ($archive->has_not_reached_eof()) {
         // Extract a file from archive to wp_content_dir
         $archive->extract_one_file_to(WP_CONTENT_DIR, array(AI1WM_PACKAGE_NAME, AI1WM_DATABASE_NAME));
         // Increment processed files counter
         $processed++;
         // We are only extracting files for 5 seconds at a time
         $time = microtime(true) - $start;
         if ($time > 5) {
             // More than 5 seconds have passed, break and do another request
             $completed = false;
             break;
         }
     }
     // Set new file map pointer
     $this->pointer($archive->get_file_pointer());
     // Close the archive file
     $archive->close();
     // Set progress
     Ai1wm_Status::set(array('processed' => $processed));
     // Redirect
     if ($completed) {
         $this->route_to('database');
     } else {
         $this->route_to('content');
     }
 }
	public static function execute( $params ) {

		// Read blogs.json file
		$handle = fopen( ai1wm_blogs_path( $params ), 'r' );
		if ( $handle === false ) {
			throw new Ai1wm_Import_Exception( 'Unable to read blogs.json file' );
		}

		// Parse blogs.json file
		$blogs = fread( $handle, filesize( ai1wm_blogs_path( $params ) ) );
		$blogs = json_decode( $blogs );

		// Close handle
		fclose( $handle );

		// Set content offset
		if ( isset( $params['content_offset'] ) ) {
			$content_offset = (int) $params['content_offset'];
		} else {
			$content_offset = 0;
		}

		// Set archive offset
		if ( isset( $params['archive_offset']) ) {
			$archive_offset = (int) $params['archive_offset'];
		} else {
			$archive_offset = 0;
		}

		// Get total files
		if ( isset( $params['total_files'] ) ) {
			$total_files = (int) $params['total_files'];
		} else {
			$total_files = 1;
		}

		// Get total size
		if ( isset( $params['total_size'] ) ) {
			$total_size = (int) $params['total_size'];
		} else {
			$total_size = 1;
		}

		// Get processed files
		if ( isset( $params['processed'] ) ) {
			$processed = (int) $params['processed'];
		} else {
			$processed = 0;
		}

		// What percent of files have we processed?
		$progress = (int) ( ( $processed / $total_size ) * 100 );

		// Set progress
		if ( empty( $content_offset ) ) {
			Ai1wm_Status::info( sprintf( __( 'Restoring %d files...<br />%d%% complete', AI1WM_PLUGIN_NAME ), $total_files, $progress ) );
		}

		// Start time
		$start = microtime( true );

		// Flag to hold if all files have been processed
		$completed = true;

		// Open the archive file for reading
		$archive = new Ai1wm_Extractor( ai1wm_archive_path( $params ) );

		// Set the file pointer to the one that we have saved
		$archive->set_file_pointer( null, $archive_offset );

		$old_paths = array();
		$new_paths = array();

		// Set extract paths
		foreach ( $blogs as $blog ) {
			$old_paths[] = ai1wm_sites_path( $blog->Old->Id );
			$new_paths[] = ai1wm_sites_path( $blog->New->Id );
		}

		while ( $archive->has_not_reached_eof() ) {
			try {

				// Extract a file from archive to WP_CONTENT_DIR
				if ( ( $content_offset = $archive->extract_one_file_to( WP_CONTENT_DIR, array( AI1WM_PACKAGE_NAME, AI1WM_MULTISITE_NAME, AI1WM_DATABASE_NAME, AI1WM_MUPLUGINS_NAME ), $old_paths, $new_paths, $content_offset, 10 ) ) ) {

					// Set progress
					if ( ( $processed += $content_offset ) ) {
						$progress = (int) ( ( $processed / $total_size ) * 100 );
					}

					// Set progress
					Ai1wm_Status::info( sprintf( __( 'Restoring %d files...<br />%d%% complete', AI1WM_PLUGIN_NAME ), $total_files, $progress ) );

					// Set content offset
					$params['content_offset'] = $content_offset;

					// Set archive offset
					$params['archive_offset'] = $archive_offset;

					// Set completed flag
					$params['completed'] = false;

					// Close the archive file
					$archive->close();

					return $params;
				}

				// Set content offset
				$content_offset = 0;

				// Set archive offset
				$archive_offset = $archive->get_file_pointer();

			} catch ( Exception $e ) {
				// Skip bad file permissions
			}

			// Increment processed files
			$processed += $archive->get_current_filesize();

			// More than 10 seconds have passed, break and do another request
			if ( ( microtime( true ) - $start ) > 10 ) {
				$completed = false;
				break;
			}
		}

		// Set content offset
		$params['content_offset'] = $content_offset;

		// Set archive offset
		$params['archive_offset'] = $archive_offset;

		// Set processed files
		$params['processed'] = $processed;

		// Set completed flag
		$params['completed'] = $completed;

		// Close the archive file
		$archive->close();

		return $params;
	}
	public static function execute( $params ) {

		// Set progress
		Ai1wm_Status::info( __( 'Unpacking archive...', AI1WM_PLUGIN_NAME ) );

		// Open the archive file for reading
		$archive = new Ai1wm_Extractor( ai1wm_archive_path( $params ) );

		// Validate the archive file consistency
		if ( ! $archive->is_valid() ) {
			throw new Ai1wm_Import_Exception(
				__(
					'The archive file is corrupted. Follow this article to resolve the problem: ' .
					'<a href="https://help.servmask.com/knowledgebase/corrupted-archive/" target="_blank">https://help.servmask.com/knowledgebase/corrupted-archive/</a>',
					AI1WM_PLUGIN_NAME
				)
			);
		}

		// Obtain the name of the archive
		$name = ai1wm_archive_name( $params );

		// Obtain the size of the archive
		$size = ai1wm_archive_bytes( $params );

		// Check file size of the archive
		if ( false === $size ) {
			throw new Ai1wm_Not_Accesible_Exception(
				sprintf( __( 'Unable to get the file size of <strong>%s</strong>', AI1WM_PLUGIN_NAME ), $name )
			);
		}

		$allowed_size = apply_filters( 'ai1wm_max_file_size', AI1WM_MAX_FILE_SIZE );

		// Let's check the size of the file to make sure it is less than the maximum allowed
		if ( ( $allowed_size > 0 ) && ( $size > $allowed_size ) ) {
			throw new Ai1wm_Import_Exception(
				sprintf(
					__(
						'The file that you are trying to import is over the maximum upload file size limit of <strong>%s</strong>.<br />' .
						'You can remove this restriction by purchasing our ' .
						'<a href="https://servmask.com/products/unlimited-extension" target="_blank">Unlimited Extension</a>.',
						AI1WM_PLUGIN_NAME
					),
					size_format( $allowed_size )
				)
			);
		}

		// Unpack package.json, multisite.json and database.sql files
		$archive->extract_by_files_array(
			ai1wm_storage_path( $params ),
			array(
				AI1WM_PACKAGE_NAME,
				AI1WM_MULTISITE_NAME,
				AI1WM_DATABASE_NAME,
			)
		);

		// Close the archive file
		$archive->close();

		// Check package.json file
		if ( false === is_file( ai1wm_package_path( $params ) ) ) {
			throw new Ai1wm_Import_Exception(
				__( 'Invalid archive file. It should contain <strong>package.json</strong> file.', AI1WM_PLUGIN_NAME )
			);
		}

		return $params;
	}
 /**
  * Add content files and directories
  *
  * @return void
  */
 public function content()
 {
     // Set content offset
     if (isset($this->args['content_offset'])) {
         $content_offset = $this->args['content_offset'];
     } else {
         $content_offset = 0;
     }
     // Set archive offset
     if (isset($this->args['archive_offset'])) {
         $archive_offset = $this->args['archive_offset'];
     } else {
         $archive_offset = 0;
     }
     // Set total files
     if (isset($this->args['total'])) {
         $total = $this->args['total'];
     } else {
         $total = 1;
     }
     // Set processed files
     if (isset($this->args['processed'])) {
         $processed = $this->args['processed'];
     } else {
         $processed = 0;
     }
     // What percent of files have we processed?
     $progress = (int) ($processed / $total * 100);
     // Set progress
     if (empty($content_offset)) {
         Ai1wm_Status::set(array('type' => 'info', 'message' => sprintf(__('Restoring %d files...<br />%.2f%% complete', AI1WM_PLUGIN_NAME), $total, $progress)));
     }
     // Start time
     $start = microtime(true);
     // Flag to hold if all files have been processed
     $completed = true;
     // Open the archive file for reading
     $archive = new Ai1wm_Extractor($this->storage()->archive());
     // Set the file pointer to the one that we have saved
     $archive->set_file_pointer(null, $archive_offset);
     while ($archive->has_not_reached_eof()) {
         try {
             // Extract a file from archive to WP_CONTENT_DIR
             if ($content_offset = $archive->extract_one_file_to(WP_CONTENT_DIR, array(AI1WM_PACKAGE_NAME, AI1WM_DATABASE_NAME), $content_offset, 3)) {
                 // Set progress
                 if (($sub_progress = $content_offset / $archive->get_current_filesize()) < 1) {
                     $progress += $sub_progress;
                 }
                 // Set progress
                 Ai1wm_Status::set(array('type' => 'info', 'message' => sprintf(__('Restoring %d files...<br />%.2f%% complete', AI1WM_PLUGIN_NAME), $total, $progress)));
                 // Set content offset
                 $this->args['content_offset'] = $content_offset;
                 // Set archive offset
                 $this->args['archive_offset'] = $archive_offset;
                 // Close the archive file
                 $archive->close();
                 // Redirect
                 return $this->route_to('content');
             }
             // Set content offset
             $content_offset = 0;
             // Set archive offset
             $archive_offset = $archive->get_file_pointer();
         } catch (Exception $e) {
             // Skip bad file permissions
         }
         // Increment processed files counter
         $processed++;
         // Time elapsed
         if (microtime(true) - $start > 3) {
             // More than 3 seconds have passed, break and do another request
             $completed = false;
             break;
         }
     }
     // Set content offset
     $this->args['content_offset'] = $content_offset;
     // Set archive offset
     $this->args['archive_offset'] = $archive_offset;
     // Set processed files
     $this->args['processed'] = $processed;
     // Close the archive file
     $archive->close();
     // Redirect
     if ($completed) {
         $this->route_to('database');
     } else {
         $this->route_to('content');
     }
 }