IMipService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. * @param bool $isModified
  318. */
  319. public function addSubjectAndHeading(IEMailTemplate $template,
  320. string $method, string $sender, string $summary, bool $isModified, ?Property $replyingAttendee = null): void {
  321. if ($method === IMipPlugin::METHOD_CANCEL) {
  322. // TRANSLATORS Subject for email, when an invitation is cancelled. Ex: "Cancelled: {{Event Name}}"
  323. $template->setSubject($this->l10n->t('Cancelled: %1$s', [$summary]));
  324. $template->addHeading($this->l10n->t('"%1$s" has been canceled', [$summary]));
  325. } elseif ($method === IMipPlugin::METHOD_REPLY) {
  326. // TRANSLATORS Subject for email, when an invitation is replied to. Ex: "Re: {{Event Name}}"
  327. $template->setSubject($this->l10n->t('Re: %1$s', [$summary]));
  328. // Build the strings
  329. $partstat = (isset($replyingAttendee)) ? $replyingAttendee->offsetGet('PARTSTAT') : null;
  330. $partstat = ($partstat instanceof Parameter) ? $partstat->getValue() : null;
  331. switch ($partstat) {
  332. case 'ACCEPTED':
  333. $template->addHeading($this->l10n->t('%1$s has accepted your invitation', [$sender]));
  334. break;
  335. case 'TENTATIVE':
  336. $template->addHeading($this->l10n->t('%1$s has tentatively accepted your invitation', [$sender]));
  337. break;
  338. case 'DECLINED':
  339. $template->addHeading($this->l10n->t('%1$s has declined your invitation', [$sender]));
  340. break;
  341. case null:
  342. default:
  343. $template->addHeading($this->l10n->t('%1$s has responded to your invitation', [$sender]));
  344. break;
  345. }
  346. } elseif ($method === IMipPlugin::METHOD_REQUEST && $isModified) {
  347. // TRANSLATORS Subject for email, when an invitation is updated. Ex: "Invitation updated: {{Event Name}}"
  348. $template->setSubject($this->l10n->t('Invitation updated: %1$s', [$summary]));
  349. $template->addHeading($this->l10n->t('%1$s updated the event "%2$s"', [$sender, $summary]));
  350. } else {
  351. // TRANSLATORS Subject for email, when an invitation is sent. Ex: "Invitation: {{Event Name}}"
  352. $template->setSubject($this->l10n->t('Invitation: %1$s', [$summary]));
  353. $template->addHeading($this->l10n->t('%1$s would like to invite you to "%2$s"', [$sender, $summary]));
  354. }
  355. }
  356. /**
  357. * @param string $path
  358. * @return string
  359. */
  360. public function getAbsoluteImagePath($path): string {
  361. return $this->urlGenerator->getAbsoluteURL(
  362. $this->urlGenerator->imagePath('core', $path)
  363. );
  364. }
  365. /**
  366. * addAttendees: add organizer and attendee names/emails to iMip mail.
  367. *
  368. * Enable with DAV setting: invitation_list_attendees (default: no)
  369. *
  370. * The default is 'no', which matches old behavior, and is privacy preserving.
  371. *
  372. * To enable including attendees in invitation emails:
  373. * % php occ config:app:set dav invitation_list_attendees --value yes
  374. *
  375. * @param IEMailTemplate $template
  376. * @param IL10N $this->l10n
  377. * @param VEvent $vevent
  378. * @author brad2014 on github.com
  379. */
  380. public function addAttendees(IEMailTemplate $template, VEvent $vevent) {
  381. if ($this->config->getAppValue('dav', 'invitation_list_attendees', 'no') === 'no') {
  382. return;
  383. }
  384. if (isset($vevent->ORGANIZER)) {
  385. /** @var Property | Property\ICalendar\CalAddress $organizer */
  386. $organizer = $vevent->ORGANIZER;
  387. $organizerEmail = substr($organizer->getNormalizedValue(), 7);
  388. /** @var string|null $organizerName */
  389. $organizerName = isset($organizer->CN) ? $organizer->CN->getValue() : null;
  390. $organizerHTML = sprintf('<a href="%s">%s</a>',
  391. htmlspecialchars($organizer->getNormalizedValue()),
  392. htmlspecialchars($organizerName ?: $organizerEmail));
  393. $organizerText = sprintf('%s <%s>', $organizerName, $organizerEmail);
  394. if(isset($organizer['PARTSTAT']) ) {
  395. /** @var Parameter $partstat */
  396. $partstat = $organizer['PARTSTAT'];
  397. if(strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  398. $organizerHTML .= ' ✔︎';
  399. $organizerText .= ' ✔︎';
  400. }
  401. }
  402. $template->addBodyListItem($organizerHTML, $this->l10n->t('Organizer:'),
  403. $this->getAbsoluteImagePath('caldav/organizer.png'),
  404. $organizerText, '', IMipPlugin::IMIP_INDENT);
  405. }
  406. $attendees = $vevent->select('ATTENDEE');
  407. if (count($attendees) === 0) {
  408. return;
  409. }
  410. $attendeesHTML = [];
  411. $attendeesText = [];
  412. foreach ($attendees as $attendee) {
  413. $attendeeEmail = substr($attendee->getNormalizedValue(), 7);
  414. $attendeeName = isset($attendee['CN']) ? $attendee['CN']->getValue() : null;
  415. $attendeeHTML = sprintf('<a href="%s">%s</a>',
  416. htmlspecialchars($attendee->getNormalizedValue()),
  417. htmlspecialchars($attendeeName ?: $attendeeEmail));
  418. $attendeeText = sprintf('%s <%s>', $attendeeName, $attendeeEmail);
  419. if (isset($attendee['PARTSTAT'])) {
  420. /** @var Parameter $partstat */
  421. $partstat = $attendee['PARTSTAT'];
  422. if (strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  423. $attendeeHTML .= ' ✔︎';
  424. $attendeeText .= ' ✔︎';
  425. }
  426. }
  427. $attendeesHTML[] = $attendeeHTML;
  428. $attendeesText[] = $attendeeText;
  429. }
  430. $template->addBodyListItem(implode('<br/>', $attendeesHTML), $this->l10n->t('Attendees:'),
  431. $this->getAbsoluteImagePath('caldav/attendees.png'),
  432. implode("\n", $attendeesText), '', IMipPlugin::IMIP_INDENT);
  433. }
  434. /**
  435. * @param IEMailTemplate $template
  436. * @param VEVENT $vevent
  437. * @param $data
  438. */
  439. public function addBulletList(IEMailTemplate $template, VEvent $vevent, $data) {
  440. $template->addBodyListItem(
  441. $data['meeting_title_html'] ?? $data['meeting_title'], $this->l10n->t('Title:'),
  442. $this->getAbsoluteImagePath('caldav/title.png'), $data['meeting_title'], '', IMipPlugin::IMIP_INDENT);
  443. if ($data['meeting_when'] !== '') {
  444. $template->addBodyListItem($data['meeting_when_html'] ?? $data['meeting_when'], $this->l10n->t('Time:'),
  445. $this->getAbsoluteImagePath('caldav/time.png'), $data['meeting_when'], '', IMipPlugin::IMIP_INDENT);
  446. }
  447. if ($data['meeting_location'] !== '') {
  448. $template->addBodyListItem($data['meeting_location_html'] ?? $data['meeting_location'], $this->l10n->t('Location:'),
  449. $this->getAbsoluteImagePath('caldav/location.png'), $data['meeting_location'], '', IMipPlugin::IMIP_INDENT);
  450. }
  451. if ($data['meeting_url'] !== '') {
  452. $template->addBodyListItem($data['meeting_url_html'] ?? $data['meeting_url'], $this->l10n->t('Link:'),
  453. $this->getAbsoluteImagePath('caldav/link.png'), $data['meeting_url'], '', IMipPlugin::IMIP_INDENT);
  454. }
  455. $this->addAttendees($template, $vevent);
  456. /* Put description last, like an email body, since it can be arbitrarily long */
  457. if ($data['meeting_description']) {
  458. $template->addBodyListItem($data['meeting_description_html'] ?? $data['meeting_description'], $this->l10n->t('Description:'),
  459. $this->getAbsoluteImagePath('caldav/description.png'), $data['meeting_description'], '', IMipPlugin::IMIP_INDENT);
  460. }
  461. }
  462. /**
  463. * @param Message $iTipMessage
  464. * @return null|Property
  465. */
  466. public function getCurrentAttendee(Message $iTipMessage): ?Property {
  467. /** @var VEvent $vevent */
  468. $vevent = $iTipMessage->message->VEVENT;
  469. $attendees = $vevent->select('ATTENDEE');
  470. foreach ($attendees as $attendee) {
  471. /** @var Property $attendee */
  472. if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
  473. return $attendee;
  474. }
  475. }
  476. return null;
  477. }
  478. /**
  479. * @param Message $iTipMessage
  480. * @param VEvent $vevent
  481. * @param int $lastOccurrence
  482. * @return string
  483. */
  484. public function createInvitationToken(Message $iTipMessage, VEvent $vevent, int $lastOccurrence): string {
  485. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  486. $attendee = $iTipMessage->recipient;
  487. $organizer = $iTipMessage->sender;
  488. $sequence = $iTipMessage->sequence;
  489. $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
  490. $vevent->{'RECURRENCE-ID'}->serialize() : null;
  491. $uid = $vevent->{'UID'};
  492. $query = $this->db->getQueryBuilder();
  493. $query->insert('calendar_invitations')
  494. ->values([
  495. 'token' => $query->createNamedParameter($token),
  496. 'attendee' => $query->createNamedParameter($attendee),
  497. 'organizer' => $query->createNamedParameter($organizer),
  498. 'sequence' => $query->createNamedParameter($sequence),
  499. 'recurrenceid' => $query->createNamedParameter($recurrenceId),
  500. 'expiration' => $query->createNamedParameter($lastOccurrence),
  501. 'uid' => $query->createNamedParameter($uid)
  502. ])
  503. ->execute();
  504. return $token;
  505. }
  506. /**
  507. * Create a valid VCalendar object out of the details of
  508. * a VEvent and its associated iTip Message
  509. *
  510. * We do this to filter out all unchanged VEvents
  511. * This is especially important in iTip Messages with recurrences
  512. * and recurrence exceptions
  513. *
  514. * @param Message $iTipMessage
  515. * @param VEvent $vEvent
  516. * @return VCalendar
  517. */
  518. public function generateVCalendar(Message $iTipMessage, VEvent $vEvent): VCalendar {
  519. $vCalendar = new VCalendar();
  520. $vCalendar->add('METHOD', $iTipMessage->method);
  521. foreach ($iTipMessage->message->getComponents() as $component) {
  522. if ($component instanceof VEvent) {
  523. continue;
  524. }
  525. $vCalendar->add(clone $component);
  526. }
  527. $vCalendar->add($vEvent);
  528. return $vCalendar;
  529. }
  530. /**
  531. * @param IEMailTemplate $template
  532. * @param $token
  533. */
  534. public function addResponseButtons(IEMailTemplate $template, $token) {
  535. $template->addBodyButtonGroup(
  536. $this->l10n->t('Accept'),
  537. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
  538. 'token' => $token,
  539. ]),
  540. $this->l10n->t('Decline'),
  541. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
  542. 'token' => $token,
  543. ])
  544. );
  545. }
  546. public function addMoreOptionsButton(IEMailTemplate $template, $token) {
  547. $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
  548. 'token' => $token,
  549. ]);
  550. $html = vsprintf('<small><a href="%s">%s</a></small>', [
  551. $moreOptionsURL, $this->l10n->t('More options …')
  552. ]);
  553. $text = $this->l10n->t('More options at %s', [$moreOptionsURL]);
  554. $template->addBodyText($html, $text);
  555. }
  556. public function getReplyingAttendee(Message $iTipMessage): ?Property {
  557. /** @var VEvent $vevent */
  558. $vevent = $iTipMessage->message->VEVENT;
  559. $attendees = $vevent->select('ATTENDEE');
  560. foreach ($attendees as $attendee) {
  561. /** @var Property $attendee */
  562. if (strcasecmp($attendee->getValue(), $iTipMessage->sender) === 0) {
  563. return $attendee;
  564. }
  565. }
  566. return null;
  567. }
  568. }