RefreshWebcalService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\WebcalCaching;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use Psr\Log\LoggerInterface;
  11. use Sabre\DAV\Exception\BadRequest;
  12. use Sabre\DAV\Exception\Forbidden;
  13. use Sabre\DAV\PropPatch;
  14. use Sabre\VObject\Component;
  15. use Sabre\VObject\DateTimeParser;
  16. use Sabre\VObject\InvalidDataException;
  17. use Sabre\VObject\ParseException;
  18. use Sabre\VObject\Reader;
  19. use Sabre\VObject\Recur\NoInstancesException;
  20. use Sabre\VObject\Splitter\ICalendar;
  21. use Sabre\VObject\UUIDUtil;
  22. use function count;
  23. class RefreshWebcalService {
  24. public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
  25. public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
  26. public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
  27. public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
  28. public function __construct(
  29. private CalDavBackend $calDavBackend,
  30. private LoggerInterface $logger,
  31. private Connection $connection,
  32. private ITimeFactory $time,
  33. ) {
  34. }
  35. public function refreshSubscription(string $principalUri, string $uri) {
  36. $subscription = $this->getSubscription($principalUri, $uri);
  37. $mutations = [];
  38. if (!$subscription) {
  39. return;
  40. }
  41. // Check the refresh rate if there is any
  42. if (!empty($subscription['{http://apple.com/ns/ical/}refreshrate'])) {
  43. // add the refresh interval to the lastmodified timestamp
  44. $refreshInterval = new \DateInterval($subscription['{http://apple.com/ns/ical/}refreshrate']);
  45. $updateTime = $this->time->getDateTime();
  46. $updateTime->setTimestamp($subscription['lastmodified'])->add($refreshInterval);
  47. if ($updateTime->getTimestamp() > $this->time->getTime()) {
  48. return;
  49. }
  50. }
  51. $webcalData = $this->connection->queryWebcalFeed($subscription);
  52. if (!$webcalData) {
  53. return;
  54. }
  55. $localData = $this->calDavBackend->getLimitedCalendarObjects((int)$subscription['id'], CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  56. $stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1;
  57. $stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1;
  58. $stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1;
  59. try {
  60. $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING);
  61. while ($vObject = $splitter->getNext()) {
  62. /** @var Component $vObject */
  63. $compName = null;
  64. $uid = null;
  65. foreach ($vObject->getComponents() as $component) {
  66. if ($component->name === 'VTIMEZONE') {
  67. continue;
  68. }
  69. $compName = $component->name;
  70. if ($stripAlarms) {
  71. unset($component->{'VALARM'});
  72. }
  73. if ($stripAttachments) {
  74. unset($component->{'ATTACH'});
  75. }
  76. $uid = $component->{ 'UID' }->getValue();
  77. }
  78. if ($stripTodos && $compName === 'VTODO') {
  79. continue;
  80. }
  81. if (!isset($uid)) {
  82. continue;
  83. }
  84. try {
  85. $denormalized = $this->calDavBackend->getDenormalizedData($vObject->serialize());
  86. } catch (InvalidDataException|Forbidden $ex) {
  87. $this->logger->warning('Unable to denormalize calendar object from subscription {subscriptionId}', ['exception' => $ex, 'subscriptionId' => $subscription['id'], 'source' => $subscription['source']]);
  88. continue;
  89. }
  90. // Find all identical sets and remove them from the update
  91. if (isset($localData[$uid]) && $denormalized['etag'] === $localData[$uid]['etag']) {
  92. unset($localData[$uid]);
  93. continue;
  94. }
  95. $vObjectCopy = clone $vObject;
  96. $identical = isset($localData[$uid]) && $this->compareWithoutDtstamp($vObjectCopy, $localData[$uid]);
  97. if ($identical) {
  98. unset($localData[$uid]);
  99. continue;
  100. }
  101. // Find all modified sets and update them
  102. if (isset($localData[$uid]) && $denormalized['etag'] !== $localData[$uid]['etag']) {
  103. $this->calDavBackend->updateCalendarObject($subscription['id'], $localData[$uid]['uri'], $vObject->serialize(), CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  104. unset($localData[$uid]);
  105. continue;
  106. }
  107. // Only entirely new events get created here
  108. try {
  109. $objectUri = $this->getRandomCalendarObjectUri();
  110. $this->calDavBackend->createCalendarObject($subscription['id'], $objectUri, $vObject->serialize(), CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  111. } catch (NoInstancesException|BadRequest $ex) {
  112. $this->logger->warning('Unable to create calendar object from subscription {subscriptionId}', ['exception' => $ex, 'subscriptionId' => $subscription['id'], 'source' => $subscription['source']]);
  113. }
  114. }
  115. $ids = array_map(static function ($dataSet): int {
  116. return (int)$dataSet['id'];
  117. }, $localData);
  118. $uris = array_map(static function ($dataSet): string {
  119. return $dataSet['uri'];
  120. }, $localData);
  121. if (!empty($ids) && !empty($uris)) {
  122. // Clean up on aisle 5
  123. // The only events left over in the $localData array should be those that don't exist upstream
  124. // All deleted VObjects from upstream are removed
  125. $this->calDavBackend->purgeCachedEventsForSubscription($subscription['id'], $ids, $uris);
  126. }
  127. $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData);
  128. if ($newRefreshRate) {
  129. $mutations[self::REFRESH_RATE] = $newRefreshRate;
  130. }
  131. $this->updateSubscription($subscription, $mutations);
  132. } catch (ParseException $ex) {
  133. $this->logger->error('Subscription {subscriptionId} could not be refreshed due to a parsing error', ['exception' => $ex, 'subscriptionId' => $subscription['id']]);
  134. }
  135. }
  136. /**
  137. * loads subscription from backend
  138. */
  139. public function getSubscription(string $principalUri, string $uri): ?array {
  140. $subscriptions = array_values(array_filter(
  141. $this->calDavBackend->getSubscriptionsForUser($principalUri),
  142. function ($sub) use ($uri) {
  143. return $sub['uri'] === $uri;
  144. }
  145. ));
  146. if (count($subscriptions) === 0) {
  147. return null;
  148. }
  149. return $subscriptions[0];
  150. }
  151. /**
  152. * check if:
  153. * - current subscription stores a refreshrate
  154. * - the webcal feed suggests a refreshrate
  155. * - return suggested refreshrate if user didn't set a custom one
  156. *
  157. */
  158. private function checkWebcalDataForRefreshRate(array $subscription, string $webcalData): ?string {
  159. // if there is no refreshrate stored in the database, check the webcal feed
  160. // whether it suggests any refresh rate and store that in the database
  161. if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
  162. return null;
  163. }
  164. /** @var Component\VCalendar $vCalendar */
  165. $vCalendar = Reader::read($webcalData);
  166. $newRefreshRate = null;
  167. if (isset($vCalendar->{'X-PUBLISHED-TTL'})) {
  168. $newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
  169. }
  170. if (isset($vCalendar->{'REFRESH-INTERVAL'})) {
  171. $newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
  172. }
  173. if (!$newRefreshRate) {
  174. return null;
  175. }
  176. // check if new refresh rate is even valid
  177. try {
  178. DateTimeParser::parseDuration($newRefreshRate);
  179. } catch (InvalidDataException $ex) {
  180. return null;
  181. }
  182. return $newRefreshRate;
  183. }
  184. /**
  185. * update subscription stored in database
  186. * used to set:
  187. * - refreshrate
  188. * - source
  189. *
  190. * @param array $subscription
  191. * @param array $mutations
  192. */
  193. private function updateSubscription(array $subscription, array $mutations) {
  194. if (empty($mutations)) {
  195. return;
  196. }
  197. $propPatch = new PropPatch($mutations);
  198. $this->calDavBackend->updateSubscription($subscription['id'], $propPatch);
  199. $propPatch->commit();
  200. }
  201. /**
  202. * Returns a random uri for a calendar-object
  203. *
  204. * @return string
  205. */
  206. public function getRandomCalendarObjectUri():string {
  207. return UUIDUtil::getUUID() . '.ics';
  208. }
  209. private function compareWithoutDtstamp(Component $vObject, array $calendarObject): bool {
  210. foreach ($vObject->getComponents() as $component) {
  211. unset($component->{'DTSTAMP'});
  212. }
  213. $localVobject = Reader::read($calendarObject['calendardata']);
  214. foreach ($localVobject->getComponents() as $component) {
  215. unset($component->{'DTSTAMP'});
  216. }
  217. return strcasecmp($localVobject->serialize(), $vObject->serialize()) === 0;
  218. }
  219. }