|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\CalDAV\Schedule; |
| 11 | + |
| 12 | +use OCA\DAV\CalDAV\CalendarObject; |
| 13 | +use Sabre\DAV\INode; |
| 14 | +use Sabre\DAV\Server; |
| 15 | +use Sabre\DAV\ServerPlugin; |
| 16 | +use Sabre\VObject\Component\VCalendar; |
| 17 | +use Sabre\VObject\Reader; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +/** |
| 21 | + * Captures the iCalendar payload of a calendar object before it is overwritten |
| 22 | + * (PUT) or deleted (DELETE), and stashes it in DelegateActionContext so that |
| 23 | + * CalendarDelegateActionListener can diff old vs new when it fires later in |
| 24 | + * the same request. |
| 25 | + */ |
| 26 | +class DelegateActionCapturePlugin extends ServerPlugin { |
| 27 | + |
| 28 | + private ?Server $server = null; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly DelegateActionContext $context, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + #[\Override] |
| 36 | + public function initialize(Server $server): void { |
| 37 | + $this->server = $server; |
| 38 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10); |
| 39 | + $server->on('beforeUnbind', [$this, 'beforeUnbind'], 10); |
| 40 | + } |
| 41 | + |
| 42 | + #[\Override] |
| 43 | + public function getPluginName(): string { |
| 44 | + return 'nc-delegate-action-capture'; |
| 45 | + } |
| 46 | + |
| 47 | + public function beforeWriteContent(string $uri, INode $node, $data, $modified): void { |
| 48 | + $this->capture($node); |
| 49 | + } |
| 50 | + |
| 51 | + public function beforeUnbind(string $path): void { |
| 52 | + if ($this->server === null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + try { |
| 56 | + $node = $this->server->tree->getNodeForPath($path); |
| 57 | + } catch (Throwable) { |
| 58 | + return; |
| 59 | + } |
| 60 | + $this->capture($node); |
| 61 | + } |
| 62 | + |
| 63 | + private function capture(INode $node): void { |
| 64 | + if (!$node instanceof CalendarObject) { |
| 65 | + return; |
| 66 | + } |
| 67 | + try { |
| 68 | + $raw = $node->get(); |
| 69 | + if ($raw === '') { |
| 70 | + return; |
| 71 | + } |
| 72 | + $vCalendar = Reader::read($raw); |
| 73 | + if ($vCalendar instanceof VCalendar) { |
| 74 | + $this->context->setPrevious($vCalendar); |
| 75 | + } |
| 76 | + } catch (Throwable) { |
| 77 | + // Capture is best-effort: swallow malformed iCalendar. |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments