/**
		 * Panel showing form to restore previous Snapshot.
		 *
		 * @since 1.0.2
		 * @uses metaboxes setup in $this->admin_menu_proc()
		 * @uses $_REQUEST['item']
		 * @uses $this->config_data['items']
		 *
		 * @param none
		 * @return none
		 */
		function snapshot_admin_show_restore_panel($item) {
			global $wpmudev_snapshot;

			require( $wpmudev_snapshot->snapshot_get_setting('SNAPSHOT_PLUGIN_BASE_DIR'). '/lib/snapshot_admin_metaboxes.php' );
			$this->_snapshot_metaboxes = new wpmudev_snapshot_admin_metaboxes( );

			if ((isset($_GET['snapshot-data-item'])) && (isset($item['data'][intval($_GET['snapshot-data-item'])]))) {
				$data_item_key = intval($_GET['snapshot-data-item']);
				?>
				<div id="snapshot-settings-metaboxes-general" class="wrap snapshot-wrap">
					<?php screen_icon('snapshot'); ?>
					<h2><?php _ex("Restore Snapshot", "Snapshot Plugin Page Title", SNAPSHOT_I18N_DOMAIN); ?></h2>

					<p class="snapshot-restore-description"><?php _ex("On this page you can restore a previous snapshot. Using the 'Restore Options' section below you can also opt to turn off all plugins as well as switch to a different theme as part of the restore.", 'Snapshot page description', SNAPSHOT_I18N_DOMAIN); ?></p>

					<div id='snapshot-ajax-warning' class='updated fade'><p><?php _e('You are about to restore a previous version of your WordPress database. This will remove any new information added since the snapshot backup.', SNAPSHOT_I18N_DOMAIN); ?></p></div>

					<?php
						if (!snapshot_utility_check_server_timeout()) {
							$current_timeout = ini_get('max_execution_time');
							?><div class='error snapshot-error'><p><?php printf(__('Your web server timeout is set very low, %d seconds. Also, it appears this timeout cannot be adjusted via the Snapshot restore process. Attempting a snapshot restore could result in a partial restore of your tables.', SNAPSHOT_I18N_DOMAIN), $current_timeout); ?></p></div><?php
						}
					?>
					<?php snapshot_utility_form_ajax_panels(); ?>
					<?php
						if (isset($_GET['snapshot-data-item'])) {
							$data_item = $item['data'][$_GET['snapshot-data-item']];
						}

						$backupFolder = $wpmudev_snapshot->snapshot_get_item_destination_path($item, $data_item);
						if (empty($backupFolder)) {
							$backupFolder = $wpmudev_snapshot->snapshot_get_setting('backupBaseFolderFull');
						}

						if ((isset($data_item['filename'])) && (strlen($data_item['filename']))) {
							$manifest_filename = snapshot_utility_extract_archive_manifest(trailingslashit($backupFolder) . $data_item['filename']);
							if ($manifest_filename) {
								//echo "manifest_filename=[". $manifest_filename ."]<br />";
								$manifest_data = snapshot_utility_consume_archive_manifest($manifest_filename);
								if ($manifest_data) {
									//echo "manifest_data<pre>"; print_r($manifest_data); echo "</pre>";
									$item['MANIFEST'] = $manifest_data;
								}
							}
						}

					?>


					<div id="poststuff" class="metabox-holder">

						<form id="snapshot-edit-restore" action="<?php
							echo $wpmudev_snapshot->snapshot_get_setting('SNAPSHOT_MENU_URL'); ?>snapshots_edit_panel" method="post">
							<input type="hidden" name="snapshot-action" value="restore-request" />
							<input type="hidden" name="item" value="<?php echo $item['timestamp']; ?>" />
							<?php wp_nonce_field('snapshot-restore', 'snapshot-noonce-field'); ?>

							<?php $this->_snapshot_metaboxes->snapshot_metaboxes_show_item_header_information(
								__('Snapshot Information', SNAPSHOT_I18N_DOMAIN), $item, true ); ?>

							<?php
								$this->_snapshot_metaboxes->snapshot_metabox_show_archive_files(
									__('Selected Archive to Restore', SNAPSHOT_I18N_DOMAIN), $item, true );
							?>
							<?php
								//if (is_multisite()) {
									$this->_snapshot_metaboxes->snapshot_metabox_restore_blog_options(
										__('Restore Blog Options', SNAPSHOT_I18N_DOMAIN), $item );
								//}
							?>
							<?php
								$this->_snapshot_metaboxes->snapshot_metabox_show_restore_tables_options(
									__('What Tables to Restore?', SNAPSHOT_I18N_DOMAIN), $item, $data_item_key);
							?>
							<?php
								$this->_snapshot_metaboxes->snapshot_metabox_show_restore_files_options(
									__('What Files to Restore?', SNAPSHOT_I18N_DOMAIN), $item, $data_item_key);

							?>
							<?php
								$this->_snapshot_metaboxes->snapshot_metabox_restore_options( __('Restore Theme Options', SNAPSHOT_I18N_DOMAIN), $item );
							?>
							<input id="snapshot-form-restore-submit" class="button-primary"
							<?php
								if (!$data_item_key) {	?> disabled="disabled" <?php } ?>
								type="submit" value="<?php _e('Restore Snapshot', SNAPSHOT_I18N_DOMAIN); ?>" />
							<a class="button-secondary" href="<?php echo $wpmudev_snapshot->snapshot_get_setting('SNAPSHOT_MENU_URL'); ?>
									snapshots_edit_panel"><?php _e('Cancel', SNAPSHOT_I18N_DOMAIN); ?></a>
						</form>
					</div>
				</div>
				<?php
			} else {
				?>
				<div id="snapshot-settings-metaboxes-general" class="wrap snapshot-wrap">
					<?php screen_icon('snapshot'); ?>
					<h2><?php _ex("Restore Snapshot", "Snapshot Plugin Page Title", SNAPSHOT_I18N_DOMAIN); ?></h2>

					<p class="snapshot-restore-description"><?php _ex("ERROR: Missing argument. Please return to the main Snapshot panel and select the archive to restore. ", 'Snapshot page description', SNAPSHOT_I18N_DOMAIN); ?><a href="?page=snapshots_edit_panel">Snapshot</a>.</p>
				</div>
				<?php
			}
		}
function snapshot_utility_archives_import_proc($restoreFile, $restoreFolder) {
	global $wpmudev_snapshot, $wpdb;

	$wpmudev_snapshot->load_config();
	$wpmudev_snapshot->set_backup_folder();
	$wpmudev_snapshot->set_log_folders();

	$CONFIG_CHANGED = false;

//	echo "restoreFile=[". $restoreFile ."]<br />";
//	echo "restoreFolder=[". $restoreFolder ."]<br />";
//	echo "before items<pre>"; print_r($wpmudev_snapshot->config_data['items']); echo "</pre>";

	$error_status = array();
	$error_status['errorStatus'] 	= false;
	$error_status['errorText'] 		= "";
	$error_status['responseText'] 	= "";

	$snapshot_manifest = snapshot_utility_extract_archive_manifest($restoreFile, $restoreFolder);

	if (file_exists($snapshot_manifest)) {
		//echo "snapshot_manifest[". $snapshot_manifest ."]<br />";
		$CONFIG_CHANGED = false;

		$manifest_data = snapshot_utility_consume_archive_manifest($snapshot_manifest);
		//echo "manifest_data<pre>"; print_r($manifest_data); echo "</pre>";
		//die();

		if ( empty($manifest_data) ) {
			$error_status['errorStatus'] 	= true;
			$error_status['errorText'] 		= __("Manifest data not found in archive.", SNAPSHOT_I18N_DOMAIN);

			return $error_status;
		}

		if ( (!isset($manifest_data['ITEM'])) || (empty($manifest_data['ITEM'])) ) {
			$error_status['errorStatus'] 	= true;
			$error_status['errorText'] 		= __("Manifest data does not contain ITEM section.", SNAPSHOT_I18N_DOMAIN);

			return $error_status;
		}
		$item = $manifest_data['ITEM'];

		if ( (!isset($item['timestamp'])) || (empty($item['timestamp'])) ) {
			$error_status['errorStatus'] 	= true;
			$error_status['errorText'] 		= __("Manifest ITEM does not contain 'timestamp' item.", SNAPSHOT_I18N_DOMAIN);

			return $error_status;
		}

		//$siteurl = get_option('siteurl');
		//echo "siteurl=[". $siteurl ."]<br />";

		$RESTORE['LOCAL'] 	= array();

		if (is_multisite()) {
			$blog_details = get_blog_details($manifest_data['WP_BLOG_ID']);
			//echo "blog_details<pre>"; print_r($blog_details); echo "</pre>";
			if ((isset($blog_details->domain)) && (!empty($blog_details->domain)))
				$RESTORE['LOCAL']['WP_BLOG_DOMAIN'] = $blog_details->domain;
			else
				$RESTORE['LOCAL']['WP_BLOG_DOMAIN'] = '';

			if ((isset($blog_details->path)) && (!empty($blog_details->path)))
				$RESTORE['LOCAL']['WP_BLOG_PATH'] = $blog_details->path;
			else
				$RESTORE['LOCAL']['WP_BLOG_PATH'] = '';
		} else {
			$siteurl = get_option('siteurl');
			$RESTORE['LOCAL']['WP_BLOG_DOMAIN'] = parse_url($siteurl, PHP_URL_HOST);
			$RESTORE['LOCAL']['WP_BLOG_PATH'] 	= parse_url($siteurl, PHP_URL_PATH);
			//$RESTORE['LOCAL']['WP_BLOG_ID']		= $blog_id;
		}

		$RESTORE['IMPORT'] 	= array();

		if (isset($manifest_data['WP_BLOG_ID']))
			$RESTORE['IMPORT']['WP_BLOG_ID'] 		= $manifest_data['WP_BLOG_ID'];
		else
			$RESTORE['IMPORT']['WP_BLOG_ID']		= '';


		if (isset($manifest_data['WP_BLOG_NAME']))
			$RESTORE['IMPORT']['WP_BLOG_NAME'] 		= $manifest_data['WP_BLOG_NAME'];
		else
			$RESTORE['IMPORT']['WP_BLOG_NAME']		= '';


		if (isset($manifest_data['WP_DB_NAME']))
			$RESTORE['IMPORT']['WP_DB_NAME'] 		= $manifest_data['WP_DB_NAME'];
		else
			$RESTORE['IMPORT']['WP_DB_NAME']		= '';


		if (isset($manifest_data['WP_DB_BASE_PREFIX']))
			$RESTORE['IMPORT']['WP_DB_BASE_PREFIX'] = $manifest_data['WP_DB_BASE_PREFIX'];
		else
			$RESTORE['IMPORT']['WP_DB_BASE_PREFIX'] = '';


		if (isset($manifest_data['WP_DB_PREFIX']))
			$RESTORE['IMPORT']['WP_DB_PREFIX'] 		= $manifest_data['WP_DB_PREFIX'];
		else
			$RESTORE['IMPORT']['WP_DB_PREFIX']		= '';


		if (isset($manifest_data['WP_HOME']))
			$RESTORE['IMPORT']['WP_HOME'] 			= $manifest_data['WP_HOME'];

		if (isset($manifest_data['WP_SITEURL']))
			$RESTORE['IMPORT']['WP_SITEURL'] 		= $manifest_data['WP_SITEURL'];
		else
			$RESTORE['IMPORT']['WP_SITEURL']		= '';


		if (isset($manifest_data['WP_UPLOAD_PATH']))
			$RESTORE['IMPORT']['WP_UPLOAD_PATH'] 	= $manifest_data['WP_UPLOAD_PATH'];
		else
			$RESTORE['IMPORT']['WP_UPLOAD_PATH']	= '';


		if (isset($manifest_data['WP_UPLOAD_URLS']))
			$RESTORE['IMPORT']['WP_UPLOAD_URLS'] 	= $manifest_data['WP_UPLOAD_URLS'];
		else
			$RESTORE['IMPORT']['WP_UPLOAD_URLS']	= array();


		if (isset($manifest_data['WP_BLOG_DOMAIN'])) {
			$RESTORE['IMPORT']['WP_BLOG_DOMAIN'] = $manifest_data['WP_BLOG_DOMAIN'];
		} else if (isset($manifest_data['WP_SITEURL'])) {
			$RESTORE['LOCAL']['WP_BLOG_DOMAIN'] = parse_url($manifest_data['WP_SITEURL'], PHP_URL_HOST);
		}

		if (isset($manifest_data['WP_BLOG_PATH'])) {
			$RESTORE['IMPORT']['WP_BLOG_PATH'] = $manifest_data['WP_BLOG_PATH'];
		} else if (isset($manifest_data['WP_SITEURL'])) {
			$RESTORE['IMPORT']['WP_BLOG_PATH'] = parse_url($manifest_data['WP_SITEURL'], PHP_URL_PATH);
		}

		//echo "RESTORE<pre>"; print_r($RESTORE); echo "</pre>";
		//die();

		if (($RESTORE['IMPORT']['WP_BLOG_DOMAIN'] != $RESTORE['LOCAL']['WP_BLOG_DOMAIN'])
		 || ($RESTORE['IMPORT']['WP_BLOG_PATH'] != $RESTORE['LOCAL']['WP_BLOG_PATH'])) {

			$item['IMPORT'] = $RESTORE['IMPORT'];

			// For Multisite we try and lookup the site based on the DOMAIN+PATH
			if (is_multisite()) {
				global $wpdb;

				if (is_subdomain_install()) {
					$sql_str = $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $RESTORE['IMPORT']['WP_BLOG_DOMAIN']);
					//$sql_str = $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $RESTORE['LOCAL']['WP_BLOG_DOMAIN']);
				} else {
					$snapshot_blog_id_search_path 	= trailingslashit($RESTORE['IMPORT']['WP_BLOG_PATH']);
					$snapshot_blog_id_search_domain = untrailingslashit($RESTORE['IMPORT']['WP_BLOG_DOMAIN']);
					$sql_str = $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s LIMIT 1",
						$snapshot_blog_id_search_domain, $snapshot_blog_id_search_path);
				}
				//echo "sql_str=[". $sql_str ."]<br />";
				$blog = $wpdb->get_row( $sql_str );
				if ((isset($blog->blog_id)) && ($blog->blog_id > 0)) { // found
					//echo "blog<pre>"; print_r($blog); echo "</pre>";
					$item['blog-id'] = $blog->blog_id;
				} else {
					$item['blog-id'] = 0;
				}
			} else {
				$item['blog-id'] = 0;
			}
		}

		//echo "item<pre>"; print_r($item); echo "</pre>";
		//die();

		$item_key = $item['timestamp'];

		if ( (!isset($item['data'])) || (!count($item['data'])) ) {
			$error_status['errorStatus'] 	= true;
			$error_status['errorText'] 		= __("Manifest ITEM does not contain 'data' section.", SNAPSHOT_I18N_DOMAIN);

			return $error_status;
		}

		// Now we check the manifest item against the config data.
		foreach($item['data'] as $data_item_key => $data_item) {

			if ((!isset($data_item['filename'])) || (empty($data_item['filename']))) {
				$item['data'][$data_item_key]['filename'] = basename($restoreFile);
			}

			if ((!isset($data_item['file_size'])) || (empty($data_item['file_size']))) {
				$item['data'][$data_item_key]['file_size'] = filesize($restoreFile);
			}
		}

		if (!isset($wpmudev_snapshot->config_data['items'][$item_key])) {
			$wpmudev_snapshot->config_data['items'][$item_key] = $item;
			$CONFIG_CHANGED = true;

			$error_status['errorStatus'] 	= false;
			$error_status['responseText'] 	= __("Archive imported successfully.", SNAPSHOT_I18N_DOMAIN);

		} else {
			foreach($item['data'] as $data_item_key => $data_item) {

				if (!isset($wpmudev_snapshot->config_data['items'][$item_key]['data'][$data_item_key])) {
					$wpmudev_snapshot->config_data['items'][$item_key]['data'][$data_item_key] = $data_item;
					$CONFIG_CHANGED = true;

					$error_status['errorStatus'] 	= false;
					$error_status['responseText'] 	= __("Archive imported successfully.", SNAPSHOT_I18N_DOMAIN);

				} else {
					$error_status['errorStatus'] 	= false;
					$error_status['responseText'] 	= __("already present. not importing.", SNAPSHOT_I18N_DOMAIN);
				}
			}
		}

		if ($CONFIG_CHANGED == true) {
			$wpmudev_snapshot->save_config();
		}
	} else {
		$error_status['errorStatus'] 	= true;
		$error_status['errorText'] 		= __("Manifest data not found in archive.", SNAPSHOT_I18N_DOMAIN);
	}

	return $error_status;
}