1
0

Manager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 OC\Calendar;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Calendar\Exceptions\CalendarException;
  11. use OCP\Calendar\ICalendar;
  12. use OCP\Calendar\ICalendarProvider;
  13. use OCP\Calendar\ICalendarQuery;
  14. use OCP\Calendar\ICreateFromString;
  15. use OCP\Calendar\IHandleImipMessage;
  16. use OCP\Calendar\IManager;
  17. use Psr\Container\ContainerInterface;
  18. use Psr\Log\LoggerInterface;
  19. use Sabre\VObject\Component\VCalendar;
  20. use Sabre\VObject\Component\VEvent;
  21. use Sabre\VObject\Property\VCard\DateTime;
  22. use Sabre\VObject\Reader;
  23. use Throwable;
  24. use function array_map;
  25. use function array_merge;
  26. class Manager implements IManager {
  27. /**
  28. * @var ICalendar[] holds all registered calendars
  29. */
  30. private array $calendars = [];
  31. /**
  32. * @var \Closure[] to call to load/register calendar providers
  33. */
  34. private array $calendarLoaders = [];
  35. public function __construct(
  36. private Coordinator $coordinator,
  37. private ContainerInterface $container,
  38. private LoggerInterface $logger,
  39. private ITimeFactory $timeFactory,
  40. ) {
  41. }
  42. /**
  43. * This function is used to search and find objects within the user's calendars.
  44. * In case $pattern is empty all events/journals/todos will be returned.
  45. *
  46. * @param string $pattern which should match within the $searchProperties
  47. * @param array $searchProperties defines the properties within the query pattern should match
  48. * @param array $options - optional parameters:
  49. * ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]]
  50. * @param integer|null $limit - limit number of search results
  51. * @param integer|null $offset - offset for paging of search results
  52. * @return array an array of events/journals/todos which are arrays of arrays of key-value-pairs
  53. * @since 13.0.0
  54. */
  55. public function search(
  56. $pattern,
  57. array $searchProperties = [],
  58. array $options = [],
  59. $limit = null,
  60. $offset = null,
  61. ): array {
  62. $this->loadCalendars();
  63. $result = [];
  64. foreach ($this->calendars as $calendar) {
  65. $r = $calendar->search($pattern, $searchProperties, $options, $limit, $offset);
  66. foreach ($r as $o) {
  67. $o['calendar-key'] = $calendar->getKey();
  68. $result[] = $o;
  69. }
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Check if calendars are available
  75. *
  76. * @return bool true if enabled, false if not
  77. * @since 13.0.0
  78. */
  79. public function isEnabled(): bool {
  80. return !empty($this->calendars) || !empty($this->calendarLoaders);
  81. }
  82. /**
  83. * Registers a calendar
  84. *
  85. * @since 13.0.0
  86. */
  87. public function registerCalendar(ICalendar $calendar): void {
  88. $this->calendars[$calendar->getKey()] = $calendar;
  89. }
  90. /**
  91. * Unregisters a calendar
  92. *
  93. * @since 13.0.0
  94. */
  95. public function unregisterCalendar(ICalendar $calendar): void {
  96. unset($this->calendars[$calendar->getKey()]);
  97. }
  98. /**
  99. * In order to improve lazy loading a closure can be registered which will be called in case
  100. * calendars are actually requested
  101. *
  102. * @since 13.0.0
  103. */
  104. public function register(\Closure $callable): void {
  105. $this->calendarLoaders[] = $callable;
  106. }
  107. /**
  108. * @return ICalendar[]
  109. *
  110. * @since 13.0.0
  111. */
  112. public function getCalendars(): array {
  113. $this->loadCalendars();
  114. return array_values($this->calendars);
  115. }
  116. /**
  117. * removes all registered calendar instances
  118. *
  119. * @since 13.0.0
  120. */
  121. public function clear(): void {
  122. $this->calendars = [];
  123. $this->calendarLoaders = [];
  124. }
  125. /**
  126. * loads all calendars
  127. */
  128. private function loadCalendars(): void {
  129. foreach ($this->calendarLoaders as $callable) {
  130. $callable($this);
  131. }
  132. $this->calendarLoaders = [];
  133. }
  134. /**
  135. * @return ICreateFromString[]
  136. */
  137. public function getCalendarsForPrincipal(string $principalUri, array $calendarUris = []): array {
  138. $context = $this->coordinator->getRegistrationContext();
  139. if ($context === null) {
  140. return [];
  141. }
  142. return array_merge(
  143. ...array_map(function ($registration) use ($principalUri, $calendarUris) {
  144. try {
  145. /** @var ICalendarProvider $provider */
  146. $provider = $this->container->get($registration->getService());
  147. } catch (Throwable $e) {
  148. $this->logger->error('Could not load calendar provider ' . $registration->getService() . ': ' . $e->getMessage(), [
  149. 'exception' => $e,
  150. ]);
  151. return [];
  152. }
  153. return $provider->getCalendars($principalUri, $calendarUris);
  154. }, $context->getCalendarProviders())
  155. );
  156. }
  157. public function searchForPrincipal(ICalendarQuery $query): array {
  158. /** @var CalendarQuery $query */
  159. $calendars = $this->getCalendarsForPrincipal(
  160. $query->getPrincipalUri(),
  161. $query->getCalendarUris(),
  162. );
  163. $results = [];
  164. foreach ($calendars as $calendar) {
  165. $r = $calendar->search(
  166. $query->getSearchPattern() ?? '',
  167. $query->getSearchProperties(),
  168. $query->getOptions(),
  169. $query->getLimit(),
  170. $query->getOffset()
  171. );
  172. foreach ($r as $o) {
  173. $o['calendar-key'] = $calendar->getKey();
  174. $results[] = $o;
  175. }
  176. }
  177. return $results;
  178. }
  179. public function newQuery(string $principalUri): ICalendarQuery {
  180. return new CalendarQuery($principalUri);
  181. }
  182. /**
  183. * @throws \OCP\DB\Exception
  184. */
  185. public function handleIMipReply(
  186. string $principalUri,
  187. string $sender,
  188. string $recipient,
  189. string $calendarData,
  190. ): bool {
  191. /** @var VCalendar $vObject|null */
  192. $vObject = Reader::read($calendarData);
  193. if ($vObject === null) {
  194. return false;
  195. }
  196. /** @var VEvent|null $vEvent */
  197. $vEvent = $vObject->{'VEVENT'};
  198. if ($vEvent === null) {
  199. return false;
  200. }
  201. // First, we check if the correct method is passed to us
  202. if (strcasecmp('REPLY', $vObject->{'METHOD'}->getValue()) !== 0) {
  203. $this->logger->warning('Wrong method provided for processing');
  204. return false;
  205. }
  206. // check if mail recipient and organizer are one and the same
  207. $organizer = substr($vEvent->{'ORGANIZER'}->getValue(), 7);
  208. if (strcasecmp($recipient, $organizer) !== 0) {
  209. $this->logger->warning('Recipient and ORGANIZER must be identical');
  210. return false;
  211. }
  212. //check if the event is in the future
  213. /** @var DateTime $eventTime */
  214. $eventTime = $vEvent->{'DTSTART'};
  215. if ($eventTime->getDateTime()->getTimeStamp() < $this->timeFactory->getTime()) { // this might cause issues with recurrences
  216. $this->logger->warning('Only events in the future are processed');
  217. return false;
  218. }
  219. $calendars = $this->getCalendarsForPrincipal($principalUri);
  220. if (empty($calendars)) {
  221. $this->logger->warning('Could not find any calendars for principal ' . $principalUri);
  222. return false;
  223. }
  224. $found = null;
  225. // if the attendee has been found in at least one calendar event with the UID of the iMIP event
  226. // we process it.
  227. // Benefit: no attendee lost
  228. // Drawback: attendees that have been deleted will still be able to update their partstat
  229. foreach ($calendars as $calendar) {
  230. // We should not search in writable calendars
  231. if ($calendar instanceof IHandleImipMessage) {
  232. $o = $calendar->search($sender, ['ATTENDEE'], ['uid' => $vEvent->{'UID'}->getValue()]);
  233. if (!empty($o)) {
  234. $found = $calendar;
  235. $name = $o[0]['uri'];
  236. break;
  237. }
  238. }
  239. }
  240. if (empty($found)) {
  241. $this->logger->info('Event not found in any calendar for principal ' . $principalUri . 'and UID' . $vEvent->{'UID'}->getValue());
  242. return false;
  243. }
  244. try {
  245. $found->handleIMipMessage($name, $calendarData); // sabre will handle the scheduling behind the scenes
  246. } catch (CalendarException $e) {
  247. $this->logger->error('Could not update calendar for iMIP processing', ['exception' => $e]);
  248. return false;
  249. }
  250. return true;
  251. }
  252. /**
  253. * @since 25.0.0
  254. * @throws \OCP\DB\Exception
  255. */
  256. public function handleIMipCancel(
  257. string $principalUri,
  258. string $sender,
  259. ?string $replyTo,
  260. string $recipient,
  261. string $calendarData,
  262. ): bool {
  263. /** @var VCalendar $vObject|null */
  264. $vObject = Reader::read($calendarData);
  265. if ($vObject === null) {
  266. return false;
  267. }
  268. /** @var VEvent|null $vEvent */
  269. $vEvent = $vObject->{'VEVENT'};
  270. if ($vEvent === null) {
  271. return false;
  272. }
  273. // First, we check if the correct method is passed to us
  274. if (strcasecmp('CANCEL', $vObject->{'METHOD'}->getValue()) !== 0) {
  275. $this->logger->warning('Wrong method provided for processing');
  276. return false;
  277. }
  278. $attendee = substr($vEvent->{'ATTENDEE'}->getValue(), 7);
  279. if (strcasecmp($recipient, $attendee) !== 0) {
  280. $this->logger->warning('Recipient must be an ATTENDEE of this event');
  281. return false;
  282. }
  283. // Thirdly, we need to compare the email address the CANCEL is coming from (in Mail)
  284. // or the Reply- To Address submitted with the CANCEL email
  285. // to the email address in the ORGANIZER.
  286. // We don't want to accept a CANCEL request from just anyone
  287. $organizer = substr($vEvent->{'ORGANIZER'}->getValue(), 7);
  288. $isNotOrganizer = ($replyTo !== null) ? (strcasecmp($sender, $organizer) !== 0 && strcasecmp($replyTo, $organizer) !== 0) : (strcasecmp($sender, $organizer) !== 0);
  289. if ($isNotOrganizer) {
  290. $this->logger->warning('Sender must be the ORGANIZER of this event');
  291. return false;
  292. }
  293. //check if the event is in the future
  294. /** @var DateTime $eventTime */
  295. $eventTime = $vEvent->{'DTSTART'};
  296. if ($eventTime->getDateTime()->getTimeStamp() < $this->timeFactory->getTime()) { // this might cause issues with recurrences
  297. $this->logger->warning('Only events in the future are processed');
  298. return false;
  299. }
  300. // Check if we have a calendar to work with
  301. $calendars = $this->getCalendarsForPrincipal($principalUri);
  302. if (empty($calendars)) {
  303. $this->logger->warning('Could not find any calendars for principal ' . $principalUri);
  304. return false;
  305. }
  306. $found = null;
  307. // if the attendee has been found in at least one calendar event with the UID of the iMIP event
  308. // we process it.
  309. // Benefit: no attendee lost
  310. // Drawback: attendees that have been deleted will still be able to update their partstat
  311. foreach ($calendars as $calendar) {
  312. // We should not search in writable calendars
  313. if ($calendar instanceof IHandleImipMessage) {
  314. $o = $calendar->search($recipient, ['ATTENDEE'], ['uid' => $vEvent->{'UID'}->getValue()]);
  315. if (!empty($o)) {
  316. $found = $calendar;
  317. $name = $o[0]['uri'];
  318. break;
  319. }
  320. }
  321. }
  322. if (empty($found)) {
  323. $this->logger->info('Event not found in any calendar for principal ' . $principalUri . 'and UID' . $vEvent->{'UID'}->getValue());
  324. // this is a safe operation
  325. // we can ignore events that have been cancelled but were not in the calendar anyway
  326. return true;
  327. }
  328. try {
  329. $found->handleIMipMessage($name, $calendarData); // sabre will handle the scheduling behind the scenes
  330. return true;
  331. } catch (CalendarException $e) {
  332. $this->logger->error('Could not update calendar for iMIP processing', ['exception' => $e]);
  333. return false;
  334. }
  335. }
  336. }