TimezoneService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(
  18. private IConfig $config,
  19. private PropertyMapper $propertyMapper,
  20. private IManager $calendarManager,
  21. ) {
  22. }
  23. public function getUserTimezone(string $userId): ?string {
  24. $fromConfig = $this->config->getUserValue(
  25. $userId,
  26. 'core',
  27. 'timezone',
  28. );
  29. if ($fromConfig !== '') {
  30. return $fromConfig;
  31. }
  32. $availabilityPropPath = 'calendars/' . $userId . '/inbox';
  33. $availabilityProp = '{' . Plugin::NS_CALDAV . '}calendar-availability';
  34. $availabilities = $this->propertyMapper->findPropertyByPathAndName($userId, $availabilityPropPath, $availabilityProp);
  35. if (!empty($availabilities)) {
  36. $availability = $availabilities[0]->getPropertyvalue();
  37. /** @var VCalendar $vCalendar */
  38. $vCalendar = Reader::read($availability);
  39. /** @var VTimeZone $vTimezone */
  40. $vTimezone = $vCalendar->VTIMEZONE;
  41. // Sabre has a fallback to date_default_timezone_get
  42. return $vTimezone->getTimeZone()->getName();
  43. }
  44. $principal = 'principals/users/' . $userId;
  45. $uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
  46. $calendars = $this->calendarManager->getCalendarsForPrincipal($principal);
  47. /** @var ?VTimeZone $personalCalendarTimezone */
  48. $personalCalendarTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) use ($uri) {
  49. if ($acc !== null) {
  50. return $acc;
  51. }
  52. if ($calendar->getUri() === $uri && !$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
  53. return $calendar->getSchedulingTimezone();
  54. }
  55. return null;
  56. });
  57. if ($personalCalendarTimezone !== null) {
  58. return $personalCalendarTimezone->getTimeZone()->getName();
  59. }
  60. // No timezone in the personalCalendarTimezone calendar or no personalCalendarTimezone calendar
  61. // Loop through all calendars until we find a timezone.
  62. /** @var ?VTimeZone $firstTimezone */
  63. $firstTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) {
  64. if ($acc !== null) {
  65. return $acc;
  66. }
  67. if (!$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
  68. return $calendar->getSchedulingTimezone();
  69. }
  70. return null;
  71. });
  72. if ($firstTimezone !== null) {
  73. return $firstTimezone->getTimeZone()->getName();
  74. }
  75. return null;
  76. }
  77. public function getDefaultTimezone(): string {
  78. return $this->config->getSystemValueString('default_timezone', 'UTC');
  79. }
  80. }