/** * Final validation after all params etc have been set, setting errors as appropriate. * * This is called when options are updated from settings screen, generating errors as appropriate, * and when plugin is initialised, in which case errors not generated and capabilities not checked. * * @return bool TRUE if options OK, FALSE otherwise */ private function final_validate() { //check wp_content dir $current_content_dir = $this->current_site_conf['web_path'] . $this->current_site_conf['wp_content_dir']; if (WP_CONTENT_DIR != $current_content_dir) { SitePushErrors::add_error("Currently configured WordPress content directory (" . WP_CONTENT_DIR . ") is different from the configured uploads directory in your sites config file ({$current_content_dir})", 'warning'); } //check uploads dir $uld = wp_upload_dir(); if ($uld['error']) { SitePushErrors::add_error("Could not get path of upload directory. SitePush should still work, but won't be able to push files in your uploads directory. WordPress returned the following error:<br />{$uld['error']}", 'warning'); } else { $current_uld = $this->current_site_conf['web_path'] . $this->current_site_conf['wp_uploads_dir']; if ($uld['basedir'] != $current_uld) { SitePushErrors::add_error("Currently configured WordPress uploads directory ({$uld['basedir']}) is different from the configured uploads directory in your sites config file ({$current_uld})", 'warning', 'options'); } } //check plugins dir $current_plugins_dir = $this->current_site_conf['web_path'] . $this->current_site_conf['wp_plugin_dir']; if (WP_PLUGIN_DIR != $current_plugins_dir) { SitePushErrors::add_error("Currently configured WordPress plugins directory (" . WP_PLUGIN_DIR . ") is different from the configured plugins directory in your sites config file ({$current_plugins_dir})", 'warning'); } //check mu-plugins dir $current_muplugins_dir = $this->current_site_conf['web_path'] . $this->current_site_conf['wpmu_plugin_dir']; if (WPMU_PLUGIN_DIR != $current_muplugins_dir) { SitePushErrors::add_error("Currently configured WordPress must-use plugins directory (" . WPMU_PLUGIN_DIR . ") is different from the configured plugins directory in your sites config file ({$current_muplugins_dir})", 'warning'); } //check themes dir $current_themes_dir = $this->current_site_conf['web_path'] . $this->current_site_conf['wp_themes_dir']; if (WP_CONTENT_DIR . '/themes' != $current_themes_dir) { SitePushErrors::add_error("Currently configured WordPress themes directory (" . WP_CONTENT_DIR . "/themes) is different from the configured themes directory in your sites config file ({$current_themes_dir})", 'warning'); } return !SitePushErrors::is_error(); }
/** * Copy source DB to destintation, with backup * * @param array $table_groups which groups of tables to push * @access public * @return bool result of push command */ public function push_db($table_groups = array()) { $db_source = $this->options->get_db_params_for_site($this->source); $db_dest = $this->options->get_db_params_for_site($this->dest); //last minute error checking if (!$db_source) { SitePushErrors::add_error('Database not pushed. DB config for source site is empty.', 'fatal-error'); } if (!$db_dest) { SitePushErrors::add_error('Database not pushed. DB config for destination site is empty.', 'fatal-error'); } if (SitePushErrors::is_error()) { return FALSE; } if ($db_source['name'] == $db_dest['name'] && gethostbyname($db_source['host']) == gethostbyname($db_dest['host'])) { SitePushErrors::add_error('Database not pushed. Source and destination databases cannot be the same.', 'fatal-error'); } if (!@shell_exec("{$this->options->mysql_path} --version")) { SitePushErrors::add_error('mysql not found, not configured properly or PHP safe mode is preventing it from being run.'); } if (!@shell_exec("{$this->options->mysqldump_path} --version")) { SitePushErrors::add_error('mysqldump not found, not configured properly or PHP safe mode is preventing it from being run.'); } if (SitePushErrors::is_error()) { return FALSE; } //work out which table(s) to push $tables = ''; if ($table_groups) { foreach ($table_groups as $table_group) { //if table group is an array, then it is an array of custom table groups if (is_array($table_group)) { $tables .= ' ' . $this->get_custom_tables($table_group); } else { $tables .= ' ' . $this->get_tables($table_group); } } } $tables = trim($tables); if ($tables) { $tables = "--tables {$tables}"; } elseif (is_multisite()) { $this->fix_domains = TRUE; } //we are pushing all tables, and in multisite, so we need to fix domains after push //backup database $backup_file = $this->database_backup($db_dest); //create mysql command $source_host = !empty($db_source['host']) ? " --host={$db_source['host']}" : ''; $dest_host = !empty($db_dest['host']) ? " --host={$db_dest['host']}" : ''; $dump_command = "{$this->options->mysqldump_path} {$this->dump_options}{$source_host} -u {$db_source['user']} -p'{$db_source['pw']}' {$db_source['name']} {$tables}"; $mysql_command = "{$this->options->mysql_path} -D {$db_dest['name']} -u {$db_dest['user']}{$dest_host} -p'{$db_dest['pw']}'"; $command = "{$dump_command} | " . $this->make_remote($mysql_command); //write file which will undo the push if ($this->save_undo && $backup_file) { $undo['type'] = 'mysql'; $undo['original'] = $command; //$undo['remote'] = $this->remote_shell; //@later make remote $undo['undo'] = "'{$this->options->mysql_path}' -u {$db_dest['user']} -p'{$db_dest['pw']}'{$dest_host} -D {$db_dest['name']} < '{$backup_file}'"; $this->write_undo_file($undo); } //turn maintenance mode on $this->set_maintenance_mode('on'); if ($tables) { $this->add_result("Pushing database tables from {$db_source['label']} to {$db_dest['label']}: {$tables}"); } else { $this->add_result("Pushing whole database", 1); } $this->add_result("Database source: {$db_source['label']} ({$db_source['name']}) on {$this->source_params['domain']}", 2); $this->add_result("Database dest: {$db_dest['label']} ({$db_dest['name']}) on {$this->dest_params['domain']}", 2); //run the command $result = $this->my_exec($command); //fix multisite domains if required if ($this->fix_domains) { $this->fix_multisite_domains(); } //turn maintenance mode off $this->set_maintenance_mode('off'); return $result; }
/** * Run the push. * * This is where all options for SitePushCore are set, and the relevant pushes are run. * * @param SitePushCore $my_push * @param array $push_options options for this push from $_REQUEST * @return bool TRUE if push completed without errors, FALSE otherwise */ public function do_the_push($my_push, $push_options) { //if we are going to do a push, check that we were referred from options page as expected check_admin_referer('sitepush-dopush', 'sitepush-nonce'); //final check everything is OK $this->final_check($my_push); if (SitePushErrors::count_errors('all-errors')) { return FALSE; } $start_micro_time = function_exists('microtime') ? microtime(TRUE) : 0; $start_time = time(); $my_push->add_result("Push started at " . date('r'), 1); //track if we have actually tried to push anything $done_push = FALSE; $my_push->sites_conf_path = $this->options->sites_conf; $my_push->dbs_conf_path = $this->options->dbs_conf; $my_push->source = $push_options['source']; $my_push->dest = $push_options['dest']; $my_push->dry_run = $push_options['dry_run'] ? TRUE : FALSE; $my_push->do_backup = $push_options['do_backup'] ? TRUE : FALSE; $my_push->source_backup_path = $this->options->backup_path; $my_push->dest_backup_path = $this->options->backup_path; $my_push->echo_output = TRUE; //initialise some parameters $push_files = FALSE; $results = array(); //should be empty at end if everything worked as expected $db_types = array(); $this->do_action('push_begin', $my_push, $push_options); /* -------------------------------------------------------------- */ /* !Push WordPress Files */ /* -------------------------------------------------------------- */ if ($push_options['push_uploads']) { $push_files = TRUE; $my_push->push_uploads = TRUE; } if ($push_options['push_themes']) { $push_files = TRUE; $my_push->push_themes = TRUE; } if ($push_options['push_theme'] && !$push_options['push_themes']) { //pushes current (child) theme $push_files = TRUE; $my_push->theme = _deprecated_get_theme_stylesheet(); } if ($push_options['push_plugins']) { $push_files = TRUE; $my_push->push_plugins = TRUE; } if ($push_options['push_mu_plugins']) { $push_files = TRUE; $my_push->push_mu_plugins = TRUE; } if ($push_options['push_wp_core']) { $push_files = TRUE; $my_push->push_wp_files = TRUE; } //do the push if ($push_files) { $this->do_action('pre_push_files', $my_push, $push_options); $results[] = $my_push->push_files(); $this->do_action('post_push_files', $my_push, $push_options); $done_push = TRUE; } /* -------------------------------------------------------------- */ /* !Push WordPress Database */ /* -------------------------------------------------------------- */ $db_push = FALSE; if ($push_options['db_all_tables']) { $db_types[] = 'all_tables'; $db_push = TRUE; } else { //we only check other options if we're not pushing whole DB if ($push_options['db_post_content']) { $db_types[] = 'content'; } if ($push_options['db_comments']) { $db_types[] = 'comments'; } if ($push_options['db_users']) { $db_types[] = 'users'; } if ($push_options['db_options']) { $db_types[] = 'options'; } if ($push_options['db_multisite_tables']) { $db_types[] = 'multisite'; } if ($push_options['db_custom_table_groups']) { $db_types[] = $push_options['db_custom_table_groups']; } if ($db_types) { $db_push = TRUE; } } $restore_options = FALSE; if ($db_push) { //save various options which we don't want overwritten if we are doing a pull $restore_options = $this->options->get_current_site() == $push_options['dest']; if ($restore_options) { $current_options = get_option('sitepush_options'); $current_user_options = $this->get_all_user_options(); $current_active_plugins = get_option('active_plugins'); //if we don't delete the options before DB push then WP won't restore options properly if //option wasn't present in DB we are pulling from delete_option('sitepush_options'); $this->delete_all_user_options(); } //push DB $this->do_action('pre_push_db', $my_push, $push_options); $results[] = $my_push->push_db($db_types); $this->do_action('post_push_db', $my_push, $push_options); $done_push = TRUE; } /* -------------------------------------------------------------- */ /* !Clear Cache */ /* -------------------------------------------------------------- */ if ($push_options['clear_cache'] && $this->options->cache_key) { $this->do_action('pre_clear_cache', $my_push, $push_options); $my_push->clear_cache(); $this->do_action('post_clear_cache', $my_push, $push_options); } elseif ($push_options['clear_cache'] && !$this->options->cache_key) { SitePushErrors::add_error("Could not clear the destination cache because the cache secret key is not set.", 'warning'); } /* -------------------------------------------------------------- */ /* !Other things to do */ /* -------------------------------------------------------------- */ //normally result should be empty - results to display are captured in class and output separately //if anything is output here it probably means something went wrong //clean the array of empty elements $cleaned_results = array(); foreach ($results as $result) { if (trim($result)) { $cleaned_results[] = $result; } } //save current site & user options back to DB so options on site we are pulling from won't overwrite if ($restore_options) { $this->options->update($current_options); $this->save_all_user_options($current_user_options); //deactivating sitepush ensures that when we update option cached value isn't used //we reactivate again after this if clause just to make sure it's active deactivate_plugins(SITEPUSH_BASENAME); update_option('active_plugins', $current_active_plugins); } //make sure sitepush is still activated and save our options to DB so if we have pulled DB from elsewhere we don't overwrite sitepush options activate_plugin(SITEPUSH_BASENAME); $my_push->add_result("Push completed at " . date('r'), 1); $duration = time() - $start_time; if ($duration >= 3600) { $time_took = gmdate('H:i:s', $duration); } elseif ($duration >= 60) { $time_took = gmdate('i:s', $duration); } elseif ($duration < 10 && $start_micro_time) { $time_took = microtime(TRUE) - $start_micro_time . " seconds"; } else { $time_took = "{$duration} seconds"; } $my_push->add_result("Push took {$time_took}", 1); $this->do_action('push_complete', $my_push, $push_options); return SitePushErrors::is_error() ? FALSE : $done_push; }
public function display_screen() { //check that the user has the required capability if (!$this->plugin->can_use()) { wp_die(__('You do not have sufficient permissions to access this page.')); } ?> <div class="wrap"> <h2>SitePush</h2> <?php //initialise options from form data $push_options['db_all_tables'] = SitePushPlugin::get_query_var('sitepush_push_db_all_tables') ? TRUE : FALSE; $push_options['db_post_content'] = SitePushPlugin::get_query_var('sitepush_push_db_post_content') ? TRUE : FALSE; $push_options['db_comments'] = SitePushPlugin::get_query_var('sitepush_push_db_comments') ? TRUE : FALSE; $push_options['db_users'] = SitePushPlugin::get_query_var('sitepush_push_db_users') ? TRUE : FALSE; $push_options['db_options'] = SitePushPlugin::get_query_var('sitepush_push_db_options') ? TRUE : FALSE; $push_options['push_uploads'] = SitePushPlugin::get_query_var('sitepush_push_uploads') ? TRUE : FALSE; $push_options['push_theme'] = SitePushPlugin::get_query_var('sitepush_push_theme') ? TRUE : FALSE; $push_options['push_themes'] = SitePushPlugin::get_query_var('sitepush_push_themes') ? TRUE : FALSE; $push_options['push_plugins'] = SitePushPlugin::get_query_var('sitepush_push_plugins') ? TRUE : FALSE; $push_options['push_mu_plugins'] = SitePushPlugin::get_query_var('sitepush_push_mu_plugins') ? TRUE : FALSE; $push_options['push_wp_core'] = SitePushPlugin::get_query_var('sitepush_push_wp_core') ? TRUE : FALSE; $push_options['clear_cache'] = SitePushPlugin::get_query_var('clear_cache') ? TRUE : FALSE; $push_options['dry_run'] = SitePushPlugin::get_query_var('sitepush_dry_run') ? TRUE : FALSE; $push_options['do_backup'] = SitePushPlugin::get_query_var('sitepush_push_backup') ? TRUE : FALSE; $db_custom_table_groups = SitePushPlugin::get_query_var('sitepush_db_custom_table_groups'); if ($db_custom_table_groups) { foreach ($db_custom_table_groups as $key => $group) { $push_options['db_custom_table_groups'][] = $key; } } else { $push_options['db_custom_table_groups'] = array(); } $push_options['source'] = SitePushPlugin::get_query_var('sitepush_source') ? SitePushPlugin::get_query_var('sitepush_source') : ''; $push_options['dest'] = SitePushPlugin::get_query_var('sitepush_dest') ? SitePushPlugin::get_query_var('sitepush_dest') : ''; //multisite specific options $push_options['db_multisite_tables'] = SitePushPlugin::get_query_var('sitepush_push_db_multisite_tables') ? TRUE : FALSE; $user_options = get_user_option('sitepush_options'); $this->user_last_source = empty($user_options['last_source']) ? '' : $user_options['last_source']; $this->user_last_dest = empty($user_options['last_dest']) ? '' : $user_options['last_dest']; SitePushErrors::errors('all', 'sitepush'); if ($push_options['dest']) { //output directly to screen... doesn't work on all browsers. ob_implicit_flush(); //save source/dest to user options $user_options = get_user_option('sitepush_options'); $user_options['last_source'] = $push_options['source']; $user_options['last_dest'] = $push_options['dest']; update_user_option(get_current_user_id(), 'sitepush_options', $user_options); // do the push! if ($this->plugin->can_admin() && $this->options->debug_output_level) { $hide_html = ''; } else { $hide_html = ' style="display: none;"'; echo "<div id='running'></div>"; } echo "<script>\n\t\t\t\tvar scrollIntervalID = window.setInterval(function(){\n\t\t\t\t\tjQuery('#sitepush-results').scrollTop( jQuery('#sitepush-results').prop('scrollHeight') );\n\t\t\t\t}, 100);\n\t\t\t</script>"; echo "<h3{$hide_html} class='sitepush-results'>Push results</h3>"; echo "<pre id='sitepush-results' class='sitepush-results' {$hide_html}>"; //Do the push! $obj_sitepushcore = new SitePushCore($push_options['source'], $push_options['dest']); if (!SitePushErrors::count_errors('all-errors')) { $push_result = $this->plugin->do_the_push($obj_sitepushcore, $push_options); } else { $push_result = FALSE; } echo "</pre>"; echo "<script>\n\t\t\t\tjQuery('#running').hide();\n\t\t\t\tif( ! jQuery('#sitepush-results').text() ) jQuery('.sitepush-results').hide();\n\t\t\t\tclearInterval( scrollIntervalID );\n\t\t\t\tjQuery('#sitepush-results').scrollTop( jQuery('#sitepush-results').prop('scrollHeight') );\n\t\t\t</script>"; if ($push_result) { if ($push_options['dry_run']) { SitePushErrors::add_error("Dry run complete. Nothing was actually pushed, but you can see what would have been done from the output above.", 'notice'); } elseif (SitePushErrors::count_errors('warning')) { SitePushErrors::add_error("Push complete (with warnings).", 'notice'); } else { SitePushErrors::add_error("Push complete.", 'notice'); } //bit of a hack... do one page load for destination site to make sure SitePush has activated plugins etc //before any user accesses the site wp_remote_get($obj_sitepushcore->dest_params['domain']); } else { if (!SitePushErrors::is_error()) { SitePushErrors::add_error("Nothing selected to push"); } } SitePushErrors::errors(); } //set up what menu options are selected by default if (!empty($_REQUEST['sitepush-nonce'])) { //already done a push, so redo what we had before $default_source = empty($_REQUEST['sitepush_source']) ? '' : $_REQUEST['sitepush_source']; $default_dest = empty($_REQUEST['sitepush_dest']) ? '' : $_REQUEST['sitepush_dest']; } if (empty($default_source)) { $default_source = $this->user_last_source ? $this->user_last_source : $this->options->get_current_site(); } if (empty($default_dest)) { $default_dest = $this->user_last_dest ? $this->user_last_dest : ''; } ?> <form method="post" action=""> <?php wp_nonce_field('sitepush-dopush', 'sitepush-nonce'); ?> <table class="form-table"> <?php if (SITEPUSH_SHOW_MULTISITE) { ?> <tr> <th scope="row"><label for="sitepush_source">Site</label></th> <td> <?php bloginfo('name'); ?> </td> </tr> <?php } ?> <tr> <th scope="row"><label for="sitepush_source">Source</label></th> <td> <select name="sitepush_source" id="sitepush_source" class="site-selector"> <?php foreach ($this->plugin->get_sites('source') as $site) { echo "<option value='{$site}'"; if ($default_source == $site) { echo " selected='selected'"; } echo ">{$this->options->sites[$site]['label']}</option>"; } ?> </select> </td> </tr> <tr> <th scope="row"><label for="sitepush_dest">Destination</label></th> <td> <select name="sitepush_dest" id="sitepush_dest" class="site-selector"> <?php foreach ($this->plugin->get_sites('destination') as $site) { $use_cache = $this->options->sites[$site]['use_cache'] ? 'yes' : 'no'; echo "<option value='{$site}' data-cache='{$use_cache}'"; if ($default_dest == $site) { echo " selected='selected'"; } echo ">{$this->options->sites[$site]['label']}</option>"; } ?> </select> <span id='sitepush_dest-warning'><span> </td> </tr> <?php $ms_message = SITEPUSH_SHOW_MULTISITE ? "<br /><i>current site only</i>" : ''; ?> <tr> <th scope="row">Database content<?php echo $ms_message; ?> </th> <td> <?php if (!SITEPUSH_SHOW_MULTISITE) { echo $this->option_html('sitepush_push_db_all_tables', 'Entire database <i>(this will overwrite all content and settings)</i>', 'admin_only'); } ?> <?php echo $this->option_html('sitepush_push_db_post_content', 'All post content <i>(pages, posts, media, links, custom post types, post meta, categories, tags & custom taxonomies)</i>', 'user'); ?> <?php if ($this->plugin->can_admin() || !$this->options->non_admin_exclude_comments) { echo $this->option_html('sitepush_push_db_comments', 'Comments', 'user'); } ?> <?php if (!SITEPUSH_SHOW_MULTISITE) { echo $this->option_html('sitepush_push_db_users', 'Users & user meta', 'admin_only'); } ?> <?php if ($this->plugin->can_admin() || !$this->options->non_admin_exclude_options) { echo $this->option_html('sitepush_push_db_options', 'WordPress options', 'user'); } ?> <?php foreach ($this->options->db_custom_table_groups_array as $key => $table_group) { //if label is preceded by $$$ then field only shows to admins if (strpos($table_group['label'], '$$$') === 0) { $admin_only = TRUE; $table_group['label'] = substr($table_group['label'], 3); } else { $admin_only = FALSE; } echo $this->option_html(array('sitepush_db_custom_table_groups', $key), $table_group['label'], $admin_only); } ?> </td> </tr> <?php $files_output = ''; if (!SITEPUSH_SHOW_MULTISITE) { $files_output .= $this->option_html('sitepush_push_theme', 'Current theme <i>(' . _deprecated_get_current_theme() . ')</i>', 'admin_only'); } if (!SITEPUSH_SHOW_MULTISITE) { $files_output .= $this->option_html('sitepush_push_themes', 'All themes', 'admin_only'); } if (!SITEPUSH_SHOW_MULTISITE) { $files_output .= $this->option_html('sitepush_push_plugins', 'WordPress plugins', 'admin_only'); } if (!SITEPUSH_SHOW_MULTISITE && file_exists($this->options->current_site_conf['web_path'] . $this->options->current_site_conf['wpmu_plugin_dir'])) { $files_output .= $this->option_html('sitepush_push_mu_plugins', 'WordPress must-use plugins', 'admin_only'); } if ('ERROR' != $this->options->current_site_conf['wp_uploads_dir']) { $files_output .= $this->option_html('sitepush_push_uploads', 'WordPress media uploads', 'user'); } elseif ($this->plugin->can_admin()) { $files_output .= "Uploads directory could not be determined, so uploaded media files cannot be pushed.<br />"; } if ($files_output) { echo "<tr><th scope='row'>Files{$ms_message}</th><td>{$files_output}</td></tr>"; } ?> <?php if (SITEPUSH_SHOW_MULTISITE && $this->plugin->can_admin()) { ?> <tr> <th scope="row">Multisite database content<br /><i>affects all sites</i></th> <td> <?php echo $this->option_html('sitepush_push_db_users', 'Users & user meta', 'admin_only'); ?> <?php echo $this->option_html('sitepush_push_db_multisite_tables', 'Multisite tables <i>(blogs, blog_versions, registration_log, signups, site, sitemeta, sitecategories)</i>', 'admin_only'); ?> <?php echo $this->option_html('sitepush_push_db_all_tables', 'Entire database for all sites <i>(Caution! This will overwrite all content and settings for all sites in this network installation)</i>', 'admin_only'); ?> </td> </tr> <tr> <th scope="row">Multisite files<br /><i>affects all sites</i></th> <td> <?php echo $this->option_html('sitepush_push_theme', 'Current theme <i>(' . _deprecated_get_current_theme() . ')</i>', 'admin_only'); ?> <?php echo $this->option_html('sitepush_push_themes', 'All themes', 'admin_only'); ?> <?php echo $this->option_html('sitepush_push_plugins', 'WordPress plugins', 'admin_only'); ?> <?php if (file_exists($this->options->current_site_conf['web_path'] . $this->options->current_site_conf['wpmu_plugin_dir'])) { echo $this->option_html('sitepush_push_mu_plugins', 'WordPress must-use plugins', 'admin_only'); } ?> </td> </tr> <?php } ?> <?php $output = ''; if (!empty($this->options->cache_key) && $this->options->use_cache) { $output .= $this->option_html('clear_cache', 'Clear cache on destination', 'user', 'checked'); } if ($this->options->backup_path) { $output .= $this->option_html('sitepush_push_backup', 'Backup push <i>(note - restoring from a backup is currently a manual process and ideally requires command line access)</i>', 'user', 'checked'); } if ($this->options->debug_output_level >= 3) { $output .= $this->option_html('sitepush_dry_run', 'Dry run <i>(show what actions would be performed by push, but don\'t actually do anything)</i>', 'admin_only'); } /* No undo till we get it working properly! <br /><label title="undo"><input type="radio" name="push_type" value="undo"<?php echo $push_type=='undo'?' checked="checked"':'';?> /> Undo the last push (<?php echo date( "D j F, Y \a\t H:i:s e O T",$obj_sitepushcore->get_last_undo_time() );?>)</label> */ if ($output) { echo "<tr valign='top'><th scope='row'>Push options</th><td>{$output}</td></tr>"; } ?> <?php if (!$this->plugin->can_admin()) { ?> <tr> <th scope="row"> </th> <td> <br /><span class="description">To push plugins, themes or settings, please ask an administrator.</span> </td> </tr> <?php } ?> </table> <p class="submit"> <input type="submit" class="button-primary" value="Push Content" id="push-button" /> </p> </form> </div> <?php }