示例#1
0
function step_1_validate_form($restore_form) {

	$form_errors = array();
	$form_errors['form'] = array();
	$form_errors['message-error'] = array();
	$form_errors['message-success'] = array();

	// Do the form validation first before the heavy processing
	if ( (!isset($restore_form['snapshot']['archive-file'])) || (!strlen($restore_form['snapshot']['archive-file'])) ) {
		$form_errors['form']['snapshot']['archive-file'] = "Snapshot Archive file cannot be empty.";
	} else {
		$_SESSION['restore_form']['snapshot']['archive-file'] 	= $restore_form['snapshot']['archive-file'];
	}

	if ( (!isset($restore_form['wordpress']['reload'])) || (!strlen($restore_form['wordpress']['reload'])) ) {
		$form_errors['form']['wordpress']['reload'] = "WordPress Reload cannot be empty.";
	} else if (($restore_form['wordpress']['reload'] != "yes") && ($restore_form['wordpress']['reload'] != "no")) {
		$form_errors['form']['wordpress']['reload'] = "WordPress Reload invalid value given.";
		return $form_errors;
	} else {
		$_SESSION['restore_form']['wordpress']['reload'] = $restore_form['wordpress']['reload'];
	}

	if ( (!isset($restore_form['wordpress']['install-path'])) || (!strlen($restore_form['wordpress']['install-path'])) ) {
		$form_errors['form']['wordpress']['install-path'] = "WordPress Install Path file cannot be empty.";
	} else {
		$_SESSION['restore_form']['wordpress']['install-path'] = untrailingslashit_snapshot($restore_form['wordpress']['install-path']);
		if (!file_exists($_SESSION['restore_form']['wordpress']['install-path'])) {
			mkdir($_SESSION['restore_form']['wordpress']['install-path'], 0777, true);
		}
		$_SESSION['restore_form']['wordpress']['install-path'] = trailingslashit_snapshot($_SESSION['restore_form']['wordpress']['install-path']);

	}


	if (count($form_errors['form']))
		return $form_errors;


	// If here then the form is valid. Now get into the heavy processing
	if (!isset($_SESSION['restore_form']['snapshot']['archive-file']))
		$_SESSION['restore_form']['snapshot']['archive-file'] = '';

	unset($_SESSION['restore_form']['snapshot']['archive-file-local']);
	unset($_SESSION['restore_form']['snapshot']['archive-file-remote']);

	if (substr($_SESSION['restore_form']['snapshot']['archive-file'], 0, strlen('http')) == "http") {
		$_SESSION['restore_form']['snapshot']['archive-file-remote'] 	= $_SESSION['restore_form']['snapshot']['archive-file'];
		$_SESSION['restore_form']['snapshot']['archive-file-local'] 	= dirname(__FILE__) ."/_snapshot/file/".
			basename($_SESSION['restore_form']['snapshot']['archive-file']);

		if (file_exists($_SESSION['restore_form']['snapshot']['archive-file-local'])) {
			$unlink_ret = unlink($_SESSION['restore_form']['snapshot']['archive-file-local']);
			if ($unlink_ret !== true) {
				$form_errors['message-error'][] = "Unable to delete previous local file [". $_SESSION['restore_form']['snapshot']['archive-file-local'] ."]. Manually delete the file. Check parent folder permissions and reload the page.";
				return $form_errors;
			}
		}

		$func_ret = remote_url_to_local_file($_SESSION['restore_form']['snapshot']['archive-file-remote'],
		 	$_SESSION['restore_form']['snapshot']['archive-file-local']);
		if ( (!file_exists($_SESSION['restore_form']['snapshot']['archive-file-local']))
		  || (!filesize($_SESSION['restore_form']['snapshot']['archive-file-local'])) ) {
			$form_errors['message-error'][] = "Attempted to download remote Snapshot file to local [". $_SESSION['restore_form']['snapshot']['archive-file-local'] ."]<br />";". File not found or is empty. Check parent folder permissions and reload the page.";
			return $form_errors;
		} else {
			$form_errors['message-success'][] = "Remote Snapshot Archive [". $_SESSION['restore_form']['snapshot']['archive-file-remote'] ."] downloaded and extracted successfully.";
		}
	} else {
		$local_file = '';
		if (substr($_SESSION['restore_form']['snapshot']['archive-file'], 0, 1) == "/") {
			$local_file = $_SESSION['restore_form']['snapshot']['archive-file'];
		} else {
			$local_file = trailingslashit_snapshot($_SERVER['DOCUMENT_ROOT']) . $_SESSION['restore_form']['snapshot']['archive-file'];
		}
		if (file_exists($local_file)) {
			$_SESSION['restore_form']['snapshot']['archive-file-local'] = $local_file;
			$form_errors['message-success'][] = "Local Snapshot Archive located [". basename($local_file) ."] successfully.";
		}
	}

	if ((isset($_SESSION['restore_form']['snapshot']['archive-file-local'])) && (strlen($_SESSION['restore_form']['snapshot']['archive-file-local']))) {

		$_SESSION['restore_form']['snapshot']['extract-path'] = dirname(__FILE__) ."/_snapshot/extract/";
		unzip_archive($_SESSION['restore_form']['snapshot']['archive-file-local'], $_SESSION['restore_form']['snapshot']['extract-path']);

		// Locate and consume the Snapshot manifest file
		$_SESSION['restore_form']['snapshot']['manifest-file'] = trailingslashit_snapshot($_SESSION['restore_form']['snapshot']['extract-path'])
		 	."snapshot_manifest.txt";

		if (!file_exists($_SESSION['restore_form']['snapshot']['manifest-file'])) {
			$form_errors['message-error'][] = "Snapshot archive Manifest file missing. Cannot restore/migrate via Snapshot.";
			return $form_errors;
		}
		$manifest_data = snapshot_utility_consume_archive_manifest($_SESSION['restore_form']['snapshot']['manifest-file']);
		if (is_array($manifest_data)) {
			$_SESSION['restore_form']['snapshot']['manifest-data'] = $manifest_data;
			$form_errors['message-success'][] = "Snapshot archive Manifest located and loaded successfully.";
		}
	}



	if ( ($_SESSION['restore_form']['wordpress']['reload'] == "yes") && (isset($_SESSION['restore_form']['snapshot']['manifest-data']['WP_VERSION'])) ) {

		$_SESSION['restore_form']['wordpress']['archive-file-remote'] = 'http://wordpress.org/wordpress-'.
			$_SESSION['restore_form']['snapshot']['manifest-data']['WP_VERSION']. '.zip';

		$_SESSION['restore_form']['wordpress']['archive-file-local'] = dirname(__FILE__) ."/_wordpress/file/".
			basename($_SESSION['restore_form']['wordpress']['archive-file-remote']);

		$func_ret = remote_url_to_local_file($_SESSION['restore_form']['wordpress']['archive-file-remote'],
		 	$_SESSION['restore_form']['wordpress']['archive-file-local']);

		if ( (!file_exists($_SESSION['restore_form']['wordpress']['archive-file-local']))
		  || (!filesize($_SESSION['restore_form']['wordpress']['archive-file-local'])) ) {
			$form_errors['message-error'][] = "Attempted to download WordPress file to local [".
			$_SESSION['restore_form']['wordpress']['archive-file-local'] ."]. File not found or is empty. Check parent folder permissions and reload the page.";
			return $form_errors;

		} else {
			$form_errors['message-success'][] = "Remote WordPress  Archive [".
			 	basename($_SESSION['restore_form']['wordpress']['archive-file-local']) ."] downloaded successfully.";

			// Extract WordPress files into place
			$_SESSION['restore_form']['wordpress']['extract-path'] = dirname(__FILE__) ."/_wordpress/extract/";

			$unzip_ret = unzip_archive($_SESSION['restore_form']['wordpress']['archive-file-local'], $_SESSION['restore_form']['wordpress']['extract-path']);
			if (file_exists($_SESSION['restore_form']['wordpress']['extract-path'] ."/wordpress")) {
				$_SESSION['restore_form']['wordpress']['extract-path'] = $_SESSION['restore_form']['wordpress']['extract-path'] ."/wordpress";

				$form_errors['message-success'][] = "WordPress Archive extracted successfully.";
			}

			move_tree($_SESSION['restore_form']['wordpress']['extract-path'], $_SESSION['restore_form']['wordpress']['install-path']);
		}
	}


	return $form_errors;
}
示例#2
0
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;
}
		/**
		 * 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
			}
		}
示例#4
0
 /**
  * AJAX callback function from the snapshot restore form. This is the first
  * step of the restore. This step will unzip the archive and retrieve the
  * the MANIFEST file content.
  *
  * @since 1.0.2
  * @see
  *
  * @param none
  * @return JSON formatted array status.
  */
 function snapshot_ajax_restore_init($item)
 {
     global $wpdb, $current_blog;
     $error_status = array();
     $error_status['errorStatus'] = false;
     $error_status['errorText'] = "";
     $error_status['responseText'] = "";
     $home_path = apply_filters('snapshot_home_path', get_home_path());
     if (!isset($_POST['item_data'])) {
         $error_status['errorStatus'] = true;
         $error_status['errorText'] = "<p>" . __("ERROR: The Snapshot missing 'item_data' key", SNAPSHOT_I18N_DOMAIN) . "</p>";
         return $error_status;
     }
     $item_data = intval($_POST['item_data']);
     if (!isset($item['data'][$item_data])) {
         $error_status['errorStatus'] = true;
         $error_status['errorText'] = "<p>" . __("ERROR: The Snapshot incorrect 'item_data' [" . $data_item . "] key", SNAPSHOT_I18N_DOMAIN) . "</p>";
         return $error_status;
     }
     //$error_status['data'] = $item['data'];
     $data_item = $item['data'][$item_data];
     //echo "data_item<pre>"; print_r($data_item); echo "</pre>";
     $backupZipFolder = $this->snapshot_get_item_destination_path($item, $data_item, false);
     //echo "backupZipFolder[". $backupZipFolder ."]<br />";
     //die();
     $restoreFile = trailingslashit($backupZipFolder) . $data_item['filename'];
     $error_status['restoreFile'] = $restoreFile;
     if (!file_exists($restoreFile)) {
         $error_status_errorText = "<p>" . __("ERROR: The Snapshot file not found:", SNAPSHOT_I18N_DOMAIN) . " " . $restoreFile . "</p>";
         $restoreFile = trailingslashit($this->_settings['backupBaseFolderFull']) . $data_item['filename'];
         $error_status['restoreFile'] = $restoreFile;
         if (!file_exists($restoreFile)) {
             $error_status['errorStatus'] = true;
             $error_status['errorText'] = $error_status_errorText . "<p>" . __("ERROR: The Snapshot file not found:", SNAPSHOT_I18N_DOMAIN) . " " . $restoreFile . "</p>";
             return $error_status;
         }
     }
     // Create a unique folder for our restore processing. Will later need to remove it.
     $sessionRestoreFolder = trailingslashit($this->_settings['backupRestoreFolderFull']);
     wp_mkdir_p($sessionRestoreFolder);
     if (!is_writable($sessionRestoreFolder)) {
         $error_status['errorStatus'] = true;
         $error_status['errorText'] = "<p>" . __("ERROR: The Snapshot folder is not writeable. Check the settings", SNAPSHOT_I18N_DOMAIN) . " " . $sessionRestoreFolder . "</p>";
         return $error_status;
     }
     // Cleanup any files from a previous restore attempt
     if ($dh = opendir($sessionRestoreFolder)) {
         while (($file = readdir($dh)) !== false) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             snapshot_utility_recursive_rmdir($sessionRestoreFolder . $file);
         }
         closedir($dh);
     }
     if ($this->config_data['config']['zipLibrary'] == "PclZip") {
         if (!defined('PCLZIP_TEMPORARY_DIR')) {
             define('PCLZIP_TEMPORARY_DIR', trailingslashit($this->_settings['backupBackupFolderFull']) . $item['timestamp'] . "/");
         }
         if (!class_exists('class PclZip')) {
             require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
         }
         $zipArchive = new PclZip($restoreFile);
         $zip_contents = $zipArchive->listContent();
         if ($zip_contents) {
             $extract_files = $zipArchive->extract(PCLZIP_OPT_PATH, $sessionRestoreFolder);
             if ($extract_files) {
                 $this->_session->data['restoreFolder'] = $sessionRestoreFolder;
             }
         }
     } else {
         $zip = new ZipArchive();
         $res = $zip->open($restoreFile);
         if ($res === TRUE) {
             $extract_ret = $zip->extractTo($sessionRestoreFolder);
             if ($extract_ret !== false) {
                 $this->_session->data['restoreFolder'] = $sessionRestoreFolder;
             }
         }
     }
     $error_status['MANIFEST'] = array();
     $snapshot_manifest_file = trailingslashit($sessionRestoreFolder) . 'snapshot_manifest.txt';
     if (file_exists($snapshot_manifest_file)) {
         $error_status['MANIFEST'] = snapshot_utility_consume_archive_manifest($snapshot_manifest_file);
         //unlink($snapshot_manifest_file);
     }
     if (isset($error_status['MANIFEST']['SNAPSHOT_VERSION'])) {
         if ($error_status['MANIFEST']['SNAPSHOT_VERSION'] == "1.0" && !isset($error_status['MANIFEST']['TABLES-DATA'])) {
             $backupFile = trailingslashit($sessionRestoreFolder) . 'snapshot_backups.sql';
             $table_segments = snapshot_utility_get_table_segments_from_single($backupFile);
             if ($table_segments) {
                 $error_status['MANIFEST']['TABLES-DATA'] = $table_segments;
                 unlink($backupFile);
             }
         }
     }
     if (is_multisite()) {
         //echo "item<pre>"; print_r($item); echo "</pre>";
         //echo "error_status<pre>"; print_r($error_status); echo "</pre>";
         //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
         //switch_to_blog( $item['blog-id'] );
         $error_status['MANIFEST']['RESTORE']['SOURCE'] = array();
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_BLOG_ID'] = $error_status['MANIFEST']['WP_BLOG_ID'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'] = $error_status['MANIFEST']['WP_DB_PREFIX'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'] = $error_status['MANIFEST']['WP_DB_BASE_PREFIX'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_SITEURL'] = $error_status['MANIFEST']['WP_SITEURL'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR'] = $error_status['MANIFEST']['WP_UPLOAD_PATH'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_UPLOAD_URLS'] = $error_status['MANIFEST']['WP_UPLOAD_URLS'];
         switch_to_blog($_POST['snapshot-blog-id']);
         $error_status['MANIFEST']['RESTORE']['DEST'] = array();
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_BLOG_ID'] = $_POST['snapshot-blog-id'];
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'] = $wpdb->get_blog_prefix($_POST['snapshot-blog-id']);
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_BASE_PREFIX'] = $wpdb->base_prefix;
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] = get_site_url($_POST['snapshot-blog-id']);
         if (empty($error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'])) {
             if (!empty($_POST['snapshot_blog_search'])) {
                 $error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] = network_site_url('/' . untrailingslashit($_POST['snapshot_blog_search']) . '/');
             } else {
                 $error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] = $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_SITEURL'];
             }
         }
         $wp_upload_dir = wp_upload_dir();
         //echo "wp_upload_dir<pre>"; print_r($wp_upload_dir); echo "</pre>";
         //die();
         $wp_upload_dir['basedir'] = str_replace('\\', '/', $wp_upload_dir['basedir']);
         $error_status['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR'] = str_replace($home_path, '', $wp_upload_dir['basedir']);
         //echo "error_status<pre>"; print_r($error_status); echo "</pre>";
         //die();
     } else {
         $error_status['MANIFEST']['RESTORE']['SOURCE'] = array();
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_BLOG_ID'] = $error_status['MANIFEST']['WP_BLOG_ID'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'] = $error_status['MANIFEST']['WP_DB_PREFIX'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'] = $error_status['MANIFEST']['WP_DB_BASE_PREFIX'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_SITEURL'] = $error_status['MANIFEST']['WP_SITEURL'];
         $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_UPLOAD_URLS'] = $error_status['MANIFEST']['WP_UPLOAD_URLS'];
         $wp_upload_dir = wp_upload_dir();
         $wp_upload_dir['basedir'] = str_replace('\\', '/', $wp_upload_dir['basedir']);
         $error_status['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR'] = $wp_upload_dir['basedir'];
         $error_status['MANIFEST']['RESTORE']['DEST'] = array();
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_BLOG_ID'] = $error_status['MANIFEST']['WP_BLOG_ID'];
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'] = $wpdb->prefix;
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_BASE_PREFIX'] = $wpdb->base_prefix;
         $error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] = get_site_url($error_status['MANIFEST']['WP_BLOG_ID']);
         if (empty($error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'])) {
             $error_status['MANIFEST']['RESTORE']['DEST']['WP_SITEURL'] = $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_SITEURL'];
         }
         $error_status['MANIFEST']['RESTORE']['DEST']['UPLOAD_DIR'] = $error_status['MANIFEST']['RESTORE']['SOURCE']['UPLOAD_DIR'];
     }
     if (!isset($_POST['snapshot-tables-option'])) {
         $_POST['snapshot-tables-option'] = "none";
     }
     if ($_POST['snapshot-tables-option'] == "none") {
         unset($error_status['MANIFEST']['TABLES']);
         $error_status['MANIFEST']['TABLES'] = array();
     } else {
         if ($_POST['snapshot-tables-option'] == "selected") {
             if (isset($_POST['snapshot-tables-array'])) {
                 $error_status['MANIFEST']['TABLES'] = $_POST['snapshot-tables-array'];
             }
         } else {
             if ($_POST['snapshot-tables-option'] == "all") {
                 $manifest_tables = array();
                 foreach ($error_status['MANIFEST']['TABLES'] as $table_set_key => $table_set) {
                     // Per the instructions on the page. When selecting 'all' we do not include the global tables: users and usermeta
                     if ($table_set_key == 'global') {
                         continue;
                     }
                     $manifest_tables = array_merge($manifest_tables, array_values($table_set));
                 }
                 //echo "manifest_tables<pre>"; print_r($manifest_tables); echo "</pre>";
                 //die();
                 $error_status['MANIFEST']['TABLES'] = $manifest_tables;
             }
         }
     }
     //echo "RESTORE<pre>"; print_r($error_status['MANIFEST']['RESTORE']); echo "</pre>";
     //echo "TABLES<pre>"; print_r($error_status['MANIFEST']['TABLES']); echo "</pre>";
     //echo "MANIFEST<pre>"; print_r($error_status['MANIFEST']); echo "</pre>";
     //echo "wpdb<pre>"; print_r($wpdb); echo "</pre>";
     //die();
     // upload_path wp-content/blogs.dir/7/files
     if (isset($error_status['MANIFEST']['TABLES']) && count($error_status['MANIFEST']['TABLES'])) {
         $tables_array = array();
         foreach ($error_status['MANIFEST']['TABLES'] as $table_name) {
             $table_info = array();
             $table_info['table_name'] = $table_name;
             if (strncasecmp($table_name, $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'], strlen($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'])) == 0) {
                 $table_info['table_name_base'] = str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'], '', $table_name);
                 $table_info['table_name_restore'] = $this->_settings['recover_table_prefix'] . str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'], $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'], $table_name);
                 $table_name_dest = str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_PREFIX'], $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_PREFIX'], $table_name);
             } else {
                 if (strncasecmp($table_name, $error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'], strlen($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'])) == 0) {
                     $table_info['table_name_base'] = str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'], '', $table_name);
                     $table_info['table_name_restore'] = $this->_settings['recover_table_prefix'] . str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'], $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_BASE_PREFIX'], $table_name);
                     $table_name_dest = str_replace($error_status['MANIFEST']['RESTORE']['SOURCE']['WP_DB_BASE_PREFIX'], $error_status['MANIFEST']['RESTORE']['DEST']['WP_DB_BASE_PREFIX'], $table_name);
                 } else {
                     // If the table name is not using the DB_PREFIX or DB_BASE_PREFIX then don't convert it.
                     $table_info['table_name_base'] = $table_name;
                     $table_info['table_name_restore'] = $table_name;
                     $table_name_dest = $table_name;
                 }
             }
             $table_info['label'] = $table_name . " > " . $table_name_dest;
             $table_info['table_name_dest'] = $table_name_dest;
             $tables_array[$table_name] = $table_info;
         }
         $error_status['MANIFEST']['TABLES'] = $tables_array;
         //echo "MANIFEST<pre>"; print_r($error_status['MANIFEST']['TABLES']); echo "</pre>";
         //die();
     }
     if (isset($error_status['MANIFEST']['TABLES-DATA']) && count($error_status['MANIFEST']['TABLES-DATA'])) {
         $tables_data_sets = array();
         foreach ($error_status['MANIFEST']['TABLES-DATA'] as $table_set) {
             if (!isset($table_set['table_name'])) {
                 continue;
             }
             //echo "table_set table_name[". $table_set['table_name'] ."]<br />";
             if (array_key_exists($table_set['table_name'], $error_status['MANIFEST']['TABLES']) !== false) {
                 $tables_data_sets[] = $table_set;
             } else {
                 //echo "Table[". $table_set['table_name'] ."] not found in tables<br />";
             }
         }
         $error_status['MANIFEST']['TABLES-DATA'] = $tables_data_sets;
     }
     //echo "MANIFEST<pre>"; print_r($error_status['MANIFEST']['TABLES']); echo "</pre>";
     //echo "MANIFEST<pre>"; print_r($error_status['MANIFEST']['TABLES-DATA']); echo "</pre>";
     //die();
     if (!isset($_POST['snapshot-files-option'])) {
         $_POST['snapshot-files-option'] = "none";
     }
     if ($_POST['snapshot-files-option'] == "none") {
         unset($error_status['MANIFEST']['FILES-DATA']);
         $error_status['MANIFEST']['FILES-DATA'] = array();
     } else {
         if ($_POST['snapshot-files-option'] == "selected") {
             if (isset($_POST['snapshot-files-sections'])) {
                 $error_status['MANIFEST']['FILES-DATA'] = $_POST['snapshot-files-sections'];
             }
         } else {
             if ($_POST['snapshot-files-option'] == "all") {
                 if (isset($error_status['MANIFEST']['ITEM']['data'])) {
                     $data_item = snapshot_utility_latest_data_item($error_status['MANIFEST']['ITEM']['data']);
                     if (isset($data_item['files-sections'])) {
                         $error_status['MANIFEST']['FILES-DATA'] = array_values($data_item['files-sections']);
                         $array_idx = array_search('config', $error_status['MANIFEST']['FILES-DATA']);
                         if ($array_idx !== false) {
                             unset($error_status['MANIFEST']['FILES-DATA'][$array_idx]);
                         }
                         $array_idx = array_search('htaccess', $error_status['MANIFEST']['FILES-DATA']);
                         if ($array_idx !== false) {
                             unset($error_status['MANIFEST']['FILES-DATA'][$array_idx]);
                         }
                     }
                 }
             }
         }
     }
     //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
     //echo "MANIFEST<pre>"; print_r($error_status['MANIFEST']); echo "</pre>";
     //echo "MANIFEST RESTORE<pre>"; print_r($error_status['MANIFEST']['RESTORE']); echo "</pre>";
     //die();
     $this->_session->data['MANIFEST'] = $error_status['MANIFEST'];
     return $error_status;
 }