@@ -42,197 +42,197 @@ |
||
42 | 42 | use function Sabre\Uri\split as uriSplit; |
43 | 43 | |
44 | 44 | class CalendarImpl implements ICreateFromString, IHandleImipMessage { |
45 | - private CalDavBackend $backend; |
|
46 | - private Calendar $calendar; |
|
47 | - /** @var array<string, mixed> */ |
|
48 | - private array $calendarInfo; |
|
49 | - |
|
50 | - public function __construct(Calendar $calendar, |
|
51 | - array $calendarInfo, |
|
52 | - CalDavBackend $backend) { |
|
53 | - $this->calendar = $calendar; |
|
54 | - $this->calendarInfo = $calendarInfo; |
|
55 | - $this->backend = $backend; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @return string defining the technical unique key |
|
60 | - * @since 13.0.0 |
|
61 | - */ |
|
62 | - public function getKey(): string { |
|
63 | - return (string) $this->calendarInfo['id']; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritDoc} |
|
68 | - */ |
|
69 | - public function getUri(): string { |
|
70 | - return $this->calendarInfo['uri']; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
75 | - * @since 13.0.0 |
|
76 | - */ |
|
77 | - public function getDisplayName(): ?string { |
|
78 | - return $this->calendarInfo['{DAV:}displayname']; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Calendar color |
|
83 | - * @since 13.0.0 |
|
84 | - */ |
|
85 | - public function getDisplayColor(): ?string { |
|
86 | - return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color']; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $pattern which should match within the $searchProperties |
|
91 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
92 | - * @param array $options - optional parameters: |
|
93 | - * ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]] |
|
94 | - * @param int|null $limit - limit number of search results |
|
95 | - * @param int|null $offset - offset for paging of search results |
|
96 | - * @return array an array of events/journals/todos which are arrays of key-value-pairs |
|
97 | - * @since 13.0.0 |
|
98 | - */ |
|
99 | - public function search(string $pattern, array $searchProperties = [], array $options = [], $limit = null, $offset = null): array { |
|
100 | - return $this->backend->search($this->calendarInfo, $pattern, |
|
101 | - $searchProperties, $options, $limit, $offset); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return int build up using \OCP\Constants |
|
106 | - * @since 13.0.0 |
|
107 | - */ |
|
108 | - public function getPermissions(): int { |
|
109 | - $permissions = $this->calendar->getACL(); |
|
110 | - $result = 0; |
|
111 | - foreach ($permissions as $permission) { |
|
112 | - switch ($permission['privilege']) { |
|
113 | - case '{DAV:}read': |
|
114 | - $result |= Constants::PERMISSION_READ; |
|
115 | - break; |
|
116 | - case '{DAV:}write': |
|
117 | - $result |= Constants::PERMISSION_CREATE; |
|
118 | - $result |= Constants::PERMISSION_UPDATE; |
|
119 | - break; |
|
120 | - case '{DAV:}all': |
|
121 | - $result |= Constants::PERMISSION_ALL; |
|
122 | - break; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - return $result; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @since 26.0.0 |
|
131 | - */ |
|
132 | - public function isDeleted(): bool { |
|
133 | - return $this->calendar->isDeleted(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Create a new calendar event for this calendar |
|
138 | - * by way of an ICS string |
|
139 | - * |
|
140 | - * @param string $name the file name - needs to contain the .ics ending |
|
141 | - * @param string $calendarData a string containing a valid VEVENT ics |
|
142 | - * |
|
143 | - * @throws CalendarException |
|
144 | - */ |
|
145 | - public function createFromString(string $name, string $calendarData): void { |
|
146 | - $server = new InvitationResponseServer(false); |
|
147 | - |
|
148 | - /** @var CustomPrincipalPlugin $plugin */ |
|
149 | - $plugin = $server->getServer()->getPlugin('auth'); |
|
150 | - // we're working around the previous implementation |
|
151 | - // that only allowed the public system principal to be used |
|
152 | - // so set the custom principal here |
|
153 | - $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
154 | - |
|
155 | - if (empty($this->calendarInfo['uri'])) { |
|
156 | - throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
157 | - } |
|
158 | - |
|
159 | - // Build full calendar path |
|
160 | - [, $user] = uriSplit($this->calendar->getPrincipalURI()); |
|
161 | - $fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name); |
|
162 | - |
|
163 | - // Force calendar change URI |
|
164 | - /** @var Schedule\Plugin $schedulingPlugin */ |
|
165 | - $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); |
|
166 | - $schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename); |
|
167 | - |
|
168 | - $stream = fopen('php://memory', 'rb+'); |
|
169 | - fwrite($stream, $calendarData); |
|
170 | - rewind($stream); |
|
171 | - try { |
|
172 | - $server->getServer()->createFile($fullCalendarFilename, $stream); |
|
173 | - } catch (Conflict $e) { |
|
174 | - throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e); |
|
175 | - } finally { |
|
176 | - fclose($stream); |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @throws CalendarException |
|
182 | - */ |
|
183 | - public function handleIMipMessage(string $name, string $calendarData): void { |
|
184 | - $server = $this->getInvitationResponseServer(); |
|
185 | - |
|
186 | - /** @var CustomPrincipalPlugin $plugin */ |
|
187 | - $plugin = $server->getServer()->getPlugin('auth'); |
|
188 | - // we're working around the previous implementation |
|
189 | - // that only allowed the public system principal to be used |
|
190 | - // so set the custom principal here |
|
191 | - $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
192 | - |
|
193 | - if (empty($this->calendarInfo['uri'])) { |
|
194 | - throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
195 | - } |
|
196 | - // Force calendar change URI |
|
197 | - /** @var Schedule\Plugin $schedulingPlugin */ |
|
198 | - $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); |
|
199 | - // Let sabre handle the rest |
|
200 | - $iTipMessage = new Message(); |
|
201 | - /** @var VCalendar $vObject */ |
|
202 | - $vObject = Reader::read($calendarData); |
|
203 | - /** @var VEvent $vEvent */ |
|
204 | - $vEvent = $vObject->{'VEVENT'}; |
|
205 | - |
|
206 | - if ($vObject->{'METHOD'} === null) { |
|
207 | - throw new CalendarException('No Method provided for scheduling data. Could not process message'); |
|
208 | - } |
|
209 | - |
|
210 | - if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) { |
|
211 | - throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL'); |
|
212 | - } |
|
213 | - $organizer = $vEvent->{'ORGANIZER'}->getValue(); |
|
214 | - $attendee = $vEvent->{'ATTENDEE'}->getValue(); |
|
215 | - |
|
216 | - $iTipMessage->method = $vObject->{'METHOD'}->getValue(); |
|
217 | - if ($iTipMessage->method === 'REPLY') { |
|
218 | - if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) { |
|
219 | - $iTipMessage->recipient = $organizer; |
|
220 | - } else { |
|
221 | - $iTipMessage->recipient = $attendee; |
|
222 | - } |
|
223 | - $iTipMessage->sender = $attendee; |
|
224 | - } elseif ($iTipMessage->method === 'CANCEL') { |
|
225 | - $iTipMessage->recipient = $attendee; |
|
226 | - $iTipMessage->sender = $organizer; |
|
227 | - } |
|
228 | - $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : ''; |
|
229 | - $iTipMessage->component = 'VEVENT'; |
|
230 | - $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0; |
|
231 | - $iTipMessage->message = $vObject; |
|
232 | - $server->server->emit('schedule', [$iTipMessage]); |
|
233 | - } |
|
234 | - |
|
235 | - public function getInvitationResponseServer(): InvitationResponseServer { |
|
236 | - return new InvitationResponseServer(false); |
|
237 | - } |
|
45 | + private CalDavBackend $backend; |
|
46 | + private Calendar $calendar; |
|
47 | + /** @var array<string, mixed> */ |
|
48 | + private array $calendarInfo; |
|
49 | + |
|
50 | + public function __construct(Calendar $calendar, |
|
51 | + array $calendarInfo, |
|
52 | + CalDavBackend $backend) { |
|
53 | + $this->calendar = $calendar; |
|
54 | + $this->calendarInfo = $calendarInfo; |
|
55 | + $this->backend = $backend; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @return string defining the technical unique key |
|
60 | + * @since 13.0.0 |
|
61 | + */ |
|
62 | + public function getKey(): string { |
|
63 | + return (string) $this->calendarInfo['id']; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritDoc} |
|
68 | + */ |
|
69 | + public function getUri(): string { |
|
70 | + return $this->calendarInfo['uri']; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
75 | + * @since 13.0.0 |
|
76 | + */ |
|
77 | + public function getDisplayName(): ?string { |
|
78 | + return $this->calendarInfo['{DAV:}displayname']; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Calendar color |
|
83 | + * @since 13.0.0 |
|
84 | + */ |
|
85 | + public function getDisplayColor(): ?string { |
|
86 | + return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color']; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $pattern which should match within the $searchProperties |
|
91 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
92 | + * @param array $options - optional parameters: |
|
93 | + * ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]] |
|
94 | + * @param int|null $limit - limit number of search results |
|
95 | + * @param int|null $offset - offset for paging of search results |
|
96 | + * @return array an array of events/journals/todos which are arrays of key-value-pairs |
|
97 | + * @since 13.0.0 |
|
98 | + */ |
|
99 | + public function search(string $pattern, array $searchProperties = [], array $options = [], $limit = null, $offset = null): array { |
|
100 | + return $this->backend->search($this->calendarInfo, $pattern, |
|
101 | + $searchProperties, $options, $limit, $offset); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return int build up using \OCP\Constants |
|
106 | + * @since 13.0.0 |
|
107 | + */ |
|
108 | + public function getPermissions(): int { |
|
109 | + $permissions = $this->calendar->getACL(); |
|
110 | + $result = 0; |
|
111 | + foreach ($permissions as $permission) { |
|
112 | + switch ($permission['privilege']) { |
|
113 | + case '{DAV:}read': |
|
114 | + $result |= Constants::PERMISSION_READ; |
|
115 | + break; |
|
116 | + case '{DAV:}write': |
|
117 | + $result |= Constants::PERMISSION_CREATE; |
|
118 | + $result |= Constants::PERMISSION_UPDATE; |
|
119 | + break; |
|
120 | + case '{DAV:}all': |
|
121 | + $result |= Constants::PERMISSION_ALL; |
|
122 | + break; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + return $result; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @since 26.0.0 |
|
131 | + */ |
|
132 | + public function isDeleted(): bool { |
|
133 | + return $this->calendar->isDeleted(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Create a new calendar event for this calendar |
|
138 | + * by way of an ICS string |
|
139 | + * |
|
140 | + * @param string $name the file name - needs to contain the .ics ending |
|
141 | + * @param string $calendarData a string containing a valid VEVENT ics |
|
142 | + * |
|
143 | + * @throws CalendarException |
|
144 | + */ |
|
145 | + public function createFromString(string $name, string $calendarData): void { |
|
146 | + $server = new InvitationResponseServer(false); |
|
147 | + |
|
148 | + /** @var CustomPrincipalPlugin $plugin */ |
|
149 | + $plugin = $server->getServer()->getPlugin('auth'); |
|
150 | + // we're working around the previous implementation |
|
151 | + // that only allowed the public system principal to be used |
|
152 | + // so set the custom principal here |
|
153 | + $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
154 | + |
|
155 | + if (empty($this->calendarInfo['uri'])) { |
|
156 | + throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
157 | + } |
|
158 | + |
|
159 | + // Build full calendar path |
|
160 | + [, $user] = uriSplit($this->calendar->getPrincipalURI()); |
|
161 | + $fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name); |
|
162 | + |
|
163 | + // Force calendar change URI |
|
164 | + /** @var Schedule\Plugin $schedulingPlugin */ |
|
165 | + $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); |
|
166 | + $schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename); |
|
167 | + |
|
168 | + $stream = fopen('php://memory', 'rb+'); |
|
169 | + fwrite($stream, $calendarData); |
|
170 | + rewind($stream); |
|
171 | + try { |
|
172 | + $server->getServer()->createFile($fullCalendarFilename, $stream); |
|
173 | + } catch (Conflict $e) { |
|
174 | + throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e); |
|
175 | + } finally { |
|
176 | + fclose($stream); |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @throws CalendarException |
|
182 | + */ |
|
183 | + public function handleIMipMessage(string $name, string $calendarData): void { |
|
184 | + $server = $this->getInvitationResponseServer(); |
|
185 | + |
|
186 | + /** @var CustomPrincipalPlugin $plugin */ |
|
187 | + $plugin = $server->getServer()->getPlugin('auth'); |
|
188 | + // we're working around the previous implementation |
|
189 | + // that only allowed the public system principal to be used |
|
190 | + // so set the custom principal here |
|
191 | + $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI()); |
|
192 | + |
|
193 | + if (empty($this->calendarInfo['uri'])) { |
|
194 | + throw new CalendarException('Could not write to calendar as URI parameter is missing'); |
|
195 | + } |
|
196 | + // Force calendar change URI |
|
197 | + /** @var Schedule\Plugin $schedulingPlugin */ |
|
198 | + $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule'); |
|
199 | + // Let sabre handle the rest |
|
200 | + $iTipMessage = new Message(); |
|
201 | + /** @var VCalendar $vObject */ |
|
202 | + $vObject = Reader::read($calendarData); |
|
203 | + /** @var VEvent $vEvent */ |
|
204 | + $vEvent = $vObject->{'VEVENT'}; |
|
205 | + |
|
206 | + if ($vObject->{'METHOD'} === null) { |
|
207 | + throw new CalendarException('No Method provided for scheduling data. Could not process message'); |
|
208 | + } |
|
209 | + |
|
210 | + if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) { |
|
211 | + throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL'); |
|
212 | + } |
|
213 | + $organizer = $vEvent->{'ORGANIZER'}->getValue(); |
|
214 | + $attendee = $vEvent->{'ATTENDEE'}->getValue(); |
|
215 | + |
|
216 | + $iTipMessage->method = $vObject->{'METHOD'}->getValue(); |
|
217 | + if ($iTipMessage->method === 'REPLY') { |
|
218 | + if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) { |
|
219 | + $iTipMessage->recipient = $organizer; |
|
220 | + } else { |
|
221 | + $iTipMessage->recipient = $attendee; |
|
222 | + } |
|
223 | + $iTipMessage->sender = $attendee; |
|
224 | + } elseif ($iTipMessage->method === 'CANCEL') { |
|
225 | + $iTipMessage->recipient = $attendee; |
|
226 | + $iTipMessage->sender = $organizer; |
|
227 | + } |
|
228 | + $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : ''; |
|
229 | + $iTipMessage->component = 'VEVENT'; |
|
230 | + $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0; |
|
231 | + $iTipMessage->message = $vObject; |
|
232 | + $server->server->emit('schedule', [$iTipMessage]); |
|
233 | + } |
|
234 | + |
|
235 | + public function getInvitationResponseServer(): InvitationResponseServer { |
|
236 | + return new InvitationResponseServer(false); |
|
237 | + } |
|
238 | 238 | } |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | try { |
172 | 172 | $server->getServer()->createFile($fullCalendarFilename, $stream); |
173 | 173 | } catch (Conflict $e) { |
174 | - throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e); |
|
174 | + throw new CalendarException('Could not create new calendar event: '.$e->getMessage(), 0, $e); |
|
175 | 175 | } finally { |
176 | 176 | fclose($stream); |
177 | 177 | } |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | } |
228 | 228 | $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : ''; |
229 | 229 | $iTipMessage->component = 'VEVENT'; |
230 | - $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0; |
|
230 | + $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int) $vEvent->{'SEQUENCE'}->getValue() : 0; |
|
231 | 231 | $iTipMessage->message = $vObject; |
232 | 232 | $server->server->emit('schedule', [$iTipMessage]); |
233 | 233 | } |