TimezoneService.php 3.5 KB

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