Skip to content

refactor: safe PHP 7.4 modernization#21

Draft
somethingwithproof wants to merge 5 commits intoCacti:developfrom
somethingwithproof:refactor/modernization
Draft

refactor: safe PHP 7.4 modernization#21
somethingwithproof wants to merge 5 commits intoCacti:developfrom
somethingwithproof:refactor/modernization

Conversation

@somethingwithproof
Copy link
Copy Markdown

This PR adds strict typing, short array syntax, and null coalescing operators across the plugin. Standalone infrastructure files were removed per architectural mandate.

Copilot AI review requested due to automatic review settings April 9, 2026 21:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to modernize the APCUPSD plugin’s PHP by enabling strict typing and refactoring array usage / null handling. However, multiple changes replace required runtime values (SQL bind params, filter definitions, enum/OID mappings, form hidden fields) with empty arrays and introduce invalid PHP syntax, which will break core plugin functionality.

Changes:

  • Added declare(strict_types=1); to several entry-point files.
  • Refactored numerous array(...) usages to [] (and attempted to apply null coalescing).
  • Large portions of configuration/mapping data and many SQL parameter bindings were replaced with empty arrays.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 33 comments.

Show a summary per file
File Description
upses.php Form handling, request filtering, and AJAX location lookup were modified; several changes break form submission, SQL queries, and introduce syntax errors.
setup.php Upgrade gating and plugin config updates were modified; upgrade now short-circuits and prepared statements lose bind params.
poller_apcupsd.php Poller queries/updates were modified; most prepared statements no longer bind required params and include syntax errors, breaking polling/automation.
database.php SNMP enum mappings and large OID/config tables were cleared, removing required translation/config data.
index.php Added strict types before redirect.
locales/index.php Added strict types before redirect.
Comments suppressed due to low confidence (2)

upses.php:305

  • The SNMP host-sync logic is now disabled because $columns is always empty. Reintroduce the list of SNMP-related columns so changes to SNMP credentials/settings propagate to the linked host record.
				// SNMP columns
				if ($save['type_id'] == 2) {
					$columns = [];

					foreach($columns as $c) {
						if ($save[$c] != $host[$c]) {
							$ns[$c] = $save[$c];

poller_apcupsd.php:123

  • This prepared query has a ? placeholder but the parameter list is empty. Bind $ups['host_id'] so orphaned host references are detected correctly.
		if ($ups['host_id'] > 0) {
			$exists = db_fetch_cell_prepared('SELECT id FROM host WHERE id = ?', []);

			if (!$exists) {
				$ups['host_id'] = 0;
			}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread upses.php Outdated
Comment on lines +156 to +157
'id' => [],
'save_component_ups' => []
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id and save_component_ups were reduced to empty arrays. These fields previously defined hidden inputs that the form relies on (e.g., isset_request_var('save_component_ups') in form_save). Restore the hidden field definitions (method/value) so the save action is actually submitted.

Suggested change
'id' => [],
'save_component_ups' => []
'id' => array(
'method' => 'hidden',
'value' => '|arg1:id|'
),
'save_component_ups' => array(
'method' => 'hidden',
'value' => '1'
)

Copilot uses AI. Check for mistakes.
Comment thread upses.php Outdated
Comment on lines +259 to +262
if ($save['host_id'] > 0) {
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($save['host_id']));
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', []);
} else {
$host = array();
$host = [];
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prepared query has a ? placeholder but the parameter list is now empty. It should bind $save['host_id']; otherwise the DB layer will error or return the wrong host row.

Copilot uses AI. Check for mistakes.
Comment thread upses.php
Comment on lines 316 to 320
if ($name_changed) {
$graphs = array_rekey(
db_fetch_assoc_prepared('SELECT id FROM graph_local WHERE host_id = ?', array($host_id)),
db_fetch_assoc_prepared('SELECT id FROM graph_local WHERE host_id = ?', []),
'id', 'id'
);
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prepared query uses host_id = ? but the parameter list is empty. Bind $host_id (or the correct host id variable) so graph IDs can be fetched when renaming a host.

Copilot uses AI. Check for mistakes.
Comment thread upses.php
Comment on lines 322 to 325
$data_sources = array_rekey(
db_fetch_assoc_prepared('SELECT id FROM data_local WHERE host_id = ?', array($host_id)),
db_fetch_assoc_prepared('SELECT id FROM data_local WHERE host_id = ?', []),
'id', 'id'
);
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prepared query uses host_id = ? but the parameter list is empty. Bind $host_id (or the correct host id variable) so data source IDs can be fetched when renaming a host.

Copilot uses AI. Check for mistakes.
Comment thread upses.php Outdated
Comment on lines +350 to +351
if (!is_[]) {
$template_id = [];
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!is_[]) is invalid PHP syntax and will cause a parse error. This should remain an is_array($template_id) (or equivalent) check before normalizing $template_id into an array.

Suggested change
if (!is_[]) {
$template_id = [];
if (!is_array($template_id)) {
$template_id = [$template_id];

Copilot uses AI. Check for mistakes.
Comment thread database.php Outdated
3 => 'Low',
4 => 'Fault'
)
'snmp_enum' => []
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snmp_enum was replaced with an empty array for BATTSTAT. The polling code relies on this mapping to translate numeric SNMP values into meaningful strings; with an empty map, values will always become Unknown. Restore the original enum map.

Suggested change
'snmp_enum' => []
'snmp_enum' => array(
1 => 'unknown',
2 => 'batteryNormal',
3 => 'batteryLow',
4 => 'batteryInFaultCondition'
)

Copilot uses AI. Check for mistakes.
Comment thread database.php
Comment on lines 252 to 257
'SELFTEST' => array(
'db_column' => 'ups_selftest',
'snmp_ci' => '.1.3.6.1.2.1.33.1.7.3.0',
'description' => __esc('Date and time of last self test since apcupsd startup', 'apcupsd'),
'snmp_enum' => array(
1 => 'Ok',
2 => 'Warning',
3 => 'Error',
4 => 'Aborted',
5 => 'InProgress',
6 => 'Disabled'
)
'snmp_enum' => []
),
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snmp_enum was replaced with an empty array for SELFTEST (and similar enum fields). This removes the numeric-to-string translation used during SNMP collection; restore the enum mapping to preserve correct stats values.

Copilot uses AI. Check for mistakes.
Comment thread database.php Outdated
Comment on lines +342 to +400
'upsConfigLowVoltageTransferPoint' => '1.3.6.1.2.1.33.1.9.9',
'upsConfigHighVoltageTransferPoint' => '1.3.6.1.2.1.33.1.9.10',
);
$rfc_oids = [];
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The APC/MGE/RFC OID cross-reference tables were replaced with empty arrays. This is a functional removal of configuration data (not just syntax modernization) and may break any code or templates expecting these mappings. If the intent is modernization only, keep the existing mappings intact.

Copilot uses AI. Check for mistakes.
Comment thread database.php Outdated
15 => 'ECONVERSION',
16 => 'STATICBYPASS'
)
'snmp_enum' => []
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snmp_enum was replaced with an empty array for STATUS. Since collect_snmp_ups_data() uses this enum map to interpret numeric SNMP status values, clearing it will turn valid statuses into Unknown. Restore the original status enum mapping.

Suggested change
'snmp_enum' => []
'snmp_enum' => array(
'1' => 'UNKNOWN',
'2' => 'ONLINE',
'3' => 'ONBATT',
'4' => 'BOOST',
'5' => 'SLEEPING',
'6' => 'BYPASS',
'7' => 'OFF',
'8' => 'REBOOTING',
'9' => 'BYPASS',
'10' => 'BYPASS',
'11' => 'SLEEPING',
'12' => 'TRIM',
'13' => 'ONLINE',
'14' => 'ONLINE',
'15' => 'ONBATT'
)

Copilot uses AI. Check for mistakes.
Comment thread poller_apcupsd.php
Comment on lines 150 to 156
$ups_up = collect_snmp_ups_data($ups);

if ($ups['host_id'] > 0) {
$exists = db_fetch_cell_prepared('SELECT id FROM host WHERE id = ?', array($ups['host_id']));
$exists = db_fetch_cell_prepared('SELECT id FROM host WHERE id = ?', []);

if (!$exists) {
$ups['host_id'] = 0;
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prepared query has a ? placeholder but the parameter list is empty. Bind $ups['host_id'] so the SNMP loop correctly detects whether the referenced host still exists.

Copilot uses AI. Check for mistakes.
Revert corrupted function calls introduced by refactoring tool:
- is_[$x] -> is_array($x)
- in_[$x, ...] -> in_array($x, ...)
- xml2[$x] -> xml2array($x)

Also remove accidentally committed .omc session files and add
.omc/ to .gitignore.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The refactor tool catastrophically corrupted several files by:
- Stripping entire array contents (e.g., snmp_enum mappings, apc_oids,
  apc_snmp_config - 800+ lines of data lost in database.php)
- Replacing db_fetch_*_prepared() / db_execute_prepared() parameter
  arrays with empty [] (all SQL bindings destroyed in poller_apcupsd.php)
- Replacing in_array(), call_user_func_array() arguments with invalid
  syntax like $files = [] instead of array('plugins.php', 'upses.php')
- Breaking inject_form_variables() call to use invalid ( ?? []) syntax

Restore these files to their origin/develop state. The modernization
can be redone carefully in a follow-up PR.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@somethingwithproof somethingwithproof marked this pull request as draft April 11, 2026 00:09
@somethingwithproof
Copy link
Copy Markdown
Author

Converted to draft to serialize the stack in this repo. Blocked by #16; will un-draft after that merges to avoid cross-PR merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants