mirror of
https://github.com/SubleXBle/Fail2Ban-Report.git
synced 2026-02-10 10:48:33 -06:00
Update action_report-ip.php
multiselection changes
This commit is contained in:
@@ -1,51 +1,118 @@
|
||||
<?php
|
||||
// includes/actions/action_report-ip.php
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$ip = $_POST['ip'] ?? null;
|
||||
$config = parse_ini_file('/opt/Fail2Ban-Report/fail2ban-report.config');
|
||||
|
||||
// Basic checks
|
||||
if (!$config['report'] || !$config['report_types'] || !$ip) {
|
||||
$ips = $_POST['ip'] ?? null;
|
||||
if (!$config['report'] || !$config['report_types'] || !$ips) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Reporting not enabled or invalid IP address.',
|
||||
'type' => 'info',
|
||||
'message' => 'Reporting not enabled or invalid IP(s).',
|
||||
'type' => 'info',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Parse report service list
|
||||
$services = explode(',', $config['report_types']);
|
||||
$results = [];
|
||||
|
||||
foreach ($services as $service) {
|
||||
$service = trim($service);
|
||||
$script = __DIR__ . "/reports/$service.php";
|
||||
|
||||
if (file_exists($script)) {
|
||||
ob_start();
|
||||
include $script;
|
||||
$response = ob_get_clean();
|
||||
|
||||
$decoded = json_decode($response, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
$results[$service] = [
|
||||
'success' => false,
|
||||
'message' => "Invalid JSON response: " . json_last_error_msg(),
|
||||
'raw_response' => $response
|
||||
];
|
||||
} else {
|
||||
$results[$service] = $decoded;
|
||||
}
|
||||
} else {
|
||||
$results[$service] = ['success' => false, 'message' => "$service script not available."];
|
||||
}
|
||||
if (!is_array($ips)) {
|
||||
$ips = [$ips]; // Convert single IP to array
|
||||
}
|
||||
|
||||
// Return collected responses from all report sources
|
||||
$services = array_map('trim', explode(',', $config['report_types']));
|
||||
$results = [];
|
||||
$allMessages = [];
|
||||
$overallSuccess = true;
|
||||
|
||||
foreach ($ips as $ip) {
|
||||
$ip = trim($ip);
|
||||
$reportResults = [];
|
||||
$messages = [];
|
||||
$ipSuccess = true;
|
||||
|
||||
foreach ($services as $service) {
|
||||
$script = __DIR__ . "/reports/$service.php";
|
||||
|
||||
// Check API keys for services that require them
|
||||
if ($service === 'abuseipdb' && empty($config['abuseipdb_key'])) {
|
||||
$ipSuccess = false;
|
||||
$reportResults[$service] = [
|
||||
'success' => false,
|
||||
'message' => 'AbuseIPDB API key not set.',
|
||||
'type' => 'error'
|
||||
];
|
||||
$messages[] = "[$service] API key missing";
|
||||
continue;
|
||||
}
|
||||
if ($service === 'ipinfo' && empty($config['ipinfo_key'])) {
|
||||
$ipSuccess = false;
|
||||
$reportResults[$service] = [
|
||||
'success' => false,
|
||||
'message' => 'IPInfo API key not set.',
|
||||
'type' => 'error'
|
||||
];
|
||||
$messages[] = "[$service] API key missing";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file_exists($script)) {
|
||||
$_POST['ip'] = $ip;
|
||||
|
||||
ob_start();
|
||||
include $script;
|
||||
$response = ob_get_clean();
|
||||
|
||||
// Pause 500ms to avoid hitting API rate limits
|
||||
usleep(500000);
|
||||
|
||||
$decoded = json_decode($response, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
$ipSuccess = false;
|
||||
$reportResults[$service] = [
|
||||
'success' => false,
|
||||
'message' => "Invalid JSON from $service: " . json_last_error_msg(),
|
||||
'raw_response' => $response
|
||||
];
|
||||
$messages[] = "[$service] JSON error";
|
||||
} else {
|
||||
if (empty($decoded['success'])) {
|
||||
$ipSuccess = false;
|
||||
}
|
||||
$reportResults[$service] = $decoded;
|
||||
$messages[] = "[$service] " . ($decoded['message'] ?? 'Reported.');
|
||||
}
|
||||
} else {
|
||||
$ipSuccess = false;
|
||||
$reportResults[$service] = [
|
||||
'success' => false,
|
||||
'message' => "$service report script not available",
|
||||
];
|
||||
$messages[] = "[$service] not available";
|
||||
}
|
||||
}
|
||||
|
||||
if (!$ipSuccess) {
|
||||
$overallSuccess = false;
|
||||
}
|
||||
|
||||
$ipMessage = implode(' | ', $messages);
|
||||
$allMessages[] = "$ip → $ipMessage";
|
||||
|
||||
$results[] = [
|
||||
'ip' => $ip,
|
||||
'success' => $ipSuccess,
|
||||
'message' => $ipMessage,
|
||||
'data' => $reportResults,
|
||||
];
|
||||
}
|
||||
|
||||
// Final overall message type forced to 'info' for better UI
|
||||
$finalType = 'info';
|
||||
$finalMessage = implode(' || ', $allMessages);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Report data retrieved.',
|
||||
'data' => $results,
|
||||
'type' => 'info',
|
||||
]);
|
||||
'success' => $overallSuccess,
|
||||
'message' => $finalMessage,
|
||||
'type' => $finalType,
|
||||
'results' => $results
|
||||
], JSON_PRETTY_PRINT);
|
||||
|
||||
Reference in New Issue
Block a user