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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ installation guidance and source-checked behavior.

--- 3.0 ---

* issue#292: Use the `'monitor'` gettext text domain for the `monitor_list` setting labels in `setup.php` instead of `'thold'`, pass it to the Criticality label in `setup.php` that had no domain, add a test that every translation helper call passes the plugin text domain, refresh `locales/po/cacti.pot` to match current PHP source strings, and remove orphan `locales/LC_MESSAGES/ka.mo`
* security: Require POST for the save, remove, mute and card-order actions so csrf-magic covers them, and only let a dashboard's owner save over it
* security: Bind the plugin version lookup and the device_remove cleanup deletes as prepared statements instead of building them from the device list
* bug: Escape the device description, location, site name and Monitor Text in the List view
Expand Down
Binary file removed locales/LC_MESSAGES/ka.mo
Binary file not shown.
7 changes: 5 additions & 2 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,11 @@ msgid "Any"
msgstr ""

#: setup.php
#, php-format
msgid "Click 'Continue' to %s monitoring on these Device(s)"
msgid "Click 'Continue' to enable monitoring on these Device(s)"
msgstr ""

#: setup.php
msgid "Click 'Continue' to disable monitoring on these Device(s)"
msgstr ""

#: setup.php
Expand Down
6 changes: 3 additions & 3 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function monitor_device_table_bottom() {
];

if (version_compare(CACTI_VERSION, '1.3.0', '<')) {
$select = '<td>' . __('Criticality') . '</td><td><select id="criticality">';
$select = '<td>' . __('Criticality', 'monitor') . '</td><td><select id="criticality">';

foreach ($criticalities as $index => $crit) {
if ($index == get_request_var('criticality')) {
Expand Down Expand Up @@ -939,8 +939,8 @@ function monitor_config_settings() {
'max_length' => '255'
],
'monitor_list' => [
'friendly_name' => __('Notification List', 'thold'),
'description' => __('Select a Notification List below. All Emails subscribed to the notification list will be notified.', 'thold'),
'friendly_name' => __('Notification List', 'monitor'),
'description' => __('Select a Notification List below. All Emails subscribed to the notification list will be notified.', 'monitor'),
'method' => 'drop_sql',
'sql' => 'SELECT id, name FROM plugin_notification_lists ORDER BY name',
'default' => '',
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/I18nTextDomainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/*
+-------------------------------------------------------------------------+
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+

/*
* Every translation helper call in the plugin's own PHP files must pass the
* 'monitor' text domain, so its strings are looked up in this plugin's catalog
* and not silently in Cacti's core one. The scan itself lives in
* tests/bin/check-i18n-domain.php; it uses PHP's tokenizer and needs no Cacti
* checkout.
*/

require_once __DIR__ . '/../bin/check-i18n-domain.php';

it('passes the monitor text domain to every translation helper call', function () {
/*
* Calls that already lacked the domain before this check existed, as
* file => number of calls. They are listed so the check passes without
* widening the change that introduced it. The count must match exactly:
* fixing a call without lowering its count here fails the test, so the
* list can only shrink. Do not add entries for new code.
*/
$known = [
// nav bar and list labels with no domain (one is the core 'Devices' label)
'includes/render.php' => 2,
// report e-mail paragraph
'poller_functions.php' => 1,
// core role name passed to auth_augment_roles()
'setup.php' => 1,
];

$found = [];

foreach (i18n_domain_offenders(dirname(__DIR__, 2), 'monitor') as $offender) {
$found[$offender['file']][] = $offender['file'] . ':' . $offender['line'] . ' ' . $offender['call'] . ': ' . $offender['reason'];
}

$unexpected = [];

foreach ($found as $file => $lines) {
if (count($lines) !== ($known[$file] ?? 0)) {
$unexpected = array_merge($unexpected, $lines);
}
}

foreach ($known as $file => $count) {
if (!isset($found[$file])) {
$unexpected[] = $file . ': expected ' . $count . ' known calls without the text domain, found none; remove it from the list';
}
}

expect($unexpected)->toBe([]);
});
40 changes: 40 additions & 0 deletions tests/Unit/MonitorConfigSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for monitor_config_settings() in setup.php. Text domain usage
* is covered for the whole plugin by I18nTextDomainTest.php.
*/

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

beforeEach(function () {
$GLOBALS['__test_request'] = array();
$GLOBALS['__test_config_options'] = array();
$GLOBALS['__test_current_page'] = 'settings.php';
});

it('registers the monitor settings tab including monitor_list', function () {
global $tabs, $formats, $settings, $criticalities, $page_refresh_interval, $settings_user, $tabs_graphs;

$tabs = array();
$formats = array();
$settings = array();
$criticalities = array();
$page_refresh_interval = array(60 => '1 Minute');
$settings_user = array();
$tabs_graphs = array();

monitor_config_settings();

expect($tabs)->toHaveKey('monitor');
expect($settings['monitor'])->toHaveKey('monitor_list');
expect($settings['monitor']['monitor_list']['friendly_name'])->toBe('Notification List');
expect($settings['monitor']['monitor_list']['description'])->toContain('Notification List');
});
223 changes: 223 additions & 0 deletions tests/bin/check-i18n-domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php
/*
+-------------------------------------------------------------------------+
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+

/*
* Find translation helper calls in a plugin's own PHP files that do not pass
* the plugin's gettext text domain.
*
* Cacti's helpers take the domain as an argument and fall back to the core
* 'cacti' domain without it, so a missing or misspelled domain silently makes
* the string untranslatable from the plugin's catalog. Where the domain goes
* (include/global_languages.php in Cacti):
*
* __(), __esc(), __x(), __esc_x() last argument (any number of printf args)
* __n(), __esc_n() 4th argument
* __xn(), __esc_xn() 5th argument
* __gettext() 2nd argument
*
* The files are read with PHP's tokenizer, so strings and comments that merely
* mention a helper are not calls. The domain must be a string literal.
* vendor/, tests/, locales/ and hidden directories are not scanned.
*
* Usage: php tests/bin/check-i18n-domain.php <domain>
*
* Exits 1 and prints file:line for each offender, 0 when there are none.
*/

if (!function_exists('i18n_domain_offenders')) {
/**
* @param string $root Plugin root directory.
* @param string $domain Text domain every helper call must pass.
*
* @return array<int, array{file: string, line: int, call: string, reason: string}>
*/
function i18n_domain_offenders($root, $domain) {
$root = rtrim(realpath($root), '/');
$offenders = [];

foreach (i18n_domain_files($root) as $file) {
$relative = substr($file, strlen($root) + 1);

foreach (i18n_domain_check_source(file_get_contents($file), $domain) as $offender) {
$offender['file'] = $relative;
$offenders[] = $offender;
}
}

return $offenders;
}

/**
* @param string $root Plugin root directory.
*
* @return array<int, string> Absolute paths, sorted.
*/
function i18n_domain_files($root) {
$skip = ['vendor', 'tests', 'locales'];
$files = [];

$iterator = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
function ($current) use ($root, $skip) {
$relative = substr($current->getPathname(), strlen($root) + 1);

if ($current->getFilename()[0] === '.') {
return false;
}

if ($current->isDir()) {
return !in_array($relative, $skip, true);
}

return $current->getExtension() === 'php';
}
)
);

foreach ($iterator as $file) {
$files[] = $file->getPathname();
}

sort($files);

return $files;
}

/**
* @param string $source PHP source code.
* @param string $domain Expected text domain.
*
* @return array<int, array{line: int, call: string, reason: string}>
*/
function i18n_domain_check_source($source, $domain) {
// Position of the domain argument: an index from the start, or -1 for the last argument.
$positions = [
'__' => -1, '__esc' => -1, '__x' => -1, '__esc_x' => -1,
'__n' => 3, '__esc_n' => 3, '__xn' => 4, '__esc_xn' => 4,
'__gettext' => 1,
];

$tokens = array_values(array_filter(
token_get_all($source),
function ($token) {
return !is_array($token) || !in_array($token[0], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT], true);
}
));
$offenders = [];
$count = count($tokens);

for ($i = 0; $i < $count; $i++) {
if (!is_array($tokens[$i]) || !in_array($tokens[$i][0], [T_STRING, T_NAME_FULLY_QUALIFIED], true)) {
continue;
}

$name = ltrim($tokens[$i][1], '\\');

if (!isset($positions[$name]) || ($tokens[$i + 1] ?? null) !== '(') {
continue;
}

// Skip method calls, static calls, declarations and instantiations.
$previous = $tokens[$i - 1] ?? null;

if (is_array($previous) && in_array($previous[0], [T_OBJECT_OPERATOR, T_NULLSAFE_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_NEW], true)) {
continue;
}

$arguments = i18n_domain_arguments($tokens, $i + 1);
$wanted = $positions[$name] === -1 ? count($arguments) - 1 : $positions[$name];
$reason = null;

if (!isset($arguments[$wanted]) || ($positions[$name] === -1 && count($arguments) < 2)) {
$reason = 'no text domain argument';
} elseif (count($arguments[$wanted]) !== 1 || !is_array($arguments[$wanted][0]) || $arguments[$wanted][0][0] !== T_CONSTANT_ENCAPSED_STRING) {
$reason = 'text domain is not a string literal';
} elseif (substr($arguments[$wanted][0][1], 1, -1) !== $domain) {
$reason = 'text domain is ' . $arguments[$wanted][0][1] . ", expected '" . $domain . "'";
}

if ($reason !== null) {
$offenders[] = ['line' => $tokens[$i][2], 'call' => $name . '()', 'reason' => $reason];
}
}

return $offenders;
}

/**
* Split the arguments of the call whose opening parenthesis is at $open.
*
* @param array<int, mixed> $tokens Significant tokens.
* @param int $open Index of the '(' token.
*
* @return array<int, array<int, mixed>> Tokens of each top-level argument.
*/
function i18n_domain_arguments($tokens, $open) {
$arguments = [[]];
$depth = 0;
$count = count($tokens);

for ($i = $open; $i < $count; $i++) {
$token = $tokens[$i];
$text = is_array($token) ? $token[1] : $token;

if ($text === '(' || $text === '[' || $text === '{' || (is_array($token) && in_array($token[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true))) {
$depth++;

if ($depth === 1) {
continue;
}
} elseif ($text === ')' || $text === ']' || $text === '}') {
$depth--;

if ($depth === 0) {
break;
}
} elseif ($text === ',' && $depth === 1) {
$arguments[] = [];

continue;
}

$arguments[count($arguments) - 1][] = $token;
}

// A trailing comma leaves an empty last argument.
if (end($arguments) === []) {
array_pop($arguments);
}

return $arguments;
}
}

if (PHP_SAPI === 'cli' && isset($argv[0]) && realpath($argv[0]) === __FILE__) {
$domain = $argv[1] ?? '';

if ($domain === '') {
fwrite(STDERR, "Usage: php tests/bin/check-i18n-domain.php <domain>\n");

exit(2);
}

$offenders = i18n_domain_offenders(dirname(__DIR__, 2), $domain);

foreach ($offenders as $offender) {
print $offender['file'] . ':' . $offender['line'] . ' ' . $offender['call'] . ': ' . $offender['reason'] . "\n";
}

exit($offenders === [] ? 0 : 1);
}
6 changes: 6 additions & 0 deletions tests/bootstrap-unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ function get_selected_theme() {
}
}

if (!function_exists('get_current_page')) {
function get_current_page() {
return $GLOBALS['__test_current_page'] ?? '';
}
}

if (!function_exists('monitor_test_mock_db')) {
function monitor_test_mock_db($fn, $match, $result) {
$GLOBALS['__test_db_fixtures'][] = array('fn' => $fn, 'match' => $match, 'result' => $result);
Expand Down