CalendarImpl.php 8.0 KB

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