Skip to content

Commit 9aa671b

Browse files
committed
Added AbuseIPDB support, including settings management and error handling.
1 parent 3992ee4 commit 9aa671b

File tree

5 files changed

+476
-40
lines changed

5 files changed

+476
-40
lines changed

modules/core/handler_modules.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,20 +1017,58 @@ public function process() {
10171017
class Hm_Handler_process_spam_report_settings extends Hm_Handler_Module {
10181018
public function process() {
10191019
list($success, $form) = $this->process_form(array('save_settings'));
1020-
if (!$success || !array_key_exists('spamcop_settings', $this->request->post)) {
1020+
if (!$success) {
10211021
return;
10221022
}
10231023

10241024
$new_settings = $this->get('new_user_settings', array());
1025-
$spamcop = $this->request->post['spamcop_settings'];
10261025

1027-
$set_email_setting = function($key, $value) use (&$new_settings) {
1028-
$new_settings[$key] = (!empty($value) && filter_var($value, FILTER_VALIDATE_EMAIL)) ? $value : '';
1026+
// Helper function for validation
1027+
$set_setting = function($key, $value, $validator = null) use (&$new_settings) {
1028+
if ($validator && !$validator($value)) {
1029+
$new_settings[$key] = '';
1030+
} else {
1031+
$new_settings[$key] = $value;
1032+
}
10291033
};
10301034

1031-
$new_settings['spamcop_enabled_setting'] = isset($spamcop['enabled']);
1032-
$set_email_setting('spamcop_submission_email_setting', $spamcop['submission_email'] ?? '');
1033-
$set_email_setting('spamcop_from_email_setting', $spamcop['from_email'] ?? '');
1035+
// Process SpamCop settings
1036+
if (array_key_exists('spamcop_settings', $this->request->post)) {
1037+
$spamcop = $this->request->post['spamcop_settings'];
1038+
$new_settings['spamcop_enabled_setting'] = isset($spamcop['enabled']);
1039+
$set_setting('spamcop_submission_email_setting', $spamcop['submission_email'] ?? '', function($v) {
1040+
return filter_var($v, FILTER_VALIDATE_EMAIL);
1041+
});
1042+
$set_setting('spamcop_from_email_setting', $spamcop['from_email'] ?? '', function($v) {
1043+
return filter_var($v, FILTER_VALIDATE_EMAIL);
1044+
});
1045+
}
1046+
1047+
// Process AbuseIPDB settings
1048+
if (array_key_exists('abuseipdb_settings', $this->request->post)) {
1049+
$abuseipdb = $this->request->post['abuseipdb_settings'];
1050+
$new_settings['abuseipdb_enabled_setting'] = isset($abuseipdb['enabled']);
1051+
1052+
// Handle API key: if field is empty but api_key_set flag is present, preserve original key
1053+
$api_key = $abuseipdb['api_key'] ?? '';
1054+
$api_key_was_set = isset($abuseipdb['api_key_set']) && $abuseipdb['api_key_set'] == '1';
1055+
1056+
if (empty($api_key) && $api_key_was_set) {
1057+
// User left field empty but key was originally set - preserve the original key
1058+
$original_key = $this->user_config->get('abuseipdb_api_key_setting', '');
1059+
if (!empty($original_key)) {
1060+
$new_settings['abuseipdb_api_key_setting'] = $original_key;
1061+
} else {
1062+
$new_settings['abuseipdb_api_key_setting'] = '';
1063+
}
1064+
} else {
1065+
// User entered a new value (or cleared it) - validate and set it
1066+
$set_setting('abuseipdb_api_key_setting', $api_key, function($v) {
1067+
// API key validation: non-empty string, reasonable length (10-200 chars)
1068+
return !empty($v) && strlen($v) >= 10 && strlen($v) <= 200;
1069+
});
1070+
}
1071+
}
10341072

10351073
$this->out('new_user_settings', $new_settings, false);
10361074
}

modules/core/output_modules.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,56 @@ protected function output() {
23792379
}
23802380
}
23812381

2382+
/**
2383+
* Option to enable/disable AbuseIPDB reporting
2384+
* @subpackage core/output
2385+
*/
2386+
class Hm_Output_abuseipdb_enabled_setting extends Hm_Output_Module {
2387+
protected function output() {
2388+
$settings = $this->get('user_settings', array());
2389+
$enabled = get_setting_value($settings, 'abuseipdb_enabled', false);
2390+
$checked = $enabled ? ' checked="checked"' : '';
2391+
$reset = $enabled ? '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-counterclockwise refresh_list reset_default_value_checkbox"></i></span>' : '';
2392+
2393+
return '<tr class="report_spam_setting"><td><label class="form-check-label" for="abuseipdb_enabled">'.
2394+
$this->trans('Enable AbuseIPDB reporting').'</label></td>'.
2395+
'<td><input class="form-check-input" type="checkbox" '.$checked.' id="abuseipdb_enabled" name="abuseipdb_settings[enabled]" data-default-value="false" value="1" />'.$reset.'</td></tr>';
2396+
}
2397+
}
2398+
2399+
/**
2400+
* Option for AbuseIPDB API key
2401+
* @subpackage core/output
2402+
*/
2403+
class Hm_Output_abuseipdb_api_key_setting extends Hm_Output_Module {
2404+
protected function output() {
2405+
$settings = $this->get('user_settings', array());
2406+
$api_key = get_setting_value($settings, 'abuseipdb_api_key', '');
2407+
2408+
// Mask API key if it exists - show empty field with indicator
2409+
// The handler will preserve the original key if empty value is submitted
2410+
$display_value = '';
2411+
$placeholder = $this->trans('Your AbuseIPDB API key');
2412+
2413+
if (!empty($api_key)) {
2414+
// Show empty field but indicate key is set via placeholder
2415+
// User must enter new value to change it, or leave empty to keep current
2416+
$placeholder = $this->trans('API key is set (••••••••) - enter new value to change');
2417+
}
2418+
2419+
$reset = !empty($api_key) ? '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-counterclockwise refresh_list reset_default_value_input"></i></span>' : '';
2420+
2421+
// Add a hidden field to track if API key was originally set
2422+
// This helps the handler know to preserve the key if field is left empty
2423+
$hidden_field = !empty($api_key) ? '<input type="hidden" name="abuseipdb_settings[api_key_set]" value="1" />' : '';
2424+
2425+
return '<tr class="report_spam_setting"><td><label for="abuseipdb_api_key">'.
2426+
$this->trans('AbuseIPDB API Key').'</label></td>'.
2427+
'<td class="d-flex">'.$hidden_field.
2428+
'<input class="form-control form-control-sm" type="password" id="abuseipdb_api_key" name="abuseipdb_settings[api_key]" value="'.$this->html_safe($display_value).'" placeholder="'.$placeholder.'" autocomplete="off" />'.$reset.'</td></tr>';
2429+
}
2430+
}
2431+
23822432
/**
23832433
* Option to warn user when he has unsaved changes.
23842434
* @subpackage imap/output

modules/core/setup.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@
106106
add_output('settings', 'spamcop_enabled_setting', true, 'core', 'start_report_spam_settings', 'after');
107107
add_output('settings', 'spamcop_submission_email_setting', true, 'core', 'spamcop_enabled_setting', 'after');
108108
add_output('settings', 'spamcop_from_email_setting', true, 'core', 'spamcop_submission_email_setting', 'after');
109-
add_output('settings', 'start_everything_settings', true, 'core', 'spamcop_from_email_setting', 'after');
109+
add_output('settings', 'abuseipdb_enabled_setting', true, 'core', 'spamcop_from_email_setting', 'after');
110+
add_output('settings', 'abuseipdb_api_key_setting', true, 'core', 'abuseipdb_enabled_setting', 'after');
111+
add_output('settings', 'start_everything_settings', true, 'core', 'abuseipdb_api_key_setting', 'after');
110112
add_output('settings', 'all_since_setting', true, 'core', 'start_everything_settings', 'after');
111113
add_output('settings', 'all_source_max_setting', true, 'core', 'all_since_setting', 'after');
112114
add_output('settings', 'start_all_email_settings', true, 'core', 'all_source_max_setting', 'after');
@@ -366,5 +368,6 @@
366368
'images_whitelist' => FILTER_UNSAFE_RAW,
367369
'update' => FILTER_VALIDATE_BOOLEAN,
368370
'spamcop_settings' => array('filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FORCE_ARRAY),
371+
'abuseipdb_settings' => array('filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FORCE_ARRAY),
369372
)
370373
);

0 commit comments

Comments
 (0)