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
44 changes: 44 additions & 0 deletions .github/workflows/plugin-ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ permissions:

env:
CACTI: 1.2.x
FLOWVIEW_E2E_SCHEME: http
FLOWVIEW_E2E_HOST: 127.0.0.1:8080
FLOWVIEW_E2E_DB_HOST: 127.0.0.1
FLOWVIEW_E2E_DB_NAME: cacti
FLOWVIEW_E2E_DB_USER: cactiuser
FLOWVIEW_E2E_DB_PASS: cactiuser

concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
Expand Down Expand Up @@ -201,6 +207,44 @@ jobs:
"$CACTI_PEST_BIN" --configuration=plugins/flowview/phpunit.xml \
--coverage-clover=plugins/flowview/coverage/clover.xml

# The end-to-end tests send real requests to the FlowView pages, so serve
# the installed Cacti with PHP's built-in server as the web user.
- name: Serve Cacti for the end-to-end tests
run: |
p="${{ github.workspace }}"
while [ "$p" != "/" ]; do
sudo chmod o+x "$p" || true
p=$(dirname "$p")
done

cd ${{ github.workspace }}/cacti
# The CLI installer creates the log as root. Both test users initialize Cacti.
sudo chown runner:www-data log/cacti.log
sudo chmod 664 log/cacti.log
sudo sed -i "s#^\$url_path = .*#\$url_path = '/';#" include/config.php
sudo -u www-data nohup php -S "$FLOWVIEW_E2E_HOST" -t ${{ github.workspace }}/cacti > "${{ runner.temp }}/cacti-web.log" 2>&1 &

for i in $(seq 1 30); do
if curl -fsS --max-time 5 "$FLOWVIEW_E2E_SCHEME://$FLOWVIEW_E2E_HOST/index.php" -o "${{ runner.temp }}/flowview-login.html" &&
grep -Fq "name='login_username'" "${{ runner.temp }}/flowview-login.html"; then
exit 0
fi
sleep 1
done

cat "${{ runner.temp }}/cacti-web.log"
exit 1

# tests/Integration is not in phpunit.xml's test suites, so the unit run
# above leaves it out and it is named here.
- name: Run Pest End-to-End Tests
env:
FLOWVIEW_E2E_CACTI: ${{ github.workspace }}/cacti
run: |
cd ${{ github.workspace }}/cacti
"$CACTI_PEST_BIN" --configuration=plugins/flowview/phpunit.xml \
--no-coverage plugins/flowview/tests/Integration

# Whole-file coverage is meaningless here: most of the plugin only runs
# inside a live Cacti. What is enforceable is that a change covers the
# lines it adds.
Expand Down
2 changes: 1 addition & 1 deletion flowview_bulkarin.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@

if ($origin == '') {
if (file_exists($whois_path) && is_executable($whois_path) && $whois_provider != '') {
$last_line = exec("$whois_path -h $whois_provider $cidr | grep 'origin:' | head -1 | awk -F':' '{print \$2}'", $output, $return_var);
$last_line = exec(cacti_escapeshellcmd($whois_path) . ' -h ' . cacti_escapeshellarg($whois_provider) . ' ' . cacti_escapeshellarg($cidr) . " | grep 'origin:' | head -1 | awk -F':' '{print \$2}'", $output, $return_var);

/* attempt to prevent rate limiting */
sleep(1);
Expand Down
140 changes: 11 additions & 129 deletions flowview_databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
require('./lib/utility.php');
require_once('./plugins/flowview/includes/functions.php');
require_once('./plugins/flowview/includes/database.php');
require_once(__DIR__ . '/includes/database_details.php');

flowview_connect();

Expand Down Expand Up @@ -70,126 +71,7 @@ function flowview_get_item_details(): bool {

require($config['base_path'] . '/plugins/flowview/includes/arrays.php');

$cols = get_all_columns();
$tab = get_nfilter_request_var('tab');
$ids = json_decode(base64_decode(str_replace('line_', '', get_request_var('id'))), true);
$table = "plugin_flowview_irr_$tab";

$sql_where = '';
$sql_params = [];

if (cacti_sizeof($ids) && flowview_db_table_exists($table)) {
foreach($ids as $col => $value) {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . "$col = ?";
$sql_params[] = $value;
}
} else {
return false;
}

$details = flowview_db_fetch_row_prepared("SELECT *
FROM $table
$sql_where
LIMIT 1", $sql_params);

ob_start();

$mnt_by_present = false;

print "<div style='height:350px;overflow-y:scroll'>";

html_start_box(__('Internet Route Registry Details', 'flowview'), '100%', false, 3, 'center', '');

$response = flowview_print_details($cols, $details);

html_end_box();

if ($response['mnt_by_present'] == true) {
$maintainers = preg_split('/[\s]+/', trim($response['mnt_by'])) ?: [];

foreach($maintainers as $m) {
$sql_params = [];
$sql_params[] = $m;
$sql_params[] = $response['mnt_by_source'];

$details = flowview_db_fetch_row_prepared("SELECT *
FROM plugin_flowview_irr_mntner
WHERE mntner = ?
AND source = ?
LIMIT 1", $sql_params);

if (cacti_sizeof($details)) {
html_start_box(__('Authorized Agent Details', 'flowview'), '100%', false, 3, 'center', '');

flowview_print_details($cols, $details);

html_end_box();
}
}
}

print "</div>";

ob_get_flush();
return true;
}

/**
* @param mixed $cols
* @param mixed $details
*
* @return array
*/
function flowview_print_details(&$cols, &$details): array {
$mnt_by_present = false;
$mnt_by = '';
$mnt_by_source = '';

if (cacti_sizeof($details)) {
foreach($details as $column => $value) {
if ($column == 'mnt_by' && $value != '') {
$mnt_by_present = true;
$mnt_by = $value;
}

if ($column == 'source') {
$mnt_by_source = $value;
}

/* get rid of some nagging hyphens */
$value = str_replace("------------------------------------------\n", '', $value);

if ($value != '' && $value != '0000-00-00 00:00:00' && $column != 'present') {
print '<tr>';

if ($column == 'as_set') {
$value = strtoupper($value);
}

if (isset($cols[$column])) {
print '<td style="width:18%;font-weight:bold;vertical-align:text-top">' . html_escape($cols[$column]['display']) . ':</td>';
} else {
print '<td style="width:18%;font-weight:bold;vertical-align:text-top">' . html_escape(ucfirst(str_replace('_', ' ', $column))) . ':</td>';
}

if ($column == 'remarks' ||
$column == 'import' ||
$column == 'export' ||
$column == 'mp_import' ||
$column == 'mp_export') {
print '<td style="width:82%">' . str_replace("\n", '<br>', html_escape($value)) . '</td>';
} else {
print '<td style="width:82%;white-space:pre-wrap">' . str_replace("\n", ', ', html_escape($value)) . '</td>';
}

print '</tr>';
}
}
} else {
print '<tr><td><em>' . __('No Details Found', 'flowview') . '</em></td></tr>';
}

return ['mnt_by_present' => $mnt_by_present, 'mnt_by' => $mnt_by, 'mnt_by_source' => $mnt_by_source];
return flowview_render_item_details(get_all_columns());
}

/**
Expand Down Expand Up @@ -958,7 +840,7 @@ function view_db_table($tab, &$tabs): void {

if (cacti_sizeof($sources) > 0) {
foreach ($sources as $key => $value) {
print "<option value='" . $key . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
print "<option value='" . html_escape($key) . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
}
}
?>
Expand Down Expand Up @@ -1140,9 +1022,9 @@ function() {
$value = $result[$c];
}

form_selectable_cell($value, $i, '', $align);
form_selectable_cell(html_escape($value), $i, '', $align);
} else {
form_selectable_cell($result[$c], $i, '', $align);
form_selectable_cell(html_escape($result[$c]), $i, '', $align);
}
}
}
Expand Down Expand Up @@ -1339,7 +1221,7 @@ function view_dns_cache(): void {

if (cacti_sizeof($sources) > 0) {
foreach ($sources as $key => $value) {
print "<option value='" . $key . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
print "<option value='" . html_escape($key) . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
}
}
?>
Expand Down Expand Up @@ -1526,10 +1408,10 @@ function purgeFilter() {
form_alternate_row('line' . $l['id'], false);
form_selectable_cell(filter_value($l['ip'], get_request_var('filter')), $l['id']);
form_selectable_cell(filter_value($l['host'], get_request_var('filter')), $l['id']);
form_selectable_cell($l['source'], $l['id']);
form_selectable_cell(html_escape($l['source']), $l['id']);
form_selectable_cell($l['arin_verified'] == 1 ? __('Verified', 'flowview'):__('Unverified', 'flowview'), $l['id']);
form_selectable_cell($l['arin_id'], $l['id'], '', 'right');
form_selectable_cell($l['origin'], $l['id'], '', 'right');
form_selectable_cell(html_escape($l['arin_id']), $l['id'], '', 'right');
form_selectable_cell(html_escape($l['origin']), $l['id'], '', 'right');
form_selectable_cell(date('Y-m-d H:i:s', $l['time']), $l['id'], '', 'right');
form_checkbox_cell($l['host'], $l['id']);
form_end_row();
Expand Down Expand Up @@ -1644,7 +1526,7 @@ function view_routes($tab): void {

if (cacti_sizeof($sources)) {
foreach ($sources as $key => $value) {
print "<option value='" . $key . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
print "<option value='" . html_escape($key) . "'" . (get_request_var('source') == $key ? ' selected':'') . '>' . html_escape($value) . '</option>';
}
}
?>
Expand Down Expand Up @@ -1901,7 +1783,7 @@ function() {
form_selectable_cell(filter_value($l['descr'], get_request_var('filter')), $rid);
form_selectable_cell(filter_value($l['source'], get_request_var('filter')), $rid);
form_selectable_cell(filter_value($l['mnt_by'], get_request_var('filter')), $rid);
form_selectable_cell($l['last_modified'], $rid, '', 'right');
form_selectable_cell(html_escape($l['last_modified']), $rid, '', 'right');
form_end_row();
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions flowview_filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ function actions_filters(): void {
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') {
for ($i=0; $i<cacti_count($selected_items); $i++) {
flowview_db_execute('DELETE FROM plugin_flowview_queries WHERE id = ' . $selected_items[$i]);
flowview_db_execute_prepared('DELETE FROM plugin_flowview_queries WHERE id = ?', [$selected_items[$i]]);
}
} elseif (get_nfilter_request_var('drp_action') == '3') {
for ($i=0; $i<cacti_count($selected_items); $i++) {
flowview_db_execute("UPDATE plugin_flowview_queries SET enabled='' WHERE id = " . $selected_items[$i]);
flowview_db_execute_prepared("UPDATE plugin_flowview_queries SET enabled='' WHERE id = ?", [$selected_items[$i]]);
}
} elseif (get_nfilter_request_var('drp_action') == '4') {
for ($i=0; $i<cacti_count($selected_items); $i++) {
flowview_db_execute("UPDATE plugin_flowview_queries SET enabled='on' WHERE id = " . $selected_items[$i]);
flowview_db_execute_prepared("UPDATE plugin_flowview_queries SET enabled='on' WHERE id = ?", [$selected_items[$i]]);
}
} elseif (get_nfilter_request_var('drp_action') == '5') {
$started = flowview_launch_materialize($selected_items);
Expand Down Expand Up @@ -180,8 +180,8 @@ function actions_filters(): void {
input_validate_input_number($matches[1]);
/* ==================================================== */

$filter_list .= '<li>' . flowview_db_fetch_cell_prepared('SELECT name FROM plugin_flowview_queries
WHERE id = ?', [$matches[1]]) . '</li>';
$filter_list .= '<li>' . html_escape(flowview_db_fetch_cell_prepared('SELECT name FROM plugin_flowview_queries
WHERE id = ?', [$matches[1]])) . '</li>';
$filter_array[] = $matches[1];
}
}
Expand Down Expand Up @@ -501,7 +501,7 @@ function clearFilter() {
if (empty($filter['device'])) {
form_selectable_cell(__('All', 'flowview'), $filter['id']);
} else {
form_selectable_cell($filter['device'], $filter['id']);
form_selectable_cell(html_escape($filter['device']), $filter['id']);
}

if ($filter['statistics'] > 0) {
Expand Down
13 changes: 9 additions & 4 deletions flowview_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,21 @@
);

if (cacti_sizeof($queued_reports)) {
$sql_where = ' AND fs.id NOT IN (' . implode(', ', $queued_reports) . ')';
$sql_where = ' AND fs.id NOT IN (' . implode(', ', array_fill(0, cacti_sizeof($queued_reports), '?')) . ')';
$sql_params = array_values($queued_reports);
} else {
$sql_where = '';
$sql_where = '';
$sql_params = [];
}

$schedules = flowview_db_fetch_assoc("SELECT fs.*
$sql_params[] = $t;

$schedules = flowview_db_fetch_assoc_prepared("SELECT fs.*
FROM plugin_flowview_schedules AS fs
WHERE enabled = 'on'
$sql_where
AND ($t - sendinterval > lastsent)");
AND (? - sendinterval > lastsent)",
$sql_params);

$php = read_config_option('path_php_binary');

Expand Down
2 changes: 1 addition & 1 deletion flowview_upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ function flowview_upgrade($current, $old): void {
flowview_db_execute("UPDATE plugin_flowview_schedules SET title='Ugraded Schedule' WHERE title=''");

/* Set the new version */
db_execute_prepared("REPLACE INTO settings (name, value) VALUES ('plugin_flowview_version', ?)", [$current]);
set_config_option('plugin_flowview_version', $current);

flowview_db_execute('ALTER TABLE plugin_flowview_devices ENGINE=InnoDB');
}
Expand Down
Loading