IMipPlugin.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author brad2014 <brad2014@users.noreply.github.com>
  7. * @author Brad Rubenstein <brad@wbr.tech>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Leon Klingele <leon@struktur.de>
  12. * @author rakekniven <mark.ziegler@rakekniven.de>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\CalDAV\Schedule;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\Defaults;
  34. use OCP\IConfig;
  35. use OCP\IDBConnection;
  36. use OCP\IL10N;
  37. use OCP\ILogger;
  38. use OCP\IURLGenerator;
  39. use OCP\IUserManager;
  40. use OCP\L10N\IFactory as L10NFactory;
  41. use OCP\Mail\IEMailTemplate;
  42. use OCP\Mail\IMailer;
  43. use OCP\Security\ISecureRandom;
  44. use OCP\Util;
  45. use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin;
  46. use Sabre\VObject\Component\VCalendar;
  47. use Sabre\VObject\Component\VEvent;
  48. use Sabre\VObject\DateTimeParser;
  49. use Sabre\VObject\ITip\Message;
  50. use Sabre\VObject\Parameter;
  51. use Sabre\VObject\Property;
  52. use Sabre\VObject\Recur\EventIterator;
  53. /**
  54. * iMIP handler.
  55. *
  56. * This class is responsible for sending out iMIP messages. iMIP is the
  57. * email-based transport for iTIP. iTIP deals with scheduling operations for
  58. * iCalendar objects.
  59. *
  60. * If you want to customize the email that gets sent out, you can do so by
  61. * extending this class and overriding the sendMessage method.
  62. *
  63. * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
  64. * @author Evert Pot (http://evertpot.com/)
  65. * @license http://sabre.io/license/ Modified BSD License
  66. */
  67. class IMipPlugin extends SabreIMipPlugin {
  68. /** @var string */
  69. private $userId;
  70. /** @var IConfig */
  71. private $config;
  72. /** @var IMailer */
  73. private $mailer;
  74. /** @var ILogger */
  75. private $logger;
  76. /** @var ITimeFactory */
  77. private $timeFactory;
  78. /** @var L10NFactory */
  79. private $l10nFactory;
  80. /** @var IURLGenerator */
  81. private $urlGenerator;
  82. /** @var ISecureRandom */
  83. private $random;
  84. /** @var IDBConnection */
  85. private $db;
  86. /** @var Defaults */
  87. private $defaults;
  88. /** @var IUserManager */
  89. private $userManager;
  90. const MAX_DATE = '2038-01-01';
  91. const METHOD_REQUEST = 'request';
  92. const METHOD_REPLY = 'reply';
  93. const METHOD_CANCEL = 'cancel';
  94. /**
  95. * @param IConfig $config
  96. * @param IMailer $mailer
  97. * @param ILogger $logger
  98. * @param ITimeFactory $timeFactory
  99. * @param L10NFactory $l10nFactory
  100. * @param IUrlGenerator $urlGenerator
  101. * @param Defaults $defaults
  102. * @param ISecureRandom $random
  103. * @param IDBConnection $db
  104. * @param string $userId
  105. */
  106. public function __construct(IConfig $config, IMailer $mailer, ILogger $logger,
  107. ITimeFactory $timeFactory, L10NFactory $l10nFactory,
  108. IURLGenerator $urlGenerator, Defaults $defaults,
  109. ISecureRandom $random, IDBConnection $db, IUserManager $userManager,
  110. $userId) {
  111. parent::__construct('');
  112. $this->userId = $userId;
  113. $this->config = $config;
  114. $this->mailer = $mailer;
  115. $this->logger = $logger;
  116. $this->timeFactory = $timeFactory;
  117. $this->l10nFactory = $l10nFactory;
  118. $this->urlGenerator = $urlGenerator;
  119. $this->random = $random;
  120. $this->db = $db;
  121. $this->defaults = $defaults;
  122. $this->userManager = $userManager;
  123. }
  124. /**
  125. * Event handler for the 'schedule' event.
  126. *
  127. * @param Message $iTipMessage
  128. * @return void
  129. */
  130. public function schedule(Message $iTipMessage) {
  131. // Not sending any emails if the system considers the update
  132. // insignificant.
  133. if (!$iTipMessage->significantChange) {
  134. if (!$iTipMessage->scheduleStatus) {
  135. $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
  136. }
  137. return;
  138. }
  139. $summary = $iTipMessage->message->VEVENT->SUMMARY;
  140. if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
  141. return;
  142. }
  143. if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
  144. return;
  145. }
  146. // don't send out mails for events that already took place
  147. $lastOccurrence = $this->getLastOccurrence($iTipMessage->message);
  148. $currentTime = $this->timeFactory->getTime();
  149. if ($lastOccurrence < $currentTime) {
  150. return;
  151. }
  152. // Strip off mailto:
  153. $sender = substr($iTipMessage->sender, 7);
  154. $recipient = substr($iTipMessage->recipient, 7);
  155. $senderName = $iTipMessage->senderName ?: null;
  156. $recipientName = $iTipMessage->recipientName ?: null;
  157. if ($senderName === null || empty(trim($senderName))) {
  158. $user = $this->userManager->get($this->userId);
  159. if ($user) {
  160. // getDisplayName automatically uses the uid
  161. // if no display-name is set
  162. $senderName = $user->getDisplayName();
  163. }
  164. }
  165. /** @var VEvent $vevent */
  166. $vevent = $iTipMessage->message->VEVENT;
  167. $attendee = $this->getCurrentAttendee($iTipMessage);
  168. $defaultLang = $this->l10nFactory->findLanguage();
  169. $lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee);
  170. $l10n = $this->l10nFactory->get('dav', $lang);
  171. $meetingAttendeeName = $recipientName ?: $recipient;
  172. $meetingInviteeName = $senderName ?: $sender;
  173. $meetingTitle = $vevent->SUMMARY;
  174. $meetingDescription = $vevent->DESCRIPTION;
  175. $start = $vevent->DTSTART;
  176. if (isset($vevent->DTEND)) {
  177. $end = $vevent->DTEND;
  178. } elseif (isset($vevent->DURATION)) {
  179. $isFloating = $vevent->DTSTART->isFloating();
  180. $end = clone $vevent->DTSTART;
  181. $endDateTime = $end->getDateTime();
  182. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  183. $end->setDateTime($endDateTime, $isFloating);
  184. } elseif (!$vevent->DTSTART->hasTime()) {
  185. $isFloating = $vevent->DTSTART->isFloating();
  186. $end = clone $vevent->DTSTART;
  187. $endDateTime = $end->getDateTime();
  188. $endDateTime = $endDateTime->modify('+1 day');
  189. $end->setDateTime($endDateTime, $isFloating);
  190. } else {
  191. $end = clone $vevent->DTSTART;
  192. }
  193. $meetingWhen = $this->generateWhenString($l10n, $start, $end);
  194. $meetingUrl = $vevent->URL;
  195. $meetingLocation = $vevent->LOCATION;
  196. $defaultVal = '--';
  197. $method = self::METHOD_REQUEST;
  198. switch (strtolower($iTipMessage->method)) {
  199. case self::METHOD_REPLY:
  200. $method = self::METHOD_REPLY;
  201. break;
  202. case self::METHOD_CANCEL:
  203. $method = self::METHOD_CANCEL;
  204. break;
  205. }
  206. $data = [
  207. 'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
  208. 'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
  209. 'meeting_title' => (string)$meetingTitle ?: $defaultVal,
  210. 'meeting_description' => (string)$meetingDescription ?: $defaultVal,
  211. 'meeting_url' => (string)$meetingUrl ?: $defaultVal,
  212. ];
  213. $fromEMail = Util::getDefaultEmailAddress('invitations-noreply');
  214. $fromName = $l10n->t('%1$s via %2$s', [$senderName, $this->defaults->getName()]);
  215. $message = $this->mailer->createMessage()
  216. ->setFrom([$fromEMail => $fromName])
  217. ->setReplyTo([$sender => $senderName])
  218. ->setTo([$recipient => $recipientName]);
  219. $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
  220. $template->addHeader();
  221. $summary = ((string) $summary !== '') ? (string) $summary : $l10n->t('Untitled event');
  222. $this->addSubjectAndHeading($template, $l10n, $method, $summary,
  223. $meetingAttendeeName, $meetingInviteeName);
  224. $this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation,
  225. $meetingDescription, $meetingUrl);
  226. // Only add response buttons to invitation requests: Fix Issue #11230
  227. if (($method == self::METHOD_REQUEST) && $this->getAttendeeRSVP($attendee)) {
  228. /*
  229. ** Only offer invitation accept/reject buttons, which link back to the
  230. ** nextcloud server, to recipients who can access the nextcloud server via
  231. ** their internet/intranet. Issue #12156
  232. **
  233. ** The app setting is stored in the appconfig database table.
  234. **
  235. ** For nextcloud servers accessible to the public internet, the default
  236. ** "invitation_link_recipients" value "yes" (all recipients) is appropriate.
  237. **
  238. ** When the nextcloud server is restricted behind a firewall, accessible
  239. ** only via an internal network or via vpn, you can set "dav.invitation_link_recipients"
  240. ** to the email address or email domain, or comma separated list of addresses or domains,
  241. ** of recipients who can access the server.
  242. **
  243. ** To always deliver URLs, set invitation_link_recipients to "yes".
  244. ** To suppress URLs entirely, set invitation_link_recipients to boolean "no".
  245. */
  246. $recipientDomain = substr(strrchr($recipient, "@"), 1);
  247. $invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes'))));
  248. if (strcmp('yes', $invitationLinkRecipients[0]) === 0
  249. || in_array(strtolower($recipient), $invitationLinkRecipients)
  250. || in_array(strtolower($recipientDomain), $invitationLinkRecipients)) {
  251. $this->addResponseButtons($template, $l10n, $iTipMessage, $lastOccurrence);
  252. }
  253. }
  254. $template->addFooter();
  255. $message->useTemplate($template);
  256. $attachment = $this->mailer->createAttachment(
  257. $iTipMessage->message->serialize(),
  258. 'event.ics',// TODO(leon): Make file name unique, e.g. add event id
  259. 'text/calendar; method=' . $iTipMessage->method
  260. );
  261. $message->attach($attachment);
  262. try {
  263. $failed = $this->mailer->send($message);
  264. $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
  265. if ($failed) {
  266. $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);
  267. $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
  268. }
  269. } catch (\Exception $ex) {
  270. $this->logger->logException($ex, ['app' => 'dav']);
  271. $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
  272. }
  273. }
  274. /**
  275. * check if event took place in the past already
  276. * @param VCalendar $vObject
  277. * @return int
  278. */
  279. private function getLastOccurrence(VCalendar $vObject) {
  280. /** @var VEvent $component */
  281. $component = $vObject->VEVENT;
  282. $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
  283. // Finding the last occurrence is a bit harder
  284. if (!isset($component->RRULE)) {
  285. if (isset($component->DTEND)) {
  286. $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
  287. } elseif (isset($component->DURATION)) {
  288. /** @var \DateTime $endDate */
  289. $endDate = clone $component->DTSTART->getDateTime();
  290. // $component->DTEND->getDateTime() returns DateTimeImmutable
  291. $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
  292. $lastOccurrence = $endDate->getTimestamp();
  293. } elseif (!$component->DTSTART->hasTime()) {
  294. /** @var \DateTime $endDate */
  295. $endDate = clone $component->DTSTART->getDateTime();
  296. // $component->DTSTART->getDateTime() returns DateTimeImmutable
  297. $endDate = $endDate->modify('+1 day');
  298. $lastOccurrence = $endDate->getTimestamp();
  299. } else {
  300. $lastOccurrence = $firstOccurrence;
  301. }
  302. } else {
  303. $it = new EventIterator($vObject, (string)$component->UID);
  304. $maxDate = new \DateTime(self::MAX_DATE);
  305. if ($it->isInfinite()) {
  306. $lastOccurrence = $maxDate->getTimestamp();
  307. } else {
  308. $end = $it->getDtEnd();
  309. while ($it->valid() && $end < $maxDate) {
  310. $end = $it->getDtEnd();
  311. $it->next();
  312. }
  313. $lastOccurrence = $end->getTimestamp();
  314. }
  315. }
  316. return $lastOccurrence;
  317. }
  318. /**
  319. * @param Message $iTipMessage
  320. * @return null|Property
  321. */
  322. private function getCurrentAttendee(Message $iTipMessage) {
  323. /** @var VEvent $vevent */
  324. $vevent = $iTipMessage->message->VEVENT;
  325. $attendees = $vevent->select('ATTENDEE');
  326. foreach ($attendees as $attendee) {
  327. /** @var Property $attendee */
  328. if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
  329. return $attendee;
  330. }
  331. }
  332. return null;
  333. }
  334. /**
  335. * @param string $default
  336. * @param Property|null $attendee
  337. * @return string
  338. */
  339. private function getAttendeeLangOrDefault($default, Property $attendee = null) {
  340. if ($attendee !== null) {
  341. $lang = $attendee->offsetGet('LANGUAGE');
  342. if ($lang instanceof Parameter) {
  343. return $lang->getValue();
  344. }
  345. }
  346. return $default;
  347. }
  348. /**
  349. * @param Property|null $attendee
  350. * @return bool
  351. */
  352. private function getAttendeeRSVP(Property $attendee = null) {
  353. if ($attendee !== null) {
  354. $rsvp = $attendee->offsetGet('RSVP');
  355. if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
  356. return true;
  357. }
  358. }
  359. // RFC 5545 3.2.17: default RSVP is false
  360. return false;
  361. }
  362. /**
  363. * @param IL10N $l10n
  364. * @param Property $dtstart
  365. * @param Property $dtend
  366. */
  367. private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) {
  368. $isAllDay = $dtstart instanceof Property\ICalendar\Date;
  369. /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
  370. /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
  371. /** @var \DateTimeImmutable $dtstartDt */
  372. $dtstartDt = $dtstart->getDateTime();
  373. /** @var \DateTimeImmutable $dtendDt */
  374. $dtendDt = $dtend->getDateTime();
  375. $diff = $dtstartDt->diff($dtendDt);
  376. $dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
  377. $dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));
  378. if ($isAllDay) {
  379. // One day event
  380. if ($diff->days === 1) {
  381. return $l10n->l('date', $dtstartDt, ['width' => 'medium']);
  382. }
  383. // DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
  384. // the email should show 2020-01-01 to 2020-01-04.
  385. $dtendDt->modify('-1 day');
  386. //event that spans over multiple days
  387. $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
  388. $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
  389. return $localeStart . ' - ' . $localeEnd;
  390. }
  391. /** @var Property\ICalendar\DateTime $dtstart */
  392. /** @var Property\ICalendar\DateTime $dtend */
  393. $isFloating = $dtstart->isFloating();
  394. $startTimezone = $endTimezone = null;
  395. if (!$isFloating) {
  396. $prop = $dtstart->offsetGet('TZID');
  397. if ($prop instanceof Parameter) {
  398. $startTimezone = $prop->getValue();
  399. }
  400. $prop = $dtend->offsetGet('TZID');
  401. if ($prop instanceof Parameter) {
  402. $endTimezone = $prop->getValue();
  403. }
  404. }
  405. $localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
  406. $l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
  407. // always show full date with timezone if timezones are different
  408. if ($startTimezone !== $endTimezone) {
  409. $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
  410. return $localeStart . ' (' . $startTimezone . ') - ' .
  411. $localeEnd . ' (' . $endTimezone . ')';
  412. }
  413. // show only end time if date is the same
  414. if ($this->isDayEqual($dtstartDt, $dtendDt)) {
  415. $localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
  416. } else {
  417. $localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
  418. $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
  419. }
  420. return $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
  421. }
  422. /**
  423. * @param \DateTime $dtStart
  424. * @param \DateTime $dtEnd
  425. * @return bool
  426. */
  427. private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) {
  428. return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
  429. }
  430. /**
  431. * @param IEMailTemplate $template
  432. * @param IL10N $l10n
  433. * @param string $method
  434. * @param string $summary
  435. * @param string $attendeeName
  436. * @param string $inviteeName
  437. */
  438. private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
  439. $method, $summary, $attendeeName, $inviteeName) {
  440. if ($method === self::METHOD_CANCEL) {
  441. $template->setSubject('Cancelled: ' . $summary);
  442. $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
  443. $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
  444. } elseif ($method === self::METHOD_REPLY) {
  445. $template->setSubject('Re: ' . $summary);
  446. $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
  447. $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
  448. } else {
  449. $template->setSubject('Invitation: ' . $summary);
  450. $template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
  451. }
  452. }
  453. /**
  454. * @param IEMailTemplate $template
  455. * @param IL10N $l10n
  456. * @param string $time
  457. * @param string $location
  458. * @param string $description
  459. * @param string $url
  460. */
  461. private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) {
  462. $template->addBodyListItem($time, $l10n->t('When:'),
  463. $this->getAbsoluteImagePath('filetypes/text-calendar.svg'));
  464. if ($location) {
  465. $template->addBodyListItem($location, $l10n->t('Where:'),
  466. $this->getAbsoluteImagePath('filetypes/location.svg'));
  467. }
  468. if ($description) {
  469. $template->addBodyListItem((string)$description, $l10n->t('Description:'),
  470. $this->getAbsoluteImagePath('filetypes/text.svg'));
  471. }
  472. if ($url) {
  473. $template->addBodyListItem((string)$url, $l10n->t('Link:'),
  474. $this->getAbsoluteImagePath('filetypes/link.svg'));
  475. }
  476. }
  477. /**
  478. * @param IEMailTemplate $template
  479. * @param IL10N $l10n
  480. * @param Message $iTipMessage
  481. * @param int $lastOccurrence
  482. */
  483. private function addResponseButtons(IEMailTemplate $template, IL10N $l10n,
  484. Message $iTipMessage, $lastOccurrence) {
  485. $token = $this->createInvitationToken($iTipMessage, $lastOccurrence);
  486. $template->addBodyButtonGroup(
  487. $l10n->t('Accept'),
  488. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
  489. 'token' => $token,
  490. ]),
  491. $l10n->t('Decline'),
  492. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
  493. 'token' => $token,
  494. ])
  495. );
  496. $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
  497. 'token' => $token,
  498. ]);
  499. $html = vsprintf('<small><a href="%s">%s</a></small>', [
  500. $moreOptionsURL, $l10n->t('More options …')
  501. ]);
  502. $text = $l10n->t('More options at %s', [$moreOptionsURL]);
  503. $template->addBodyText($html, $text);
  504. }
  505. /**
  506. * @param string $path
  507. * @return string
  508. */
  509. private function getAbsoluteImagePath($path) {
  510. return $this->urlGenerator->getAbsoluteURL(
  511. $this->urlGenerator->imagePath('core', $path)
  512. );
  513. }
  514. /**
  515. * @param Message $iTipMessage
  516. * @param int $lastOccurrence
  517. * @return string
  518. */
  519. private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
  520. $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
  521. /** @var VEvent $vevent */
  522. $vevent = $iTipMessage->message->VEVENT;
  523. $attendee = $iTipMessage->recipient;
  524. $organizer = $iTipMessage->sender;
  525. $sequence = $iTipMessage->sequence;
  526. $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
  527. $vevent->{'RECURRENCE-ID'}->serialize() : null;
  528. $uid = $vevent->{'UID'};
  529. $query = $this->db->getQueryBuilder();
  530. $query->insert('calendar_invitations')
  531. ->values([
  532. 'token' => $query->createNamedParameter($token),
  533. 'attendee' => $query->createNamedParameter($attendee),
  534. 'organizer' => $query->createNamedParameter($organizer),
  535. 'sequence' => $query->createNamedParameter($sequence),
  536. 'recurrenceid' => $query->createNamedParameter($recurrenceId),
  537. 'expiration' => $query->createNamedParameter($lastOccurrence),
  538. 'uid' => $query->createNamedParameter($uid)
  539. ])
  540. ->execute();
  541. return $token;
  542. }
  543. }