core/source/conf.php
2025-09-22 23:17:08 +02:00

145 lines
3 KiB
PHP

<?php
/*
davina — Calendar data CalDAV conditioner
Copyright (C) 2025 Fenris <fenris@folksprak.org>
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/>.
*/
namespace davina\conf;
require_once(DIR_LOGIC . '/helpers/list.php');
/**
*/
class _state
{
public static $data = null;
}
/**
*/
function validate_realm_name(
string $realm_name
) : string
{
$matchs = null;
$result = preg_match('/^[0-9a-zA-Z]+$/', $realm_name, $matches);
return ($result === 1);
}
/**
*/
function load(
string $path
) : void
{
$data_raw = \json_decode(
\file_get_contents(
$path
),
true
);
_state::$data = [
/**
* @todo check for name duplicates
*/
'realms' => \davina\helpers\list_\map(
$data_raw['realms'],
function ($realm_raw) {
if (! validate_realm_name($realm_raw['name']))
{
throw (new \Exception(\sprintf('invalid realm name: "%s"', $realm_raw['name'])));
}
else
{
return [
'name' => $realm_raw['name'],
'auth' => (function ($auth_raw) use ($realm_raw) {
switch ($auth_raw['kind'])
{
case 'pass_through':
{
return [
'kind' => 'pass_through',
'data' => null
];
break;
}
case 'static':
{
return [
'kind' => 'static',
'data' => [
'password' => ($auth_raw['data']['password'] ?? $realm_raw['name']),
]
];
break;
}
default:
{
throw (new \Exception(\sprintf('invalid auth kind: %s', $auth_raw['kind'])));
break;
}
}
}) ($realm_raw['auth']),
'source' => (function ($source_raw) {
switch ($source_raw['kind'])
{
case 'ics_feed':
{
return [
'kind' => 'ics_feed',
'data' => [
'url' => $source_raw['data']['url'],
'lifetime' => ($source_raw['data']['lifetime'] ?? (60 * 15)),
'conflate' => ($source_raw['data']['conflate'] ?? true),
]
];
break;
}
default:
{
throw (new \Exception(\sprintf('invalid source kind: %s', $source_raw['kind'])));
}
}
}) ($realm_raw['source']),
];
}
}
),
'settings' => [
'timezone' => (($data_raw['settings'] ?? [])['timezone'] ?? 'UTC'),
]
];
}
/**
*/
function get(
)
{
if (_state::$data === null)
{
throw (new \Exception('not yet loaded'));
}
else
{
return _state::$data;
}
}
?>