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
16 changes: 6 additions & 10 deletions monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@

include_once __DIR__ . '/db_functions.php';
include_once __DIR__ . '/monitor_render.php';
include_once __DIR__ . '/monitor_helpers.php';
include_once __DIR__ . '/monitor_controller.php';

validateRequestVars();
Expand All @@ -142,28 +143,23 @@

break;
case 'ajax_mute_all':
muteAllHosts();
drawPage();
monitorRunActionAndRender('muteAllHosts');

break;
case 'ajax_unmute_all':
unmuteAllHosts();
drawPage();
monitorRunActionAndRender('unmuteAllHosts');

break;
case 'dbchange':
loadDashboardSettings();
drawPage();
monitorRunActionAndRender('loadDashboardSettings');

break;
case 'remove':
removeDashboard();
drawPage();
monitorRunActionAndRender('removeDashboard');

break;
case 'saveDb':
saveSettings();
drawPage();
monitorRunActionAndRender('saveSettings');

break;
case 'save':
Expand Down
38 changes: 38 additions & 0 deletions monitor_helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
Comment on lines +7 to +12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated in 7d38d68 to use the standard Cacti header block in both new files.

| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/**
* Execute one monitor action callback and then render the monitor page.
*
* @param callable $action Action callback with no arguments.
*
* @return void
*/
function monitorRunActionAndRender(callable $action): void {
$action();
drawPage();
}
66 changes: 66 additions & 0 deletions tests/test_action_render_wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

Comment on lines +1 to +2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated in 7d38d68 to use the standard Cacti header block in both new files.

/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
*/

require_once __DIR__ . '/../monitor_helpers.php';

$events = [];

function drawPage(): void {
global $events;
$events[] = 'render';
}

function monitor_test_action(): void {
global $events;
$events[] = 'action';
}

function assert_same($expected, $actual, string $message): void {
if ($expected !== $actual) {
fwrite(STDERR, $message . PHP_EOL);
fwrite(STDERR, 'Expected: ' . json_encode($expected) . PHP_EOL);
fwrite(STDERR, 'Actual: ' . json_encode($actual) . PHP_EOL);
exit(1);
}
}

function assert_regex(string $pattern, string $subject, string $message): void {
if (!preg_match($pattern, $subject)) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
}

monitorRunActionAndRender('monitor_test_action');
assert_same(['action', 'render'], $events, 'Wrapper should run action before render.');

$source = file_get_contents(__DIR__ . '/../monitor.php');
if ($source === false) {
fwrite(STDERR, "Unable to read monitor.php\n");
exit(1);
}

$expected_patterns = [
"/include_once\\s+__DIR__\\s*\\.\\s*'\\/monitor_helpers\\.php'\\s*;/",
"/monitorRunActionAndRender\\(\\s*'muteAllHosts'\\s*\\)\\s*;/",
"/monitorRunActionAndRender\\(\\s*'unmuteAllHosts'\\s*\\)\\s*;/",
"/monitorRunActionAndRender\\(\\s*'loadDashboardSettings'\\s*\\)\\s*;/",
"/monitorRunActionAndRender\\(\\s*'removeDashboard'\\s*\\)\\s*;/",
"/monitorRunActionAndRender\\(\\s*'saveSettings'\\s*\\)\\s*;/"
];

foreach ($expected_patterns as $expected_pattern) {
assert_regex($expected_pattern, $source, "Expected monitor.php to match pattern: $expected_pattern");
}

echo "OK\n";