403));
}
// Handle form submission
if (isset($_POST['dwarpal_login_settings']) && isset($_POST['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'dwarpal_login_settings')) {
$dwarpal_old_enabled = get_option('dwarpal_login_security_enabled', true);
$dwarpal_new_enabled = isset($_POST['dwarpal_login_security_enabled']) ? 1 : 0;
$dwarpal_new_limit = isset($_POST['dwarpal_login_attempts_limit']) ? absint(wp_unslash($_POST['dwarpal_login_attempts_limit'])) : 5;
$dwarpal_new_duration = isset($_POST['dwarpal_login_lockout_duration']) ? absint(wp_unslash($_POST['dwarpal_login_lockout_duration'])) : 3600;
// Ensure minimum values
if ($dwarpal_new_limit < 1) $dwarpal_new_limit = 5;
if ($dwarpal_new_duration < 60) $dwarpal_new_duration = 3600;
$dwarpal_result1 = update_option('dwarpal_login_security_enabled', $dwarpal_new_enabled);
$dwarpal_result2 = update_option('dwarpal_login_attempts_limit', $dwarpal_new_limit);
$dwarpal_result3 = update_option('dwarpal_login_lockout_duration', $dwarpal_new_duration);
// Log the change
if (class_exists('Dwarpal_Security')) {
$dwarpal_security = new Dwarpal_Security();
$dwarpal_status = $dwarpal_new_enabled ? 'enabled' : 'disabled';
$dwarpal_security->log_event('settings_changed', sprintf('Login Security %s by user: %s (Limit: %d, Duration: %ds)', $dwarpal_status, wp_get_current_user()->user_login, $dwarpal_new_limit, $dwarpal_new_duration));
}
if ($dwarpal_result1 !== false && $dwarpal_result2 !== false && $dwarpal_result3 !== false) {
echo '
' . esc_html__('Settings saved. Login Security is now ', 'dwarpal') . ($dwarpal_new_enabled ? esc_html__('enabled', 'dwarpal') : esc_html__('disabled', 'dwarpal')) . '.
';
} else {
echo '' . esc_html__('Error: Some settings could not be saved.', 'dwarpal') . '
';
}
} elseif (isset($_POST['dwarpal_login_settings'])) {
echo '' . esc_html__('Security check failed. Please try again.', 'dwarpal') . '
';
}
// Handle lockout clearing
if (isset($_POST['dwarpal_clear_lockout']) && isset($_POST['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'dwarpal_clear_lockout')) {
$dwarpal_login_security = new Dwarpal_Login_Security();
if (isset($_POST['clear_all_lockouts']) && $_POST['clear_all_lockouts'] === '1') {
$dwarpal_count = $dwarpal_login_security->clear_all_lockouts();
if ($dwarpal_count > 0) {
/* translators: %d: Number of lockouts */
echo '' . sprintf(esc_html__('Successfully cleared %d lockout(s).', 'dwarpal'), esc_html($dwarpal_count)) . '
';
} else {
echo '' . esc_html__('No active lockouts found.', 'dwarpal') . '
';
}
} elseif (isset($_POST['clear_ip']) && !empty($_POST['clear_ip'])) {
$dwarpal_ip = sanitize_text_field(wp_unslash($_POST['clear_ip']));
if (filter_var($dwarpal_ip, FILTER_VALIDATE_IP)) {
if ($dwarpal_login_security->clear_ip_lockout($dwarpal_ip)) {
/* translators: %s: IP address */
echo '' . sprintf(esc_html__('Lockout cleared for IP: %s', 'dwarpal'), esc_html($dwarpal_ip)) . '
';
} else {
echo '' . esc_html__('Failed to clear lockout. IP may not be locked out.', 'dwarpal') . '
';
}
} else {
echo '' . esc_html__('Invalid IP address.', 'dwarpal') . '
';
}
}
}
// Handle whitelist management
if (isset($_POST['dwarpal_whitelist_action']) && isset($_POST['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'dwarpal_whitelist_manage')) {
$dwarpal_whitelisted_ips = get_option('dwarpal_whitelisted_ips', array());
if (!is_array($dwarpal_whitelisted_ips)) {
$dwarpal_whitelisted_ips = array();
}
if (isset($_POST['add_whitelist_ip']) && !empty($_POST['whitelist_ip'])) {
$dwarpal_ip = sanitize_text_field(wp_unslash($_POST['whitelist_ip']));
// Validate IP or CIDR range
$dwarpal_is_valid = false;
if (strpos($dwarpal_ip, '/') !== false) {
// CIDR range
list($dwarpal_subnet, $dwarpal_mask) = explode('/', $dwarpal_ip);
if (filter_var($dwarpal_subnet, FILTER_VALIDATE_IP) && is_numeric($dwarpal_mask) && $dwarpal_mask >= 0 && $dwarpal_mask <= 128) {
$dwarpal_is_valid = true;
}
} else {
// Single IP
if (filter_var($dwarpal_ip, FILTER_VALIDATE_IP)) {
$dwarpal_is_valid = true;
}
}
if ($dwarpal_is_valid && !in_array($dwarpal_ip, $dwarpal_whitelisted_ips, true)) {
$dwarpal_whitelisted_ips[] = $dwarpal_ip;
update_option('dwarpal_whitelisted_ips', $dwarpal_whitelisted_ips);
if (class_exists('Dwarpal_Security')) {
$dwarpal_security = new Dwarpal_Security();
$dwarpal_security->log_event('ip_whitelisted', sprintf('IP whitelisted for login security: %s by user: %s', $dwarpal_ip, wp_get_current_user()->user_login));
}
/* translators: %s: IP address */
echo '' . sprintf(esc_html__('IP %s has been added to the whitelist.', 'dwarpal'), esc_html($dwarpal_ip)) . '
';
} elseif (!$dwarpal_is_valid) {
echo '' . esc_html__('Invalid IP address or CIDR range.', 'dwarpal') . '
';
} else {
echo '' . esc_html__('IP is already in the whitelist.', 'dwarpal') . '
';
}
} elseif (isset($_POST['remove_whitelist_ip']) && !empty($_POST['whitelist_ip'])) {
$dwarpal_ip = sanitize_text_field(wp_unslash($_POST['whitelist_ip']));
$dwarpal_key = array_search($dwarpal_ip, $dwarpal_whitelisted_ips, true);
if ($dwarpal_key !== false) {
unset($dwarpal_whitelisted_ips[$dwarpal_key]);
$dwarpal_whitelisted_ips = array_values($dwarpal_whitelisted_ips); // Re-index array
update_option('dwarpal_whitelisted_ips', $dwarpal_whitelisted_ips);
if (class_exists('Dwarpal_Security')) {
$dwarpal_security = new Dwarpal_Security();
$dwarpal_security->log_event('ip_unwhitelisted', sprintf('IP removed from login security whitelist: %s by user: %s', $dwarpal_ip, wp_get_current_user()->user_login));
}
/* translators: %s: IP address */
echo '' . sprintf(esc_html__('IP %s has been removed from the whitelist.', 'dwarpal'), esc_html($dwarpal_ip)) . '
';
} else {
echo '' . esc_html__('IP not found in whitelist.', 'dwarpal') . '
';
}
}
}
$dwarpal_login_security_enabled = get_option('dwarpal_login_security_enabled', true);
$dwarpal_login_attempts_limit = get_option('dwarpal_login_attempts_limit', 5);
$dwarpal_login_lockout_duration = get_option('dwarpal_login_lockout_duration', 3600);
?>