Compare commits

..

2 commits

Author SHA1 Message Date
fenris baea0378fb [int] 2025-09-21 13:28:00 +02:00
fenris a8fbce49b0 [int] 2025-09-21 13:27:37 +02:00
10 changed files with 294 additions and 97 deletions

View file

@ -2,6 +2,8 @@
namespace davina\conf;
require_once(DIR_LOGIC . '/helpers/list.php');
/**
*/
@ -26,6 +28,7 @@ class struct_source_data_ics_feed
*/
class struct_source
{
public string $name;
public string $kind;
public $data;
}
@ -44,7 +47,10 @@ class struct_settings
class struct_root
{
public struct_auth $auth;
public struct_source $source;
/**
* @var array {list<struct_source>}
*/
public array $sources;
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(
@ -78,12 +96,29 @@ function load(
$auth->data = (($data_raw['auth'] ?? [])['data'] ?? null);
$data->auth = $auth;
}
// source
// sources
{
$source = new struct_source();
$source->kind = $data_raw['source']['kind'];
$source->data = ($data_raw['source']['data'] ?? null);
$data->source = $source;
$sources = [];
/**
* @todo check for name duplicates
*/
$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
{

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);
$source = \davina\sources\make(
[
'kind' => \davina\conf\get()->source->kind,
'data' => \davina\conf\get()->source->data,
]
);
/**
* @todo nicht einfach die source names als keys verwenden
*/
$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(
[
new \Sabre\CalDAV\Principal\Collection($principal_backend),
new \Sabre\CalDAV\CalendarRoot($principal_backend, new \davina\overwrites\class_caldav_backend($source)),
new \Sabre\CalDAV\Principal\Collection(
$principal_backend
),
new \Sabre\CalDAV\CalendarRoot(
$principal_backend,
new \davina\overwrites\class_caldav_backend($sources)
),
]
);
@ -42,12 +52,8 @@ function main(
$server->addPlugin(
new \Sabre\DAV\Auth\Plugin(
\davina\overwrites\make_auth_backend(
$source,
[
'kind' => \davina\conf\get()->auth->kind,
'data' => \davina\conf\get()->auth->data,
]
new \davina\overwrites\class_auth_backend_basic(
$sources
)
)
);

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(
\davina\sources\interface_source $source
array $sources
)
{
// parent::__construct();
$this->source = $source;
$this->sources = $sources;
$this->setRealm('davina');
}
@ -34,20 +35,38 @@ class class_auth_backend_basic
/**
*/
protected function validateUserPass(
/*string */$username,
/*string */$identifier,
/*string */$password
)/* : bool*/
{
$parts = \explode('-', $identifier, 2);
$source_name = $parts[0];
$username = (
(\count($parts) >= 2)
?
$parts[1]
:
$source_name
);
$parameters = [
'username' => $username,
'password' => $password,
];
/**
* @todo check for security
*/
\davina\set_parameters($parameters);
$data = $this->source->get(/*$parameters*/[]);
return ($data !== null);
if (! \array_key_exists($source_name, $this->sources))
{
return false;
}
else
{
$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(DIR_LOGIC . '/helpers/call.php');
require_once(DIR_LOGIC . '/helpers/list.php');
require_once(DIR_LOGIC . '/helpers/ics.php');
require_once(DIR_LOGIC . '/model.php');
require_once(DIR_LOGIC . '/sources/_interface.php');
@ -19,52 +20,27 @@ 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(
\davina\sources\interface_source $source
array $sources
)
{
// 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),
]
);
}
/**
* @todo outsource
@ -123,6 +99,97 @@ class class_caldav_backend
}
return $vcalendar;
}
/**
*/
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'));
}
}
/**
@ -132,7 +199,10 @@ class class_caldav_backend
$principalUri
)
{
$calendar = $this->source->get([]);
// $source = $this->source;
$source_name = \explode('/', $principalUri)[1];
$source = $this->get_source($source_name);
$calendar = $source->read([]);
$tags = [];
foreach ($calendar->events as $event)
{
@ -143,7 +213,12 @@ class class_caldav_backend
}
$result = \array_map(
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),
'principaluri' => $principalUri,
'{DAV:}displayname' => $tag,
@ -187,8 +262,12 @@ class class_caldav_backend
$calendarId
)
{
$tag = $calendarId;
$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([]);
$result = \array_map(
fn($event) => [
'calendarid' => $calendarId,
@ -207,7 +286,7 @@ class class_caldav_backend
fn($event) => \in_array(
$tag,
\array_map(
fn($x) => $this->hash_tag($x),
fn($x) => $this->encode_tag($x),
$event->tags
)
)
@ -227,7 +306,12 @@ class class_caldav_backend
)
{
$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(
\array_filter(
$calendar->events,

View file

@ -3,6 +3,7 @@
namespace davina\overwrites;
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'));
/*
return [
[
'uri' => 'principals/dummy',
$this->sources = $sources;
}
/**
*/
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
)
{
// throw (new \Exception('not implemented: getPrincipalByPath'));
$parts = \explode('/', $path);
$username = $parts[1];
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(
array $descriptor
string $kind,
$data
) : interface_source
{
switch ($descriptor['kind'])
switch ($kind)
{
case 'ics_feed':
{
return (
new class_source_ics_feed(
$descriptor['data']['url'],
($descriptor['data']['lifetime'] ?? (60 * 15)),
($descriptor['data']['combine'] ?? false)
$data['url'],
($data['lifetime'] ?? (60 * 15)),
($data['combine'] ?? false)
)
);
}
@ -35,7 +30,7 @@ function make(
new \Exception(
\sprintf(
'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
) : \davina\model\struct_calendar
;

View file

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

View file

@ -47,6 +47,7 @@ def main():
"--links",
"--verbose",
"--exclude='conf.json'",
"--exclude='data'",
("%s/" % args.build_directory),
(
("%s" % args.target_directory)