Files
cacti/cli/version.php
Thomas VincentandTheWitness fcbcbdd1d7 refactor: replace strpos() boolean checks with str_contains/str_starts_with (#6645)
* refactor: replace strpos() boolean checks with str_contains/str_starts_with

Completes the PHP 8.x string function migration started in lib/.
Converts remaining legacy patterns across 81 files:

- strpos($x, $y) !== false → str_contains($x, $y)
- strpos($x, $y) === false → !str_contains($x, $y)
- strpos($x, $y) != false  → str_contains($x, $y)
- bare if (strpos($x, $y)) → if (str_contains($x, $y))
- substr($x, 0, strlen($y)) == $y → str_starts_with($x, $y)

Intentionally preserves strpos() calls that use the return value as
an integer offset:
- user_admin.php / user_group_admin.php (4 calls, ternary for position)
- auth_profile.php (2 calls, offset for substr); guards fixed from
  bare if ($pos) to if ($pos !== false)

Behavior fix: ss_esxi_vhosts.php:55 bare strpos() missed position-0
matches; str_contains() now correctly detects 'not installed' at any
position. Adjacent line already used str_contains().

Ref: https://github.com/Cacti/cacti/issues/6641

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: correct dead-code elseif in plugin_display_compat() version comparison

The if/elseif chain in plugin_display_compat() checked >=, >=, >, <
— the second >= was unreachable dead code. Corrected to <= to complete
the operator set (>=, <=, >, <).

Without this fix, <= constraints (e.g. "<=1.2") fell through to the <
branch and were rendered as "< =1.2" instead of "<= 1.2".

This pre-existing bug was exposed when converting strpos() to
str_contains(), which made PHPStan detect the always-false condition.

Ref: https://github.com/Cacti/cacti/issues/6641

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: add limit to explode('=') in CLI argument parsers

17 CLI scripts used explode('=', $parameter) without a limit of 2,
silently truncating values containing = (e.g. --note=a=b parsed as
$value='a' instead of 'a=b'). The other 40 CLI scripts already had
the correct limit.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-02-25 11:38:59 -05:00

230 lines
6.0 KiB
PHP
Executable File

#!/usr/bin/env php
<?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. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty or |
| 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/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
// setup defaults
$quiet = false;
$debug = false;
$dev = false;
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (str_contains($parameter, '=')) {
[$arg, $value] = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--dev':
case '-d':
$dev = true;
break;
case '--debug':
display_version();
$debug = true;
break;
case '-q':
case '--quiet':
$quiet = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit(0);
case '--help':
case '-H':
case '-h':
display_help();
exit(0);
default:
display_help();
printf('ERROR: Invalid Argument: (%s)' . PHP_EOL . PHP_EOL, $arg);
exit(1);
}
}
}
if ($debug) {
$tests = [
'CACTI_VERSION' => CACTI_VERSION,
'CACTI_VERSION_FULL' => CACTI_VERSION_FULL,
'get_cacti_db_version()' => get_cacti_db_version(),
'is_install_needed()' => is_install_needed(),
];
foreach ($tests as $name => $value) {
printf('%35s = (Rel %1s, Dev %1s) %s' . PHP_EOL, $name, is_cacti_release($value), is_cacti_develop($value), $value);
}
print PHP_EOL;
$tests = [
'CACTI_VERSION_TEXT' => CACTI_VERSION_TEXT,
'CACTI_VERSION_TEXT_FULL' => CACTI_VERSION_TEXT_FULL,
'CACTI_VERSION_TEXT_CLI' => CACTI_VERSION_TEXT_CLI,
'get_cacti_version_text(false)' => get_cacti_version_text(false),
'get_cacti_version_text(true)' => get_cacti_version_text(true),
];
foreach ($tests as $name => $value) {
printf('%35s = %s' . PHP_EOL, $name, $value);
}
print PHP_EOL;
$tests = [
'Fresh' => 'new_install',
// '1.3.0' => '1.3.0',
// 'Develop' => CACTI_VERSION_FULL,
// '1.3 Dev 569' => '1.3.0.99.1553092569.fab5112a',
'1.3 Dev 328' => '1.3.0.99.1553092328.12f20874',
// '1.3 Beta 2' => '1.3.0.2',
// '1.3 Beta 1' => '1.3.0.1',
// '1.2 Dev 329' => '1.2.3.99.1553092329.0d39f3ad',
// '1.2.2' => '1.2.2',
// '1.2.0' => '1.2.0',
// '1.2 Beta 1' => '1.2.0.1',
'0.8.8h' => '0.8.8h',
// '0.8.8b' => '0.8.8b',
// '0.8.8' => '0.8.8',
];
$sources = $tests;
$keys = [];
foreach (array_keys($tests) as $index => $key) {
$keys[$key] = chr($index + ord('a'));
}
$matrix = [];
$dkeys = $keys;
foreach ($keys as $key) {
foreach ($dkeys as $dkey) {
$matrix[$key][$dkey] = ' ';
}
}
foreach ($tests as $test => $version) {
$key = $keys[$test];
$formatted = format_cacti_version($version);
printf(
'%15s (Rel %1s, Dev %1s) => %s (%s)' . PHP_EOL,
$test,
is_cacti_release($formatted),
is_cacti_develop($formatted),
$formatted,
version_to_bits($formatted, false)
);
foreach ($sources as $name => $source) {
$dkey = $keys[$name];
$matrix[$key][$dkey] = cacti_version_compare($formatted, $source, '<') ? '+' : '.';
printf(
' => %15s = %-15s (%20s)' . PHP_EOL,
$name,
cacti_version_compare($formatted, $source, '<') ? 'Upgrade' : 'Not Required',
version_to_bits($source, false)
);
}
print PHP_EOL;
}
print ' ';
foreach ($keys as $key) {
print $key . ' ';
}
print PHP_EOL;
foreach ($keys as $name => $key) {
print $key;
foreach ($dkeys as $dkey) {
print ' ' . $matrix[$key][$dkey];
}
print ' ' . $name . PHP_EOL;
}
exit;
}
if ($dev) {
print CACTI_VERSION . '.99.' . time() . PHP_EOL;
} elseif ($quiet) {
print CACTI_VERSION_FULL . PHP_EOL;
} else {
display_version();
print PHP_EOL;
print 'Full: ' . CACTI_VERSION_TEXT_FULL . PHP_EOL;
print 'Code: ' . CACTI_VERSION_FULL . PHP_EOL;
print 'Data: ' . format_cacti_version(get_cacti_db_version()) . PHP_EOL;
print 'Dev.: ' . CACTI_VERSION . '.99.' . time() . PHP_EOL;
}
/**
* display_version - displays version information
*
* @return void
*/
function display_version() : void {
$version = get_cacti_cli_version();
print "Cacti Version Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays help information
*
* @return void
*/
function display_help() : void {
display_version();
print PHP_EOL;
print 'usage: version.php [option]' . PHP_EOL . PHP_EOL;
print 'Options:' . PHP_EOL;
print ' -d, --dev show development upgrade version (generated)' . PHP_EOL;
print ' -q, --quiet no headers' . PHP_EOL;
print ' --debug show debug testing and matrix' . PHP_EOL . PHP_EOL;
}