45)";
break;
case "OS":
$whereClauses[] = "OS LIKE '%2003%' OR OS LIKE '%2008%'";
break;
case "GLPI":
$whereClauses[] = "GLPI IS NULL OR GLPIlu IS NULL OR DATEDIFF(now(), GLPIlu) > 7";
break;
case "SCCM":
$whereClauses[] = "SCCM IS NULL OR (SCCMlu IS NOT NULL AND DATEDIFF(now(), SCCMlu) > 7)";
break;
case "NESSUS":
$whereClauses[] = "EPO IS NULL";
break;
case "NBU":
$whereClauses[] = "NBU IS NULL OR (NBUlu IS NOT NULL AND DATEDIFF(now(), NBUlu) > 30)";
break;
case "SCOM":
$whereClauses[] = "SCOM IS NULL";
break;
case "zabbix":
$whereClauses[] = "zabbix IS NULL";
break;
case "S1":
$whereClauses[] = "S1 IS NULL OR (S1lu IS NOT NULL AND DATEDIFF(now(), S1lu) > 7)";
break;
default:
$filterTitle = null; // No valid filter
break;
}
if ($filterTitle) {
echo "$filterTitle
";
}
}
// Combine all WHERE clauses
if (!empty($whereClauses)) {
$sql .= " WHERE " . implode(' AND ', $whereClauses);
}
$sql .= " ORDER BY server";
// Prepare and execute the query
$stmt = $conn->prepare($sql);
// Note: If you had parameters, you would bind them here, e.g., $stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
// --- PART 2: HELPER FUNCTIONS FOR RENDERING ---
/**
* Renders a standard status cell based on a value and its last update date.
* @param string|null $status The status value (e.g., 'Y', 'N').
* @param string|null $lastUpdate The date of the last update.
* @param int $daysThreshold The number of days to be considered "out of date".
* @param string $okText Text to display for OK status (e.g., "OK", "OK (NBU)").
* @return string The generated HTML for two cells.
*/
function renderStatusCellWithDate(?string $status, ?string $lastUpdate, int $daysThreshold, string $okText = 'OK'): string {
// Sanitize output to prevent XSS
$status = htmlspecialchars($status ?? '', ENT_QUOTES, 'UTF-8');
$lastUpdate = htmlspecialchars($lastUpdate ?? '', ENT_QUOTES, 'UTF-8');
$okText = htmlspecialchars($okText, ENT_QUOTES, 'UTF-8');
if ($status === 'Y') {
if (empty($lastUpdate)) {
// Status is OK, but date is missing
return " | $okText | "
. "Missing | ";
}
try {
$diff = date_diff(date_create($lastUpdate), date_create());
$days = (int) $diff->format("%R%a");
if ($days > $daysThreshold) {
// Out of date
return "$okText | "
. "$lastUpdate ($days days) | ";
} else {
// Compliant
return "$okText | "
. "$lastUpdate | ";
}
} catch (Exception) {
// Handle invalid date format gracefully
return "Invalid Date | $lastUpdate | ";
}
}
if ($status !== '') {
// Not applicable, non-supported, etc.
return "$status | | ";
}
// Missing
return "Missing | | ";
}
// --- PART 3: CLEAN DATA DISPLAY LOOP ---
// Initialize counters
$counters = [
'total' => 0, 'ok' => 0, 'nOS' => 0, 'nAD' => 0, 'nSCCM' => 0, 'nGLPI' => 0,
'nFI' => 0, 'nNESSUS' => 0, 'nNBU' => 0, 'nDPM' => 0, 'nS1' => 0
];
while ($row = $result->fetch_assoc()) {
$counters['total']++;
// Sanitize server name for URL and display
$serverName = htmlspecialchars($row['Server'], ENT_QUOTES, 'UTF-8');
$serverUrl = urlencode($row['Server']);
// Determine overall row status
$isCompliant = isset($row['AD'], $row['GLPI'], $row['SCCM'], $row['EPO']) && (isset($row['NBU']) || isset($row['DPM'])) && isset($row['SCOM']);
if ($isCompliant) {
$counters['ok']++;
$serverCell = "$serverName | ";
} else {
$serverCell = "$serverName | ";
}
// OS Cell
$osCell = " | ";
if (!empty($row['OS'])) {
$os = htmlspecialchars($row['OS']);
if (preg_match('(XP|2003|2000|2008|Windows 7|2012)', $row['OS'])) {
$osCell = "$os | ";
} else {
$counters['nOS']++;
$osCell = "$os | ";
}
}
echo "";
echo $serverCell;
echo $osCell;
echo "| " . htmlspecialchars($row['crit'] ?? '') . " | ";
echo "" . htmlspecialchars($row['dpt'] ?? '') . " | ";
// Use helper function for status columns
echo renderStatusCellWithDate($row['AD'], $row['ADlu'], 45);
echo renderStatusCellWithDate($row['GLPI'], $row['GLPIlu'], 7);
echo renderStatusCellWithDate($row['SCCM'], $row['SCCMlu'], 7);
// NESSUS (EPO) Cell - Custom logic
if (str_contains($row['EPO'] ?? '', '.')) {
echo "" . htmlspecialchars($row['EPO']) . " | ";
} elseif (isset($row['EPO']) && !in_array($row['EPO'], ['Y', 'N'])) {
echo "" . htmlspecialchars($row['EPO']) . " | ";
} else {
echo "Missing | ";
$counters['nNESSUS']++;
}
// BACKUP Cell - Custom logic for NBU/DPM
if ($row['NBU'] === 'Y') {
echo renderStatusCellWithDate($row['NBU'], $row['NBUlu'], 30, 'OK (NBU)');
} elseif ($row['DPM'] === 'Y') {
echo renderStatusCellWithDate($row['DPM'], $row['DPMlu'], 30, 'OK (DPM)');
} else {
// Handle non-Y cases for NBU or DPM, or missing
$backupStatus = $row['NBU'] ?? $row['DPM'] ?? null;
echo renderStatusCellWithDate($backupStatus, null, 30);
}
// SCOM Cell - Simple logic
if ($row['SCOM'] === 'Y') {
echo "OK | ";
} elseif (isset($row['SCOM'])) {
echo "" . htmlspecialchars($row['SCOM']) . " | ";
} else {
echo "Missing | ";
}
// Zabbix Cell - Simple logic
$zabbixStatus = $row['zabbix'] ?? '';
if ($zabbixStatus === 'Y' || str_contains($zabbixStatus, '.')) {
echo "" . htmlspecialchars(str_replace('Y', 'OK', $zabbixStatus)) . " | ";
} elseif (isset($row['zabbix'])) {
echo "" . htmlspecialchars($zabbixStatus) . " | ";
} else {
echo "Missing | ";
}
// S1 Cell
$s1Status = $row['S1'] ?? '';
$s1Text = ($s1Status === 'Y' || str_contains($s1Status, '.')) ? str_replace('Y', 'OK', $s1Status) : 'OK';
echo renderStatusCellWithDate($s1Status, $row['S1lu'], 7, $s1Text);
echo "
";
}
// You can now use the $counters array to pass data to your JS for the KPIs
// For example:
echo "";
$stmt->close();
$conn->close();
?>