|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2017, Georg Ehrke <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Christoph Wurst <[email protected]> |
|
9
|
|
|
* @author Georg Ehrke <[email protected]> |
|
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
namespace OCA\DAV\CalDAV; |
|
29
|
|
|
|
|
30
|
|
|
use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin; |
|
31
|
|
|
use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer; |
|
32
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
33
|
|
|
use OCP\Calendar\Exceptions\CalendarException; |
|
34
|
|
|
use OCP\Calendar\ICreateFromString; |
|
35
|
|
|
use OCP\Constants; |
|
36
|
|
|
use OCP\Security\ISecureRandom; |
|
37
|
|
|
use Psr\Log\LoggerInterface; |
|
38
|
|
|
use Sabre\DAV\Exception\Conflict; |
|
39
|
|
|
use Sabre\VObject\Component\VCalendar; |
|
40
|
|
|
use Sabre\VObject\Component\VEvent; |
|
41
|
|
|
use Sabre\VObject\Document; |
|
42
|
|
|
use Sabre\VObject\ITip\Message; |
|
43
|
|
|
use Sabre\VObject\Property\VCard\DateTime; |
|
44
|
|
|
use Sabre\VObject\Reader; |
|
45
|
|
|
use function Sabre\Uri\split as uriSplit; |
|
46
|
|
|
|
|
47
|
|
|
class CalendarImpl implements ICreateFromString { |
|
48
|
|
|
|
|
49
|
|
|
private CalDavBackend $backend; |
|
50
|
|
|
private Calendar $calendar; |
|
51
|
|
|
/** @var array<string, mixed> */ |
|
52
|
|
|
private array $calendarInfo; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct(Calendar $calendar, |
|
55
|
|
|
array $calendarInfo, |
|
56
|
|
|
CalDavBackend $backend) { |
|
57
|
|
|
$this->calendar = $calendar; |
|
58
|
|
|
$this->calendarInfo = $calendarInfo; |
|
59
|
|
|
$this->backend = $backend; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string defining the technical unique key |
|
64
|
|
|
* @since 13.0.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getKey(): string { |
|
67
|
|
|
return (string) $this->calendarInfo['id']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getUri(): string { |
|
74
|
|
|
return $this->calendarInfo['uri']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* In comparison to getKey() this function returns a human readable (maybe translated) name |
|
79
|
|
|
* @since 13.0.0 |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getDisplayName(): ?string { |
|
82
|
|
|
return $this->calendarInfo['{DAV:}displayname']; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Calendar color |
|
87
|
|
|
* @since 13.0.0 |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getDisplayColor(): ?string { |
|
90
|
|
|
return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $pattern which should match within the $searchProperties |
|
95
|
|
|
* @param array $searchProperties defines the properties within the query pattern should match |
|
96
|
|
|
* @param array $options - optional parameters: |
|
97
|
|
|
* ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]] |
|
98
|
|
|
* @param int|null $limit - limit number of search results |
|
99
|
|
|
* @param int|null $offset - offset for paging of search results |
|
100
|
|
|
* @return array an array of events/journals/todos which are arrays of key-value-pairs |
|
101
|
|
|
* @since 13.0.0 |
|
102
|
|
|
*/ |
|
103
|
|
|
public function search(string $pattern, array $searchProperties = [], array $options = [], $limit = null, $offset = null): array { |
|
104
|
|
|
return $this->backend->search($this->calendarInfo, $pattern, |
|
105
|
|
|
$searchProperties, $options, $limit, $offset); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return int build up using \OCP\Constants |
|
110
|
|
|
* @since 13.0.0 |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getPermissions(): int { |
|
113
|
|
|
$permissions = $this->calendar->getACL(); |
|
114
|
|
|
$result = 0; |
|
115
|
|
|
foreach ($permissions as $permission) { |
|
116
|
|
|
switch ($permission['privilege']) { |
|
117
|
|
|
case '{DAV:}read': |
|
118
|
|
|
$result |= Constants::PERMISSION_READ; |
|
119
|
|
|
break; |
|
120
|
|
|
case '{DAV:}write': |
|
121
|
|
|
$result |= Constants::PERMISSION_CREATE; |
|
122
|
|
|
$result |= Constants::PERMISSION_UPDATE; |
|
123
|
|
|
break; |
|
124
|
|
|
case '{DAV:}all': |
|
125
|
|
|
$result |= Constants::PERMISSION_ALL; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @since 26.0.0 |
|
135
|
|
|
*/ |
|
136
|
|
|
public function isDeleted(): bool { |
|
137
|
|
|
return $this->calendar->isDeleted(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Create a new calendar event for this calendar |
|
142
|
|
|
* by way of an ICS string |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $name the file name - needs to contain the .ics ending |
|
145
|
|
|
* @param string $calendarData a string containing a valid VEVENT ics |
|
146
|
|
|
* |
|
147
|
|
|
* @throws CalendarException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function createFromString(string $name, string $calendarData): void { |
|
150
|
|
|
$server = new InvitationResponseServer(false); |
|
151
|
|
|
|
|
152
|
|
|
/** @var CustomPrincipalPlugin $plugin */ |
|
153
|
|
|
$plugin = $server->server->getPlugin('auth'); |
|
154
|
|
|
// we're working around the previous implementation |
|
155
|
|
|
// that only allowed the public system principal to be used |
|
156
|
|
|
// so set the custom principal here |
|
157
|
|
|
$plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
158
|
|
|
|
|
159
|
|
|
if (empty($this->calendarInfo['uri'])) { |
|
160
|
|
|
throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Build full calendar path |
|
164
|
|
|
[, $user] = uriSplit($this->calendar->getPrincipalURI()); |
|
165
|
|
|
$fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name); |
|
166
|
|
|
|
|
167
|
|
|
// Force calendar change URI |
|
168
|
|
|
/** @var Schedule\Plugin $schedulingPlugin */ |
|
169
|
|
|
$schedulingPlugin = $server->server->getPlugin('caldav-schedule'); |
|
170
|
|
|
$schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename); |
|
171
|
|
|
|
|
172
|
|
|
$stream = fopen('php://memory', 'rb+'); |
|
173
|
|
|
fwrite($stream, $calendarData); |
|
174
|
|
|
rewind($stream); |
|
175
|
|
|
try { |
|
176
|
|
|
$server->server->createFile($fullCalendarFilename, $stream); |
|
177
|
|
|
} catch (Conflict $e) { |
|
178
|
|
|
throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e); |
|
179
|
|
|
} finally { |
|
180
|
|
|
fclose($stream); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @throws CalendarException |
|
186
|
|
|
*/ |
|
187
|
|
|
public function handleIMipMessage(string $name, string $calendarData): void { |
|
188
|
|
|
$server = new InvitationResponseServer(false); |
|
189
|
|
|
|
|
190
|
|
|
/** @var CustomPrincipalPlugin $plugin */ |
|
191
|
|
|
$plugin = $server->server->getPlugin('auth'); |
|
192
|
|
|
// we're working around the previous implementation |
|
193
|
|
|
// that only allowed the public system principal to be used |
|
194
|
|
|
// so set the custom principal here |
|
195
|
|
|
$plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
196
|
|
|
|
|
197
|
|
|
if (empty($this->calendarInfo['uri'])) { |
|
198
|
|
|
throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
199
|
|
|
} |
|
200
|
|
|
// Force calendar change URI |
|
201
|
|
|
/** @var Schedule\Plugin $schedulingPlugin */ |
|
202
|
|
|
$schedulingPlugin = $server->server->getPlugin('caldav-schedule'); |
|
203
|
|
|
// Let sabre handle the rest |
|
204
|
|
|
$iTipMessage = new Message(); |
|
205
|
|
|
/** @var VCalendar $vObject */ |
|
206
|
|
|
$vObject = Reader::read($calendarData); |
|
207
|
|
|
/** @var VEvent $vEvent */ |
|
208
|
|
|
$vEvent = $vObject->{'VEVENT'}; |
|
209
|
|
|
|
|
210
|
|
|
if($vObject->{'METHOD'} === null) { |
|
211
|
|
|
throw new CalendarException('No Method provided for scheduling data. Could not process message'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if(!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) { |
|
215
|
|
|
throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL'); |
|
216
|
|
|
} |
|
217
|
|
|
$orgaizer = $vEvent->{'ORGANIZER'}->getValue(); |
|
218
|
|
|
$attendee = $vEvent->{'ATTENDEE'}->getValue(); |
|
219
|
|
|
|
|
220
|
|
|
$iTipMessage->method = $vObject->{'METHOD'}->getValue(); |
|
221
|
|
|
if($iTipMessage->method === 'REPLY') { |
|
222
|
|
|
if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) { |
|
223
|
|
|
$iTipMessage->recipient = $orgaizer; |
|
224
|
|
|
} else { |
|
225
|
|
|
$iTipMessage->recipient = $attendee; |
|
226
|
|
|
} |
|
227
|
|
|
$iTipMessage->sender = $attendee; |
|
228
|
|
|
} else if($iTipMessage->method === 'CANCEL') { |
|
229
|
|
|
$iTipMessage->recipient = $attendee; |
|
230
|
|
|
$iTipMessage->sender = $orgaizer; |
|
231
|
|
|
} |
|
232
|
|
|
$iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : ''; |
|
233
|
|
|
$iTipMessage->component = 'VEVENT'; |
|
234
|
|
|
$iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0; |
|
235
|
|
|
$iTipMessage->message = $vObject; |
|
236
|
|
|
$schedulingPlugin->scheduleLocalDelivery($iTipMessage); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|