Example #1
0
function dbsc_post_actions()
{
    if (!isset($_REQUEST['dbsc_action'])) {
        return;
    }
    if (!current_user_can(dbsc_REQUIRED_CAPABILITY)) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    $tokens = get_option('outlandish_sync_tokens') ?: array();
    switch ($_REQUEST['dbsc_action']) {
        //add a token
        case 'add':
            $decodedToken = base64_decode($_POST['token']);
            @(list($secret, $url) = explode(' ', $decodedToken));
            if (empty($secret) || empty($url) || !preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url)) {
                $gotoUrl = dbsc_url(array('error' => 'The token is not valid.'));
            } elseif ($url == get_bloginfo('wpurl')) {
                $gotoUrl = dbsc_url(array('error' => 'The token cannot be added as it is for this installation.'));
            } else {
                $tokens[$url] = $secret;
                update_option('outlandish_sync_tokens', $tokens);
                $gotoUrl = dbsc_url();
            }
            wp_redirect($gotoUrl);
            exit;
            //remove a token
        //remove a token
        case 'remove':
            unset($tokens[$_POST['url']]);
            update_option('outlandish_sync_tokens', $tokens);
            wp_redirect(dbsc_url());
            exit;
            //pull from remote db
        //pull from remote db
        case 'pull':
            try {
                //send post request with secret
                $result = dbsc_post($_REQUEST['url'], 'dbsc_pull', array('secret' => $tokens[$_REQUEST['url']]));
                if ($result == 'You don\'t know me') {
                    $gotoUrl = dbsc_url(array('error' => 'Invalid site token'));
                } elseif ($result == '0') {
                    $gotoUrl = dbsc_url(array('error' => 'Sync failed. Is the plugin activated on the remote server?'));
                } else {
                    $sql = $result;
                    if ($sql && preg_match('|^/\\* Dump of database |', $sql)) {
                        //backup current database
                        $backupfile = dbsc_makeBackup();
                        //store some options to restore after sync
                        $optionCache = dbsc_cacheOptions();
                        $orderCache = dbsc_cacheOrders();
                        //load the new data
                        if (dbsc_loadSql($sql)) {
                            //clear object cache
                            wp_cache_flush();
                            //restore options
                            dbsc_restoreOptions($optionCache);
                            dbsc_restoreOrders($orderCache);
                            $gotoUrl = dbsc_url(array('message' => 'Database synced successfully'));
                        } else {
                            //import failed part way through so attempt to restore last backup
                            $compressedSql = substr(file_get_contents($backupfile), 10);
                            //strip gzip header
                            dbsc_loadSql(gzinflate($compressedSql));
                            $gotoUrl = dbsc_url(array('error' => 'Sync failed. SQL error.'));
                        }
                    } else {
                        $gotoUrl = dbsc_url(array('error' => 'Sync failed. Invalid dump.'));
                    }
                }
            } catch (Exception $ex) {
                $gotoUrl = dbsc_url(array('error' => 'Remote site not accessible (HTTP ' . $ex->getCode() . ')'));
            }
            wp_redirect($gotoUrl);
            exit;
            //push to remote db
        //push to remote db
        case 'push':
            //get SQL data
            ob_start();
            dbsc_mysqldump();
            $sql = ob_get_clean();
            try {
                //send post request with secret and SQL data
                $result = dbsc_post($_REQUEST['url'], 'dbsc_push', array('secret' => $tokens[$_REQUEST['url']], 'sql' => $sql));
                if ($result == 'You don\'t know me') {
                    $gotoUrl = dbsc_url(array('error' => 'Invalid site token'));
                } elseif ($result == '0') {
                    $gotoUrl = dbsc_url(array('error' => 'Sync failed. Is the plugin activated on the remote server?'));
                } elseif ($result == 'OK') {
                    $gotoUrl = dbsc_url(array('message' => 'Database synced successfully'));
                } else {
                    $gotoUrl = dbsc_url(array('error' => 'Something may be wrong'));
                }
            } catch (RuntimeException $ex) {
                $gotoUrl = dbsc_url(array('error' => 'Remote site not accessible (HTTP ' . $ex->getCode() . ')'));
            }
            wp_redirect($gotoUrl);
            exit;
    }
}
Example #2
0
?>
">
		<p><?php 
echo dbsc_stripHttp(get_bloginfo('wpurl')) . " &#x21d2 " . dbsc_stripHttp($url);
?>
</p>
		<p>
			<b>Delete all data except for <span style="color: red">user</span> and <span style="color: red">order</span> data</b> in the remote WordPress database and replace with the data from this database.
			<input type="submit" value="Push" class="button-primary">
		</p>
	</form>

	<h3>Pull</h3>

	<form method="post" action="<?php 
dbsc_url();
?>
">
		<input type="hidden" name="dbsc_action" value="pull">
		<input type="hidden" name="url" value="<?php 
echo $url;
?>
">
		<p><?php 
echo dbsc_stripHttp(get_bloginfo('wpurl')) . " &#x21d0 " . dbsc_stripHttp($url);
?>
</p>
		<p>
			<b>Delete all data except for <span style="color: red">user</span> and <span style="color: red">order</span> data data</b> in this database and replace with the data from the remote WordPress database.
			<input type="submit" value="Pull" class="button-primary">
		</p>
Example #3
0
					<form method="post" action="">
						<input type="hidden" name="dbsc_action" value="remove">
						<input type="hidden" name="url" value="<?php 
        echo $url;
        ?>
">
						<p>
							<a href="<?php 
        echo $url;
        ?>
/wp-admin/tools.php?page=dbsc_options"><?php 
        echo dbsc_stripHttp($url);
        ?>
</a>
							<a class="button" href="<?php 
        echo htmlspecialchars(dbsc_url(array('dbsc_action' => 'sync', 'url' => $url)));
        ?>
">Sync...</a>
							<input type="submit" value="Remove" class="button-secondary">
						</p>
					</form>
					<?php 
    }
    ?>
				<?php 
} else {
    ?>
				No sites linked yet
				<?php 
}
?>