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 */
23namespace OCA\DAV\CalDAV\ICSExportPlugin;
24
25use OCP\IConfig;
26use OCP\ILogger;
27use Sabre\HTTP\ResponseInterface;
28use Sabre\VObject\DateTimeParser;
29use Sabre\VObject\InvalidDataException;
30use Sabre\VObject\Property\ICalendar\Duration;
31
32/**
33 * Class ICSExportPlugin
34 *
35 * @package OCA\DAV\CalDAV\ICSExportPlugin
36 */
37class ICSExportPlugin extends \Sabre\CalDAV\ICSExportPlugin {
38
39	/** @var IConfig */
40	private $config;
41
42	/** @var ILogger */
43	private $logger;
44
45	/** @var string */
46	private const DEFAULT_REFRESH_INTERVAL = 'PT4H';
47
48	/**
49	 * ICSExportPlugin constructor.
50	 *
51	 * @param IConfig $config
52	 */
53	public function __construct(IConfig $config, ILogger $logger) {
54		$this->config = $config;
55		$this->logger = $logger;
56	}
57
58	/**
59	 * @inheritDoc
60	 */
61	protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) {
62		if (!isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
63			$value = $this->config->getAppValue('dav', 'defaultRefreshIntervalExportedCalendars', self::DEFAULT_REFRESH_INTERVAL);
64			$properties['{http://nextcloud.com/ns}refresh-interval'] = $value;
65		}
66
67		return parent::generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response);
68	}
69
70	/**
71	 * @inheritDoc
72	 */
73	public function mergeObjects(array $properties, array $inputObjects) {
74		$vcalendar = parent::mergeObjects($properties, $inputObjects);
75
76		if (isset($properties['{http://nextcloud.com/ns}refresh-interval'])) {
77			$refreshIntervalValue = $properties['{http://nextcloud.com/ns}refresh-interval'];
78			try {
79				DateTimeParser::parseDuration($refreshIntervalValue);
80			} catch (InvalidDataException $ex) {
81				$this->logger->debug('Invalid refresh interval for exported calendar, falling back to default value ...');
82				$refreshIntervalValue = self::DEFAULT_REFRESH_INTERVAL;
83			}
84
85			// https://tools.ietf.org/html/rfc7986#section-5.7
86			$refreshInterval = new Duration($vcalendar, 'REFRESH-INTERVAL', $refreshIntervalValue);
87			$refreshInterval->add('VALUE', 'DURATION');
88			$vcalendar->add($refreshInterval);
89
90			// Legacy property for compatibility
91			$vcalendar->{'X-PUBLISHED-TTL'} = $refreshIntervalValue;
92		}
93
94		return $vcalendar;
95	}
96}
97