Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification
- dev: Pin the CI database image and shared test dependencies, disable persisted checkout credentials, and limit workflow permissions and runtime

- issue#96: Include hosts from every selected tree in tree exports, without pulling in non-device graphs or truncating long host lists

--- 2.1 ---

- security: Replace the confirmation pages' inline `onClick='cactiReturnTo()'` Cancel buttons with the CSP-safe `cactiReturnTo` class so the pages no longer trip Cacti's Content-Security-Policy script-src-attr directive
Expand Down
34 changes: 30 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,35 @@ function check_system_paths(&$export, $export_path) {
}
}

/**
* Gets the host ids associated with the selected trees.
*
* Graph and header tree items carry host_id 0, which would pull every
* non-device graph into the export, so only device items are used. The ids
* are fetched as rows rather than with GROUP_CONCAT(), which truncates at
* group_concat_max_len and can leave a partial id at the end of the list.
*
* @param array $tree_ids The selected graph_tree_items.graph_tree_id values.
*
* @return string The matching host ids as a comma-separated list, or ''
* when none exist.
*/
function gexport_get_tree_hosts($tree_ids) {
$placeholders = implode(', ', array_fill(0, cacti_sizeof($tree_ids), '?'));

$rows = db_fetch_assoc_prepared('SELECT DISTINCT host_id
FROM graph_tree_items
WHERE graph_tree_id IN(' . $placeholders . ')
AND host_id > 0',
array_values($tree_ids));

if (!cacti_sizeof($rows)) {
return '';
}

return implode(',', array_map('intval', array_column($rows, 'host_id')));
}

/**
* This function exports all the graphs and some html for mgtg view data.
* These are all the graphs that are in scope for the export be it a tree
Expand Down Expand Up @@ -1027,10 +1056,7 @@ function export_graphs(&$export, $export_path) {

export_debug('There are ' . cacti_sizeof($graphs) . ' graphs not in hosts to export for all trees');

$hosts = db_fetch_cell_prepared('SELECT GROUP_CONCAT(DISTINCT host_id)
FROM graph_tree_items
WHERE graph_tree_id IN(?)',
[implode(', ', $ntree)]);
$hosts = gexport_get_tree_hosts($ntree);

export_debug('There are ' . cacti_sizeof(explode(',',$hosts)) . ' hosts to export for all trees');

Expand Down
112 changes: 112 additions & 0 deletions tests/Unit/GexportTreeHostSelectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/

beforeAll(function () {
require_once __DIR__ . '/../../includes/functions.php';
});

beforeEach(function () {
gexport_test_reset_db_mocks();
$GLOBALS['__test_allowed_trees'] = [];
$GLOBALS['__test_allowed_graphs'] = [];
$GLOBALS['__test_allowed_graph_calls'] = [];
});

it('binds every selected tree id when looking up their hosts', function () {
$query = null;
$params = null;

gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) use (&$query, &$params) {
$query = $sql;
$params = $bound;

return true;
}, [['host_id' => '10'], ['host_id' => '20']]);

expect(gexport_get_tree_hosts([3, 8]))->toBe('10,20')
->and(preg_replace('/\s+/', ' ', trim($query)))
->toBe(
'SELECT DISTINCT host_id ' .
'FROM graph_tree_items WHERE graph_tree_id IN(?, ?) AND host_id > 0'
)
->and($params)->toBe([3, 8]);
});

it('binds one tree id with a single placeholder', function () {
$query = null;
$params = null;

gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) use (&$query, &$params) {
$query = $sql;
$params = $bound;

return true;
}, [['host_id' => '10']]);

expect(gexport_get_tree_hosts([3]))->toBe('10')
->and(preg_replace('/\s+/', ' ', trim($query)))
->toBe(
'SELECT DISTINCT host_id ' .
'FROM graph_tree_items WHERE graph_tree_id IN(?) AND host_id > 0'
)
->and($params)->toBe([3]);
});

it('returns an empty host list when the selected trees hold no devices', function () {
gexport_test_mock_db('db_fetch_assoc_prepared', 'FROM graph_tree_items', []);

expect(gexport_get_tree_hosts([3]))->toBe('');
});

it('keeps every host id intact when the list is long', function () {
// GROUP_CONCAT() stops at group_concat_max_len (1024 bytes by default).
$rows = [];

for ($id = 100000; $id < 100400; $id++) {
$rows[] = ['host_id' => (string) $id];
}

gexport_test_mock_db('db_fetch_assoc_prepared', 'FROM graph_tree_items', $rows);

$hosts = explode(',', gexport_get_tree_hosts([3]));

expect($hosts)->toHaveCount(400)
->and(end($hosts))->toBe('100399');
});

it('uses hosts from all selected trees when finding exportable graphs', function () {
$GLOBALS['__test_allowed_trees'] = [
['id' => 3, 'name' => 'Tree 3'],
['id' => 8, 'name' => 'Tree 8'],
];

gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) {
return strpos($sql, 'FROM graph_tree_items') !== false && $bound === [3, 8];
}, [['host_id' => '10'], ['host_id' => '20']]);

$export = [
'export_clear' => 'off',
'export_effective_user' => 5,
'graph_tree' => '3,8',
'graph_site' => '0',
'id' => 1,
'export_presentation' => 'tree',
'export_threads' => 0,
'graph_max' => 0,
];
$export_path = sys_get_temp_dir() . '/gexport-tree-hosts-' . uniqid();

try {
expect(export_graphs($export, $export_path))->toBe(0)
->and($GLOBALS['__test_allowed_graph_calls'][0]['sql_where'])
->toBe('gl.host_id IN(10,20)');
} finally {
delTree($export_path);
}
});
38 changes: 38 additions & 0 deletions tests/bootstrap-unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@

$GLOBALS['__test_db_calls'] = array();
$GLOBALS['__test_db_fixtures'] = array();
$GLOBALS['__test_allowed_trees'] = [];
$GLOBALS['__test_allowed_graphs'] = [];
$GLOBALS['__test_allowed_graph_calls'] = [];

if (!function_exists('gexport_test_mock_db')) {
function gexport_test_mock_db($fn, $match, $result) {
Expand Down Expand Up @@ -328,6 +331,37 @@ function cacti_log($message, $also_print = false, $log_type = '', $level = 0) {
}
}

if (!function_exists('get_allowed_trees')) {
function get_allowed_trees($edit = false, $return_sql = false, $sql_where = '', $sql_order = 'name', $sql_limit = '', &$total_rows = 0, $user_id = 0) {
return $GLOBALS['__test_allowed_trees'];
}
}

if (!function_exists('get_allowed_graphs')) {
function get_allowed_graphs($sql_where = '', $sql_order = 'gtg.title_cache', $sql_limit = '', &$total_rows = 0, $user_id = 0, $graph_id = 0) {
$GLOBALS['__test_allowed_graph_calls'][] = [
'sql_where' => $sql_where,
'user_id' => $user_id,
];

return $GLOBALS['__test_allowed_graphs'];
}
}

if (!function_exists('array_rekey')) {
function array_rekey($rows, $key, $value) {
$rekeyed = [];

if (is_array($rows)) {
foreach ($rows as $row) {
$rekeyed[$row[$key]] = $row[$value];
}
}

return $rekeyed;
}
}

if (!function_exists('cacti_sizeof')) {
function cacti_sizeof($array) {
return is_array($array) ? count($array) : 0;
Expand Down Expand Up @@ -389,6 +423,10 @@ function sql_save($array, $table, $key = 'id') {
define('POLLER_VERBOSITY_LOW', 2);
}

if (!defined('POLLER_VERBOSITY_HIGH')) {
define('POLLER_VERBOSITY_HIGH', 4);
}

if (!defined('POLLER_VERBOSITY_MEDIUM')) {
define('POLLER_VERBOSITY_MEDIUM', 3);
}
Expand Down
Loading