refactor: safe PHP 7.4 modernization#21
refactor: safe PHP 7.4 modernization#21somethingwithproof wants to merge 5 commits intoCacti:developfrom
Conversation
There was a problem hiding this comment.
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
$columnsis always empty. Reintroduce the list of SNMP-related columns so changes to SNMP credentials/settings propagate to the linkedhostrecord.
// 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.
| 'id' => [], | ||
| 'save_component_ups' => [] |
There was a problem hiding this comment.
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.
| 'id' => [], | |
| 'save_component_ups' => [] | |
| 'id' => array( | |
| 'method' => 'hidden', | |
| 'value' => '|arg1:id|' | |
| ), | |
| 'save_component_ups' => array( | |
| 'method' => 'hidden', | |
| 'value' => '1' | |
| ) |
| 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 = []; |
There was a problem hiding this comment.
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.
| 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' | ||
| ); |
There was a problem hiding this comment.
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.
| $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' | ||
| ); |
There was a problem hiding this comment.
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.
| if (!is_[]) { | ||
| $template_id = []; |
There was a problem hiding this comment.
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.
| if (!is_[]) { | |
| $template_id = []; | |
| if (!is_array($template_id)) { | |
| $template_id = [$template_id]; |
| 3 => 'Low', | ||
| 4 => 'Fault' | ||
| ) | ||
| 'snmp_enum' => [] |
There was a problem hiding this comment.
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.
| 'snmp_enum' => [] | |
| 'snmp_enum' => array( | |
| 1 => 'unknown', | |
| 2 => 'batteryNormal', | |
| 3 => 'batteryLow', | |
| 4 => 'batteryInFaultCondition' | |
| ) |
| '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' => [] | ||
| ), |
There was a problem hiding this comment.
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.
| 'upsConfigLowVoltageTransferPoint' => '1.3.6.1.2.1.33.1.9.9', | ||
| 'upsConfigHighVoltageTransferPoint' => '1.3.6.1.2.1.33.1.9.10', | ||
| ); | ||
| $rfc_oids = []; |
There was a problem hiding this comment.
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.
| 15 => 'ECONVERSION', | ||
| 16 => 'STATICBYPASS' | ||
| ) | ||
| 'snmp_enum' => [] |
There was a problem hiding this comment.
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.
| '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' | |
| ) |
| $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; |
There was a problem hiding this comment.
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.
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>
|
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. |
This PR adds strict typing, short array syntax, and null coalescing operators across the plugin. Standalone infrastructure files were removed per architectural mandate.