1
0

CalendarImpl.php 7.8 KB

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