TimezoneService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCA\DAV\Db\PropertyMapper;
  9. use OCP\Calendar\ICalendar;
  10. use OCP\Calendar\IManager;
  11. use OCP\IConfig;
  12. use Sabre\VObject\Component\VCalendar;
  13. use Sabre\VObject\Component\VTimeZone;
  14. use Sabre\VObject\Reader;
  15. use function array_reduce;
  16. class TimezoneService {
  17. public function __construct(private IConfig $config,
  18. private PropertyMapper $propertyMapper,
  19. private IManager $calendarManager) {
  20. }
  21. public function getUserTimezone(string $userId): ?string {
  22. $fromConfig = $this->config->getUserValue(
  23. $userId,
  24. 'core',
  25. 'timezone',
  26. );
  27. if ($fromConfig !== '') {
  28. return $fromConfig;
  29. }
  30. $availabilityPropPath = 'calendars/' . $userId . '/inbox';
  31. $availabilityProp = '{' . Plugin::NS_CALDAV . '}calendar-availability';
  32. $availabilities = $this->propertyMapper->findPropertyByPathAndName($userId, $availabilityPropPath, $availabilityProp);
  33. if (!empty($availabilities)) {
  34. $availability = $availabilities[0]->getPropertyvalue();
  35. /** @var VCalendar $vCalendar */
  36. $vCalendar = Reader::read($availability);
  37. /** @var VTimeZone $vTimezone */
  38. $vTimezone = $vCalendar->VTIMEZONE;
  39. // Sabre has a fallback to date_default_timezone_get
  40. return $vTimezone->getTimeZone()->getName();
  41. }
  42. $principal = 'principals/users/' . $userId;
  43. $uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
  44. $calendars = $this->calendarManager->getCalendarsForPrincipal($principal);
  45. /** @var ?VTimeZone $personalCalendarTimezone */
  46. $personalCalendarTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) use ($uri) {
  47. if ($acc !== null) {
  48. return $acc;
  49. }
  50. if ($calendar->getUri() === $uri && !$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
  51. return $calendar->getSchedulingTimezone();
  52. }
  53. return null;
  54. });
  55. if ($personalCalendarTimezone !== null) {
  56. return $personalCalendarTimezone->getTimeZone()->getName();
  57. }
  58. // No timezone in the personalCalendarTimezone calendar or no personalCalendarTimezone calendar
  59. // Loop through all calendars until we find a timezone.
  60. /** @var ?VTimeZone $firstTimezone */
  61. $firstTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) {
  62. if ($acc !== null) {
  63. return $acc;
  64. }
  65. if (!$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
  66. return $calendar->getSchedulingTimezone();
  67. }
  68. return null;
  69. });
  70. if ($firstTimezone !== null) {
  71. return $firstTimezone->getTimeZone()->getName();
  72. }
  73. return null;
  74. }
  75. public function getDefaultTimezone(): string {
  76. return $this->config->getSystemValueString('default_timezone', 'UTC');
  77. }
  78. }