1
0

Plugin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. * @author Richard Steinmetz <richard@steinmetz.cloud>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OCA\DAV\CalDAV\Schedule;
  31. use DateTimeZone;
  32. use OCA\DAV\CalDAV\CalDavBackend;
  33. use OCA\DAV\CalDAV\Calendar;
  34. use OCA\DAV\CalDAV\CalendarHome;
  35. use OCP\IConfig;
  36. use Sabre\CalDAV\ICalendar;
  37. use Sabre\DAV\INode;
  38. use Sabre\DAV\IProperties;
  39. use Sabre\DAV\PropFind;
  40. use Sabre\DAV\Server;
  41. use Sabre\DAV\Xml\Property\LocalHref;
  42. use Sabre\DAVACL\IPrincipal;
  43. use Sabre\HTTP\RequestInterface;
  44. use Sabre\HTTP\ResponseInterface;
  45. use Sabre\VObject\Component;
  46. use Sabre\VObject\Component\VCalendar;
  47. use Sabre\VObject\Component\VEvent;
  48. use Sabre\VObject\DateTimeParser;
  49. use Sabre\VObject\Document;
  50. use Sabre\VObject\FreeBusyGenerator;
  51. use Sabre\VObject\ITip;
  52. use Sabre\VObject\Parameter;
  53. use Sabre\VObject\Property;
  54. use Sabre\VObject\Reader;
  55. use function \Sabre\Uri\split;
  56. class Plugin extends \Sabre\CalDAV\Schedule\Plugin {
  57. /**
  58. * @var IConfig
  59. */
  60. private $config;
  61. /** @var ITip\Message[] */
  62. private $schedulingResponses = [];
  63. /** @var string|null */
  64. private $pathOfCalendarObjectChange = null;
  65. public const CALENDAR_USER_TYPE = '{' . self::NS_CALDAV . '}calendar-user-type';
  66. public const SCHEDULE_DEFAULT_CALENDAR_URL = '{' . Plugin::NS_CALDAV . '}schedule-default-calendar-URL';
  67. /**
  68. * @param IConfig $config
  69. */
  70. public function __construct(IConfig $config) {
  71. $this->config = $config;
  72. }
  73. /**
  74. * Initializes the plugin
  75. *
  76. * @param Server $server
  77. * @return void
  78. */
  79. public function initialize(Server $server) {
  80. parent::initialize($server);
  81. $server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90);
  82. $server->on('afterWriteContent', [$this, 'dispatchSchedulingResponses']);
  83. $server->on('afterCreateFile', [$this, 'dispatchSchedulingResponses']);
  84. }
  85. /**
  86. * Allow manual setting of the object change URL
  87. * to support public write
  88. *
  89. * @param string $path
  90. */
  91. public function setPathOfCalendarObjectChange(string $path): void {
  92. $this->pathOfCalendarObjectChange = $path;
  93. }
  94. /**
  95. * This method handler is invoked during fetching of properties.
  96. *
  97. * We use this event to add calendar-auto-schedule-specific properties.
  98. *
  99. * @param PropFind $propFind
  100. * @param INode $node
  101. * @return void
  102. */
  103. public function propFind(PropFind $propFind, INode $node) {
  104. if ($node instanceof IPrincipal) {
  105. // overwrite Sabre/Dav's implementation
  106. $propFind->handle(self::CALENDAR_USER_TYPE, function () use ($node) {
  107. if ($node instanceof IProperties) {
  108. $props = $node->getProperties([self::CALENDAR_USER_TYPE]);
  109. if (isset($props[self::CALENDAR_USER_TYPE])) {
  110. return $props[self::CALENDAR_USER_TYPE];
  111. }
  112. }
  113. return 'INDIVIDUAL';
  114. });
  115. }
  116. parent::propFind($propFind, $node);
  117. }
  118. /**
  119. * Returns a list of addresses that are associated with a principal.
  120. *
  121. * @param string $principal
  122. * @return array
  123. */
  124. protected function getAddressesForPrincipal($principal) {
  125. $result = parent::getAddressesForPrincipal($principal);
  126. if ($result === null) {
  127. $result = [];
  128. }
  129. return $result;
  130. }
  131. /**
  132. * @param RequestInterface $request
  133. * @param ResponseInterface $response
  134. * @param VCalendar $vCal
  135. * @param mixed $calendarPath
  136. * @param mixed $modified
  137. * @param mixed $isNew
  138. */
  139. public function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew) {
  140. // Save the first path we get as a calendar-object-change request
  141. if (!$this->pathOfCalendarObjectChange) {
  142. $this->pathOfCalendarObjectChange = $request->getPath();
  143. }
  144. parent::calendarObjectChange($request, $response, $vCal, $calendarPath, $modified, $isNew);
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. public function scheduleLocalDelivery(ITip\Message $iTipMessage):void {
  150. /** @var Component|null $vevent */
  151. $vevent = $iTipMessage->message->VEVENT ?? null;
  152. // Strip VALARMs from incoming VEVENT
  153. if ($vevent && isset($vevent->VALARM)) {
  154. $vevent->remove('VALARM');
  155. }
  156. parent::scheduleLocalDelivery($iTipMessage);
  157. // We only care when the message was successfully delivered locally
  158. if ($iTipMessage->scheduleStatus !== '1.2;Message delivered locally') {
  159. return;
  160. }
  161. // We only care about request. reply and cancel are properly handled
  162. // by parent::scheduleLocalDelivery already
  163. if (strcasecmp($iTipMessage->method, 'REQUEST') !== 0) {
  164. return;
  165. }
  166. // If parent::scheduleLocalDelivery set scheduleStatus to 1.2,
  167. // it means that it was successfully delivered locally.
  168. // Meaning that the ACL plugin is loaded and that a principal
  169. // exists for the given recipient id, no need to double check
  170. /** @var \Sabre\DAVACL\Plugin $aclPlugin */
  171. $aclPlugin = $this->server->getPlugin('acl');
  172. $principalUri = $aclPlugin->getPrincipalByUri($iTipMessage->recipient);
  173. $calendarUserType = $this->getCalendarUserTypeForPrincipal($principalUri);
  174. if (strcasecmp($calendarUserType, 'ROOM') !== 0 && strcasecmp($calendarUserType, 'RESOURCE') !== 0) {
  175. return;
  176. }
  177. $attendee = $this->getCurrentAttendee($iTipMessage);
  178. if (!$attendee) {
  179. return;
  180. }
  181. // We only respond when a response was actually requested
  182. $rsvp = $this->getAttendeeRSVP($attendee);
  183. if (!$rsvp) {
  184. return;
  185. }
  186. if (!$vevent) {
  187. return;
  188. }
  189. // We don't support autoresponses for recurrencing events for now
  190. if (isset($vevent->RRULE) || isset($vevent->RDATE)) {
  191. return;
  192. }
  193. $dtstart = $vevent->DTSTART;
  194. $dtend = $this->getDTEndFromVEvent($vevent);
  195. $uid = $vevent->UID->getValue();
  196. $sequence = isset($vevent->SEQUENCE) ? $vevent->SEQUENCE->getValue() : 0;
  197. $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ? $vevent->{'RECURRENCE-ID'}->serialize() : '';
  198. $message = <<<EOF
  199. BEGIN:VCALENDAR
  200. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  201. METHOD:REPLY
  202. VERSION:2.0
  203. BEGIN:VEVENT
  204. ATTENDEE;PARTSTAT=%s:%s
  205. ORGANIZER:%s
  206. UID:%s
  207. SEQUENCE:%s
  208. REQUEST-STATUS:2.0;Success
  209. %sEND:VEVENT
  210. END:VCALENDAR
  211. EOF;
  212. if ($this->isAvailableAtTime($attendee->getValue(), $dtstart->getDateTime(), $dtend->getDateTime(), $uid)) {
  213. $partStat = 'ACCEPTED';
  214. } else {
  215. $partStat = 'DECLINED';
  216. }
  217. $vObject = Reader::read(vsprintf($message, [
  218. $partStat,
  219. $iTipMessage->recipient,
  220. $iTipMessage->sender,
  221. $uid,
  222. $sequence,
  223. $recurrenceId
  224. ]));
  225. $responseITipMessage = new ITip\Message();
  226. $responseITipMessage->uid = $uid;
  227. $responseITipMessage->component = 'VEVENT';
  228. $responseITipMessage->method = 'REPLY';
  229. $responseITipMessage->sequence = $sequence;
  230. $responseITipMessage->sender = $iTipMessage->recipient;
  231. $responseITipMessage->recipient = $iTipMessage->sender;
  232. $responseITipMessage->message = $vObject;
  233. // We can't dispatch them now already, because the organizers calendar-object
  234. // was not yet created. Hence Sabre/DAV won't find a calendar-object, when we
  235. // send our reply.
  236. $this->schedulingResponses[] = $responseITipMessage;
  237. }
  238. /**
  239. * @param string $uri
  240. */
  241. public function dispatchSchedulingResponses(string $uri):void {
  242. if ($uri !== $this->pathOfCalendarObjectChange) {
  243. return;
  244. }
  245. foreach ($this->schedulingResponses as $schedulingResponse) {
  246. $this->scheduleLocalDelivery($schedulingResponse);
  247. }
  248. }
  249. /**
  250. * Always use the personal calendar as target for scheduled events
  251. *
  252. * @param PropFind $propFind
  253. * @param INode $node
  254. * @return void
  255. */
  256. public function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
  257. if ($node instanceof IPrincipal) {
  258. $propFind->handle(self::SCHEDULE_DEFAULT_CALENDAR_URL, function () use ($node) {
  259. /** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
  260. $caldavPlugin = $this->server->getPlugin('caldav');
  261. $principalUrl = $node->getPrincipalUrl();
  262. $calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
  263. if (!$calendarHomePath) {
  264. return null;
  265. }
  266. $isResourceOrRoom = strpos($principalUrl, 'principals/calendar-resources') === 0 ||
  267. strpos($principalUrl, 'principals/calendar-rooms') === 0;
  268. if (strpos($principalUrl, 'principals/users') === 0) {
  269. [, $userId] = split($principalUrl);
  270. $uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
  271. $displayName = CalDavBackend::PERSONAL_CALENDAR_NAME;
  272. } elseif ($isResourceOrRoom) {
  273. $uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI;
  274. $displayName = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME;
  275. } else {
  276. // How did we end up here?
  277. // TODO - throw exception or just ignore?
  278. return null;
  279. }
  280. /** @var CalendarHome $calendarHome */
  281. $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
  282. if (!$calendarHome->childExists($uri)) {
  283. // If the default calendar doesn't exist
  284. if ($isResourceOrRoom) {
  285. $calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [
  286. '{DAV:}displayname' => $displayName,
  287. ]);
  288. } else {
  289. // And we're not handling scheduling on resource/room booking
  290. $userCalendars = [];
  291. /**
  292. * If the default calendar of the user isn't set and the
  293. * fallback doesn't match any of the user's calendar
  294. * try to find the first "personal" calendar we can write to
  295. * instead of creating a new one.
  296. * A appropriate personal calendar to receive invites:
  297. * - isn't a calendar subscription
  298. * - user can write to it (no virtual/3rd-party calendars)
  299. * - calendar isn't a share
  300. */
  301. foreach ($calendarHome->getChildren() as $node) {
  302. if ($node instanceof Calendar && !$node->isSubscription() && $node->canWrite() && !$node->isShared() && !$node->isDeleted()) {
  303. $userCalendars[] = $node;
  304. }
  305. }
  306. if (count($userCalendars) > 0) {
  307. // Calendar backend returns calendar by calendarorder property
  308. $uri = $userCalendars[0]->getName();
  309. } else {
  310. // Otherwise if we have really nothing, create a new calendar
  311. $calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [
  312. '{DAV:}displayname' => $displayName,
  313. ]);
  314. }
  315. }
  316. }
  317. $result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);
  318. if (empty($result)) {
  319. return null;
  320. }
  321. return new LocalHref($result[0]['href']);
  322. });
  323. }
  324. }
  325. /**
  326. * Returns a list of addresses that are associated with a principal.
  327. *
  328. * @param string $principal
  329. * @return string|null
  330. */
  331. protected function getCalendarUserTypeForPrincipal($principal):?string {
  332. $calendarUserType = '{' . self::NS_CALDAV . '}calendar-user-type';
  333. $properties = $this->server->getProperties(
  334. $principal,
  335. [$calendarUserType]
  336. );
  337. // If we can't find this information, we'll stop processing
  338. if (!isset($properties[$calendarUserType])) {
  339. return null;
  340. }
  341. return $properties[$calendarUserType];
  342. }
  343. /**
  344. * @param ITip\Message $iTipMessage
  345. * @return null|Property
  346. */
  347. private function getCurrentAttendee(ITip\Message $iTipMessage):?Property {
  348. /** @var VEvent $vevent */
  349. $vevent = $iTipMessage->message->VEVENT;
  350. $attendees = $vevent->select('ATTENDEE');
  351. foreach ($attendees as $attendee) {
  352. /** @var Property $attendee */
  353. if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
  354. return $attendee;
  355. }
  356. }
  357. return null;
  358. }
  359. /**
  360. * @param Property|null $attendee
  361. * @return bool
  362. */
  363. private function getAttendeeRSVP(Property $attendee = null):bool {
  364. if ($attendee !== null) {
  365. $rsvp = $attendee->offsetGet('RSVP');
  366. if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
  367. return true;
  368. }
  369. }
  370. // RFC 5545 3.2.17: default RSVP is false
  371. return false;
  372. }
  373. /**
  374. * @param VEvent $vevent
  375. * @return Property\ICalendar\DateTime
  376. */
  377. private function getDTEndFromVEvent(VEvent $vevent):Property\ICalendar\DateTime {
  378. if (isset($vevent->DTEND)) {
  379. return $vevent->DTEND;
  380. }
  381. if (isset($vevent->DURATION)) {
  382. $isFloating = $vevent->DTSTART->isFloating();
  383. /** @var Property\ICalendar\DateTime $end */
  384. $end = clone $vevent->DTSTART;
  385. $endDateTime = $end->getDateTime();
  386. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  387. $end->setDateTime($endDateTime, $isFloating);
  388. return $end;
  389. }
  390. if (!$vevent->DTSTART->hasTime()) {
  391. $isFloating = $vevent->DTSTART->isFloating();
  392. /** @var Property\ICalendar\DateTime $end */
  393. $end = clone $vevent->DTSTART;
  394. $endDateTime = $end->getDateTime();
  395. $endDateTime = $endDateTime->modify('+1 day');
  396. $end->setDateTime($endDateTime, $isFloating);
  397. return $end;
  398. }
  399. return clone $vevent->DTSTART;
  400. }
  401. /**
  402. * @param string $email
  403. * @param \DateTimeInterface $start
  404. * @param \DateTimeInterface $end
  405. * @param string $ignoreUID
  406. * @return bool
  407. */
  408. private function isAvailableAtTime(string $email, \DateTimeInterface $start, \DateTimeInterface $end, string $ignoreUID):bool {
  409. // This method is heavily inspired by Sabre\CalDAV\Schedule\Plugin::scheduleLocalDelivery
  410. // and Sabre\CalDAV\Schedule\Plugin::getFreeBusyForEmail
  411. $aclPlugin = $this->server->getPlugin('acl');
  412. $this->server->removeListener('propFind', [$aclPlugin, 'propFind']);
  413. $result = $aclPlugin->principalSearch(
  414. ['{http://sabredav.org/ns}email-address' => $this->stripOffMailTo($email)],
  415. [
  416. '{DAV:}principal-URL',
  417. '{' . self::NS_CALDAV . '}calendar-home-set',
  418. '{' . self::NS_CALDAV . '}schedule-inbox-URL',
  419. '{http://sabredav.org/ns}email-address',
  420. ]
  421. );
  422. $this->server->on('propFind', [$aclPlugin, 'propFind'], 20);
  423. // Grabbing the calendar list
  424. $objects = [];
  425. $calendarTimeZone = new DateTimeZone('UTC');
  426. $homePath = $result[0][200]['{' . self::NS_CALDAV . '}calendar-home-set']->getHref();
  427. foreach ($this->server->tree->getNodeForPath($homePath)->getChildren() as $node) {
  428. if (!$node instanceof ICalendar) {
  429. continue;
  430. }
  431. // Getting the list of object uris within the time-range
  432. $urls = $node->calendarQuery([
  433. 'name' => 'VCALENDAR',
  434. 'comp-filters' => [
  435. [
  436. 'name' => 'VEVENT',
  437. 'is-not-defined' => false,
  438. 'time-range' => [
  439. 'start' => $start,
  440. 'end' => $end,
  441. ],
  442. 'comp-filters' => [],
  443. 'prop-filters' => [],
  444. ],
  445. [
  446. 'name' => 'VEVENT',
  447. 'is-not-defined' => false,
  448. 'time-range' => null,
  449. 'comp-filters' => [],
  450. 'prop-filters' => [
  451. [
  452. 'name' => 'UID',
  453. 'is-not-defined' => false,
  454. 'time-range' => null,
  455. 'text-match' => [
  456. 'value' => $ignoreUID,
  457. 'negate-condition' => true,
  458. 'collation' => 'i;octet',
  459. ],
  460. 'param-filters' => [],
  461. ],
  462. ]
  463. ],
  464. ],
  465. 'prop-filters' => [],
  466. 'is-not-defined' => false,
  467. 'time-range' => null,
  468. ]);
  469. foreach ($urls as $url) {
  470. $objects[] = $node->getChild($url)->get();
  471. }
  472. }
  473. $inboxProps = $this->server->getProperties(
  474. $result[0][200]['{' . self::NS_CALDAV . '}schedule-inbox-URL']->getHref(),
  475. ['{' . self::NS_CALDAV . '}calendar-availability']
  476. );
  477. $vcalendar = new VCalendar();
  478. $vcalendar->METHOD = 'REPLY';
  479. $generator = new FreeBusyGenerator();
  480. $generator->setObjects($objects);
  481. $generator->setTimeRange($start, $end);
  482. $generator->setBaseObject($vcalendar);
  483. $generator->setTimeZone($calendarTimeZone);
  484. if (isset($inboxProps['{' . self::NS_CALDAV . '}calendar-availability'])) {
  485. $generator->setVAvailability(
  486. Reader::read(
  487. $inboxProps['{' . self::NS_CALDAV . '}calendar-availability']
  488. )
  489. );
  490. }
  491. $result = $generator->getResult();
  492. if (!isset($result->VFREEBUSY)) {
  493. return false;
  494. }
  495. /** @var Component $freeBusyComponent */
  496. $freeBusyComponent = $result->VFREEBUSY;
  497. $freeBusyProperties = $freeBusyComponent->select('FREEBUSY');
  498. // If there is no Free-busy property at all, the time-range is empty and available
  499. if (count($freeBusyProperties) === 0) {
  500. return true;
  501. }
  502. // If more than one Free-Busy property was returned, it means that an event
  503. // starts or ends inside this time-range, so it's not available and we return false
  504. if (count($freeBusyProperties) > 1) {
  505. return false;
  506. }
  507. /** @var Property $freeBusyProperty */
  508. $freeBusyProperty = $freeBusyProperties[0];
  509. if (!$freeBusyProperty->offsetExists('FBTYPE')) {
  510. // If there is no FBTYPE, it means it's busy
  511. return false;
  512. }
  513. $fbTypeParameter = $freeBusyProperty->offsetGet('FBTYPE');
  514. if (!($fbTypeParameter instanceof Parameter)) {
  515. return false;
  516. }
  517. return (strcasecmp($fbTypeParameter->getValue(), 'FREE') === 0);
  518. }
  519. /**
  520. * @param string $email
  521. * @return string
  522. */
  523. private function stripOffMailTo(string $email): string {
  524. if (stripos($email, 'mailto:') === 0) {
  525. return substr($email, 7);
  526. }
  527. return $email;
  528. }
  529. }