IMipService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * DAV App
  5. *
  6. * @copyright 2022 Anna Larch <anna.larch@gmx.net>
  7. *
  8. * @author Anna Larch <anna.larch@gmx.net>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OCA\DAV\CalDAV\Schedule;
  24. use OC\URLGenerator;
  25. use OCP\IConfig;
  26. use OCP\IDBConnection;
  27. use OCP\IL10N;
  28. use OCP\L10N\IFactory as L10NFactory;
  29. use OCP\Mail\IEMailTemplate;
  30. use OCP\Security\ISecureRandom;
  31. use Sabre\VObject\Component\VCalendar;
  32. use Sabre\VObject\Component\VEvent;
  33. use Sabre\VObject\DateTimeParser;
  34. use Sabre\VObject\ITip\Message;
  35. use Sabre\VObject\Parameter;
  36. use Sabre\VObject\Property;
  37. use Sabre\VObject\Recur\EventIterator;
  38. class IMipService {
  39. private URLGenerator $urlGenerator;
  40. private IConfig $config;
  41. private IDBConnection $db;
  42. private ISecureRandom $random;
  43. private L10NFactory $l10nFactory;
  44. private IL10N $l10n;
  45. /** @var string[] */
  46. private const STRING_DIFF = [
  47. 'meeting_title' => 'SUMMARY',
  48. 'meeting_description' => 'DESCRIPTION',
  49. 'meeting_url' => 'URL',
  50. 'meeting_location' => 'LOCATION'
  51. ];
  52. public function __construct(URLGenerator $urlGenerator,
  53. IConfig $config,
  54. IDBConnection $db,
  55. ISecureRandom $random,
  56. L10NFactory $l10nFactory) {
  57. $this->urlGenerator = $urlGenerator;
  58. $this->config = $config;
  59. $this->db = $db;
  60. $this->random = $random;
  61. $this->l10nFactory = $l10nFactory;
  62. $default = $this->l10nFactory->findGenericLanguage();
  63. $this->l10n = $this->l10nFactory->get('dav', $default);
  64. }
  65. /**
  66. * @param string|null $senderName
  67. * @param string $default
  68. * @return string
  69. */
  70. public function getFrom(?string $senderName, string $default): string {
  71. if ($senderName === null) {
  72. return $default;
  73. }
  74. return $this->l10n->t('%1$s via %2$s', [$senderName, $default]);
  75. }
  76. public static function readPropertyWithDefault(VEvent $vevent, string $property, string $default) {
  77. if (isset($vevent->$property)) {
  78. $value = $vevent->$property->getValue();
  79. if (!empty($value)) {
  80. return $value;
  81. }
  82. }
  83. return $default;
  84. }
  85. private function generateDiffString(VEvent $vevent, VEvent $oldVEvent, string $property, string $default): ?string {
  86. $strikethrough = "<span style='text-decoration: line-through'>%s</span><br />%s";
  87. if (!isset($vevent->$property)) {
  88. return $default;
  89. }
  90. $newstring = $vevent->$property->getValue();
  91. if(isset($oldVEvent->$property) && $oldVEvent->$property->getValue() !== $newstring ) {
  92. $oldstring = $oldVEvent->$property->getValue();
  93. return sprintf($strikethrough, $oldstring, $newstring);
  94. }
  95. return $newstring;
  96. }
  97. /**
  98. * @param VEvent $vEvent
  99. * @param VEvent|null $oldVEvent
  100. * @return array
  101. */
  102. public function buildBodyData(VEvent $vEvent, ?VEvent $oldVEvent): array {
  103. $defaultVal = '';
  104. $data = [];
  105. $data['meeting_when'] = $this->generateWhenString($vEvent);
  106. foreach(self::STRING_DIFF as $key => $property) {
  107. $data[$key] = self::readPropertyWithDefault($vEvent, $property, $defaultVal);
  108. }
  109. $data['meeting_url_html'] = self::readPropertyWithDefault($vEvent, 'URL', $defaultVal);
  110. if(!empty($oldVEvent)) {
  111. $oldMeetingWhen = $this->generateWhenString($oldVEvent);
  112. $data['meeting_title_html'] = $this->generateDiffString($vEvent, $oldVEvent, 'SUMMARY', $data['meeting_title']);
  113. $data['meeting_description_html'] = $this->generateDiffString($vEvent, $oldVEvent, 'DESCRIPTION', $data['meeting_description']);
  114. $data['meeting_location_html'] = $this->generateDiffString($vEvent, $oldVEvent, 'LOCATION', $data['meeting_location']);
  115. $oldUrl = self::readPropertyWithDefault($oldVEvent, 'URL', $defaultVal);
  116. $data['meeting_url_html'] = !empty($oldUrl) && $oldUrl !== $data['meeting_url'] ? sprintf('<a href="%1$s">%1$s</a>', $oldUrl) : $data['meeting_url'];
  117. $data['meeting_when_html'] =
  118. ($oldMeetingWhen !== $data['meeting_when'] && $oldMeetingWhen !== null)
  119. ? sprintf("<span style='text-decoration: line-through'>%s</span><br />%s", $oldMeetingWhen, $data['meeting_when'])
  120. : $data['meeting_when'];
  121. }
  122. return $data;
  123. }
  124. /**
  125. * @param IL10N $this->l10n
  126. * @param VEvent $vevent
  127. * @return false|int|string
  128. */
  129. public function generateWhenString(VEvent $vevent) {
  130. /** @var Property\ICalendar\DateTime $dtstart */
  131. $dtstart = $vevent->DTSTART;
  132. if (isset($vevent->DTEND)) {
  133. /** @var Property\ICalendar\DateTime $dtend */
  134. $dtend = $vevent->DTEND;
  135. } elseif (isset($vevent->DURATION)) {
  136. $isFloating = $dtstart->isFloating();
  137. $dtend = clone $dtstart;
  138. $endDateTime = $dtend->getDateTime();
  139. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  140. $dtend->setDateTime($endDateTime, $isFloating);
  141. } elseif (!$dtstart->hasTime()) {
  142. $isFloating = $dtstart->isFloating();
  143. $dtend = clone $dtstart;
  144. $endDateTime = $dtend->getDateTime();
  145. $endDateTime = $endDateTime->modify('+1 day');
  146. $dtend->setDateTime($endDateTime, $isFloating);
  147. } else {
  148. $dtend = clone $dtstart;
  149. }
  150. /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
  151. /** @var \DateTimeImmutable $dtstartDt */
  152. $dtstartDt = $dtstart->getDateTime();
  153. /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
  154. /** @var \DateTimeImmutable $dtendDt */
  155. $dtendDt = $dtend->getDateTime();
  156. $diff = $dtstartDt->diff($dtendDt);
  157. $dtstartDt = new \DateTime($dtstartDt->format(\DateTimeInterface::ATOM));
  158. $dtendDt = new \DateTime($dtendDt->format(\DateTimeInterface::ATOM));
  159. if ($dtstart instanceof Property\ICalendar\Date) {
  160. // One day event
  161. if ($diff->days === 1) {
  162. return $this->l10n->l('date', $dtstartDt, ['width' => 'medium']);
  163. }
  164. // DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
  165. // the email should show 2020-01-01 to 2020-01-04.
  166. $dtendDt->modify('-1 day');
  167. //event that spans over multiple days
  168. $localeStart = $this->l10n->l('date', $dtstartDt, ['width' => 'medium']);
  169. $localeEnd = $this->l10n->l('date', $dtendDt, ['width' => 'medium']);
  170. return $localeStart . ' - ' . $localeEnd;
  171. }
  172. /** @var Property\ICalendar\DateTime $dtstart */
  173. /** @var Property\ICalendar\DateTime $dtend */
  174. $isFloating = $dtstart->isFloating();
  175. $startTimezone = $endTimezone = null;
  176. if (!$isFloating) {
  177. $prop = $dtstart->offsetGet('TZID');
  178. if ($prop instanceof Parameter) {
  179. $startTimezone = $prop->getValue();
  180. }
  181. $prop = $dtend->offsetGet('TZID');
  182. if ($prop instanceof Parameter) {
  183. $endTimezone = $prop->getValue();
  184. }
  185. }
  186. $localeStart = $this->l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
  187. $this->l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
  188. // always show full date with timezone if timezones are different
  189. if ($startTimezone !== $endTimezone) {
  190. $localeEnd = $this->l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
  191. return $localeStart . ' (' . $startTimezone . ') - ' .
  192. $localeEnd . ' (' . $endTimezone . ')';
  193. }
  194. // show only end time if date is the same
  195. if ($dtstartDt->format('Y-m-d') === $dtendDt->format('Y-m-d')) {
  196. $localeEnd = $this->l10n->l('time', $dtendDt, ['width' => 'short']);
  197. } else {
  198. $localeEnd = $this->l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
  199. $this->l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
  200. }
  201. return $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
  202. }
  203. /**
  204. * @param VEvent $vEvent
  205. * @return array
  206. */
  207. public function buildCancelledBodyData(VEvent $vEvent): array {
  208. $defaultVal = '';
  209. $strikethrough = "<span style='text-decoration: line-through'>%s</span>";
  210. $newMeetingWhen = $this->generateWhenString($vEvent);
  211. $newSummary = isset($vEvent->SUMMARY) && (string)$vEvent->SUMMARY !== '' ? (string)$vEvent->SUMMARY : $this->l10n->t('Untitled event');;
  212. $newDescription = isset($vEvent->DESCRIPTION) && (string)$vEvent->DESCRIPTION !== '' ? (string)$vEvent->DESCRIPTION : $defaultVal;
  213. $newUrl = isset($vEvent->URL) && (string)$vEvent->URL !== '' ? sprintf('<a href="%1$s">%1$s</a>', $vEvent->URL) : $defaultVal;
  214. $newLocation = isset($vEvent->LOCATION) && (string)$vEvent->LOCATION !== '' ? (string)$vEvent->LOCATION : $defaultVal;
  215. $data = [];
  216. $data['meeting_when_html'] = $newMeetingWhen === '' ?: sprintf($strikethrough, $newMeetingWhen);
  217. $data['meeting_when'] = $newMeetingWhen;
  218. $data['meeting_title_html'] = sprintf($strikethrough, $newSummary);
  219. $data['meeting_title'] = $newSummary !== '' ? $newSummary: $this->l10n->t('Untitled event');
  220. $data['meeting_description_html'] = $newDescription !== '' ? sprintf($strikethrough, $newDescription) : '';
  221. $data['meeting_description'] = $newDescription;
  222. $data['meeting_url_html'] = $newUrl !== '' ? sprintf($strikethrough, $newUrl) : '';
  223. $data['meeting_url'] = isset($vEvent->URL) ? (string)$vEvent->URL : '';
  224. $data['meeting_location_html'] = $newLocation !== '' ? sprintf($strikethrough, $newLocation) : '';
  225. $data['meeting_location'] = $newLocation;
  226. return $data;
  227. }
  228. /**
  229. * Check if event took place in the past
  230. *
  231. * @param VCalendar $vObject
  232. * @return int
  233. */
  234. public function getLastOccurrence(VCalendar $vObject) {
  235. /** @var VEvent $component */
  236. $component = $vObject->VEVENT;
  237. if (isset($component->RRULE)) {
  238. $it = new EventIterator($vObject, (string)$component->UID);
  239. $maxDate = new \DateTime(IMipPlugin::MAX_DATE);
  240. if ($it->isInfinite()) {
  241. return $maxDate->getTimestamp();
  242. }
  243. $end = $it->getDtEnd();
  244. while ($it->valid() && $end < $maxDate) {
  245. $end = $it->getDtEnd();
  246. $it->next();
  247. }
  248. return $end->getTimestamp();
  249. }
  250. /** @var Property\ICalendar\DateTime $dtStart */
  251. $dtStart = $component->DTSTART;
  252. if (isset($component->DTEND)) {
  253. /** @var Property\ICalendar\DateTime $dtEnd */
  254. $dtEnd = $component->DTEND;
  255. return $dtEnd->getDateTime()->getTimeStamp();
  256. }
  257. if(isset($component->DURATION)) {
  258. /** @var \DateTime $endDate */
  259. $endDate = clone $dtStart->getDateTime();
  260. // $component->DTEND->getDateTime() returns DateTimeImmutable
  261. $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
  262. return $endDate->getTimestamp();
  263. }
  264. if(!$dtStart->hasTime()) {
  265. /** @var \DateTime $endDate */
  266. // $component->DTSTART->getDateTime() returns DateTimeImmutable
  267. $endDate = clone $dtStart->getDateTime();
  268. $endDate = $endDate->modify('+1 day');
  269. return $endDate->getTimestamp();
  270. }
  271. // No computation of end time possible - return start date
  272. return $dtStart->getDateTime()->getTimeStamp();
  273. }
  274. /**
  275. * @param Property|null $attendee
  276. */
  277. public function setL10n(?Property $attendee = null) {
  278. if($attendee === null) {
  279. return;
  280. }
  281. $lang = $attendee->offsetGet('LANGUAGE');
  282. if ($lang instanceof Parameter) {
  283. $lang = $lang->getValue();
  284. $this->l10n = $this->l10nFactory->get('dav', $lang);
  285. }
  286. }
  287. /**
  288. * @param Property|null $attendee
  289. * @return bool
  290. */
  291. public function getAttendeeRsvpOrReqForParticipant(?Property $attendee = null) {
  292. if($attendee === null) {
  293. return false;
  294. }
  295. $rsvp = $attendee->offsetGet('RSVP');
  296. if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
  297. return true;
  298. }
  299. $role = $attendee->offsetGet('ROLE');
  300. // @see https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.16
  301. // Attendees without a role are assumed required and should receive an invitation link even if they have no RSVP set
  302. if ($role === null
  303. || (($role instanceof Parameter) && (strcasecmp($role->getValue(), 'REQ-PARTICIPANT') === 0))
  304. || (($role instanceof Parameter) && (strcasecmp($role->getValue(), 'OPT-PARTICIPANT') === 0))
  305. ) {
  306. return true;
  307. }
  308. // RFC 5545 3.2.17: default RSVP is false
  309. return false;
  310. }
  311. /**
  312. * @param IEMailTemplate $template
  313. * @param string $method
  314. * @param string $sender
  315. * @param string $summary
  316. * @param string|null $partstat
  317. */
  318. public function addSubjectAndHeading(IEMailTemplate $template,
  319. string $method, string $sender, string $summary): void {
  320. if ($method === IMipPlugin::METHOD_CANCEL) {
  321. // TRANSLATORS Subject for email, when an invitation is cancelled. Ex: "Cancelled: {{Event Name}}"
  322. $template->setSubject($this->l10n->t('Cancelled: %1$s', [$summary]));
  323. $template->addHeading($this->l10n->t('"%1$s" has been canceled', [$summary]));
  324. } elseif ($method === IMipPlugin::METHOD_REPLY) {
  325. // TRANSLATORS Subject for email, when an invitation is replied to. Ex: "Re: {{Event Name}}"
  326. $template->setSubject($this->l10n->t('Re: %1$s', [$summary]));
  327. $template->addHeading($this->l10n->t('%1$s has responded to your invitation', [$sender]));
  328. } else {
  329. // TRANSLATORS Subject for email, when an invitation is sent. Ex: "Invitation: {{Event Name}}"
  330. $template->setSubject($this->l10n->t('Invitation: %1$s', [$summary]));
  331. $template->addHeading($this->l10n->t('%1$s would like to invite you to "%2$s"', [$sender, $summary]));
  332. }
  333. }
  334. /**
  335. * @param string $path
  336. * @return string
  337. */
  338. public function getAbsoluteImagePath($path): string {
  339. return $this->urlGenerator->getAbsoluteURL(
  340. $this->urlGenerator->imagePath('core', $path)
  341. );
  342. }
  343. /**
  344. * addAttendees: add organizer and attendee names/emails to iMip mail.
  345. *
  346. * Enable with DAV setting: invitation_list_attendees (default: no)
  347. *
  348. * The default is 'no', which matches old behavior, and is privacy preserving.
  349. *
  350. * To enable including attendees in invitation emails:
  351. * % php occ config:app:set dav invitation_list_attendees --value yes
  352. *
  353. * @param IEMailTemplate $template
  354. * @param IL10N $this->l10n
  355. * @param VEvent $vevent
  356. * @author brad2014 on github.com
  357. */
  358. public function addAttendees(IEMailTemplate $template, VEvent $vevent) {
  359. if ($this->config->getAppValue('dav', 'invitation_list_attendees', 'no') === 'no') {
  360. return;
  361. }
  362. if (isset($vevent->ORGANIZER)) {
  363. /** @var Property | Property\ICalendar\CalAddress $organizer */
  364. $organizer = $vevent->ORGANIZER;
  365. $organizerEmail = substr($organizer->getNormalizedValue(), 7);
  366. /** @var string|null $organizerName */
  367. $organizerName = isset($organizer->CN) ? $organizer->CN->getValue() : null;
  368. $organizerHTML = sprintf('<a href="%s">%s</a>',
  369. htmlspecialchars($organizer->getNormalizedValue()),
  370. htmlspecialchars($organizerName ?: $organizerEmail));
  371. $organizerText = sprintf('%s <%s>', $organizerName, $organizerEmail);
  372. if(isset($organizer['PARTSTAT']) ) {
  373. /** @var Parameter $partstat */
  374. $partstat = $organizer['PARTSTAT'];
  375. if(strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  376. $organizerHTML .= ' ✔︎';
  377. $organizerText .= ' ✔︎';
  378. }
  379. }
  380. $template->addBodyListItem($organizerHTML, $this->l10n->t('Organizer:'),
  381. $this->getAbsoluteImagePath('caldav/organizer.png'),
  382. $organizerText, '', IMipPlugin::IMIP_INDENT);
  383. }
  384. $attendees = $vevent->select('ATTENDEE');
  385. if (count($attendees) === 0) {
  386. return;
  387. }
  388. $attendeesHTML = [];
  389. $attendeesText = [];
  390. foreach ($attendees as $attendee) {
  391. $attendeeEmail = substr($attendee->getNormalizedValue(), 7);
  392. $attendeeName = isset($attendee['CN']) ? $attendee['CN']->getValue() : null;
  393. $attendeeHTML = sprintf('<a href="%s">%s</a>',
  394. htmlspecialchars($attendee->getNormalizedValue()),
  395. htmlspecialchars($attendeeName ?: $attendeeEmail));
  396. $attendeeText = sprintf('%s <%s>', $attendeeName, $attendeeEmail);
  397. if (isset($attendee['PARTSTAT'])) {
  398. /** @var Parameter $partstat */
  399. $partstat = $attendee['PARTSTAT'];
  400. if (strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  401. $attendeeHTML .= ' ✔︎';
  402. $attendeeText .= ' ✔︎';
  403. }
  404. }
  405. $attendeesHTML[] = $attendeeHTML;
  406. $attendeesText[] = $attendeeText;
  407. }
  408. $template->addBodyListItem(implode('<br/>', $attendeesHTML), $this->l10n->t('Attendees:'),
  409. $this->getAbsoluteImagePath('caldav/attendees.png'),
  410. implode("\n", $attendeesText), '', IMipPlugin::IMIP_INDENT);
  411. }
  412. /**
  413. * @param IEMailTemplate $template
  414. * @param VEVENT $vevent
  415. * @param $data
  416. */
  417. public function addBulletList(IEMailTemplate $template, VEvent $vevent, $data) {
  418. $template->addBodyListItem(
  419. $data['meeting_title_html'] ?? $data['meeting_title'], $this->l10n->t('Title:'),
  420. $this->getAbsoluteImagePath('caldav/title.png'), $data['meeting_title'], '', IMipPlugin::IMIP_INDENT);
  421. if ($data['meeting_when'] !== '') {
  422. $template->addBodyListItem($data['meeting_when_html'] ?? $data['meeting_when'], $this->l10n->t('Time:'),
  423. $this->getAbsoluteImagePath('caldav/time.png'), $data['meeting_when'], '', IMipPlugin::IMIP_INDENT);
  424. }
  425. if ($data['meeting_location'] !== '') {
  426. $template->addBodyListItem($data['meeting_location_html'] ?? $data['meeting_location'], $this->l10n->t('Location:'),
  427. $this->getAbsoluteImagePath('caldav/location.png'), $data['meeting_location'], '', IMipPlugin::IMIP_INDENT);
  428. }
  429. if ($data['meeting_url'] !== '') {
  430. $template->addBodyListItem($data['meeting_url_html'] ?? $data['meeting_url'], $this->l10n->t('Link:'),
  431. $this->getAbsoluteImagePath('caldav/link.png'), $data['meeting_url'], '', IMipPlugin::IMIP_INDENT);
  432. }
  433. $this->addAttendees($template, $vevent);
  434. /* Put description last, like an email body, since it can be arbitrarily long */
  435. if ($data['meeting_description']) {
  436. $template->addBodyListItem($data['meeting_description_html'] ?? $data['meeting_description'], $this->l10n->t('Description:'),
  437. $this->getAbsoluteImagePath('caldav/description.png'), $data['meeting_description'], '', IMipPlugin::IMIP_INDENT);
  438. }
  439. }
  440. /**
  441. * @param Message $iTipMessage
  442. * @return null|Property
  443. */
  444. public function getCurrentAttendee(Message $iTipMessage): ?Property {
  445. /** @var VEvent $vevent */
  446. $vevent = $iTipMessage->message->VEVENT;
  447. $attendees = $vevent->select('ATTENDEE');
  448. foreach ($attendees as $attendee) {
  449. /** @var Property $attendee */
  450. if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
  451. return $attendee;
  452. }
  453. }
  454. return null;
  455. }
  456. /**
  457. * @param Message $iTipMessage
  458. * @param VEvent $vevent
  459. * @param int $lastOccurrence
  460. * @return string
  461. */
  462. public function createInvitationToken(Message $iTipMessage, VEvent $vevent, int $lastOccurrence): string {
  463. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  464. $attendee = $iTipMessage->recipient;
  465. $organizer = $iTipMessage->sender;
  466. $sequence = $iTipMessage->sequence;
  467. $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
  468. $vevent->{'RECURRENCE-ID'}->serialize() : null;
  469. $uid = $vevent->{'UID'};
  470. $query = $this->db->getQueryBuilder();
  471. $query->insert('calendar_invitations')
  472. ->values([
  473. 'token' => $query->createNamedParameter($token),
  474. 'attendee' => $query->createNamedParameter($attendee),
  475. 'organizer' => $query->createNamedParameter($organizer),
  476. 'sequence' => $query->createNamedParameter($sequence),
  477. 'recurrenceid' => $query->createNamedParameter($recurrenceId),
  478. 'expiration' => $query->createNamedParameter($lastOccurrence),
  479. 'uid' => $query->createNamedParameter($uid)
  480. ])
  481. ->execute();
  482. return $token;
  483. }
  484. /**
  485. * Create a valid VCalendar object out of the details of
  486. * a VEvent and its associated iTip Message
  487. *
  488. * We do this to filter out all unchanged VEvents
  489. * This is especially important in iTip Messages with recurrences
  490. * and recurrence exceptions
  491. *
  492. * @param Message $iTipMessage
  493. * @param VEvent $vEvent
  494. * @return VCalendar
  495. */
  496. public function generateVCalendar(Message $iTipMessage, VEvent $vEvent): VCalendar {
  497. $vCalendar = new VCalendar();
  498. $vCalendar->add('METHOD', $iTipMessage->method);
  499. foreach ($iTipMessage->message->getComponents() as $component) {
  500. if ($component instanceof VEvent) {
  501. continue;
  502. }
  503. $vCalendar->add(clone $component);
  504. }
  505. $vCalendar->add($vEvent);
  506. return $vCalendar;
  507. }
  508. /**
  509. * @param IEMailTemplate $template
  510. * @param $token
  511. */
  512. public function addResponseButtons(IEMailTemplate $template, $token) {
  513. $template->addBodyButtonGroup(
  514. $this->l10n->t('Accept'),
  515. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
  516. 'token' => $token,
  517. ]),
  518. $this->l10n->t('Decline'),
  519. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
  520. 'token' => $token,
  521. ])
  522. );
  523. }
  524. public function addMoreOptionsButton(IEMailTemplate $template, $token) {
  525. $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
  526. 'token' => $token,
  527. ]);
  528. $html = vsprintf('<small><a href="%s">%s</a></small>', [
  529. $moreOptionsURL, $this->l10n->t('More options …')
  530. ]);
  531. $text = $this->l10n->t('More options at %s', [$moreOptionsURL]);
  532. $template->addBodyText($html, $text);
  533. }
  534. }