@@ -2,8 +2,8 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV; |
4 | 4 | |
5 | -use Sabre\VObject; |
|
6 | 5 | use DateTime; |
6 | +use Sabre\VObject; |
|
7 | 7 | |
8 | 8 | /** |
9 | 9 | * CalendarQuery Validator |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
302 | 302 | |
303 | 303 | // Fire up the iterator! |
304 | - $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
304 | + $it = new VObject\Recur\EventIterator($component->parent->parent, (string) $component->parent->UID); |
|
305 | 305 | while ($it->valid()) { |
306 | 306 | $expandedEvent = $it->getEventObject(); |
307 | 307 | |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | return true; |
318 | 318 | } |
319 | 319 | |
320 | - if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
320 | + if ((string) $expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
321 | 321 | // This is an alarm with a non-relative trigger |
322 | 322 | // time, likely created by a buggy client. The |
323 | 323 | // implication is that every alarm in this |
@@ -20,356 +20,356 @@ |
||
20 | 20 | */ |
21 | 21 | class CalendarQueryValidator { |
22 | 22 | |
23 | - /** |
|
24 | - * Verify if a list of filters applies to the calendar data object |
|
25 | - * |
|
26 | - * The list of filters must be formatted as parsed by \Sabre\CalDAV\CalendarQueryParser |
|
27 | - * |
|
28 | - * @param VObject\Component $vObject |
|
29 | - * @param array $filters |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function validate(VObject\Component\VCalendar $vObject, array $filters) { |
|
33 | - |
|
34 | - // The top level object is always a component filter. |
|
35 | - // We'll parse it manually, as it's pretty simple. |
|
36 | - if ($vObject->name !== $filters['name']) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - return |
|
41 | - $this->validateCompFilters($vObject, $filters['comp-filters']) && |
|
42 | - $this->validatePropFilters($vObject, $filters['prop-filters']); |
|
43 | - |
|
44 | - |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * This method checks the validity of comp-filters. |
|
49 | - * |
|
50 | - * A list of comp-filters needs to be specified. Also the parent of the |
|
51 | - * component we're checking should be specified, not the component to check |
|
52 | - * itself. |
|
53 | - * |
|
54 | - * @param VObject\Component $parent |
|
55 | - * @param array $filters |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - protected function validateCompFilters(VObject\Component $parent, array $filters) { |
|
59 | - |
|
60 | - foreach ($filters as $filter) { |
|
61 | - |
|
62 | - $isDefined = isset($parent->{$filter['name']}); |
|
63 | - |
|
64 | - if ($filter['is-not-defined']) { |
|
65 | - |
|
66 | - if ($isDefined) { |
|
67 | - return false; |
|
68 | - } else { |
|
69 | - continue; |
|
70 | - } |
|
71 | - |
|
72 | - } |
|
73 | - if (!$isDefined) { |
|
74 | - return false; |
|
75 | - } |
|
76 | - |
|
77 | - if ($filter['time-range']) { |
|
78 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
79 | - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
80 | - continue 2; |
|
81 | - } |
|
82 | - } |
|
83 | - return false; |
|
84 | - } |
|
85 | - |
|
86 | - if (!$filter['comp-filters'] && !$filter['prop-filters']) { |
|
87 | - continue; |
|
88 | - } |
|
89 | - |
|
90 | - // If there are sub-filters, we need to find at least one component |
|
91 | - // for which the subfilters hold true. |
|
92 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
93 | - |
|
94 | - if ( |
|
95 | - $this->validateCompFilters($subComponent, $filter['comp-filters']) && |
|
96 | - $this->validatePropFilters($subComponent, $filter['prop-filters'])) { |
|
97 | - // We had a match, so this comp-filter succeeds |
|
98 | - continue 2; |
|
99 | - } |
|
100 | - |
|
101 | - } |
|
102 | - |
|
103 | - // If we got here it means there were sub-comp-filters or |
|
104 | - // sub-prop-filters and there was no match. This means this filter |
|
105 | - // needs to return false. |
|
106 | - return false; |
|
107 | - |
|
108 | - } |
|
109 | - |
|
110 | - // If we got here it means we got through all comp-filters alive so the |
|
111 | - // filters were all true. |
|
112 | - return true; |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * This method checks the validity of prop-filters. |
|
118 | - * |
|
119 | - * A list of prop-filters needs to be specified. Also the parent of the |
|
120 | - * property we're checking should be specified, not the property to check |
|
121 | - * itself. |
|
122 | - * |
|
123 | - * @param VObject\Component $parent |
|
124 | - * @param array $filters |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - protected function validatePropFilters(VObject\Component $parent, array $filters) { |
|
128 | - |
|
129 | - foreach ($filters as $filter) { |
|
130 | - |
|
131 | - $isDefined = isset($parent->{$filter['name']}); |
|
132 | - |
|
133 | - if ($filter['is-not-defined']) { |
|
134 | - |
|
135 | - if ($isDefined) { |
|
136 | - return false; |
|
137 | - } else { |
|
138 | - continue; |
|
139 | - } |
|
140 | - |
|
141 | - } |
|
142 | - if (!$isDefined) { |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - if ($filter['time-range']) { |
|
147 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
148 | - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
149 | - continue 2; |
|
150 | - } |
|
151 | - } |
|
152 | - return false; |
|
153 | - } |
|
154 | - |
|
155 | - if (!$filter['param-filters'] && !$filter['text-match']) { |
|
156 | - continue; |
|
157 | - } |
|
158 | - |
|
159 | - // If there are sub-filters, we need to find at least one property |
|
160 | - // for which the subfilters hold true. |
|
161 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
162 | - |
|
163 | - if ( |
|
164 | - $this->validateParamFilters($subComponent, $filter['param-filters']) && |
|
165 | - (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match'])) |
|
166 | - ) { |
|
167 | - // We had a match, so this prop-filter succeeds |
|
168 | - continue 2; |
|
169 | - } |
|
170 | - |
|
171 | - } |
|
172 | - |
|
173 | - // If we got here it means there were sub-param-filters or |
|
174 | - // text-match filters and there was no match. This means the |
|
175 | - // filter needs to return false. |
|
176 | - return false; |
|
177 | - |
|
178 | - } |
|
179 | - |
|
180 | - // If we got here it means we got through all prop-filters alive so the |
|
181 | - // filters were all true. |
|
182 | - return true; |
|
183 | - |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * This method checks the validity of param-filters. |
|
188 | - * |
|
189 | - * A list of param-filters needs to be specified. Also the parent of the |
|
190 | - * parameter we're checking should be specified, not the parameter to check |
|
191 | - * itself. |
|
192 | - * |
|
193 | - * @param VObject\Property $parent |
|
194 | - * @param array $filters |
|
195 | - * @return bool |
|
196 | - */ |
|
197 | - protected function validateParamFilters(VObject\Property $parent, array $filters) { |
|
198 | - |
|
199 | - foreach ($filters as $filter) { |
|
200 | - |
|
201 | - $isDefined = isset($parent[$filter['name']]); |
|
202 | - |
|
203 | - if ($filter['is-not-defined']) { |
|
204 | - |
|
205 | - if ($isDefined) { |
|
206 | - return false; |
|
207 | - } else { |
|
208 | - continue; |
|
209 | - } |
|
210 | - |
|
211 | - } |
|
212 | - if (!$isDefined) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - |
|
216 | - if (!$filter['text-match']) { |
|
217 | - continue; |
|
218 | - } |
|
219 | - |
|
220 | - // If there are sub-filters, we need to find at least one parameter |
|
221 | - // for which the subfilters hold true. |
|
222 | - foreach ($parent[$filter['name']]->getParts() as $paramPart) { |
|
223 | - |
|
224 | - if ($this->validateTextMatch($paramPart, $filter['text-match'])) { |
|
225 | - // We had a match, so this param-filter succeeds |
|
226 | - continue 2; |
|
227 | - } |
|
228 | - |
|
229 | - } |
|
230 | - |
|
231 | - // If we got here it means there was a text-match filter and there |
|
232 | - // were no matches. This means the filter needs to return false. |
|
233 | - return false; |
|
234 | - |
|
235 | - } |
|
236 | - |
|
237 | - // If we got here it means we got through all param-filters alive so the |
|
238 | - // filters were all true. |
|
239 | - return true; |
|
240 | - |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * This method checks the validity of a text-match. |
|
245 | - * |
|
246 | - * A single text-match should be specified as well as the specific property |
|
247 | - * or parameter we need to validate. |
|
248 | - * |
|
249 | - * @param VObject\Node|string $check Value to check against. |
|
250 | - * @param array $textMatch |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - protected function validateTextMatch($check, array $textMatch) { |
|
254 | - |
|
255 | - if ($check instanceof VObject\Node) { |
|
256 | - $check = $check->getValue(); |
|
257 | - } |
|
258 | - |
|
259 | - $isMatching = \Sabre\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']); |
|
260 | - |
|
261 | - return ($textMatch['negate-condition'] xor $isMatching); |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Validates if a component matches the given time range. |
|
267 | - * |
|
268 | - * This is all based on the rules specified in rfc4791, which are quite |
|
269 | - * complex. |
|
270 | - * |
|
271 | - * @param VObject\Node $component |
|
272 | - * @param DateTime $start |
|
273 | - * @param DateTime $end |
|
274 | - * @return bool |
|
275 | - */ |
|
276 | - protected function validateTimeRange(VObject\Node $component, $start, $end) { |
|
277 | - |
|
278 | - if (is_null($start)) { |
|
279 | - $start = new DateTime('1900-01-01'); |
|
280 | - } |
|
281 | - if (is_null($end)) { |
|
282 | - $end = new DateTime('3000-01-01'); |
|
283 | - } |
|
284 | - |
|
285 | - switch ($component->name) { |
|
286 | - |
|
287 | - case 'VEVENT' : |
|
288 | - case 'VTODO' : |
|
289 | - case 'VJOURNAL' : |
|
290 | - |
|
291 | - return $component->isInTimeRange($start, $end); |
|
292 | - |
|
293 | - case 'VALARM' : |
|
294 | - |
|
295 | - // If the valarm is wrapped in a recurring event, we need to |
|
296 | - // expand the recursions, and validate each. |
|
297 | - // |
|
298 | - // Our datamodel doesn't easily allow us to do this straight |
|
299 | - // in the VALARM component code, so this is a hack, and an |
|
300 | - // expensive one too. |
|
301 | - if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
|
302 | - |
|
303 | - // Fire up the iterator! |
|
304 | - $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
305 | - while ($it->valid()) { |
|
306 | - $expandedEvent = $it->getEventObject(); |
|
307 | - |
|
308 | - // We need to check from these expanded alarms, which |
|
309 | - // one is the first to trigger. Based on this, we can |
|
310 | - // determine if we can 'give up' expanding events. |
|
311 | - $firstAlarm = null; |
|
312 | - if ($expandedEvent->VALARM !== null) { |
|
313 | - foreach ($expandedEvent->VALARM as $expandedAlarm) { |
|
314 | - |
|
315 | - $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime(); |
|
316 | - if ($expandedAlarm->isInTimeRange($start, $end)) { |
|
317 | - return true; |
|
318 | - } |
|
319 | - |
|
320 | - if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
321 | - // This is an alarm with a non-relative trigger |
|
322 | - // time, likely created by a buggy client. The |
|
323 | - // implication is that every alarm in this |
|
324 | - // recurring event trigger at the exact same |
|
325 | - // time. It doesn't make sense to traverse |
|
326 | - // further. |
|
327 | - } else { |
|
328 | - // We store the first alarm as a means to |
|
329 | - // figure out when we can stop traversing. |
|
330 | - if (!$firstAlarm || $effectiveTrigger < $firstAlarm) { |
|
331 | - $firstAlarm = $effectiveTrigger; |
|
332 | - } |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - if (is_null($firstAlarm)) { |
|
337 | - // No alarm was found. |
|
338 | - // |
|
339 | - // Or technically: No alarm that will change for |
|
340 | - // every instance of the recurrence was found, |
|
341 | - // which means we can assume there was no match. |
|
342 | - return false; |
|
343 | - } |
|
344 | - if ($firstAlarm > $end) { |
|
345 | - return false; |
|
346 | - } |
|
347 | - $it->next(); |
|
348 | - } |
|
349 | - return false; |
|
350 | - } else { |
|
351 | - return $component->isInTimeRange($start, $end); |
|
352 | - } |
|
353 | - |
|
354 | - case 'VFREEBUSY' : |
|
355 | - throw new \Sabre\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components'); |
|
356 | - |
|
357 | - case 'COMPLETED' : |
|
358 | - case 'CREATED' : |
|
359 | - case 'DTEND' : |
|
360 | - case 'DTSTAMP' : |
|
361 | - case 'DTSTART' : |
|
362 | - case 'DUE' : |
|
363 | - case 'LAST-MODIFIED' : |
|
364 | - return ($start <= $component->getDateTime() && $end >= $component->getDateTime()); |
|
365 | - |
|
366 | - |
|
367 | - |
|
368 | - default : |
|
369 | - throw new \Sabre\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component'); |
|
370 | - |
|
371 | - } |
|
372 | - |
|
373 | - } |
|
23 | + /** |
|
24 | + * Verify if a list of filters applies to the calendar data object |
|
25 | + * |
|
26 | + * The list of filters must be formatted as parsed by \Sabre\CalDAV\CalendarQueryParser |
|
27 | + * |
|
28 | + * @param VObject\Component $vObject |
|
29 | + * @param array $filters |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function validate(VObject\Component\VCalendar $vObject, array $filters) { |
|
33 | + |
|
34 | + // The top level object is always a component filter. |
|
35 | + // We'll parse it manually, as it's pretty simple. |
|
36 | + if ($vObject->name !== $filters['name']) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + return |
|
41 | + $this->validateCompFilters($vObject, $filters['comp-filters']) && |
|
42 | + $this->validatePropFilters($vObject, $filters['prop-filters']); |
|
43 | + |
|
44 | + |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * This method checks the validity of comp-filters. |
|
49 | + * |
|
50 | + * A list of comp-filters needs to be specified. Also the parent of the |
|
51 | + * component we're checking should be specified, not the component to check |
|
52 | + * itself. |
|
53 | + * |
|
54 | + * @param VObject\Component $parent |
|
55 | + * @param array $filters |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + protected function validateCompFilters(VObject\Component $parent, array $filters) { |
|
59 | + |
|
60 | + foreach ($filters as $filter) { |
|
61 | + |
|
62 | + $isDefined = isset($parent->{$filter['name']}); |
|
63 | + |
|
64 | + if ($filter['is-not-defined']) { |
|
65 | + |
|
66 | + if ($isDefined) { |
|
67 | + return false; |
|
68 | + } else { |
|
69 | + continue; |
|
70 | + } |
|
71 | + |
|
72 | + } |
|
73 | + if (!$isDefined) { |
|
74 | + return false; |
|
75 | + } |
|
76 | + |
|
77 | + if ($filter['time-range']) { |
|
78 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
79 | + if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
80 | + continue 2; |
|
81 | + } |
|
82 | + } |
|
83 | + return false; |
|
84 | + } |
|
85 | + |
|
86 | + if (!$filter['comp-filters'] && !$filter['prop-filters']) { |
|
87 | + continue; |
|
88 | + } |
|
89 | + |
|
90 | + // If there are sub-filters, we need to find at least one component |
|
91 | + // for which the subfilters hold true. |
|
92 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
93 | + |
|
94 | + if ( |
|
95 | + $this->validateCompFilters($subComponent, $filter['comp-filters']) && |
|
96 | + $this->validatePropFilters($subComponent, $filter['prop-filters'])) { |
|
97 | + // We had a match, so this comp-filter succeeds |
|
98 | + continue 2; |
|
99 | + } |
|
100 | + |
|
101 | + } |
|
102 | + |
|
103 | + // If we got here it means there were sub-comp-filters or |
|
104 | + // sub-prop-filters and there was no match. This means this filter |
|
105 | + // needs to return false. |
|
106 | + return false; |
|
107 | + |
|
108 | + } |
|
109 | + |
|
110 | + // If we got here it means we got through all comp-filters alive so the |
|
111 | + // filters were all true. |
|
112 | + return true; |
|
113 | + |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * This method checks the validity of prop-filters. |
|
118 | + * |
|
119 | + * A list of prop-filters needs to be specified. Also the parent of the |
|
120 | + * property we're checking should be specified, not the property to check |
|
121 | + * itself. |
|
122 | + * |
|
123 | + * @param VObject\Component $parent |
|
124 | + * @param array $filters |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + protected function validatePropFilters(VObject\Component $parent, array $filters) { |
|
128 | + |
|
129 | + foreach ($filters as $filter) { |
|
130 | + |
|
131 | + $isDefined = isset($parent->{$filter['name']}); |
|
132 | + |
|
133 | + if ($filter['is-not-defined']) { |
|
134 | + |
|
135 | + if ($isDefined) { |
|
136 | + return false; |
|
137 | + } else { |
|
138 | + continue; |
|
139 | + } |
|
140 | + |
|
141 | + } |
|
142 | + if (!$isDefined) { |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + if ($filter['time-range']) { |
|
147 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
148 | + if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
149 | + continue 2; |
|
150 | + } |
|
151 | + } |
|
152 | + return false; |
|
153 | + } |
|
154 | + |
|
155 | + if (!$filter['param-filters'] && !$filter['text-match']) { |
|
156 | + continue; |
|
157 | + } |
|
158 | + |
|
159 | + // If there are sub-filters, we need to find at least one property |
|
160 | + // for which the subfilters hold true. |
|
161 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
162 | + |
|
163 | + if ( |
|
164 | + $this->validateParamFilters($subComponent, $filter['param-filters']) && |
|
165 | + (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match'])) |
|
166 | + ) { |
|
167 | + // We had a match, so this prop-filter succeeds |
|
168 | + continue 2; |
|
169 | + } |
|
170 | + |
|
171 | + } |
|
172 | + |
|
173 | + // If we got here it means there were sub-param-filters or |
|
174 | + // text-match filters and there was no match. This means the |
|
175 | + // filter needs to return false. |
|
176 | + return false; |
|
177 | + |
|
178 | + } |
|
179 | + |
|
180 | + // If we got here it means we got through all prop-filters alive so the |
|
181 | + // filters were all true. |
|
182 | + return true; |
|
183 | + |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * This method checks the validity of param-filters. |
|
188 | + * |
|
189 | + * A list of param-filters needs to be specified. Also the parent of the |
|
190 | + * parameter we're checking should be specified, not the parameter to check |
|
191 | + * itself. |
|
192 | + * |
|
193 | + * @param VObject\Property $parent |
|
194 | + * @param array $filters |
|
195 | + * @return bool |
|
196 | + */ |
|
197 | + protected function validateParamFilters(VObject\Property $parent, array $filters) { |
|
198 | + |
|
199 | + foreach ($filters as $filter) { |
|
200 | + |
|
201 | + $isDefined = isset($parent[$filter['name']]); |
|
202 | + |
|
203 | + if ($filter['is-not-defined']) { |
|
204 | + |
|
205 | + if ($isDefined) { |
|
206 | + return false; |
|
207 | + } else { |
|
208 | + continue; |
|
209 | + } |
|
210 | + |
|
211 | + } |
|
212 | + if (!$isDefined) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + |
|
216 | + if (!$filter['text-match']) { |
|
217 | + continue; |
|
218 | + } |
|
219 | + |
|
220 | + // If there are sub-filters, we need to find at least one parameter |
|
221 | + // for which the subfilters hold true. |
|
222 | + foreach ($parent[$filter['name']]->getParts() as $paramPart) { |
|
223 | + |
|
224 | + if ($this->validateTextMatch($paramPart, $filter['text-match'])) { |
|
225 | + // We had a match, so this param-filter succeeds |
|
226 | + continue 2; |
|
227 | + } |
|
228 | + |
|
229 | + } |
|
230 | + |
|
231 | + // If we got here it means there was a text-match filter and there |
|
232 | + // were no matches. This means the filter needs to return false. |
|
233 | + return false; |
|
234 | + |
|
235 | + } |
|
236 | + |
|
237 | + // If we got here it means we got through all param-filters alive so the |
|
238 | + // filters were all true. |
|
239 | + return true; |
|
240 | + |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * This method checks the validity of a text-match. |
|
245 | + * |
|
246 | + * A single text-match should be specified as well as the specific property |
|
247 | + * or parameter we need to validate. |
|
248 | + * |
|
249 | + * @param VObject\Node|string $check Value to check against. |
|
250 | + * @param array $textMatch |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + protected function validateTextMatch($check, array $textMatch) { |
|
254 | + |
|
255 | + if ($check instanceof VObject\Node) { |
|
256 | + $check = $check->getValue(); |
|
257 | + } |
|
258 | + |
|
259 | + $isMatching = \Sabre\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']); |
|
260 | + |
|
261 | + return ($textMatch['negate-condition'] xor $isMatching); |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Validates if a component matches the given time range. |
|
267 | + * |
|
268 | + * This is all based on the rules specified in rfc4791, which are quite |
|
269 | + * complex. |
|
270 | + * |
|
271 | + * @param VObject\Node $component |
|
272 | + * @param DateTime $start |
|
273 | + * @param DateTime $end |
|
274 | + * @return bool |
|
275 | + */ |
|
276 | + protected function validateTimeRange(VObject\Node $component, $start, $end) { |
|
277 | + |
|
278 | + if (is_null($start)) { |
|
279 | + $start = new DateTime('1900-01-01'); |
|
280 | + } |
|
281 | + if (is_null($end)) { |
|
282 | + $end = new DateTime('3000-01-01'); |
|
283 | + } |
|
284 | + |
|
285 | + switch ($component->name) { |
|
286 | + |
|
287 | + case 'VEVENT' : |
|
288 | + case 'VTODO' : |
|
289 | + case 'VJOURNAL' : |
|
290 | + |
|
291 | + return $component->isInTimeRange($start, $end); |
|
292 | + |
|
293 | + case 'VALARM' : |
|
294 | + |
|
295 | + // If the valarm is wrapped in a recurring event, we need to |
|
296 | + // expand the recursions, and validate each. |
|
297 | + // |
|
298 | + // Our datamodel doesn't easily allow us to do this straight |
|
299 | + // in the VALARM component code, so this is a hack, and an |
|
300 | + // expensive one too. |
|
301 | + if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
|
302 | + |
|
303 | + // Fire up the iterator! |
|
304 | + $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
305 | + while ($it->valid()) { |
|
306 | + $expandedEvent = $it->getEventObject(); |
|
307 | + |
|
308 | + // We need to check from these expanded alarms, which |
|
309 | + // one is the first to trigger. Based on this, we can |
|
310 | + // determine if we can 'give up' expanding events. |
|
311 | + $firstAlarm = null; |
|
312 | + if ($expandedEvent->VALARM !== null) { |
|
313 | + foreach ($expandedEvent->VALARM as $expandedAlarm) { |
|
314 | + |
|
315 | + $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime(); |
|
316 | + if ($expandedAlarm->isInTimeRange($start, $end)) { |
|
317 | + return true; |
|
318 | + } |
|
319 | + |
|
320 | + if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
321 | + // This is an alarm with a non-relative trigger |
|
322 | + // time, likely created by a buggy client. The |
|
323 | + // implication is that every alarm in this |
|
324 | + // recurring event trigger at the exact same |
|
325 | + // time. It doesn't make sense to traverse |
|
326 | + // further. |
|
327 | + } else { |
|
328 | + // We store the first alarm as a means to |
|
329 | + // figure out when we can stop traversing. |
|
330 | + if (!$firstAlarm || $effectiveTrigger < $firstAlarm) { |
|
331 | + $firstAlarm = $effectiveTrigger; |
|
332 | + } |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + if (is_null($firstAlarm)) { |
|
337 | + // No alarm was found. |
|
338 | + // |
|
339 | + // Or technically: No alarm that will change for |
|
340 | + // every instance of the recurrence was found, |
|
341 | + // which means we can assume there was no match. |
|
342 | + return false; |
|
343 | + } |
|
344 | + if ($firstAlarm > $end) { |
|
345 | + return false; |
|
346 | + } |
|
347 | + $it->next(); |
|
348 | + } |
|
349 | + return false; |
|
350 | + } else { |
|
351 | + return $component->isInTimeRange($start, $end); |
|
352 | + } |
|
353 | + |
|
354 | + case 'VFREEBUSY' : |
|
355 | + throw new \Sabre\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components'); |
|
356 | + |
|
357 | + case 'COMPLETED' : |
|
358 | + case 'CREATED' : |
|
359 | + case 'DTEND' : |
|
360 | + case 'DTSTAMP' : |
|
361 | + case 'DTSTART' : |
|
362 | + case 'DUE' : |
|
363 | + case 'LAST-MODIFIED' : |
|
364 | + return ($start <= $component->getDateTime() && $end >= $component->getDateTime()); |
|
365 | + |
|
366 | + |
|
367 | + |
|
368 | + default : |
|
369 | + throw new \Sabre\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component'); |
|
370 | + |
|
371 | + } |
|
372 | + |
|
373 | + } |
|
374 | 374 | |
375 | 375 | } |
@@ -25,7 +25,7 @@ |
||
25 | 25 | * |
26 | 26 | * The list of filters must be formatted as parsed by \Sabre\CalDAV\CalendarQueryParser |
27 | 27 | * |
28 | - * @param VObject\Component $vObject |
|
28 | + * @param VObject\Parser\Sabre\VObject\Document|null $vObject |
|
29 | 29 | * @param array $filters |
30 | 30 | * @return bool |
31 | 31 | */ |
@@ -2,13 +2,13 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV; |
4 | 4 | |
5 | +use DateTime; |
|
5 | 6 | use DateTimeZone; |
6 | 7 | use Sabre\DAV; |
7 | -use Sabre\VObject; |
|
8 | +use Sabre\DAV\Exception\BadRequest; |
|
8 | 9 | use Sabre\HTTP\RequestInterface; |
9 | 10 | use Sabre\HTTP\ResponseInterface; |
10 | -use Sabre\DAV\Exception\BadRequest; |
|
11 | -use DateTime; |
|
11 | +use Sabre\VObject; |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * ICS Exporter |
@@ -306,7 +306,7 @@ |
||
306 | 306 | // VTIMEZONE is special, because we need to filter out the duplicates |
307 | 307 | case 'VTIMEZONE' : |
308 | 308 | // Naively just checking tzid. |
309 | - if (in_array((string)$child->TZID, $collectedTimezones)) continue; |
|
309 | + if (in_array((string) $child->TZID, $collectedTimezones)) continue; |
|
310 | 310 | |
311 | 311 | $timezones[] = clone $child; |
312 | 312 | $collectedTimezones[] = $child->TZID; |
@@ -46,321 +46,321 @@ |
||
46 | 46 | */ |
47 | 47 | class ICSExportPlugin extends DAV\ServerPlugin { |
48 | 48 | |
49 | - /** |
|
50 | - * Reference to Server class |
|
51 | - * |
|
52 | - * @var \Sabre\DAV\Server |
|
53 | - */ |
|
54 | - protected $server; |
|
55 | - |
|
56 | - /** |
|
57 | - * Initializes the plugin and registers event handlers |
|
58 | - * |
|
59 | - * @param \Sabre\DAV\Server $server |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public function initialize(DAV\Server $server) { |
|
63 | - |
|
64 | - $this->server = $server; |
|
65 | - $server->on('method:GET', [$this, 'httpGet'], 90); |
|
66 | - $server->on('browserButtonActions', function($path, $node, &$actions) { |
|
67 | - if ($node instanceof ICalendar) { |
|
68 | - $actions .= '<a href="' . htmlspecialchars($path, ENT_QUOTES, 'UTF-8') . '?export"><span class="oi" data-glyph="calendar"></span></a>'; |
|
69 | - } |
|
70 | - }); |
|
71 | - |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Intercepts GET requests on calendar urls ending with ?export. |
|
76 | - * |
|
77 | - * @param RequestInterface $request |
|
78 | - * @param ResponseInterface $response |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
82 | - |
|
83 | - $queryParams = $request->getQueryParameters(); |
|
84 | - if (!array_key_exists('export', $queryParams)) return; |
|
85 | - |
|
86 | - $path = $request->getPath(); |
|
87 | - |
|
88 | - $node = $this->server->getProperties($path, [ |
|
89 | - '{DAV:}resourcetype', |
|
90 | - '{DAV:}displayname', |
|
91 | - '{http://sabredav.org/ns}sync-token', |
|
92 | - '{DAV:}sync-token', |
|
93 | - '{http://apple.com/ns/ical/}calendar-color', |
|
94 | - ]); |
|
95 | - |
|
96 | - if (!isset($node['{DAV:}resourcetype']) || !$node['{DAV:}resourcetype']->is('{' . Plugin::NS_CALDAV . '}calendar')) { |
|
97 | - return; |
|
98 | - } |
|
99 | - // Marking the transactionType, for logging purposes. |
|
100 | - $this->server->transactionType = 'get-calendar-export'; |
|
101 | - |
|
102 | - $properties = $node; |
|
103 | - |
|
104 | - $start = null; |
|
105 | - $end = null; |
|
106 | - $expand = false; |
|
107 | - $componentType = false; |
|
108 | - if (isset($queryParams['start'])) { |
|
109 | - if (!ctype_digit($queryParams['start'])) { |
|
110 | - throw new BadRequest('The start= parameter must contain a unix timestamp'); |
|
111 | - } |
|
112 | - $start = DateTime::createFromFormat('U', $queryParams['start']); |
|
113 | - } |
|
114 | - if (isset($queryParams['end'])) { |
|
115 | - if (!ctype_digit($queryParams['end'])) { |
|
116 | - throw new BadRequest('The end= parameter must contain a unix timestamp'); |
|
117 | - } |
|
118 | - $end = DateTime::createFromFormat('U', $queryParams['end']); |
|
119 | - } |
|
120 | - if (isset($queryParams['expand']) && !!$queryParams['expand']) { |
|
121 | - if (!$start || !$end) { |
|
122 | - throw new BadRequest('If you\'d like to expand recurrences, you must specify both a start= and end= parameter.'); |
|
123 | - } |
|
124 | - $expand = true; |
|
125 | - $componentType = 'VEVENT'; |
|
126 | - } |
|
127 | - if (isset($queryParams['componentType'])) { |
|
128 | - if (!in_array($queryParams['componentType'], ['VEVENT', 'VTODO', 'VJOURNAL'])) { |
|
129 | - throw new BadRequest('You are not allowed to search for components of type: ' . $queryParams['componentType'] . ' here'); |
|
130 | - } |
|
131 | - $componentType = $queryParams['componentType']; |
|
132 | - } |
|
133 | - |
|
134 | - $format = \Sabre\HTTP\Util::Negotiate( |
|
135 | - $request->getHeader('Accept'), |
|
136 | - [ |
|
137 | - 'text/calendar', |
|
138 | - 'application/calendar+json', |
|
139 | - ] |
|
140 | - ); |
|
141 | - |
|
142 | - if (isset($queryParams['accept'])) { |
|
143 | - if ($queryParams['accept'] === 'application/calendar+json' || $queryParams['accept'] === 'jcal') { |
|
144 | - $format = 'application/calendar+json'; |
|
145 | - } |
|
146 | - } |
|
147 | - if (!$format) { |
|
148 | - $format = 'text/calendar'; |
|
149 | - } |
|
150 | - |
|
151 | - $this->generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response); |
|
152 | - |
|
153 | - // Returning false to break the event chain |
|
154 | - return false; |
|
155 | - |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * This method is responsible for generating the actual, full response. |
|
160 | - * |
|
161 | - * @param string $path |
|
162 | - * @param DateTime|null $start |
|
163 | - * @param DateTime|null $end |
|
164 | - * @param bool $expand |
|
165 | - * @param string $componentType |
|
166 | - * @param string $format |
|
167 | - * @param array $properties |
|
168 | - * @param ResponseInterface $response |
|
169 | - */ |
|
170 | - protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) { |
|
171 | - |
|
172 | - $calDataProp = '{' . Plugin::NS_CALDAV . '}calendar-data'; |
|
173 | - |
|
174 | - $blobs = []; |
|
175 | - if ($start || $end || $componentType) { |
|
176 | - |
|
177 | - // If there was a start or end filter, we need to enlist |
|
178 | - // calendarQuery for speed. |
|
179 | - $calendarNode = $this->server->tree->getNodeForPath($path); |
|
180 | - $queryResult = $calendarNode->calendarQuery([ |
|
181 | - 'name' => 'VCALENDAR', |
|
182 | - 'comp-filters' => [ |
|
183 | - [ |
|
184 | - 'name' => $componentType, |
|
185 | - 'comp-filters' => [], |
|
186 | - 'prop-filters' => [], |
|
187 | - 'is-not-defined' => false, |
|
188 | - 'time-range' => [ |
|
189 | - 'start' => $start, |
|
190 | - 'end' => $end, |
|
191 | - ], |
|
192 | - ], |
|
193 | - ], |
|
194 | - 'prop-filters' => [], |
|
195 | - 'is-not-defined' => false, |
|
196 | - 'time-range' => null, |
|
197 | - ]); |
|
198 | - |
|
199 | - // queryResult is just a list of base urls. We need to prefix the |
|
200 | - // calendar path. |
|
201 | - $queryResult = array_map( |
|
202 | - function($item) use ($path) { |
|
203 | - return $path . '/' . $item; |
|
204 | - }, |
|
205 | - $queryResult |
|
206 | - ); |
|
207 | - $nodes = $this->server->getPropertiesForMultiplePaths($queryResult, [$calDataProp]); |
|
208 | - unset($queryResult); |
|
209 | - |
|
210 | - } else { |
|
211 | - $nodes = $this->server->getPropertiesForPath($path, [$calDataProp], 1); |
|
212 | - } |
|
213 | - |
|
214 | - // Flattening the arrays |
|
215 | - foreach ($nodes as $node) { |
|
216 | - if (isset($node[200][$calDataProp])) { |
|
217 | - $blobs[$node['href']] = $node[200][$calDataProp]; |
|
218 | - } |
|
219 | - } |
|
220 | - unset($nodes); |
|
221 | - |
|
222 | - $mergedCalendar = $this->mergeObjects( |
|
223 | - $properties, |
|
224 | - $blobs |
|
225 | - ); |
|
226 | - |
|
227 | - if ($expand) { |
|
228 | - $calendarTimeZone = null; |
|
229 | - // We're expanding, and for that we need to figure out the |
|
230 | - // calendar's timezone. |
|
231 | - $tzProp = '{' . Plugin::NS_CALDAV . '}calendar-timezone'; |
|
232 | - $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
233 | - if (isset($tzResult[$tzProp])) { |
|
234 | - // This property contains a VCALENDAR with a single |
|
235 | - // VTIMEZONE. |
|
236 | - $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
237 | - $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
238 | - // Destroy circular references to PHP will GC the object. |
|
239 | - $vtimezoneObj->destroy(); |
|
240 | - unset($vtimezoneObj); |
|
241 | - } else { |
|
242 | - // Defaulting to UTC. |
|
243 | - $calendarTimeZone = new DateTimeZone('UTC'); |
|
244 | - } |
|
245 | - |
|
246 | - $mergedCalendar = $mergedCalendar->expand($start, $end, $calendarTimeZone); |
|
247 | - } |
|
248 | - |
|
249 | - $response->setHeader('Content-Type', $format); |
|
250 | - |
|
251 | - switch ($format) { |
|
252 | - case 'text/calendar' : |
|
253 | - $mergedCalendar = $mergedCalendar->serialize(); |
|
254 | - break; |
|
255 | - case 'application/calendar+json' : |
|
256 | - $mergedCalendar = json_encode($mergedCalendar->jsonSerialize()); |
|
257 | - break; |
|
258 | - } |
|
259 | - |
|
260 | - $response->setStatus(200); |
|
261 | - $response->setBody($mergedCalendar); |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Merges all calendar objects, and builds one big iCalendar blob. |
|
267 | - * |
|
268 | - * @param array $properties Some CalDAV properties |
|
269 | - * @param array $inputObjects |
|
270 | - * @return VObject\Component\VCalendar |
|
271 | - */ |
|
272 | - public function mergeObjects(array $properties, array $inputObjects) { |
|
273 | - |
|
274 | - $calendar = new VObject\Component\VCalendar(); |
|
275 | - $calendar->version = '2.0'; |
|
276 | - if (DAV\Server::$exposeVersion) { |
|
277 | - $calendar->prodid = '-//SabreDAV//SabreDAV ' . DAV\Version::VERSION . '//EN'; |
|
278 | - } else { |
|
279 | - $calendar->prodid = '-//SabreDAV//SabreDAV//EN'; |
|
280 | - } |
|
281 | - if (isset($properties['{DAV:}displayname'])) { |
|
282 | - $calendar->{'X-WR-CALNAME'} = $properties['{DAV:}displayname']; |
|
283 | - } |
|
284 | - if (isset($properties['{http://apple.com/ns/ical/}calendar-color'])) { |
|
285 | - $calendar->{'X-APPLE-CALENDAR-COLOR'} = $properties['{http://apple.com/ns/ical/}calendar-color']; |
|
286 | - } |
|
287 | - |
|
288 | - $collectedTimezones = []; |
|
289 | - |
|
290 | - $timezones = []; |
|
291 | - $objects = []; |
|
292 | - |
|
293 | - foreach ($inputObjects as $href => $inputObject) { |
|
294 | - |
|
295 | - $nodeComp = VObject\Reader::read($inputObject); |
|
296 | - |
|
297 | - foreach ($nodeComp->children() as $child) { |
|
298 | - |
|
299 | - switch ($child->name) { |
|
300 | - case 'VEVENT' : |
|
301 | - case 'VTODO' : |
|
302 | - case 'VJOURNAL' : |
|
303 | - $objects[] = clone $child; |
|
304 | - break; |
|
305 | - |
|
306 | - // VTIMEZONE is special, because we need to filter out the duplicates |
|
307 | - case 'VTIMEZONE' : |
|
308 | - // Naively just checking tzid. |
|
309 | - if (in_array((string)$child->TZID, $collectedTimezones)) continue; |
|
310 | - |
|
311 | - $timezones[] = clone $child; |
|
312 | - $collectedTimezones[] = $child->TZID; |
|
313 | - break; |
|
314 | - |
|
315 | - } |
|
316 | - |
|
317 | - } |
|
318 | - // Destroy circular references to PHP will GC the object. |
|
319 | - $nodeComp->destroy(); |
|
320 | - unset($nodeComp); |
|
321 | - |
|
322 | - } |
|
323 | - |
|
324 | - foreach ($timezones as $tz) $calendar->add($tz); |
|
325 | - foreach ($objects as $obj) $calendar->add($obj); |
|
326 | - |
|
327 | - return $calendar; |
|
328 | - |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Returns a plugin name. |
|
333 | - * |
|
334 | - * Using this name other plugins will be able to access other plugins |
|
335 | - * using \Sabre\DAV\Server::getPlugin |
|
336 | - * |
|
337 | - * @return string |
|
338 | - */ |
|
339 | - public function getPluginName() { |
|
340 | - |
|
341 | - return 'ics-export'; |
|
342 | - |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Returns a bunch of meta-data about the plugin. |
|
347 | - * |
|
348 | - * Providing this information is optional, and is mainly displayed by the |
|
349 | - * Browser plugin. |
|
350 | - * |
|
351 | - * The description key in the returned array may contain html and will not |
|
352 | - * be sanitized. |
|
353 | - * |
|
354 | - * @return array |
|
355 | - */ |
|
356 | - public function getPluginInfo() { |
|
357 | - |
|
358 | - return [ |
|
359 | - 'name' => $this->getPluginName(), |
|
360 | - 'description' => 'Adds the ability to export CalDAV calendars as a single iCalendar file.', |
|
361 | - 'link' => 'http://sabre.io/dav/ics-export-plugin/', |
|
362 | - ]; |
|
363 | - |
|
364 | - } |
|
49 | + /** |
|
50 | + * Reference to Server class |
|
51 | + * |
|
52 | + * @var \Sabre\DAV\Server |
|
53 | + */ |
|
54 | + protected $server; |
|
55 | + |
|
56 | + /** |
|
57 | + * Initializes the plugin and registers event handlers |
|
58 | + * |
|
59 | + * @param \Sabre\DAV\Server $server |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public function initialize(DAV\Server $server) { |
|
63 | + |
|
64 | + $this->server = $server; |
|
65 | + $server->on('method:GET', [$this, 'httpGet'], 90); |
|
66 | + $server->on('browserButtonActions', function($path, $node, &$actions) { |
|
67 | + if ($node instanceof ICalendar) { |
|
68 | + $actions .= '<a href="' . htmlspecialchars($path, ENT_QUOTES, 'UTF-8') . '?export"><span class="oi" data-glyph="calendar"></span></a>'; |
|
69 | + } |
|
70 | + }); |
|
71 | + |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Intercepts GET requests on calendar urls ending with ?export. |
|
76 | + * |
|
77 | + * @param RequestInterface $request |
|
78 | + * @param ResponseInterface $response |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
82 | + |
|
83 | + $queryParams = $request->getQueryParameters(); |
|
84 | + if (!array_key_exists('export', $queryParams)) return; |
|
85 | + |
|
86 | + $path = $request->getPath(); |
|
87 | + |
|
88 | + $node = $this->server->getProperties($path, [ |
|
89 | + '{DAV:}resourcetype', |
|
90 | + '{DAV:}displayname', |
|
91 | + '{http://sabredav.org/ns}sync-token', |
|
92 | + '{DAV:}sync-token', |
|
93 | + '{http://apple.com/ns/ical/}calendar-color', |
|
94 | + ]); |
|
95 | + |
|
96 | + if (!isset($node['{DAV:}resourcetype']) || !$node['{DAV:}resourcetype']->is('{' . Plugin::NS_CALDAV . '}calendar')) { |
|
97 | + return; |
|
98 | + } |
|
99 | + // Marking the transactionType, for logging purposes. |
|
100 | + $this->server->transactionType = 'get-calendar-export'; |
|
101 | + |
|
102 | + $properties = $node; |
|
103 | + |
|
104 | + $start = null; |
|
105 | + $end = null; |
|
106 | + $expand = false; |
|
107 | + $componentType = false; |
|
108 | + if (isset($queryParams['start'])) { |
|
109 | + if (!ctype_digit($queryParams['start'])) { |
|
110 | + throw new BadRequest('The start= parameter must contain a unix timestamp'); |
|
111 | + } |
|
112 | + $start = DateTime::createFromFormat('U', $queryParams['start']); |
|
113 | + } |
|
114 | + if (isset($queryParams['end'])) { |
|
115 | + if (!ctype_digit($queryParams['end'])) { |
|
116 | + throw new BadRequest('The end= parameter must contain a unix timestamp'); |
|
117 | + } |
|
118 | + $end = DateTime::createFromFormat('U', $queryParams['end']); |
|
119 | + } |
|
120 | + if (isset($queryParams['expand']) && !!$queryParams['expand']) { |
|
121 | + if (!$start || !$end) { |
|
122 | + throw new BadRequest('If you\'d like to expand recurrences, you must specify both a start= and end= parameter.'); |
|
123 | + } |
|
124 | + $expand = true; |
|
125 | + $componentType = 'VEVENT'; |
|
126 | + } |
|
127 | + if (isset($queryParams['componentType'])) { |
|
128 | + if (!in_array($queryParams['componentType'], ['VEVENT', 'VTODO', 'VJOURNAL'])) { |
|
129 | + throw new BadRequest('You are not allowed to search for components of type: ' . $queryParams['componentType'] . ' here'); |
|
130 | + } |
|
131 | + $componentType = $queryParams['componentType']; |
|
132 | + } |
|
133 | + |
|
134 | + $format = \Sabre\HTTP\Util::Negotiate( |
|
135 | + $request->getHeader('Accept'), |
|
136 | + [ |
|
137 | + 'text/calendar', |
|
138 | + 'application/calendar+json', |
|
139 | + ] |
|
140 | + ); |
|
141 | + |
|
142 | + if (isset($queryParams['accept'])) { |
|
143 | + if ($queryParams['accept'] === 'application/calendar+json' || $queryParams['accept'] === 'jcal') { |
|
144 | + $format = 'application/calendar+json'; |
|
145 | + } |
|
146 | + } |
|
147 | + if (!$format) { |
|
148 | + $format = 'text/calendar'; |
|
149 | + } |
|
150 | + |
|
151 | + $this->generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response); |
|
152 | + |
|
153 | + // Returning false to break the event chain |
|
154 | + return false; |
|
155 | + |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * This method is responsible for generating the actual, full response. |
|
160 | + * |
|
161 | + * @param string $path |
|
162 | + * @param DateTime|null $start |
|
163 | + * @param DateTime|null $end |
|
164 | + * @param bool $expand |
|
165 | + * @param string $componentType |
|
166 | + * @param string $format |
|
167 | + * @param array $properties |
|
168 | + * @param ResponseInterface $response |
|
169 | + */ |
|
170 | + protected function generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, ResponseInterface $response) { |
|
171 | + |
|
172 | + $calDataProp = '{' . Plugin::NS_CALDAV . '}calendar-data'; |
|
173 | + |
|
174 | + $blobs = []; |
|
175 | + if ($start || $end || $componentType) { |
|
176 | + |
|
177 | + // If there was a start or end filter, we need to enlist |
|
178 | + // calendarQuery for speed. |
|
179 | + $calendarNode = $this->server->tree->getNodeForPath($path); |
|
180 | + $queryResult = $calendarNode->calendarQuery([ |
|
181 | + 'name' => 'VCALENDAR', |
|
182 | + 'comp-filters' => [ |
|
183 | + [ |
|
184 | + 'name' => $componentType, |
|
185 | + 'comp-filters' => [], |
|
186 | + 'prop-filters' => [], |
|
187 | + 'is-not-defined' => false, |
|
188 | + 'time-range' => [ |
|
189 | + 'start' => $start, |
|
190 | + 'end' => $end, |
|
191 | + ], |
|
192 | + ], |
|
193 | + ], |
|
194 | + 'prop-filters' => [], |
|
195 | + 'is-not-defined' => false, |
|
196 | + 'time-range' => null, |
|
197 | + ]); |
|
198 | + |
|
199 | + // queryResult is just a list of base urls. We need to prefix the |
|
200 | + // calendar path. |
|
201 | + $queryResult = array_map( |
|
202 | + function($item) use ($path) { |
|
203 | + return $path . '/' . $item; |
|
204 | + }, |
|
205 | + $queryResult |
|
206 | + ); |
|
207 | + $nodes = $this->server->getPropertiesForMultiplePaths($queryResult, [$calDataProp]); |
|
208 | + unset($queryResult); |
|
209 | + |
|
210 | + } else { |
|
211 | + $nodes = $this->server->getPropertiesForPath($path, [$calDataProp], 1); |
|
212 | + } |
|
213 | + |
|
214 | + // Flattening the arrays |
|
215 | + foreach ($nodes as $node) { |
|
216 | + if (isset($node[200][$calDataProp])) { |
|
217 | + $blobs[$node['href']] = $node[200][$calDataProp]; |
|
218 | + } |
|
219 | + } |
|
220 | + unset($nodes); |
|
221 | + |
|
222 | + $mergedCalendar = $this->mergeObjects( |
|
223 | + $properties, |
|
224 | + $blobs |
|
225 | + ); |
|
226 | + |
|
227 | + if ($expand) { |
|
228 | + $calendarTimeZone = null; |
|
229 | + // We're expanding, and for that we need to figure out the |
|
230 | + // calendar's timezone. |
|
231 | + $tzProp = '{' . Plugin::NS_CALDAV . '}calendar-timezone'; |
|
232 | + $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
233 | + if (isset($tzResult[$tzProp])) { |
|
234 | + // This property contains a VCALENDAR with a single |
|
235 | + // VTIMEZONE. |
|
236 | + $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
237 | + $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
238 | + // Destroy circular references to PHP will GC the object. |
|
239 | + $vtimezoneObj->destroy(); |
|
240 | + unset($vtimezoneObj); |
|
241 | + } else { |
|
242 | + // Defaulting to UTC. |
|
243 | + $calendarTimeZone = new DateTimeZone('UTC'); |
|
244 | + } |
|
245 | + |
|
246 | + $mergedCalendar = $mergedCalendar->expand($start, $end, $calendarTimeZone); |
|
247 | + } |
|
248 | + |
|
249 | + $response->setHeader('Content-Type', $format); |
|
250 | + |
|
251 | + switch ($format) { |
|
252 | + case 'text/calendar' : |
|
253 | + $mergedCalendar = $mergedCalendar->serialize(); |
|
254 | + break; |
|
255 | + case 'application/calendar+json' : |
|
256 | + $mergedCalendar = json_encode($mergedCalendar->jsonSerialize()); |
|
257 | + break; |
|
258 | + } |
|
259 | + |
|
260 | + $response->setStatus(200); |
|
261 | + $response->setBody($mergedCalendar); |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Merges all calendar objects, and builds one big iCalendar blob. |
|
267 | + * |
|
268 | + * @param array $properties Some CalDAV properties |
|
269 | + * @param array $inputObjects |
|
270 | + * @return VObject\Component\VCalendar |
|
271 | + */ |
|
272 | + public function mergeObjects(array $properties, array $inputObjects) { |
|
273 | + |
|
274 | + $calendar = new VObject\Component\VCalendar(); |
|
275 | + $calendar->version = '2.0'; |
|
276 | + if (DAV\Server::$exposeVersion) { |
|
277 | + $calendar->prodid = '-//SabreDAV//SabreDAV ' . DAV\Version::VERSION . '//EN'; |
|
278 | + } else { |
|
279 | + $calendar->prodid = '-//SabreDAV//SabreDAV//EN'; |
|
280 | + } |
|
281 | + if (isset($properties['{DAV:}displayname'])) { |
|
282 | + $calendar->{'X-WR-CALNAME'} = $properties['{DAV:}displayname']; |
|
283 | + } |
|
284 | + if (isset($properties['{http://apple.com/ns/ical/}calendar-color'])) { |
|
285 | + $calendar->{'X-APPLE-CALENDAR-COLOR'} = $properties['{http://apple.com/ns/ical/}calendar-color']; |
|
286 | + } |
|
287 | + |
|
288 | + $collectedTimezones = []; |
|
289 | + |
|
290 | + $timezones = []; |
|
291 | + $objects = []; |
|
292 | + |
|
293 | + foreach ($inputObjects as $href => $inputObject) { |
|
294 | + |
|
295 | + $nodeComp = VObject\Reader::read($inputObject); |
|
296 | + |
|
297 | + foreach ($nodeComp->children() as $child) { |
|
298 | + |
|
299 | + switch ($child->name) { |
|
300 | + case 'VEVENT' : |
|
301 | + case 'VTODO' : |
|
302 | + case 'VJOURNAL' : |
|
303 | + $objects[] = clone $child; |
|
304 | + break; |
|
305 | + |
|
306 | + // VTIMEZONE is special, because we need to filter out the duplicates |
|
307 | + case 'VTIMEZONE' : |
|
308 | + // Naively just checking tzid. |
|
309 | + if (in_array((string)$child->TZID, $collectedTimezones)) continue; |
|
310 | + |
|
311 | + $timezones[] = clone $child; |
|
312 | + $collectedTimezones[] = $child->TZID; |
|
313 | + break; |
|
314 | + |
|
315 | + } |
|
316 | + |
|
317 | + } |
|
318 | + // Destroy circular references to PHP will GC the object. |
|
319 | + $nodeComp->destroy(); |
|
320 | + unset($nodeComp); |
|
321 | + |
|
322 | + } |
|
323 | + |
|
324 | + foreach ($timezones as $tz) $calendar->add($tz); |
|
325 | + foreach ($objects as $obj) $calendar->add($obj); |
|
326 | + |
|
327 | + return $calendar; |
|
328 | + |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Returns a plugin name. |
|
333 | + * |
|
334 | + * Using this name other plugins will be able to access other plugins |
|
335 | + * using \Sabre\DAV\Server::getPlugin |
|
336 | + * |
|
337 | + * @return string |
|
338 | + */ |
|
339 | + public function getPluginName() { |
|
340 | + |
|
341 | + return 'ics-export'; |
|
342 | + |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Returns a bunch of meta-data about the plugin. |
|
347 | + * |
|
348 | + * Providing this information is optional, and is mainly displayed by the |
|
349 | + * Browser plugin. |
|
350 | + * |
|
351 | + * The description key in the returned array may contain html and will not |
|
352 | + * be sanitized. |
|
353 | + * |
|
354 | + * @return array |
|
355 | + */ |
|
356 | + public function getPluginInfo() { |
|
357 | + |
|
358 | + return [ |
|
359 | + 'name' => $this->getPluginName(), |
|
360 | + 'description' => 'Adds the ability to export CalDAV calendars as a single iCalendar file.', |
|
361 | + 'link' => 'http://sabre.io/dav/ics-export-plugin/', |
|
362 | + ]; |
|
363 | + |
|
364 | + } |
|
365 | 365 | |
366 | 366 | } |
@@ -76,7 +76,7 @@ |
||
76 | 76 | * |
77 | 77 | * @param RequestInterface $request |
78 | 78 | * @param ResponseInterface $response |
79 | - * @return bool |
|
79 | + * @return null|false |
|
80 | 80 | */ |
81 | 81 | public function httpGet(RequestInterface $request, ResponseInterface $response) { |
82 | 82 |
@@ -81,7 +81,9 @@ discard block |
||
81 | 81 | public function httpGet(RequestInterface $request, ResponseInterface $response) { |
82 | 82 | |
83 | 83 | $queryParams = $request->getQueryParameters(); |
84 | - if (!array_key_exists('export', $queryParams)) return; |
|
84 | + if (!array_key_exists('export', $queryParams)) { |
|
85 | + return; |
|
86 | + } |
|
85 | 87 | |
86 | 88 | $path = $request->getPath(); |
87 | 89 | |
@@ -306,7 +308,9 @@ discard block |
||
306 | 308 | // VTIMEZONE is special, because we need to filter out the duplicates |
307 | 309 | case 'VTIMEZONE' : |
308 | 310 | // Naively just checking tzid. |
309 | - if (in_array((string)$child->TZID, $collectedTimezones)) continue; |
|
311 | + if (in_array((string)$child->TZID, $collectedTimezones)) { |
|
312 | + continue; |
|
313 | + } |
|
310 | 314 | |
311 | 315 | $timezones[] = clone $child; |
312 | 316 | $collectedTimezones[] = $child->TZID; |
@@ -321,8 +325,12 @@ discard block |
||
321 | 325 | |
322 | 326 | } |
323 | 327 | |
324 | - foreach ($timezones as $tz) $calendar->add($tz); |
|
325 | - foreach ($objects as $obj) $calendar->add($obj); |
|
328 | + foreach ($timezones as $tz) { |
|
329 | + $calendar->add($tz); |
|
330 | + } |
|
331 | + foreach ($objects as $obj) { |
|
332 | + $calendar->add($obj); |
|
333 | + } |
|
326 | 334 | |
327 | 335 | return $calendar; |
328 | 336 |
@@ -2,8 +2,8 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV\Notifications; |
4 | 4 | |
5 | -use Sabre\DAV; |
|
6 | 5 | use Sabre\CalDAV; |
6 | +use Sabre\DAV; |
|
7 | 7 | use Sabre\DAVACL; |
8 | 8 | |
9 | 9 | /** |
@@ -22,152 +22,152 @@ |
||
22 | 22 | */ |
23 | 23 | class Collection extends DAV\Collection implements ICollection, DAVACL\IACL { |
24 | 24 | |
25 | - /** |
|
26 | - * The notification backend |
|
27 | - * |
|
28 | - * @var Sabre\CalDAV\Backend\NotificationSupport |
|
29 | - */ |
|
30 | - protected $caldavBackend; |
|
31 | - |
|
32 | - /** |
|
33 | - * Principal uri |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $principalUri; |
|
38 | - |
|
39 | - /** |
|
40 | - * Constructor |
|
41 | - * |
|
42 | - * @param CalDAV\Backend\NotificationSupport $caldavBackend |
|
43 | - * @param string $principalUri |
|
44 | - */ |
|
45 | - public function __construct(CalDAV\Backend\NotificationSupport $caldavBackend, $principalUri) { |
|
46 | - |
|
47 | - $this->caldavBackend = $caldavBackend; |
|
48 | - $this->principalUri = $principalUri; |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns all notifications for a principal |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getChildren() { |
|
58 | - |
|
59 | - $children = []; |
|
60 | - $notifications = $this->caldavBackend->getNotificationsForPrincipal($this->principalUri); |
|
61 | - |
|
62 | - foreach ($notifications as $notification) { |
|
63 | - |
|
64 | - $children[] = new Node( |
|
65 | - $this->caldavBackend, |
|
66 | - $this->principalUri, |
|
67 | - $notification |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - return $children; |
|
72 | - |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Returns the name of this object |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function getName() { |
|
81 | - |
|
82 | - return 'notifications'; |
|
83 | - |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the owner principal |
|
88 | - * |
|
89 | - * This must be a url to a principal, or null if there's no owner |
|
90 | - * |
|
91 | - * @return string|null |
|
92 | - */ |
|
93 | - public function getOwner() { |
|
94 | - |
|
95 | - return $this->principalUri; |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Returns a group principal |
|
101 | - * |
|
102 | - * This must be a url to a principal, or null if there's no owner |
|
103 | - * |
|
104 | - * @return string|null |
|
105 | - */ |
|
106 | - public function getGroup() { |
|
107 | - |
|
108 | - return null; |
|
109 | - |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Returns a list of ACE's for this node. |
|
114 | - * |
|
115 | - * Each ACE has the following properties: |
|
116 | - * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
117 | - * currently the only supported privileges |
|
118 | - * * 'principal', a url to the principal who owns the node |
|
119 | - * * 'protected' (optional), indicating that this ACE is not allowed to |
|
120 | - * be updated. |
|
121 | - * |
|
122 | - * @return array |
|
123 | - */ |
|
124 | - public function getACL() { |
|
125 | - |
|
126 | - return [ |
|
127 | - [ |
|
128 | - 'principal' => $this->getOwner(), |
|
129 | - 'privilege' => '{DAV:}read', |
|
130 | - 'protected' => true, |
|
131 | - ], |
|
132 | - [ |
|
133 | - 'principal' => $this->getOwner(), |
|
134 | - 'privilege' => '{DAV:}write', |
|
135 | - 'protected' => true, |
|
136 | - ] |
|
137 | - ]; |
|
138 | - |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Updates the ACL |
|
143 | - * |
|
144 | - * This method will receive a list of new ACE's as an array argument. |
|
145 | - * |
|
146 | - * @param array $acl |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function setACL(array $acl) { |
|
150 | - |
|
151 | - throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here'); |
|
152 | - |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Returns the list of supported privileges for this node. |
|
157 | - * |
|
158 | - * The returned data structure is a list of nested privileges. |
|
159 | - * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple |
|
160 | - * standard structure. |
|
161 | - * |
|
162 | - * If null is returned from this method, the default privilege set is used, |
|
163 | - * which is fine for most common usecases. |
|
164 | - * |
|
165 | - * @return array|null |
|
166 | - */ |
|
167 | - public function getSupportedPrivilegeSet() { |
|
168 | - |
|
169 | - return null; |
|
170 | - |
|
171 | - } |
|
25 | + /** |
|
26 | + * The notification backend |
|
27 | + * |
|
28 | + * @var Sabre\CalDAV\Backend\NotificationSupport |
|
29 | + */ |
|
30 | + protected $caldavBackend; |
|
31 | + |
|
32 | + /** |
|
33 | + * Principal uri |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $principalUri; |
|
38 | + |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * |
|
42 | + * @param CalDAV\Backend\NotificationSupport $caldavBackend |
|
43 | + * @param string $principalUri |
|
44 | + */ |
|
45 | + public function __construct(CalDAV\Backend\NotificationSupport $caldavBackend, $principalUri) { |
|
46 | + |
|
47 | + $this->caldavBackend = $caldavBackend; |
|
48 | + $this->principalUri = $principalUri; |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns all notifications for a principal |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getChildren() { |
|
58 | + |
|
59 | + $children = []; |
|
60 | + $notifications = $this->caldavBackend->getNotificationsForPrincipal($this->principalUri); |
|
61 | + |
|
62 | + foreach ($notifications as $notification) { |
|
63 | + |
|
64 | + $children[] = new Node( |
|
65 | + $this->caldavBackend, |
|
66 | + $this->principalUri, |
|
67 | + $notification |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + return $children; |
|
72 | + |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Returns the name of this object |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function getName() { |
|
81 | + |
|
82 | + return 'notifications'; |
|
83 | + |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the owner principal |
|
88 | + * |
|
89 | + * This must be a url to a principal, or null if there's no owner |
|
90 | + * |
|
91 | + * @return string|null |
|
92 | + */ |
|
93 | + public function getOwner() { |
|
94 | + |
|
95 | + return $this->principalUri; |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Returns a group principal |
|
101 | + * |
|
102 | + * This must be a url to a principal, or null if there's no owner |
|
103 | + * |
|
104 | + * @return string|null |
|
105 | + */ |
|
106 | + public function getGroup() { |
|
107 | + |
|
108 | + return null; |
|
109 | + |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Returns a list of ACE's for this node. |
|
114 | + * |
|
115 | + * Each ACE has the following properties: |
|
116 | + * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
117 | + * currently the only supported privileges |
|
118 | + * * 'principal', a url to the principal who owns the node |
|
119 | + * * 'protected' (optional), indicating that this ACE is not allowed to |
|
120 | + * be updated. |
|
121 | + * |
|
122 | + * @return array |
|
123 | + */ |
|
124 | + public function getACL() { |
|
125 | + |
|
126 | + return [ |
|
127 | + [ |
|
128 | + 'principal' => $this->getOwner(), |
|
129 | + 'privilege' => '{DAV:}read', |
|
130 | + 'protected' => true, |
|
131 | + ], |
|
132 | + [ |
|
133 | + 'principal' => $this->getOwner(), |
|
134 | + 'privilege' => '{DAV:}write', |
|
135 | + 'protected' => true, |
|
136 | + ] |
|
137 | + ]; |
|
138 | + |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Updates the ACL |
|
143 | + * |
|
144 | + * This method will receive a list of new ACE's as an array argument. |
|
145 | + * |
|
146 | + * @param array $acl |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function setACL(array $acl) { |
|
150 | + |
|
151 | + throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here'); |
|
152 | + |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Returns the list of supported privileges for this node. |
|
157 | + * |
|
158 | + * The returned data structure is a list of nested privileges. |
|
159 | + * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple |
|
160 | + * standard structure. |
|
161 | + * |
|
162 | + * If null is returned from this method, the default privilege set is used, |
|
163 | + * which is fine for most common usecases. |
|
164 | + * |
|
165 | + * @return array|null |
|
166 | + */ |
|
167 | + public function getSupportedPrivilegeSet() { |
|
168 | + |
|
169 | + return null; |
|
170 | + |
|
171 | + } |
|
172 | 172 | |
173 | 173 | } |
@@ -2,9 +2,9 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV\Notifications; |
4 | 4 | |
5 | -use Sabre\DAV; |
|
6 | 5 | use Sabre\CalDAV; |
7 | 6 | use Sabre\CalDAV\Xml\Notification\NotificationInterface; |
7 | +use Sabre\DAV; |
|
8 | 8 | use Sabre\DAVACL; |
9 | 9 | |
10 | 10 | /** |
@@ -20,174 +20,174 @@ |
||
20 | 20 | */ |
21 | 21 | class Node extends DAV\File implements INode, DAVACL\IACL { |
22 | 22 | |
23 | - /** |
|
24 | - * The notification backend |
|
25 | - * |
|
26 | - * @var Sabre\CalDAV\Backend\NotificationSupport |
|
27 | - */ |
|
28 | - protected $caldavBackend; |
|
29 | - |
|
30 | - /** |
|
31 | - * The actual notification |
|
32 | - * |
|
33 | - * @var Sabre\CalDAV\Notifications\INotificationType |
|
34 | - */ |
|
35 | - protected $notification; |
|
36 | - |
|
37 | - /** |
|
38 | - * Owner principal of the notification |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $principalUri; |
|
43 | - |
|
44 | - /** |
|
45 | - * Constructor |
|
46 | - * |
|
47 | - * @param CalDAV\Backend\NotificationSupport $caldavBackend |
|
48 | - * @param string $principalUri |
|
49 | - * @param NotificationInterface $notification |
|
50 | - */ |
|
51 | - public function __construct(CalDAV\Backend\NotificationSupport $caldavBackend, $principalUri, NotificationInterface $notification) { |
|
52 | - |
|
53 | - $this->caldavBackend = $caldavBackend; |
|
54 | - $this->principalUri = $principalUri; |
|
55 | - $this->notification = $notification; |
|
56 | - |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Returns the path name for this notification |
|
61 | - * |
|
62 | - * @return id |
|
63 | - */ |
|
64 | - public function getName() { |
|
65 | - |
|
66 | - return $this->notification->getId() . '.xml'; |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the etag for the notification. |
|
72 | - * |
|
73 | - * The etag must be surrounded by litteral double-quotes. |
|
74 | - * |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getETag() { |
|
78 | - |
|
79 | - return $this->notification->getETag(); |
|
80 | - |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * This method must return an xml element, using the |
|
85 | - * Sabre\CalDAV\Notifications\INotificationType classes. |
|
86 | - * |
|
87 | - * @return INotificationType |
|
88 | - */ |
|
89 | - public function getNotificationType() { |
|
90 | - |
|
91 | - return $this->notification; |
|
92 | - |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Deletes this notification |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function delete() { |
|
101 | - |
|
102 | - $this->caldavBackend->deleteNotification($this->getOwner(), $this->notification); |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns the owner principal |
|
108 | - * |
|
109 | - * This must be a url to a principal, or null if there's no owner |
|
110 | - * |
|
111 | - * @return string|null |
|
112 | - */ |
|
113 | - public function getOwner() { |
|
114 | - |
|
115 | - return $this->principalUri; |
|
116 | - |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Returns a group principal |
|
121 | - * |
|
122 | - * This must be a url to a principal, or null if there's no owner |
|
123 | - * |
|
124 | - * @return string|null |
|
125 | - */ |
|
126 | - public function getGroup() { |
|
127 | - |
|
128 | - return null; |
|
129 | - |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns a list of ACE's for this node. |
|
134 | - * |
|
135 | - * Each ACE has the following properties: |
|
136 | - * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
137 | - * currently the only supported privileges |
|
138 | - * * 'principal', a url to the principal who owns the node |
|
139 | - * * 'protected' (optional), indicating that this ACE is not allowed to |
|
140 | - * be updated. |
|
141 | - * |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function getACL() { |
|
145 | - |
|
146 | - return [ |
|
147 | - [ |
|
148 | - 'principal' => $this->getOwner(), |
|
149 | - 'privilege' => '{DAV:}read', |
|
150 | - 'protected' => true, |
|
151 | - ], |
|
152 | - [ |
|
153 | - 'principal' => $this->getOwner(), |
|
154 | - 'privilege' => '{DAV:}write', |
|
155 | - 'protected' => true, |
|
156 | - ] |
|
157 | - ]; |
|
158 | - |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Updates the ACL |
|
163 | - * |
|
164 | - * This method will receive a list of new ACE's as an array argument. |
|
165 | - * |
|
166 | - * @param array $acl |
|
167 | - * @return void |
|
168 | - */ |
|
169 | - public function setACL(array $acl) { |
|
170 | - |
|
171 | - throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here'); |
|
172 | - |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Returns the list of supported privileges for this node. |
|
177 | - * |
|
178 | - * The returned data structure is a list of nested privileges. |
|
179 | - * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple |
|
180 | - * standard structure. |
|
181 | - * |
|
182 | - * If null is returned from this method, the default privilege set is used, |
|
183 | - * which is fine for most common usecases. |
|
184 | - * |
|
185 | - * @return array|null |
|
186 | - */ |
|
187 | - public function getSupportedPrivilegeSet() { |
|
188 | - |
|
189 | - return null; |
|
190 | - |
|
191 | - } |
|
23 | + /** |
|
24 | + * The notification backend |
|
25 | + * |
|
26 | + * @var Sabre\CalDAV\Backend\NotificationSupport |
|
27 | + */ |
|
28 | + protected $caldavBackend; |
|
29 | + |
|
30 | + /** |
|
31 | + * The actual notification |
|
32 | + * |
|
33 | + * @var Sabre\CalDAV\Notifications\INotificationType |
|
34 | + */ |
|
35 | + protected $notification; |
|
36 | + |
|
37 | + /** |
|
38 | + * Owner principal of the notification |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $principalUri; |
|
43 | + |
|
44 | + /** |
|
45 | + * Constructor |
|
46 | + * |
|
47 | + * @param CalDAV\Backend\NotificationSupport $caldavBackend |
|
48 | + * @param string $principalUri |
|
49 | + * @param NotificationInterface $notification |
|
50 | + */ |
|
51 | + public function __construct(CalDAV\Backend\NotificationSupport $caldavBackend, $principalUri, NotificationInterface $notification) { |
|
52 | + |
|
53 | + $this->caldavBackend = $caldavBackend; |
|
54 | + $this->principalUri = $principalUri; |
|
55 | + $this->notification = $notification; |
|
56 | + |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Returns the path name for this notification |
|
61 | + * |
|
62 | + * @return id |
|
63 | + */ |
|
64 | + public function getName() { |
|
65 | + |
|
66 | + return $this->notification->getId() . '.xml'; |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the etag for the notification. |
|
72 | + * |
|
73 | + * The etag must be surrounded by litteral double-quotes. |
|
74 | + * |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getETag() { |
|
78 | + |
|
79 | + return $this->notification->getETag(); |
|
80 | + |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * This method must return an xml element, using the |
|
85 | + * Sabre\CalDAV\Notifications\INotificationType classes. |
|
86 | + * |
|
87 | + * @return INotificationType |
|
88 | + */ |
|
89 | + public function getNotificationType() { |
|
90 | + |
|
91 | + return $this->notification; |
|
92 | + |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Deletes this notification |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function delete() { |
|
101 | + |
|
102 | + $this->caldavBackend->deleteNotification($this->getOwner(), $this->notification); |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns the owner principal |
|
108 | + * |
|
109 | + * This must be a url to a principal, or null if there's no owner |
|
110 | + * |
|
111 | + * @return string|null |
|
112 | + */ |
|
113 | + public function getOwner() { |
|
114 | + |
|
115 | + return $this->principalUri; |
|
116 | + |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Returns a group principal |
|
121 | + * |
|
122 | + * This must be a url to a principal, or null if there's no owner |
|
123 | + * |
|
124 | + * @return string|null |
|
125 | + */ |
|
126 | + public function getGroup() { |
|
127 | + |
|
128 | + return null; |
|
129 | + |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns a list of ACE's for this node. |
|
134 | + * |
|
135 | + * Each ACE has the following properties: |
|
136 | + * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
137 | + * currently the only supported privileges |
|
138 | + * * 'principal', a url to the principal who owns the node |
|
139 | + * * 'protected' (optional), indicating that this ACE is not allowed to |
|
140 | + * be updated. |
|
141 | + * |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function getACL() { |
|
145 | + |
|
146 | + return [ |
|
147 | + [ |
|
148 | + 'principal' => $this->getOwner(), |
|
149 | + 'privilege' => '{DAV:}read', |
|
150 | + 'protected' => true, |
|
151 | + ], |
|
152 | + [ |
|
153 | + 'principal' => $this->getOwner(), |
|
154 | + 'privilege' => '{DAV:}write', |
|
155 | + 'protected' => true, |
|
156 | + ] |
|
157 | + ]; |
|
158 | + |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Updates the ACL |
|
163 | + * |
|
164 | + * This method will receive a list of new ACE's as an array argument. |
|
165 | + * |
|
166 | + * @param array $acl |
|
167 | + * @return void |
|
168 | + */ |
|
169 | + public function setACL(array $acl) { |
|
170 | + |
|
171 | + throw new DAV\Exception\NotImplemented('Updating ACLs is not implemented here'); |
|
172 | + |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Returns the list of supported privileges for this node. |
|
177 | + * |
|
178 | + * The returned data structure is a list of nested privileges. |
|
179 | + * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple |
|
180 | + * standard structure. |
|
181 | + * |
|
182 | + * If null is returned from this method, the default privilege set is used, |
|
183 | + * which is fine for most common usecases. |
|
184 | + * |
|
185 | + * @return array|null |
|
186 | + */ |
|
187 | + public function getSupportedPrivilegeSet() { |
|
188 | + |
|
189 | + return null; |
|
190 | + |
|
191 | + } |
|
192 | 192 | |
193 | 193 | } |
@@ -59,7 +59,7 @@ |
||
59 | 59 | /** |
60 | 60 | * Returns the path name for this notification |
61 | 61 | * |
62 | - * @return id |
|
62 | + * @return string |
|
63 | 63 | */ |
64 | 64 | public function getName() { |
65 | 65 |
@@ -3,11 +3,11 @@ |
||
3 | 3 | namespace Sabre\CalDAV\Notifications; |
4 | 4 | |
5 | 5 | use Sabre\DAV; |
6 | -use Sabre\DAV\PropFind; |
|
6 | +use Sabre\DAVACL; |
|
7 | 7 | use Sabre\DAV\INode as BaseINode; |
8 | -use Sabre\DAV\ServerPlugin; |
|
8 | +use Sabre\DAV\PropFind; |
|
9 | 9 | use Sabre\DAV\Server; |
10 | -use Sabre\DAVACL; |
|
10 | +use Sabre\DAV\ServerPlugin; |
|
11 | 11 | use Sabre\HTTP\RequestInterface; |
12 | 12 | use Sabre\HTTP\ResponseInterface; |
13 | 13 |
@@ -67,7 +67,7 @@ |
||
67 | 67 | |
68 | 68 | $this->server = $server; |
69 | 69 | $server->on('method:GET', [$this, 'httpGet'], 90); |
70 | - $server->on('propFind', [$this, 'propFind']); |
|
70 | + $server->on('propFind', [$this, 'propFind']); |
|
71 | 71 | |
72 | 72 | $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
73 | 73 | $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
@@ -135,8 +135,9 @@ |
||
135 | 135 | return; |
136 | 136 | } |
137 | 137 | |
138 | - if (!$node instanceof INode) |
|
139 | - return; |
|
138 | + if (!$node instanceof INode) { |
|
139 | + return; |
|
140 | + } |
|
140 | 141 | |
141 | 142 | $writer = $this->server->xml->getWriter(); |
142 | 143 | $writer->contextUri = $this->server->getBaseUri(); |
@@ -26,155 +26,155 @@ |
||
26 | 26 | */ |
27 | 27 | class Plugin extends ServerPlugin { |
28 | 28 | |
29 | - /** |
|
30 | - * This is the namespace for the proprietary calendarserver extensions |
|
31 | - */ |
|
32 | - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
33 | - |
|
34 | - /** |
|
35 | - * Reference to the main server object. |
|
36 | - * |
|
37 | - * @var Server |
|
38 | - */ |
|
39 | - protected $server; |
|
40 | - |
|
41 | - /** |
|
42 | - * Returns a plugin name. |
|
43 | - * |
|
44 | - * Using this name other plugins will be able to access other plugins |
|
45 | - * using \Sabre\DAV\Server::getPlugin |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function getPluginName() { |
|
50 | - |
|
51 | - return 'notifications'; |
|
52 | - |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * This initializes the plugin. |
|
57 | - * |
|
58 | - * This function is called by Sabre\DAV\Server, after |
|
59 | - * addPlugin is called. |
|
60 | - * |
|
61 | - * This method should set up the required event subscriptions. |
|
62 | - * |
|
63 | - * @param Server $server |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function initialize(Server $server) { |
|
67 | - |
|
68 | - $this->server = $server; |
|
69 | - $server->on('method:GET', [$this, 'httpGet'], 90); |
|
70 | - $server->on('propFind', [$this, 'propFind']); |
|
71 | - |
|
72 | - $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
73 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
|
74 | - |
|
75 | - array_push($server->protectedProperties, |
|
76 | - '{' . self::NS_CALENDARSERVER . '}notification-URL', |
|
77 | - '{' . self::NS_CALENDARSERVER . '}notificationtype' |
|
78 | - ); |
|
79 | - |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * PropFind |
|
84 | - * |
|
85 | - * @param PropFind $propFind |
|
86 | - * @param BaseINode $node |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function propFind(PropFind $propFind, BaseINode $node) { |
|
90 | - |
|
91 | - $caldavPlugin = $this->server->getPlugin('caldav'); |
|
92 | - |
|
93 | - if ($node instanceof DAVACL\IPrincipal) { |
|
94 | - |
|
95 | - $principalUrl = $node->getPrincipalUrl(); |
|
96 | - |
|
97 | - // notification-URL property |
|
98 | - $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($principalUrl, $caldavPlugin) { |
|
99 | - |
|
100 | - $notificationPath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl) . '/notifications/'; |
|
101 | - return new DAV\Xml\Property\Href($notificationPath); |
|
102 | - |
|
103 | - }); |
|
104 | - |
|
105 | - } |
|
106 | - |
|
107 | - if ($node instanceof INode) { |
|
108 | - |
|
109 | - $propFind->handle( |
|
110 | - '{' . self::NS_CALENDARSERVER . '}notificationtype', |
|
111 | - [$node, 'getNotificationType'] |
|
112 | - ); |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * This event is triggered before the usual GET request handler. |
|
120 | - * |
|
121 | - * We use this to intercept GET calls to notification nodes, and return the |
|
122 | - * proper response. |
|
123 | - * |
|
124 | - * @param RequestInterface $request |
|
125 | - * @param ResponseInterface $response |
|
126 | - * @return void |
|
127 | - */ |
|
128 | - public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
129 | - |
|
130 | - $path = $request->getPath(); |
|
131 | - |
|
132 | - try { |
|
133 | - $node = $this->server->tree->getNodeForPath($path); |
|
134 | - } catch (DAV\Exception\NotFound $e) { |
|
135 | - return; |
|
136 | - } |
|
137 | - |
|
138 | - if (!$node instanceof INode) |
|
139 | - return; |
|
140 | - |
|
141 | - $writer = $this->server->xml->getWriter(); |
|
142 | - $writer->contextUri = $this->server->getBaseUri(); |
|
143 | - $writer->openMemory(); |
|
144 | - $writer->startDocument('1.0', 'UTF-8'); |
|
145 | - $writer->startElement('{http://calendarserver.org/ns/}notification'); |
|
146 | - $node->getNotificationType()->xmlSerializeFull($writer); |
|
147 | - $writer->endElement(); |
|
148 | - |
|
149 | - $response->setHeader('Content-Type', 'application/xml'); |
|
150 | - $response->setHeader('ETag', $node->getETag()); |
|
151 | - $response->setStatus(200); |
|
152 | - $response->setBody($writer->outputMemory()); |
|
153 | - |
|
154 | - // Return false to break the event chain. |
|
155 | - return false; |
|
156 | - |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Returns a bunch of meta-data about the plugin. |
|
161 | - * |
|
162 | - * Providing this information is optional, and is mainly displayed by the |
|
163 | - * Browser plugin. |
|
164 | - * |
|
165 | - * The description key in the returned array may contain html and will not |
|
166 | - * be sanitized. |
|
167 | - * |
|
168 | - * @return array |
|
169 | - */ |
|
170 | - public function getPluginInfo() { |
|
171 | - |
|
172 | - return [ |
|
173 | - 'name' => $this->getPluginName(), |
|
174 | - 'description' => 'Adds support for caldav-notifications, which is required to enable caldav-sharing.', |
|
175 | - 'link' => 'http://sabre.io/dav/caldav-sharing/', |
|
176 | - ]; |
|
177 | - |
|
178 | - } |
|
29 | + /** |
|
30 | + * This is the namespace for the proprietary calendarserver extensions |
|
31 | + */ |
|
32 | + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
33 | + |
|
34 | + /** |
|
35 | + * Reference to the main server object. |
|
36 | + * |
|
37 | + * @var Server |
|
38 | + */ |
|
39 | + protected $server; |
|
40 | + |
|
41 | + /** |
|
42 | + * Returns a plugin name. |
|
43 | + * |
|
44 | + * Using this name other plugins will be able to access other plugins |
|
45 | + * using \Sabre\DAV\Server::getPlugin |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function getPluginName() { |
|
50 | + |
|
51 | + return 'notifications'; |
|
52 | + |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * This initializes the plugin. |
|
57 | + * |
|
58 | + * This function is called by Sabre\DAV\Server, after |
|
59 | + * addPlugin is called. |
|
60 | + * |
|
61 | + * This method should set up the required event subscriptions. |
|
62 | + * |
|
63 | + * @param Server $server |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function initialize(Server $server) { |
|
67 | + |
|
68 | + $this->server = $server; |
|
69 | + $server->on('method:GET', [$this, 'httpGet'], 90); |
|
70 | + $server->on('propFind', [$this, 'propFind']); |
|
71 | + |
|
72 | + $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
73 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
|
74 | + |
|
75 | + array_push($server->protectedProperties, |
|
76 | + '{' . self::NS_CALENDARSERVER . '}notification-URL', |
|
77 | + '{' . self::NS_CALENDARSERVER . '}notificationtype' |
|
78 | + ); |
|
79 | + |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * PropFind |
|
84 | + * |
|
85 | + * @param PropFind $propFind |
|
86 | + * @param BaseINode $node |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function propFind(PropFind $propFind, BaseINode $node) { |
|
90 | + |
|
91 | + $caldavPlugin = $this->server->getPlugin('caldav'); |
|
92 | + |
|
93 | + if ($node instanceof DAVACL\IPrincipal) { |
|
94 | + |
|
95 | + $principalUrl = $node->getPrincipalUrl(); |
|
96 | + |
|
97 | + // notification-URL property |
|
98 | + $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($principalUrl, $caldavPlugin) { |
|
99 | + |
|
100 | + $notificationPath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl) . '/notifications/'; |
|
101 | + return new DAV\Xml\Property\Href($notificationPath); |
|
102 | + |
|
103 | + }); |
|
104 | + |
|
105 | + } |
|
106 | + |
|
107 | + if ($node instanceof INode) { |
|
108 | + |
|
109 | + $propFind->handle( |
|
110 | + '{' . self::NS_CALENDARSERVER . '}notificationtype', |
|
111 | + [$node, 'getNotificationType'] |
|
112 | + ); |
|
113 | + |
|
114 | + } |
|
115 | + |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * This event is triggered before the usual GET request handler. |
|
120 | + * |
|
121 | + * We use this to intercept GET calls to notification nodes, and return the |
|
122 | + * proper response. |
|
123 | + * |
|
124 | + * @param RequestInterface $request |
|
125 | + * @param ResponseInterface $response |
|
126 | + * @return void |
|
127 | + */ |
|
128 | + public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
129 | + |
|
130 | + $path = $request->getPath(); |
|
131 | + |
|
132 | + try { |
|
133 | + $node = $this->server->tree->getNodeForPath($path); |
|
134 | + } catch (DAV\Exception\NotFound $e) { |
|
135 | + return; |
|
136 | + } |
|
137 | + |
|
138 | + if (!$node instanceof INode) |
|
139 | + return; |
|
140 | + |
|
141 | + $writer = $this->server->xml->getWriter(); |
|
142 | + $writer->contextUri = $this->server->getBaseUri(); |
|
143 | + $writer->openMemory(); |
|
144 | + $writer->startDocument('1.0', 'UTF-8'); |
|
145 | + $writer->startElement('{http://calendarserver.org/ns/}notification'); |
|
146 | + $node->getNotificationType()->xmlSerializeFull($writer); |
|
147 | + $writer->endElement(); |
|
148 | + |
|
149 | + $response->setHeader('Content-Type', 'application/xml'); |
|
150 | + $response->setHeader('ETag', $node->getETag()); |
|
151 | + $response->setStatus(200); |
|
152 | + $response->setBody($writer->outputMemory()); |
|
153 | + |
|
154 | + // Return false to break the event chain. |
|
155 | + return false; |
|
156 | + |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Returns a bunch of meta-data about the plugin. |
|
161 | + * |
|
162 | + * Providing this information is optional, and is mainly displayed by the |
|
163 | + * Browser plugin. |
|
164 | + * |
|
165 | + * The description key in the returned array may contain html and will not |
|
166 | + * be sanitized. |
|
167 | + * |
|
168 | + * @return array |
|
169 | + */ |
|
170 | + public function getPluginInfo() { |
|
171 | + |
|
172 | + return [ |
|
173 | + 'name' => $this->getPluginName(), |
|
174 | + 'description' => 'Adds support for caldav-notifications, which is required to enable caldav-sharing.', |
|
175 | + 'link' => 'http://sabre.io/dav/caldav-sharing/', |
|
176 | + ]; |
|
177 | + |
|
178 | + } |
|
179 | 179 | |
180 | 180 | } |
@@ -123,7 +123,7 @@ |
||
123 | 123 | * |
124 | 124 | * @param RequestInterface $request |
125 | 125 | * @param ResponseInterface $response |
126 | - * @return void |
|
126 | + * @return null|false |
|
127 | 127 | */ |
128 | 128 | public function httpGet(RequestInterface $request, ResponseInterface $response) { |
129 | 129 |
@@ -4,15 +4,15 @@ |
||
4 | 4 | |
5 | 5 | use DateTimeZone; |
6 | 6 | use Sabre\DAV; |
7 | +use Sabre\DAVACL; |
|
7 | 8 | use Sabre\DAV\Exception\BadRequest; |
8 | 9 | use Sabre\DAV\MkCol; |
9 | 10 | use Sabre\DAV\Xml\Property\Href; |
10 | -use Sabre\DAVACL; |
|
11 | -use Sabre\VObject; |
|
12 | 11 | use Sabre\HTTP; |
13 | -use Sabre\Uri; |
|
14 | 12 | use Sabre\HTTP\RequestInterface; |
15 | 13 | use Sabre\HTTP\ResponseInterface; |
14 | +use Sabre\Uri; |
|
15 | +use Sabre\VObject; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * CalDAV plugin |
@@ -179,13 +179,13 @@ discard block |
||
179 | 179 | |
180 | 180 | $this->server = $server; |
181 | 181 | |
182 | - $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
183 | - $server->on('report', [$this, 'report']); |
|
184 | - $server->on('propFind', [$this, 'propFind']); |
|
185 | - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
186 | - $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
187 | - $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
188 | - $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
182 | + $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
183 | + $server->on('report', [$this, 'report']); |
|
184 | + $server->on('propFind', [$this, 'propFind']); |
|
185 | + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
186 | + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
187 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
188 | + $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
189 | 189 | |
190 | 190 | $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
191 | 191 | $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | if (isset($properties['{DAV:}resourcetype'])) { |
295 | 295 | $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
296 | 296 | } else { |
297 | - $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
297 | + $resourceType = ['{DAV:}collection', '{urn:ietf:params:xml:ns:caldav}calendar']; |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
@@ -881,12 +881,12 @@ discard block |
||
881 | 881 | if (!isset($component->UID)) { |
882 | 882 | throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
883 | 883 | } |
884 | - $foundUID = (string)$component->UID; |
|
884 | + $foundUID = (string) $component->UID; |
|
885 | 885 | } else { |
886 | 886 | if ($foundType !== $component->name) { |
887 | 887 | throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
888 | 888 | } |
889 | - if ($foundUID !== (string)$component->UID) { |
|
889 | + if ($foundUID !== (string) $component->UID) { |
|
890 | 890 | throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
891 | 891 | } |
892 | 892 | } |
@@ -26,929 +26,929 @@ discard block |
||
26 | 26 | */ |
27 | 27 | class Plugin extends DAV\ServerPlugin { |
28 | 28 | |
29 | - /** |
|
30 | - * This is the official CalDAV namespace |
|
31 | - */ |
|
32 | - const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav'; |
|
33 | - |
|
34 | - /** |
|
35 | - * This is the namespace for the proprietary calendarserver extensions |
|
36 | - */ |
|
37 | - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
38 | - |
|
39 | - /** |
|
40 | - * The hardcoded root for calendar objects. It is unfortunate |
|
41 | - * that we're stuck with it, but it will have to do for now |
|
42 | - */ |
|
43 | - const CALENDAR_ROOT = 'calendars'; |
|
44 | - |
|
45 | - /** |
|
46 | - * Reference to server object |
|
47 | - * |
|
48 | - * @var DAV\Server |
|
49 | - */ |
|
50 | - protected $server; |
|
51 | - |
|
52 | - /** |
|
53 | - * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data, |
|
54 | - * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're |
|
55 | - * capping it to 10M here. |
|
56 | - */ |
|
57 | - protected $maxResourceSize = 10000000; |
|
58 | - |
|
59 | - /** |
|
60 | - * Use this method to tell the server this plugin defines additional |
|
61 | - * HTTP methods. |
|
62 | - * |
|
63 | - * This method is passed a uri. It should only return HTTP methods that are |
|
64 | - * available for the specified uri. |
|
65 | - * |
|
66 | - * @param string $uri |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function getHTTPMethods($uri) { |
|
70 | - |
|
71 | - // The MKCALENDAR is only available on unmapped uri's, whose |
|
72 | - // parents extend IExtendedCollection |
|
73 | - list($parent, $name) = Uri\split($uri); |
|
74 | - |
|
75 | - $node = $this->server->tree->getNodeForPath($parent); |
|
76 | - |
|
77 | - if ($node instanceof DAV\IExtendedCollection) { |
|
78 | - try { |
|
79 | - $node->getChild($name); |
|
80 | - } catch (DAV\Exception\NotFound $e) { |
|
81 | - return ['MKCALENDAR']; |
|
82 | - } |
|
83 | - } |
|
84 | - return []; |
|
85 | - |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Returns the path to a principal's calendar home. |
|
90 | - * |
|
91 | - * The return url must not end with a slash. |
|
92 | - * This function should return null in case a principal did not have |
|
93 | - * a calendar home. |
|
94 | - * |
|
95 | - * @param string $principalUrl |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - public function getCalendarHomeForPrincipal($principalUrl) { |
|
99 | - |
|
100 | - // The default behavior for most sabre/dav servers is that there is a |
|
101 | - // principals root node, which contains users directly under it. |
|
102 | - // |
|
103 | - // This function assumes that there are two components in a principal |
|
104 | - // path. If there's more, we don't return a calendar home. This |
|
105 | - // excludes things like the calendar-proxy-read principal (which it |
|
106 | - // should). |
|
107 | - $parts = explode('/', trim($principalUrl, '/')); |
|
108 | - if (count($parts) !== 2) return; |
|
109 | - if ($parts[0] !== 'principals') return; |
|
110 | - |
|
111 | - return self::CALENDAR_ROOT . '/' . $parts[1]; |
|
112 | - |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns a list of features for the DAV: HTTP header. |
|
117 | - * |
|
118 | - * @return array |
|
119 | - */ |
|
120 | - public function getFeatures() { |
|
121 | - |
|
122 | - return ['calendar-access', 'calendar-proxy']; |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns a plugin name. |
|
128 | - * |
|
129 | - * Using this name other plugins will be able to access other plugins |
|
130 | - * using DAV\Server::getPlugin |
|
131 | - * |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function getPluginName() { |
|
135 | - |
|
136 | - return 'caldav'; |
|
137 | - |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Returns a list of reports this plugin supports. |
|
142 | - * |
|
143 | - * This will be used in the {DAV:}supported-report-set property. |
|
144 | - * Note that you still need to subscribe to the 'report' event to actually |
|
145 | - * implement them |
|
146 | - * |
|
147 | - * @param string $uri |
|
148 | - * @return array |
|
149 | - */ |
|
150 | - public function getSupportedReportSet($uri) { |
|
151 | - |
|
152 | - $node = $this->server->tree->getNodeForPath($uri); |
|
153 | - |
|
154 | - $reports = []; |
|
155 | - if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) { |
|
156 | - $reports[] = '{' . self::NS_CALDAV . '}calendar-multiget'; |
|
157 | - $reports[] = '{' . self::NS_CALDAV . '}calendar-query'; |
|
158 | - } |
|
159 | - if ($node instanceof ICalendar) { |
|
160 | - $reports[] = '{' . self::NS_CALDAV . '}free-busy-query'; |
|
161 | - } |
|
162 | - // iCal has a bug where it assumes that sync support is enabled, only |
|
163 | - // if we say we support it on the calendar-home, even though this is |
|
164 | - // not actually the case. |
|
165 | - if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) { |
|
166 | - $reports[] = '{DAV:}sync-collection'; |
|
167 | - } |
|
168 | - return $reports; |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Initializes the plugin |
|
174 | - * |
|
175 | - * @param DAV\Server $server |
|
176 | - * @return void |
|
177 | - */ |
|
178 | - public function initialize(DAV\Server $server) { |
|
179 | - |
|
180 | - $this->server = $server; |
|
181 | - |
|
182 | - $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
183 | - $server->on('report', [$this, 'report']); |
|
184 | - $server->on('propFind', [$this, 'propFind']); |
|
185 | - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
186 | - $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
187 | - $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
188 | - $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
189 | - |
|
190 | - $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
|
191 | - $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
192 | - |
|
193 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
194 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport'; |
|
195 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport'; |
|
196 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport'; |
|
197 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar'; |
|
198 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp'; |
|
199 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
200 | - |
|
201 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar'; |
|
202 | - |
|
203 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read'; |
|
204 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write'; |
|
205 | - |
|
206 | - array_push($server->protectedProperties, |
|
207 | - |
|
208 | - '{' . self::NS_CALDAV . '}supported-calendar-component-set', |
|
209 | - '{' . self::NS_CALDAV . '}supported-calendar-data', |
|
210 | - '{' . self::NS_CALDAV . '}max-resource-size', |
|
211 | - '{' . self::NS_CALDAV . '}min-date-time', |
|
212 | - '{' . self::NS_CALDAV . '}max-date-time', |
|
213 | - '{' . self::NS_CALDAV . '}max-instances', |
|
214 | - '{' . self::NS_CALDAV . '}max-attendees-per-instance', |
|
215 | - '{' . self::NS_CALDAV . '}calendar-home-set', |
|
216 | - '{' . self::NS_CALDAV . '}supported-collation-set', |
|
217 | - '{' . self::NS_CALDAV . '}calendar-data', |
|
218 | - |
|
219 | - // CalendarServer extensions |
|
220 | - '{' . self::NS_CALENDARSERVER . '}getctag', |
|
221 | - '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for', |
|
222 | - '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for' |
|
223 | - |
|
224 | - ); |
|
225 | - |
|
226 | - if ($aclPlugin = $server->getPlugin('acl')) { |
|
227 | - $aclPlugin->principalSearchPropertySet['{' . self::NS_CALDAV . '}calendar-user-address-set'] = 'Calendar address'; |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * This functions handles REPORT requests specific to CalDAV |
|
233 | - * |
|
234 | - * @param string $reportName |
|
235 | - * @param mixed $report |
|
236 | - * @return bool |
|
237 | - */ |
|
238 | - public function report($reportName, $report) { |
|
239 | - |
|
240 | - switch ($reportName) { |
|
241 | - case '{' . self::NS_CALDAV . '}calendar-multiget' : |
|
242 | - $this->server->transactionType = 'report-calendar-multiget'; |
|
243 | - $this->calendarMultiGetReport($report); |
|
244 | - return false; |
|
245 | - case '{' . self::NS_CALDAV . '}calendar-query' : |
|
246 | - $this->server->transactionType = 'report-calendar-query'; |
|
247 | - $this->calendarQueryReport($report); |
|
248 | - return false; |
|
249 | - case '{' . self::NS_CALDAV . '}free-busy-query' : |
|
250 | - $this->server->transactionType = 'report-free-busy-query'; |
|
251 | - $this->freeBusyQueryReport($report); |
|
252 | - return false; |
|
253 | - |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * This function handles the MKCALENDAR HTTP method, which creates |
|
261 | - * a new calendar. |
|
262 | - * |
|
263 | - * @param RequestInterface $request |
|
264 | - * @param ResponseInterface $response |
|
265 | - * @return bool |
|
266 | - */ |
|
267 | - public function httpMkCalendar(RequestInterface $request, ResponseInterface $response) { |
|
268 | - |
|
269 | - $body = $request->getBodyAsString(); |
|
270 | - $path = $request->getPath(); |
|
271 | - |
|
272 | - $properties = []; |
|
273 | - |
|
274 | - if ($body) { |
|
275 | - |
|
276 | - try { |
|
277 | - $mkcalendar = $this->server->xml->expect( |
|
278 | - '{urn:ietf:params:xml:ns:caldav}mkcalendar', |
|
279 | - $body |
|
280 | - ); |
|
281 | - } catch (\Sabre\Xml\ParseException $e) { |
|
282 | - throw new BadRequest($e->getMessage(), null, $e); |
|
283 | - } |
|
284 | - $properties = $mkcalendar->getProperties(); |
|
285 | - |
|
286 | - } |
|
287 | - |
|
288 | - // iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored |
|
289 | - // subscriptions. Before that it used MKCOL which was the correct way |
|
290 | - // to do this. |
|
291 | - // |
|
292 | - // If the body had a {DAV:}resourcetype, it means we stumbled upon this |
|
293 | - // request, and we simply use it instead of the pre-defined list. |
|
294 | - if (isset($properties['{DAV:}resourcetype'])) { |
|
295 | - $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
|
296 | - } else { |
|
297 | - $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
298 | - } |
|
299 | - |
|
300 | - $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
|
301 | - |
|
302 | - $this->server->httpResponse->setStatus(201); |
|
303 | - $this->server->httpResponse->setHeader('Content-Length', 0); |
|
304 | - |
|
305 | - // This breaks the method chain. |
|
306 | - return false; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * PropFind |
|
311 | - * |
|
312 | - * This method handler is invoked before any after properties for a |
|
313 | - * resource are fetched. This allows us to add in any CalDAV specific |
|
314 | - * properties. |
|
315 | - * |
|
316 | - * @param DAV\PropFind $propFind |
|
317 | - * @param DAV\INode $node |
|
318 | - * @return void |
|
319 | - */ |
|
320 | - public function propFind(DAV\PropFind $propFind, DAV\INode $node) { |
|
321 | - |
|
322 | - $ns = '{' . self::NS_CALDAV . '}'; |
|
323 | - |
|
324 | - if ($node instanceof ICalendarObjectContainer) { |
|
325 | - |
|
326 | - $propFind->handle($ns . 'max-resource-size', $this->maxResourceSize); |
|
327 | - $propFind->handle($ns . 'supported-calendar-data', function() { |
|
328 | - return new Xml\Property\SupportedCalendarData(); |
|
329 | - }); |
|
330 | - $propFind->handle($ns . 'supported-collation-set', function() { |
|
331 | - return new Xml\Property\SupportedCollationSet(); |
|
332 | - }); |
|
333 | - |
|
334 | - } |
|
335 | - |
|
336 | - if ($node instanceof DAVACL\IPrincipal) { |
|
337 | - |
|
338 | - $principalUrl = $node->getPrincipalUrl(); |
|
339 | - |
|
340 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
|
341 | - |
|
342 | - $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
|
343 | - if (is_null($calendarHomePath)) return null; |
|
344 | - return new Href($calendarHomePath . '/'); |
|
345 | - |
|
346 | - }); |
|
347 | - // The calendar-user-address-set property is basically mapped to |
|
348 | - // the {DAV:}alternate-URI-set property. |
|
349 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-user-address-set', function() use ($node) { |
|
350 | - $addresses = $node->getAlternateUriSet(); |
|
351 | - $addresses[] = $this->server->getBaseUri() . $node->getPrincipalUrl() . '/'; |
|
352 | - return new Href($addresses, false); |
|
353 | - }); |
|
354 | - // For some reason somebody thought it was a good idea to add |
|
355 | - // another one of these properties. We're supporting it too. |
|
356 | - $propFind->handle('{' . self::NS_CALENDARSERVER . '}email-address-set', function() use ($node) { |
|
357 | - $addresses = $node->getAlternateUriSet(); |
|
358 | - $emails = []; |
|
359 | - foreach ($addresses as $address) { |
|
360 | - if (substr($address, 0, 7) === 'mailto:') { |
|
361 | - $emails[] = substr($address, 7); |
|
362 | - } |
|
363 | - } |
|
364 | - return new Xml\Property\EmailAddressSet($emails); |
|
365 | - }); |
|
366 | - |
|
367 | - // These two properties are shortcuts for ical to easily find |
|
368 | - // other principals this principal has access to. |
|
369 | - $propRead = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for'; |
|
370 | - $propWrite = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for'; |
|
371 | - |
|
372 | - if ($propFind->getStatus($propRead) === 404 || $propFind->getStatus($propWrite) === 404) { |
|
373 | - |
|
374 | - $aclPlugin = $this->server->getPlugin('acl'); |
|
375 | - $membership = $aclPlugin->getPrincipalMembership($propFind->getPath()); |
|
376 | - $readList = []; |
|
377 | - $writeList = []; |
|
378 | - |
|
379 | - foreach ($membership as $group) { |
|
380 | - |
|
381 | - $groupNode = $this->server->tree->getNodeForPath($group); |
|
382 | - |
|
383 | - $listItem = Uri\split($group)[0] . '/'; |
|
384 | - |
|
385 | - // If the node is either ap proxy-read or proxy-write |
|
386 | - // group, we grab the parent principal and add it to the |
|
387 | - // list. |
|
388 | - if ($groupNode instanceof Principal\IProxyRead) { |
|
389 | - $readList[] = $listItem; |
|
390 | - } |
|
391 | - if ($groupNode instanceof Principal\IProxyWrite) { |
|
392 | - $writeList[] = $listItem; |
|
393 | - } |
|
394 | - |
|
395 | - } |
|
396 | - |
|
397 | - $propFind->set($propRead, new Href($readList)); |
|
398 | - $propFind->set($propWrite, new Href($writeList)); |
|
399 | - |
|
400 | - } |
|
401 | - |
|
402 | - } // instanceof IPrincipal |
|
403 | - |
|
404 | - if ($node instanceof ICalendarObject) { |
|
405 | - |
|
406 | - // The calendar-data property is not supposed to be a 'real' |
|
407 | - // property, but in large chunks of the spec it does act as such. |
|
408 | - // Therefore we simply expose it as a property. |
|
409 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
|
410 | - $val = $node->get(); |
|
411 | - if (is_resource($val)) |
|
412 | - $val = stream_get_contents($val); |
|
413 | - |
|
414 | - // Taking out \r to not screw up the xml output |
|
415 | - return str_replace("\r", "", $val); |
|
416 | - |
|
417 | - }); |
|
418 | - |
|
419 | - } |
|
420 | - |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * This function handles the calendar-multiget REPORT. |
|
425 | - * |
|
426 | - * This report is used by the client to fetch the content of a series |
|
427 | - * of urls. Effectively avoiding a lot of redundant requests. |
|
428 | - * |
|
429 | - * @param CalendarMultiGetReport $report |
|
430 | - * @return void |
|
431 | - */ |
|
432 | - public function calendarMultiGetReport($report) { |
|
433 | - |
|
434 | - $needsJson = $report->contentType === 'application/calendar+json'; |
|
435 | - |
|
436 | - $timeZones = []; |
|
437 | - $propertyList = []; |
|
438 | - |
|
439 | - $paths = array_map( |
|
440 | - [$this->server, 'calculateUri'], |
|
441 | - $report->hrefs |
|
442 | - ); |
|
443 | - |
|
444 | - foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) { |
|
445 | - |
|
446 | - if (($needsJson || $report->expand) && isset($objProps[200]['{' . self::NS_CALDAV . '}calendar-data'])) { |
|
447 | - $vObject = VObject\Reader::read($objProps[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
448 | - |
|
449 | - if ($report->expand) { |
|
450 | - // We're expanding, and for that we need to figure out the |
|
451 | - // calendar's timezone. |
|
452 | - list($calendarPath) = Uri\split($uri); |
|
453 | - if (!isset($timeZones[$calendarPath])) { |
|
454 | - // Checking the calendar-timezone property. |
|
455 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
456 | - $tzResult = $this->server->getProperties($calendarPath, [$tzProp]); |
|
457 | - if (isset($tzResult[$tzProp])) { |
|
458 | - // This property contains a VCALENDAR with a single |
|
459 | - // VTIMEZONE. |
|
460 | - $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
461 | - $timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
462 | - } else { |
|
463 | - // Defaulting to UTC. |
|
464 | - $timeZone = new DateTimeZone('UTC'); |
|
465 | - } |
|
466 | - $timeZones[$calendarPath] = $timeZone; |
|
467 | - } |
|
468 | - |
|
469 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]); |
|
470 | - } |
|
471 | - if ($needsJson) { |
|
472 | - $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
473 | - } else { |
|
474 | - $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
475 | - } |
|
476 | - // Destroy circular references so PHP will garbage collect the |
|
477 | - // object. |
|
478 | - $vObject->destroy(); |
|
479 | - } |
|
480 | - |
|
481 | - $propertyList[] = $objProps; |
|
482 | - |
|
483 | - } |
|
484 | - |
|
485 | - $prefer = $this->server->getHTTPPrefer(); |
|
486 | - |
|
487 | - $this->server->httpResponse->setStatus(207); |
|
488 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
489 | - $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
490 | - $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, $prefer['return'] === 'minimal')); |
|
491 | - |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * This function handles the calendar-query REPORT |
|
496 | - * |
|
497 | - * This report is used by clients to request calendar objects based on |
|
498 | - * complex conditions. |
|
499 | - * |
|
500 | - * @param Xml\Request\CalendarQueryReport $report |
|
501 | - * @return void |
|
502 | - */ |
|
503 | - public function calendarQueryReport($report) { |
|
504 | - |
|
505 | - $path = $this->server->getRequestUri(); |
|
506 | - |
|
507 | - $needsJson = $report->contentType === 'application/calendar+json'; |
|
508 | - |
|
509 | - $node = $this->server->tree->getNodeForPath($this->server->getRequestUri()); |
|
510 | - $depth = $this->server->getHTTPDepth(0); |
|
511 | - |
|
512 | - // The default result is an empty array |
|
513 | - $result = []; |
|
514 | - |
|
515 | - $calendarTimeZone = null; |
|
516 | - if ($report->expand) { |
|
517 | - // We're expanding, and for that we need to figure out the |
|
518 | - // calendar's timezone. |
|
519 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
520 | - $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
521 | - if (isset($tzResult[$tzProp])) { |
|
522 | - // This property contains a VCALENDAR with a single |
|
523 | - // VTIMEZONE. |
|
524 | - $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
525 | - $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
526 | - |
|
527 | - // Destroy circular references so PHP will garbage collect the |
|
528 | - // object. |
|
529 | - $vtimezoneObj->destroy(); |
|
530 | - } else { |
|
531 | - // Defaulting to UTC. |
|
532 | - $calendarTimeZone = new DateTimeZone('UTC'); |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - // The calendarobject was requested directly. In this case we handle |
|
537 | - // this locally. |
|
538 | - if ($depth == 0 && $node instanceof ICalendarObject) { |
|
539 | - |
|
540 | - $requestedCalendarData = true; |
|
541 | - $requestedProperties = $report->properties; |
|
542 | - |
|
543 | - if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) { |
|
544 | - |
|
545 | - // We always retrieve calendar-data, as we need it for filtering. |
|
546 | - $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data'; |
|
547 | - |
|
548 | - // If calendar-data wasn't explicitly requested, we need to remove |
|
549 | - // it after processing. |
|
550 | - $requestedCalendarData = false; |
|
551 | - } |
|
552 | - |
|
553 | - $properties = $this->server->getPropertiesForPath( |
|
554 | - $path, |
|
555 | - $requestedProperties, |
|
556 | - 0 |
|
557 | - ); |
|
558 | - |
|
559 | - // This array should have only 1 element, the first calendar |
|
560 | - // object. |
|
561 | - $properties = current($properties); |
|
562 | - |
|
563 | - // If there wasn't any calendar-data returned somehow, we ignore |
|
564 | - // this. |
|
565 | - if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) { |
|
566 | - |
|
567 | - $validator = new CalendarQueryValidator(); |
|
568 | - |
|
569 | - $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
570 | - if ($validator->validate($vObject, $report->filters)) { |
|
571 | - |
|
572 | - // If the client didn't require the calendar-data property, |
|
573 | - // we won't give it back. |
|
574 | - if (!$requestedCalendarData) { |
|
575 | - unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
576 | - } else { |
|
577 | - |
|
578 | - |
|
579 | - if ($report->expand) { |
|
580 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
581 | - } |
|
582 | - if ($needsJson) { |
|
583 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
584 | - } elseif ($report->expand) { |
|
585 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
586 | - } |
|
587 | - } |
|
588 | - |
|
589 | - $result = [$properties]; |
|
590 | - |
|
591 | - } |
|
592 | - // Destroy circular references so PHP will garbage collect the |
|
593 | - // object. |
|
594 | - $vObject->destroy(); |
|
595 | - |
|
596 | - } |
|
597 | - |
|
598 | - } |
|
599 | - |
|
600 | - if ($node instanceof ICalendarObjectContainer && $depth === 0) { |
|
601 | - |
|
602 | - if (strpos($this->server->httpRequest->getHeader('User-Agent'), 'MSFT-') === 0) { |
|
603 | - // Microsoft clients incorrectly supplied depth as 0, when it actually |
|
604 | - // should have set depth to 1. We're implementing a workaround here |
|
605 | - // to deal with this. |
|
606 | - // |
|
607 | - // This targets at least the following clients: |
|
608 | - // Windows 10 |
|
609 | - // Windows Phone 8, 10 |
|
610 | - $depth = 1; |
|
611 | - } else { |
|
612 | - throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1'); |
|
613 | - } |
|
614 | - |
|
615 | - } |
|
616 | - |
|
617 | - // If we're dealing with a calendar, the calendar itself is responsible |
|
618 | - // for the calendar-query. |
|
619 | - if ($node instanceof ICalendarObjectContainer && $depth == 1) { |
|
620 | - |
|
621 | - $nodePaths = $node->calendarQuery($report->filters); |
|
622 | - |
|
623 | - foreach ($nodePaths as $path) { |
|
624 | - |
|
625 | - list($properties) = |
|
626 | - $this->server->getPropertiesForPath($this->server->getRequestUri() . '/' . $path, $report->properties); |
|
627 | - |
|
628 | - if (($needsJson || $report->expand)) { |
|
629 | - $vObject = VObject\Reader::read($properties[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
630 | - |
|
631 | - if ($report->expand) { |
|
632 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
633 | - } |
|
634 | - |
|
635 | - if ($needsJson) { |
|
636 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
637 | - } else { |
|
638 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
639 | - } |
|
640 | - |
|
641 | - // Destroy circular references so PHP will garbage collect the |
|
642 | - // object. |
|
643 | - $vObject->destroy(); |
|
644 | - } |
|
645 | - $result[] = $properties; |
|
646 | - |
|
647 | - } |
|
648 | - |
|
649 | - } |
|
650 | - |
|
651 | - $prefer = $this->server->getHTTPPrefer(); |
|
652 | - |
|
653 | - $this->server->httpResponse->setStatus(207); |
|
654 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
655 | - $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
656 | - $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, $prefer['return'] === 'minimal')); |
|
657 | - |
|
658 | - } |
|
659 | - |
|
660 | - /** |
|
661 | - * This method is responsible for parsing the request and generating the |
|
662 | - * response for the CALDAV:free-busy-query REPORT. |
|
663 | - * |
|
664 | - * @param Xml\Request\FreeBusyQueryReport $report |
|
665 | - * @return void |
|
666 | - */ |
|
667 | - protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report) { |
|
668 | - |
|
669 | - $uri = $this->server->getRequestUri(); |
|
670 | - |
|
671 | - $acl = $this->server->getPlugin('acl'); |
|
672 | - if ($acl) { |
|
673 | - $acl->checkPrivileges($uri, '{' . self::NS_CALDAV . '}read-free-busy'); |
|
674 | - } |
|
675 | - |
|
676 | - $calendar = $this->server->tree->getNodeForPath($uri); |
|
677 | - if (!$calendar instanceof ICalendar) { |
|
678 | - throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars'); |
|
679 | - } |
|
680 | - |
|
681 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
682 | - |
|
683 | - // Figuring out the default timezone for the calendar, for floating |
|
684 | - // times. |
|
685 | - $calendarProps = $this->server->getProperties($uri, [$tzProp]); |
|
686 | - |
|
687 | - if (isset($calendarProps[$tzProp])) { |
|
688 | - $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]); |
|
689 | - $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
690 | - // Destroy circular references so PHP will garbage collect the object. |
|
691 | - $vtimezoneObj->destroy(); |
|
692 | - } else { |
|
693 | - $calendarTimeZone = new DateTimeZone('UTC'); |
|
694 | - } |
|
695 | - |
|
696 | - // Doing a calendar-query first, to make sure we get the most |
|
697 | - // performance. |
|
698 | - $urls = $calendar->calendarQuery([ |
|
699 | - 'name' => 'VCALENDAR', |
|
700 | - 'comp-filters' => [ |
|
701 | - [ |
|
702 | - 'name' => 'VEVENT', |
|
703 | - 'comp-filters' => [], |
|
704 | - 'prop-filters' => [], |
|
705 | - 'is-not-defined' => false, |
|
706 | - 'time-range' => [ |
|
707 | - 'start' => $report->start, |
|
708 | - 'end' => $report->end, |
|
709 | - ], |
|
710 | - ], |
|
711 | - ], |
|
712 | - 'prop-filters' => [], |
|
713 | - 'is-not-defined' => false, |
|
714 | - 'time-range' => null, |
|
715 | - ]); |
|
716 | - |
|
717 | - $objects = array_map(function($url) use ($calendar) { |
|
718 | - $obj = $calendar->getChild($url)->get(); |
|
719 | - return $obj; |
|
720 | - }, $urls); |
|
721 | - |
|
722 | - $generator = new VObject\FreeBusyGenerator(); |
|
723 | - $generator->setObjects($objects); |
|
724 | - $generator->setTimeRange($report->start, $report->end); |
|
725 | - $generator->setTimeZone($calendarTimeZone); |
|
726 | - $result = $generator->getResult(); |
|
727 | - $result = $result->serialize(); |
|
728 | - |
|
729 | - $this->server->httpResponse->setStatus(200); |
|
730 | - $this->server->httpResponse->setHeader('Content-Type', 'text/calendar'); |
|
731 | - $this->server->httpResponse->setHeader('Content-Length', strlen($result)); |
|
732 | - $this->server->httpResponse->setBody($result); |
|
733 | - |
|
734 | - } |
|
735 | - |
|
736 | - /** |
|
737 | - * This method is triggered before a file gets updated with new content. |
|
738 | - * |
|
739 | - * This plugin uses this method to ensure that CalDAV objects receive |
|
740 | - * valid calendar data. |
|
741 | - * |
|
742 | - * @param string $path |
|
743 | - * @param DAV\IFile $node |
|
744 | - * @param resource $data |
|
745 | - * @param bool $modified Should be set to true, if this event handler |
|
746 | - * changed &$data. |
|
747 | - * @return void |
|
748 | - */ |
|
749 | - public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
|
750 | - |
|
751 | - if (!$node instanceof ICalendarObject) |
|
752 | - return; |
|
753 | - |
|
754 | - // We're onyl interested in ICalendarObject nodes that are inside of a |
|
755 | - // real calendar. This is to avoid triggering validation and scheduling |
|
756 | - // for non-calendars (such as an inbox). |
|
757 | - list($parent) = Uri\split($path); |
|
758 | - $parentNode = $this->server->tree->getNodeForPath($parent); |
|
759 | - |
|
760 | - if (!$parentNode instanceof ICalendar) |
|
761 | - return; |
|
762 | - |
|
763 | - $this->validateICalendar( |
|
764 | - $data, |
|
765 | - $path, |
|
766 | - $modified, |
|
767 | - $this->server->httpRequest, |
|
768 | - $this->server->httpResponse, |
|
769 | - false |
|
770 | - ); |
|
771 | - |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * This method is triggered before a new file is created. |
|
776 | - * |
|
777 | - * This plugin uses this method to ensure that newly created calendar |
|
778 | - * objects contain valid calendar data. |
|
779 | - * |
|
780 | - * @param string $path |
|
781 | - * @param resource $data |
|
782 | - * @param DAV\ICollection $parentNode |
|
783 | - * @param bool $modified Should be set to true, if this event handler |
|
784 | - * changed &$data. |
|
785 | - * @return void |
|
786 | - */ |
|
787 | - public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
|
788 | - |
|
789 | - if (!$parentNode instanceof ICalendar) |
|
790 | - return; |
|
791 | - |
|
792 | - $this->validateICalendar( |
|
793 | - $data, |
|
794 | - $path, |
|
795 | - $modified, |
|
796 | - $this->server->httpRequest, |
|
797 | - $this->server->httpResponse, |
|
798 | - true |
|
799 | - ); |
|
800 | - |
|
801 | - } |
|
802 | - |
|
803 | - /** |
|
804 | - * Checks if the submitted iCalendar data is in fact, valid. |
|
805 | - * |
|
806 | - * An exception is thrown if it's not. |
|
807 | - * |
|
808 | - * @param resource|string $data |
|
809 | - * @param string $path |
|
810 | - * @param bool $modified Should be set to true, if this event handler |
|
811 | - * changed &$data. |
|
812 | - * @param RequestInterface $request The http request. |
|
813 | - * @param ResponseInterface $response The http response. |
|
814 | - * @param bool $isNew Is the item a new one, or an update. |
|
815 | - * @return void |
|
816 | - */ |
|
817 | - protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew) { |
|
818 | - |
|
819 | - // If it's a stream, we convert it to a string first. |
|
820 | - if (is_resource($data)) { |
|
821 | - $data = stream_get_contents($data); |
|
822 | - } |
|
823 | - |
|
824 | - $before = md5($data); |
|
825 | - // Converting the data to unicode, if needed. |
|
826 | - $data = DAV\StringUtil::ensureUTF8($data); |
|
827 | - |
|
828 | - if ($before !== md5($data)) $modified = true; |
|
829 | - |
|
830 | - try { |
|
831 | - |
|
832 | - // If the data starts with a [, we can reasonably assume we're dealing |
|
833 | - // with a jCal object. |
|
834 | - if (substr($data, 0, 1) === '[') { |
|
835 | - $vobj = VObject\Reader::readJson($data); |
|
836 | - |
|
837 | - // Converting $data back to iCalendar, as that's what we |
|
838 | - // technically support everywhere. |
|
839 | - $data = $vobj->serialize(); |
|
840 | - $modified = true; |
|
841 | - } else { |
|
842 | - $vobj = VObject\Reader::read($data); |
|
843 | - } |
|
844 | - |
|
845 | - } catch (VObject\ParseException $e) { |
|
846 | - |
|
847 | - throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: ' . $e->getMessage()); |
|
848 | - |
|
849 | - } |
|
850 | - |
|
851 | - if ($vobj->name !== 'VCALENDAR') { |
|
852 | - throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.'); |
|
853 | - } |
|
854 | - |
|
855 | - $sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
856 | - |
|
857 | - // Get the Supported Components for the target calendar |
|
858 | - list($parentPath) = Uri\split($path); |
|
859 | - $calendarProperties = $this->server->getProperties($parentPath, [$sCCS]); |
|
860 | - |
|
861 | - if (isset($calendarProperties[$sCCS])) { |
|
862 | - $supportedComponents = $calendarProperties[$sCCS]->getValue(); |
|
863 | - } else { |
|
864 | - $supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT']; |
|
865 | - } |
|
866 | - |
|
867 | - $foundType = null; |
|
868 | - $foundUID = null; |
|
869 | - foreach ($vobj->getComponents() as $component) { |
|
870 | - switch ($component->name) { |
|
871 | - case 'VTIMEZONE' : |
|
872 | - continue 2; |
|
873 | - case 'VEVENT' : |
|
874 | - case 'VTODO' : |
|
875 | - case 'VJOURNAL' : |
|
876 | - if (is_null($foundType)) { |
|
877 | - $foundType = $component->name; |
|
878 | - if (!in_array($foundType, $supportedComponents)) { |
|
879 | - throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType); |
|
880 | - } |
|
881 | - if (!isset($component->UID)) { |
|
882 | - throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
|
883 | - } |
|
884 | - $foundUID = (string)$component->UID; |
|
885 | - } else { |
|
886 | - if ($foundType !== $component->name) { |
|
887 | - throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
|
888 | - } |
|
889 | - if ($foundUID !== (string)$component->UID) { |
|
890 | - throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
|
891 | - } |
|
892 | - } |
|
893 | - break; |
|
894 | - default : |
|
895 | - throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here'); |
|
896 | - |
|
897 | - } |
|
898 | - } |
|
899 | - if (!$foundType) |
|
900 | - throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
901 | - |
|
902 | - // We use an extra variable to allow event handles to tell us wether |
|
903 | - // the object was modified or not. |
|
904 | - // |
|
905 | - // This helps us determine if we need to re-serialize the object. |
|
906 | - $subModified = false; |
|
907 | - |
|
908 | - $this->server->emit( |
|
909 | - 'calendarObjectChange', |
|
910 | - [ |
|
911 | - $request, |
|
912 | - $response, |
|
913 | - $vobj, |
|
914 | - $parentPath, |
|
915 | - &$subModified, |
|
916 | - $isNew |
|
917 | - ] |
|
918 | - ); |
|
919 | - |
|
920 | - if ($subModified) { |
|
921 | - // An event handler told us that it modified the object. |
|
922 | - $data = $vobj->serialize(); |
|
923 | - |
|
924 | - // Using md5 to figure out if there was an *actual* change. |
|
925 | - if (!$modified && $before !== md5($data)) { |
|
926 | - $modified = true; |
|
927 | - } |
|
928 | - |
|
929 | - } |
|
930 | - |
|
931 | - // Destroy circular references so PHP will garbage collect the object. |
|
932 | - $vobj->destroy(); |
|
933 | - |
|
934 | - } |
|
935 | - |
|
936 | - |
|
937 | - /** |
|
938 | - * This method is used to generate HTML output for the |
|
939 | - * DAV\Browser\Plugin. This allows us to generate an interface users |
|
940 | - * can use to create new calendars. |
|
941 | - * |
|
942 | - * @param DAV\INode $node |
|
943 | - * @param string $output |
|
944 | - * @return bool |
|
945 | - */ |
|
946 | - public function htmlActionsPanel(DAV\INode $node, &$output) { |
|
947 | - |
|
948 | - if (!$node instanceof CalendarHome) |
|
949 | - return; |
|
950 | - |
|
951 | - $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
29 | + /** |
|
30 | + * This is the official CalDAV namespace |
|
31 | + */ |
|
32 | + const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav'; |
|
33 | + |
|
34 | + /** |
|
35 | + * This is the namespace for the proprietary calendarserver extensions |
|
36 | + */ |
|
37 | + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
38 | + |
|
39 | + /** |
|
40 | + * The hardcoded root for calendar objects. It is unfortunate |
|
41 | + * that we're stuck with it, but it will have to do for now |
|
42 | + */ |
|
43 | + const CALENDAR_ROOT = 'calendars'; |
|
44 | + |
|
45 | + /** |
|
46 | + * Reference to server object |
|
47 | + * |
|
48 | + * @var DAV\Server |
|
49 | + */ |
|
50 | + protected $server; |
|
51 | + |
|
52 | + /** |
|
53 | + * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data, |
|
54 | + * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're |
|
55 | + * capping it to 10M here. |
|
56 | + */ |
|
57 | + protected $maxResourceSize = 10000000; |
|
58 | + |
|
59 | + /** |
|
60 | + * Use this method to tell the server this plugin defines additional |
|
61 | + * HTTP methods. |
|
62 | + * |
|
63 | + * This method is passed a uri. It should only return HTTP methods that are |
|
64 | + * available for the specified uri. |
|
65 | + * |
|
66 | + * @param string $uri |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function getHTTPMethods($uri) { |
|
70 | + |
|
71 | + // The MKCALENDAR is only available on unmapped uri's, whose |
|
72 | + // parents extend IExtendedCollection |
|
73 | + list($parent, $name) = Uri\split($uri); |
|
74 | + |
|
75 | + $node = $this->server->tree->getNodeForPath($parent); |
|
76 | + |
|
77 | + if ($node instanceof DAV\IExtendedCollection) { |
|
78 | + try { |
|
79 | + $node->getChild($name); |
|
80 | + } catch (DAV\Exception\NotFound $e) { |
|
81 | + return ['MKCALENDAR']; |
|
82 | + } |
|
83 | + } |
|
84 | + return []; |
|
85 | + |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Returns the path to a principal's calendar home. |
|
90 | + * |
|
91 | + * The return url must not end with a slash. |
|
92 | + * This function should return null in case a principal did not have |
|
93 | + * a calendar home. |
|
94 | + * |
|
95 | + * @param string $principalUrl |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + public function getCalendarHomeForPrincipal($principalUrl) { |
|
99 | + |
|
100 | + // The default behavior for most sabre/dav servers is that there is a |
|
101 | + // principals root node, which contains users directly under it. |
|
102 | + // |
|
103 | + // This function assumes that there are two components in a principal |
|
104 | + // path. If there's more, we don't return a calendar home. This |
|
105 | + // excludes things like the calendar-proxy-read principal (which it |
|
106 | + // should). |
|
107 | + $parts = explode('/', trim($principalUrl, '/')); |
|
108 | + if (count($parts) !== 2) return; |
|
109 | + if ($parts[0] !== 'principals') return; |
|
110 | + |
|
111 | + return self::CALENDAR_ROOT . '/' . $parts[1]; |
|
112 | + |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns a list of features for the DAV: HTTP header. |
|
117 | + * |
|
118 | + * @return array |
|
119 | + */ |
|
120 | + public function getFeatures() { |
|
121 | + |
|
122 | + return ['calendar-access', 'calendar-proxy']; |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns a plugin name. |
|
128 | + * |
|
129 | + * Using this name other plugins will be able to access other plugins |
|
130 | + * using DAV\Server::getPlugin |
|
131 | + * |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function getPluginName() { |
|
135 | + |
|
136 | + return 'caldav'; |
|
137 | + |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Returns a list of reports this plugin supports. |
|
142 | + * |
|
143 | + * This will be used in the {DAV:}supported-report-set property. |
|
144 | + * Note that you still need to subscribe to the 'report' event to actually |
|
145 | + * implement them |
|
146 | + * |
|
147 | + * @param string $uri |
|
148 | + * @return array |
|
149 | + */ |
|
150 | + public function getSupportedReportSet($uri) { |
|
151 | + |
|
152 | + $node = $this->server->tree->getNodeForPath($uri); |
|
153 | + |
|
154 | + $reports = []; |
|
155 | + if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) { |
|
156 | + $reports[] = '{' . self::NS_CALDAV . '}calendar-multiget'; |
|
157 | + $reports[] = '{' . self::NS_CALDAV . '}calendar-query'; |
|
158 | + } |
|
159 | + if ($node instanceof ICalendar) { |
|
160 | + $reports[] = '{' . self::NS_CALDAV . '}free-busy-query'; |
|
161 | + } |
|
162 | + // iCal has a bug where it assumes that sync support is enabled, only |
|
163 | + // if we say we support it on the calendar-home, even though this is |
|
164 | + // not actually the case. |
|
165 | + if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) { |
|
166 | + $reports[] = '{DAV:}sync-collection'; |
|
167 | + } |
|
168 | + return $reports; |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Initializes the plugin |
|
174 | + * |
|
175 | + * @param DAV\Server $server |
|
176 | + * @return void |
|
177 | + */ |
|
178 | + public function initialize(DAV\Server $server) { |
|
179 | + |
|
180 | + $this->server = $server; |
|
181 | + |
|
182 | + $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
183 | + $server->on('report', [$this, 'report']); |
|
184 | + $server->on('propFind', [$this, 'propFind']); |
|
185 | + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
186 | + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
187 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
188 | + $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
189 | + |
|
190 | + $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
|
191 | + $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
192 | + |
|
193 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
194 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport'; |
|
195 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport'; |
|
196 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport'; |
|
197 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar'; |
|
198 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp'; |
|
199 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
200 | + |
|
201 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar'; |
|
202 | + |
|
203 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read'; |
|
204 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write'; |
|
205 | + |
|
206 | + array_push($server->protectedProperties, |
|
207 | + |
|
208 | + '{' . self::NS_CALDAV . '}supported-calendar-component-set', |
|
209 | + '{' . self::NS_CALDAV . '}supported-calendar-data', |
|
210 | + '{' . self::NS_CALDAV . '}max-resource-size', |
|
211 | + '{' . self::NS_CALDAV . '}min-date-time', |
|
212 | + '{' . self::NS_CALDAV . '}max-date-time', |
|
213 | + '{' . self::NS_CALDAV . '}max-instances', |
|
214 | + '{' . self::NS_CALDAV . '}max-attendees-per-instance', |
|
215 | + '{' . self::NS_CALDAV . '}calendar-home-set', |
|
216 | + '{' . self::NS_CALDAV . '}supported-collation-set', |
|
217 | + '{' . self::NS_CALDAV . '}calendar-data', |
|
218 | + |
|
219 | + // CalendarServer extensions |
|
220 | + '{' . self::NS_CALENDARSERVER . '}getctag', |
|
221 | + '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for', |
|
222 | + '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for' |
|
223 | + |
|
224 | + ); |
|
225 | + |
|
226 | + if ($aclPlugin = $server->getPlugin('acl')) { |
|
227 | + $aclPlugin->principalSearchPropertySet['{' . self::NS_CALDAV . '}calendar-user-address-set'] = 'Calendar address'; |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * This functions handles REPORT requests specific to CalDAV |
|
233 | + * |
|
234 | + * @param string $reportName |
|
235 | + * @param mixed $report |
|
236 | + * @return bool |
|
237 | + */ |
|
238 | + public function report($reportName, $report) { |
|
239 | + |
|
240 | + switch ($reportName) { |
|
241 | + case '{' . self::NS_CALDAV . '}calendar-multiget' : |
|
242 | + $this->server->transactionType = 'report-calendar-multiget'; |
|
243 | + $this->calendarMultiGetReport($report); |
|
244 | + return false; |
|
245 | + case '{' . self::NS_CALDAV . '}calendar-query' : |
|
246 | + $this->server->transactionType = 'report-calendar-query'; |
|
247 | + $this->calendarQueryReport($report); |
|
248 | + return false; |
|
249 | + case '{' . self::NS_CALDAV . '}free-busy-query' : |
|
250 | + $this->server->transactionType = 'report-free-busy-query'; |
|
251 | + $this->freeBusyQueryReport($report); |
|
252 | + return false; |
|
253 | + |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * This function handles the MKCALENDAR HTTP method, which creates |
|
261 | + * a new calendar. |
|
262 | + * |
|
263 | + * @param RequestInterface $request |
|
264 | + * @param ResponseInterface $response |
|
265 | + * @return bool |
|
266 | + */ |
|
267 | + public function httpMkCalendar(RequestInterface $request, ResponseInterface $response) { |
|
268 | + |
|
269 | + $body = $request->getBodyAsString(); |
|
270 | + $path = $request->getPath(); |
|
271 | + |
|
272 | + $properties = []; |
|
273 | + |
|
274 | + if ($body) { |
|
275 | + |
|
276 | + try { |
|
277 | + $mkcalendar = $this->server->xml->expect( |
|
278 | + '{urn:ietf:params:xml:ns:caldav}mkcalendar', |
|
279 | + $body |
|
280 | + ); |
|
281 | + } catch (\Sabre\Xml\ParseException $e) { |
|
282 | + throw new BadRequest($e->getMessage(), null, $e); |
|
283 | + } |
|
284 | + $properties = $mkcalendar->getProperties(); |
|
285 | + |
|
286 | + } |
|
287 | + |
|
288 | + // iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored |
|
289 | + // subscriptions. Before that it used MKCOL which was the correct way |
|
290 | + // to do this. |
|
291 | + // |
|
292 | + // If the body had a {DAV:}resourcetype, it means we stumbled upon this |
|
293 | + // request, and we simply use it instead of the pre-defined list. |
|
294 | + if (isset($properties['{DAV:}resourcetype'])) { |
|
295 | + $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
|
296 | + } else { |
|
297 | + $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
298 | + } |
|
299 | + |
|
300 | + $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
|
301 | + |
|
302 | + $this->server->httpResponse->setStatus(201); |
|
303 | + $this->server->httpResponse->setHeader('Content-Length', 0); |
|
304 | + |
|
305 | + // This breaks the method chain. |
|
306 | + return false; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * PropFind |
|
311 | + * |
|
312 | + * This method handler is invoked before any after properties for a |
|
313 | + * resource are fetched. This allows us to add in any CalDAV specific |
|
314 | + * properties. |
|
315 | + * |
|
316 | + * @param DAV\PropFind $propFind |
|
317 | + * @param DAV\INode $node |
|
318 | + * @return void |
|
319 | + */ |
|
320 | + public function propFind(DAV\PropFind $propFind, DAV\INode $node) { |
|
321 | + |
|
322 | + $ns = '{' . self::NS_CALDAV . '}'; |
|
323 | + |
|
324 | + if ($node instanceof ICalendarObjectContainer) { |
|
325 | + |
|
326 | + $propFind->handle($ns . 'max-resource-size', $this->maxResourceSize); |
|
327 | + $propFind->handle($ns . 'supported-calendar-data', function() { |
|
328 | + return new Xml\Property\SupportedCalendarData(); |
|
329 | + }); |
|
330 | + $propFind->handle($ns . 'supported-collation-set', function() { |
|
331 | + return new Xml\Property\SupportedCollationSet(); |
|
332 | + }); |
|
333 | + |
|
334 | + } |
|
335 | + |
|
336 | + if ($node instanceof DAVACL\IPrincipal) { |
|
337 | + |
|
338 | + $principalUrl = $node->getPrincipalUrl(); |
|
339 | + |
|
340 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
|
341 | + |
|
342 | + $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
|
343 | + if (is_null($calendarHomePath)) return null; |
|
344 | + return new Href($calendarHomePath . '/'); |
|
345 | + |
|
346 | + }); |
|
347 | + // The calendar-user-address-set property is basically mapped to |
|
348 | + // the {DAV:}alternate-URI-set property. |
|
349 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-user-address-set', function() use ($node) { |
|
350 | + $addresses = $node->getAlternateUriSet(); |
|
351 | + $addresses[] = $this->server->getBaseUri() . $node->getPrincipalUrl() . '/'; |
|
352 | + return new Href($addresses, false); |
|
353 | + }); |
|
354 | + // For some reason somebody thought it was a good idea to add |
|
355 | + // another one of these properties. We're supporting it too. |
|
356 | + $propFind->handle('{' . self::NS_CALENDARSERVER . '}email-address-set', function() use ($node) { |
|
357 | + $addresses = $node->getAlternateUriSet(); |
|
358 | + $emails = []; |
|
359 | + foreach ($addresses as $address) { |
|
360 | + if (substr($address, 0, 7) === 'mailto:') { |
|
361 | + $emails[] = substr($address, 7); |
|
362 | + } |
|
363 | + } |
|
364 | + return new Xml\Property\EmailAddressSet($emails); |
|
365 | + }); |
|
366 | + |
|
367 | + // These two properties are shortcuts for ical to easily find |
|
368 | + // other principals this principal has access to. |
|
369 | + $propRead = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for'; |
|
370 | + $propWrite = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for'; |
|
371 | + |
|
372 | + if ($propFind->getStatus($propRead) === 404 || $propFind->getStatus($propWrite) === 404) { |
|
373 | + |
|
374 | + $aclPlugin = $this->server->getPlugin('acl'); |
|
375 | + $membership = $aclPlugin->getPrincipalMembership($propFind->getPath()); |
|
376 | + $readList = []; |
|
377 | + $writeList = []; |
|
378 | + |
|
379 | + foreach ($membership as $group) { |
|
380 | + |
|
381 | + $groupNode = $this->server->tree->getNodeForPath($group); |
|
382 | + |
|
383 | + $listItem = Uri\split($group)[0] . '/'; |
|
384 | + |
|
385 | + // If the node is either ap proxy-read or proxy-write |
|
386 | + // group, we grab the parent principal and add it to the |
|
387 | + // list. |
|
388 | + if ($groupNode instanceof Principal\IProxyRead) { |
|
389 | + $readList[] = $listItem; |
|
390 | + } |
|
391 | + if ($groupNode instanceof Principal\IProxyWrite) { |
|
392 | + $writeList[] = $listItem; |
|
393 | + } |
|
394 | + |
|
395 | + } |
|
396 | + |
|
397 | + $propFind->set($propRead, new Href($readList)); |
|
398 | + $propFind->set($propWrite, new Href($writeList)); |
|
399 | + |
|
400 | + } |
|
401 | + |
|
402 | + } // instanceof IPrincipal |
|
403 | + |
|
404 | + if ($node instanceof ICalendarObject) { |
|
405 | + |
|
406 | + // The calendar-data property is not supposed to be a 'real' |
|
407 | + // property, but in large chunks of the spec it does act as such. |
|
408 | + // Therefore we simply expose it as a property. |
|
409 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
|
410 | + $val = $node->get(); |
|
411 | + if (is_resource($val)) |
|
412 | + $val = stream_get_contents($val); |
|
413 | + |
|
414 | + // Taking out \r to not screw up the xml output |
|
415 | + return str_replace("\r", "", $val); |
|
416 | + |
|
417 | + }); |
|
418 | + |
|
419 | + } |
|
420 | + |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * This function handles the calendar-multiget REPORT. |
|
425 | + * |
|
426 | + * This report is used by the client to fetch the content of a series |
|
427 | + * of urls. Effectively avoiding a lot of redundant requests. |
|
428 | + * |
|
429 | + * @param CalendarMultiGetReport $report |
|
430 | + * @return void |
|
431 | + */ |
|
432 | + public function calendarMultiGetReport($report) { |
|
433 | + |
|
434 | + $needsJson = $report->contentType === 'application/calendar+json'; |
|
435 | + |
|
436 | + $timeZones = []; |
|
437 | + $propertyList = []; |
|
438 | + |
|
439 | + $paths = array_map( |
|
440 | + [$this->server, 'calculateUri'], |
|
441 | + $report->hrefs |
|
442 | + ); |
|
443 | + |
|
444 | + foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) { |
|
445 | + |
|
446 | + if (($needsJson || $report->expand) && isset($objProps[200]['{' . self::NS_CALDAV . '}calendar-data'])) { |
|
447 | + $vObject = VObject\Reader::read($objProps[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
448 | + |
|
449 | + if ($report->expand) { |
|
450 | + // We're expanding, and for that we need to figure out the |
|
451 | + // calendar's timezone. |
|
452 | + list($calendarPath) = Uri\split($uri); |
|
453 | + if (!isset($timeZones[$calendarPath])) { |
|
454 | + // Checking the calendar-timezone property. |
|
455 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
456 | + $tzResult = $this->server->getProperties($calendarPath, [$tzProp]); |
|
457 | + if (isset($tzResult[$tzProp])) { |
|
458 | + // This property contains a VCALENDAR with a single |
|
459 | + // VTIMEZONE. |
|
460 | + $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
461 | + $timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
462 | + } else { |
|
463 | + // Defaulting to UTC. |
|
464 | + $timeZone = new DateTimeZone('UTC'); |
|
465 | + } |
|
466 | + $timeZones[$calendarPath] = $timeZone; |
|
467 | + } |
|
468 | + |
|
469 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]); |
|
470 | + } |
|
471 | + if ($needsJson) { |
|
472 | + $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
473 | + } else { |
|
474 | + $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
475 | + } |
|
476 | + // Destroy circular references so PHP will garbage collect the |
|
477 | + // object. |
|
478 | + $vObject->destroy(); |
|
479 | + } |
|
480 | + |
|
481 | + $propertyList[] = $objProps; |
|
482 | + |
|
483 | + } |
|
484 | + |
|
485 | + $prefer = $this->server->getHTTPPrefer(); |
|
486 | + |
|
487 | + $this->server->httpResponse->setStatus(207); |
|
488 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
489 | + $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
490 | + $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, $prefer['return'] === 'minimal')); |
|
491 | + |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * This function handles the calendar-query REPORT |
|
496 | + * |
|
497 | + * This report is used by clients to request calendar objects based on |
|
498 | + * complex conditions. |
|
499 | + * |
|
500 | + * @param Xml\Request\CalendarQueryReport $report |
|
501 | + * @return void |
|
502 | + */ |
|
503 | + public function calendarQueryReport($report) { |
|
504 | + |
|
505 | + $path = $this->server->getRequestUri(); |
|
506 | + |
|
507 | + $needsJson = $report->contentType === 'application/calendar+json'; |
|
508 | + |
|
509 | + $node = $this->server->tree->getNodeForPath($this->server->getRequestUri()); |
|
510 | + $depth = $this->server->getHTTPDepth(0); |
|
511 | + |
|
512 | + // The default result is an empty array |
|
513 | + $result = []; |
|
514 | + |
|
515 | + $calendarTimeZone = null; |
|
516 | + if ($report->expand) { |
|
517 | + // We're expanding, and for that we need to figure out the |
|
518 | + // calendar's timezone. |
|
519 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
520 | + $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
521 | + if (isset($tzResult[$tzProp])) { |
|
522 | + // This property contains a VCALENDAR with a single |
|
523 | + // VTIMEZONE. |
|
524 | + $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
525 | + $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
526 | + |
|
527 | + // Destroy circular references so PHP will garbage collect the |
|
528 | + // object. |
|
529 | + $vtimezoneObj->destroy(); |
|
530 | + } else { |
|
531 | + // Defaulting to UTC. |
|
532 | + $calendarTimeZone = new DateTimeZone('UTC'); |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + // The calendarobject was requested directly. In this case we handle |
|
537 | + // this locally. |
|
538 | + if ($depth == 0 && $node instanceof ICalendarObject) { |
|
539 | + |
|
540 | + $requestedCalendarData = true; |
|
541 | + $requestedProperties = $report->properties; |
|
542 | + |
|
543 | + if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) { |
|
544 | + |
|
545 | + // We always retrieve calendar-data, as we need it for filtering. |
|
546 | + $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data'; |
|
547 | + |
|
548 | + // If calendar-data wasn't explicitly requested, we need to remove |
|
549 | + // it after processing. |
|
550 | + $requestedCalendarData = false; |
|
551 | + } |
|
552 | + |
|
553 | + $properties = $this->server->getPropertiesForPath( |
|
554 | + $path, |
|
555 | + $requestedProperties, |
|
556 | + 0 |
|
557 | + ); |
|
558 | + |
|
559 | + // This array should have only 1 element, the first calendar |
|
560 | + // object. |
|
561 | + $properties = current($properties); |
|
562 | + |
|
563 | + // If there wasn't any calendar-data returned somehow, we ignore |
|
564 | + // this. |
|
565 | + if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) { |
|
566 | + |
|
567 | + $validator = new CalendarQueryValidator(); |
|
568 | + |
|
569 | + $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
570 | + if ($validator->validate($vObject, $report->filters)) { |
|
571 | + |
|
572 | + // If the client didn't require the calendar-data property, |
|
573 | + // we won't give it back. |
|
574 | + if (!$requestedCalendarData) { |
|
575 | + unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
576 | + } else { |
|
577 | + |
|
578 | + |
|
579 | + if ($report->expand) { |
|
580 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
581 | + } |
|
582 | + if ($needsJson) { |
|
583 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
584 | + } elseif ($report->expand) { |
|
585 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
586 | + } |
|
587 | + } |
|
588 | + |
|
589 | + $result = [$properties]; |
|
590 | + |
|
591 | + } |
|
592 | + // Destroy circular references so PHP will garbage collect the |
|
593 | + // object. |
|
594 | + $vObject->destroy(); |
|
595 | + |
|
596 | + } |
|
597 | + |
|
598 | + } |
|
599 | + |
|
600 | + if ($node instanceof ICalendarObjectContainer && $depth === 0) { |
|
601 | + |
|
602 | + if (strpos($this->server->httpRequest->getHeader('User-Agent'), 'MSFT-') === 0) { |
|
603 | + // Microsoft clients incorrectly supplied depth as 0, when it actually |
|
604 | + // should have set depth to 1. We're implementing a workaround here |
|
605 | + // to deal with this. |
|
606 | + // |
|
607 | + // This targets at least the following clients: |
|
608 | + // Windows 10 |
|
609 | + // Windows Phone 8, 10 |
|
610 | + $depth = 1; |
|
611 | + } else { |
|
612 | + throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1'); |
|
613 | + } |
|
614 | + |
|
615 | + } |
|
616 | + |
|
617 | + // If we're dealing with a calendar, the calendar itself is responsible |
|
618 | + // for the calendar-query. |
|
619 | + if ($node instanceof ICalendarObjectContainer && $depth == 1) { |
|
620 | + |
|
621 | + $nodePaths = $node->calendarQuery($report->filters); |
|
622 | + |
|
623 | + foreach ($nodePaths as $path) { |
|
624 | + |
|
625 | + list($properties) = |
|
626 | + $this->server->getPropertiesForPath($this->server->getRequestUri() . '/' . $path, $report->properties); |
|
627 | + |
|
628 | + if (($needsJson || $report->expand)) { |
|
629 | + $vObject = VObject\Reader::read($properties[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
630 | + |
|
631 | + if ($report->expand) { |
|
632 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
633 | + } |
|
634 | + |
|
635 | + if ($needsJson) { |
|
636 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
637 | + } else { |
|
638 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
639 | + } |
|
640 | + |
|
641 | + // Destroy circular references so PHP will garbage collect the |
|
642 | + // object. |
|
643 | + $vObject->destroy(); |
|
644 | + } |
|
645 | + $result[] = $properties; |
|
646 | + |
|
647 | + } |
|
648 | + |
|
649 | + } |
|
650 | + |
|
651 | + $prefer = $this->server->getHTTPPrefer(); |
|
652 | + |
|
653 | + $this->server->httpResponse->setStatus(207); |
|
654 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
655 | + $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
656 | + $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, $prefer['return'] === 'minimal')); |
|
657 | + |
|
658 | + } |
|
659 | + |
|
660 | + /** |
|
661 | + * This method is responsible for parsing the request and generating the |
|
662 | + * response for the CALDAV:free-busy-query REPORT. |
|
663 | + * |
|
664 | + * @param Xml\Request\FreeBusyQueryReport $report |
|
665 | + * @return void |
|
666 | + */ |
|
667 | + protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report) { |
|
668 | + |
|
669 | + $uri = $this->server->getRequestUri(); |
|
670 | + |
|
671 | + $acl = $this->server->getPlugin('acl'); |
|
672 | + if ($acl) { |
|
673 | + $acl->checkPrivileges($uri, '{' . self::NS_CALDAV . '}read-free-busy'); |
|
674 | + } |
|
675 | + |
|
676 | + $calendar = $this->server->tree->getNodeForPath($uri); |
|
677 | + if (!$calendar instanceof ICalendar) { |
|
678 | + throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars'); |
|
679 | + } |
|
680 | + |
|
681 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
682 | + |
|
683 | + // Figuring out the default timezone for the calendar, for floating |
|
684 | + // times. |
|
685 | + $calendarProps = $this->server->getProperties($uri, [$tzProp]); |
|
686 | + |
|
687 | + if (isset($calendarProps[$tzProp])) { |
|
688 | + $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]); |
|
689 | + $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
690 | + // Destroy circular references so PHP will garbage collect the object. |
|
691 | + $vtimezoneObj->destroy(); |
|
692 | + } else { |
|
693 | + $calendarTimeZone = new DateTimeZone('UTC'); |
|
694 | + } |
|
695 | + |
|
696 | + // Doing a calendar-query first, to make sure we get the most |
|
697 | + // performance. |
|
698 | + $urls = $calendar->calendarQuery([ |
|
699 | + 'name' => 'VCALENDAR', |
|
700 | + 'comp-filters' => [ |
|
701 | + [ |
|
702 | + 'name' => 'VEVENT', |
|
703 | + 'comp-filters' => [], |
|
704 | + 'prop-filters' => [], |
|
705 | + 'is-not-defined' => false, |
|
706 | + 'time-range' => [ |
|
707 | + 'start' => $report->start, |
|
708 | + 'end' => $report->end, |
|
709 | + ], |
|
710 | + ], |
|
711 | + ], |
|
712 | + 'prop-filters' => [], |
|
713 | + 'is-not-defined' => false, |
|
714 | + 'time-range' => null, |
|
715 | + ]); |
|
716 | + |
|
717 | + $objects = array_map(function($url) use ($calendar) { |
|
718 | + $obj = $calendar->getChild($url)->get(); |
|
719 | + return $obj; |
|
720 | + }, $urls); |
|
721 | + |
|
722 | + $generator = new VObject\FreeBusyGenerator(); |
|
723 | + $generator->setObjects($objects); |
|
724 | + $generator->setTimeRange($report->start, $report->end); |
|
725 | + $generator->setTimeZone($calendarTimeZone); |
|
726 | + $result = $generator->getResult(); |
|
727 | + $result = $result->serialize(); |
|
728 | + |
|
729 | + $this->server->httpResponse->setStatus(200); |
|
730 | + $this->server->httpResponse->setHeader('Content-Type', 'text/calendar'); |
|
731 | + $this->server->httpResponse->setHeader('Content-Length', strlen($result)); |
|
732 | + $this->server->httpResponse->setBody($result); |
|
733 | + |
|
734 | + } |
|
735 | + |
|
736 | + /** |
|
737 | + * This method is triggered before a file gets updated with new content. |
|
738 | + * |
|
739 | + * This plugin uses this method to ensure that CalDAV objects receive |
|
740 | + * valid calendar data. |
|
741 | + * |
|
742 | + * @param string $path |
|
743 | + * @param DAV\IFile $node |
|
744 | + * @param resource $data |
|
745 | + * @param bool $modified Should be set to true, if this event handler |
|
746 | + * changed &$data. |
|
747 | + * @return void |
|
748 | + */ |
|
749 | + public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
|
750 | + |
|
751 | + if (!$node instanceof ICalendarObject) |
|
752 | + return; |
|
753 | + |
|
754 | + // We're onyl interested in ICalendarObject nodes that are inside of a |
|
755 | + // real calendar. This is to avoid triggering validation and scheduling |
|
756 | + // for non-calendars (such as an inbox). |
|
757 | + list($parent) = Uri\split($path); |
|
758 | + $parentNode = $this->server->tree->getNodeForPath($parent); |
|
759 | + |
|
760 | + if (!$parentNode instanceof ICalendar) |
|
761 | + return; |
|
762 | + |
|
763 | + $this->validateICalendar( |
|
764 | + $data, |
|
765 | + $path, |
|
766 | + $modified, |
|
767 | + $this->server->httpRequest, |
|
768 | + $this->server->httpResponse, |
|
769 | + false |
|
770 | + ); |
|
771 | + |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * This method is triggered before a new file is created. |
|
776 | + * |
|
777 | + * This plugin uses this method to ensure that newly created calendar |
|
778 | + * objects contain valid calendar data. |
|
779 | + * |
|
780 | + * @param string $path |
|
781 | + * @param resource $data |
|
782 | + * @param DAV\ICollection $parentNode |
|
783 | + * @param bool $modified Should be set to true, if this event handler |
|
784 | + * changed &$data. |
|
785 | + * @return void |
|
786 | + */ |
|
787 | + public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
|
788 | + |
|
789 | + if (!$parentNode instanceof ICalendar) |
|
790 | + return; |
|
791 | + |
|
792 | + $this->validateICalendar( |
|
793 | + $data, |
|
794 | + $path, |
|
795 | + $modified, |
|
796 | + $this->server->httpRequest, |
|
797 | + $this->server->httpResponse, |
|
798 | + true |
|
799 | + ); |
|
800 | + |
|
801 | + } |
|
802 | + |
|
803 | + /** |
|
804 | + * Checks if the submitted iCalendar data is in fact, valid. |
|
805 | + * |
|
806 | + * An exception is thrown if it's not. |
|
807 | + * |
|
808 | + * @param resource|string $data |
|
809 | + * @param string $path |
|
810 | + * @param bool $modified Should be set to true, if this event handler |
|
811 | + * changed &$data. |
|
812 | + * @param RequestInterface $request The http request. |
|
813 | + * @param ResponseInterface $response The http response. |
|
814 | + * @param bool $isNew Is the item a new one, or an update. |
|
815 | + * @return void |
|
816 | + */ |
|
817 | + protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew) { |
|
818 | + |
|
819 | + // If it's a stream, we convert it to a string first. |
|
820 | + if (is_resource($data)) { |
|
821 | + $data = stream_get_contents($data); |
|
822 | + } |
|
823 | + |
|
824 | + $before = md5($data); |
|
825 | + // Converting the data to unicode, if needed. |
|
826 | + $data = DAV\StringUtil::ensureUTF8($data); |
|
827 | + |
|
828 | + if ($before !== md5($data)) $modified = true; |
|
829 | + |
|
830 | + try { |
|
831 | + |
|
832 | + // If the data starts with a [, we can reasonably assume we're dealing |
|
833 | + // with a jCal object. |
|
834 | + if (substr($data, 0, 1) === '[') { |
|
835 | + $vobj = VObject\Reader::readJson($data); |
|
836 | + |
|
837 | + // Converting $data back to iCalendar, as that's what we |
|
838 | + // technically support everywhere. |
|
839 | + $data = $vobj->serialize(); |
|
840 | + $modified = true; |
|
841 | + } else { |
|
842 | + $vobj = VObject\Reader::read($data); |
|
843 | + } |
|
844 | + |
|
845 | + } catch (VObject\ParseException $e) { |
|
846 | + |
|
847 | + throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: ' . $e->getMessage()); |
|
848 | + |
|
849 | + } |
|
850 | + |
|
851 | + if ($vobj->name !== 'VCALENDAR') { |
|
852 | + throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.'); |
|
853 | + } |
|
854 | + |
|
855 | + $sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
856 | + |
|
857 | + // Get the Supported Components for the target calendar |
|
858 | + list($parentPath) = Uri\split($path); |
|
859 | + $calendarProperties = $this->server->getProperties($parentPath, [$sCCS]); |
|
860 | + |
|
861 | + if (isset($calendarProperties[$sCCS])) { |
|
862 | + $supportedComponents = $calendarProperties[$sCCS]->getValue(); |
|
863 | + } else { |
|
864 | + $supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT']; |
|
865 | + } |
|
866 | + |
|
867 | + $foundType = null; |
|
868 | + $foundUID = null; |
|
869 | + foreach ($vobj->getComponents() as $component) { |
|
870 | + switch ($component->name) { |
|
871 | + case 'VTIMEZONE' : |
|
872 | + continue 2; |
|
873 | + case 'VEVENT' : |
|
874 | + case 'VTODO' : |
|
875 | + case 'VJOURNAL' : |
|
876 | + if (is_null($foundType)) { |
|
877 | + $foundType = $component->name; |
|
878 | + if (!in_array($foundType, $supportedComponents)) { |
|
879 | + throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType); |
|
880 | + } |
|
881 | + if (!isset($component->UID)) { |
|
882 | + throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
|
883 | + } |
|
884 | + $foundUID = (string)$component->UID; |
|
885 | + } else { |
|
886 | + if ($foundType !== $component->name) { |
|
887 | + throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
|
888 | + } |
|
889 | + if ($foundUID !== (string)$component->UID) { |
|
890 | + throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
|
891 | + } |
|
892 | + } |
|
893 | + break; |
|
894 | + default : |
|
895 | + throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here'); |
|
896 | + |
|
897 | + } |
|
898 | + } |
|
899 | + if (!$foundType) |
|
900 | + throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
901 | + |
|
902 | + // We use an extra variable to allow event handles to tell us wether |
|
903 | + // the object was modified or not. |
|
904 | + // |
|
905 | + // This helps us determine if we need to re-serialize the object. |
|
906 | + $subModified = false; |
|
907 | + |
|
908 | + $this->server->emit( |
|
909 | + 'calendarObjectChange', |
|
910 | + [ |
|
911 | + $request, |
|
912 | + $response, |
|
913 | + $vobj, |
|
914 | + $parentPath, |
|
915 | + &$subModified, |
|
916 | + $isNew |
|
917 | + ] |
|
918 | + ); |
|
919 | + |
|
920 | + if ($subModified) { |
|
921 | + // An event handler told us that it modified the object. |
|
922 | + $data = $vobj->serialize(); |
|
923 | + |
|
924 | + // Using md5 to figure out if there was an *actual* change. |
|
925 | + if (!$modified && $before !== md5($data)) { |
|
926 | + $modified = true; |
|
927 | + } |
|
928 | + |
|
929 | + } |
|
930 | + |
|
931 | + // Destroy circular references so PHP will garbage collect the object. |
|
932 | + $vobj->destroy(); |
|
933 | + |
|
934 | + } |
|
935 | + |
|
936 | + |
|
937 | + /** |
|
938 | + * This method is used to generate HTML output for the |
|
939 | + * DAV\Browser\Plugin. This allows us to generate an interface users |
|
940 | + * can use to create new calendars. |
|
941 | + * |
|
942 | + * @param DAV\INode $node |
|
943 | + * @param string $output |
|
944 | + * @return bool |
|
945 | + */ |
|
946 | + public function htmlActionsPanel(DAV\INode $node, &$output) { |
|
947 | + |
|
948 | + if (!$node instanceof CalendarHome) |
|
949 | + return; |
|
950 | + |
|
951 | + $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
952 | 952 | <h3>Create new calendar</h3> |
953 | 953 | <input type="hidden" name="sabreAction" value="mkcol" /> |
954 | 954 | <input type="hidden" name="resourceType" value="{DAV:}collection,{' . self::NS_CALDAV . '}calendar" /> |
@@ -958,68 +958,68 @@ discard block |
||
958 | 958 | </form> |
959 | 959 | </td></tr>'; |
960 | 960 | |
961 | - return false; |
|
962 | - |
|
963 | - } |
|
964 | - |
|
965 | - /** |
|
966 | - * This event is triggered after GET requests. |
|
967 | - * |
|
968 | - * This is used to transform data into jCal, if this was requested. |
|
969 | - * |
|
970 | - * @param RequestInterface $request |
|
971 | - * @param ResponseInterface $response |
|
972 | - * @return void |
|
973 | - */ |
|
974 | - public function httpAfterGet(RequestInterface $request, ResponseInterface $response) { |
|
975 | - |
|
976 | - if (strpos($response->getHeader('Content-Type'), 'text/calendar') === false) { |
|
977 | - return; |
|
978 | - } |
|
979 | - |
|
980 | - $result = HTTP\Util::negotiate( |
|
981 | - $request->getHeader('Accept'), |
|
982 | - ['text/calendar', 'application/calendar+json'] |
|
983 | - ); |
|
984 | - |
|
985 | - if ($result !== 'application/calendar+json') { |
|
986 | - // Do nothing |
|
987 | - return; |
|
988 | - } |
|
989 | - |
|
990 | - // Transforming. |
|
991 | - $vobj = VObject\Reader::read($response->getBody()); |
|
992 | - |
|
993 | - $jsonBody = json_encode($vobj->jsonSerialize()); |
|
994 | - $response->setBody($jsonBody); |
|
995 | - |
|
996 | - // Destroy circular references so PHP will garbage collect the object. |
|
997 | - $vobj->destroy(); |
|
998 | - |
|
999 | - $response->setHeader('Content-Type', 'application/calendar+json'); |
|
1000 | - $response->setHeader('Content-Length', strlen($jsonBody)); |
|
1001 | - |
|
1002 | - } |
|
1003 | - |
|
1004 | - /** |
|
1005 | - * Returns a bunch of meta-data about the plugin. |
|
1006 | - * |
|
1007 | - * Providing this information is optional, and is mainly displayed by the |
|
1008 | - * Browser plugin. |
|
1009 | - * |
|
1010 | - * The description key in the returned array may contain html and will not |
|
1011 | - * be sanitized. |
|
1012 | - * |
|
1013 | - * @return array |
|
1014 | - */ |
|
1015 | - public function getPluginInfo() { |
|
1016 | - |
|
1017 | - return [ |
|
1018 | - 'name' => $this->getPluginName(), |
|
1019 | - 'description' => 'Adds support for CalDAV (rfc4791)', |
|
1020 | - 'link' => 'http://sabre.io/dav/caldav/', |
|
1021 | - ]; |
|
1022 | - |
|
1023 | - } |
|
961 | + return false; |
|
962 | + |
|
963 | + } |
|
964 | + |
|
965 | + /** |
|
966 | + * This event is triggered after GET requests. |
|
967 | + * |
|
968 | + * This is used to transform data into jCal, if this was requested. |
|
969 | + * |
|
970 | + * @param RequestInterface $request |
|
971 | + * @param ResponseInterface $response |
|
972 | + * @return void |
|
973 | + */ |
|
974 | + public function httpAfterGet(RequestInterface $request, ResponseInterface $response) { |
|
975 | + |
|
976 | + if (strpos($response->getHeader('Content-Type'), 'text/calendar') === false) { |
|
977 | + return; |
|
978 | + } |
|
979 | + |
|
980 | + $result = HTTP\Util::negotiate( |
|
981 | + $request->getHeader('Accept'), |
|
982 | + ['text/calendar', 'application/calendar+json'] |
|
983 | + ); |
|
984 | + |
|
985 | + if ($result !== 'application/calendar+json') { |
|
986 | + // Do nothing |
|
987 | + return; |
|
988 | + } |
|
989 | + |
|
990 | + // Transforming. |
|
991 | + $vobj = VObject\Reader::read($response->getBody()); |
|
992 | + |
|
993 | + $jsonBody = json_encode($vobj->jsonSerialize()); |
|
994 | + $response->setBody($jsonBody); |
|
995 | + |
|
996 | + // Destroy circular references so PHP will garbage collect the object. |
|
997 | + $vobj->destroy(); |
|
998 | + |
|
999 | + $response->setHeader('Content-Type', 'application/calendar+json'); |
|
1000 | + $response->setHeader('Content-Length', strlen($jsonBody)); |
|
1001 | + |
|
1002 | + } |
|
1003 | + |
|
1004 | + /** |
|
1005 | + * Returns a bunch of meta-data about the plugin. |
|
1006 | + * |
|
1007 | + * Providing this information is optional, and is mainly displayed by the |
|
1008 | + * Browser plugin. |
|
1009 | + * |
|
1010 | + * The description key in the returned array may contain html and will not |
|
1011 | + * be sanitized. |
|
1012 | + * |
|
1013 | + * @return array |
|
1014 | + */ |
|
1015 | + public function getPluginInfo() { |
|
1016 | + |
|
1017 | + return [ |
|
1018 | + 'name' => $this->getPluginName(), |
|
1019 | + 'description' => 'Adds support for CalDAV (rfc4791)', |
|
1020 | + 'link' => 'http://sabre.io/dav/caldav/', |
|
1021 | + ]; |
|
1022 | + |
|
1023 | + } |
|
1024 | 1024 | |
1025 | 1025 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | /** |
116 | 116 | * Returns a list of features for the DAV: HTTP header. |
117 | 117 | * |
118 | - * @return array |
|
118 | + * @return string[] |
|
119 | 119 | */ |
120 | 120 | public function getFeatures() { |
121 | 121 | |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | * |
234 | 234 | * @param string $reportName |
235 | 235 | * @param mixed $report |
236 | - * @return bool |
|
236 | + * @return false|null |
|
237 | 237 | */ |
238 | 238 | public function report($reportName, $report) { |
239 | 239 | |
@@ -805,7 +805,7 @@ discard block |
||
805 | 805 | * |
806 | 806 | * An exception is thrown if it's not. |
807 | 807 | * |
808 | - * @param resource|string $data |
|
808 | + * @param resource $data |
|
809 | 809 | * @param string $path |
810 | 810 | * @param bool $modified Should be set to true, if this event handler |
811 | 811 | * changed &$data. |
@@ -941,7 +941,7 @@ discard block |
||
941 | 941 | * |
942 | 942 | * @param DAV\INode $node |
943 | 943 | * @param string $output |
944 | - * @return bool |
|
944 | + * @return null|false |
|
945 | 945 | */ |
946 | 946 | public function htmlActionsPanel(DAV\INode $node, &$output) { |
947 | 947 |
@@ -105,8 +105,12 @@ discard block |
||
105 | 105 | // excludes things like the calendar-proxy-read principal (which it |
106 | 106 | // should). |
107 | 107 | $parts = explode('/', trim($principalUrl, '/')); |
108 | - if (count($parts) !== 2) return; |
|
109 | - if ($parts[0] !== 'principals') return; |
|
108 | + if (count($parts) !== 2) { |
|
109 | + return; |
|
110 | + } |
|
111 | + if ($parts[0] !== 'principals') { |
|
112 | + return; |
|
113 | + } |
|
110 | 114 | |
111 | 115 | return self::CALENDAR_ROOT . '/' . $parts[1]; |
112 | 116 | |
@@ -340,7 +344,9 @@ discard block |
||
340 | 344 | $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
341 | 345 | |
342 | 346 | $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
343 | - if (is_null($calendarHomePath)) return null; |
|
347 | + if (is_null($calendarHomePath)) { |
|
348 | + return null; |
|
349 | + } |
|
344 | 350 | return new Href($calendarHomePath . '/'); |
345 | 351 | |
346 | 352 | }); |
@@ -408,8 +414,9 @@ discard block |
||
408 | 414 | // Therefore we simply expose it as a property. |
409 | 415 | $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
410 | 416 | $val = $node->get(); |
411 | - if (is_resource($val)) |
|
412 | - $val = stream_get_contents($val); |
|
417 | + if (is_resource($val)) { |
|
418 | + $val = stream_get_contents($val); |
|
419 | + } |
|
413 | 420 | |
414 | 421 | // Taking out \r to not screw up the xml output |
415 | 422 | return str_replace("\r", "", $val); |
@@ -748,8 +755,9 @@ discard block |
||
748 | 755 | */ |
749 | 756 | public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
750 | 757 | |
751 | - if (!$node instanceof ICalendarObject) |
|
752 | - return; |
|
758 | + if (!$node instanceof ICalendarObject) { |
|
759 | + return; |
|
760 | + } |
|
753 | 761 | |
754 | 762 | // We're onyl interested in ICalendarObject nodes that are inside of a |
755 | 763 | // real calendar. This is to avoid triggering validation and scheduling |
@@ -757,8 +765,9 @@ discard block |
||
757 | 765 | list($parent) = Uri\split($path); |
758 | 766 | $parentNode = $this->server->tree->getNodeForPath($parent); |
759 | 767 | |
760 | - if (!$parentNode instanceof ICalendar) |
|
761 | - return; |
|
768 | + if (!$parentNode instanceof ICalendar) { |
|
769 | + return; |
|
770 | + } |
|
762 | 771 | |
763 | 772 | $this->validateICalendar( |
764 | 773 | $data, |
@@ -786,8 +795,9 @@ discard block |
||
786 | 795 | */ |
787 | 796 | public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
788 | 797 | |
789 | - if (!$parentNode instanceof ICalendar) |
|
790 | - return; |
|
798 | + if (!$parentNode instanceof ICalendar) { |
|
799 | + return; |
|
800 | + } |
|
791 | 801 | |
792 | 802 | $this->validateICalendar( |
793 | 803 | $data, |
@@ -825,7 +835,9 @@ discard block |
||
825 | 835 | // Converting the data to unicode, if needed. |
826 | 836 | $data = DAV\StringUtil::ensureUTF8($data); |
827 | 837 | |
828 | - if ($before !== md5($data)) $modified = true; |
|
838 | + if ($before !== md5($data)) { |
|
839 | + $modified = true; |
|
840 | + } |
|
829 | 841 | |
830 | 842 | try { |
831 | 843 | |
@@ -896,8 +908,9 @@ discard block |
||
896 | 908 | |
897 | 909 | } |
898 | 910 | } |
899 | - if (!$foundType) |
|
900 | - throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
911 | + if (!$foundType) { |
|
912 | + throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
913 | + } |
|
901 | 914 | |
902 | 915 | // We use an extra variable to allow event handles to tell us wether |
903 | 916 | // the object was modified or not. |
@@ -945,8 +958,9 @@ discard block |
||
945 | 958 | */ |
946 | 959 | public function htmlActionsPanel(DAV\INode $node, &$output) { |
947 | 960 | |
948 | - if (!$node instanceof CalendarHome) |
|
949 | - return; |
|
961 | + if (!$node instanceof CalendarHome) { |
|
962 | + return; |
|
963 | + } |
|
950 | 964 | |
951 | 965 | $output .= '<tr><td colspan="2"><form method="post" action=""> |
952 | 966 | <h3>Create new calendar</h3> |
@@ -2,8 +2,8 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV\Principal; |
4 | 4 | |
5 | -use Sabre\DAVACL; |
|
6 | 5 | use Sabre\DAV; |
6 | +use Sabre\DAVACL; |
|
7 | 7 | |
8 | 8 | /** |
9 | 9 | * ProxyRead principal |
@@ -18,164 +18,164 @@ |
||
18 | 18 | */ |
19 | 19 | class ProxyRead implements IProxyRead { |
20 | 20 | |
21 | - /** |
|
22 | - * Principal information from the parent principal. |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $principalInfo; |
|
27 | - |
|
28 | - /** |
|
29 | - * Principal backend |
|
30 | - * |
|
31 | - * @var DAVACL\PrincipalBackend\BackendInterface |
|
32 | - */ |
|
33 | - protected $principalBackend; |
|
34 | - |
|
35 | - /** |
|
36 | - * Creates the object. |
|
37 | - * |
|
38 | - * Note that you MUST supply the parent principal information. |
|
39 | - * |
|
40 | - * @param DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
41 | - * @param array $principalInfo |
|
42 | - */ |
|
43 | - public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo) { |
|
44 | - |
|
45 | - $this->principalInfo = $principalInfo; |
|
46 | - $this->principalBackend = $principalBackend; |
|
47 | - |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Returns this principals name. |
|
52 | - * |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getName() { |
|
56 | - |
|
57 | - return 'calendar-proxy-read'; |
|
58 | - |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns the last modification time |
|
63 | - * |
|
64 | - * @return null |
|
65 | - */ |
|
66 | - public function getLastModified() { |
|
67 | - |
|
68 | - return null; |
|
69 | - |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Deletes the current node |
|
74 | - * |
|
75 | - * @throws DAV\Exception\Forbidden |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function delete() { |
|
79 | - |
|
80 | - throw new DAV\Exception\Forbidden('Permission denied to delete node'); |
|
81 | - |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Renames the node |
|
86 | - * |
|
87 | - * @throws DAV\Exception\Forbidden |
|
88 | - * @param string $name The new name |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function setName($name) { |
|
92 | - |
|
93 | - throw new DAV\Exception\Forbidden('Permission denied to rename file'); |
|
94 | - |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns a list of alternative urls for a principal |
|
100 | - * |
|
101 | - * This can for example be an email address, or ldap url. |
|
102 | - * |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - public function getAlternateUriSet() { |
|
106 | - |
|
107 | - return []; |
|
108 | - |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns the full principal url |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getPrincipalUrl() { |
|
117 | - |
|
118 | - return $this->principalInfo['uri'] . '/' . $this->getName(); |
|
119 | - |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the list of group members |
|
124 | - * |
|
125 | - * If this principal is a group, this function should return |
|
126 | - * all member principal uri's for the group. |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function getGroupMemberSet() { |
|
131 | - |
|
132 | - return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl()); |
|
133 | - |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Returns the list of groups this principal is member of |
|
138 | - * |
|
139 | - * If this principal is a member of a (list of) groups, this function |
|
140 | - * should return a list of principal uri's for it's members. |
|
141 | - * |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function getGroupMembership() { |
|
145 | - |
|
146 | - return $this->principalBackend->getGroupMembership($this->getPrincipalUrl()); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Sets a list of group members |
|
152 | - * |
|
153 | - * If this principal is a group, this method sets all the group members. |
|
154 | - * The list of members is always overwritten, never appended to. |
|
155 | - * |
|
156 | - * This method should throw an exception if the members could not be set. |
|
157 | - * |
|
158 | - * @param array $principals |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - public function setGroupMemberSet(array $principals) { |
|
162 | - |
|
163 | - $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals); |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Returns the displayname |
|
169 | - * |
|
170 | - * This should be a human readable name for the principal. |
|
171 | - * If none is available, return the nodename. |
|
172 | - * |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - public function getDisplayName() { |
|
176 | - |
|
177 | - return $this->getName(); |
|
178 | - |
|
179 | - } |
|
21 | + /** |
|
22 | + * Principal information from the parent principal. |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $principalInfo; |
|
27 | + |
|
28 | + /** |
|
29 | + * Principal backend |
|
30 | + * |
|
31 | + * @var DAVACL\PrincipalBackend\BackendInterface |
|
32 | + */ |
|
33 | + protected $principalBackend; |
|
34 | + |
|
35 | + /** |
|
36 | + * Creates the object. |
|
37 | + * |
|
38 | + * Note that you MUST supply the parent principal information. |
|
39 | + * |
|
40 | + * @param DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
41 | + * @param array $principalInfo |
|
42 | + */ |
|
43 | + public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo) { |
|
44 | + |
|
45 | + $this->principalInfo = $principalInfo; |
|
46 | + $this->principalBackend = $principalBackend; |
|
47 | + |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Returns this principals name. |
|
52 | + * |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getName() { |
|
56 | + |
|
57 | + return 'calendar-proxy-read'; |
|
58 | + |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns the last modification time |
|
63 | + * |
|
64 | + * @return null |
|
65 | + */ |
|
66 | + public function getLastModified() { |
|
67 | + |
|
68 | + return null; |
|
69 | + |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Deletes the current node |
|
74 | + * |
|
75 | + * @throws DAV\Exception\Forbidden |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function delete() { |
|
79 | + |
|
80 | + throw new DAV\Exception\Forbidden('Permission denied to delete node'); |
|
81 | + |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Renames the node |
|
86 | + * |
|
87 | + * @throws DAV\Exception\Forbidden |
|
88 | + * @param string $name The new name |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function setName($name) { |
|
92 | + |
|
93 | + throw new DAV\Exception\Forbidden('Permission denied to rename file'); |
|
94 | + |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns a list of alternative urls for a principal |
|
100 | + * |
|
101 | + * This can for example be an email address, or ldap url. |
|
102 | + * |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + public function getAlternateUriSet() { |
|
106 | + |
|
107 | + return []; |
|
108 | + |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns the full principal url |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getPrincipalUrl() { |
|
117 | + |
|
118 | + return $this->principalInfo['uri'] . '/' . $this->getName(); |
|
119 | + |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the list of group members |
|
124 | + * |
|
125 | + * If this principal is a group, this function should return |
|
126 | + * all member principal uri's for the group. |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function getGroupMemberSet() { |
|
131 | + |
|
132 | + return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl()); |
|
133 | + |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Returns the list of groups this principal is member of |
|
138 | + * |
|
139 | + * If this principal is a member of a (list of) groups, this function |
|
140 | + * should return a list of principal uri's for it's members. |
|
141 | + * |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function getGroupMembership() { |
|
145 | + |
|
146 | + return $this->principalBackend->getGroupMembership($this->getPrincipalUrl()); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Sets a list of group members |
|
152 | + * |
|
153 | + * If this principal is a group, this method sets all the group members. |
|
154 | + * The list of members is always overwritten, never appended to. |
|
155 | + * |
|
156 | + * This method should throw an exception if the members could not be set. |
|
157 | + * |
|
158 | + * @param array $principals |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + public function setGroupMemberSet(array $principals) { |
|
162 | + |
|
163 | + $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals); |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Returns the displayname |
|
169 | + * |
|
170 | + * This should be a human readable name for the principal. |
|
171 | + * If none is available, return the nodename. |
|
172 | + * |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + public function getDisplayName() { |
|
176 | + |
|
177 | + return $this->getName(); |
|
178 | + |
|
179 | + } |
|
180 | 180 | |
181 | 181 | } |
@@ -2,8 +2,8 @@ |
||
2 | 2 | |
3 | 3 | namespace Sabre\CalDAV\Principal; |
4 | 4 | |
5 | -use Sabre\DAVACL; |
|
6 | 5 | use Sabre\DAV; |
6 | +use Sabre\DAVACL; |
|
7 | 7 | |
8 | 8 | /** |
9 | 9 | * ProxyWrite principal |
@@ -18,164 +18,164 @@ |
||
18 | 18 | */ |
19 | 19 | class ProxyWrite implements IProxyWrite { |
20 | 20 | |
21 | - /** |
|
22 | - * Parent principal information |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $principalInfo; |
|
27 | - |
|
28 | - /** |
|
29 | - * Principal Backend |
|
30 | - * |
|
31 | - * @var DAVACL\PrincipalBackend\BackendInterface |
|
32 | - */ |
|
33 | - protected $principalBackend; |
|
34 | - |
|
35 | - /** |
|
36 | - * Creates the object |
|
37 | - * |
|
38 | - * Note that you MUST supply the parent principal information. |
|
39 | - * |
|
40 | - * @param DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
41 | - * @param array $principalInfo |
|
42 | - */ |
|
43 | - public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo) { |
|
44 | - |
|
45 | - $this->principalInfo = $principalInfo; |
|
46 | - $this->principalBackend = $principalBackend; |
|
47 | - |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Returns this principals name. |
|
52 | - * |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getName() { |
|
56 | - |
|
57 | - return 'calendar-proxy-write'; |
|
58 | - |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Returns the last modification time |
|
63 | - * |
|
64 | - * @return null |
|
65 | - */ |
|
66 | - public function getLastModified() { |
|
67 | - |
|
68 | - return null; |
|
69 | - |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Deletes the current node |
|
74 | - * |
|
75 | - * @throws DAV\Exception\Forbidden |
|
76 | - * @return void |
|
77 | - */ |
|
78 | - public function delete() { |
|
79 | - |
|
80 | - throw new DAV\Exception\Forbidden('Permission denied to delete node'); |
|
81 | - |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Renames the node |
|
86 | - * |
|
87 | - * @throws DAV\Exception\Forbidden |
|
88 | - * @param string $name The new name |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function setName($name) { |
|
92 | - |
|
93 | - throw new DAV\Exception\Forbidden('Permission denied to rename file'); |
|
94 | - |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns a list of alternative urls for a principal |
|
100 | - * |
|
101 | - * This can for example be an email address, or ldap url. |
|
102 | - * |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - public function getAlternateUriSet() { |
|
106 | - |
|
107 | - return []; |
|
108 | - |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns the full principal url |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getPrincipalUrl() { |
|
117 | - |
|
118 | - return $this->principalInfo['uri'] . '/' . $this->getName(); |
|
119 | - |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the list of group members |
|
124 | - * |
|
125 | - * If this principal is a group, this function should return |
|
126 | - * all member principal uri's for the group. |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function getGroupMemberSet() { |
|
131 | - |
|
132 | - return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl()); |
|
133 | - |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Returns the list of groups this principal is member of |
|
138 | - * |
|
139 | - * If this principal is a member of a (list of) groups, this function |
|
140 | - * should return a list of principal uri's for it's members. |
|
141 | - * |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function getGroupMembership() { |
|
145 | - |
|
146 | - return $this->principalBackend->getGroupMembership($this->getPrincipalUrl()); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Sets a list of group members |
|
152 | - * |
|
153 | - * If this principal is a group, this method sets all the group members. |
|
154 | - * The list of members is always overwritten, never appended to. |
|
155 | - * |
|
156 | - * This method should throw an exception if the members could not be set. |
|
157 | - * |
|
158 | - * @param array $principals |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - public function setGroupMemberSet(array $principals) { |
|
162 | - |
|
163 | - $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals); |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Returns the displayname |
|
169 | - * |
|
170 | - * This should be a human readable name for the principal. |
|
171 | - * If none is available, return the nodename. |
|
172 | - * |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - public function getDisplayName() { |
|
176 | - |
|
177 | - return $this->getName(); |
|
178 | - |
|
179 | - } |
|
21 | + /** |
|
22 | + * Parent principal information |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $principalInfo; |
|
27 | + |
|
28 | + /** |
|
29 | + * Principal Backend |
|
30 | + * |
|
31 | + * @var DAVACL\PrincipalBackend\BackendInterface |
|
32 | + */ |
|
33 | + protected $principalBackend; |
|
34 | + |
|
35 | + /** |
|
36 | + * Creates the object |
|
37 | + * |
|
38 | + * Note that you MUST supply the parent principal information. |
|
39 | + * |
|
40 | + * @param DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
41 | + * @param array $principalInfo |
|
42 | + */ |
|
43 | + public function __construct(DAVACL\PrincipalBackend\BackendInterface $principalBackend, array $principalInfo) { |
|
44 | + |
|
45 | + $this->principalInfo = $principalInfo; |
|
46 | + $this->principalBackend = $principalBackend; |
|
47 | + |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Returns this principals name. |
|
52 | + * |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getName() { |
|
56 | + |
|
57 | + return 'calendar-proxy-write'; |
|
58 | + |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Returns the last modification time |
|
63 | + * |
|
64 | + * @return null |
|
65 | + */ |
|
66 | + public function getLastModified() { |
|
67 | + |
|
68 | + return null; |
|
69 | + |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Deletes the current node |
|
74 | + * |
|
75 | + * @throws DAV\Exception\Forbidden |
|
76 | + * @return void |
|
77 | + */ |
|
78 | + public function delete() { |
|
79 | + |
|
80 | + throw new DAV\Exception\Forbidden('Permission denied to delete node'); |
|
81 | + |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Renames the node |
|
86 | + * |
|
87 | + * @throws DAV\Exception\Forbidden |
|
88 | + * @param string $name The new name |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function setName($name) { |
|
92 | + |
|
93 | + throw new DAV\Exception\Forbidden('Permission denied to rename file'); |
|
94 | + |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns a list of alternative urls for a principal |
|
100 | + * |
|
101 | + * This can for example be an email address, or ldap url. |
|
102 | + * |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + public function getAlternateUriSet() { |
|
106 | + |
|
107 | + return []; |
|
108 | + |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns the full principal url |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getPrincipalUrl() { |
|
117 | + |
|
118 | + return $this->principalInfo['uri'] . '/' . $this->getName(); |
|
119 | + |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the list of group members |
|
124 | + * |
|
125 | + * If this principal is a group, this function should return |
|
126 | + * all member principal uri's for the group. |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function getGroupMemberSet() { |
|
131 | + |
|
132 | + return $this->principalBackend->getGroupMemberSet($this->getPrincipalUrl()); |
|
133 | + |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Returns the list of groups this principal is member of |
|
138 | + * |
|
139 | + * If this principal is a member of a (list of) groups, this function |
|
140 | + * should return a list of principal uri's for it's members. |
|
141 | + * |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function getGroupMembership() { |
|
145 | + |
|
146 | + return $this->principalBackend->getGroupMembership($this->getPrincipalUrl()); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Sets a list of group members |
|
152 | + * |
|
153 | + * If this principal is a group, this method sets all the group members. |
|
154 | + * The list of members is always overwritten, never appended to. |
|
155 | + * |
|
156 | + * This method should throw an exception if the members could not be set. |
|
157 | + * |
|
158 | + * @param array $principals |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + public function setGroupMemberSet(array $principals) { |
|
162 | + |
|
163 | + $this->principalBackend->setGroupMemberSet($this->getPrincipalUrl(), $principals); |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Returns the displayname |
|
169 | + * |
|
170 | + * This should be a human readable name for the principal. |
|
171 | + * If none is available, return the nodename. |
|
172 | + * |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + public function getDisplayName() { |
|
176 | + |
|
177 | + return $this->getName(); |
|
178 | + |
|
179 | + } |
|
180 | 180 | |
181 | 181 | } |
@@ -155,7 +155,7 @@ |
||
155 | 155 | * @param string $to Recipient email address |
156 | 156 | * @param string $subject Subject of the email |
157 | 157 | * @param string $body iCalendar body |
158 | - * @param array $headers List of headers |
|
158 | + * @param string[] $headers List of headers |
|
159 | 159 | * @return void |
160 | 160 | */ |
161 | 161 | protected function mail($to, $subject, $body, array $headers) { |
@@ -99,11 +99,13 @@ |
||
99 | 99 | |
100 | 100 | $summary = $iTipMessage->message->VEVENT->SUMMARY; |
101 | 101 | |
102 | - if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') |
|
103 | - return; |
|
102 | + if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') { |
|
103 | + return; |
|
104 | + } |
|
104 | 105 | |
105 | - if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') |
|
106 | - return; |
|
106 | + if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') { |
|
107 | + return; |
|
108 | + } |
|
107 | 109 | |
108 | 110 | $sender = substr($iTipMessage->sender, 7); |
109 | 111 | $recipient = substr($iTipMessage->recipient, 7); |
@@ -21,35 +21,35 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class IMipPlugin extends DAV\ServerPlugin { |
23 | 23 | |
24 | - /** |
|
25 | - * Email address used in From: header. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $senderEmail; |
|
30 | - |
|
31 | - /** |
|
32 | - * ITipMessage |
|
33 | - * |
|
34 | - * @var ITip\Message |
|
35 | - */ |
|
36 | - protected $itipMessage; |
|
37 | - |
|
38 | - /** |
|
39 | - * Creates the email handler. |
|
40 | - * |
|
41 | - * @param string $senderEmail. The 'senderEmail' is the email that shows up |
|
42 | - * in the 'From:' address. This should |
|
43 | - * generally be some kind of no-reply email |
|
44 | - * address you own. |
|
45 | - */ |
|
46 | - public function __construct($senderEmail) { |
|
47 | - |
|
48 | - $this->senderEmail = $senderEmail; |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /* |
|
24 | + /** |
|
25 | + * Email address used in From: header. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $senderEmail; |
|
30 | + |
|
31 | + /** |
|
32 | + * ITipMessage |
|
33 | + * |
|
34 | + * @var ITip\Message |
|
35 | + */ |
|
36 | + protected $itipMessage; |
|
37 | + |
|
38 | + /** |
|
39 | + * Creates the email handler. |
|
40 | + * |
|
41 | + * @param string $senderEmail. The 'senderEmail' is the email that shows up |
|
42 | + * in the 'From:' address. This should |
|
43 | + * generally be some kind of no-reply email |
|
44 | + * address you own. |
|
45 | + */ |
|
46 | + public function __construct($senderEmail) { |
|
47 | + |
|
48 | + $this->senderEmail = $senderEmail; |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /* |
|
53 | 53 | * This initializes the plugin. |
54 | 54 | * |
55 | 55 | * This function is called by Sabre\DAV\Server, after |
@@ -60,131 +60,131 @@ discard block |
||
60 | 60 | * @param DAV\Server $server |
61 | 61 | * @return void |
62 | 62 | */ |
63 | - public function initialize(DAV\Server $server) { |
|
64 | - |
|
65 | - $server->on('schedule', [$this, 'schedule'], 120); |
|
66 | - |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Returns a plugin name. |
|
71 | - * |
|
72 | - * Using this name other plugins will be able to access other plugins |
|
73 | - * using \Sabre\DAV\Server::getPlugin |
|
74 | - * |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getPluginName() { |
|
78 | - |
|
79 | - return 'imip'; |
|
80 | - |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Event handler for the 'schedule' event. |
|
85 | - * |
|
86 | - * @param ITip\Message $iTipMessage |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function schedule(ITip\Message $iTipMessage) { |
|
90 | - |
|
91 | - // Not sending any emails if the system considers the update |
|
92 | - // insignificant. |
|
93 | - if (!$iTipMessage->significantChange) { |
|
94 | - if (!$iTipMessage->scheduleStatus) { |
|
95 | - $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; |
|
96 | - } |
|
97 | - return; |
|
98 | - } |
|
99 | - |
|
100 | - $summary = $iTipMessage->message->VEVENT->SUMMARY; |
|
101 | - |
|
102 | - if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') |
|
103 | - return; |
|
104 | - |
|
105 | - if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') |
|
106 | - return; |
|
107 | - |
|
108 | - $sender = substr($iTipMessage->sender, 7); |
|
109 | - $recipient = substr($iTipMessage->recipient, 7); |
|
110 | - |
|
111 | - if ($iTipMessage->senderName) { |
|
112 | - $sender = $iTipMessage->senderName . ' <' . $sender . '>'; |
|
113 | - } |
|
114 | - if ($iTipMessage->recipientName) { |
|
115 | - $recipient = $iTipMessage->recipientName . ' <' . $recipient . '>'; |
|
116 | - } |
|
117 | - |
|
118 | - $subject = 'SabreDAV iTIP message'; |
|
119 | - switch (strtoupper($iTipMessage->method)) { |
|
120 | - case 'REPLY' : |
|
121 | - $subject = 'Re: ' . $summary; |
|
122 | - break; |
|
123 | - case 'REQUEST' : |
|
124 | - $subject = $summary; |
|
125 | - break; |
|
126 | - case 'CANCEL' : |
|
127 | - $subject = 'Cancelled: ' . $summary; |
|
128 | - break; |
|
129 | - } |
|
130 | - |
|
131 | - $headers = [ |
|
132 | - 'Reply-To: ' . $sender, |
|
133 | - 'From: ' . $this->senderEmail, |
|
134 | - 'Content-Type: text/calendar; charset=UTF-8; method=' . $iTipMessage->method, |
|
135 | - ]; |
|
136 | - if (DAV\Server::$exposeVersion) { |
|
137 | - $headers[] = 'X-Sabre-Version: ' . DAV\Version::VERSION; |
|
138 | - } |
|
139 | - $this->mail( |
|
140 | - $recipient, |
|
141 | - $subject, |
|
142 | - $iTipMessage->message->serialize(), |
|
143 | - $headers |
|
144 | - ); |
|
145 | - $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; |
|
146 | - |
|
147 | - } |
|
148 | - |
|
149 | - // @codeCoverageIgnoreStart |
|
150 | - // This is deemed untestable in a reasonable manner |
|
151 | - |
|
152 | - /** |
|
153 | - * This function is responsible for sending the actual email. |
|
154 | - * |
|
155 | - * @param string $to Recipient email address |
|
156 | - * @param string $subject Subject of the email |
|
157 | - * @param string $body iCalendar body |
|
158 | - * @param array $headers List of headers |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - protected function mail($to, $subject, $body, array $headers) { |
|
162 | - |
|
163 | - mail($to, $subject, $body, implode("\r\n", $headers)); |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - // @codeCoverageIgnoreEnd |
|
168 | - |
|
169 | - /** |
|
170 | - * Returns a bunch of meta-data about the plugin. |
|
171 | - * |
|
172 | - * Providing this information is optional, and is mainly displayed by the |
|
173 | - * Browser plugin. |
|
174 | - * |
|
175 | - * The description key in the returned array may contain html and will not |
|
176 | - * be sanitized. |
|
177 | - * |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function getPluginInfo() { |
|
181 | - |
|
182 | - return [ |
|
183 | - 'name' => $this->getPluginName(), |
|
184 | - 'description' => 'Email delivery (rfc6037) for CalDAV scheduling', |
|
185 | - 'link' => 'http://sabre.io/dav/scheduling/', |
|
186 | - ]; |
|
187 | - |
|
188 | - } |
|
63 | + public function initialize(DAV\Server $server) { |
|
64 | + |
|
65 | + $server->on('schedule', [$this, 'schedule'], 120); |
|
66 | + |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Returns a plugin name. |
|
71 | + * |
|
72 | + * Using this name other plugins will be able to access other plugins |
|
73 | + * using \Sabre\DAV\Server::getPlugin |
|
74 | + * |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getPluginName() { |
|
78 | + |
|
79 | + return 'imip'; |
|
80 | + |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Event handler for the 'schedule' event. |
|
85 | + * |
|
86 | + * @param ITip\Message $iTipMessage |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function schedule(ITip\Message $iTipMessage) { |
|
90 | + |
|
91 | + // Not sending any emails if the system considers the update |
|
92 | + // insignificant. |
|
93 | + if (!$iTipMessage->significantChange) { |
|
94 | + if (!$iTipMessage->scheduleStatus) { |
|
95 | + $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; |
|
96 | + } |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + $summary = $iTipMessage->message->VEVENT->SUMMARY; |
|
101 | + |
|
102 | + if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') |
|
103 | + return; |
|
104 | + |
|
105 | + if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') |
|
106 | + return; |
|
107 | + |
|
108 | + $sender = substr($iTipMessage->sender, 7); |
|
109 | + $recipient = substr($iTipMessage->recipient, 7); |
|
110 | + |
|
111 | + if ($iTipMessage->senderName) { |
|
112 | + $sender = $iTipMessage->senderName . ' <' . $sender . '>'; |
|
113 | + } |
|
114 | + if ($iTipMessage->recipientName) { |
|
115 | + $recipient = $iTipMessage->recipientName . ' <' . $recipient . '>'; |
|
116 | + } |
|
117 | + |
|
118 | + $subject = 'SabreDAV iTIP message'; |
|
119 | + switch (strtoupper($iTipMessage->method)) { |
|
120 | + case 'REPLY' : |
|
121 | + $subject = 'Re: ' . $summary; |
|
122 | + break; |
|
123 | + case 'REQUEST' : |
|
124 | + $subject = $summary; |
|
125 | + break; |
|
126 | + case 'CANCEL' : |
|
127 | + $subject = 'Cancelled: ' . $summary; |
|
128 | + break; |
|
129 | + } |
|
130 | + |
|
131 | + $headers = [ |
|
132 | + 'Reply-To: ' . $sender, |
|
133 | + 'From: ' . $this->senderEmail, |
|
134 | + 'Content-Type: text/calendar; charset=UTF-8; method=' . $iTipMessage->method, |
|
135 | + ]; |
|
136 | + if (DAV\Server::$exposeVersion) { |
|
137 | + $headers[] = 'X-Sabre-Version: ' . DAV\Version::VERSION; |
|
138 | + } |
|
139 | + $this->mail( |
|
140 | + $recipient, |
|
141 | + $subject, |
|
142 | + $iTipMessage->message->serialize(), |
|
143 | + $headers |
|
144 | + ); |
|
145 | + $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; |
|
146 | + |
|
147 | + } |
|
148 | + |
|
149 | + // @codeCoverageIgnoreStart |
|
150 | + // This is deemed untestable in a reasonable manner |
|
151 | + |
|
152 | + /** |
|
153 | + * This function is responsible for sending the actual email. |
|
154 | + * |
|
155 | + * @param string $to Recipient email address |
|
156 | + * @param string $subject Subject of the email |
|
157 | + * @param string $body iCalendar body |
|
158 | + * @param array $headers List of headers |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + protected function mail($to, $subject, $body, array $headers) { |
|
162 | + |
|
163 | + mail($to, $subject, $body, implode("\r\n", $headers)); |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + // @codeCoverageIgnoreEnd |
|
168 | + |
|
169 | + /** |
|
170 | + * Returns a bunch of meta-data about the plugin. |
|
171 | + * |
|
172 | + * Providing this information is optional, and is mainly displayed by the |
|
173 | + * Browser plugin. |
|
174 | + * |
|
175 | + * The description key in the returned array may contain html and will not |
|
176 | + * be sanitized. |
|
177 | + * |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function getPluginInfo() { |
|
181 | + |
|
182 | + return [ |
|
183 | + 'name' => $this->getPluginName(), |
|
184 | + 'description' => 'Email delivery (rfc6037) for CalDAV scheduling', |
|
185 | + 'link' => 'http://sabre.io/dav/scheduling/', |
|
186 | + ]; |
|
187 | + |
|
188 | + } |
|
189 | 189 | |
190 | 190 | } |