Files
cacti/cli/sqltable_to_php.php
T
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

283 lines
8.5 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 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/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
// switch to main database for cli's
if (POLLER_ID > 1) {
db_switch_remote_to_main();
}
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$table = '';
$plugin = '';
$create = true;
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (str_contains($parameter, '=')) {
[$arg, $value] = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--table':
$table = trim(sql_clean($value));
break;
case '--plugin':
$plugin = trim(sql_clean($value));
break;
case '--update':
$create = false;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit(0);
case '--help':
case '-H':
case '-h':
display_help();
exit(0);
default:
}
}
}
if ($table == '') {
print 'ERROR: You must provide a table name' . PHP_EOL;
display_help();
exit(1);
} else {
print sqltable_to_php($table, $create, $plugin);
}
function sqltable_to_php(string $table, bool $create, string $plugin = '') : string {
global $database_default;
include_once(CACTI_PATH_LIBRARY . '/database.php');
$result = db_fetch_assoc('SHOW tables FROM `' . $database_default . '`');
$tables = [];
$text = '';
if (cacti_sizeof($result)) {
foreach ($result as $index => $arr) {
foreach ($arr as $t) {
$tables[] = $t;
}
}
} else {
print "ERROR: Obtaining list of tables from $database_default" . PHP_EOL;
exit;
}
if (in_array($table, $tables, true)) {
$result = db_fetch_assoc("SHOW FULL columns FROM $table");
$cols = [];
$pri = [];
$keys = [];
$text = PHP_EOL . '$data = array();' . PHP_EOL;
if (cacti_sizeof($result)) {
foreach ($result as $r) {
$text .= "\$data['columns'][] = array(";
$text .= "'name' => '" . $r['Field'] . "'";
if (str_contains(strtolower($r['Type']), ' unsigned')) {
$r['Type'] = str_ireplace(' unsigned', '', $r['Type']);
$text .= ", 'unsigned' => true";
}
$text .= ", 'type' => " . db_qstr($r['Type']);
$text .= ", 'NULL' => " . (strtolower($r['Null']) == 'no' ? 'false' : 'true');
if ($r['Default'] != '' && trim($r['Default']) != '') {
if ($r['Default'] == "''") {
$r['Default'] = '';
} elseif (str_contains($r['Default'], 'current_timestamp()')) {
$r['Default'] = str_replace('current_timestamp()', 'CURRENT_TIMESTAMP', $r['Default']);
}
$text .= ", 'default' => '" . $r['Default'] . "'";
} elseif (stripos($r['Type'], 'char') !== false) {
$text .= ", 'default' => ''";
}
if (trim($r['Extra']) != '') {
if (strtolower($r['Extra']) == 'on update current_timestamp') {
$text .= ", 'on_update' => 'CURRENT_TIMESTAMP'";
}
if (strtolower($r['Extra']) == 'auto_increment') {
$text .= ", 'auto_increment' => true";
}
}
if (trim($r['Comment']) != '') {
$text .= ", 'comment' => '" . $r['Comment'] . "'";
}
$text .= ');' . PHP_EOL;
}
} else {
print "ERROR: Obtaining list of columns from $table" . PHP_EOL;
exit;
}
$result = db_fetch_assoc("SHOW INDEX FROM $table");
if (cacti_sizeof($result)) {
$unique_keys = [];
foreach ($result as $r) {
if ($r['Key_name'] == 'PRIMARY') {
$pri[] = $r['Column_name'];
} else {
$keys[$r['Key_name']][$r['Seq_in_index']] = $r['Column_name'];
if ($r['Non_unique'] == 0) {
$unique_keys[$r['Key_name']] = $r['Key_name'];
}
}
}
if (!empty($pri)) {
if ($plugin != '' || $create) {
$text .= "\$data['primary'] = '" . implode('`,`', $pri) . "';" . PHP_EOL;
} else {
$text .= "\$data['primary'] = array('" . implode("','", $pri) . "');" . PHP_EOL;
}
}
if (!empty($keys)) {
foreach ($keys as $n => $k) {
if ($plugin != '') {
$text .= "\$data['keys'][] = array('name' => '$n', " . (isset($unique_keys[$n]) ? "'unique' => true, " : '') . "'columns' => '" . implode('`,`', $k) . "');" . PHP_EOL;
} else {
$text .= "\$data['keys'][] = array('name' => '$n', " . (isset($unique_keys[$n]) ? "'unique' => true, " : '') . "'columns' => array('" . implode("','", $k) . "'));" . PHP_EOL;
}
}
}
} else {
// print "ERROR: Obtaining list of indexes from $table" . PHP_EOL;
// exit;
}
$result = db_fetch_row_prepared('SELECT ENGINE, TABLE_COMMENT, ROW_FORMAT, CHARACTER_SET_NAME
FROM information_schema.TABLES tbl JOIN information_schema.COLLATIONS coll ON tbl.TABLE_COLLATION=coll.COLLATION_NAME
WHERE TABLE_SCHEMA = SCHEMA()
AND TABLE_NAME = ?',
[$table]);
if (cacti_sizeof($result)) {
$text .= "\$data['type'] = '" . $result['ENGINE'] . "';" . PHP_EOL;
$text .= "\$data['charset'] = '" . $result['CHARACTER_SET_NAME'] . "';" . PHP_EOL;
if (!empty($result['TABLE_COMMENT'])) {
$text .= "\$data['comment'] = '" . $result['TABLE_COMMENT'] . "';" . PHP_EOL;
}
$text .= "\$data['row_format'] = '" . $result['ROW_FORMAT'] . "';" . PHP_EOL;
if ($create) {
if ($plugin != '') {
$text .= "api_plugin_db_table_create ('$plugin', '$table', \$data);" . PHP_EOL;
} else {
$text .= "db_table_create ('$table', \$data);" . PHP_EOL;
}
} else {
$text .= "db_update_table ('$table', \$data, false);" . PHP_EOL;
}
} else {
print 'ERROR: Unable to get tables details from Information Schema' . PHP_EOL;
exit;
}
}
return $text;
}
function sql_clean(string $text) : string {
$text = str_replace(['\\', '/', "'", '"', '|'], '', $text);
return $text;
}
/**
* display_version - displays version information
*
* @return void
*/
function display_version() : void {
$version = get_cacti_cli_version();
print "Cacti SQL to PHP 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: sqltable_to_php.php --table=table_name [--plugin=name] [--update]' . PHP_EOL . PHP_EOL;
print 'A simple developers utility to create a save schema for a newly created or' . PHP_EOL;
print 'modified database table in a format that is consumable by Cacti.' . PHP_EOL . PHP_EOL;
print "These save schema's can be placed into a plugins setup.php file in order" . PHP_EOL;
print "to create the tables inside of a plugin as a part of it's install function." . PHP_EOL;
print 'The plugin parameter is optional, but if you want the table(s) automatically' . PHP_EOL;
print "removed from Cacti when uninstalling the plugin, specify it's name." . PHP_EOL . PHP_EOL;
print 'Required:' . PHP_EOL;
print '--table=table_name - The table that you want exported' . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print '--plugin=name - The name of the plugin that will manage tables' . PHP_EOL;
print '--update - The utility provides create syntax. If the update flag is' . PHP_EOL;
print ' specified, the utility will provide update syntax' . PHP_EOL . PHP_EOL;
}