This commit is contained in:
fenris 2025-09-21 13:27:37 +02:00
parent 64c887275a
commit a8fbce49b0
9 changed files with 293 additions and 97 deletions

View file

@ -2,6 +2,8 @@
namespace davina\conf; namespace davina\conf;
require_once(DIR_LOGIC . '/helpers/list.php');
/** /**
*/ */
@ -26,6 +28,7 @@ class struct_source_data_ics_feed
*/ */
class struct_source class struct_source
{ {
public string $name;
public string $kind; public string $kind;
public $data; public $data;
} }
@ -44,7 +47,10 @@ class struct_settings
class struct_root class struct_root
{ {
public struct_auth $auth; public struct_auth $auth;
public struct_source $source; /**
* @var array {list<struct_source>}
*/
public array $sources;
public struct_settings $settings; public struct_settings $settings;
} }
@ -57,6 +63,18 @@ class _state
} }
/**
*/
function validate_source_name(
string $source_name
) : string
{
$matchs = null;
$result = preg_match('/^[0-9a-zA-Z]+$/', $source_name, $matches);
return ($result === 1);
}
/** /**
*/ */
function load( function load(
@ -78,12 +96,29 @@ function load(
$auth->data = (($data_raw['auth'] ?? [])['data'] ?? null); $auth->data = (($data_raw['auth'] ?? [])['data'] ?? null);
$data->auth = $auth; $data->auth = $auth;
} }
// source // sources
{ {
$source = new struct_source(); $sources = [];
$source->kind = $data_raw['source']['kind']; /**
$source->data = ($data_raw['source']['data'] ?? null); * @todo check for name duplicates
$data->source = $source; */
$data->sources = \davina\helpers\list_\map(
$data_raw['sources'],
function ($source_raw) {
if (! validate_source_name($source_raw['name']))
{
throw (new \Exception(\sprintf('invalid source name: "%s"', $source_raw['name'])));
}
else
{
$source = new struct_source();
$source->name = $source_raw['name'];
$source->kind = $source_raw['kind'];
$source->data = ($source_raw['data'] ?? null);
return $source;
}
}
);
} }
// settings // settings
{ {

35
source/helpers/list.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace davina\helpers\list_;
/**
*/
function map(
array $list,
\Closure $function
) : array
{
return \array_map(
$function,
$list
);
}
/**
*/
function filter(
array $list,
\Closure $predicate
) : array
{
return \array_values(
\array_filter(
$list,
$predicate
)
);
}
?>

View file

@ -22,19 +22,29 @@ function main(
\date_default_timezone_set(\davina\conf\get()->settings->timezone); \date_default_timezone_set(\davina\conf\get()->settings->timezone);
$source = \davina\sources\make( /**
[ * @todo nicht einfach die source names als keys verwenden
'kind' => \davina\conf\get()->source->kind, */
'data' => \davina\conf\get()->source->data, $sources = [];
] foreach (\davina\conf\get()->sources as $source_raw)
); {
$sources[$source_raw->name] = \davina\sources\make(
$source_raw->kind,
$source_raw->data
);
}
$principal_backend = new \davina\overwrites\class_principle_backend(); $principal_backend = new \davina\overwrites\class_principle_backend($sources);
$server = new \Sabre\DAV\Server( $server = new \Sabre\DAV\Server(
[ [
new \Sabre\CalDAV\Principal\Collection($principal_backend), new \Sabre\CalDAV\Principal\Collection(
new \Sabre\CalDAV\CalendarRoot($principal_backend, new \davina\overwrites\class_caldav_backend($source)), $principal_backend
),
new \Sabre\CalDAV\CalendarRoot(
$principal_backend,
new \davina\overwrites\class_caldav_backend($sources)
),
] ]
); );
@ -42,12 +52,8 @@ function main(
$server->addPlugin( $server->addPlugin(
new \Sabre\DAV\Auth\Plugin( new \Sabre\DAV\Auth\Plugin(
\davina\overwrites\make_auth_backend( new \davina\overwrites\class_auth_backend_basic(
$source, $sources
[
'kind' => \davina\conf\get()->auth->kind,
'data' => \davina\conf\get()->auth->data,
]
) )
) )
); );

View file

@ -15,18 +15,19 @@ class class_auth_backend_basic
{ {
/** /**
* @var array {list<\davina\sources\interface_source>}
*/ */
private \davina\sources\interface_source $source; private array $sources;
/** /**
*/ */
public function __construct( public function __construct(
\davina\sources\interface_source $source array $sources
) )
{ {
// parent::__construct(); // parent::__construct();
$this->source = $source; $this->sources = $sources;
$this->setRealm('davina'); $this->setRealm('davina');
} }
@ -34,20 +35,38 @@ class class_auth_backend_basic
/** /**
*/ */
protected function validateUserPass( protected function validateUserPass(
/*string */$username, /*string */$identifier,
/*string */$password /*string */$password
)/* : bool*/ )/* : bool*/
{ {
$parts = \explode('-', $identifier, 2);
$source_name = $parts[0];
$username = (
(\count($parts) >= 2)
?
$parts[1]
:
$source_name
);
$parameters = [ $parameters = [
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
]; ];
/** if (! \array_key_exists($source_name, $this->sources))
* @todo check for security {
*/ return false;
\davina\set_parameters($parameters); }
$data = $this->source->get(/*$parameters*/[]); else
return ($data !== null); {
$source = $this->sources[$source_name];
/**
* @todo check for security
*/
\davina\set_parameters($parameters);
$data = $source->read(/*$parameters*/[]);
return ($data !== null);
}
} }
} }

View file

@ -4,6 +4,7 @@ namespace davina\overwrites;
require_once('vendor/autoload.php'); require_once('vendor/autoload.php');
require_once(DIR_LOGIC . '/helpers/call.php'); require_once(DIR_LOGIC . '/helpers/call.php');
require_once(DIR_LOGIC . '/helpers/list.php');
require_once(DIR_LOGIC . '/helpers/ics.php'); require_once(DIR_LOGIC . '/helpers/ics.php');
require_once(DIR_LOGIC . '/model.php'); require_once(DIR_LOGIC . '/model.php');
require_once(DIR_LOGIC . '/sources/_interface.php'); require_once(DIR_LOGIC . '/sources/_interface.php');
@ -19,50 +20,25 @@ class class_caldav_backend
/** /**
* @var array {map<string, \davina\sources\interface_source>}
*/ */
private \davina\sources\interface_source $source; private array $sources;
/**
*/
private ?string $selected_source_name;
/** /**
*/ */
public function __construct( public function __construct(
\davina\sources\interface_source $source array $sources
) )
{ {
// parent::__construct(); // parent::__construct();
$this->source = $source; $this->sources = $sources;
} $this->selected_source_name = null;
/**
*/
private function hash_tag(
string $tag
) : string
{
return \hash('sha256', $tag);
}
/**
*/
private function encode_tag(
string $tag
) : string
{
return \davina\helpers\call\convey(
$tag,
[
fn($x) => \strtolower($x),
fn($x) => \preg_replace('/ä/', 'ae', $x),
fn($x) => \preg_replace('/ö/', 'oe', $x),
fn($x) => \preg_replace('/ü/', 'ue', $x),
fn($x) => \preg_replace('/ß/', 'sz', $x),
fn($x) => \preg_replace('/\-/', '', $x),
fn($x) => \preg_replace('/ /', '-', $x),
fn($x) => \preg_replace('/[^a-zA-Z0-9_\-]/s', '_', $x),
]
);
} }
@ -125,6 +101,97 @@ class class_caldav_backend
} }
/**
*/
private function encode_tag(
string $tag
) : string
{
return \davina\helpers\call\convey(
$tag,
[
fn($x) => \strtolower($x),
fn($x) => \preg_replace('/ä/', 'ae', $x),
fn($x) => \preg_replace('/ö/', 'oe', $x),
fn($x) => \preg_replace('/ü/', 'ue', $x),
fn($x) => \preg_replace('/ß/', 'sz', $x),
fn($x) => \preg_replace('/\-/', '', $x),
fn($x) => \preg_replace('/ /', '-', $x),
fn($x) => \preg_replace('/[^a-zA-Z0-9_\-]/s', '_', $x),
]
);
}
/**
* @param array $parameters {
* record<
* source_name:string,
* calendar_name:string,
* >
* }
*/
private function encode_calendar_identifier(
array $parameters
) : string
{
return \sprintf(
'%s-%s',
$parameters['source_name'],
$parameters['calendar_name']
);
}
/**
* @return array {
* record<
* source_name:string,
* calendar_name:string,
* >
* }
*/
private function decode_calendar_identifier(
string $str
) : array
{
$parts = \explode('-', $str, 2);
return [
'source_name' => $parts[0],
'calendar_name' => $parts[1],
];
}
/**
*/
private function get_source(
$source_name
) : \davina\sources\interface_source
{
if (
($this->selected_source_name === null)
||
($this->selected_source_name === $source_name)
)
{
$this->selected_source_name = $source_name;
if (! \array_key_exists($source_name, $this->sources))
{
throw (new \Exception(\sprintf('no such source: %s', $source_name)));
}
else
{
return $this->sources[$source_name];
}
}
else
{
throw (new \Exception('may not change source'));
}
}
/** /**
* [implementation] * [implementation]
*/ */
@ -132,7 +199,10 @@ class class_caldav_backend
$principalUri $principalUri
) )
{ {
$calendar = $this->source->get([]); // $source = $this->source;
$source_name = \explode('/', $principalUri)[1];
$source = $this->get_source($source_name);
$calendar = $source->read([]);
$tags = []; $tags = [];
foreach ($calendar->events as $event) foreach ($calendar->events as $event)
{ {
@ -143,7 +213,12 @@ class class_caldav_backend
} }
$result = \array_map( $result = \array_map(
fn($tag) => [ fn($tag) => [
'id' => $this->hash_tag($tag), 'id' => $this->encode_calendar_identifier(
[
'source_name' => $source_name,
'calendar_name' => $this->encode_tag($tag),
]
),
'uri' => $this->encode_tag($tag), 'uri' => $this->encode_tag($tag),
'principaluri' => $principalUri, 'principaluri' => $principalUri,
'{DAV:}displayname' => $tag, '{DAV:}displayname' => $tag,
@ -187,8 +262,12 @@ class class_caldav_backend
$calendarId $calendarId
) )
{ {
$tag = $calendarId; $calendar_identifier = $this->decode_calendar_identifier($calendarId);
$calendar = $this->source->get([]); $source_name = $calendar_identifier['source_name'];
$source = $this->get_source($source_name);
$tag = $calendar_identifier['calendar_name'];
$calendar = $source->read([]);
$result = \array_map( $result = \array_map(
fn($event) => [ fn($event) => [
'calendarid' => $calendarId, 'calendarid' => $calendarId,
@ -207,7 +286,7 @@ class class_caldav_backend
fn($event) => \in_array( fn($event) => \in_array(
$tag, $tag,
\array_map( \array_map(
fn($x) => $this->hash_tag($x), fn($x) => $this->encode_tag($x),
$event->tags $event->tags
) )
) )
@ -227,7 +306,12 @@ class class_caldav_backend
) )
{ {
$id = $objectUri; $id = $objectUri;
$calendar = $this->source->get([]); $calendar_identifier = $this->decode_calendar_identifier($calendarId);
$source_name = $calendar_identifier['source_name'];
$source = $this->get_source($source_name);
$tag = $calendar_identifier['calendar_name'];
$calendar = $source->read([]);
$events = \array_values( $events = \array_values(
\array_filter( \array_filter(
$calendar->events, $calendar->events,

View file

@ -3,6 +3,7 @@
namespace davina\overwrites; namespace davina\overwrites;
require_once('vendor/autoload.php'); require_once('vendor/autoload.php');
require_once(DIR_LOGIC . '/helpers/list.php');
/** /**
@ -12,17 +13,39 @@ class class_principle_backend
{ {
/** /**
* @var array {map<string, \davina\sources\interface_source>}
*/ */
public function getPrincipalsByPrefix($prefixPath) private array $sources;
/**
*/
public function __construct(
array $sources
)
{ {
throw (new \Exception('not implemented: getPrincipalsByPrefix')); $this->sources = $sources;
/* }
return [
[
'uri' => 'principals/dummy', /**
*/
public function getPrincipalsByPrefix(
$prefixPath
)
{
return \davina\helpers\list_\map(
\array_keys($this->sources),
fn($source_name) => [
'uri' => \davina\helpers\string_\coin(
'principals/{{name}}',
[
'name' => $source_name,
]
),
'{DAV:}displayname' => $source_name,
] ]
]; );
*/
} }
@ -32,7 +55,6 @@ class class_principle_backend
$path $path
) )
{ {
// throw (new \Exception('not implemented: getPrincipalByPath'));
$parts = \explode('/', $path); $parts = \explode('/', $path);
$username = $parts[1]; $username = $parts[1];
return [ return [

View file

@ -6,26 +6,21 @@ require_once(DIR_LOGIC . '/sources/ics_feed.php');
/** /**
* @param array $descriptor {
* record<
* kind:string,
* data:any
* >
* }
*/ */
function make( function make(
array $descriptor string $kind,
$data
) : interface_source ) : interface_source
{ {
switch ($descriptor['kind']) switch ($kind)
{ {
case 'ics_feed': case 'ics_feed':
{ {
return ( return (
new class_source_ics_feed( new class_source_ics_feed(
$descriptor['data']['url'], $data['url'],
($descriptor['data']['lifetime'] ?? (60 * 15)), ($data['lifetime'] ?? (60 * 15)),
($descriptor['data']['combine'] ?? false) ($data['combine'] ?? false)
) )
); );
} }
@ -35,7 +30,7 @@ function make(
new \Exception( new \Exception(
\sprintf( \sprintf(
'unhandled source kind: %s', 'unhandled source kind: %s',
$descriptor['kind'] $kind
) )
) )
); );
@ -44,4 +39,4 @@ function make(
} }
} }
?>

View file

@ -16,7 +16,7 @@ interface interface_source
* > * >
* } * }
*/ */
public function get( public function read(
array $parameters array $parameters
) : \davina\model\struct_calendar ) : \davina\model\struct_calendar
; ;

View file

@ -121,7 +121,7 @@ class class_source_ics_feed
( (
$this->combine $this->combine
? ?
['combined'] ['all']
: :
$vevent->categories $vevent->categories
) )
@ -178,7 +178,7 @@ class class_source_ics_feed
/** /**
* [implementation] * [implementation]
*/ */
public function get( public function read(
array $parameters array $parameters
) : \davina\model\struct_calendar ) : \davina\model\struct_calendar
{ {