diff --git a/src/www/widgets/widgets/thermal_sensors.widget.php b/src/www/widgets/widgets/thermal_sensors.widget.php index 4bb227558..734dec8b5 100644 --- a/src/www/widgets/widgets/thermal_sensors.widget.php +++ b/src/www/widgets/widgets/thermal_sensors.widget.php @@ -30,28 +30,40 @@ require_once("guiconfig.inc"); config_read_array('widgets', 'thermal_sensors_widget'); -function validate_temp_value($value) +function fix_temp_value($value, int $default): int { if (is_numeric($value) && (int)$value == $value && $value >= 0 and $value <= 100) { + return (int)$value; + } else { + return $default; + } +} + +function fix_checkbox_value($value): bool +{ + if ($value !== '') { return true; } else { return false; } } -$fieldnames = array('thermal_sensors_widget_zone_warning_threshold', 'thermal_sensors_widget_zone_critical_threshold', - 'thermal_sensors_widget_core_warning_threshold', 'thermal_sensors_widget_core_critical_threshold'); +$fields = [ + ['name' => 'thermal_sensors_widget_zone_warning_threshold', 'default' => 70, 'processFunc' => 'fix_temp_value'], + ['name' => 'thermal_sensors_widget_zone_critical_threshold', 'default' => 80, 'processFunc' => 'fix_temp_value'], + ['name' => 'thermal_sensors_widget_core_warning_threshold', 'default' => 70, 'processFunc' => 'fix_temp_value'], + ['name' => 'thermal_sensors_widget_core_critical_threshold', 'default' => 80, 'processFunc' => 'fix_temp_value'], + ['name' => 'thermal_sensors_widget_show_one_core_temp', 'default' => false, 'processFunc' => 'fix_checkbox_value'], +]; if ($_SERVER['REQUEST_METHOD'] === 'GET') { - $pconfig = array(); - foreach ($fieldnames as $fieldname) { - $defaultValue = strpos($fieldname, 'critical') !== false ? 80 : 70; - $pconfig[$fieldname] = !empty($config['widgets']['thermal_sensors_widget'][$fieldname]) ? $config['widgets']['thermal_sensors_widget'][$fieldname] : $defaultValue; + $pconfig = []; + foreach ($fields as $field) { + $pconfig[$field['name']] = !empty($config['widgets']['thermal_sensors_widget'][$field['name']]) ? $config['widgets']['thermal_sensors_widget'][$field['name']] : $field['default']; } } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { - foreach ($fieldnames as $fieldname) { - $defaultValue = strpos($fieldname, 'critical') !== false ? 80 : 70; - $newValue = !empty($_POST[$fieldname]) ? $_POST[$fieldname] : ""; - $config['widgets']['thermal_sensors_widget'][$fieldname] = validate_temp_value($newValue) ? $newValue : $defaultValue; + foreach ($fields as $field) { + $newValue = $field['processFunc']($_POST[$field['name']] ?? '', $field['default']); + $config['widgets']['thermal_sensors_widget'][$field['name']] = $newValue; } write_config("Thermal sensors widget saved via Dashboard."); header(url_safe('Location: /index.php')); @@ -60,53 +72,71 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { ?>