set_showhide(); break; case 'save_settings': $information = $this->save_settings(); break; // case 'whitelist': // $information = $this->whitelist(); // break; case 'whitelist_release': $information = $this->whitelist_release(); break; case 'backup_db': $information = $this->backup_db(); break; case 'admin_user': $information = $this->admin_user(); break; case 'database_prefix': $information = $this->change_database_prefix(); break; // case 'api_key': // $information = $this->api_key(); // break; case 'reset_api_key': $information = $this->reset_api_key(); break; case 'malware_scan': $information = $this->malware_scan(); break; // case 'malware_get_scan_results': // $information = $this->malware_get_scan_results(); // break; case 'clear_all_logs': $information = $this->purge_logs(); break; case 'file_change': $information = $this->file_change(); break; case 'release_lockout': $information = $this->release_lockout(); break; case 'module_status': $information = $this->update_module_status(); break; case 'wordpress_salts': $information = $this->wordpress_salts(); break; case 'file_permissions': $information = $this->file_permissions(); break; case 'reload_backup_exclude': $information = $this->reload_backup_exclude(); break; case 'security_site': $information = $this->security_site(); break; case 'activate_network_brute_force': $information = $this->activate_network_brute_force(); break; } } MainWP_Helper::write( $information ); } function set_showhide() { $hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : ''; MainWP_Helper::update_option( 'mainwp_ithemes_hide_plugin', $hide ); $information['result'] = 'success'; return $information; } public function ithemes_init() { if ( get_option( 'mainwp_ithemes_ext_enabled' ) !== 'Y' ) { return; } if ( get_option( 'mainwp_ithemes_hide_plugin' ) === 'hide' ) { add_filter( 'all_plugins', array( $this, 'all_plugins' ) ); add_action( 'admin_menu', array( $this, 'remove_menu' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); } } public function admin_init() { remove_meta_box( 'itsec-dashboard-widget', 'dashboard', 'normal' ); } public function all_plugins( $plugins ) { foreach ( $plugins as $key => $value ) { $plugin_slug = basename( $key, '.php' ); if ( 'better-wp-security' === $plugin_slug || 'ithemes-security-pro' === $plugin_slug ) { unset( $plugins[ $key ] ); } } return $plugins; } public function remove_menu() { remove_menu_page( 'itsec' ); } function save_settings() { if ( ! class_exists( 'ITSEC_Lib' ) ) { require( ITSEC_Core::get_core_dir() . '/core/class-itsec-lib.php' ); } $_itsec_modules = array( 'global', 'away-mode', 'backup', 'hide-backend', 'ipcheck', 'ban-users', 'brute-force', 'file-change', '404-detection', 'network-brute-force', 'ssl', 'strong-passwords', 'system-tweaks', 'wordpress-tweaks', 'multisite-tweaks', //'salts', //'content-directory', ); $require_permalinks = false; $updated = false; $errors = array(); $nbf_settings = array(); $update_settings = maybe_unserialize( base64_decode( $_POST['settings'] ) ); foreach($update_settings as $module => $settings) { $do_not_save = false; if (in_array($module, $_itsec_modules)) { if ($module == 'wordpress-salts') { $settings['last_generated'] = ITSEC_Modules::get_setting( $module, 'last_generated' ); // not update } else if ($module == 'global') { $keep_olds = array( 'did_upgrade', 'log_info', 'show_new_dashboard_notice', 'show_security_check' , 'nginx_file' ); foreach($keep_olds as $key) { $settings[$key] = ITSEC_Modules::get_setting( $module, $key ); // not update } if (!isset($settings['log_location']) || empty($settings['log_location']) ) { $settings['log_location'] = ITSEC_Modules::get_setting( $module, 'log_location' ); } else { $result = $this->validate_directory('log_location', $settings['log_location']); if ($result !== true) { $errors[] = $result; $settings['log_location'] = ITSEC_Modules::get_setting( $module, 'log_location' ); // no change } } } else if ($module == 'backup') { if (!isset($settings['location']) || empty($settings['location']) ) { $settings['location'] = ITSEC_Modules::get_setting( $module, 'location' ); } else { $result = $this->validate_directory('location', $settings['location']); if ($result !== true) { $errors[] = $result; $settings['location'] = ITSEC_Modules::get_setting( $module, 'location' ); // no change } } if (!isset($settings['exclude']) ) { $settings['exclude'] = ITSEC_Modules::get_setting( $module, 'exclude' );; } } else if ($module == 'hide-backend') { if (isset($settings['enabled']) && !empty($settings['enabled'])) { $permalink_structure = get_option( 'permalink_structure', false ); if ( empty( $permalink_structure ) && ! is_multisite() ) { $errors[] = __( 'You must change WordPress permalinks to a setting other than "Plain" in order to use "Hide Backend" feature.', 'better-wp-security' ); $require_permalinks = true; $do_not_save = true; } } } else if ($module == 'network-brute-force') { if ( isset( $settings['email'] ) ) { $result = $this->activate_api_key($settings); if ($result === false) { $nbf_settings = $settings; $errors[] = 'Error: Active iThemes Network Brute Force Protection Api Key'; } else { $nbf_settings = $result; } } else { $previous_settings = ITSEC_Modules::get_settings( $module ); // update 'enable_ban' field only if (isset($settings['enable_ban'])) { $previous_settings['enable_ban'] = $settings['enable_ban']; $nbf_settings = $previous_settings; } else { $do_not_save = true; $nbf_settings = $previous_settings; } } $settings = $nbf_settings; } if ( !$do_not_save ) { ITSEC_Modules::set_settings( $module, $settings ); $updated = true; } } } require_once( ITSEC_Core::get_core_dir() . '/lib/class-itsec-lib-config-file.php' ); $values = array( 'permalink_structure' => get_option( 'permalink_structure' ), 'is_multisite' => is_multisite() ? 1 : 0, 'users_can_register' => get_site_option( 'users_can_register' ) ? 1 : 0, 'server_nginx' => ( ITSEC_Lib::get_server() === 'nginx' ) ? 1 : 0, 'has_ssl' => ITSEC_Lib::get_ssl() ? 1 : 0, 'jquery_version' => ITSEC_Modules::get_setting( 'wordpress-tweaks', 'jquery_version' ), 'is_jquery_version_safe'=> ITSEC_Lib::is_jquery_version_safe(), 'server_rules' => ITSEC_Lib_Config_File::get_server_config(), 'config_rules' => ITSEC_Lib_Config_File::get_wp_config(), 'lockouts_host' => $this->get_lockouts( 'host', true ), 'lockouts_user' => $this->get_lockouts( 'user', true ), 'lockouts_username' => $this->get_lockouts( 'username', true ), 'default_log_location' => ITSEC_Modules::get_default( 'global', 'log_location' ), 'default_location' => ITSEC_Modules::get_default( 'backup', 'location' ), 'excludable_tables' => $this->get_excludable_tables(), ); $return = array( 'site_status' => $values ); if ($require_permalinks) { $return['require_permalinks'] = 1; } $return['nbf_settings'] = $nbf_settings; if (!empty($errors)) { $return['extra_message'] = $errors; } if ($updated) $return['result'] = 'success'; else $return['error'] = __('Not Updated', 'mainwp-child' ); return $return; } public static function activate_network_brute_force() { $data = maybe_unserialize( base64_decode( $_POST['data'] ) ); $information = array(); if (is_array($data)) { $settings = ITSEC_Modules::get_settings( 'network-brute-force' ); $settings['email'] = $data['email']; $settings['updates_optin'] = $data['updates_optin']; $settings['api_nag'] = false; $results = ITSEC_Modules::set_settings( 'network-brute-force', $settings ); if ( is_wp_error( $results ) ) { $information['error'] = 'Error: Active iThemes Network Brute Force Protection Api Key'; } else if ( $results['saved'] ) { ITSEC_Modules::activate( 'network-brute-force' ); $nbf_settings = ITSEC_Modules::get_settings( 'network-brute-force' ); // ITSEC_Response::set_response( '
' . __( 'Your site is now using Network Brute Force Protection.', 'better-wp-security' ) . '
' ); } } if ($nbf_settings !== null) { $information['nbf_settings'] = $nbf_settings; $information['result'] = 'success'; } return $information; } private function validate_directory($name, $folder) { require_once( ITSEC_Core::get_core_dir() . 'lib/class-itsec-lib-directory.php' ); $error = null; if ( ! ITSEC_Lib_Directory::is_dir( $folder ) ) { $result = ITSEC_Lib_Directory::create( $folder ); if ( is_wp_error( $result ) ) { $error = sprintf( _x( 'The directory supplied in %1$s cannot be used as a valid directory. %2$s', '%1$s is the input name. %2$s is the error message.', 'better-wp-security' ), $name, $result->get_error_message() ); } } if ( empty( $error ) && ! ITSEC_Lib_Directory::is_writable( $folder ) ) { $error = sprintf( __( 'The directory supplied in %1$s is not writable. Please select a directory that can be written to.', 'better-wp-security' ), $name ); } if ( empty( $error ) ) { ITSEC_Lib_Directory::add_file_listing_protection( $folder ); return true; } else { return $error; } } private function activate_api_key($settings) { global $mainwp_itsec_modules_path; require_once ( $mainwp_itsec_modules_path . 'ipcheck/utilities.php' ); $key = ITSEC_Network_Brute_Force_Utilities::get_api_key( $settings['email'], $settings['updates_optin'] ); if ( is_wp_error( $key ) ) { return false; // $this->set_can_save( false ); // $this->add_error( $key ); } else { $secret = ITSEC_Network_Brute_Force_Utilities::activate_api_key( $key ); if ( is_wp_error( $secret ) ) { return false; // $this->set_can_save( false ); // $this->add_error( $secret ); } else { $settings['api_key'] = $key; $settings['api_secret'] = $secret; $settings['api_nag'] = false; ITSEC_Response::reload_module( 'network-brute-force' ); } } unset( $settings['email'] ); return $settings; } function backup_status() { $status = 0; if ( ! is_multisite() && class_exists( 'backupbuddy_api' ) && count( backupbuddy_api::getSchedules() ) >= 1 ) { $status = 1; } elseif ( ! is_multisite() && class_exists( 'backupbuddy_api' ) ) { $status = 2; } elseif ( $this->has_backup() === true && $this->scheduled_backup() === true ) { $status = 3; } elseif ( $this->has_backup() === true ) { $status = 4; } return $status; } public function has_backup() { $has_backup = false; return apply_filters( 'itsec_has_external_backup', $has_backup ); } public function scheduled_backup() { $sceduled_backup = false; return apply_filters( 'itsec_scheduled_external_backup', $sceduled_backup ); } public function whitelist() { global $itsec_globals; $ip = $_POST['ip']; $add_temp = false; $temp_ip = get_site_option( 'itsec_temp_whitelist_ip' ); if ( false !== $temp_ip ) { if ( ( $temp_ip['exp'] < $itsec_globals['current_time'] ) || ( $temp_ip['exp'] !== $ip ) ) { delete_site_option( 'itsec_temp_whitelist_ip' ); $add_temp = true; } } else { $add_temp = true; } if ( false === $add_temp ) { return array( 'error' => 'Not Updated' ); } else { $response = array( 'ip' => $ip, 'exp' => $itsec_globals['current_time'] + 86400, ); add_site_option( 'itsec_temp_whitelist_ip', $response ); $response['exp_diff'] = human_time_diff( $itsec_globals['current_time'], $response['exp'] ); $response['message1'] = __( 'Your IP Address', 'better-wp-security' ); $response['message2'] = __( 'is whitelisted for', 'better-wp-security' ); return $response; } } function whitelist_release() { delete_site_option( 'itsec_temp_whitelist_ip' ); return 'success'; } function backup_db() { global $itsec_backup, $mainwp_itsec_modules_path; if ( ! isset( $itsec_backup ) ) { require_once ( $mainwp_itsec_modules_path . 'backup/class-itsec-backup.php' ); $itsec_backup = new ITSEC_Backup(); $itsec_backup->run(); } $return = array(); $str_error = ''; $result = $itsec_backup->do_backup( true ); if ( is_wp_error( $result ) ) { $errors = ITSEC_Response::get_error_strings( $result ); foreach ( $errors as $error ) { $str_error .= $error . '%1$s
.', 'better-wp-security' ), gettype( $result ) ) ;
}
if (!empty($str_error)) {
$return['error'] = $str_error;
}
return $return;
}
private function wordpress_salts() {
global $mainwp_itsec_modules_path;
if ( ! class_exists( 'ITSEC_WordPress_Salts_Utilities' ) ) {
require( $mainwp_itsec_modules_path . 'salts/utilities.php' );
}
$result = ITSEC_WordPress_Salts_Utilities::generate_new_salts();
$str_error = '';
if ( is_wp_error( $result ) ) {
$errors = ITSEC_Response::get_error_strings( $result );
foreach ( $errors as $error ) {
$str_error .= $error . '%1$s
.', 'better-wp-security' ), $result['new_prefix'] );
}
}
return $return;
}
public function api_key() {
$settings = get_site_option( 'itsec_ipcheck' );
if ( ! is_array( $settings ) ) {
$settings = array();
}
$settings['reset'] = true;
$return = array();
if ( update_site_option( 'itsec_ipcheck', $settings ) ) {
$return['result'] = 'success';
} else {
$return['result'] = 'nochange';
}
return $return;
}
public function reset_api_key() {
$defaults = ITSEC_Modules::get_defaults( 'network-brute-force' );
$results = ITSEC_Modules::set_settings( 'network-brute-force', $defaults );
ITSEC_Response::set_response( $results['saved'] );
ITSEC_Response::add_errors( $results['errors'] );
ITSEC_Response::add_messages( $results['messages'] );
$information = array();
if ( $results['saved'] ) {
$information['result'] = 'success';
$information['nbf_settings'] = ITSEC_Modules::get_settings( 'network-brute-force');
} else if ( empty( $results['errors'] ) ) {
$information['error_reset_api'] = 1;
}
return $information;
}
public function malware_scan() {
global $mainwp_itsec_modules_path;
if ( ! class_exists( 'ITSEC_Malware_Scanner' ) ) {
require_once( $mainwp_itsec_modules_path . 'malware/class-itsec-malware-scanner.php' );
require_once( $mainwp_itsec_modules_path . 'malware/class-itsec-malware-scan-results-template.php' );
}
$response = array();
if ( ! ITSEC_Core::current_user_can_manage() ) {
$response['error'] = 'The currently logged in user does not have sufficient permissions to run this scan.';
} else {
$results = ITSEC_Malware_Scanner::scan();
$response['html'] = ITSEC_Malware_Scan_Results_Template::get_html( $results, true );
}
return $response;
}
public function malware_get_scan_results() {
global $mainwp_itsec_modules_path;
if ( ! class_exists( 'ITSEC_Malware_Scanner' ) ) {
require_once( $mainwp_itsec_modules_path . 'malware/class-itsec-malware-scanner.php' );
require_once( $mainwp_itsec_modules_path . 'malware/class-itsec-malware-scan-results-template.php' );
}
$response = array();
$results= ITSEC_Malware_Scanner::scan();
$response['html'] = ITSEC_Malware_Scan_Results_Template::get_html( $results, true );
return $response;
}
public function purge_logs() {
global $wpdb;
$wpdb->query( 'DELETE FROM `' . $wpdb->base_prefix . 'itsec_log`;' );
return array( 'result' => 'success' );
}
public function get_lockouts( $type = 'all', $current = false ) {
global $wpdb, $itsec_globals;
if ( 'all' !== $type || true === $current ) {
$where = ' WHERE ';
} else {
$where = '';
}
switch ( $type ) {
case 'host':
$type_statement = "`lockout_host` IS NOT NULL && `lockout_host` != ''";
break;
case 'user':
$type_statement = '`lockout_user` != 0';
break;
case 'username':
$type_statement = "`lockout_username` IS NOT NULL && `lockout_username` != ''";
break;
default:
$type_statement = '';
break;
}
if ( true === $current ) {
if ( '' !== $type_statement ) {
$and = ' AND ';
} else {
$and = '';
}
$active = $and . " `lockout_active`=1 AND `lockout_expire_gmt` > '" . date( 'Y-m-d H:i:s', $itsec_globals['current_time_gmt'] ) . "'";
} else {
$active = '';
}
$results = $wpdb->get_results( 'SELECT * FROM `' . $wpdb->base_prefix . 'itsec_lockouts`' . $where . $type_statement . $active . ';', ARRAY_A );
$output = array();
if ( is_array( $results ) && count( $results ) > 0 ) {
switch ( $type ) {
case 'host':
foreach ( $results as $val ) {
$output[] = array(
'lockout_id' => $val['lockout_id'],
'lockout_host' => $val['lockout_host'],
'lockout_expire_gmt' => $val['lockout_expire_gmt'],
);
}
break;
case 'user':
foreach ( $results as $val ) {
$output[] = array(
'lockout_id' => $val['lockout_id'],
'lockout_user' => $val['lockout_user'],
'lockout_expire_gmt' => $val['lockout_expire_gmt'],
);
}
break;
case 'username':
foreach ( $results as $val ) {
$output[] = array(
'lockout_id' => $val['lockout_id'],
'lockout_username' => $val['lockout_username'],
'lockout_expire_gmt' => $val['lockout_expire_gmt'],
);
}
break;
default:
break;
}
}
return $output;
}
public function release_lockout() {
global $wpdb;
if ( ! class_exists( 'ITSEC_Lib' ) ) {
require( ITSEC_Core::get_core_dir() . '/core/class-itsec-lib.php' );
}
$lockout_ids = $_POST['lockout_ids'];
if ( ! is_array( $lockout_ids ) ) {
$lockout_ids = array();
}
$type = 'updated';
$message = __( 'The selected lockouts have been cleared.', 'better-wp-security' );
foreach ( $lockout_ids as $value ) {
$wpdb->update(
$wpdb->base_prefix . 'itsec_lockouts',
array(
'lockout_active' => 0,
),
array(
'lockout_id' => intval( $value ),
)
);
}
ITSEC_Lib::clear_caches();
if ( ! is_multisite() ) {
if ( ! function_exists( 'add_settings_error' ) ) {
require_once( ABSPATH . '/wp-admin/includes/template.php' );
}
add_settings_error( 'itsec', esc_attr( 'settings_updated' ), $message, $type );
}
return array(
'result' => 'success',
);
}
public function update_module_status() {
$active_modules = $_POST['active_modules'];
if (!is_array($active_modules))
$active_modules = array();
update_site_option( 'itsec_active_modules', $active_modules );
return array('result' => 'success');
}
private function reload_backup_exclude( ) {
return array(
'exclude' => ITSEC_Modules::get_setting( 'backup', 'exclude' ),
'excludable_tables' => $this->get_excludable_tables(),
'result' => 'success'
);
}
private function get_excludable_tables( ) {
global $wpdb;
$all_sites = ITSEC_Modules::get_setting( 'backup', 'all_sites' );
$ignored_tables = array(
'commentmeta',
'comments',
'links',
'options',
'postmeta',
'posts',
'term_relationships',
'term_taxonomy',
'terms',
'usermeta',
'users',
);
if ( $all_sites ) {
$query = 'SHOW_TABLES';
} else {
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', "{$wpdb->base_prefix}%" );
}
$tables = $wpdb->get_results( $query, ARRAY_N );
$excludes = array();
foreach ( $tables as $table ) {
$short_table = substr( $table[0], strlen( $wpdb->prefix ) );
if ( in_array( $short_table, $ignored_tables ) ) {
continue;
}
$excludes[$short_table] = $table[0];
}
return $excludes ;
}
private function security_site() {
global $mainwp_itsec_modules_path;
require_once( $mainwp_itsec_modules_path . 'security-check/scanner.php' );
ITSEC_Security_Check_Scanner::run();
$response = ITSEC_Response::get_response();
return array('result' => 'success' , 'response' => $response);
}
}