You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ICSExportPlugin.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\CalDAV\ICSExportPlugin;
  24. use OCP\IConfig;
  25. use Psr\Log\LoggerInterface;
  26. use Sabre\HTTP\ResponseInterface;
  27. use Sabre\VObject\DateTimeParser;
  28. use Sabre\VObject\InvalidDataException;
  29. use Sabre\VObject\Property\ICalendar\Duration;
  30. /**
  31. * Class ICSExportPlugin
  32. *
  33. * @package OCA\DAV\CalDAV\ICSExportPlugin
  34. */
  35. class ICSExportPlugin extends \Sabre\CalDAV\ICSExportPlugin {
  36. private IConfig $config;
  37. private LoggerInterface $logger;
  38. /** @var string */
  39. private const DEFAULT_REFRESH_INTERVAL = 'PT4H';
  40. /**
  41. * ICSExportPlugin constructor.
  42. */
  43. public function __construct(IConfig $config, LoggerInterface $logger) {
  44. $this->config = $config;
  45. $this->logger = $logger;
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) {
  51. if (!isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  52. $value = $this->config->getAppValue('dav', 'defaultRefreshIntervalExportedCalendars', self::DEFAULT_REFRESH_INTERVAL);
  53. $properties['{http://nextcloud.com/ns}refresh-interval'] = $value;
  54. }
  55. return parent::generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response);
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. public function mergeObjects(array $properties, array $inputObjects) {
  61. $vcalendar = parent::mergeObjects($properties, $inputObjects);
  62. if (isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
  63. $refreshIntervalValue = $properties['{http://nextcloud.com/ns}refresh-interval'];
  64. try {
  65. DateTimeParser::parseDuration($refreshIntervalValue);
  66. } catch (InvalidDataException $ex) {
  67. $this->logger->debug('Invalid refresh interval for exported calendar, falling back to default value ...');
  68. $refreshIntervalValue = self::DEFAULT_REFRESH_INTERVAL;
  69. }
  70. // https://tools.ietf.org/html/rfc7986#section-5.7
  71. $refreshInterval = new Duration($vcalendar, 'REFRESH-INTERVAL', $refreshIntervalValue);
  72. $refreshInterval->add('VALUE', 'DURATION');
  73. $vcalendar->add($refreshInterval);
  74. // Legacy property for compatibility
  75. $vcalendar->{'X-PUBLISHED-TTL'} = $refreshIntervalValue;
  76. }
  77. return $vcalendar;
  78. }
  79. }