|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 7 | + * |
| 8 | + * @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\DAV\CalDAV; |
| 27 | + |
| 28 | +use OCA\DAV\Db\PropertyMapper; |
| 29 | +use OCP\Calendar\ICalendar; |
| 30 | +use OCP\Calendar\IManager; |
| 31 | +use OCP\IConfig; |
| 32 | +use Sabre\VObject\Component\VCalendar; |
| 33 | +use Sabre\VObject\Component\VTimeZone; |
| 34 | +use Sabre\VObject\Reader; |
| 35 | +use function array_reduce; |
| 36 | + |
| 37 | +class TimezoneService { |
| 38 | + |
| 39 | + public function __construct(private IConfig $config, |
| 40 | + private PropertyMapper $propertyMapper, |
| 41 | + private IManager $calendarManager) { |
| 42 | + } |
| 43 | + |
| 44 | + public function getUserTimezone(string $userId): ?string { |
| 45 | + $availabilityPropPath = 'calendars/' . $userId . '/inbox'; |
| 46 | + $availabilityProp = '{' . Plugin::NS_CALDAV . '}calendar-availability'; |
| 47 | + $availabilities = $this->propertyMapper->findPropertyByPathAndName($userId, $availabilityPropPath, $availabilityProp); |
| 48 | + if (!empty($availabilities)) { |
| 49 | + $availability = $availabilities[0]->getPropertyvalue(); |
| 50 | + /** @var VCalendar $vCalendar */ |
| 51 | + $vCalendar = Reader::read($availability); |
| 52 | + /** @var VTimeZone $vTimezone */ |
| 53 | + $vTimezone = $vCalendar->VTIMEZONE; |
| 54 | + // Sabre has a fallback to date_default_timezone_get |
| 55 | + return $vTimezone->getTimeZone()->getName(); |
| 56 | + } |
| 57 | + |
| 58 | + $principal = 'principals/users/' . $userId; |
| 59 | + $uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI); |
| 60 | + $calendars = $this->calendarManager->getCalendarsForPrincipal($principal); |
| 61 | + |
| 62 | + /** @var ?VTimeZone $personalCalendarTimezone */ |
| 63 | + $personalCalendarTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) use ($uri) { |
| 64 | + if ($acc !== null) { |
| 65 | + return $acc; |
| 66 | + } |
| 67 | + if ($calendar->getUri() === $uri && !$calendar->isDeleted() && $calendar instanceof CalendarImpl) { |
| 68 | + return $calendar->getSchedulingTimezone(); |
| 69 | + } |
| 70 | + return null; |
| 71 | + }); |
| 72 | + if ($personalCalendarTimezone !== null) { |
| 73 | + return $personalCalendarTimezone->getTimeZone()->getName(); |
| 74 | + } |
| 75 | + |
| 76 | + // No timezone in the personalCalendarTimezone calendar or no personalCalendarTimezone calendar |
| 77 | + // Loop through all calendars until we find a timezone. |
| 78 | + /** @var ?VTimeZone $firstTimezone */ |
| 79 | + $firstTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) { |
| 80 | + if ($acc !== null) { |
| 81 | + return $acc; |
| 82 | + } |
| 83 | + if (!$calendar->isDeleted() && $calendar instanceof CalendarImpl) { |
| 84 | + return $calendar->getSchedulingTimezone(); |
| 85 | + } |
| 86 | + return null; |
| 87 | + }); |
| 88 | + if ($firstTimezone !== null) { |
| 89 | + return $firstTimezone->getTimeZone()->getName(); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + public function getDefaultTimezone(): string { |
| 95 | + return $this->config->getSystemValueString('default_timezone', 'UTC'); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments