icingadb-web/library/Icingadb/Model/CustomvarFlat.php
Alexander A. Klimov 3c8ed68cc6 Upgrade license from GPLv2 to GPLv2+
This was easy because only README.md and doc/01-About.md were redacted manually, everything else via:
git ls-files -z |xargs -0 perl -pi -e 's/Icinga GmbH \| GPLv2/Icinga GmbH | GPLv2+/'

This is legal because we have only merged PRs with label:cla/signed or made by Icinga staff:
https://github.com/Icinga/icingadb-web/pulls?page=1&q=is%3Apr+is%3Aclosed+-label%3Acla%2Fsigned+-author%3Anilmerg

This has no risk for us in people distributing their own version under GPLv3 only.
After all, we won't take their patches anyway, unless they sign our CLA.

This is the cleanest solution for having e.g. these in one address space:

* Icinga Web, GPLv2+
* K8s Web, AGPLv3
* Thirdparty, some LGPLv3 and Apache-2.0

Apropos, K8s Web is even v3-licensed on purpose, to have a stronger protection against cloud ops.
2025-11-21 13:31:24 +01:00

203 lines
7.2 KiB
PHP

<?php
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2+ */
namespace Icinga\Module\Icingadb\Model;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Contract\RewriteFilterBehavior;
use Icinga\Module\Icingadb\Common\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
use ipl\Stdlib\Filter;
use ipl\Stdlib\Filter\Condition;
use Traversable;
/**
* @property string $id
* @property string $environment_id
* @property string $customvar_id
* @property string $flatname_checksum
* @property string $flatname
* @property ?string $flatvalue
*/
class CustomvarFlat extends Model
{
public function getTableName()
{
return 'customvar_flat';
}
public function getKeyName()
{
return 'id';
}
public function getColumns()
{
return [
'environment_id',
'customvar_id',
'flatname_checksum',
'flatname',
'flatvalue'
];
}
public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new Binary([
'id',
'environment_id',
'customvar_id',
'flatname_checksum'
]));
$behaviors->add(new class implements RewriteFilterBehavior {
public function rewriteCondition(Condition $condition, $relation = null)
{
if ($condition->metaData()->has('requiresTransformation')) {
/** @var string $columnName */
$columnName = $condition->metaData()->get('columnName');
$nameFilter = Filter::like($relation . 'flatname', $columnName);
$class = get_class($condition);
$valueFilter = new $class($relation . 'flatvalue', $condition->getValue());
return Filter::all($nameFilter, $valueFilter);
}
}
});
}
public function createRelations(Relations $relations)
{
$relations->belongsTo('environment', Environment::class);
$relations->belongsTo('customvar', Customvar::class);
$relations->belongsToMany('checkcommand', Checkcommand::class)
->through(CheckcommandCustomvar::class)
->setThroughAlias('t_checkcommand_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('eventcommand', Eventcommand::class)
->through(EventcommandCustomvar::class)
->setThroughAlias('t_eventcommand_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('host', Host::class)
->through(HostCustomvar::class)
->setThroughAlias('t_host_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('hostgroup', Hostgroup::class)
->through(HostgroupCustomvar::class)
->setThroughAlias('t_hostgroup_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('notification', Notification::class)
->through(NotificationCustomvar::class)
->setThroughAlias('t_notification_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('notificationcommand', Notificationcommand::class)
->through(NotificationcommandCustomvar::class)
->setThroughAlias('t_notificationcommand_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('service', Service::class)
->through(ServiceCustomvar::class)
->setThroughAlias('t_service_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('servicegroup', Servicegroup::class)
->through(ServicegroupCustomvar::class)
->setThroughAlias('t_servicegroup_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('timeperiod', Timeperiod::class)
->through(TimeperiodCustomvar::class)
->setThroughAlias('t_timeperiod_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('user', User::class)
->through(UserCustomvar::class)
->setThroughAlias('t_user_customvar')
->setCandidateKey('customvar_id');
$relations->belongsToMany('usergroup', Usergroup::class)
->through(UsergroupCustomvar::class)
->setThroughAlias('t_usergroup_customvar')
->setCandidateKey('customvar_id');
}
/**
* Restore flattened custom variables to their previous structure
*
* @param Traversable $flattenedVars
*
* @return array
*/
public function unFlattenVars(Traversable $flattenedVars): array
{
$registerValue = function (&$data, $source, $path, $value) use (&$registerValue) {
$step = array_shift($path);
$isIndex = (bool) preg_match('/^\[(\d+)]$/', $step, $m);
if ($isIndex) {
$step = $m[1];
}
if ($source !== null) {
while (! isset($source[$step])) {
if ($isIndex) {
$step = sprintf('[%d]', $step);
$isIndex = false;
} else {
if (empty($path)) {
break;
}
$step = implode('.', [$step, array_shift($path)]);
}
}
}
if (! empty($path)) {
if (! isset($data[$step])) {
$data[$step] = [];
}
$registerValue($data[$step], $source[$step] ?? null, $path, $value);
} else {
// Since empty custom vars of type dictionaries and arrays have null values in customvar_flat table,
// we won't be able to render them as such. Therefore, we have to use the value of the `customvar`
// table if it's not null, otherwise the current value, which is a "null" string.
$data[$step] = $value === null && ($source[$step] ?? null) === [] ? $source[$step] : $value;
}
};
if ($flattenedVars instanceof Query) {
$flattenedVars->withColumns(['customvar.name', 'customvar.value']);
}
$vars = [];
foreach ($flattenedVars as $var) {
if (isset($var->customvar->name)) {
$var->customvar->value = json_decode($var->customvar->value, true);
$realName = $var->customvar->name;
$source = [$realName => $var->customvar->value];
$sourcePath = ltrim(substr($var->flatname, strlen($realName)), '.');
$path = array_merge(
[$realName],
$sourcePath
? preg_split('/(?<=\w|])\.|(?<!^|\.)(?=\[)/', $sourcePath)
: []
);
} else {
$path = explode('.', $var->flatname);
$source = null;
}
$registerValue($vars, $source, $path, $var->flatvalue);
if (isset($var->customvar->name)) {
$var->customvar->name = null;
$var->customvar->value = null;
}
}
return $vars;
}
}