Cacti / cacti

Cacti ™
http://www.cacti.net
GNU General Public License v2.0
1.63k stars 404 forks source link

ERROR PHP WARNING: Undefined array key "description" in file: D:\cacti\apache\htdocs\cacti\settings.php on line: 1221 #5776

Closed MSS970 closed 3 months ago

MSS970 commented 3 months ago

Cacti 1.3 DEV Configuration -> Settings -> Search filter Attempted to filter out the settings, entered a string e.g. syslog. Checked the cacti logs and got:

2024-06-21 20:06:31 - PHP ERROR Backtrace: (D:\cacti\apache\htdocs\cacti\settings.php[42]:settings_search(), D:\cacti\apache\htdocs\cacti\settings.php[1221]:stristr(), CactiErrorHandler()) 2024-06-21 20:06:31 - ERROR PHP DEPRECATED: stristr(): Passing null to parameter #1 ($haystack) of type string is deprecated in file: D:\cacti\apache\htdocs\cacti\settings.php on line: 1221 2024-06-21 20:06:31 - PHP ERROR WARNING Backtrace: (D:\cacti\apache\htdocs\cacti\settings.php[42]:settings_search(), D:\cacti\apache\htdocs\cacti\settings.php[1221]:CactiErrorHandler()) 2024-06-21 20:06:31 - ERROR PHP WARNING: Undefined array key "description" in file: D:\cacti\apache\htdocs\cacti\settings.php on line: 1221

TheWitness commented 3 months ago

Is this the develop version?

MSS970 commented 3 months ago

Yes

MSS970 commented 3 months ago

The solution is to enhance the following function in settings.php, below is the enhanced version:

function settings_search() { global $settings;

validate_settings_filter();

$filter = get_request_var('filter');

$response = array(
    'tabs'    => array(),
    'rows'    => array(),
    'spacers' => array()
);

$last_spacer = '';
$tabs        = array();
$spacers     = array();

foreach($settings as $tab => $page) {
    foreach($page as $field_name => $field_array) {
        if ($field_array['method'] == 'spacer') {
            $last_spacer = $field_name;
        } elseif (($field_array['method'] != 'hidden')) {
            $friendly_key = false;
            $description_key = false;

            if (array_key_exists('friendly_name', $field_array)) {
                if (stristr($field_array['friendly_name'], $filter) !== false) {
                    $friendly_key = true;
                } else {
                    $friendly_key = false;
                }
            } else {
                $friendly_key = false;
            }
            if (array_key_exists('description', $field_array)) {
                if (stristr($field_array['description'], $filter) !== false) {
                    $description_key = true;
                } else {
                    $description_key = false;
                }
            } else {
                $description_key = false;
            }
            if ($friendly_key !== false || $description_key !== false) {
                $tabs[] = $tab;
                $response['rows'][] = $field_name;

                if ($last_spacer != '') {
                    $spacers[] = $last_spacer;
                }
            }
        }
    }
}

if (cacti_sizeof($tabs)) {
    $response['tabs'] = array_values(array_unique($tabs, SORT_STRING));
}

if (cacti_sizeof($spacers)) {
    $response['spacers'] = array_values(array_unique($spacers, SORT_STRING));
}

print json_encode($response);

}

TheWitness commented 3 months ago

I'm wondering where this was caused? Is it possible to reproduce in 1.2.27? Do you have any plugins installed? If so, which ones?

MSS970 commented 3 months ago

Hi, I can't produce it in cacti 1.2.27 as I am running on 1.3 Dev.

I will provide you with the list of field_name that has caused the incident (tab, field_name).

In fact I have fixed this issue and submitted a pull request to merge the change, kindly review.

Regards, MSS

MSS970 commented 3 months ago

It seems that the "thold" plugin has caused this incident, "thold_notification_delay" setting field does not have "description"!

Tab: "alerts" field_name: "thold_notification_delay" method: "hidden" key: friendly_name value: Device Notification Delay Options key: method value: hidden

Can you kindly review the pull request and merge if suitable or let me know if any change is required.

Much appreciated. MSS

TheWitness commented 3 months ago

Resolved. Thanks for the contribution!