Skip to content

Commit

Permalink
Merge pull request #71 from rpc-scandinavia/develop
Browse files Browse the repository at this point in the history
Include and Exclude filter on discovered addressbook urls and discovered calendar urls
  • Loading branch information
matidau authored Aug 1, 2024
2 parents 555320f + 7ce7e18 commit 39fcaa8
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
69 changes: 69 additions & 0 deletions src/backend/caldav/caldav.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public function GetFolderList() {
$folders = array();
$calendars = $this->_caldav->FindCalendars();
foreach ($calendars as $val) {
if ($this->GetFolderIsIncluded($val->url) == false) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderList() Excluded folder '%s'", $val->url));
continue;
}
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderList() Included folder '%s'", $val->url));

$fpath = explode("/", $val->url, -1);
if (is_array($fpath)) {
$folderid = array_pop($fpath);
Expand Down Expand Up @@ -2199,5 +2205,68 @@ private function explodeUnescapedDelimiter($delimiter, $data, $escapeCharacter =

return $result;
}

/**
* Filter discovered calendar urls, by inclusion or exclusion.
* The default is to always include the calendar collection, unless either include or exclude is uncommented and configured.
* The calendar collection is included, either when the include filter is not configured, or when the include filter matches.
* The calendar collection is excluded, when the exclude filter is configured and it matches. Exclude always wins over include.
* The defined constants can either be a string or an array of multiple strings, to provide multiple filters.
*/
private function GetFolderIsIncluded($url) {
// Exist and retain original behaviour.
if ((defined('CALDAV_PATH_INCLUDE') == false) && (defined('CALDAV_PATH_EXCLUDE') == false)) {
return true;
}
if ($url === null) {
return true;
}

// Convert to array.
$includes = array();
if (defined('CALDAV_PATH_INCLUDE') == true) {
if (is_array(CALDAV_PATH_INCLUDE) == true) {
$includes = CALDAV_PATH_INCLUDE;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Include filter is an array with %s elements", count($includes)));
} else if (CALDAV_PATH_INCLUDE !== '') {
$includes = array(CALDAV_PATH_INCLUDE);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Include filter is a string '%s'", CALDAV_PATH_INCLUDE));
}
}

$excludes = array();
if (defined('CALDAV_PATH_EXCLUDE') == true) {
if (is_array(CALDAV_PATH_EXCLUDE) == true) {
$excludes = CALDAV_PATH_EXCLUDE;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Exclude filter is an array with %s elements", count($excludes)));
} else if (CALDAV_PATH_EXCLUDE !== '') {
$excludes = array(CALDAV_PATH_EXCLUDE);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Exclude filter is a string '%s'", CALDAV_PATH_EXCLUDE));
}
}

// Test for matches.
$foundOneInclude = (count($includes) == 0);
foreach ($includes as $include) {
if (fnmatch($include, $url) == true) {
$foundOneInclude = true;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Include filter '%s' matches '%s'", $include, $url));
break;
}
}

$foundNoExcludes = (count($excludes) == 0);
$foundOneExclude = false;
foreach ($excludes as $exclude) {
if (fnmatch($exclude, $url) == true) {
$foundOneExclude = true;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV::GetFolderIsIncluded() Exclude filter '%s' matches '%s'", $exclude, $url));
break;
}
}
$foundNoExcludes = ($foundOneExclude == false);

return (($foundOneInclude == true) && ($foundNoExcludes == true));
} // GetFolderIsIncluded

};
10 changes: 10 additions & 0 deletions src/backend/caldav/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@
// Maximum period to sync.
// Some servers don't support more than 10 years so you will need to change this
define('CALDAV_MAX_SYNC_PERIOD', 2147483647);

// Filter discovered calendar urls, by inclusion or exclusion.
// The default is to always include the calendar collection, unless either include or exclude is uncommented and configured.
// The calendar collection is included, either when the include filter is not configured, or when the include filter matches.
// The calendar collection is excluded, when the exclude filter is configured and it matches. Exclude always wins over include.
// The defined constants can either be a string or an array of multiple strings, to provide multiple filters.
// Exclude example: '*/archived-appointments/'
// Include example: '*sync*'
//define('CALDAV_PATH_INCLUDE', '');
//define('CALDAV_PATH_EXCLUDE', '');
73 changes: 71 additions & 2 deletions src/backend/carddav/carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,8 +1328,13 @@ private function discoverAddressbooks() {
continue;
}
}
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::discoverAddressbooks() Found addressbook '%s'", urldecode($response->url)));
$this->addressbooks[] = urldecode($response->url);

if ($this->getAddressbookIsIncluded($response->url) == true) {
$this->addressbooks[] = urldecode($response->url);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::discoverAddressbooks() Included addressbook '%s'", urldecode($response->url)));
} else {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::discoverAddressbooks() Excluded addressbook '%s'", urldecode($response->url)));
}
}
unset($xml);
}
Expand Down Expand Up @@ -1462,4 +1467,68 @@ private function getAddressbookFromUrl($addressbookUrl) {
return $addressbookId;
}


/**
* Filter discovered addressbook urls, by inclusion or exclusion.
* The default is to always include the addressbook collection, unless either include or exclude is uncommented and configured.
* The addressbook collection is included, either when the include filter is not configured, or when the include filter matches.
* The addressbook collection is excluded, when the exclude filter is configured and it matches. Exclude always wins over include.
* The defined constants can either be a string or an array of multiple strings, to provide multiple filters.
*/
private function getAddressbookIsIncluded($url) {
// Exist and retain original behaviour.
if ((defined('CARDDAV_PATH_INCLUDE') == false) && (defined('CARDDAV_PATH_EXCLUDE') == false)) {
return true;
}
if ($url === null) {
return true;
}

// Convert to array.
$includes = array();
if (defined('CARDDAV_PATH_INCLUDE') == true) {
if (is_array(CARDDAV_PATH_INCLUDE) == true) {
$includes = CARDDAV_PATH_INCLUDE;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Include filter is an array with %s elements", count($includes)));
} else if (CARDDAV_PATH_INCLUDE !== '') {
$includes = array(CARDDAV_PATH_INCLUDE);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Include filter is a string '%s'", CARDDAV_PATH_INCLUDE));
}
}

$excludes = array();
if (defined('CARDDAV_PATH_EXCLUDE') == true) {
if (is_array(CARDDAV_PATH_EXCLUDE) == true) {
$excludes = CARDDAV_PATH_EXCLUDE;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Exclude filter is an array with %s elements", count($excludes)));
} else if (CARDDAV_PATH_EXCLUDE !== '') {
$excludes = array(CARDDAV_PATH_EXCLUDE);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Exclude filter is a string '%s'", CARDDAV_PATH_EXCLUDE));
}
}

// Test for matches.
$foundOneInclude = (count($includes) == 0);
foreach ($includes as $include) {
if (fnmatch($include, $url) == true) {
$foundOneInclude = true;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Include filter '%s' matches '%s'", $include, $url));
break;
}
}

$foundNoExcludes = (count($excludes) == 0);
$foundOneExclude = false;
foreach ($excludes as $exclude) {
if (fnmatch($exclude, $url) == true) {
$foundOneExclude = true;
ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCardDAV::getAddressbookIsIncluded() Exclude filter '%s' matches '%s'", $exclude, $url));
break;
}
}
$foundNoExcludes = ($foundOneExclude == false);

return (($foundOneInclude == true) && ($foundNoExcludes == true));
} // getAddressbookIsIncluded

}
10 changes: 10 additions & 0 deletions src/backend/carddav/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@
// Davical needs it
// SOGo official demo online needs it, but some SOGo installation don't need it, so test it
define('CARDDAV_URL_VCARD_EXTENSION', '.vcf');

// Filter discovered addressbook urls, by inclusion or exclusion.
// The default is to always include the addressbook collection, unless either include or exclude is uncommented and configured.
// The addressbook collection is included, either when the include filter is not configured, or when the include filter matches.
// The addressbook collection is excluded, when the exclude filter is configured and it matches. Exclude always wins over include.
// The defined constants can either be a string or an array of multiple strings, to provide multiple filters.
// Exclude example: '*/archived-addresses/'
// Include example: '*sync*'
//define('CARDDAV_PATH_INCLUDE', '');
//define('CARDDAV_PATH_EXCLUDE', '');

0 comments on commit 39fcaa8

Please sign in to comment.