2016-11-04 17:05:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
2016-11-15 19:01:40 +03:00
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com>
|
2016-11-04 17:05:15 +03:00
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2016-11-15 19:01:40 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-11-04 17:05:15 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2016-11-04 17:05:15 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-04 17:05:15 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-11-04 17:05:15 +03:00
|
|
|
namespace OCA\DAV\CalDAV\Schedule;
|
|
|
|
|
2019-07-30 13:58:35 +03:00
|
|
|
use DateTimeZone;
|
2016-11-15 19:01:40 +03:00
|
|
|
use OCA\DAV\CalDAV\CalDavBackend;
|
|
|
|
use OCA\DAV\CalDAV\CalendarHome;
|
2020-03-08 19:33:27 +03:00
|
|
|
use OCP\IConfig;
|
2019-07-30 13:58:35 +03:00
|
|
|
use Sabre\CalDAV\ICalendar;
|
2016-11-15 19:01:40 +03:00
|
|
|
use Sabre\DAV\INode;
|
2019-07-26 16:04:58 +03:00
|
|
|
use Sabre\DAV\IProperties;
|
2016-11-15 19:01:40 +03:00
|
|
|
use Sabre\DAV\PropFind;
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\Xml\Property\LocalHref;
|
|
|
|
use Sabre\DAVACL\IPrincipal;
|
2019-07-30 13:58:35 +03:00
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
use Sabre\VObject\Component;
|
|
|
|
use Sabre\VObject\Component\VCalendar;
|
|
|
|
use Sabre\VObject\Component\VEvent;
|
|
|
|
use Sabre\VObject\DateTimeParser;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Sabre\VObject\FreeBusyGenerator;
|
2019-07-30 13:58:35 +03:00
|
|
|
use Sabre\VObject\ITip;
|
|
|
|
use Sabre\VObject\Parameter;
|
|
|
|
use Sabre\VObject\Property;
|
|
|
|
use Sabre\VObject\Reader;
|
2020-03-08 19:33:27 +03:00
|
|
|
use function \Sabre\Uri\split;
|
2016-11-15 19:01:40 +03:00
|
|
|
|
2016-11-04 17:05:15 +03:00
|
|
|
class Plugin extends \Sabre\CalDAV\Schedule\Plugin {
|
|
|
|
|
2020-03-08 19:33:27 +03:00
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2019-07-30 13:58:35 +03:00
|
|
|
/** @var ITip\Message[] */
|
|
|
|
private $schedulingResponses = [];
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
private $pathOfCalendarObjectChange = null;
|
|
|
|
|
2020-03-08 19:33:27 +03:00
|
|
|
public const CALENDAR_USER_TYPE = '{' . self::NS_CALDAV . '}calendar-user-type';
|
|
|
|
public const SCHEDULE_DEFAULT_CALENDAR_URL = '{' . Plugin::NS_CALDAV . '}schedule-default-calendar-URL';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct(IConfig $config) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:01:40 +03:00
|
|
|
/**
|
|
|
|
* Initializes the plugin
|
|
|
|
*
|
|
|
|
* @param Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function initialize(Server $server) {
|
2016-11-15 19:01:40 +03:00
|
|
|
parent::initialize($server);
|
|
|
|
$server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90);
|
2019-07-30 13:58:35 +03:00
|
|
|
$server->on('afterWriteContent', [$this, 'dispatchSchedulingResponses']);
|
|
|
|
$server->on('afterCreateFile', [$this, 'dispatchSchedulingResponses']);
|
2016-11-15 19:01:40 +03:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:39:52 +03:00
|
|
|
/**
|
|
|
|
* This method handler is invoked during fetching of properties.
|
|
|
|
*
|
|
|
|
* We use this event to add calendar-auto-schedule-specific properties.
|
|
|
|
*
|
|
|
|
* @param PropFind $propFind
|
|
|
|
* @param INode $node
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function propFind(PropFind $propFind, INode $node) {
|
2019-07-26 16:04:58 +03:00
|
|
|
if ($node instanceof IPrincipal) {
|
|
|
|
// overwrite Sabre/Dav's implementation
|
2020-03-08 19:33:27 +03:00
|
|
|
$propFind->handle(self::CALENDAR_USER_TYPE, function () use ($node) {
|
2019-07-26 16:04:58 +03:00
|
|
|
if ($node instanceof IProperties) {
|
2020-03-08 19:33:27 +03:00
|
|
|
$props = $node->getProperties([self::CALENDAR_USER_TYPE]);
|
2018-10-16 15:39:52 +03:00
|
|
|
|
2020-03-08 19:33:27 +03:00
|
|
|
if (isset($props[self::CALENDAR_USER_TYPE])) {
|
|
|
|
return $props[self::CALENDAR_USER_TYPE];
|
2019-07-26 16:04:58 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:39:52 +03:00
|
|
|
|
2019-07-26 16:04:58 +03:00
|
|
|
return 'INDIVIDUAL';
|
|
|
|
});
|
|
|
|
}
|
2019-07-30 13:58:35 +03:00
|
|
|
|
|
|
|
parent::propFind($propFind, $node);
|
2018-10-16 15:39:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-04 17:05:15 +03:00
|
|
|
/**
|
|
|
|
* Returns a list of addresses that are associated with a principal.
|
|
|
|
*
|
|
|
|
* @param string $principal
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getAddressesForPrincipal($principal) {
|
|
|
|
$result = parent::getAddressesForPrincipal($principal);
|
|
|
|
|
|
|
|
if ($result === null) {
|
|
|
|
$result = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2016-11-15 19:01:40 +03:00
|
|
|
|
2019-07-30 13:58:35 +03:00
|
|
|
/**
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
* @param VCalendar $vCal
|
|
|
|
* @param mixed $calendarPath
|
|
|
|
* @param mixed $modified
|
|
|
|
* @param mixed $isNew
|
|
|
|
*/
|
|
|
|
public function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew) {
|
|
|
|
// Save the first path we get as a calendar-object-change request
|
|
|
|
if (!$this->pathOfCalendarObjectChange) {
|
|
|
|
$this->pathOfCalendarObjectChange = $request->getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::calendarObjectChange($request, $response, $vCal, $calendarPath, $modified, $isNew);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function scheduleLocalDelivery(ITip\Message $iTipMessage):void {
|
|
|
|
parent::scheduleLocalDelivery($iTipMessage);
|
|
|
|
|
|
|
|
// We only care when the message was successfully delivered locally
|
|
|
|
if ($iTipMessage->scheduleStatus !== '1.2;Message delivered locally') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only care about request. reply and cancel are properly handled
|
|
|
|
// by parent::scheduleLocalDelivery already
|
|
|
|
if (strcasecmp($iTipMessage->method, 'REQUEST') !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If parent::scheduleLocalDelivery set scheduleStatus to 1.2,
|
|
|
|
// it means that it was successfully delivered locally.
|
|
|
|
// Meaning that the ACL plugin is loaded and that a principial
|
|
|
|
// exists for the given recipient id, no need to double check
|
|
|
|
/** @var \Sabre\DAVACL\Plugin $aclPlugin */
|
|
|
|
$aclPlugin = $this->server->getPlugin('acl');
|
|
|
|
$principalUri = $aclPlugin->getPrincipalByUri($iTipMessage->recipient);
|
|
|
|
$calendarUserType = $this->getCalendarUserTypeForPrincipal($principalUri);
|
|
|
|
if (strcasecmp($calendarUserType, 'ROOM') !== 0 && strcasecmp($calendarUserType, 'RESOURCE') !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attendee = $this->getCurrentAttendee($iTipMessage);
|
|
|
|
if (!$attendee) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only respond when a response was actually requested
|
|
|
|
$rsvp = $this->getAttendeeRSVP($attendee);
|
|
|
|
if (!$rsvp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($iTipMessage->message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vcalendar = $iTipMessage->message;
|
|
|
|
if (!isset($vcalendar->VEVENT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Component $vevent */
|
|
|
|
$vevent = $vcalendar->VEVENT;
|
|
|
|
|
|
|
|
// We don't support autoresponses for recurrencing events for now
|
|
|
|
if (isset($vevent->RRULE) || isset($vevent->RDATE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dtstart = $vevent->DTSTART;
|
2020-10-05 16:12:57 +03:00
|
|
|
$dtend = $this->getDTEndFromVEvent($vevent);
|
2019-07-30 13:58:35 +03:00
|
|
|
$uid = $vevent->UID->getValue();
|
|
|
|
$sequence = isset($vevent->SEQUENCE) ? $vevent->SEQUENCE->getValue() : 0;
|
|
|
|
$recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ? $vevent->{'RECURRENCE-ID'}->serialize() : '';
|
|
|
|
|
|
|
|
$message = <<<EOF
|
|
|
|
BEGIN:VCALENDAR
|
|
|
|
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
|
|
|
|
METHOD:REPLY
|
|
|
|
VERSION:2.0
|
|
|
|
BEGIN:VEVENT
|
|
|
|
ATTENDEE;PARTSTAT=%s:%s
|
|
|
|
ORGANIZER:%s
|
|
|
|
UID:%s
|
|
|
|
SEQUENCE:%s
|
|
|
|
REQUEST-STATUS:2.0;Success
|
|
|
|
%sEND:VEVENT
|
|
|
|
END:VCALENDAR
|
|
|
|
EOF;
|
|
|
|
|
|
|
|
if ($this->isAvailableAtTime($attendee->getValue(), $dtstart->getDateTime(), $dtend->getDateTime(), $uid)) {
|
|
|
|
$partStat = 'ACCEPTED';
|
|
|
|
} else {
|
|
|
|
$partStat = 'DECLINED';
|
|
|
|
}
|
|
|
|
|
|
|
|
$vObject = Reader::read(vsprintf($message, [
|
|
|
|
$partStat,
|
|
|
|
$iTipMessage->recipient,
|
|
|
|
$iTipMessage->sender,
|
|
|
|
$uid,
|
|
|
|
$sequence,
|
|
|
|
$recurrenceId
|
|
|
|
]));
|
|
|
|
|
|
|
|
$responseITipMessage = new ITip\Message();
|
|
|
|
$responseITipMessage->uid = $uid;
|
|
|
|
$responseITipMessage->component = 'VEVENT';
|
|
|
|
$responseITipMessage->method = 'REPLY';
|
|
|
|
$responseITipMessage->sequence = $sequence;
|
|
|
|
$responseITipMessage->sender = $iTipMessage->recipient;
|
|
|
|
$responseITipMessage->recipient = $iTipMessage->sender;
|
|
|
|
$responseITipMessage->message = $vObject;
|
|
|
|
|
|
|
|
// We can't dispatch them now already, because the organizers calendar-object
|
|
|
|
// was not yet created. Hence Sabre/DAV won't find a calendar-object, when we
|
|
|
|
// send our reply.
|
|
|
|
$this->schedulingResponses[] = $responseITipMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function dispatchSchedulingResponses(string $uri):void {
|
|
|
|
if ($uri !== $this->pathOfCalendarObjectChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->schedulingResponses as $schedulingResponse) {
|
|
|
|
$this->scheduleLocalDelivery($schedulingResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:01:40 +03:00
|
|
|
/**
|
|
|
|
* Always use the personal calendar as target for scheduled events
|
|
|
|
*
|
|
|
|
* @param PropFind $propFind
|
|
|
|
* @param INode $node
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
|
2016-11-15 19:01:40 +03:00
|
|
|
if ($node instanceof IPrincipal) {
|
2020-03-08 19:33:27 +03:00
|
|
|
$propFind->handle(self::SCHEDULE_DEFAULT_CALENDAR_URL, function () use ($node) {
|
2016-11-15 19:01:40 +03:00
|
|
|
/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
|
|
|
|
$caldavPlugin = $this->server->getPlugin('caldav');
|
|
|
|
$principalUrl = $node->getPrincipalUrl();
|
|
|
|
|
|
|
|
$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
|
|
|
|
if (!$calendarHomePath) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
if (strpos($principalUrl, 'principals/users') === 0) {
|
2021-01-12 12:15:48 +03:00
|
|
|
[, $userId] = split($principalUrl);
|
2020-03-08 19:33:27 +03:00
|
|
|
$uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
|
|
|
|
$displayName = CalDavBackend::PERSONAL_CALENDAR_NAME;
|
2018-05-28 21:12:13 +03:00
|
|
|
} elseif (strpos($principalUrl, 'principals/calendar-resources') === 0 ||
|
|
|
|
strpos($principalUrl, 'principals/calendar-rooms') === 0) {
|
|
|
|
$uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI;
|
2020-03-08 19:33:27 +03:00
|
|
|
$displayName = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME;
|
2018-05-28 21:12:13 +03:00
|
|
|
} else {
|
|
|
|
// How did we end up here?
|
|
|
|
// TODO - throw exception or just ignore?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:01:40 +03:00
|
|
|
/** @var CalendarHome $calendarHome */
|
|
|
|
$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
|
2018-05-28 21:12:13 +03:00
|
|
|
if (!$calendarHome->childExists($uri)) {
|
|
|
|
$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [
|
2020-03-08 19:33:27 +03:00
|
|
|
'{DAV:}displayname' => $displayName,
|
2016-11-15 19:01:40 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:12:13 +03:00
|
|
|
$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);
|
2016-11-15 19:01:40 +03:00
|
|
|
if (empty($result)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LocalHref($result[0]['href']);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 13:58:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of addresses that are associated with a principal.
|
|
|
|
*
|
|
|
|
* @param string $principal
|
2020-08-14 16:10:48 +03:00
|
|
|
* @return string|null
|
2019-07-30 13:58:35 +03:00
|
|
|
*/
|
|
|
|
protected function getCalendarUserTypeForPrincipal($principal):?string {
|
|
|
|
$calendarUserType = '{' . self::NS_CALDAV . '}calendar-user-type';
|
|
|
|
$properties = $this->server->getProperties(
|
|
|
|
$principal,
|
|
|
|
[$calendarUserType]
|
|
|
|
);
|
|
|
|
|
|
|
|
// If we can't find this information, we'll stop processing
|
|
|
|
if (!isset($properties[$calendarUserType])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $properties[$calendarUserType];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ITip\Message $iTipMessage
|
|
|
|
* @return null|Property
|
|
|
|
*/
|
|
|
|
private function getCurrentAttendee(ITip\Message $iTipMessage):?Property {
|
|
|
|
/** @var VEvent $vevent */
|
|
|
|
$vevent = $iTipMessage->message->VEVENT;
|
|
|
|
$attendees = $vevent->select('ATTENDEE');
|
|
|
|
foreach ($attendees as $attendee) {
|
|
|
|
/** @var Property $attendee */
|
|
|
|
if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
|
|
|
|
return $attendee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Property|null $attendee
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function getAttendeeRSVP(Property $attendee = null):bool {
|
|
|
|
if ($attendee !== null) {
|
|
|
|
$rsvp = $attendee->offsetGet('RSVP');
|
|
|
|
if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// RFC 5545 3.2.17: default RSVP is false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param VEvent $vevent
|
|
|
|
* @return Property\ICalendar\DateTime
|
|
|
|
*/
|
|
|
|
private function getDTEndFromVEvent(VEvent $vevent):Property\ICalendar\DateTime {
|
|
|
|
if (isset($vevent->DTEND)) {
|
|
|
|
return $vevent->DTEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vevent->DURATION)) {
|
|
|
|
$isFloating = $vevent->DTSTART->isFloating();
|
|
|
|
/** @var Property\ICalendar\DateTime $end */
|
|
|
|
$end = clone $vevent->DTSTART;
|
|
|
|
$endDateTime = $end->getDateTime();
|
|
|
|
$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
|
|
|
|
$end->setDateTime($endDateTime, $isFloating);
|
|
|
|
return $end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$vevent->DTSTART->hasTime()) {
|
|
|
|
$isFloating = $vevent->DTSTART->isFloating();
|
|
|
|
/** @var Property\ICalendar\DateTime $end */
|
|
|
|
$end = clone $vevent->DTSTART;
|
|
|
|
$endDateTime = $end->getDateTime();
|
|
|
|
$endDateTime = $endDateTime->modify('+1 day');
|
|
|
|
$end->setDateTime($endDateTime, $isFloating);
|
|
|
|
return $end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone $vevent->DTSTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $email
|
|
|
|
* @param \DateTimeInterface $start
|
|
|
|
* @param \DateTimeInterface $end
|
|
|
|
* @param string $ignoreUID
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isAvailableAtTime(string $email, \DateTimeInterface $start, \DateTimeInterface $end, string $ignoreUID):bool {
|
|
|
|
// This method is heavily inspired by Sabre\CalDAV\Schedule\Plugin::scheduleLocalDelivery
|
|
|
|
// and Sabre\CalDAV\Schedule\Plugin::getFreeBusyForEmail
|
|
|
|
|
|
|
|
$aclPlugin = $this->server->getPlugin('acl');
|
|
|
|
$this->server->removeListener('propFind', [$aclPlugin, 'propFind']);
|
|
|
|
|
|
|
|
$result = $aclPlugin->principalSearch(
|
|
|
|
['{http://sabredav.org/ns}email-address' => $this->stripOffMailTo($email)],
|
|
|
|
[
|
|
|
|
'{DAV:}principal-URL',
|
|
|
|
'{' . self::NS_CALDAV . '}calendar-home-set',
|
|
|
|
'{' . self::NS_CALDAV . '}schedule-inbox-URL',
|
|
|
|
'{http://sabredav.org/ns}email-address',
|
|
|
|
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->server->on('propFind', [$aclPlugin, 'propFind'], 20);
|
|
|
|
|
|
|
|
|
|
|
|
// Grabbing the calendar list
|
|
|
|
$objects = [];
|
|
|
|
$calendarTimeZone = new DateTimeZone('UTC');
|
|
|
|
|
|
|
|
$homePath = $result[0][200]['{' . self::NS_CALDAV . '}calendar-home-set']->getHref();
|
|
|
|
foreach ($this->server->tree->getNodeForPath($homePath)->getChildren() as $node) {
|
|
|
|
if (!$node instanceof ICalendar) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting the list of object uris within the time-range
|
|
|
|
$urls = $node->calendarQuery([
|
|
|
|
'name' => 'VCALENDAR',
|
|
|
|
'comp-filters' => [
|
|
|
|
[
|
|
|
|
'name' => 'VEVENT',
|
|
|
|
'is-not-defined' => false,
|
|
|
|
'time-range' => [
|
|
|
|
'start' => $start,
|
|
|
|
'end' => $end,
|
|
|
|
],
|
2020-10-05 16:12:57 +03:00
|
|
|
'comp-filters' => [],
|
|
|
|
'prop-filters' => [],
|
2019-07-30 13:58:35 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'VEVENT',
|
|
|
|
'is-not-defined' => false,
|
|
|
|
'time-range' => null,
|
2020-10-05 16:12:57 +03:00
|
|
|
'comp-filters' => [],
|
|
|
|
'prop-filters' => [
|
2019-07-30 13:58:35 +03:00
|
|
|
[
|
2020-10-05 16:12:57 +03:00
|
|
|
'name' => 'UID',
|
2019-07-30 13:58:35 +03:00
|
|
|
'is-not-defined' => false,
|
2020-10-05 16:12:57 +03:00
|
|
|
'time-range' => null,
|
|
|
|
'text-match' => [
|
2019-07-30 13:58:35 +03:00
|
|
|
'value' => $ignoreUID,
|
|
|
|
'negate-condition' => true,
|
|
|
|
'collation' => 'i;octet',
|
|
|
|
],
|
|
|
|
'param-filters' => [],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'prop-filters' => [],
|
|
|
|
'is-not-defined' => false,
|
|
|
|
'time-range' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($urls as $url) {
|
|
|
|
$objects[] = $node->getChild($url)->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$inboxProps = $this->server->getProperties(
|
|
|
|
$result[0][200]['{' . self::NS_CALDAV . '}schedule-inbox-URL']->getHref(),
|
|
|
|
['{' . self::NS_CALDAV . '}calendar-availability']
|
|
|
|
);
|
|
|
|
|
|
|
|
$vcalendar = new VCalendar();
|
|
|
|
$vcalendar->METHOD = 'REPLY';
|
|
|
|
|
|
|
|
$generator = new FreeBusyGenerator();
|
|
|
|
$generator->setObjects($objects);
|
|
|
|
$generator->setTimeRange($start, $end);
|
|
|
|
$generator->setBaseObject($vcalendar);
|
|
|
|
$generator->setTimeZone($calendarTimeZone);
|
|
|
|
|
|
|
|
if (isset($inboxProps['{' . self::NS_CALDAV . '}calendar-availability'])) {
|
|
|
|
$generator->setVAvailability(
|
|
|
|
Reader::read(
|
|
|
|
$inboxProps['{' . self::NS_CALDAV . '}calendar-availability']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $generator->getResult();
|
|
|
|
if (!isset($result->VFREEBUSY)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Component $freeBusyComponent */
|
|
|
|
$freeBusyComponent = $result->VFREEBUSY;
|
|
|
|
$freeBusyProperties = $freeBusyComponent->select('FREEBUSY');
|
|
|
|
// If there is no Free-busy property at all, the time-range is empty and available
|
|
|
|
if (count($freeBusyProperties) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If more than one Free-Busy property was returned, it means that an event
|
|
|
|
// starts or ends inside this time-range, so it's not availabe and we return false
|
|
|
|
if (count($freeBusyProperties) > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Property $freeBusyProperty */
|
|
|
|
$freeBusyProperty = $freeBusyProperties[0];
|
|
|
|
if (!$freeBusyProperty->offsetExists('FBTYPE')) {
|
|
|
|
// If there is no FBTYPE, it means it's busy
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fbTypeParameter = $freeBusyProperty->offsetGet('FBTYPE');
|
|
|
|
if (!($fbTypeParameter instanceof Parameter)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (strcasecmp($fbTypeParameter->getValue(), 'FREE') === 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $email
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function stripOffMailTo(string $email): string {
|
2020-10-05 16:12:57 +03:00
|
|
|
if (stripos($email, 'mailto:') === 0) {
|
2019-07-30 13:58:35 +03:00
|
|
|
return substr($email, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $email;
|
|
|
|
}
|
2016-11-04 17:05:15 +03:00
|
|
|
}
|