<input type="hidden" name="url" value="<?php echo $url; ?> "> <p> <a href="<?php echo $url; ?> /wp-admin/tools.php?page=dbs_options"><?php echo dbs_stripHttp($url); ?> </a> <a class="button" href="<?php echo htmlspecialchars(dbs_url(array('dbs_action' => 'sync', 'url' => $url))); ?> ">Sync...</a> <input type="submit" value="Remove" class="button-secondary"> </p> </form> <?php } ?> <?php } else { ?> No sites linked yet <?php } ?>
function dbs_post_actions() { if (!isset($_REQUEST['dbs_action'])) { return; } if (!current_user_can(DBS_REQUIRED_CAPABILITY)) { wp_die(__('You do not have sufficient permissions to access this page.')); } $tokens = get_option('outlandish_sync_tokens') ?: array(); switch ($_REQUEST['dbs_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 = dbs_url(array('error' => 'The token is not valid.')); } elseif ($url == get_bloginfo('wpurl')) { $gotoUrl = dbs_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 = dbs_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(dbs_url()); exit; //pull from remote db //pull from remote db case 'pull': try { //send post request with secret $result = dbs_post($_REQUEST['url'], 'dbs_pull', array('secret' => $tokens[$_REQUEST['url']])); if ($result == 'You don\'t know me') { $gotoUrl = dbs_url(array('error' => 'Invalid site token')); } elseif ($result == '0') { $gotoUrl = dbs_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 = dbs_makeBackup(); //store some options to restore after sync $optionCache = dbs_cacheOptions(); //load the new data if (dbs_loadSql($sql)) { //clear object cache wp_cache_flush(); //restore options dbs_restoreOptions($optionCache); $gotoUrl = dbs_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 dbs_loadSql(gzinflate($compressedSql)); $gotoUrl = dbs_url(array('error' => 'Sync failed. SQL error.')); } } else { $gotoUrl = dbs_url(array('error' => 'Sync failed. Invalid dump.')); } } } catch (Exception $ex) { $gotoUrl = dbs_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(); dbs_mysqldump(); $sql = ob_get_clean(); try { //send post request with secret and SQL data $result = dbs_post($_REQUEST['url'], 'dbs_push', array('secret' => $tokens[$_REQUEST['url']], 'sql' => $sql)); if ($result == 'You don\'t know me') { $gotoUrl = dbs_url(array('error' => 'Invalid site token')); } elseif ($result == '0') { $gotoUrl = dbs_url(array('error' => 'Sync failed. Is the plugin activated on the remote server?')); } elseif ($result == 'OK') { $gotoUrl = dbs_url(array('message' => 'Database synced successfully')); } else { $gotoUrl = dbs_url(array('error' => 'Something may be wrong')); } } catch (RuntimeException $ex) { $gotoUrl = dbs_url(array('error' => 'Remote site not accessible (HTTP ' . $ex->getCode() . ')')); } wp_redirect($gotoUrl); exit; } }
?> "> <p><?php echo dbs_stripHttp(get_bloginfo('wpurl')) . " ⇒ " . dbs_stripHttp($url); ?> </p> <p> <b>Delete all 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 dbs_url(); ?> "> <input type="hidden" name="dbs_action" value="pull"> <input type="hidden" name="url" value="<?php echo $url; ?> "> <p><?php echo dbs_stripHttp(get_bloginfo('wpurl')) . " ⇐ " . dbs_stripHttp($url); ?> </p> <p> <b>Delete all data</b> in this database and replace with the data from the remote WordPress database. <input type="submit" value="Pull" class="button-primary"> </p>