ICSExportPlugin.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\ICSExportPlugin;
  7. use OCP\IConfig;
  8. use Psr\Log\LoggerInterface;
  9. use Sabre\HTTP\ResponseInterface;
  10. use Sabre\VObject\DateTimeParser;
  11. use Sabre\VObject\InvalidDataException;
  12. use Sabre\VObject\Property\ICalendar\Duration;
  13. /**
  14. * Class ICSExportPlugin
  15. *
  16. * @package OCA\DAV\CalDAV\ICSExportPlugin
  17. */
  18. class ICSExportPlugin extends \Sabre\CalDAV\ICSExportPlugin {
  19. /** @var string */
  20. private const DEFAULT_REFRESH_INTERVAL = 'PT4H';
  21. /**
  22. * ICSExportPlugin constructor.
  23. */
  24. public function __construct(
  25. private IConfig $config,
  26. private LoggerInterface $logger,
  27. ) {
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) {
  33. if (!isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  34. $value = $this->config->getAppValue('dav', 'defaultRefreshIntervalExportedCalendars', self::DEFAULT_REFRESH_INTERVAL);
  35. $properties['{http://nextcloud.com/ns}refresh-interval'] = $value;
  36. }
  37. return parent::generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response);
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. public function mergeObjects(array $properties, array $inputObjects) {
  43. $vcalendar = parent::mergeObjects($properties, $inputObjects);
  44. if (isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  45. $refreshIntervalValue = $properties['{http://nextcloud.com/ns}refresh-interval'];
  46. try {
  47. DateTimeParser::parseDuration($refreshIntervalValue);
  48. } catch (InvalidDataException $ex) {
  49. $this->logger->debug('Invalid refresh interval for exported calendar, falling back to default value ...');
  50. $refreshIntervalValue = self::DEFAULT_REFRESH_INTERVAL;
  51. }
  52. // https://tools.ietf.org/html/rfc7986#section-5.7
  53. $refreshInterval = new Duration($vcalendar, 'REFRESH-INTERVAL', $refreshIntervalValue);
  54. $refreshInterval->add('VALUE', 'DURATION');
  55. $vcalendar->add($refreshInterval);
  56. // Legacy property for compatibility
  57. $vcalendar->{'X-PUBLISHED-TTL'} = $refreshIntervalValue;
  58. }
  59. return $vcalendar;
  60. }
  61. }