ICSExportPlugin.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. private IConfig $config;
  20. private LoggerInterface $logger;
  21. /** @var string */
  22. private const DEFAULT_REFRESH_INTERVAL = 'PT4H';
  23. /**
  24. * ICSExportPlugin constructor.
  25. */
  26. public function __construct(IConfig $config, LoggerInterface $logger) {
  27. $this->config = $config;
  28. $this->logger = $logger;
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) {
  34. if (!isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  35. $value = $this->config->getAppValue('dav', 'defaultRefreshIntervalExportedCalendars', self::DEFAULT_REFRESH_INTERVAL);
  36. $properties['{http://nextcloud.com/ns}refresh-interval'] = $value;
  37. }
  38. return parent::generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response);
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. public function mergeObjects(array $properties, array $inputObjects) {
  44. $vcalendar = parent::mergeObjects($properties, $inputObjects);
  45. if (isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  46. $refreshIntervalValue = $properties['{http://nextcloud.com/ns}refresh-interval'];
  47. try {
  48. DateTimeParser::parseDuration($refreshIntervalValue);
  49. } catch (InvalidDataException $ex) {
  50. $this->logger->debug('Invalid refresh interval for exported calendar, falling back to default value ...');
  51. $refreshIntervalValue = self::DEFAULT_REFRESH_INTERVAL;
  52. }
  53. // https://tools.ietf.org/html/rfc7986#section-5.7
  54. $refreshInterval = new Duration($vcalendar, 'REFRESH-INTERVAL', $refreshIntervalValue);
  55. $refreshInterval->add('VALUE', 'DURATION');
  56. $vcalendar->add($refreshInterval);
  57. // Legacy property for compatibility
  58. $vcalendar->{'X-PUBLISHED-TTL'} = $refreshIntervalValue;
  59. }
  60. return $vcalendar;
  61. }
  62. }