Files
librenms-librenms/LibreNMS/Util/DynamicConfigItem.php
Jellyfrog 0a351b49fd Laravel 9.x Shift (#14504)
* Move `resources/lang` folder

* Shift registered middleware

* Remove `fruitcake/laravel-cors` dependency

* Streamline `$commands` property

* Upgrade to Flysystem 3.0

* Shift core files

* Convert `optional()` to nullsafe operator

* Remove unnecessary `$model` property

* Convert route options to fluent methods

Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods.

* Convert deprecated `$dates` property to `$casts`

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them and merged your true customizations -
where ENV variables may not be used.

* Bump Laravel dependencies

* Use `<env>` tags for configuration

`<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI.

Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/

* Fix error provider

* Match new symfony syntax

* Match upstream syntax

* Fix route syntax

* generate composer.lock

* Sync back configs

* routes

* composer

* Fix more flare

* fix cors

* sync lang

* Apply fixes from StyleCI (#14517)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* bump larastan

* update packages

* wip

* Temporarily lower phpstan level

* Update phpstan.neon

* wip

* wip

* wip

* Apply fixes from StyleCI (#14592)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* test

* Update CiHelper.php

* Update test.yml

* Update CiHelper.php

* Update CiHelper.php

* Apply fixes from StyleCI (#14616)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* test?

* fix phpstan problems

* dont run snmpsim on github ci

* Fix whitespace

* More whitespace

* More whitespace ???

* I think the space broke it

* fix the reset of the whitespace

* hard code auth guard

---------

Co-authored-by: Shift <shift@laravelshift.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Tony Murray <murraytony@gmail.com>
2023-04-17 06:51:35 -05:00

268 lines
6.8 KiB
PHP

<?php
/**
* DynamicConfigItem.php
*
* -Description-
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use LibreNMS\Config;
use Validator;
class DynamicConfigItem implements \ArrayAccess
{
public $name;
public $group;
public $section;
public $value;
public $type;
public $default;
public $overridden = false; // overridden by config.php
public $hidden = false;
public $required = false;
public $disabled = false;
public $options = [];
public $when;
public $pattern;
public $validate;
public $units;
public function __construct($name, $settings = [])
{
$this->name = $name;
$this->value = Config::get($this->name, $this->default);
foreach ($settings as $key => $value) {
$this->$key = $value;
}
}
/**
* Check given value is valid. Using the type of this config item and possibly other variables.
*
* @param mixed $value
* @return bool|mixed
*/
public function checkValue($value)
{
if ($this->validate) {
return $this->buildValidator($value)->passes();
} elseif ($this->type == 'boolean') {
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null;
} elseif ($this->type == 'integer') {
return (! is_bool($value) && filter_var($value, FILTER_VALIDATE_INT)) || $value === '0' || $value === 0;
} elseif ($this->type == 'float') {
return filter_var($value, FILTER_VALIDATE_FLOAT) !== false;
} elseif ($this->type == 'select') {
return in_array($value, array_keys($this->options));
} elseif ($this->type == 'email') {
// allow email format that includes display text
if (preg_match('/.* <(.*)>/', $value, $matches)) {
$value = $matches[1];
}
return filter_var($value, FILTER_VALIDATE_EMAIL);
} elseif ($this->type == 'array') {
return is_array($value); // this should probably have more complex validation via validator rules
} elseif ($this->type == 'array-sub-keyed') {
if (! is_array($value)) {
return false;
}
foreach ($value as $v) {
if (! is_array($v)) {
return false;
}
}
return true;
} elseif ($this->type == 'color') {
return (bool) preg_match('/^#?[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/', $value);
} elseif (in_array($this->type, ['text', 'password'])) {
return ! is_array($value);
} elseif ($this->type === 'executable') {
return is_file($value) && is_executable($value);
} elseif ($this->type === 'directory') {
return is_dir($value);
}
return false;
}
public function getGroup()
{
return $this->group;
}
public function getSection()
{
return $this->section;
}
public function getValue()
{
return $this->value;
}
public function getOptions()
{
return array_reduce($this->options, function ($result, $option) {
$key = $this->optionTranslationKey($option);
$trans = __($key);
$result[$option] = ($trans === $key ? $option : $trans);
return $result;
}, []);
}
public function isHidden()
{
return $this->hidden;
}
public function isRequired()
{
return $this->required;
}
public function getType()
{
return $this->type;
}
public function hasDescription()
{
$key = $this->descriptionTranslationKey();
return __($key) !== $key;
}
public function hasHelp()
{
$key = $this->helpTranslationKey();
return __($key) !== $key;
}
public function hasUnits()
{
return isset($this->units);
}
public function getUnits()
{
return $this->hasUnits() ? __($this->units) : '';
}
public function getDescription()
{
$key = $this->descriptionTranslationKey();
$trans = __($key);
return $trans === $key ? $this->name : $trans;
}
public function getHelp()
{
return __($this->helpTranslationKey());
}
public function only($fields = [])
{
$array = [];
foreach ($fields as $field) {
$array[$field] = $this->$field;
}
return $array;
}
public function toArray()
{
return get_object_vars($this);
}
public function isValid()
{
return ($this->group == '' || $this->type) && ! $this->hidden && ! $this->disabled;
}
/**
* @param mixed $value The value that was validated
* @return string
*/
public function getValidationMessage($value)
{
return $this->validate
? implode(" \n", $this->buildValidator($value)->messages()->all())
: __('settings.validate.' . $this->type, ['id' => $this->name, 'value' => json_encode($value)]);
}
// ArrayAccess functions
public function offsetExists($offset): bool
{
return isset($this->$offset);
}
#[\ReturnTypeWillChange]
public function offsetGet($offset): mixed
{
return isset($this->$offset) ? $this->$offset : null;
}
public function offsetSet($offset, $value): void
{
$this->$offset = $value;
}
public function offsetUnset($offset): void
{
unset($this->$offset);
}
public function getName()
{
return $this->name;
}
private function descriptionTranslationKey()
{
return "settings.settings.$this->name.description";
}
private function helpTranslationKey()
{
return "settings.settings.$this->name.help";
}
private function optionTranslationKey($option)
{
return "settings.settings.$this->name.options.$option";
}
private function buildValidator($value)
{
return Validator::make(['value' => $value], $this->validate);
}
}