1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
|
<?php
declare(strict_types=1);
namespace Sabre\CalDAV\Schedule;
use DateTimeZone;
use Sabre\CalDAV\ICalendar;
use Sabre\CalDAV\ICalendarObject;
use Sabre\CalDAV\Xml\Property\ScheduleCalendarTransp;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Exception\NotImplemented;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Sharing;
use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\DAVACL;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\VObject;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\ITip;
use Sabre\VObject\ITip\Message;
use Sabre\VObject\Reader;
/**
* CalDAV scheduling plugin.
* =========================.
*
* This plugin provides the functionality added by the "Scheduling Extensions
* to CalDAV" standard, as defined in RFC6638.
*
* calendar-auto-schedule largely works by intercepting a users request to
* update their local calendar. If a user creates a new event with attendees,
* this plugin is supposed to grab the information from that event, and notify
* the attendees of this.
*
* There's 3 possible transports for this:
* * local delivery
* * delivery through email (iMip)
* * server-to-server delivery (iSchedule)
*
* iMip is simply, because we just need to add the iTip message as an email
* attachment. Local delivery is harder, because we both need to add this same
* message to a local DAV inbox, as well as live-update the relevant events.
*
* iSchedule is something for later.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Plugin extends ServerPlugin
{
/**
* This is the official CalDAV namespace.
*/
const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav';
/**
* Reference to main Server object.
*
* @var Server
*/
protected $server;
/**
* Returns a list of features for the DAV: HTTP header.
*
* @return array
*/
public function getFeatures()
{
return ['calendar-auto-schedule', 'calendar-availability'];
}
/**
* Returns the name of the plugin.
*
* Using this name other plugins will be able to access other plugins
* using Server::getPlugin
*
* @return string
*/
public function getPluginName()
{
return 'caldav-schedule';
}
/**
* Initializes the plugin.
*/
public function initialize(Server $server)
{
$this->server = $server;
$server->on('method:POST', [$this, 'httpPost']);
$server->on('propFind', [$this, 'propFind']);
$server->on('propPatch', [$this, 'propPatch']);
$server->on('calendarObjectChange', [$this, 'calendarObjectChange']);
$server->on('beforeUnbind', [$this, 'beforeUnbind']);
$server->on('schedule', [$this, 'scheduleLocalDelivery']);
$server->on('getSupportedPrivilegeSet', [$this, 'getSupportedPrivilegeSet']);
$ns = '{'.self::NS_CALDAV.'}';
/*
* This information ensures that the {DAV:}resourcetype property has
* the correct values.
*/
$server->resourceTypeMapping['\\Sabre\\CalDAV\\Schedule\\IOutbox'] = $ns.'schedule-outbox';
$server->resourceTypeMapping['\\Sabre\\CalDAV\\Schedule\\IInbox'] = $ns.'schedule-inbox';
/*
* Properties we protect are made read-only by the server.
*/
array_push($server->protectedProperties,
$ns.'schedule-inbox-URL',
$ns.'schedule-outbox-URL',
$ns.'calendar-user-address-set',
$ns.'calendar-user-type',
$ns.'schedule-default-calendar-URL'
);
}
/**
* Use this method to tell the server this plugin defines additional
* HTTP methods.
*
* This method is passed a uri. It should only return HTTP methods that are
* available for the specified uri.
*
* @param string $uri
*
* @return array
*/
public function getHTTPMethods($uri)
{
try {
$node = $this->server->tree->getNodeForPath($uri);
} catch (NotFound $e) {
return [];
}
if ($node instanceof IOutbox) {
return ['POST'];
}
return [];
}
/**
* This method handles POST request for the outbox.
*
* @return bool
*/
public function httpPost(RequestInterface $request, ResponseInterface $response)
{
// Checking if this is a text/calendar content type
$contentType = $request->getHeader('Content-Type');
if (!$contentType || 0 !== strpos($contentType, 'text/calendar')) {
return;
}
$path = $request->getPath();
// Checking if we're talking to an outbox
try {
$node = $this->server->tree->getNodeForPath($path);
} catch (NotFound $e) {
return;
}
if (!$node instanceof IOutbox) {
return;
}
$this->server->transactionType = 'post-caldav-outbox';
$this->outboxRequest($node, $request, $response);
// Returning false breaks the event chain and tells the server we've
// handled the request.
return false;
}
/**
* This method handler is invoked during fetching of properties.
*
* We use this event to add calendar-auto-schedule-specific properties.
*/
public function propFind(PropFind $propFind, INode $node)
{
if ($node instanceof DAVACL\IPrincipal) {
$caldavPlugin = $this->server->getPlugin('caldav');
$principalUrl = $node->getPrincipalUrl();
// schedule-outbox-URL property
$propFind->handle('{'.self::NS_CALDAV.'}schedule-outbox-URL', function () use ($principalUrl, $caldavPlugin) {
$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
if (!$calendarHomePath) {
return null;
}
$outboxPath = $calendarHomePath.'/outbox/';
return new LocalHref($outboxPath);
});
// schedule-inbox-URL property
$propFind->handle('{'.self::NS_CALDAV.'}schedule-inbox-URL', function () use ($principalUrl, $caldavPlugin) {
$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
if (!$calendarHomePath) {
return null;
}
$inboxPath = $calendarHomePath.'/inbox/';
return new LocalHref($inboxPath);
});
$propFind->handle('{'.self::NS_CALDAV.'}schedule-default-calendar-URL', function () use ($principalUrl, $caldavPlugin) {
// We don't support customizing this property yet, so in the
// meantime we just grab the first calendar in the home-set.
$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
if (!$calendarHomePath) {
return null;
}
$sccs = '{'.self::NS_CALDAV.'}supported-calendar-component-set';
$result = $this->server->getPropertiesForPath($calendarHomePath, [
'{DAV:}resourcetype',
'{DAV:}share-access',
$sccs,
], 1);
foreach ($result as $child) {
if (!isset($child[200]['{DAV:}resourcetype']) || !$child[200]['{DAV:}resourcetype']->is('{'.self::NS_CALDAV.'}calendar')) {
// Node is either not a calendar
continue;
}
if (isset($child[200]['{DAV:}share-access'])) {
$shareAccess = $child[200]['{DAV:}share-access']->getValue();
if (Sharing\Plugin::ACCESS_NOTSHARED !== $shareAccess && Sharing\Plugin::ACCESS_SHAREDOWNER !== $shareAccess) {
// Node is a shared node, not owned by the relevant
// user.
continue;
}
}
if (!isset($child[200][$sccs]) || in_array('VEVENT', $child[200][$sccs]->getValue())) {
// Either there is no supported-calendar-component-set
// (which is fine) or we found one that supports VEVENT.
return new LocalHref($child['href']);
}
}
});
// The server currently reports every principal to be of type
// 'INDIVIDUAL'
$propFind->handle('{'.self::NS_CALDAV.'}calendar-user-type', function () {
return 'INDIVIDUAL';
});
}
// Mapping the old property to the new property.
$propFind->handle('{http://calendarserver.org/ns/}calendar-availability', function () use ($propFind, $node) {
// In case it wasn't clear, the only difference is that we map the
// old property to a different namespace.
$availProp = '{'.self::NS_CALDAV.'}calendar-availability';
$subPropFind = new PropFind(
$propFind->getPath(),
[$availProp]
);
$this->server->getPropertiesByNode(
$subPropFind,
$node
);
$propFind->set(
'{http://calendarserver.org/ns/}calendar-availability',
$subPropFind->get($availProp),
$subPropFind->getStatus($availProp)
);
});
}
/**
* This method is called during property updates.
*
* @param string $path
*/
public function propPatch($path, PropPatch $propPatch)
{
// Mapping the old property to the new property.
$propPatch->handle('{http://calendarserver.org/ns/}calendar-availability', function ($value) use ($path) {
$availProp = '{'.self::NS_CALDAV.'}calendar-availability';
$subPropPatch = new PropPatch([$availProp => $value]);
$this->server->emit('propPatch', [$path, $subPropPatch]);
$subPropPatch->commit();
return $subPropPatch->getResult()[$availProp];
});
}
/**
* This method is triggered whenever there was a calendar object gets
* created or updated.
*
* @param RequestInterface $request HTTP request
* @param ResponseInterface $response HTTP Response
* @param VCalendar $vCal Parsed iCalendar object
* @param mixed $calendarPath Path to calendar collection
* @param mixed $modified the iCalendar object has been touched
* @param mixed $isNew Whether this was a new item or we're updating one
*/
public function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew)
{
if (!$this->scheduleReply($this->server->httpRequest)) {
return;
}
$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
$addresses = $this->getAddressesForPrincipal(
$calendarNode->getOwner()
);
if (!$isNew) {
$node = $this->server->tree->getNodeForPath($request->getPath());
$oldObj = Reader::read($node->get());
} else {
$oldObj = null;
}
$this->processICalendarChange($oldObj, $vCal, $addresses, [], $modified);
if ($oldObj) {
// Destroy circular references so PHP will GC the object.
$oldObj->destroy();
}
}
/**
* This method is responsible for delivering the ITip message.
*/
public function deliver(ITip\Message $iTipMessage)
{
$this->server->emit('schedule', [$iTipMessage]);
if (!$iTipMessage->scheduleStatus) {
$iTipMessage->scheduleStatus = '5.2;There was no system capable of delivering the scheduling message';
}
// In case the change was considered 'insignificant', we are going to
// remove any error statuses, if any. See ticket #525.
list($baseCode) = explode('.', $iTipMessage->scheduleStatus);
if (!$iTipMessage->significantChange && in_array($baseCode, ['3', '5'])) {
$iTipMessage->scheduleStatus = null;
}
}
/**
* This method is triggered before a file gets deleted.
*
* We use this event to make sure that when this happens, attendees get
* cancellations, and organizers get 'DECLINED' statuses.
*
* @param string $path
*/
public function beforeUnbind($path)
{
// FIXME: We shouldn't trigger this functionality when we're issuing a
// MOVE. This is a hack.
if ('MOVE' === $this->server->httpRequest->getMethod()) {
return;
}
$node = $this->server->tree->getNodeForPath($path);
if (!$node instanceof ICalendarObject || $node instanceof ISchedulingObject) {
return;
}
if (!$this->scheduleReply($this->server->httpRequest)) {
return;
}
$addresses = $this->getAddressesForPrincipal(
$node->getOwner()
);
$broker = new ITip\Broker();
$messages = $broker->parseEvent(null, $addresses, $node->get());
foreach ($messages as $message) {
$this->deliver($message);
}
}
/**
* Event handler for the 'schedule' event.
*
* This handler attempts to look at local accounts to deliver the
* scheduling object.
*/
public function scheduleLocalDelivery(ITip\Message $iTipMessage)
{
$aclPlugin = $this->server->getPlugin('acl');
// Local delivery is not available if the ACL plugin is not loaded.
if (!$aclPlugin) {
return;
}
$caldavNS = '{'.self::NS_CALDAV.'}';
$principalUri = $aclPlugin->getPrincipalByUri($iTipMessage->recipient);
if (!$principalUri) {
$iTipMessage->scheduleStatus = '3.7;Could not find principal.';
return;
}
// We found a principal URL, now we need to find its inbox.
// Unfortunately we may not have sufficient privileges to find this, so
// we are temporarily turning off ACL to let this come through.
//
// Once we support PHP 5.5, this should be wrapped in a try..finally
// block so we can ensure that this privilege gets added again after.
$this->server->removeListener('propFind', [$aclPlugin, 'propFind']);
$result = $this->server->getProperties(
$principalUri,
[
'{DAV:}principal-URL',
$caldavNS.'calendar-home-set',
$caldavNS.'schedule-inbox-URL',
$caldavNS.'schedule-default-calendar-URL',
'{http://sabredav.org/ns}email-address',
]
);
// Re-registering the ACL event
$this->server->on('propFind', [$aclPlugin, 'propFind'], 20);
if (!isset($result[$caldavNS.'schedule-inbox-URL'])) {
$iTipMessage->scheduleStatus = '5.2;Could not find local inbox';
return;
}
if (!isset($result[$caldavNS.'calendar-home-set'])) {
$iTipMessage->scheduleStatus = '5.2;Could not locate a calendar-home-set';
return;
}
if (!isset($result[$caldavNS.'schedule-default-calendar-URL'])) {
$iTipMessage->scheduleStatus = '5.2;Could not find a schedule-default-calendar-URL property';
return;
}
$calendarPath = $result[$caldavNS.'schedule-default-calendar-URL']->getHref();
$homePath = $result[$caldavNS.'calendar-home-set']->getHref();
$inboxPath = $result[$caldavNS.'schedule-inbox-URL']->getHref();
if ('REPLY' === $iTipMessage->method) {
$privilege = 'schedule-deliver-reply';
} else {
$privilege = 'schedule-deliver-invite';
}
if (!$aclPlugin->checkPrivileges($inboxPath, $caldavNS.$privilege, DAVACL\Plugin::R_PARENT, false)) {
$iTipMessage->scheduleStatus = '3.8;insufficient privileges: '.$privilege.' is required on the recipient schedule inbox.';
return;
}
// Next, we're going to find out if the item already exits in one of
// the users' calendars.
$uid = $iTipMessage->uid;
$newFileName = 'sabredav-'.\Sabre\DAV\UUIDUtil::getUUID().'.ics';
$home = $this->server->tree->getNodeForPath($homePath);
$inbox = $this->server->tree->getNodeForPath($inboxPath);
$currentObject = null;
$objectNode = null;
$oldICalendarData = null;
$isNewNode = false;
$result = $home->getCalendarObjectByUID($uid);
if ($result) {
// There was an existing object, we need to update probably.
$objectPath = $homePath.'/'.$result;
$objectNode = $this->server->tree->getNodeForPath($objectPath);
$oldICalendarData = $objectNode->get();
$currentObject = Reader::read($oldICalendarData);
} else {
$isNewNode = true;
}
$broker = new ITip\Broker();
$newObject = $broker->processMessage($iTipMessage, $currentObject);
$inbox->createFile($newFileName, $iTipMessage->message->serialize());
if (!$newObject) {
// We received an iTip message referring to a UID that we don't
// have in any calendars yet, and processMessage did not give us a
// calendarobject back.
//
// The implication is that processMessage did not understand the
// iTip message.
$iTipMessage->scheduleStatus = '5.0;iTip message was not processed by the server, likely because we didn\'t understand it.';
return;
}
// Note that we are bypassing ACL on purpose by calling this directly.
// We may need to look a bit deeper into this later. Supporting ACL
// here would be nice.
if ($isNewNode) {
$calendar = $this->server->tree->getNodeForPath($calendarPath);
$calendar->createFile($newFileName, $newObject->serialize());
} else {
// If the message was a reply, we may have to inform other
// attendees of this attendees status. Therefore we're shooting off
// another itipMessage.
if ('REPLY' === $iTipMessage->method) {
$this->processICalendarChange(
$oldICalendarData,
$newObject,
[$iTipMessage->recipient],
[$iTipMessage->sender]
);
}
$objectNode->put($newObject->serialize());
}
$iTipMessage->scheduleStatus = '1.2;Message delivered locally';
}
/**
* This method is triggered whenever a subsystem requests the privileges
* that are supported on a particular node.
*
* We need to add a number of privileges for scheduling purposes.
*/
public function getSupportedPrivilegeSet(INode $node, array &$supportedPrivilegeSet)
{
$ns = '{'.self::NS_CALDAV.'}';
if ($node instanceof IOutbox) {
$supportedPrivilegeSet[$ns.'schedule-send'] = [
'abstract' => false,
'aggregates' => [
$ns.'schedule-send-invite' => [
'abstract' => false,
'aggregates' => [],
],
$ns.'schedule-send-reply' => [
'abstract' => false,
'aggregates' => [],
],
$ns.'schedule-send-freebusy' => [
'abstract' => false,
'aggregates' => [],
],
// Privilege from an earlier scheduling draft, but still
// used by some clients.
$ns.'schedule-post-vevent' => [
'abstract' => false,
'aggregates' => [],
],
],
];
}
if ($node instanceof IInbox) {
$supportedPrivilegeSet[$ns.'schedule-deliver'] = [
'abstract' => false,
'aggregates' => [
$ns.'schedule-deliver-invite' => [
'abstract' => false,
'aggregates' => [],
],
$ns.'schedule-deliver-reply' => [
'abstract' => false,
'aggregates' => [],
],
$ns.'schedule-query-freebusy' => [
'abstract' => false,
'aggregates' => [],
],
],
];
}
}
/**
* This method looks at an old iCalendar object, a new iCalendar object and
* starts sending scheduling messages based on the changes.
*
* A list of addresses needs to be specified, so the system knows who made
* the update, because the behavior may be different based on if it's an
* attendee or an organizer.
*
* This method may update $newObject to add any status changes.
*
* @param VCalendar|string $oldObject
* @param array $ignore any addresses to not send messages to
* @param bool $modified a marker to indicate that the original object
* modified by this process
*/
protected function processICalendarChange($oldObject, VCalendar $newObject, array $addresses, array $ignore = [], &$modified = false)
{
$broker = new ITip\Broker();
$messages = $broker->parseEvent($newObject, $addresses, $oldObject);
if ($messages) {
$modified = true;
}
foreach ($messages as $message) {
if (in_array($message->recipient, $ignore)) {
continue;
}
$this->deliver($message);
if (isset($newObject->VEVENT->ORGANIZER) && ($newObject->VEVENT->ORGANIZER->getNormalizedValue() === $message->recipient)) {
if ($message->scheduleStatus) {
$newObject->VEVENT->ORGANIZER['SCHEDULE-STATUS'] = $message->getScheduleStatus();
}
unset($newObject->VEVENT->ORGANIZER['SCHEDULE-FORCE-SEND']);
} else {
if (isset($newObject->VEVENT->ATTENDEE)) {
foreach ($newObject->VEVENT->ATTENDEE as $attendee) {
if ($attendee->getNormalizedValue() === $message->recipient) {
if ($message->scheduleStatus) {
$attendee['SCHEDULE-STATUS'] = $message->getScheduleStatus();
}
unset($attendee['SCHEDULE-FORCE-SEND']);
break;
}
}
}
}
}
}
/**
* Returns a list of addresses that are associated with a principal.
*
* @param string $principal
*
* @return array
*/
protected function getAddressesForPrincipal($principal)
{
$CUAS = '{'.self::NS_CALDAV.'}calendar-user-address-set';
$properties = $this->server->getProperties(
$principal,
[$CUAS]
);
// If we can't find this information, we'll stop processing
if (!isset($properties[$CUAS])) {
return [];
}
$addresses = $properties[$CUAS]->getHrefs();
return $addresses;
}
/**
* This method handles POST requests to the schedule-outbox.
*
* Currently, two types of requests are supported:
* * FREEBUSY requests from RFC 6638
* * Simple iTIP messages from draft-desruisseaux-caldav-sched-04
*
* The latter is from an expired early draft of the CalDAV scheduling
* extensions, but iCal depends on a feature from that spec, so we
* implement it.
*/
public function outboxRequest(IOutbox $outboxNode, RequestInterface $request, ResponseInterface $response)
{
$outboxPath = $request->getPath();
// Parsing the request body
try {
$vObject = VObject\Reader::read($request->getBody());
} catch (VObject\ParseException $e) {
throw new BadRequest('The request body must be a valid iCalendar object. Parse error: '.$e->getMessage());
}
// The incoming iCalendar object must have a METHOD property, and a
// component. The combination of both determines what type of request
// this is.
$componentType = null;
foreach ($vObject->getComponents() as $component) {
if ('VTIMEZONE' !== $component->name) {
$componentType = $component->name;
break;
}
}
if (is_null($componentType)) {
throw new BadRequest('We expected at least one VTODO, VJOURNAL, VFREEBUSY or VEVENT component');
}
// Validating the METHOD
$method = strtoupper((string) $vObject->METHOD);
if (!$method) {
throw new BadRequest('A METHOD property must be specified in iTIP messages');
}
// So we support one type of request:
//
// REQUEST with a VFREEBUSY component
$acl = $this->server->getPlugin('acl');
if ('VFREEBUSY' === $componentType && 'REQUEST' === $method) {
$acl && $acl->checkPrivileges($outboxPath, '{'.self::NS_CALDAV.'}schedule-send-freebusy');
$this->handleFreeBusyRequest($outboxNode, $vObject, $request, $response);
// Destroy circular references so PHP can GC the object.
$vObject->destroy();
unset($vObject);
} else {
throw new NotImplemented('We only support VFREEBUSY (REQUEST) on this endpoint');
}
}
/**
* This method is responsible for parsing a free-busy query request and
* returning its result in $response.
*/
protected function handleFreeBusyRequest(IOutbox $outbox, VObject\Component $vObject, RequestInterface $request, ResponseInterface $response)
{
$vFreeBusy = $vObject->VFREEBUSY;
$organizer = $vFreeBusy->ORGANIZER;
$organizer = (string) $organizer;
// Validating if the organizer matches the owner of the inbox.
$owner = $outbox->getOwner();
$caldavNS = '{'.self::NS_CALDAV.'}';
$uas = $caldavNS.'calendar-user-address-set';
$props = $this->server->getProperties($owner, [$uas]);
if (empty($props[$uas]) || !in_array($organizer, $props[$uas]->getHrefs())) {
throw new Forbidden('The organizer in the request did not match any of the addresses for the owner of this inbox');
}
if (!isset($vFreeBusy->ATTENDEE)) {
throw new BadRequest('You must at least specify 1 attendee');
}
$attendees = [];
foreach ($vFreeBusy->ATTENDEE as $attendee) {
$attendees[] = (string) $attendee;
}
if (!isset($vFreeBusy->DTSTART) || !isset($vFreeBusy->DTEND)) {
throw new BadRequest('DTSTART and DTEND must both be specified');
}
$startRange = $vFreeBusy->DTSTART->getDateTime();
$endRange = $vFreeBusy->DTEND->getDateTime();
$results = [];
foreach ($attendees as $attendee) {
$results[] = $this->getFreeBusyForEmail($attendee, $startRange, $endRange, $vObject);
}
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$scheduleResponse = $dom->createElement('cal:schedule-response');
foreach ($this->server->xml->namespaceMap as $namespace => $prefix) {
$scheduleResponse->setAttribute('xmlns:'.$prefix, $namespace);
}
$dom->appendChild($scheduleResponse);
foreach ($results as $result) {
$xresponse = $dom->createElement('cal:response');
$recipient = $dom->createElement('cal:recipient');
$recipientHref = $dom->createElement('d:href');
$recipientHref->appendChild($dom->createTextNode($result['href']));
$recipient->appendChild($recipientHref);
$xresponse->appendChild($recipient);
$reqStatus = $dom->createElement('cal:request-status');
$reqStatus->appendChild($dom->createTextNode($result['request-status']));
$xresponse->appendChild($reqStatus);
if (isset($result['calendar-data'])) {
$calendardata = $dom->createElement('cal:calendar-data');
$calendardata->appendChild($dom->createTextNode(str_replace("\r\n", "\n", $result['calendar-data']->serialize())));
$xresponse->appendChild($calendardata);
}
$scheduleResponse->appendChild($xresponse);
}
$response->setStatus(200);
$response->setHeader('Content-Type', 'application/xml');
$response->setBody($dom->saveXML());
}
/**
* Returns free-busy information for a specific address. The returned
* data is an array containing the following properties:.
*
* calendar-data : A VFREEBUSY VObject
* request-status : an iTip status code.
* href: The principal's email address, as requested
*
* The following request status codes may be returned:
* * 2.0;description
* * 3.7;description
*
* @param string $email address
*
* @return array
*/
protected function getFreeBusyForEmail($email, \DateTimeInterface $start, \DateTimeInterface $end, VObject\Component $request)
{
$caldavNS = '{'.self::NS_CALDAV.'}';
$aclPlugin = $this->server->getPlugin('acl');
if ('mailto:' === substr($email, 0, 7)) {
$email = substr($email, 7);
}
$result = $aclPlugin->principalSearch(
['{http://sabredav.org/ns}email-address' => $email],
[
'{DAV:}principal-URL',
$caldavNS.'calendar-home-set',
$caldavNS.'schedule-inbox-URL',
'{http://sabredav.org/ns}email-address',
]
);
if (!count($result)) {
return [
'request-status' => '3.7;Could not find principal',
'href' => 'mailto:'.$email,
];
}
if (!isset($result[0][200][$caldavNS.'calendar-home-set'])) {
return [
'request-status' => '3.7;No calendar-home-set property found',
'href' => 'mailto:'.$email,
];
}
if (!isset($result[0][200][$caldavNS.'schedule-inbox-URL'])) {
return [
'request-status' => '3.7;No schedule-inbox-URL property found',
'href' => 'mailto:'.$email,
];
}
$homeSet = $result[0][200][$caldavNS.'calendar-home-set']->getHref();
$inboxUrl = $result[0][200][$caldavNS.'schedule-inbox-URL']->getHref();
// Do we have permission?
$aclPlugin->checkPrivileges($inboxUrl, $caldavNS.'schedule-query-freebusy');
// Grabbing the calendar list
$objects = [];
$calendarTimeZone = new DateTimeZone('UTC');
foreach ($this->server->tree->getNodeForPath($homeSet)->getChildren() as $node) {
if (!$node instanceof ICalendar) {
continue;
}
$sct = $caldavNS.'schedule-calendar-transp';
$ctz = $caldavNS.'calendar-timezone';
$props = $node->getProperties([$sct, $ctz]);
if (isset($props[$sct]) && ScheduleCalendarTransp::TRANSPARENT == $props[$sct]->getValue()) {
// If a calendar is marked as 'transparent', it means we must
// ignore it for free-busy purposes.
continue;
}
if (isset($props[$ctz])) {
$vtimezoneObj = VObject\Reader::read($props[$ctz]);
$calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
// Destroy circular references so PHP can garbage collect the object.
$vtimezoneObj->destroy();
}
// Getting the list of object uris within the time-range
$urls = $node->calendarQuery([
'name' => 'VCALENDAR',
'comp-filters' => [
[
'name' => 'VEVENT',
'comp-filters' => [],
'prop-filters' => [],
'is-not-defined' => false,
'time-range' => [
'start' => $start,
'end' => $end,
],
],
],
'prop-filters' => [],
'is-not-defined' => false,
'time-range' => null,
]);
$calObjects = array_map(function ($url) use ($node) {
$obj = $node->getChild($url)->get();
return $obj;
}, $urls);
$objects = array_merge($objects, $calObjects);
}
$inboxProps = $this->server->getProperties(
$inboxUrl,
$caldavNS.'calendar-availability'
);
$vcalendar = new VObject\Component\VCalendar();
$vcalendar->METHOD = 'REPLY';
$generator = new VObject\FreeBusyGenerator();
$generator->setObjects($objects);
$generator->setTimeRange($start, $end);
$generator->setBaseObject($vcalendar);
$generator->setTimeZone($calendarTimeZone);
if ($inboxProps) {
$generator->setVAvailability(
VObject\Reader::read(
$inboxProps[$caldavNS.'calendar-availability']
)
);
}
$result = $generator->getResult();
$vcalendar->VFREEBUSY->ATTENDEE = 'mailto:'.$email;
$vcalendar->VFREEBUSY->UID = (string) $request->VFREEBUSY->UID;
$vcalendar->VFREEBUSY->ORGANIZER = clone $request->VFREEBUSY->ORGANIZER;
return [
'calendar-data' => $result,
'request-status' => '2.0;Success',
'href' => 'mailto:'.$email,
];
}
/**
* This method checks the 'Schedule-Reply' header
* and returns false if it's 'F', otherwise true.
*
* @return bool
*/
private function scheduleReply(RequestInterface $request)
{
$scheduleReply = $request->getHeader('Schedule-Reply');
return 'F' !== $scheduleReply;
}
/**
* Returns a bunch of meta-data about the plugin.
*
* Providing this information is optional, and is mainly displayed by the
* Browser plugin.
*
* The description key in the returned array may contain html and will not
* be sanitized.
*
* @return array
*/
public function getPluginInfo()
{
return [
'name' => $this->getPluginName(),
'description' => 'Adds calendar-auto-schedule, as defined in rfc6638',
'link' => 'http://sabre.io/dav/scheduling/',
];
}
}
|