2015-10-15 22:52:37 +10:00
< ? php
2019-02-19 23:52:21 +07:00
/*
*
* Credits
*
* Plugin - Name : iThemes Security
* Plugin URI : https :// ithemes . com / security
* Author : iThemes
* Author URI : https :// ithemes . com
* License : GPLv2
*
* The code is used for the MainWP iThemes Security Extension
* Extension URL : https :// mainwp . com / extension / ithemes - security /
*
*/
2015-10-15 22:52:37 +10:00
class MainWP_Child_iThemes_Security {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2017-12-04 21:55:05 +01:00
public $is_plugin_installed = false ;
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
static function Instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
self :: $instance = new MainWP_Child_iThemes_Security ();
2015-10-15 22:52:37 +10:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
2018-09-27 19:52:32 +02:00
public function __construct () {
2020-03-26 14:11:33 +00:00
require_once ABSPATH . 'wp-admin/includes/plugin.php' ;
2017-12-04 21:55:05 +01:00
if ( is_plugin_active ( 'better-wp-security/better-wp-security.php' ) || is_plugin_active ( 'ithemes-security-pro/ithemes-security-pro.php' ) ) {
2018-09-27 19:52:32 +02:00
$this -> is_plugin_installed = true ;
}
2020-03-26 17:03:00 +00:00
if ( ! $this -> is_plugin_installed ) {
2017-12-04 21:55:05 +01:00
return ;
2020-03-26 14:11:33 +00:00
}
2018-09-27 19:52:32 +02:00
2015-12-22 19:18:07 +01:00
add_filter ( 'mainwp-site-sync-others-data' , array ( $this , 'syncOthersData' ), 10 , 2 );
2015-10-15 22:52:37 +10:00
}
2018-06-26 19:52:53 +02:00
// ok
2015-12-22 19:18:07 +01:00
function syncOthersData ( $information , $data = array () ) {
2015-10-15 22:52:37 +10:00
if ( is_array ( $data ) && isset ( $data [ 'ithemeExtActivated' ] ) && ( 'yes' === $data [ 'ithemeExtActivated' ] ) ) {
2020-03-26 17:03:00 +00:00
try {
2018-06-26 19:52:53 +02:00
$information [ 'syncIThemeData' ] = array (
2020-03-26 14:05:04 +00:00
'users_and_roles' => $this -> get_available_admin_users_and_roles (),
2018-09-27 19:52:32 +02:00
);
2020-03-26 17:03:00 +00:00
} catch ( Exception $e ) {
2018-06-26 19:52:53 +02:00
error_log ( $e -> getMessage ());
}
2018-09-27 19:52:32 +02:00
}
2015-12-22 19:18:07 +01:00
return $information ;
2015-10-15 22:52:37 +10:00
}
public function action () {
$information = array ();
2020-03-26 17:03:00 +00:00
if ( ! class_exists ( 'ITSEC_Core' ) || ! class_exists ( 'ITSEC_Modules' )) {
2016-06-08 21:47:49 +02:00
$information [ 'error' ] = 'NO_ITHEME' ;
2015-10-15 22:52:37 +10:00
MainWP_Helper :: write ( $information );
2018-09-27 19:52:32 +02:00
}
2016-06-08 21:47:49 +02:00
global $mainwp_itsec_modules_path ;
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
$mainwp_itsec_modules_path = ITSEC_Core :: get_core_dir () . '/modules/' ;
2018-11-14 21:33:36 +07:00
2015-10-15 22:52:37 +10:00
if ( isset ( $_POST [ 'mwp_action' ] ) ) {
switch ( $_POST [ 'mwp_action' ] ) {
case 'set_showhide' :
$information = $this -> set_showhide ();
break ;
case 'save_settings' :
$information = $this -> save_settings ();
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' :
2016-06-08 21:47:49 +02:00
$information = $this -> change_database_prefix ();
2015-10-15 22:52:37 +10:00
break ;
2016-08-02 19:51:22 +02:00
case 'reset_api_key' :
$information = $this -> reset_api_key ();
break ;
2015-10-15 22:52:37 +10:00
case 'malware_scan' :
$information = $this -> malware_scan ();
break ;
case 'clear_all_logs' :
$information = $this -> purge_logs ();
break ;
2016-06-08 21:47:49 +02:00
case 'file_change' :
$information = $this -> file_change ();
2015-10-15 22:52:37 +10:00
break ;
case 'release_lockout' :
$information = $this -> release_lockout ();
break ;
2016-06-08 21:47:49 +02:00
case 'module_status' :
$information = $this -> update_module_status ();
2018-09-27 19:52:32 +02:00
break ;
2016-06-08 21:47:49 +02:00
case 'wordpress_salts' :
$information = $this -> wordpress_salts ();
break ;
case 'file_permissions' :
$information = $this -> file_permissions ();
break ;
2016-08-02 19:51:22 +02:00
case 'reload_backup_exclude' :
$information = $this -> reload_backup_exclude ();
break ;
case 'security_site' :
$information = $this -> security_site ();
break ;
case 'activate_network_brute_force' :
2018-09-27 19:52:32 +02:00
$information = $this -> activate_network_brute_force ();
break ;
2015-10-15 22:52:37 +10:00
}
}
MainWP_Helper :: write ( $information );
}
2018-09-27 19:52:32 +02:00
function set_showhide () {
2015-10-15 22:52:37 +10:00
$hide = isset ( $_POST [ 'showhide' ] ) && ( 'hide' === $_POST [ 'showhide' ] ) ? 'hide' : '' ;
MainWP_Helper :: update_option ( 'mainwp_ithemes_hide_plugin' , $hide );
2016-06-08 21:47:49 +02:00
$information [ 'result' ] = 'success' ;
2015-10-15 22:52:37 +10:00
return $information ;
}
public function ithemes_init () {
2020-03-26 17:03:00 +00:00
if ( ! $this -> is_plugin_installed ) {
2015-10-15 22:52:37 +10:00
return ;
2020-03-26 14:11:33 +00:00
}
2015-10-15 22:52:37 +10:00
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' ) );
2018-06-26 19:52:53 +02:00
add_action ( 'admin_head' , array ( & $this , 'custom_admin_css' ) );
2020-03-26 15:29:54 +00:00
if ( isset ( $_GET [ 'page' ]) && ( $_GET [ 'page' ] == 'itsec' || $_GET [ 'page' ] == 'itsec-security-check' ) ) {
2018-06-26 19:52:53 +02:00
wp_redirect ( get_option ( 'siteurl' ) . '/wp-admin/index.php' );
exit ();
2018-09-27 19:52:32 +02:00
}
2015-10-15 22:52:37 +10:00
}
}
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' );
}
2018-06-26 19:52:53 +02:00
function custom_admin_css () {
?>
< style type = " text/css " >
#wp-admin-bar-itsec_admin_bar_menu{
display : none ! important ;
2018-09-27 19:52:32 +02:00
}
</ style >
< ? php
2018-06-26 19:52:53 +02:00
}
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
function save_settings () {
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
if ( ! class_exists ( 'ITSEC_Lib' ) ) {
2020-03-26 14:11:33 +00:00
require ITSEC_Core :: get_core_dir () . '/core/class-itsec-lib.php' ;
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
$_itsec_modules = array (
2018-09-27 19:52:32 +02:00
'global' ,
2016-06-08 21:47:49 +02:00
'away-mode' ,
2018-09-27 19:52:32 +02:00
'backup' ,
2016-06-08 21:47:49 +02:00
'hide-backend' ,
'ipcheck' ,
'ban-users' ,
'brute-force' ,
'file-change' ,
2018-09-27 19:52:32 +02:00
'404-detection' ,
'network-brute-force' ,
2016-06-08 21:47:49 +02:00
'ssl' ,
2020-03-26 20:01:04 +00:00
// 'strong-passwords',
2018-09-27 19:52:32 +02:00
'password-requirements' ,
2016-06-08 21:47:49 +02:00
'system-tweaks' ,
'wordpress-tweaks' ,
2018-09-27 19:52:32 +02:00
'multisite-tweaks' ,
'notification-center' ,
2020-03-26 20:01:04 +00:00
// 'salts',
// 'content-directory',
2018-09-27 19:52:32 +02:00
);
$require_permalinks = false ;
2020-03-26 19:45:07 +00:00
$updated = false ;
$errors = array ();
$nbf_settings = array ();
2018-09-27 19:52:32 +02:00
2020-03-26 19:45:07 +00:00
$update_settings = maybe_unserialize ( base64_decode ( $_POST [ 'settings' ] ) );
2018-09-27 19:52:32 +02:00
2020-03-26 17:03:00 +00:00
foreach ( $update_settings as $module => $settings ) {
2016-08-02 19:51:22 +02:00
$do_not_save = false ;
2018-09-27 19:52:32 +02:00
if ( in_array ( $module , $_itsec_modules )) {
if ( $module == 'wordpress-salts' ) {
$settings [ 'last_generated' ] = ITSEC_Modules :: get_setting ( $module , 'last_generated' ); // not update
2020-03-26 14:11:33 +00:00
} elseif ( $module == 'global' ) {
2020-03-26 14:05:04 +00:00
$keep_olds = array ( 'did_upgrade' , 'log_info' , 'show_new_dashboard_notice' , 'show_security_check' , 'nginx_file' );
2020-03-26 17:03:00 +00:00
foreach ( $keep_olds as $key ) {
2020-03-26 15:29:54 +00:00
$settings [ $key ] = ITSEC_Modules :: get_setting ( $module , $key ); // not update
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 17:03:00 +00:00
if ( ! isset ( $settings [ 'log_location' ]) || empty ( $settings [ 'log_location' ]) ) {
2016-08-02 19:51:22 +02:00
$settings [ 'log_location' ] = ITSEC_Modules :: get_setting ( $module , 'log_location' );
2018-09-27 19:52:32 +02:00
} else {
2016-08-02 19:51:22 +02:00
$result = $this -> validate_directory ( 'log_location' , $settings [ 'log_location' ]);
if ( $result !== true ) {
2020-03-26 19:45:07 +00:00
$errors [] = $result ;
2016-08-02 19:51:22 +02:00
$settings [ 'log_location' ] = ITSEC_Modules :: get_setting ( $module , 'log_location' ); // no change
}
}
2018-09-27 19:52:32 +02:00
2020-03-26 14:11:33 +00:00
} elseif ( $module == 'backup' ) {
2020-03-26 17:03:00 +00:00
if ( ! isset ( $settings [ 'location' ]) || empty ( $settings [ 'location' ]) ) {
2016-08-02 19:51:22 +02:00
$settings [ 'location' ] = ITSEC_Modules :: get_setting ( $module , 'location' );
} else {
$result = $this -> validate_directory ( 'location' , $settings [ 'location' ]);
if ( $result !== true ) {
2020-03-26 19:45:07 +00:00
$errors [] = $result ;
2016-08-02 19:51:22 +02:00
$settings [ 'location' ] = ITSEC_Modules :: get_setting ( $module , 'location' ); // no change
}
}
2020-03-26 17:03:00 +00:00
if ( ! isset ( $settings [ 'exclude' ]) ) {
2020-03-26 14:05:04 +00:00
$settings [ 'exclude' ] = ITSEC_Modules :: get_setting ( $module , 'exclude' );
2016-08-02 19:51:22 +02:00
}
2020-03-26 14:11:33 +00:00
} elseif ( $module == 'hide-backend' ) {
2020-03-26 17:03:00 +00:00
if ( isset ( $settings [ 'enabled' ]) && ! empty ( $settings [ 'enabled' ])) {
2016-08-02 19:51:22 +02:00
$permalink_structure = get_option ( 'permalink_structure' , false );
if ( empty ( $permalink_structure ) && ! is_multisite () ) {
2020-03-26 19:45:07 +00:00
$errors [] = __ ( 'You must change <strong>WordPress permalinks</strong> to a setting other than "Plain" in order to use "Hide Backend" feature.' , 'better-wp-security' );
2016-08-02 19:51:22 +02:00
$require_permalinks = true ;
2020-03-26 19:45:07 +00:00
$do_not_save = true ;
2016-08-02 19:51:22 +02:00
}
}
2020-03-26 14:11:33 +00:00
} elseif ( $module == 'network-brute-force' ) {
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
if ( isset ( $settings [ 'email' ] ) ) {
$result = $this -> activate_api_key ( $settings );
if ( $result === false ) {
$nbf_settings = $settings ;
2020-03-26 19:45:07 +00:00
$errors [] = 'Error: Active iThemes Network Brute Force Protection Api Key' ;
2018-09-27 19:52:32 +02:00
} else {
2016-08-02 19:51:22 +02:00
$nbf_settings = $result ;
}
} else {
2018-09-27 19:52:32 +02:00
$previous_settings = ITSEC_Modules :: get_settings ( $module );
2016-08-02 19:51:22 +02:00
// update 'enable_ban' field only
2018-09-27 19:52:32 +02:00
if ( isset ( $settings [ 'enable_ban' ])) {
$previous_settings [ 'enable_ban' ] = $settings [ 'enable_ban' ];
2020-03-26 19:45:07 +00:00
$nbf_settings = $previous_settings ;
2016-08-02 19:51:22 +02:00
} else {
2020-03-26 19:45:07 +00:00
$do_not_save = true ;
2016-08-02 19:51:22 +02:00
$nbf_settings = $previous_settings ;
}
2018-09-27 19:52:32 +02:00
}
2016-08-02 19:51:22 +02:00
$settings = $nbf_settings ;
2020-03-26 14:11:33 +00:00
} elseif ( $module == 'notification-center' ) {
2017-12-04 21:55:05 +01:00
$current_settings = ITSEC_Modules :: get_settings ( $module );
if ( isset ( $settings [ 'notifications' ])) {
2020-03-26 15:29:54 +00:00
$update_fields = array ( 'schedule' , 'enabled' , 'subject' );
2017-12-04 21:55:05 +01:00
if ( isset ( $_POST [ 'is_individual' ]) && $_POST [ 'is_individual' ]) {
2020-03-26 15:29:54 +00:00
$update_fields = array_merge ( $update_fields , array ( 'user_list' , 'email_list' ));
2017-12-04 21:55:05 +01:00
}
2018-09-27 19:52:32 +02:00
foreach ( $settings [ 'notifications' ] as $key => $val ) {
2017-12-04 21:55:05 +01:00
foreach ( $update_fields as $field ) {
2020-03-26 17:03:00 +00:00
if ( isset ( $val [ $field ])) {
2020-03-26 15:29:54 +00:00
$current_settings [ 'notifications' ][ $key ][ $field ] = $val [ $field ];
2017-12-04 21:55:05 +01:00
}
}
}
2020-03-26 19:45:07 +00:00
$updated = true ;
2017-12-04 21:55:05 +01:00
ITSEC_Modules :: set_settings ( $module , $current_settings );
}
continue ;
2018-09-27 19:52:32 +02:00
}
2020-03-26 17:03:00 +00:00
if ( ! $do_not_save ) {
2016-08-02 19:51:22 +02:00
ITSEC_Modules :: set_settings ( $module , $settings );
2020-03-26 19:45:07 +00:00
$updated = true ;
2016-06-12 18:33:51 +02:00
}
2018-09-27 19:52:32 +02:00
}
2015-10-15 22:52:37 +10:00
}
2017-08-24 20:41:12 +02:00
if ( isset ( $update_settings [ 'itsec_active_modules' ] ) ) {
2017-12-04 21:55:05 +01:00
$current_val = get_site_option ( 'itsec_active_modules' , array () );
foreach ( $update_settings [ 'itsec_active_modules' ] as $mod => $val ) {
2020-03-26 15:29:54 +00:00
$current_val [ $mod ] = $val ;
2017-12-04 21:55:05 +01:00
}
update_site_option ( 'itsec_active_modules' , $current_val );
2017-08-24 20:41:12 +02:00
}
2020-03-26 14:11:33 +00:00
require_once ITSEC_Core :: get_core_dir () . '/lib/class-itsec-lib-config-file.php' ;
2015-10-15 22:52:37 +10:00
2016-06-08 21:47:49 +02:00
$values = array (
2015-10-15 22:52:37 +10:00
'permalink_structure' => get_option ( 'permalink_structure' ),
'is_multisite' => is_multisite () ? 1 : 0 ,
2018-09-27 19:52:32 +02:00
'users_can_register' => get_site_option ( 'users_can_register' ) ? 1 : 0 ,
'server_nginx' => ( ITSEC_Lib :: get_server () === 'nginx' ) ? 1 : 0 ,
2020-03-26 14:05:04 +00:00
'has_ssl' => ITSEC_Lib :: get_ssl_support_probability (),
'jquery_version' => ITSEC_Modules :: get_setting ( 'wordpress-tweaks' , 'jquery_version' ),
'server_rules' => ITSEC_Lib_Config_File :: get_server_config (),
'config_rules' => ITSEC_Lib_Config_File :: get_wp_config (),
2015-10-15 22:52:37 +10:00
'lockouts_host' => $this -> get_lockouts ( 'host' , true ),
'lockouts_user' => $this -> get_lockouts ( 'user' , true ),
2018-09-27 19:52:32 +02:00
'lockouts_username' => $this -> get_lockouts ( 'username' , true ),
2020-03-26 15:29:54 +00:00
'default_log_location' => ITSEC_Modules :: get_default ( 'global' , 'log_location' ),
'default_location' => ITSEC_Modules :: get_default ( 'backup' , 'location' ),
'excludable_tables' => $this -> get_excludable_tables (),
'users_and_roles' => $this -> get_available_admin_users_and_roles (),
2015-10-15 22:52:37 +10:00
);
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
$return = array (
2020-03-26 14:05:04 +00:00
'site_status' => $values ,
2016-06-08 21:47:49 +02:00
);
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
if ( $require_permalinks ) {
$return [ 'require_permalinks' ] = 1 ;
2018-09-27 19:52:32 +02:00
}
$return [ 'nbf_settings' ] = $nbf_settings ;
2020-03-26 17:03:00 +00:00
if ( ! empty ( $errors )) {
2016-08-02 19:51:22 +02:00
$return [ 'extra_message' ] = $errors ;
}
2018-09-27 19:52:32 +02:00
2020-03-26 14:11:33 +00:00
if ( $updated ) {
2018-09-27 19:52:32 +02:00
$return [ 'result' ] = 'success' ;
2020-03-26 14:11:33 +00:00
} else {
2018-09-27 19:52:32 +02:00
$return [ 'error' ] = __ ( 'Not Updated' , 'mainwp-child' );
2020-03-26 14:11:33 +00:00
}
2018-09-27 19:52:32 +02:00
return $return ;
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
public static function activate_network_brute_force () {
2020-03-26 19:45:07 +00:00
$data = maybe_unserialize ( base64_decode ( $_POST [ 'data' ] ) );
2016-08-02 19:51:22 +02:00
$information = array ();
if ( is_array ( $data )) {
2020-03-26 19:45:07 +00:00
$settings = ITSEC_Modules :: get_settings ( 'network-brute-force' );
$settings [ 'email' ] = $data [ 'email' ];
2016-08-02 19:51:22 +02:00
$settings [ 'updates_optin' ] = $data [ 'updates_optin' ];
2020-03-26 19:45:07 +00:00
$settings [ 'api_nag' ] = false ;
$results = ITSEC_Modules :: set_settings ( 'network-brute-force' , $settings );
2018-09-27 19:52:32 +02:00
if ( is_wp_error ( $results ) ) {
2016-08-02 19:51:22 +02:00
$information [ 'error' ] = 'Error: Active iThemes Network Brute Force Protection Api Key' ;
2020-03-26 14:11:33 +00:00
} elseif ( $results [ 'saved' ] ) {
2018-09-27 19:52:32 +02:00
ITSEC_Modules :: activate ( 'network-brute-force' );
2016-08-02 19:51:22 +02:00
$nbf_settings = ITSEC_Modules :: get_settings ( 'network-brute-force' );
2020-03-26 20:01:04 +00:00
// ITSEC_Response::set_response( '<p>' . __( 'Your site is now using Network Brute Force Protection.', 'better-wp-security' ) . '</p>' );
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
}
2016-08-02 19:51:22 +02:00
if ( $nbf_settings !== null ) {
$information [ 'nbf_settings' ] = $nbf_settings ;
2020-03-26 19:45:07 +00:00
$information [ 'result' ] = 'success' ;
2018-09-27 19:52:32 +02:00
}
return $information ;
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 14:05:04 +00:00
private function validate_directory ( $name , $folder ) {
2020-03-26 14:11:33 +00:00
require_once ITSEC_Core :: get_core_dir () . 'lib/class-itsec-lib-directory.php' ;
2016-08-02 19:51:22 +02:00
$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 ;
}
2015-10-15 22:52:37 +10:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 14:05:04 +00:00
private function activate_api_key ( $settings ) {
2016-08-02 19:51:22 +02:00
global $mainwp_itsec_modules_path ;
2020-03-26 14:11:33 +00:00
require_once $mainwp_itsec_modules_path . 'ipcheck/utilities.php' ;
2018-09-27 19:52:32 +02:00
$key = ITSEC_Network_Brute_Force_Utilities :: get_api_key ( $settings [ 'email' ], $settings [ 'updates_optin' ] );
2016-08-02 19:51:22 +02:00
if ( is_wp_error ( $key ) ) {
return false ;
2020-03-26 20:01:04 +00:00
// $this->set_can_save( false );
// $this->add_error( $key );
2016-08-02 19:51:22 +02:00
} else {
$secret = ITSEC_Network_Brute_Force_Utilities :: activate_api_key ( $key );
if ( is_wp_error ( $secret ) ) {
return false ;
2020-03-26 20:01:04 +00:00
// $this->set_can_save( false );
// $this->add_error( $secret );
2016-08-02 19:51:22 +02:00
} else {
2020-03-26 19:45:07 +00:00
$settings [ 'api_key' ] = $key ;
2016-08-02 19:51:22 +02:00
$settings [ 'api_secret' ] = $secret ;
$settings [ 'api_nag' ] = false ;
2015-10-15 22:52:37 +10:00
2016-08-02 19:51:22 +02:00
ITSEC_Response :: reload_module ( 'network-brute-force' );
}
}
unset ( $settings [ 'email' ] );
return $settings ;
}
2015-10-15 22:52:37 +10:00
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' ] );
2015-12-05 17:38:52 +01:00
$response [ 'message1' ] = __ ( 'Your IP Address' , 'better-wp-security' );
$response [ 'message2' ] = __ ( 'is whitelisted for' , 'better-wp-security' );
2015-10-15 22:52:37 +10:00
return $response ;
}
}
function whitelist_release () {
delete_site_option ( 'itsec_temp_whitelist_ip' );
return 'success' ;
}
function backup_db () {
2016-06-08 21:47:49 +02:00
global $itsec_backup , $mainwp_itsec_modules_path ;
2018-09-27 19:52:32 +02:00
if ( ! isset ( $itsec_backup ) ) {
2020-03-26 14:11:33 +00:00
require_once $mainwp_itsec_modules_path . 'backup/class-itsec-backup.php' ;
2016-06-08 21:47:49 +02:00
$itsec_backup = new ITSEC_Backup ();
2018-09-27 19:52:32 +02:00
$itsec_backup -> run ();
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 19:45:07 +00:00
$return = array ();
2018-09-27 19:52:32 +02:00
$str_error = '' ;
2020-03-26 19:45:07 +00:00
$result = $itsec_backup -> do_backup ( true );
2016-06-08 21:47:49 +02:00
if ( is_wp_error ( $result ) ) {
$errors = ITSEC_Response :: get_error_strings ( $result );
foreach ( $errors as $error ) {
$str_error .= $error . '<br />' ;
}
2020-03-26 14:11:33 +00:00
} elseif ( is_string ( $result ) ) {
2020-03-26 19:45:07 +00:00
$return [ 'result' ] = 'success' ;
2016-06-08 21:47:49 +02:00
$return [ 'message' ] = $result ;
} else {
2020-03-26 14:05:04 +00:00
$str_error = sprintf ( __ ( 'The backup request returned an unexpected response. It returned a response of type <code>%1$s</code>.' , 'better-wp-security' ), gettype ( $result ) );
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 17:03:00 +00:00
if ( ! empty ( $str_error )) {
2016-06-08 21:47:49 +02:00
$return [ 'error' ] = $str_error ;
2018-09-27 19:52:32 +02:00
}
2016-06-08 21:47:49 +02:00
return $return ;
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
private function wordpress_salts () {
2018-09-27 19:52:32 +02:00
global $mainwp_itsec_modules_path ;
2016-06-08 21:47:49 +02:00
if ( ! class_exists ( 'ITSEC_WordPress_Salts_Utilities' ) ) {
2020-03-26 14:11:33 +00:00
require $mainwp_itsec_modules_path . 'salts/utilities.php' ;
2018-09-27 19:52:32 +02:00
}
2020-03-26 19:45:07 +00:00
$result = ITSEC_WordPress_Salts_Utilities :: generate_new_salts ();
2018-09-27 19:52:32 +02:00
$str_error = '' ;
2016-06-08 21:47:49 +02:00
if ( is_wp_error ( $result ) ) {
$errors = ITSEC_Response :: get_error_strings ( $result );
foreach ( $errors as $error ) {
$str_error .= $error . '<br />' ;
}
2018-09-27 19:52:32 +02:00
} else {
2020-03-26 19:45:07 +00:00
$return [ 'result' ] = 'success' ;
2020-03-26 14:05:04 +00:00
$return [ 'message' ] = __ ( 'The WordPress salts were successfully regenerated.' , 'better-wp-security' );
2020-03-26 19:45:07 +00:00
$last_generated = ITSEC_Core :: get_current_time_gmt ();
2016-06-08 21:47:49 +02:00
ITSEC_Modules :: set_setting ( 'wordpress-salts' , 'last_generated' , $last_generated );
2018-09-27 19:52:32 +02:00
}
2020-03-26 17:03:00 +00:00
if ( ! empty ( $str_error )) {
2016-06-08 21:47:49 +02:00
$return [ 'error' ] = $str_error ;
2018-09-27 19:52:32 +02:00
}
return $return ;
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
private function file_permissions () {
2020-03-26 14:11:33 +00:00
require_once ITSEC_Core :: get_core_dir () . '/lib/class-itsec-lib-config-file.php' ;
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$wp_upload_dir = ITSEC_Core :: get_wp_upload_dir ();
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$path_data = array (
array (
ABSPATH ,
0755 ,
),
array (
ABSPATH . WPINC ,
0755 ,
),
array (
ABSPATH . 'wp-admin' ,
0755 ,
),
array (
ABSPATH . 'wp-admin/js' ,
0755 ,
),
array (
WP_CONTENT_DIR ,
0755 ,
),
array (
get_theme_root (),
0755 ,
),
array (
WP_PLUGIN_DIR ,
2020-03-26 14:05:04 +00:00
0755 ,
2016-08-02 19:51:22 +02:00
),
array (
$wp_upload_dir [ 'basedir' ],
0755 ,
),
array (
ITSEC_Lib_Config_File :: get_wp_config_file_path (),
0444 ,
),
array (
ITSEC_Lib_Config_File :: get_server_config_file_path (),
0444 ,
),
);
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$rows = array ();
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
foreach ( $path_data as $path ) {
$row = array ();
2015-10-15 22:52:37 +10:00
2016-08-02 19:51:22 +02:00
list ( $path , $suggested_permissions ) = $path ;
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$display_path = preg_replace ( '/^' . preg_quote ( ABSPATH , '/' ) . '/' , '' , $path );
$display_path = ltrim ( $display_path , '/' );
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
if ( empty ( $display_path ) ) {
$display_path = '/' ;
}
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$row [] = $display_path ;
$row [] = sprintf ( '%o' , $suggested_permissions );
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$permissions = fileperms ( $path ) & 0777 ;
2020-03-26 19:45:07 +00:00
$row [] = sprintf ( '%o' , $permissions );
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
if ( ! $permissions || $permissions != $suggested_permissions ) {
$row [] = __ ( 'WARNING' , 'better-wp-security' );
$row [] = '<div style="background-color: #FEFF7F; border: 1px solid #E2E2E2;"> </div>' ;
} else {
$row [] = __ ( 'OK' , 'better-wp-security' );
$row [] = '<div style="background-color: #22EE5B; border: 1px solid #E2E2E2;"> </div>' ;
}
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$rows [] = $row ;
}
2016-06-08 21:47:49 +02:00
2016-08-02 19:51:22 +02:00
$class = 'entry-row' ;
2020-03-26 19:51:58 +00:00
ob_start ();
?>
2016-06-08 21:47:49 +02:00
< p >< input type = " button " id = " itsec-file-permissions-reload_file_permissions " name = " file-permissions[reload_file_permissions] " class = " button-primary itsec-reload-module " value = " <?php _e('Reload File Permissions Details', 'mainwp-child'); ?> " ></ p >
< table class = " widefat " >
< thead >
2016-08-02 19:51:22 +02:00
< tr >
< th >< ? php _e ( 'Relative Path' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Suggestion' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Value' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Result' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Status' , 'better-wp-security' ); ?> </th>
</ tr >
2016-06-08 21:47:49 +02:00
</ thead >
< tfoot >
2016-08-02 19:51:22 +02:00
< tr >
< th >< ? php _e ( 'Relative Path' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Suggestion' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Value' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Result' , 'better-wp-security' ); ?> </th>
< th >< ? php _e ( 'Status' , 'better-wp-security' ); ?> </th>
</ tr >
2016-06-08 21:47:49 +02:00
</ tfoot >
< tbody >
2016-08-02 19:51:22 +02:00
< ? php foreach ( $rows as $row ) : ?>
< tr class = " <?php echo $class ; ?> " >
< ? php foreach ( $row as $column ) : ?>
< td >< ? php echo $column ; ?> </td>
< ? php endforeach ; ?>
</ tr >
< ? php $class = ( 'entry-row' === $class ) ? 'entry-row alternate' : 'entry-row' ; ?>
< ? php endforeach ; ?>
2016-06-08 21:47:49 +02:00
</ tbody >
</ table >
< br />
2020-03-26 19:51:58 +00:00
< ? php
2016-06-08 21:47:49 +02:00
$html = ob_get_clean ();
2020-03-26 15:29:54 +00:00
return array ( 'html' => $html );
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
public function file_change () {
2018-09-27 19:52:32 +02:00
global $mainwp_itsec_modules_path ;
2016-06-08 21:47:49 +02:00
if ( ! class_exists ( 'ITSEC_File_Change_Scanner' ) ) {
2020-03-26 14:11:33 +00:00
require_once $mainwp_itsec_modules_path . 'file-change/scanner.php' ;
2018-09-27 19:52:32 +02:00
}
2016-06-08 21:47:49 +02:00
$result = ITSEC_File_Change_Scanner :: run_scan ( false );
if ( $result === false || $result === true || $result === - 1 ) {
2020-03-26 19:45:07 +00:00
$return [ 'result' ] = 'success' ;
2018-09-27 19:52:32 +02:00
$return [ 'scan_result' ] = $result ;
}
return $return ;
2015-10-15 22:52:37 +10:00
}
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
function admin_user () {
2018-09-27 19:52:32 +02:00
$settings = $_POST [ 'settings' ];
2020-03-26 17:03:00 +00:00
if ( ! is_array ( $settings )) {
2016-06-08 21:47:49 +02:00
$settings = array ();
2020-03-26 14:11:33 +00:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 17:03:00 +00:00
$new_username = isset ( $settings [ 'new_username' ] ) ? $settings [ 'new_username' ] : '' ;
2020-03-26 19:45:07 +00:00
$change_id = isset ( $settings [ 'change_id' ] ) && $settings [ 'change_id' ] ? true : false ;
2018-09-27 19:52:32 +02:00
2020-03-26 20:01:04 +00:00
// load utility functions
2015-10-15 22:52:37 +10:00
if ( ! class_exists ( 'ITSEC_Lib' ) ) {
global $itsec_globals ;
2020-03-26 14:11:33 +00:00
require ITSEC_Core :: get_core_dir () . '/core/class-itsec-lib.php' ;
2015-10-15 22:52:37 +10:00
}
$username_exists = username_exists ( 'admin' );
$user_id_exists = ITSEC_Lib :: user_id_exists ( 1 );
2018-09-27 19:52:32 +02:00
$msg = '' ;
2017-12-04 21:55:05 +01:00
if ( strlen ( $new_username ) >= 1 ) {
global $current_user ;
if ( ! $username_exists ) {
$msg = __ ( 'Admin user already changes.' , 'mainwp-child' );
2020-03-26 14:11:33 +00:00
} elseif ( $current_user -> user_login == 'admin' ) {
2017-12-04 21:55:05 +01:00
$return [ 'result' ] = 'CHILD_ADMIN' ;
return $return ;
}
}
2018-09-27 19:52:32 +02:00
if ( true === $change_id && ! $user_id_exists ) {
2015-10-15 22:52:37 +10:00
if ( ! empty ( $msg ) ) {
$msg .= '<br/>' ;
}
$msg .= __ ( 'Admin user ID already changes.' , 'mainwp-child' );
}
2020-03-26 20:01:04 +00:00
// if ( $change_id ) {
// $user = get_user_by( 'login', $new_username );
// if ( $user && 1 === (int) $user->ID ) {
// $return['result'] = 'CHILD_ADMIN';
// return $return;
// }
// }
2015-10-15 22:52:37 +10:00
$admin_success = true ;
2020-03-26 19:45:07 +00:00
$return = array ();
2015-10-15 22:52:37 +10:00
2016-06-08 21:47:49 +02:00
if ( strlen ( $new_username ) >= 1 && $username_exists ) {
$admin_success = $this -> change_admin_user ( $new_username , $change_id );
} elseif ( true === $change_id && $user_id_exists ) {
$admin_success = $this -> change_admin_user ( null , $change_id );
2015-10-15 22:52:37 +10:00
}
2016-06-08 21:47:49 +02:00
$return [ 'message' ] = $msg ;
2015-10-15 22:52:37 +10:00
if ( false === $admin_success ) {
2016-06-08 21:47:49 +02:00
$return [ 'result' ] = 'fail' ;
2015-10-15 22:52:37 +10:00
} else {
2016-06-08 21:47:49 +02:00
$return [ 'result' ] = 'success' ;
2015-10-15 22:52:37 +10:00
}
2016-06-08 21:47:49 +02:00
return $return ;
2015-10-15 22:52:37 +10:00
}
private function change_admin_user ( $username = null , $id = false ) {
2016-06-08 21:47:49 +02:00
global $wpdb ;
$itsec_files = ITSEC_Core :: get_itsec_files ();
2018-09-27 19:52:32 +02:00
// do not need to check this
2020-03-26 20:01:04 +00:00
// if ( $itsec_files->get_file_lock( 'admin_user' ) ) { //make sure it isn't already running
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
// sanitize the username
2015-10-15 22:52:37 +10:00
$new_user = sanitize_text_field ( $username );
2020-03-26 20:01:04 +00:00
// Get the full user object
2015-10-15 22:52:37 +10:00
$user_object = get_user_by ( 'id' , '1' );
2020-03-26 20:01:04 +00:00
if ( null !== $username && validate_username ( $new_user ) && false === username_exists ( $new_user ) ) { // there is a valid username to change
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
if ( true === $id ) { // we're changing the id too so we'll set the username
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$user_login = $new_user ;
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
} else { // we're only changing the username
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
// query main user table
2020-03-26 19:51:58 +00:00
$wpdb -> query ( 'UPDATE `' . $wpdb -> users . " ` SET user_login = ' " . esc_sql ( $new_user ) . " ' WHERE user_login='admin'; " );
2018-09-27 19:52:32 +02:00
2020-03-26 20:01:04 +00:00
if ( is_multisite () ) { // process sitemeta if we're in a multi-site situation
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$oldAdmins = $wpdb -> get_var ( 'SELECT meta_value FROM `' . $wpdb -> sitemeta . " ` WHERE meta_key = 'site_admins' " );
$newAdmins = str_replace ( '5:"admin"' , strlen ( $new_user ) . ':"' . esc_sql ( $new_user ) . '"' , $oldAdmins );
$wpdb -> query ( $wpdb -> prepare ( 'UPDATE `' . $wpdb -> sitemeta . " ` SET meta_value = %s WHERE meta_key = 'site_admins' " , $newAdmins ) );
}
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
wp_clear_auth_cookie ();
$itsec_files -> release_file_lock ( 'admin_user' );
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
return true ;
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
}
2020-03-26 20:01:04 +00:00
} elseif ( null !== $username ) { // username didn't validate
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$itsec_files -> release_file_lock ( 'admin_user' );
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
return false ;
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
} else { // only changing the id
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$user_login = $user_object -> user_login ;
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
}
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
if ( true === $id ) { // change the user id
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$wpdb -> query ( 'DELETE FROM `' . $wpdb -> users . '` WHERE ID = 1;' );
2018-09-27 19:52:32 +02:00
2020-03-26 19:51:58 +00:00
$wpdb -> insert ( $wpdb -> users , array (
'user_login' => $user_login ,
'user_pass' => $user_object -> user_pass ,
'user_nicename' => $user_object -> user_nicename ,
'user_email' => $user_object -> user_email ,
'user_url' => $user_object -> user_url ,
'user_registered' => $user_object -> user_registered ,
'user_activation_key' => $user_object -> user_activation_key ,
'user_status' => $user_object -> user_status ,
'display_name' => $user_object -> display_name ,
) );
2015-10-15 22:52:37 +10:00
2020-03-26 20:01:04 +00:00
if ( is_multisite () && null !== $username && validate_username ( $new_user ) ) { // process sitemeta if we're in a multi-site situation
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$oldAdmins = $wpdb -> get_var ( 'SELECT meta_value FROM `' . $wpdb -> sitemeta . " ` WHERE meta_key = 'site_admins' " );
$newAdmins = str_replace ( '5:"admin"' , strlen ( $new_user ) . ':"' . esc_sql ( $new_user ) . '"' , $oldAdmins );
$wpdb -> query ( 'UPDATE `' . $wpdb -> sitemeta . " ` SET meta_value = ' " . esc_sql ( $newAdmins ) . " ' WHERE meta_key = 'site_admins' " );
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
}
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$new_user = $wpdb -> insert_id ;
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
$wpdb -> query ( $wpdb -> prepare ( 'UPDATE `' . $wpdb -> posts . '` SET post_author = %s WHERE post_author = 1;' , $new_user ) );
$wpdb -> query ( $wpdb -> prepare ( 'UPDATE `' . $wpdb -> usermeta . '` SET user_id = %s WHERE user_id = 1;' , $new_user ) );
$wpdb -> query ( $wpdb -> prepare ( 'UPDATE `' . $wpdb -> comments . '` SET user_id = %s WHERE user_id = 1;' , $new_user ) );
$wpdb -> query ( $wpdb -> prepare ( 'UPDATE `' . $wpdb -> links . '` SET link_owner = %s WHERE link_owner = 1;' , $new_user ) );
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
wp_clear_auth_cookie ();
$itsec_files -> release_file_lock ( 'admin_user' );
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
return true ;
2015-10-15 22:52:37 +10:00
2020-03-26 19:51:58 +00:00
}
2020-03-26 20:01:04 +00:00
// }
2015-10-15 22:52:37 +10:00
return false ;
}
public function build_wpconfig_rules ( $rules_array , $input = null ) {
2020-03-26 20:01:04 +00:00
// Get the rules from the database if input wasn't sent
2015-10-15 22:52:37 +10:00
if ( null === $input ) {
return $rules_array ;
}
$new_dir = trailingslashit ( ABSPATH ) . $input ;
$rules [] = array (
'type' => 'add' ,
'search_text' => '//Do not delete these. Doing so WILL break your site.' ,
'rule' => '//Do not delete these. Doing so WILL break your site.' ,
);
$rules [] = array (
'type' => 'add' ,
'search_text' => 'WP_CONTENT_URL' ,
'rule' => " define( 'WP_CONTENT_URL', ' " . trailingslashit ( get_option ( 'siteurl' ) ) . $input . " ' ); " ,
);
$rules [] = array (
'type' => 'add' ,
'search_text' => 'WP_CONTENT_DIR' ,
'rule' => " define( 'WP_CONTENT_DIR', ' " . $new_dir . " ' ); " ,
);
2020-03-26 15:29:54 +00:00
$rules_array [] = array (
'type' => 'wpconfig' ,
'name' => 'Content Directory' ,
'rules' => $rules ,
);
2015-10-15 22:52:37 +10:00
return $rules_array ;
}
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
2016-06-08 21:47:49 +02:00
public function change_database_prefix () {
2018-09-27 19:52:32 +02:00
global $mainwp_itsec_modules_path ;
2020-03-26 14:11:33 +00:00
require_once $mainwp_itsec_modules_path . 'database-prefix/utility.php' ;
2016-06-08 21:47:49 +02:00
$str_error = '' ;
2020-03-26 19:45:07 +00:00
$return = array ();
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
if ( isset ( $_POST [ 'change_prefix' ] ) && 'yes' === $_POST [ 'change_prefix' ] ) {
$result = ITSEC_Database_Prefix_Utility :: change_database_prefix ();
$return = $result [ 'errors' ];
if ( is_array ( $result [ 'errors' ])) {
foreach ( $result [ 'errors' ] as $error ) {
$arr_errors = ITSEC_Response :: get_error_strings ( $error );
foreach ( $arr_errors as $er ) {
$str_error .= $er . '<br />' ;
}
2015-10-15 22:52:37 +10:00
}
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
ITSEC_Response :: reload_module ( 'database-prefix' );
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
if ( false === $result [ 'new_prefix' ] ) {
$return [ 'error' ] = $str_error ;
} else {
2020-03-26 19:45:07 +00:00
$return [ 'result' ] = 'success' ;
2016-06-08 21:47:49 +02:00
$return [ 'message' ] = sprintf ( __ ( 'The database table prefix was successfully changed to <code>%1$s</code>.' , 'better-wp-security' ), $result [ 'new_prefix' ] );
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
}
2018-09-27 19:52:32 +02:00
}
return $return ;
2015-10-15 22:52:37 +10:00
}
public function api_key () {
$settings = get_site_option ( 'itsec_ipcheck' );
if ( ! is_array ( $settings ) ) {
$settings = array ();
}
$settings [ 'reset' ] = true ;
2020-03-26 19:45:07 +00:00
$return = array ();
2015-10-15 22:52:37 +10:00
if ( update_site_option ( 'itsec_ipcheck' , $settings ) ) {
2016-06-08 21:47:49 +02:00
$return [ 'result' ] = 'success' ;
2015-10-15 22:52:37 +10:00
} else {
2016-06-08 21:47:49 +02:00
$return [ 'result' ] = 'nochange' ;
2015-10-15 22:52:37 +10:00
}
2016-06-08 21:47:49 +02:00
return $return ;
2015-10-15 22:52:37 +10:00
}
public function reset_api_key () {
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
$defaults = ITSEC_Modules :: get_defaults ( 'network-brute-force' );
2020-03-26 19:45:07 +00:00
$results = ITSEC_Modules :: set_settings ( 'network-brute-force' , $defaults );
2016-08-02 19:51:22 +02:00
ITSEC_Response :: set_response ( $results [ 'saved' ] );
ITSEC_Response :: add_errors ( $results [ 'errors' ] );
ITSEC_Response :: add_messages ( $results [ 'messages' ] );
2018-09-27 19:52:32 +02:00
2016-08-02 19:51:22 +02:00
$information = array ();
if ( $results [ 'saved' ] ) {
2020-03-26 19:45:07 +00:00
$information [ 'result' ] = 'success' ;
2018-09-27 19:52:32 +02:00
$information [ 'nbf_settings' ] = ITSEC_Modules :: get_settings ( 'network-brute-force' );
2020-03-26 14:11:33 +00:00
} elseif ( empty ( $results [ 'errors' ] ) ) {
2016-08-02 19:51:22 +02:00
$information [ 'error_reset_api' ] = 1 ;
2015-10-15 22:52:37 +10:00
}
2016-08-02 19:51:22 +02:00
return $information ;
2015-10-15 22:52:37 +10:00
}
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
public function malware_scan () {
2016-06-08 21:47:49 +02:00
global $mainwp_itsec_modules_path ;
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
if ( ! class_exists ( 'ITSEC_Malware_Scanner' ) ) {
2020-03-26 14:11:33 +00:00
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' ;
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
$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 {
2020-03-26 19:45:07 +00:00
$results = ITSEC_Malware_Scanner :: scan ();
2016-06-08 21:47:49 +02:00
$response [ 'html' ] = ITSEC_Malware_Scan_Results_Template :: get_html ( $results , true );
2018-09-27 19:52:32 +02:00
}
2015-10-15 22:52:37 +10:00
return $response ;
}
public function malware_get_scan_results () {
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
global $mainwp_itsec_modules_path ;
if ( ! class_exists ( 'ITSEC_Malware_Scanner' ) ) {
2020-03-26 14:11:33 +00:00
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' ;
2018-09-27 19:52:32 +02:00
}
2020-03-26 19:45:07 +00:00
$response = array ();
$results = ITSEC_Malware_Scanner :: scan ();
2018-09-27 19:52:32 +02:00
$response [ 'html' ] = ITSEC_Malware_Scan_Results_Template :: get_html ( $results , true );
2015-10-15 22:52:37 +10:00
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 () {
2016-06-08 21:47:49 +02:00
global $wpdb ;
2015-10-15 22:52:37 +10:00
if ( ! class_exists ( 'ITSEC_Lib' ) ) {
2020-03-26 14:11:33 +00:00
require ITSEC_Core :: get_core_dir () . '/core/class-itsec-lib.php' ;
2015-10-15 22:52:37 +10:00
}
$lockout_ids = $_POST [ 'lockout_ids' ];
if ( ! is_array ( $lockout_ids ) ) {
$lockout_ids = array ();
}
$type = 'updated' ;
2015-12-05 17:38:52 +01:00
$message = __ ( 'The selected lockouts have been cleared.' , 'better-wp-security' );
2015-10-15 22:52:37 +10:00
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' ) ) {
2020-03-26 14:11:33 +00:00
require_once ABSPATH . '/wp-admin/includes/template.php' ;
2015-10-15 22:52:37 +10:00
}
add_settings_error ( 'itsec' , esc_attr ( 'settings_updated' ), $message , $type );
}
return array (
'result' => 'success' ,
);
}
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
public function update_module_status () {
2018-09-27 19:52:32 +02:00
2016-06-08 21:47:49 +02:00
$active_modules = $_POST [ 'active_modules' ];
2018-09-27 19:52:32 +02:00
2020-03-26 17:03:00 +00:00
if ( ! is_array ( $active_modules )) {
2016-06-08 21:47:49 +02:00
$active_modules = array ();
2020-03-26 14:11:33 +00:00
}
2018-09-27 19:52:32 +02:00
2017-12-04 21:55:05 +01:00
$current_val = get_site_option ( 'itsec_active_modules' , array () );
foreach ( $active_modules as $mod => $val ) {
2020-03-26 15:29:54 +00:00
$current_val [ $mod ] = $val ;
2017-12-04 21:55:05 +01:00
}
2018-09-27 19:52:32 +02:00
2017-12-04 21:55:05 +01:00
update_site_option ( 'itsec_active_modules' , $current_val );
2020-03-26 15:29:54 +00:00
return array ( 'result' => 'success' );
2018-09-27 19:52:32 +02:00
}
2020-03-26 14:05:04 +00:00
private function reload_backup_exclude () {
2016-08-02 19:51:22 +02:00
return array (
2020-03-26 15:29:54 +00:00
'exclude' => ITSEC_Modules :: get_setting ( 'backup' , 'exclude' ),
2016-08-02 19:51:22 +02:00
'excludable_tables' => $this -> get_excludable_tables (),
2020-03-26 15:29:54 +00:00
'result' => 'success' ,
2018-09-27 19:52:32 +02:00
);
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
2020-03-26 14:05:04 +00:00
private function get_excludable_tables () {
2016-08-02 19:51:22 +02:00
global $wpdb ;
2020-03-26 19:45:07 +00:00
$all_sites = ITSEC_Modules :: get_setting ( 'backup' , 'all_sites' );
2016-08-02 19:51:22 +02:00
$ignored_tables = array (
'commentmeta' ,
'comments' ,
'links' ,
'options' ,
'postmeta' ,
'posts' ,
'term_relationships' ,
'term_taxonomy' ,
'terms' ,
'usermeta' ,
'users' ,
);
if ( $all_sites ) {
2017-05-11 21:07:42 +02:00
$query = 'SHOW TABLES' ;
2016-08-02 19:51:22 +02:00
} else {
$query = $wpdb -> prepare ( 'SHOW TABLES LIKE %s' , " { $wpdb -> base_prefix } % " );
}
2020-03-26 19:45:07 +00:00
$tables = $wpdb -> get_results ( $query , ARRAY_N );
2016-08-02 19:51:22 +02:00
$excludes = array ();
foreach ( $tables as $table ) {
$short_table = substr ( $table [ 0 ], strlen ( $wpdb -> prefix ) );
if ( in_array ( $short_table , $ignored_tables ) ) {
continue ;
}
2020-03-26 15:29:54 +00:00
$excludes [ $short_table ] = $table [ 0 ];
2016-08-02 19:51:22 +02:00
}
2016-06-08 21:47:49 +02:00
2020-03-26 14:05:04 +00:00
return $excludes ;
2016-08-02 19:51:22 +02:00
}
2018-09-27 19:52:32 +02:00
2017-12-04 21:55:05 +01:00
private function security_site () {
2016-08-02 19:51:22 +02:00
global $mainwp_itsec_modules_path ;
2020-03-26 14:11:33 +00:00
require_once $mainwp_itsec_modules_path . 'security-check/scanner.php' ;
require_once $mainwp_itsec_modules_path . 'security-check/feedback-renderer.php' ;
2017-12-04 21:55:05 +01:00
$results = ITSEC_Security_Check_Scanner :: get_results ();
ob_start ();
ITSEC_Security_Check_Feedback_Renderer :: render ( $results );
2018-09-27 19:52:32 +02:00
$response = ob_get_clean ();
2020-03-26 15:29:54 +00:00
return array (
'result' => 'success' ,
'response' => $response ,
);
2016-06-08 21:47:49 +02:00
}
2018-09-27 19:52:32 +02:00
2017-12-04 21:55:05 +01:00
// source from itheme plugin
2018-06-26 19:52:53 +02:00
// ok
2017-12-04 21:55:05 +01:00
public function get_available_admin_users_and_roles () {
if ( is_callable ( 'wp_roles' ) ) {
$roles = wp_roles ();
} else {
$roles = new WP_Roles ();
}
$available_roles = array ();
$available_users = array ();
foreach ( $roles -> roles as $role => $details ) {
if ( isset ( $details [ 'capabilities' ][ 'manage_options' ] ) && ( true === $details [ 'capabilities' ][ 'manage_options' ] ) ) {
2020-03-26 15:29:54 +00:00
$available_roles [ " role: $role " ] = translate_user_role ( $details [ 'name' ] );
2017-12-04 21:55:05 +01:00
$users = get_users ( array ( 'role' => $role ) );
foreach ( $users as $user ) {
/* translators: 1: user display name, 2: user login */
$available_users [ $user -> ID ] = sprintf ( __ ( '%1$s (%2$s)' , 'better-wp-security' ), $user -> display_name , $user -> user_login );
}
}
}
natcasesort ( $available_users );
return array (
'users' => $available_users ,
'roles' => $available_roles ,
);
}
2018-09-27 19:52:32 +02:00
2015-10-15 22:52:37 +10:00
}