core/source/conf.php

132 lines
2.4 KiB
PHP
Raw Normal View History

2025-09-09 12:07:53 +02:00
<?php
2025-09-16 12:05:35 +02:00
namespace davina\conf;
2025-09-09 12:07:53 +02:00
2025-09-21 13:27:37 +02:00
require_once(DIR_LOGIC . '/helpers/list.php');
2025-09-09 12:07:53 +02:00
/**
*/
class _state
{
2025-09-21 18:49:03 +02:00
public static $data = null;
2025-09-09 12:07:53 +02:00
}
2025-09-21 13:27:37 +02:00
/**
*/
2025-09-21 18:49:03 +02:00
function validate_realm_name(
string $realm_name
2025-09-21 13:27:37 +02:00
) : string
{
$matchs = null;
2025-09-21 18:49:03 +02:00
$result = preg_match('/^[0-9a-zA-Z]+$/', $realm_name, $matches);
2025-09-21 13:27:37 +02:00
return ($result === 1);
}
2025-09-09 12:07:53 +02:00
/**
*/
function load(
string $path
) : void
{
2025-09-09 19:43:23 +02:00
$data_raw = \json_decode(
2025-09-09 12:07:53 +02:00
\file_get_contents(
$path
),
true
);
2025-09-21 18:49:03 +02:00
_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'])));
2025-09-21 13:27:37 +02:00
}
2025-09-21 18:49:03 +02:00
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' => [
'username' => ($auth_raw['data']['username'] ?? $realm_raw['name']),
'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'),
]
];
2025-09-09 12:07:53 +02:00
}
/**
*/
function get(
2025-09-21 18:49:03 +02:00
)
2025-09-09 12:07:53 +02:00
{
2025-09-09 23:17:19 +02:00
if (_state::$data === null)
{
throw (new \Exception('not yet loaded'));
}
else
{
return _state::$data;
}
2025-09-09 12:07:53 +02:00
}
?>