From e15dcb72d781cf65fc729d093da3d5c00354f419 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Fri, 1 Dec 2023 13:40:38 +0500 Subject: [PATCH 1/2] New. Deactivator class implemented. --- inc/spbc-admin.php | 4 +- lib/CleantalkSP/SpbctWP/Deactivator.php | 386 ++++++++++++++++++ .../SpbctWP/Scanner/CureLog/CureLog.php | 3 + security-malware-firewall.php | 190 ++------- 4 files changed, 424 insertions(+), 159 deletions(-) create mode 100644 lib/CleantalkSP/SpbctWP/Deactivator.php diff --git a/inc/spbc-admin.php b/inc/spbc-admin.php index 4711f1228..e6631ac5e 100644 --- a/inc/spbc-admin.php +++ b/inc/spbc-admin.php @@ -27,7 +27,9 @@ function spbc_admin_init() spbc_admin_log_action(); } - spbc_set_malware_scan_warns(); + if ( is_main_site() ) { + spbc_set_malware_scan_warns(); + } // Admin bar $spbc->admin_bar_enabled = $spbc->settings['admin_bar__show'] && current_user_can('activate_plugins'); diff --git a/lib/CleantalkSP/SpbctWP/Deactivator.php b/lib/CleantalkSP/SpbctWP/Deactivator.php new file mode 100644 index 000000000..ad188bd6c --- /dev/null +++ b/lib/CleantalkSP/SpbctWP/Deactivator.php @@ -0,0 +1,386 @@ + array( + 'on_simple' => array( + 'mu_uninstall', + 'unregister_plugin_settings_page', + 'remove_admin_bar', + 'reset_admin_cookies', + ), + 'on_complete' => array( + 'mu_uninstall', + 'unregister_plugin_settings_page', + 'remove_admin_bar', + 'reset_admin_cookies', + 'reset_common_cookies', + 'delete_common_tables', + // + 'delete_blog_tables', + 'delete_blog_options', + 'delete_blog_meta', + // + 'delete_backups', + 'delete_fw_dir', + 'delete_frontend_meta' + ) + ), + 'network_wide' => array( + 'on_simple' => array( + 'mu_uninstall', + 'unregister_plugin_settings_page', + 'remove_admin_bar', + 'reset_admin_cookies', + ), + 'on_complete' => array( + 'mu_uninstall', + 'unregister_plugin_settings_page', + 'reset_admin_cookies', + 'reset_common_cookies', + 'remove_admin_bar', + // + 'delete_network_wide_options', + 'delete_each_blog_data', + 'delete_backups' + ) + ), + 'network_main_site' => array( + 'on_simple' => array( + 'reset_admin_cookies', + 'remove_admin_bar', + ), + 'on_complete' => array( + 'reset_admin_cookies', + 'delete_blog_tables', + 'delete_blog_options', + 'delete_blog_meta', + 'delete_fw_dir', + 'delete_frontend_meta' + ) + ), + ); + + /** + * @var + */ + private static $initial_blog; + + /** + * Perform all required deactivation logic depending on config provided. + * @param array $call_instance_config + * @return void + */ + private static function runActions($call_instance_config) + { + global $wpdb; + $deactivation_type = self::$is_complete_deactivation ? $call_instance_config['on_complete'] : $call_instance_config['on_simple']; + foreach ($deactivation_type as $action) { + switch ($action) { + case 'mu_uninstall': + self::muPluginUninstall(); + break; + case 'delete_blog_tables': + self::deleteBlogTables(); + break; + case 'delete_common_tables': + self::deleteCommonTables(); + break; + case 'delete_blog_options': + self::deleteBlogOptions(); + break; + case 'delete_backups': + \CleantalkSP\SpbctWP\Helpers\Data::removeDirectoryRecursively(SPBC_PLUGIN_DIR . 'backups'); + break; + case 'delete_fw_dir': + self::deleteSecFWUpdateFolder(); + break; + case 'delete_frontend_meta': + self::deleteFrontendMeta(); + break; + case 'delete_each_blog_data': + $blogs = array_keys($wpdb->get_results('SELECT blog_id FROM ' . $wpdb->blogs, OBJECT_K)); + + // Deleting data from each blog + foreach ($blogs as $target_blog) { + switch_to_blog($target_blog); + self::runActions(self::$deactivator_configs['single_site']); + self::logThis('actions for blog ' . $target_blog . ' ok'); + } + switch_to_blog(self::$initial_blog); + break; + case 'delete_network_wide_options': + delete_site_option(SPBC_NETWORK_SETTINGS); + delete_site_option(SPBC_PLUGINS); + delete_site_option(SPBC_THEMES); + break; + case 'reset_common_cookies': + Cookie::set('spbc_cookies_test', '0', time() - 30, '/'); + Cookie::set('spbc_log_id', '0', time() - 30, '/'); + Cookie::set('spbc_secfw_ip_wl', '0', time() - 30, '/'); + Cookie::set('spbc_timer', '0', time() - 30, '/'); + break; + case 'reset_admin_cookies': + Cookie::set('spbc_is_logged_in', '0', time() - 30, '/'); + Cookie::set('spbc_admin_logged_in', '0', time() - 30, '/'); + break; + case 'unregister_plugin_settings_page': + unregister_setting(SPBC_SETTINGS, SPBC_SETTINGS); + break; + case 'remove_admin_bar': + if ( + /** @psalm-suppress UndefinedFunction */ + !has_action('admin_bar_menu', 'spbc_admin__admin_bar__add_structure') + ) { + remove_action('admin_bar_menu', 'spbc_admin__admin_bar__add_structure', 999); + } + break; + } + self::logThis($action . ' ok'); + } + } + + /** + * Run plugin deactivation logic. + * @param bool $network Is network wide + * @param bool $do_log_on_errors Write php error log on deactivation process errors + * @return bool Result. True if all actions is completed and checked, false otherwise + * @psalm-suppress PossiblyUnusedReturnValue + */ + public static function deactivation($network, $do_log_on_errors = false) + { + global $spbc; + self::$is_complete_deactivation = !empty($spbc->settings['misc__complete_deactivation']); + + if ( + /** @psalm-suppress UndefinedFunction */ + !has_action('admin_bar_menu', 'spbc_admin__admin_bar__add_structure') + ) { + remove_action('admin_bar_menu', 'spbc_admin__admin_bar__add_structure', 999); + } + + self::$initial_blog = get_current_blog_id(); + + // Deactivation on standalone blog + if ( !is_multisite() ) { + self::runActions( + self::$deactivator_configs['single_site'] + ); + self::checkDeactivationResult('single_site'); + + //Network wide deactivation + } elseif ( $network ) { + //switch network processing to on + update_option('spbc_deactivation_in_process', true, false); + + self::runActions( + self::$deactivator_configs['network_wide'] + ); + //switch network processing to off + delete_option('spbc_deactivation_in_process'); + self::checkDeactivationResult('network_wide'); + + // Deactivation for one blog in the network + } else { + self::runActions( + self::$deactivator_configs['network_main_site'] + ); + self::checkDeactivationResult('network_main_site'); + } + + if ( $do_log_on_errors && !self::$deactivation_result ) { + error_log("Security by CleanTalk deactivation log: \r\n" . var_export(self::getLog(), true)); + } + + return self::$deactivation_result; + } + + /** + * Deletes frontend scan meta results + * @return void + */ + private static function deleteFrontendMeta() + { + global $wpdb; + // Deleting scan links results + $wpdb->query( + $wpdb->prepare( + "DELETE FROM {$wpdb->postmeta} + WHERE meta_key = %s OR meta_key = %s", + '_spbc_links_checked', + 'spbc_links_checked' + ) + ); + + // Deleting scan frontend results + $wpdb->query( + $wpdb->prepare( + "DELETE FROM {$wpdb->postmeta} + WHERE meta_key = %s OR meta_key = %s OR meta_key = %s OR meta_key = %s", + '_spbc_frontend__last_checked', + '_spbc_frontend__approved', + 'spbc_frontend__last_checked', + 'spbc_frontend__approved' + ) + ); + } + + /** + * Deletes current blog individual tables. + * @return void + */ + public static function deleteBlogTables() //ok + { + global $wpdb; + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_auth_logs'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_monitoring_users'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall__personal_ips'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall__personal_countries'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall_logs'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_traffic_control_logs'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_traffic_control_logs'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_bfp_blocked'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_sessions'); + } + + /** + * Deletes common tables such a spbc_scan_results + * @return void + */ + public static function deleteCommonTables() //ok + { + global $wpdb; + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_results'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_firewall_data'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_links_logs'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_signatures'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_frontend'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_backups'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_backuped_files'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_results_log'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_important_files'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_important_file_snapshots'); + $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_cure_log'); + } + + /** + * Deletes must-used instance of the plugin. + * @return bool + */ + public static function muPluginUninstall() + { + if ( file_exists(WPMU_PLUGIN_DIR . '/0security-malware-firewall-mu.php') ) { + return unlink(WPMU_PLUGIN_DIR . '/0security-malware-firewall-mu.php'); + } + + return false; + } + + /** + * Deletes current individual blog options. + */ + public static function deleteBlogOptions() //APBCT + { + global $wpdb; + // Deleting all data from wp_options + $wpdb->query( + 'DELETE FROM ' . $wpdb->options + . ' WHERE' + . ' option_name LIKE "spbc_%" AND' + . ' option_name <> "spbc_deactivation_in_process"' + ); + } + + /** + * Deletes update folders + * @return void + */ + private static function deleteSecFWUpdateFolder() //APBCT + { + $current_blog_id = get_current_blog_id(); + $wp_upload_dir = wp_upload_dir(); + $update_folder = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fw_files_for_blog_' . $current_blog_id . DIRECTORY_SEPARATOR; + \CleantalkSP\SpbctWP\Helpers\Data::removeDirectoryRecursively($update_folder); + } + + /** + * Do collect log for the instance. Static! + * @param $message + * @return void + */ + private static function logThis($message) + { + if ( !is_string($message) ) { + return; + } + self::$log[] = current_datetime()->format('Y-m-d H:i:s') . ' ' . $message; + } + + /** + * Return static logs of Deactivation process. + * @return string + */ + public static function getLog() + { + if ( !empty(self::$log) ) { + return implode("\r\n", self::$log); + } else { + return 'No logs collected yet.'; + } + } + + /** + * Check if deactivation is success and all required data has been removed. + * @param string $type Type of the WordPress instance ('single_site' or other can be found in the static::$deactivator_configs. + * @return void + */ + public static function checkDeactivationResult($type) + { + global $wpdb; + if ( !is_string($type) || !in_array($type, self::$deactivator_configs) ) { + self::logThis('checkDeactivationResult: no such config found'); + return; + } + + //for single site and complete deactivation + if ( $type === 'single_site' && self::$is_complete_deactivation ) { + $tables_query = 'SHOW TABLES LIKE "%spbc%"'; + $tables_query_result = $wpdb->get_results($tables_query); + $options_query = 'SELECT * FROM wp_options WHERE option_name LIKE "%spbc%"'; + $options_query_result = $wpdb->get_results($options_query); + $meta_query = 'SELECT * FROM wp_postmeta WHERE meta_key LIKE "%spbc%"'; + $meta_query_result = $wpdb->get_results($meta_query); + if ( count($tables_query_result) || count($options_query_result) || count($meta_query_result) ) { + self::logThis('checkDeactivationResult: FAILED'); + self::logThis('tables_query_result' . implode(',', $tables_query_result)); + self::logThis('options_query_result' . implode(',', $options_query_result)); + self::logThis('meta_query_result' . implode(',', $meta_query_result)); + self::$deactivation_result = false; + return; + } + } + //todo: for the further actions or unit tests this need to be refactored or updated + self::logThis('checkDeactivationResult: ' . 'OK'); + } +} diff --git a/lib/CleantalkSP/SpbctWP/Scanner/CureLog/CureLog.php b/lib/CleantalkSP/SpbctWP/Scanner/CureLog/CureLog.php index 7976b4c91..93ad10140 100644 --- a/lib/CleantalkSP/SpbctWP/Scanner/CureLog/CureLog.php +++ b/lib/CleantalkSP/SpbctWP/Scanner/CureLog/CureLog.php @@ -28,7 +28,10 @@ public function getCountData() /** * Clear cure log table + * @deprecated Since 2.125 - no need to run CuerLog clear. On deactivation + * the table will be deleted instead of clearing. * @return void + * @psalm-suppress PossiblyUnusedMethod */ public function clearLogData() { diff --git a/security-malware-firewall.php b/security-malware-firewall.php index 92a35fdd0..d0ea4b783 100644 --- a/security-malware-firewall.php +++ b/security-malware-firewall.php @@ -22,7 +22,6 @@ use CleantalkSP\SpbctWP\RemoteCalls as SpbcRemoteCalls; use CleantalkSP\SpbctWP\RenameLoginPage; use CleantalkSP\SpbctWP\Sanitize; -use CleantalkSP\SpbctWP\Scanner\CureLog\CureLog; use CleantalkSP\SpbctWP\Scanner\Stages\SignatureAnalysis\SignatureAnalysisFacade; use CleantalkSP\SpbctWP\State; use CleantalkSP\SpbctWP\Transaction; @@ -268,6 +267,7 @@ function spbc_change_author_name($link, $_author_id, $_author_nicename) // Logged hooks register_activation_hook(__FILE__, 'spbc_activation'); register_deactivation_hook(__FILE__, 'spbc_deactivation'); +register_uninstall_hook(__FILE__, 'spbc_uninstall'); // Hook for newly added blog if ( version_compare(get_bloginfo('version'), '5.1.0', '<') ) { @@ -717,164 +717,38 @@ function spbc_activation__new_blog_5_1($new_site) */ function spbc_deactivation($network) { - global $spbc, $wpdb; - - Cookie::set('spbc_is_logged_in', '0', time() - 30, '/'); - Cookie::set('spbc_admin_logged_in', '0', time() - 30, '/'); - - unregister_setting(SPBC_SETTINGS, SPBC_SETTINGS); - - // Deactivation for network - if ( is_multisite() && $network ) { - update_option('spbc_deactivation_in_process', true, false); - - spbc_mu_plugin__uninstall(); - - if ( ! empty($spbc->settings['misc__complete_deactivation']) ) { - $initial_blog = get_current_blog_id(); - - delete_site_option(SPBC_NETWORK_SETTINGS); - - $blogs = array_keys($wpdb->get_results('SELECT blog_id FROM ' . $wpdb->blogs, OBJECT_K)); - - // Deleting data from each blog - foreach ( $blogs as $blog ) { - switch_to_blog($blog); - spbc_deactivation__delete_blog_tables(); - - // Deleting all data from wp_options - $wpdb->query( - 'DELETE FROM ' . $wpdb->options - . ' WHERE' - . ' option_name LIKE "spbc_%" AND' - . ' option_name <> "spbc_deactivation_in_process"' - ); - - // Deleting scan links results - $wpdb->query( - $wpdb->prepare( - "DELETE FROM {$wpdb->postmeta} - WHERE meta_key = %s OR meta_key = %s", - '_spbc_links_checked', - 'spbc_links_checked' - ) - ); - // Deleting scan frontend results - $wpdb->query( - $wpdb->prepare( - "DELETE FROM {$wpdb->postmeta} - WHERE meta_key = %s OR meta_key = %s OR meta_key = %s OR meta_key = %s", - '_spbc_frontend__last_checked', - '_spbc_frontend__approved', - 'spbc_frontend__last_checked', - 'spbc_frontend__approved' - ) - ); - } - - switch_to_blog($initial_blog); - spbc_deactivation__delete_common_tables(); - - delete_option(SPBC_PLUGINS); - delete_option(SPBC_THEMES); - - // Delete backups - \CleantalkSP\SpbctWP\Helpers\Data::removeDirectoryRecursively(SPBC_PLUGIN_DIR . 'backups'); - - //Delete Cure log - $cure_log = new CureLog(); - $cure_log->clearLogData(); - } - - delete_option('spbc_deactivation_in_process'); - - // Deactivation for blog - } elseif ( is_multisite() ) { - spbc_deactivation__delete_blog_tables(); - - delete_option(SPBC_SETTINGS); - delete_option(SPBC_DATA); - delete_option(SPBC_CRON); - delete_option(SPBC_ERRORS); - - // Deactivation on standalone blog - } elseif ( ! is_multisite() ) { - spbc_mu_plugin__uninstall(); - - delete_option('spbc_deactivation_in_process'); - - if ( ! empty($spbc->settings['misc__complete_deactivation']) ) { - spbc_deactivation__delete_blog_tables(); - spbc_deactivation__delete_common_tables(); - - // Deleting all data from wp_options - $wpdb->query( - 'DELETE FROM ' . $wpdb->options - . ' WHERE' - . ' option_name LIKE "spbc_%" AND' - . ' option_name <> "spbc_deactivation_in_process"' - ); - - - // Deleting scan links results - $wpdb->query( - $wpdb->prepare( - "DELETE FROM {$wpdb->postmeta} - WHERE meta_key = %s OR meta_key = %s", - '_spbc_links_checked', - 'spbc_links_checked' - ) - ); - - // Deleting scan frontend results - $wpdb->query( - $wpdb->prepare( - "DELETE FROM {$wpdb->postmeta} - WHERE meta_key = %s OR meta_key = %s OR meta_key = %s OR meta_key = %s", - '_spbc_frontend__last_checked', - '_spbc_frontend__approved', - 'spbc_frontend__last_checked', - 'spbc_frontend__approved' - ) - ); - - // Delete backups - \CleantalkSP\SpbctWP\Helpers\Data::removeDirectoryRecursively(SPBC_PLUGIN_DIR . 'backups'); + \CleantalkSP\SpbctWP\Deactivator::deactivation($network); +} - //Delete Cure log - $cure_log = new CureLog(); - $cure_log->clearLogData(); - } - } +/** + * Run deactivation process (complete deactivation forced) for hook register_uninstall_hook. + * @param bool $network Is network wide command. + * @return void + */ +function spbc_uninstall($network) +{ + global $spbc; + $spbc->settings['misc__complete_deactivation'] = 1; + $spbc->save('settings'); + \CleantalkSP\SpbctWP\Deactivator::deactivation($network); } -function spbc_deactivation__delete_blog_tables() +/** + * @deprecated 2.125 use Deactivator::deleteBlogTables() + * @return void + */ +function spbc_deactivation__delete_blog_tables() //deprecated { - global $wpdb; - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_auth_logs'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_monitoring_users'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall__personal_ips'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall__personal_countries'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_firewall_logs'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_traffic_control_logs'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_traffic_control_logs'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_bfp_blocked'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'spbc_sessions'); + \CleantalkSP\SpbctWP\Deactivator::deleteBlogTables(); } -function spbc_deactivation__delete_common_tables() +/** + * @deprecated 2.125 use Deactivator::deleteCommonTables() + * @return void + */ +function spbc_deactivation__delete_common_tables() //deprecated { - global $wpdb; - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_results'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_firewall_data'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_links_logs'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_signatures'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_frontend'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_backups'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_backuped_files'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_scan_results_log'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_important_files'); - $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->base_prefix . 'spbc_important_file_snapshots'); + \CleantalkSP\SpbctWP\Deactivator::deleteCommonTables(); } // Misc functions to test the plugin. @@ -1236,14 +1110,14 @@ function spbc_mu_plugin__install() return @file_put_contents(WPMU_PLUGIN_DIR . '/0security-malware-firewall-mu.php', $file) ? true : false; } -// Uninstall MU-plugin +/** + * Uninstall MU-plugin + * @deprecated 2.125 Use Deactivator::muPluginUninstall + * @return bool + */ function spbc_mu_plugin__uninstall() { - if ( file_exists(WPMU_PLUGIN_DIR . '/0security-malware-firewall-mu.php') ) { - return unlink(WPMU_PLUGIN_DIR . '/0security-malware-firewall-mu.php'); - } - - return false; + return \CleantalkSP\SpbctWP\Deactivator::muPluginUninstall(); } function spbc_user_is_admin() From 1fb1f134e6603f2f14436a35e99ce223073ce0f4 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Mon, 4 Dec 2023 16:50:54 +0500 Subject: [PATCH 2/2] New. Activator class implemented. --- lib/CleantalkSP/SpbctWP/Activator.php | 192 ++++++++++++++++++++++++++ security-malware-firewall.php | 156 +-------------------- tests/Common/SpbcCronTest.php | 8 +- 3 files changed, 203 insertions(+), 153 deletions(-) create mode 100644 lib/CleantalkSP/SpbctWP/Activator.php diff --git a/lib/CleantalkSP/SpbctWP/Activator.php b/lib/CleantalkSP/SpbctWP/Activator.php new file mode 100644 index 000000000..899d18e69 --- /dev/null +++ b/lib/CleantalkSP/SpbctWP/Activator.php @@ -0,0 +1,192 @@ +getNotExistingTables() as $not_existing_table ) { + $db_tables_creator = new \CleantalkSP\SpbctWP\DB\TablesCreator(); + $db_tables_creator->createTable($not_existing_table); + } + + // Activation for network + if ( is_multisite() && $network ) { + // For all blogs + SpbcCron::addTask('scanner_update_signatures', 'spbc_scanner__signatures_update', 86400, time() + 100); + SpbcCron::addTask('send_php_logs', 'spbc_PHP_logs__send', 3600, time() + 300); + + if ( empty($spbc->errors['configuration']) ) { + $scanner_launch_data = spbc_get_custom_scanner_launch_data(true); + SpbcCron::addTask( + 'scanner__launch', + 'spbc_scanner__launch', + $scanner_launch_data['period'], + $scanner_launch_data['start_time'] + ); + } + + // MU-Plugin + if ( ! spbc_mu_plugin__install() ) { + spbc_log('Couldn\'t install Must-Use Plugin. This\'s not critical but it could help plugin to work faster.'); + } + + // For each blog + $initial_blog = get_current_blog_id(); + $blogs = array_keys($wpdb->get_results('SELECT blog_id FROM ' . $wpdb->blogs, OBJECT_K)); + foreach ( $blogs as $blog ) { + switch_to_blog($blog); + SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); + SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); + SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); + SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); + SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); + SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); + } + switch_to_blog($initial_blog); + + // Activation for blog + } elseif ( is_multisite() ) { + //Cron jobs + SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); + SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); + SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); + SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); + SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); + SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); + SpbcCron::addTask('security_log_clear', 'spbc_security_log_clear', 86400, time() + 43200); + SpbcCron::addTask('get_brief_data', 'spbc_set_brief_data', 86400, time() + 1800); + } elseif ( ! is_multisite() ) { + // Cron + SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); + SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); + SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); + SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); + SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); + SpbcCron::addTask('scanner_update_signatures', 'spbc_scanner__signatures_update', 86400, time() + 100); + SpbcCron::addTask('send_php_logs', 'spbc_PHP_logs__send', 3600, time() + 300); + SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); + SpbcCron::addTask('security_log_clear', 'spbc_security_log_clear', 86400, time() + 43200); + SpbcCron::addTask('get_brief_data', 'spbc_set_brief_data', 86400, time() + 1800); + + if ( empty($spbc->errors['configuration']) ) { + $scanner_launch_data = spbc_get_custom_scanner_launch_data(true); + SpbcCron::addTask( + 'scanner__launch', + 'spbc_scanner__launch', + $scanner_launch_data['period'], + $scanner_launch_data['start_time'] + ); + } + + // MU-Plugin + if ( ! spbc_mu_plugin__install() ) { + spbc_log('Couldn\'t install Must-Use Plugin. This\'s not critical but it could help plugin to work faster.'); + } + } + + // Redirect + if ( $redirect ) { + add_option('spbc_activation__redirect', $spbc->settings_link); + } + + if ( $spbc->api_key || ( ! is_main_site() && $spbc->network_settings['ms__work_mode'] != 2 ) ) { + return; + } + /** + * Filters a getting API key flag + * + * @param bool Set true if you want to get key automatically after activation the plugin + */ + if ( apply_filters('spbc_is_get_api_key', false) ) { + spbc_get_key_auto(true); + } + } + + /** + * Add action for new blog adding depending on current WP version + * @param string $wp_version Use this for example get_bloginfo('version') + * @return void + */ + public static function addActionForNetworkBlogLegacy($wp_version) + { + // Hook for newly added blog + if ( version_compare($wp_version, '5.1.0', '<') ) { + add_action('wpmu_new_blog', array('self', 'actionNewBlogLegacyMode'), 10, 6); + } else { + add_action('wp_insert_site', array('self', 'actionNewBlog'), 10, 1); + } + } + + /** + * @param $blog_id + * @param $_user_id + * @param $_domain + * @param $_path + * @param $_site_id + * @param $_meta + * @return void + * @throws \Exception + */ + private static function actionNewBlogLegacyMode($blog_id, $_user_id, $_domain, $_path, $_site_id, $_meta) + { + global $spbc; + + if ( spbc_is_plugin_active_for_network('security-malware-firewall/security-malware-firewall.php') ) { + $spbc = State::resetState(); + switch_to_blog($blog_id); + + spbc_activation(false, false); + + if ( ! $spbc->is_mainsite && ! $spbc->ms__work_mode == 1 ) { + spbc_set_api_key(); + } + + restore_current_blog(); + State::restoreState(); + } + } + + /** + * Wrapper for spbc_activation__new_blog() + * + * @param $new_site + * @psalm-suppress UnusedMethod - this method called via hook wp_insert_site/wpmu_new_blog + */ + private static function actionNewBlog($new_site) + { + self::actionNewBlogLegacyMode($new_site->blog_id, null, null, null, null, null); + } + + /** + * Function for redirect to settings + * + * @return void + */ + public static function redirectAfterActivation() + { + $settings_redirect = get_option('spbc_activation__redirect', false); + + if ( $settings_redirect && ! isset($_GET['activate-multi']) ) { + delete_option('spbc_activation__redirect'); + wp_redirect($settings_redirect); + } + } +} diff --git a/security-malware-firewall.php b/security-malware-firewall.php index d0ea4b783..304929d78 100644 --- a/security-malware-firewall.php +++ b/security-malware-firewall.php @@ -11,6 +11,7 @@ Domain Path: /i18n */ +use CleantalkSP\SpbctWP\Activator; use CleantalkSP\SpbctWP\DB; use CleantalkSP\SpbctWP\Firewall; use CleantalkSP\SpbctWP\Firewall\BFP; @@ -270,11 +271,7 @@ function spbc_change_author_name($link, $_author_id, $_author_nicename) register_uninstall_hook(__FILE__, 'spbc_uninstall'); // Hook for newly added blog -if ( version_compare(get_bloginfo('version'), '5.1.0', '<') ) { - add_action('wpmu_new_blog', 'spbc_activation__new_blog', 10, 6); -} else { - add_action('wp_insert_site', 'spbc_activation__new_blog_5_1', 10, 1); -} +Activator::addActionForNetworkBlogLegacy(get_bloginfo('version')); add_action('plugins_loaded', 'spbc_plugin_loaded', 1); // Main hook @@ -303,7 +300,7 @@ function spbc_change_author_name($link, $_author_id, $_author_nicename) include_once SPBC_PLUGIN_DIR . 'inc/spbc-admin.php'; include_once SPBC_PLUGIN_DIR . 'templates/spbc_settings_main.php'; // Templates for settings pgae - add_action('admin_init', 'spbc_activation__redirect', 1); // Redirect after activation + add_action('admin_init', array('CleantalkSP\SpbctWP\Activator', 'redirectAfterActivation'), 1); // Redirect after activation add_action('admin_init', 'spbc_admin_init', 1, 1); // Main admin hook add_action('admin_menu', 'spbc_admin_add_page'); // Admin pages add_action('network_admin_menu', 'spbc_admin_add_page'); // Network admin pages @@ -559,154 +556,9 @@ function spbc_firewall__check() */ function spbc_activation($network, $redirect = true) { - global $wpdb, $spbc; - - if ( current_action() !== 'wp_insert_site' ) { - delete_option('spbc_deactivation_in_process'); - } - - $tables_analyzer = new \CleantalkSP\SpbctWP\DB\TablesAnalyzer(); - foreach ( $tables_analyzer->getNotExistingTables() as $not_existing_table ) { - $db_tables_creator = new \CleantalkSP\SpbctWP\DB\TablesCreator(); - $db_tables_creator->createTable($not_existing_table); - } - - // Activation for network - if ( is_multisite() && $network ) { - // For all blogs - SpbcCron::addTask('scanner_update_signatures', 'spbc_scanner__signatures_update', 86400, time() + 100); - SpbcCron::addTask('send_php_logs', 'spbc_PHP_logs__send', 3600, time() + 300); - - if ( empty($spbc->errors['configuration']) ) { - $scanner_launch_data = spbc_get_custom_scanner_launch_data(true); - SpbcCron::addTask( - 'scanner__launch', - 'spbc_scanner__launch', - $scanner_launch_data['period'], - $scanner_launch_data['start_time'] - ); - } - - // MU-Plugin - if ( ! spbc_mu_plugin__install() ) { - spbc_log('Couldn\'t install Must-Use Plugin. This\'s not critical but it could help plugin to work faster.'); - } - - // For each blog - $initial_blog = get_current_blog_id(); - $blogs = array_keys($wpdb->get_results('SELECT blog_id FROM ' . $wpdb->blogs, OBJECT_K)); - foreach ( $blogs as $blog ) { - switch_to_blog($blog); - SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); - SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); - SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); - SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); - SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); - SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); - } - switch_to_blog($initial_blog); - - // Activation for blog - } elseif ( is_multisite() ) { - //Cron jobs - SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); - SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); - SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); - SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); - SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); - SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); - SpbcCron::addTask('security_log_clear', 'spbc_security_log_clear', 86400, time() + 43200); - SpbcCron::addTask('get_brief_data', 'spbc_set_brief_data', 86400, time() + 1800); - } elseif ( ! is_multisite() ) { - // Cron - SpbcCron::addTask('send_logs', 'spbc_send_logs', 3600, time() + 1800); - SpbcCron::addTask('send_report', 'spbc_send_daily_report', 86400, time() + 43200); - SpbcCron::addTask('firewall_update', 'spbc_security_firewall_update__init', 86400); - SpbcCron::addTask('send_firewall_logs', 'spbc_send_firewall_logs', 3600, time() + 1800); - SpbcCron::addTask('access_key_notices', 'spbc_access_key_notices', 3600, time() + 3500); - SpbcCron::addTask('scanner_update_signatures', 'spbc_scanner__signatures_update', 86400, time() + 100); - SpbcCron::addTask('send_php_logs', 'spbc_PHP_logs__send', 3600, time() + 300); - SpbcCron::addTask('service_get', 'spbct_perform_service_get', 86400, time() + 3500); - SpbcCron::addTask('security_log_clear', 'spbc_security_log_clear', 86400, time() + 43200); - SpbcCron::addTask('get_brief_data', 'spbc_set_brief_data', 86400, time() + 1800); - - if ( empty($spbc->errors['configuration']) ) { - $scanner_launch_data = spbc_get_custom_scanner_launch_data(true); - SpbcCron::addTask( - 'scanner__launch', - 'spbc_scanner__launch', - $scanner_launch_data['period'], - $scanner_launch_data['start_time'] - ); - } - - // MU-Plugin - if ( ! spbc_mu_plugin__install() ) { - spbc_log('Couldn\'t install Must-Use Plugin. This\'s not critical but it could help plugin to work faster.'); - } - } - - // Redirect - if ( $redirect ) { - add_option('spbc_activation__redirect', $spbc->settings_link); - } - - if ( $spbc->api_key || ( ! is_main_site() && $spbc->network_settings['ms__work_mode'] != 2 ) ) { - return; - } - /** - * Filters a getting API key flag - * - * @param bool Set true if you want to get key automatically after activation the plugin - */ - if ( apply_filters('spbc_is_get_api_key', false) ) { - spbc_get_key_auto(true); - } + Activator::activation($network, $redirect); } -/** - * Function for redirect to settings - * - * @return void - */ -function spbc_activation__redirect() -{ - $settings_rediect = get_option('spbc_activation__redirect', false); - - if ( $settings_rediect && ! isset($_GET['activate-multi']) ) { - delete_option('spbc_activation__redirect'); - wp_redirect($settings_rediect); - } -} - -function spbc_activation__new_blog($blog_id, $_user_id, $_domain, $_path, $_site_id, $_meta) -{ - global $spbc; - - if ( spbc_is_plugin_active_for_network('security-malware-firewall/security-malware-firewall.php') ) { - $spbc = State::resetState(); - switch_to_blog($blog_id); - - spbc_activation(false, false); - - if ( ! $spbc->is_mainsite && ! $spbc->ms__work_mode == 1 ) { - spbc_set_api_key(); - } - - restore_current_blog(); - State::restoreState(); - } -} - -/** - * Wrapper for spbc_activation__new_blog() - * - * @param $new_site - */ -function spbc_activation__new_blog_5_1($new_site) -{ - spbc_activation__new_blog($new_site->blog_id, null, null, null, null, null); -} /** * A code during plugin deactivation. diff --git a/tests/Common/SpbcCronTest.php b/tests/Common/SpbcCronTest.php index 55b03f67b..407df25c6 100644 --- a/tests/Common/SpbcCronTest.php +++ b/tests/Common/SpbcCronTest.php @@ -6,6 +6,10 @@ class SpbcCronTest extends TestCase { + private $removed_updater_tasks = array( + 'spbc_perform_scan_wrapper', + 'spbc_perform_scan_wrapper_act' + ); public function testCronTaskMethodsExist() { if ( !function_exists('php_uname') ) { @@ -68,7 +72,9 @@ function get_files_with_cron_call($dir, $recursive = true, $include_folders = fa $this->assertFalse(empty($matches), 'No cron tasks parsed!'); - foreach ($matches[1] as $call) { + $funcs_filtered = array_diff($matches[1], $this->removed_updater_tasks); + + foreach ($funcs_filtered as $call) { $this->assertTrue(function_exists($call), 'Cannot find function ' . $call); } }