1
0

CalendarImpl.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if ($this->calendarInfo['principaluri'] !== $permission['principal']) {
  102. continue;
  103. }
  104. switch ($permission['privilege']) {
  105. case '{DAV:}read':
  106. $result |= Constants::PERMISSION_READ;
  107. break;
  108. case '{DAV:}write':
  109. $result |= Constants::PERMISSION_CREATE;
  110. $result |= Constants::PERMISSION_UPDATE;
  111. break;
  112. case '{DAV:}all':
  113. $result |= Constants::PERMISSION_ALL;
  114. break;
  115. }
  116. }
  117. return $result;
  118. }
  119. /**
  120. * @since 31.0.0
  121. */
  122. public function isWritable(): bool {
  123. return $this->calendar->canWrite();
  124. }
  125. /**
  126. * @since 26.0.0
  127. */
  128. public function isDeleted(): bool {
  129. return $this->calendar->isDeleted();
  130. }
  131. /**
  132. * @since 31.0.0
  133. */
  134. public function isShared(): bool {
  135. return $this->calendar->isShared();
  136. }
  137. /**
  138. * Create a new calendar event for this calendar
  139. * by way of an ICS string
  140. *
  141. * @param string $name the file name - needs to contain the .ics ending
  142. * @param string $calendarData a string containing a valid VEVENT ics
  143. *
  144. * @throws CalendarException
  145. */
  146. public function createFromString(string $name, string $calendarData): void {
  147. $server = new InvitationResponseServer(false);
  148. /** @var CustomPrincipalPlugin $plugin */
  149. $plugin = $server->getServer()->getPlugin('auth');
  150. // we're working around the previous implementation
  151. // that only allowed the public system principal to be used
  152. // so set the custom principal here
  153. $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());
  154. if (empty($this->calendarInfo['uri'])) {
  155. throw new CalendarException('Could not write to calendar as URI parameter is missing');
  156. }
  157. // Build full calendar path
  158. [, $user] = uriSplit($this->calendar->getPrincipalURI());
  159. $fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name);
  160. // Force calendar change URI
  161. /** @var Schedule\Plugin $schedulingPlugin */
  162. $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
  163. $schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename);
  164. $stream = fopen('php://memory', 'rb+');
  165. fwrite($stream, $calendarData);
  166. rewind($stream);
  167. try {
  168. $server->getServer()->createFile($fullCalendarFilename, $stream);
  169. } catch (Conflict $e) {
  170. throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e);
  171. } finally {
  172. fclose($stream);
  173. }
  174. }
  175. /**
  176. * @throws CalendarException
  177. */
  178. public function handleIMipMessage(string $name, string $calendarData): void {
  179. $server = $this->getInvitationResponseServer();
  180. /** @var CustomPrincipalPlugin $plugin */
  181. $plugin = $server->getServer()->getPlugin('auth');
  182. // we're working around the previous implementation
  183. // that only allowed the public system principal to be used
  184. // so set the custom principal here
  185. $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());
  186. if (empty($this->calendarInfo['uri'])) {
  187. throw new CalendarException('Could not write to calendar as URI parameter is missing');
  188. }
  189. // Force calendar change URI
  190. /** @var Schedule\Plugin $schedulingPlugin */
  191. $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
  192. // Let sabre handle the rest
  193. $iTipMessage = new Message();
  194. /** @var VCalendar $vObject */
  195. $vObject = Reader::read($calendarData);
  196. /** @var VEvent $vEvent */
  197. $vEvent = $vObject->{'VEVENT'};
  198. if ($vObject->{'METHOD'} === null) {
  199. throw new CalendarException('No Method provided for scheduling data. Could not process message');
  200. }
  201. if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) {
  202. throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL');
  203. }
  204. $organizer = $vEvent->{'ORGANIZER'}->getValue();
  205. $attendee = $vEvent->{'ATTENDEE'}->getValue();
  206. $iTipMessage->method = $vObject->{'METHOD'}->getValue();
  207. if ($iTipMessage->method === 'REQUEST') {
  208. $iTipMessage->sender = $organizer;
  209. $iTipMessage->recipient = $attendee;
  210. } elseif ($iTipMessage->method === 'REPLY') {
  211. if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) {
  212. $iTipMessage->recipient = $organizer;
  213. } else {
  214. $iTipMessage->recipient = $attendee;
  215. }
  216. $iTipMessage->sender = $attendee;
  217. } elseif ($iTipMessage->method === 'CANCEL') {
  218. $iTipMessage->recipient = $attendee;
  219. $iTipMessage->sender = $organizer;
  220. }
  221. $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
  222. $iTipMessage->component = 'VEVENT';
  223. $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
  224. $iTipMessage->message = $vObject;
  225. $server->server->emit('schedule', [$iTipMessage]);
  226. }
  227. public function getInvitationResponseServer(): InvitationResponseServer {
  228. return new InvitationResponseServer(false);
  229. }
  230. }