Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions includes/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ function monitorGetSelectedEvents(): array {
$keys = explode(',', (string) $raw);
}

$keys = array_map('trim', $keys);
$keys = array_map(trim(...), $keys);

return array_values(array_intersect($keys, array_keys($monitor_events)));
}
Expand All @@ -494,11 +494,9 @@ function monitorGetSelectedTemplates(): array {
$keys = explode(',', (string) $raw);
}

$keys = array_filter(array_map('trim', $keys), static function ($k) {
return ctype_digit((string) $k);
});
$keys = array_filter(array_map(trim(...), $keys), static fn($k) => ctype_digit((string) $k));

return array_values(array_map('strval', array_map('intval', $keys)));
return array_values(array_map(strval(...), array_map(intval(...), $keys)));
}

/**
Expand Down Expand Up @@ -1637,7 +1635,7 @@ function monitorPanelServiceChecks(array $host, array $config): string {
$rows = '';

foreach ($tests as $t) {
$never_run = ($t['check_time'] == '' || substr((string) $t['check_time'], 0, 4) === '0000');
$never_run = ($t['check_time'] == '' || str_starts_with((string) $t['check_time'], '0000'));

if ($t['enabled'] == '') {
$cls = 'deviceUnmonitored';
Expand Down
12 changes: 5 additions & 7 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ function monitorEventFilterSql(array $events): string {
return '';
}

$thold_ids = (isset($thold_hosts) && cacti_sizeof($thold_hosts)) ? implode(',', array_map('intval', $thold_hosts)) : '0';
$servcheck_ids = (isset($servcheck_hosts) && cacti_sizeof($servcheck_hosts)) ? implode(',', array_map('intval', $servcheck_hosts)) : '0';
$muted = (isset($_SESSION['monitor_muted_hosts']) && cacti_sizeof($_SESSION['monitor_muted_hosts'])) ? implode(',', array_map('intval', $_SESSION['monitor_muted_hosts'])) : '0';
$thold_ids = (isset($thold_hosts) && cacti_sizeof($thold_hosts)) ? implode(',', array_map(intval(...), $thold_hosts)) : '0';
$servcheck_ids = (isset($servcheck_hosts) && cacti_sizeof($servcheck_hosts)) ? implode(',', array_map(intval(...), $servcheck_hosts)) : '0';
$muted = (isset($_SESSION['monitor_muted_hosts']) && cacti_sizeof($_SESSION['monitor_muted_hosts'])) ? implode(',', array_map(intval(...), $_SESSION['monitor_muted_hosts'])) : '0';

$map = [
'down' => "(h.status = 1 AND h.id NOT IN ($muted))",
Expand Down Expand Up @@ -387,9 +387,7 @@ function renderWhereJoin(string &$sql_where, string &$sql_join): void {
$tpls = explode(',', (string) $tpl_raw);
}

$tpls = array_map('intval', array_filter(array_map('trim', $tpls), static function ($t) {
return ctype_digit((string) $t);
}));
$tpls = array_map(intval(...), array_filter(array_map(trim(...), $tpls), static fn($t) => ctype_digit((string) $t)));

if (cacti_sizeof($tpls)) {
$awhere .= ($awhere == '' ? '' : ' AND ') . 'h.host_template_id IN (' . implode(',', $tpls) . ')';
Expand All @@ -404,7 +402,7 @@ function renderWhereJoin(string &$sql_where, string &$sql_join): void {
} elseif ($events_raw === '' || $events_raw === null) {
$events = [];
} else {
$events = array_map('trim', explode(',', (string) $events_raw));
$events = array_map(trim(...), explode(',', (string) $events_raw));
}

$event_sql = monitorEventFilterSql($events);
Expand Down
16 changes: 4 additions & 12 deletions includes/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,7 @@ function renderSite(): string {
foreach ($hosts as $host) {
$gid = (string) $host['site_id'];

if (!isset($groups[$gid])) {
$groups[$gid] = ['label' => $host['site_name'], 'hosts' => []];
}
$groups[$gid] ??= ['label' => $host['site_name'], 'hosts' => []];

$groups[$gid]['hosts'][] = $host;
}
Expand Down Expand Up @@ -450,9 +448,7 @@ function renderTemplate(): string {
$gid = (string) $host['host_template_id'];
$label = $host['host_template_name'] != '' ? $host['host_template_name'] : __('Non-Templated Devices', 'monitor');

if (!isset($groups[$gid])) {
$groups[$gid] = ['label' => $label, 'hosts' => []];
}
$groups[$gid] ??= ['label' => $label, 'hosts' => []];

$groups[$gid]['hosts'][] = $host;
}
Expand Down Expand Up @@ -735,9 +731,7 @@ function monitorRenderTreeCards(array $tree_list): string {
foreach ($hosts as $host) {
$gid = 'tree_' . $host['graph_tree_id'];

if (!isset($groups[$gid])) {
$groups[$gid] = ['label' => $host['graph_tree_name'], 'hosts' => []];
}
$groups[$gid] ??= ['label' => $host['graph_tree_name'], 'hosts' => []];

$groups[$gid]['hosts'][] = $host;
}
Expand Down Expand Up @@ -778,9 +772,7 @@ function monitorRenderTreeCards(array $tree_list): string {

// Default-order the tree cards by device count (busiest first); any saved
// drag order the user set still wins in monitorRenderGroupCards().
uasort($groups, static function (array $a, array $b): int {
return cacti_sizeof($b['hosts']) <=> cacti_sizeof($a['hosts']);
});
uasort($groups, static fn(array $a, array $b): int => cacti_sizeof($b['hosts']) <=> cacti_sizeof($a['hosts']));

return monitorRenderGroupCards('tree', $groups, $maxlen);
}
Expand Down
4 changes: 1 addition & 3 deletions monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@

$_SESSION['names'] = 0;

if (!isset($_SESSION['monitor_muted_hosts'])) {
$_SESSION['monitor_muted_hosts'] = [];
}
$_SESSION['monitor_muted_hosts'] ??= [];

include_once __DIR__ . '/includes/functions.php';
include_once __DIR__ . '/includes/render.php';
Expand Down
6 changes: 3 additions & 3 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function monitor_check_upgrade() {

$info = plugin_monitor_version();
$current = $info['version'];
$old = db_fetch_cell_prepared('SELECT version FROM plugin_config WHERE directory = ?', array('monitor'));
$old = db_fetch_cell_prepared('SELECT version FROM plugin_config WHERE directory = ?', ['monitor']);

if ($current != $old) {
monitor_setup_table();
Expand Down Expand Up @@ -484,7 +484,7 @@ function monitor_device_action_execute($action) {
* @return array The unmodified $devices array.
*/
function monitor_device_remove($devices) {
$ids = array_map('intval', array_values($devices));
$ids = array_map(intval(...), array_values($devices));

if (cacti_sizeof($ids)) {
$in = implode(',', array_fill(0, count($ids), '?'));
Expand Down Expand Up @@ -1460,7 +1460,7 @@ function monitor_prune_files(): void {
$whitelist = isset($manifest['whitelist']) && is_array($manifest['whitelist']) ? $manifest['whitelist'] : [];

$protected = function (string $rel) use ($whitelist): bool {
if (strncmp($rel, '.git', 4) === 0 || strncmp($rel, '.md', 3) === 0) {
if (str_starts_with($rel, '.git') || str_starts_with($rel, '.md')) {
return true;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/MonitorRequestActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@
expect($contents)->toContain('switch (monitorRequestAction()) {')
->and($contents)->not->toContain("switch (get_nfilter_request_var('action'))");
});

it('keeps registered status filters in request order', function () {
$previous = $GLOBALS['monitor_events'] ?? null;
$GLOBALS['monitor_events'] = ['down' => 'Down', 'up' => 'Up'];
try {
test_set_request(['events' => ' up , unknown,down ']);
expect(monitorGetSelectedEvents())->toBe(['up', 'down']);
} finally {
$GLOBALS['monitor_events'] = $previous;
}
});

it('normalizes numeric device template filters', function ($raw, array $expected) {
test_set_request(['template' => $raw]);
expect(monitorGetSelectedTemplates())->toBe($expected);
})->with([
'comma separated' => [' 03,none, 5, -1', ['3', '5']],
'multiple selection' => [[' 1', '2 ', ''], ['1', '2']],
'all templates' => ['', []],
]);