CalendarImpl.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Anna Larch <anna.larch@gmx.net>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV;
  28. use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
  29. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  30. use OCP\Calendar\Exceptions\CalendarException;
  31. use OCP\Calendar\ICreateFromString;
  32. use OCP\Calendar\IHandleImipMessage;
  33. use OCP\Constants;
  34. use Sabre\DAV\Exception\Conflict;
  35. use Sabre\VObject\Component\VCalendar;
  36. use Sabre\VObject\Component\VEvent;
  37. use Sabre\VObject\ITip\Message;
  38. use Sabre\VObject\Reader;
  39. use function Sabre\Uri\split as uriSplit;
  40. class CalendarImpl implements ICreateFromString, IHandleImipMessage {
  41. private CalDavBackend $backend;
  42. private Calendar $calendar;
  43. /** @var array<string, mixed> */
  44. private array $calendarInfo;
  45. public function __construct(Calendar $calendar,
  46. array $calendarInfo,
  47. CalDavBackend $backend) {
  48. $this->calendar = $calendar;
  49. $this->calendarInfo = $calendarInfo;
  50. $this->backend = $backend;
  51. }
  52. /**
  53. * @return string defining the technical unique key
  54. * @since 13.0.0
  55. */
  56. public function getKey(): string {
  57. return (string) $this->calendarInfo['id'];
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function getUri(): string {
  63. return $this->calendarInfo['uri'];
  64. }
  65. /**
  66. * In comparison to getKey() this function returns a human readable (maybe translated) name
  67. * @since 13.0.0
  68. */
  69. public function getDisplayName(): ?string {
  70. return $this->calendarInfo['{DAV:}displayname'];
  71. }
  72. /**
  73. * Calendar color
  74. * @since 13.0.0
  75. */
  76. public function getDisplayColor(): ?string {
  77. return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color'];
  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 26.0.0
  118. */
  119. public function isDeleted(): bool {
  120. return $this->calendar->isDeleted();
  121. }
  122. /**
  123. * Create a new calendar event for this calendar
  124. * by way of an ICS string
  125. *
  126. * @param string $name the file name - needs to contain the .ics ending
  127. * @param string $calendarData a string containing a valid VEVENT ics
  128. *
  129. * @throws CalendarException
  130. */
  131. public function createFromString(string $name, string $calendarData): void {
  132. $server = new InvitationResponseServer(false);
  133. /** @var CustomPrincipalPlugin $plugin */
  134. $plugin = $server->getServer()->getPlugin('auth');
  135. // we're working around the previous implementation
  136. // that only allowed the public system principal to be used
  137. // so set the custom principal here
  138. $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());
  139. if (empty($this->calendarInfo['uri'])) {
  140. throw new CalendarException('Could not write to calendar as URI parameter is missing');
  141. }
  142. // Build full calendar path
  143. [, $user] = uriSplit($this->calendar->getPrincipalURI());
  144. $fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name);
  145. // Force calendar change URI
  146. /** @var Schedule\Plugin $schedulingPlugin */
  147. $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
  148. $schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename);
  149. $stream = fopen('php://memory', 'rb+');
  150. fwrite($stream, $calendarData);
  151. rewind($stream);
  152. try {
  153. $server->getServer()->createFile($fullCalendarFilename, $stream);
  154. } catch (Conflict $e) {
  155. throw new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e);
  156. } finally {
  157. fclose($stream);
  158. }
  159. }
  160. /**
  161. * @throws CalendarException
  162. */
  163. public function handleIMipMessage(string $name, string $calendarData): void {
  164. $server = $this->getInvitationResponseServer();
  165. /** @var CustomPrincipalPlugin $plugin */
  166. $plugin = $server->getServer()->getPlugin('auth');
  167. // we're working around the previous implementation
  168. // that only allowed the public system principal to be used
  169. // so set the custom principal here
  170. $plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());
  171. if (empty($this->calendarInfo['uri'])) {
  172. throw new CalendarException('Could not write to calendar as URI parameter is missing');
  173. }
  174. // Force calendar change URI
  175. /** @var Schedule\Plugin $schedulingPlugin */
  176. $schedulingPlugin = $server->getServer()->getPlugin('caldav-schedule');
  177. // Let sabre handle the rest
  178. $iTipMessage = new Message();
  179. /** @var VCalendar $vObject */
  180. $vObject = Reader::read($calendarData);
  181. /** @var VEvent $vEvent */
  182. $vEvent = $vObject->{'VEVENT'};
  183. if ($vObject->{'METHOD'} === null) {
  184. throw new CalendarException('No Method provided for scheduling data. Could not process message');
  185. }
  186. if (!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) {
  187. throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL');
  188. }
  189. $organizer = $vEvent->{'ORGANIZER'}->getValue();
  190. $attendee = $vEvent->{'ATTENDEE'}->getValue();
  191. $iTipMessage->method = $vObject->{'METHOD'}->getValue();
  192. if ($iTipMessage->method === 'REPLY') {
  193. if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) {
  194. $iTipMessage->recipient = $organizer;
  195. } else {
  196. $iTipMessage->recipient = $attendee;
  197. }
  198. $iTipMessage->sender = $attendee;
  199. } elseif ($iTipMessage->method === 'CANCEL') {
  200. $iTipMessage->recipient = $attendee;
  201. $iTipMessage->sender = $organizer;
  202. }
  203. $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
  204. $iTipMessage->component = 'VEVENT';
  205. $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
  206. $iTipMessage->message = $vObject;
  207. $server->server->emit('schedule', [$iTipMessage]);
  208. }
  209. public function getInvitationResponseServer(): InvitationResponseServer {
  210. return new InvitationResponseServer(false);
  211. }
  212. }