CalendarImpl.php 8.7 KB

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