IMipService.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Schedule;
  8. use OC\URLGenerator;
  9. use OCA\DAV\CalDAV\EventReader;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\IConfig;
  12. use OCP\IDBConnection;
  13. use OCP\IL10N;
  14. use OCP\L10N\IFactory as L10NFactory;
  15. use OCP\Mail\IEMailTemplate;
  16. use OCP\Security\ISecureRandom;
  17. use Sabre\VObject\Component\VCalendar;
  18. use Sabre\VObject\Component\VEvent;
  19. use Sabre\VObject\DateTimeParser;
  20. use Sabre\VObject\ITip\Message;
  21. use Sabre\VObject\Parameter;
  22. use Sabre\VObject\Property;
  23. use Sabre\VObject\Recur\EventIterator;
  24. class IMipService {
  25. private IL10N $l10n;
  26. /** @var string[] */
  27. private const STRING_DIFF = [
  28. 'meeting_title' => 'SUMMARY',
  29. 'meeting_description' => 'DESCRIPTION',
  30. 'meeting_url' => 'URL',
  31. 'meeting_location' => 'LOCATION'
  32. ];
  33. public function __construct(
  34. private URLGenerator $urlGenerator,
  35. private IConfig $config,
  36. private IDBConnection $db,
  37. private ISecureRandom $random,
  38. private L10NFactory $l10nFactory,
  39. private ITimeFactory $timeFactory,
  40. ) {
  41. $default = $this->l10nFactory->findGenericLanguage();
  42. $this->l10n = $this->l10nFactory->get('dav', $default);
  43. }
  44. /**
  45. * @param string|null $senderName
  46. * @param string $default
  47. * @return string
  48. */
  49. public function getFrom(?string $senderName, string $default): string {
  50. if ($senderName === null) {
  51. return $default;
  52. }
  53. return $this->l10n->t('%1$s via %2$s', [$senderName, $default]);
  54. }
  55. public static function readPropertyWithDefault(VEvent $vevent, string $property, string $default) {
  56. if (isset($vevent->$property)) {
  57. $value = $vevent->$property->getValue();
  58. if (!empty($value)) {
  59. return $value;
  60. }
  61. }
  62. return $default;
  63. }
  64. private function generateDiffString(VEvent $vevent, VEvent $oldVEvent, string $property, string $default): ?string {
  65. $strikethrough = "<span style='text-decoration: line-through'>%s</span><br />%s";
  66. if (!isset($vevent->$property)) {
  67. return $default;
  68. }
  69. $newstring = $vevent->$property->getValue();
  70. if (isset($oldVEvent->$property) && $oldVEvent->$property->getValue() !== $newstring) {
  71. $oldstring = $oldVEvent->$property->getValue();
  72. return sprintf($strikethrough, $oldstring, $newstring);
  73. }
  74. return $newstring;
  75. }
  76. /**
  77. * Like generateDiffString() but linkifies the property values if they are urls.
  78. */
  79. private function generateLinkifiedDiffString(VEvent $vevent, VEvent $oldVEvent, string $property, string $default): ?string {
  80. if (!isset($vevent->$property)) {
  81. return $default;
  82. }
  83. /** @var string|null $newString */
  84. $newString = $vevent->$property->getValue();
  85. $oldString = isset($oldVEvent->$property) ? $oldVEvent->$property->getValue() : null;
  86. if ($oldString !== $newString) {
  87. return sprintf(
  88. "<span style='text-decoration: line-through'>%s</span><br />%s",
  89. $this->linkify($oldString) ?? $oldString ?? '',
  90. $this->linkify($newString) ?? $newString ?? ''
  91. );
  92. }
  93. return $this->linkify($newString) ?? $newString;
  94. }
  95. /**
  96. * Convert a given url to a html link element or return null otherwise.
  97. */
  98. private function linkify(?string $url): ?string {
  99. if ($url === null) {
  100. return null;
  101. }
  102. if (!str_starts_with($url, 'http://') && !str_starts_with($url, 'https://')) {
  103. return null;
  104. }
  105. return sprintf('<a href="%1$s">%1$s</a>', htmlspecialchars($url));
  106. }
  107. /**
  108. * @param VEvent $vEvent
  109. * @param VEvent|null $oldVEvent
  110. * @return array
  111. */
  112. public function buildBodyData(VEvent $vEvent, ?VEvent $oldVEvent): array {
  113. // construct event reader
  114. $eventReaderCurrent = new EventReader($vEvent);
  115. $eventReaderPrevious = !empty($oldVEvent) ? new EventReader($oldVEvent) : null;
  116. $defaultVal = '';
  117. $data = [];
  118. $data['meeting_when'] = $this->generateWhenString($eventReaderCurrent);
  119. foreach (self::STRING_DIFF as $key => $property) {
  120. $data[$key] = self::readPropertyWithDefault($vEvent, $property, $defaultVal);
  121. }
  122. $data['meeting_url_html'] = self::readPropertyWithDefault($vEvent, 'URL', $defaultVal);
  123. if (($locationHtml = $this->linkify($data['meeting_location'])) !== null) {
  124. $data['meeting_location_html'] = $locationHtml;
  125. }
  126. if (!empty($oldVEvent)) {
  127. $oldMeetingWhen = $this->generateWhenString($eventReaderPrevious);
  128. $data['meeting_title_html'] = $this->generateDiffString($vEvent, $oldVEvent, 'SUMMARY', $data['meeting_title']);
  129. $data['meeting_description_html'] = $this->generateDiffString($vEvent, $oldVEvent, 'DESCRIPTION', $data['meeting_description']);
  130. $data['meeting_location_html'] = $this->generateLinkifiedDiffString($vEvent, $oldVEvent, 'LOCATION', $data['meeting_location']);
  131. $oldUrl = self::readPropertyWithDefault($oldVEvent, 'URL', $defaultVal);
  132. $data['meeting_url_html'] = !empty($oldUrl) && $oldUrl !== $data['meeting_url'] ? sprintf('<a href="%1$s">%1$s</a>', $oldUrl) : $data['meeting_url'];
  133. $data['meeting_when_html'] = $oldMeetingWhen !== $data['meeting_when'] ? sprintf("<span style='text-decoration: line-through'>%s</span><br />%s", $oldMeetingWhen, $data['meeting_when']) : $data['meeting_when'];
  134. }
  135. // generate occurring next string
  136. if ($eventReaderCurrent->recurs()) {
  137. $data['meeting_occurring'] = $this->generateOccurringString($eventReaderCurrent);
  138. }
  139. return $data;
  140. }
  141. /**
  142. * generates a when string based on if a event has an recurrence or not
  143. *
  144. * @since 30.0.0
  145. *
  146. * @param EventReader $er
  147. *
  148. * @return string
  149. */
  150. public function generateWhenString(EventReader $er): string {
  151. return match ($er->recurs()) {
  152. true => $this->generateWhenStringRecurring($er),
  153. false => $this->generateWhenStringSingular($er)
  154. };
  155. }
  156. /**
  157. * generates a when string for a non recurring event
  158. *
  159. * @since 30.0.0
  160. *
  161. * @param EventReader $er
  162. *
  163. * @return string
  164. */
  165. public function generateWhenStringSingular(EventReader $er): string {
  166. // calculate time difference from now to start of event
  167. $occurring = $this->minimizeInterval($this->timeFactory->getDateTime()->diff($er->recurrenceDate()));
  168. // extract start date
  169. $startDate = $this->l10n->l('date', $er->startDateTime(), ['width' => 'full']);
  170. // time of the day
  171. if (!$er->entireDay()) {
  172. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  173. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  174. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  175. }
  176. // generate localized when string
  177. // TRANSLATORS
  178. // Indicates when a calendar event will happen, shown on invitation emails
  179. // Output produced in order:
  180. // In a day/week/month/year on July 1, 2024 for the entire day
  181. // In a day/week/month/year on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
  182. // In 2 days/weeks/months/years on July 1, 2024 for the entire day
  183. // In 2 days/weeks/months/years on July 1, 2024 between 8:00 AM - 9:00 AM (America/Toronto)
  184. return match ([($occurring[0] > 1), !empty($endTime)]) {
  185. [false, false] => $this->l10n->t('In a %1$s on %2$s for the entire day', [$occurring[1], $startDate]),
  186. [false, true] => $this->l10n->t('In a %1$s on %2$s between %3$s - %4$s', [$occurring[1], $startDate, $startTime, $endTime]),
  187. [true, false] => $this->l10n->t('In %1$s %2$s on %3$s for the entire day', [$occurring[0], $occurring[1], $startDate]),
  188. [true, true] => $this->l10n->t('In %1$s %2$s on %3$s between %4$s - %5$s', [$occurring[0], $occurring[1], $startDate, $startTime, $endTime]),
  189. default => $this->l10n->t('Could not generate when statement')
  190. };
  191. }
  192. /**
  193. * generates a when string based on recurrence precision/frequency
  194. *
  195. * @since 30.0.0
  196. *
  197. * @param EventReader $er
  198. *
  199. * @return string
  200. */
  201. public function generateWhenStringRecurring(EventReader $er): string {
  202. return match ($er->recurringPrecision()) {
  203. 'daily' => $this->generateWhenStringRecurringDaily($er),
  204. 'weekly' => $this->generateWhenStringRecurringWeekly($er),
  205. 'monthly' => $this->generateWhenStringRecurringMonthly($er),
  206. 'yearly' => $this->generateWhenStringRecurringYearly($er),
  207. 'fixed' => $this->generateWhenStringRecurringFixed($er),
  208. };
  209. }
  210. /**
  211. * generates a when string for a daily precision/frequency
  212. *
  213. * @since 30.0.0
  214. *
  215. * @param EventReader $er
  216. *
  217. * @return string
  218. */
  219. public function generateWhenStringRecurringDaily(EventReader $er): string {
  220. // initialize
  221. $interval = (int)$er->recurringInterval();
  222. $startTime = '';
  223. $endTime = '';
  224. $conclusion = '';
  225. // time of the day
  226. if (!$er->entireDay()) {
  227. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  228. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  229. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  230. }
  231. // conclusion
  232. if ($er->recurringConcludes()) {
  233. $conclusion = $this->l10n->l('date', $er->recurringConcludesOn(), ['width' => 'long']);
  234. }
  235. // generate localized when string
  236. // TRANSLATORS
  237. // Indicates when a calendar event will happen, shown on invitation emails
  238. // Output produced in order:
  239. // Every Day for the entire day
  240. // Every Day for the entire day until July 13, 2024
  241. // Every Day between 8:00 AM - 9:00 AM (America/Toronto)
  242. // Every Day between 8:00 AM - 9:00 AM (America/Toronto) until July 13, 2024
  243. // Every 3 Days for the entire day
  244. // Every 3 Days for the entire day until July 13, 2024
  245. // Every 3 Days between 8:00 AM - 9:00 AM (America/Toronto)
  246. // Every 3 Days between 8:00 AM - 9:00 AM (America/Toronto) until July 13, 2024
  247. return match ([($interval > 1), !empty($startTime), !empty($conclusion)]) {
  248. [false, false, false] => $this->l10n->t('Every Day for the entire day'),
  249. [false, false, true] => $this->l10n->t('Every Day for the entire day until %1$s', [$conclusion]),
  250. [false, true, false] => $this->l10n->t('Every Day between %1$s - %2$s', [$startTime, $endTime]),
  251. [false, true, true] => $this->l10n->t('Every Day between %1$s - %2$s until %3$s', [$startTime, $endTime, $conclusion]),
  252. [true, false, false] => $this->l10n->t('Every %1$d Days for the entire day', [$interval]),
  253. [true, false, true] => $this->l10n->t('Every %1$d Days for the entire day until %2$s', [$interval, $conclusion]),
  254. [true, true, false] => $this->l10n->t('Every %1$d Days between %2$s - %3$s', [$interval, $startTime, $endTime]),
  255. [true, true, true] => $this->l10n->t('Every %1$d Days between %2$s - %3$s until %4$s', [$interval, $startTime, $endTime, $conclusion]),
  256. default => $this->l10n->t('Could not generate event recurrence statement')
  257. };
  258. }
  259. /**
  260. * generates a when string for a weekly precision/frequency
  261. *
  262. * @since 30.0.0
  263. *
  264. * @param EventReader $er
  265. *
  266. * @return string
  267. */
  268. public function generateWhenStringRecurringWeekly(EventReader $er): string {
  269. // initialize
  270. $interval = (int)$er->recurringInterval();
  271. $startTime = '';
  272. $endTime = '';
  273. $conclusion = '';
  274. // days of the week
  275. $days = implode(', ', array_map(function ($value) { return $this->localizeDayName($value); }, $er->recurringDaysOfWeekNamed()));
  276. // time of the day
  277. if (!$er->entireDay()) {
  278. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  279. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  280. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  281. }
  282. // conclusion
  283. if ($er->recurringConcludes()) {
  284. $conclusion = $this->l10n->l('date', $er->recurringConcludesOn(), ['width' => 'long']);
  285. }
  286. // generate localized when string
  287. // TRANSLATORS
  288. // Indicates when a calendar event will happen, shown on invitation emails
  289. // Output produced in order:
  290. // Every Week on Monday, Wednesday, Friday for the entire day
  291. // Every Week on Monday, Wednesday, Friday for the entire day until July 13, 2024
  292. // Every Week on Monday, Wednesday, Friday between 8:00 AM - 9:00 AM (America/Toronto)
  293. // Every Week on Monday, Wednesday, Friday between 8:00 AM - 9:00 AM (America/Toronto) until July 13, 2024
  294. // Every 2 Weeks on Monday, Wednesday, Friday for the entire day
  295. // Every 2 Weeks on Monday, Wednesday, Friday for the entire day until July 13, 2024
  296. // Every 2 Weeks on Monday, Wednesday, Friday between 8:00 AM - 9:00 AM (America/Toronto)
  297. // Every 2 Weeks on Monday, Wednesday, Friday between 8:00 AM - 9:00 AM (America/Toronto) until July 13, 2024
  298. return match ([($interval > 1), !empty($startTime), !empty($conclusion)]) {
  299. [false, false, false] => $this->l10n->t('Every Week on %1$s for the entire day', [$days]),
  300. [false, false, true] => $this->l10n->t('Every Week on %1$s for the entire day until %2$s', [$days, $conclusion]),
  301. [false, true, false] => $this->l10n->t('Every Week on %1$s between %2$s - %3$s', [$days, $startTime, $endTime]),
  302. [false, true, true] => $this->l10n->t('Every Week on %1$s between %2$s - %3$s until %4$s', [$days, $startTime, $endTime, $conclusion]),
  303. [true, false, false] => $this->l10n->t('Every %1$d Weeks on %2$s for the entire day', [$interval, $days]),
  304. [true, false, true] => $this->l10n->t('Every %1$d Weeks on %2$s for the entire day until %3$s', [$interval, $days, $conclusion]),
  305. [true, true, false] => $this->l10n->t('Every %1$d Weeks on %2$s between %3$s - %4$s', [$interval, $days, $startTime, $endTime]),
  306. [true, true, true] => $this->l10n->t('Every %1$d Weeks on %2$s between %3$s - %4$s until %5$s', [$interval, $days, $startTime, $endTime, $conclusion]),
  307. default => $this->l10n->t('Could not generate event recurrence statement')
  308. };
  309. }
  310. /**
  311. * generates a when string for a monthly precision/frequency
  312. *
  313. * @since 30.0.0
  314. *
  315. * @param EventReader $er
  316. *
  317. * @return string
  318. */
  319. public function generateWhenStringRecurringMonthly(EventReader $er): string {
  320. // initialize
  321. $interval = (int)$er->recurringInterval();
  322. $startTime = '';
  323. $endTime = '';
  324. $conclusion = '';
  325. // days of month
  326. if ($er->recurringPattern() === 'R') {
  327. $days = implode(', ', array_map(function ($value) { return $this->localizeRelativePositionName($value); }, $er->recurringRelativePositionNamed())) . ' ' .
  328. implode(', ', array_map(function ($value) { return $this->localizeDayName($value); }, $er->recurringDaysOfWeekNamed()));
  329. } else {
  330. $days = implode(', ', $er->recurringDaysOfMonth());
  331. }
  332. // time of the day
  333. if (!$er->entireDay()) {
  334. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  335. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  336. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  337. }
  338. // conclusion
  339. if ($er->recurringConcludes()) {
  340. $conclusion = $this->l10n->l('date', $er->recurringConcludesOn(), ['width' => 'long']);
  341. }
  342. // generate localized when string
  343. // TRANSLATORS
  344. // Indicates when a calendar event will happen, shown on invitation emails
  345. // Output produced in order, output varies depending on if the event is absolute or releative:
  346. // Absolute: Every Month on the 1, 8 for the entire day
  347. // Relative: Every Month on the First Sunday, Saturday for the entire day
  348. // Absolute: Every Month on the 1, 8 for the entire day until December 31, 2024
  349. // Relative: Every Month on the First Sunday, Saturday for the entire day until December 31, 2024
  350. // Absolute: Every Month on the 1, 8 between 8:00 AM - 9:00 AM (America/Toronto)
  351. // Relative: Every Month on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto)
  352. // Absolute: Every Month on the 1, 8 between 8:00 AM - 9:00 AM (America/Toronto) until December 31, 2024
  353. // Relative: Every Month on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto) until December 31, 2024
  354. // Absolute: Every 2 Months on the 1, 8 for the entire day
  355. // Relative: Every 2 Months on the First Sunday, Saturday for the entire day
  356. // Absolute: Every 2 Months on the 1, 8 for the entire day until December 31, 2024
  357. // Relative: Every 2 Months on the First Sunday, Saturday for the entire day until December 31, 2024
  358. // Absolute: Every 2 Months on the 1, 8 between 8:00 AM - 9:00 AM (America/Toronto)
  359. // Relative: Every 2 Months on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto)
  360. // Absolute: Every 2 Months on the 1, 8 between 8:00 AM - 9:00 AM (America/Toronto) until December 31, 2024
  361. // Relative: Every 2 Months on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto) until December 31, 2024
  362. return match ([($interval > 1), !empty($startTime), !empty($conclusion)]) {
  363. [false, false, false] => $this->l10n->t('Every Month on the %1$s for the entire day', [$days]),
  364. [false, false, true] => $this->l10n->t('Every Month on the %1$s for the entire day until %2$s', [$days, $conclusion]),
  365. [false, true, false] => $this->l10n->t('Every Month on the %1$s between %2$s - %3$s', [$days, $startTime, $endTime]),
  366. [false, true, true] => $this->l10n->t('Every Month on the %1$s between %2$s - %3$s until %4$s', [$days, $startTime, $endTime, $conclusion]),
  367. [true, false, false] => $this->l10n->t('Every %1$d Months on the %2$s for the entire day', [$interval, $days]),
  368. [true, false, true] => $this->l10n->t('Every %1$d Months on the %2$s for the entire day until %3$s', [$interval, $days, $conclusion]),
  369. [true, true, false] => $this->l10n->t('Every %1$d Months on the %2$s between %3$s - %4$s', [$interval, $days, $startTime, $endTime]),
  370. [true, true, true] => $this->l10n->t('Every %1$d Months on the %2$s between %3$s - %4$s until %5$s', [$interval, $days, $startTime, $endTime, $conclusion]),
  371. default => $this->l10n->t('Could not generate event recurrence statement')
  372. };
  373. }
  374. /**
  375. * generates a when string for a yearly precision/frequency
  376. *
  377. * @since 30.0.0
  378. *
  379. * @param EventReader $er
  380. *
  381. * @return string
  382. */
  383. public function generateWhenStringRecurringYearly(EventReader $er): string {
  384. // initialize
  385. $interval = (int)$er->recurringInterval();
  386. $startTime = '';
  387. $endTime = '';
  388. $conclusion = '';
  389. // months of year
  390. $months = implode(', ', array_map(function ($value) { return $this->localizeMonthName($value); }, $er->recurringMonthsOfYearNamed()));
  391. // days of month
  392. if ($er->recurringPattern() === 'R') {
  393. $days = implode(', ', array_map(function ($value) { return $this->localizeRelativePositionName($value); }, $er->recurringRelativePositionNamed())) . ' ' .
  394. implode(', ', array_map(function ($value) { return $this->localizeDayName($value); }, $er->recurringDaysOfWeekNamed()));
  395. } else {
  396. $days = $er->startDateTime()->format('jS');
  397. }
  398. // time of the day
  399. if (!$er->entireDay()) {
  400. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  401. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  402. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  403. }
  404. // conclusion
  405. if ($er->recurringConcludes()) {
  406. $conclusion = $this->l10n->l('date', $er->recurringConcludesOn(), ['width' => 'long']);
  407. }
  408. // generate localized when string
  409. // TRANSLATORS
  410. // Indicates when a calendar event will happen, shown on invitation emails
  411. // Output produced in order, output varies depending on if the event is absolute or releative:
  412. // Absolute: Every Year in July on the 1st for the entire day
  413. // Relative: Every Year in July on the First Sunday, Saturday for the entire day
  414. // Absolute: Every Year in July on the 1st for the entire day until July 31, 2026
  415. // Relative: Every Year in July on the First Sunday, Saturday for the entire day until July 31, 2026
  416. // Absolute: Every Year in July on the 1st between 8:00 AM - 9:00 AM (America/Toronto)
  417. // Relative: Every Year in July on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto)
  418. // Absolute: Every Year in July on the 1st between 8:00 AM - 9:00 AM (America/Toronto) until July 31, 2026
  419. // Relative: Every Year in July on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto) until July 31, 2026
  420. // Absolute: Every 2 Years in July on the 1st for the entire day
  421. // Relative: Every 2 Years in July on the First Sunday, Saturday for the entire day
  422. // Absolute: Every 2 Years in July on the 1st for the entire day until July 31, 2026
  423. // Relative: Every 2 Years in July on the First Sunday, Saturday for the entire day until July 31, 2026
  424. // Absolute: Every 2 Years in July on the 1st between 8:00 AM - 9:00 AM (America/Toronto)
  425. // Relative: Every 2 Years in July on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto)
  426. // Absolute: Every 2 Years in July on the 1st between 8:00 AM - 9:00 AM (America/Toronto) until July 31, 2026
  427. // Relative: Every 2 Years in July on the First Sunday, Saturday between 8:00 AM - 9:00 AM (America/Toronto) until July 31, 2026
  428. return match ([($interval > 1), !empty($startTime), !empty($conclusion)]) {
  429. [false, false, false] => $this->l10n->t('Every Year in %1$s on the %2$s for the entire day', [$months, $days]),
  430. [false, false, true] => $this->l10n->t('Every Year in %1$s on the %2$s for the entire day until %3$s', [$months, $days, $conclusion]),
  431. [false, true, false] => $this->l10n->t('Every Year in %1$s on the %2$s between %3$s - %4$s', [$months, $days, $startTime, $endTime]),
  432. [false, true, true] => $this->l10n->t('Every Year in %1$s on the %2$s between %3$s - %4$s until %5$s', [$months, $days, $startTime, $endTime, $conclusion]),
  433. [true, false, false] => $this->l10n->t('Every %1$d Years in %2$s on the %3$s for the entire day', [$interval, $months, $days]),
  434. [true, false, true] => $this->l10n->t('Every %1$d Years in %2$s on the %3$s for the entire day until %4$s', [$interval, $months, $days, $conclusion]),
  435. [true, true, false] => $this->l10n->t('Every %1$d Years in %2$s on the %3$s between %4$s - %5$s', [$interval, $months, $days, $startTime, $endTime]),
  436. [true, true, true] => $this->l10n->t('Every %1$d Years in %2$s on the %3$s between %4$s - %5$s until %6$s', [$interval, $months, $days, $startTime, $endTime, $conclusion]),
  437. default => $this->l10n->t('Could not generate event recurrence statement')
  438. };
  439. }
  440. /**
  441. * generates a when string for a fixed precision/frequency
  442. *
  443. * @since 30.0.0
  444. *
  445. * @param EventReader $er
  446. *
  447. * @return string
  448. */
  449. public function generateWhenStringRecurringFixed(EventReader $er): string {
  450. // initialize
  451. $startTime = '';
  452. $endTime = '';
  453. $conclusion = '';
  454. // time of the day
  455. if (!$er->entireDay()) {
  456. $startTime = $this->l10n->l('time', $er->startDateTime(), ['width' => 'short']);
  457. $startTime .= $er->startTimeZone() != $er->endTimeZone() ? ' (' . $er->startTimeZone()->getName() . ')' : '';
  458. $endTime = $this->l10n->l('time', $er->endDateTime(), ['width' => 'short']) . ' (' . $er->endTimeZone()->getName() . ')';
  459. }
  460. // conclusion
  461. $conclusion = $this->l10n->l('date', $er->recurringConcludesOn(), ['width' => 'long']);
  462. // generate localized when string
  463. // TRANSLATORS
  464. // Indicates when a calendar event will happen, shown on invitation emails
  465. // Output produced in order:
  466. // On specific dates for the entire day until July 13, 2024
  467. // On specific dates between 8:00 AM - 9:00 AM (America/Toronto) until July 13, 2024
  468. return match (!empty($startTime)) {
  469. false => $this->l10n->t('On specific dates for the entire day until %1$s', [$conclusion]),
  470. true => $this->l10n->t('On specific dates between %1$s - %2$s until %3$s', [$startTime, $endTime, $conclusion]),
  471. };
  472. }
  473. /**
  474. * generates a occurring next string for a recurring event
  475. *
  476. * @since 30.0.0
  477. *
  478. * @param EventReader $er
  479. *
  480. * @return string
  481. */
  482. public function generateOccurringString(EventReader $er): string {
  483. // reset to initial occurrence
  484. $er->recurrenceRewind();
  485. // forward to current date
  486. $er->recurrenceAdvanceTo($this->timeFactory->getDateTime());
  487. // calculate time difference from now to start of next event occurrence and minimize it
  488. $occurrenceIn = $this->minimizeInterval($this->timeFactory->getDateTime()->diff($er->recurrenceDate()));
  489. // store next occurrence value
  490. $occurrence = $this->l10n->l('date', $er->recurrenceDate(), ['width' => 'long']);
  491. // forward one occurrence
  492. $er->recurrenceAdvance();
  493. // evaluate if occurrence is valid
  494. if ($er->recurrenceDate() !== null) {
  495. // store following occurrence value
  496. $occurrence2 = $this->l10n->l('date', $er->recurrenceDate(), ['width' => 'long']);
  497. // forward one occurrence
  498. $er->recurrenceAdvance();
  499. // evaluate if occurrence is valid
  500. if ($er->recurrenceDate()) {
  501. // store following occurrence value
  502. $occurrence3 = $this->l10n->l('date', $er->recurrenceDate(), ['width' => 'long']);
  503. }
  504. }
  505. // generate localized when string
  506. // TRANSLATORS
  507. // Indicates when a calendar event will happen, shown on invitation emails
  508. // Output produced in order:
  509. // In a day/week/month/year on July 1, 2024
  510. // In a day/week/month/year on July 1, 2024 then on July 3, 2024
  511. // In a day/week/month/year on July 1, 2024 then on July 3, 2024 and July 5, 2024
  512. // In 2 days/weeks/months/years on July 1, 2024
  513. // In 2 days/weeks/months/years on July 1, 2024 then on July 3, 2024
  514. // In 2 days/weeks/months/years on July 1, 2024 then on July 3, 2024 and July 5, 2024
  515. return match ([($occurrenceIn[0] > 1), !empty($occurrence2), !empty($occurrence3)]) {
  516. [false, false, false] => $this->l10n->t('In a %1$s on %2$s', [$occurrenceIn[1], $occurrence]),
  517. [false, true, false] => $this->l10n->t('In a %1$s on %2$s then on %3$s', [$occurrenceIn[1], $occurrence, $occurrence2]),
  518. [false, true, true] => $this->l10n->t('In a %1$s on %2$s then on %3$s and %4$s', [$occurrenceIn[1], $occurrence, $occurrence2, $occurrence3]),
  519. [true, false, false] => $this->l10n->t('In %1$s %2$s on %3$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence]),
  520. [true, true, false] => $this->l10n->t('In %1$s %2$s on %3$s then on %4$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence, $occurrence2]),
  521. [true, true, true] => $this->l10n->t('In %1$s %2$s on %3$s then on %4$s and %5$s', [$occurrenceIn[0], $occurrenceIn[1], $occurrence, $occurrence2, $occurrence3]),
  522. default => $this->l10n->t('Could not generate next recurrence statement')
  523. };
  524. }
  525. /**
  526. * @param VEvent $vEvent
  527. * @return array
  528. */
  529. public function buildCancelledBodyData(VEvent $vEvent): array {
  530. // construct event reader
  531. $eventReaderCurrent = new EventReader($vEvent);
  532. $defaultVal = '';
  533. $strikethrough = "<span style='text-decoration: line-through'>%s</span>";
  534. $newMeetingWhen = $this->generateWhenString($eventReaderCurrent);
  535. $newSummary = isset($vEvent->SUMMARY) && (string)$vEvent->SUMMARY !== '' ? (string)$vEvent->SUMMARY : $this->l10n->t('Untitled event');
  536. $newDescription = isset($vEvent->DESCRIPTION) && (string)$vEvent->DESCRIPTION !== '' ? (string)$vEvent->DESCRIPTION : $defaultVal;
  537. $newUrl = isset($vEvent->URL) && (string)$vEvent->URL !== '' ? sprintf('<a href="%1$s">%1$s</a>', $vEvent->URL) : $defaultVal;
  538. $newLocation = isset($vEvent->LOCATION) && (string)$vEvent->LOCATION !== '' ? (string)$vEvent->LOCATION : $defaultVal;
  539. $newLocationHtml = $this->linkify($newLocation) ?? $newLocation;
  540. $data = [];
  541. $data['meeting_when_html'] = $newMeetingWhen === '' ?: sprintf($strikethrough, $newMeetingWhen);
  542. $data['meeting_when'] = $newMeetingWhen;
  543. $data['meeting_title_html'] = sprintf($strikethrough, $newSummary);
  544. $data['meeting_title'] = $newSummary !== '' ? $newSummary: $this->l10n->t('Untitled event');
  545. $data['meeting_description_html'] = $newDescription !== '' ? sprintf($strikethrough, $newDescription) : '';
  546. $data['meeting_description'] = $newDescription;
  547. $data['meeting_url_html'] = $newUrl !== '' ? sprintf($strikethrough, $newUrl) : '';
  548. $data['meeting_url'] = isset($vEvent->URL) ? (string)$vEvent->URL : '';
  549. $data['meeting_location_html'] = $newLocationHtml !== '' ? sprintf($strikethrough, $newLocationHtml) : '';
  550. $data['meeting_location'] = $newLocation;
  551. return $data;
  552. }
  553. /**
  554. * Check if event took place in the past
  555. *
  556. * @param VCalendar $vObject
  557. * @return int
  558. */
  559. public function getLastOccurrence(VCalendar $vObject) {
  560. /** @var VEvent $component */
  561. $component = $vObject->VEVENT;
  562. if (isset($component->RRULE)) {
  563. $it = new EventIterator($vObject, (string)$component->UID);
  564. $maxDate = new \DateTime(IMipPlugin::MAX_DATE);
  565. if ($it->isInfinite()) {
  566. return $maxDate->getTimestamp();
  567. }
  568. $end = $it->getDtEnd();
  569. while ($it->valid() && $end < $maxDate) {
  570. $end = $it->getDtEnd();
  571. $it->next();
  572. }
  573. return $end->getTimestamp();
  574. }
  575. /** @var Property\ICalendar\DateTime $dtStart */
  576. $dtStart = $component->DTSTART;
  577. if (isset($component->DTEND)) {
  578. /** @var Property\ICalendar\DateTime $dtEnd */
  579. $dtEnd = $component->DTEND;
  580. return $dtEnd->getDateTime()->getTimeStamp();
  581. }
  582. if (isset($component->DURATION)) {
  583. /** @var \DateTime $endDate */
  584. $endDate = clone $dtStart->getDateTime();
  585. // $component->DTEND->getDateTime() returns DateTimeImmutable
  586. $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
  587. return $endDate->getTimestamp();
  588. }
  589. if (!$dtStart->hasTime()) {
  590. /** @var \DateTime $endDate */
  591. // $component->DTSTART->getDateTime() returns DateTimeImmutable
  592. $endDate = clone $dtStart->getDateTime();
  593. $endDate = $endDate->modify('+1 day');
  594. return $endDate->getTimestamp();
  595. }
  596. // No computation of end time possible - return start date
  597. return $dtStart->getDateTime()->getTimeStamp();
  598. }
  599. /**
  600. * @param Property|null $attendee
  601. */
  602. public function setL10n(?Property $attendee = null) {
  603. if ($attendee === null) {
  604. return;
  605. }
  606. $lang = $attendee->offsetGet('LANGUAGE');
  607. if ($lang instanceof Parameter) {
  608. $lang = $lang->getValue();
  609. $this->l10n = $this->l10nFactory->get('dav', $lang);
  610. }
  611. }
  612. /**
  613. * @param Property|null $attendee
  614. * @return bool
  615. */
  616. public function getAttendeeRsvpOrReqForParticipant(?Property $attendee = null) {
  617. if ($attendee === null) {
  618. return false;
  619. }
  620. $rsvp = $attendee->offsetGet('RSVP');
  621. if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
  622. return true;
  623. }
  624. $role = $attendee->offsetGet('ROLE');
  625. // @see https://datatracker.ietf.org/doc/html/rfc5545#section-3.2.16
  626. // Attendees without a role are assumed required and should receive an invitation link even if they have no RSVP set
  627. if ($role === null
  628. || (($role instanceof Parameter) && (strcasecmp($role->getValue(), 'REQ-PARTICIPANT') === 0))
  629. || (($role instanceof Parameter) && (strcasecmp($role->getValue(), 'OPT-PARTICIPANT') === 0))
  630. ) {
  631. return true;
  632. }
  633. // RFC 5545 3.2.17: default RSVP is false
  634. return false;
  635. }
  636. /**
  637. * @param IEMailTemplate $template
  638. * @param string $method
  639. * @param string $sender
  640. * @param string $summary
  641. * @param string|null $partstat
  642. * @param bool $isModified
  643. */
  644. public function addSubjectAndHeading(IEMailTemplate $template,
  645. string $method, string $sender, string $summary, bool $isModified, ?Property $replyingAttendee = null): void {
  646. if ($method === IMipPlugin::METHOD_CANCEL) {
  647. // TRANSLATORS Subject for email, when an invitation is cancelled. Ex: "Cancelled: {{Event Name}}"
  648. $template->setSubject($this->l10n->t('Cancelled: %1$s', [$summary]));
  649. $template->addHeading($this->l10n->t('"%1$s" has been canceled', [$summary]));
  650. } elseif ($method === IMipPlugin::METHOD_REPLY) {
  651. // TRANSLATORS Subject for email, when an invitation is replied to. Ex: "Re: {{Event Name}}"
  652. $template->setSubject($this->l10n->t('Re: %1$s', [$summary]));
  653. // Build the strings
  654. $partstat = (isset($replyingAttendee)) ? $replyingAttendee->offsetGet('PARTSTAT') : null;
  655. $partstat = ($partstat instanceof Parameter) ? $partstat->getValue() : null;
  656. switch ($partstat) {
  657. case 'ACCEPTED':
  658. $template->addHeading($this->l10n->t('%1$s has accepted your invitation', [$sender]));
  659. break;
  660. case 'TENTATIVE':
  661. $template->addHeading($this->l10n->t('%1$s has tentatively accepted your invitation', [$sender]));
  662. break;
  663. case 'DECLINED':
  664. $template->addHeading($this->l10n->t('%1$s has declined your invitation', [$sender]));
  665. break;
  666. case null:
  667. default:
  668. $template->addHeading($this->l10n->t('%1$s has responded to your invitation', [$sender]));
  669. break;
  670. }
  671. } elseif ($method === IMipPlugin::METHOD_REQUEST && $isModified) {
  672. // TRANSLATORS Subject for email, when an invitation is updated. Ex: "Invitation updated: {{Event Name}}"
  673. $template->setSubject($this->l10n->t('Invitation updated: %1$s', [$summary]));
  674. $template->addHeading($this->l10n->t('%1$s updated the event "%2$s"', [$sender, $summary]));
  675. } else {
  676. // TRANSLATORS Subject for email, when an invitation is sent. Ex: "Invitation: {{Event Name}}"
  677. $template->setSubject($this->l10n->t('Invitation: %1$s', [$summary]));
  678. $template->addHeading($this->l10n->t('%1$s would like to invite you to "%2$s"', [$sender, $summary]));
  679. }
  680. }
  681. /**
  682. * @param string $path
  683. * @return string
  684. */
  685. public function getAbsoluteImagePath($path): string {
  686. return $this->urlGenerator->getAbsoluteURL(
  687. $this->urlGenerator->imagePath('core', $path)
  688. );
  689. }
  690. /**
  691. * addAttendees: add organizer and attendee names/emails to iMip mail.
  692. *
  693. * Enable with DAV setting: invitation_list_attendees (default: no)
  694. *
  695. * The default is 'no', which matches old behavior, and is privacy preserving.
  696. *
  697. * To enable including attendees in invitation emails:
  698. * % php occ config:app:set dav invitation_list_attendees --value yes
  699. *
  700. * @param IEMailTemplate $template
  701. * @param IL10N $this->l10n
  702. * @param VEvent $vevent
  703. * @author brad2014 on github.com
  704. */
  705. public function addAttendees(IEMailTemplate $template, VEvent $vevent) {
  706. if ($this->config->getAppValue('dav', 'invitation_list_attendees', 'no') === 'no') {
  707. return;
  708. }
  709. if (isset($vevent->ORGANIZER)) {
  710. /** @var Property | Property\ICalendar\CalAddress $organizer */
  711. $organizer = $vevent->ORGANIZER;
  712. $organizerEmail = substr($organizer->getNormalizedValue(), 7);
  713. /** @var string|null $organizerName */
  714. $organizerName = isset($organizer->CN) ? $organizer->CN->getValue() : null;
  715. $organizerHTML = sprintf('<a href="%s">%s</a>',
  716. htmlspecialchars($organizer->getNormalizedValue()),
  717. htmlspecialchars($organizerName ?: $organizerEmail));
  718. $organizerText = sprintf('%s <%s>', $organizerName, $organizerEmail);
  719. if (isset($organizer['PARTSTAT'])) {
  720. /** @var Parameter $partstat */
  721. $partstat = $organizer['PARTSTAT'];
  722. if (strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  723. $organizerHTML .= ' ✔︎';
  724. $organizerText .= ' ✔︎';
  725. }
  726. }
  727. $template->addBodyListItem($organizerHTML, $this->l10n->t('Organizer:'),
  728. $this->getAbsoluteImagePath('caldav/organizer.png'),
  729. $organizerText, '', IMipPlugin::IMIP_INDENT);
  730. }
  731. $attendees = $vevent->select('ATTENDEE');
  732. if (count($attendees) === 0) {
  733. return;
  734. }
  735. $attendeesHTML = [];
  736. $attendeesText = [];
  737. foreach ($attendees as $attendee) {
  738. $attendeeEmail = substr($attendee->getNormalizedValue(), 7);
  739. $attendeeName = isset($attendee['CN']) ? $attendee['CN']->getValue() : null;
  740. $attendeeHTML = sprintf('<a href="%s">%s</a>',
  741. htmlspecialchars($attendee->getNormalizedValue()),
  742. htmlspecialchars($attendeeName ?: $attendeeEmail));
  743. $attendeeText = sprintf('%s <%s>', $attendeeName, $attendeeEmail);
  744. if (isset($attendee['PARTSTAT'])) {
  745. /** @var Parameter $partstat */
  746. $partstat = $attendee['PARTSTAT'];
  747. if (strcasecmp($partstat->getValue(), 'ACCEPTED') === 0) {
  748. $attendeeHTML .= ' ✔︎';
  749. $attendeeText .= ' ✔︎';
  750. }
  751. }
  752. $attendeesHTML[] = $attendeeHTML;
  753. $attendeesText[] = $attendeeText;
  754. }
  755. $template->addBodyListItem(implode('<br/>', $attendeesHTML), $this->l10n->t('Attendees:'),
  756. $this->getAbsoluteImagePath('caldav/attendees.png'),
  757. implode("\n", $attendeesText), '', IMipPlugin::IMIP_INDENT);
  758. }
  759. /**
  760. * @param IEMailTemplate $template
  761. * @param VEVENT $vevent
  762. * @param $data
  763. */
  764. public function addBulletList(IEMailTemplate $template, VEvent $vevent, $data) {
  765. $template->addBodyListItem(
  766. $data['meeting_title_html'] ?? $data['meeting_title'], $this->l10n->t('Title:'),
  767. $this->getAbsoluteImagePath('caldav/title.png'), $data['meeting_title'], '', IMipPlugin::IMIP_INDENT);
  768. if ($data['meeting_when'] !== '') {
  769. $template->addBodyListItem($data['meeting_when_html'] ?? $data['meeting_when'], $this->l10n->t('When:'),
  770. $this->getAbsoluteImagePath('caldav/time.png'), $data['meeting_when'], '', IMipPlugin::IMIP_INDENT);
  771. }
  772. if ($data['meeting_location'] !== '') {
  773. $template->addBodyListItem($data['meeting_location_html'] ?? $data['meeting_location'], $this->l10n->t('Location:'),
  774. $this->getAbsoluteImagePath('caldav/location.png'), $data['meeting_location'], '', IMipPlugin::IMIP_INDENT);
  775. }
  776. if ($data['meeting_url'] !== '') {
  777. $template->addBodyListItem($data['meeting_url_html'] ?? $data['meeting_url'], $this->l10n->t('Link:'),
  778. $this->getAbsoluteImagePath('caldav/link.png'), $data['meeting_url'], '', IMipPlugin::IMIP_INDENT);
  779. }
  780. if (isset($data['meeting_occurring'])) {
  781. $template->addBodyListItem($data['meeting_occurring_html'] ?? $data['meeting_occurring'], $this->l10n->t('Occurring:'),
  782. $this->getAbsoluteImagePath('caldav/time.png'), $data['meeting_occurring'], '', IMipPlugin::IMIP_INDENT);
  783. }
  784. $this->addAttendees($template, $vevent);
  785. /* Put description last, like an email body, since it can be arbitrarily long */
  786. if ($data['meeting_description']) {
  787. $template->addBodyListItem($data['meeting_description_html'] ?? $data['meeting_description'], $this->l10n->t('Description:'),
  788. $this->getAbsoluteImagePath('caldav/description.png'), $data['meeting_description'], '', IMipPlugin::IMIP_INDENT);
  789. }
  790. }
  791. /**
  792. * @param Message $iTipMessage
  793. * @return null|Property
  794. */
  795. public function getCurrentAttendee(Message $iTipMessage): ?Property {
  796. /** @var VEvent $vevent */
  797. $vevent = $iTipMessage->message->VEVENT;
  798. $attendees = $vevent->select('ATTENDEE');
  799. foreach ($attendees as $attendee) {
  800. if ($iTipMessage->method === 'REPLY' && strcasecmp($attendee->getValue(), $iTipMessage->sender) === 0) {
  801. /** @var Property $attendee */
  802. return $attendee;
  803. } elseif (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
  804. /** @var Property $attendee */
  805. return $attendee;
  806. }
  807. }
  808. return null;
  809. }
  810. /**
  811. * @param Message $iTipMessage
  812. * @param VEvent $vevent
  813. * @param int $lastOccurrence
  814. * @return string
  815. */
  816. public function createInvitationToken(Message $iTipMessage, VEvent $vevent, int $lastOccurrence): string {
  817. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  818. $attendee = $iTipMessage->recipient;
  819. $organizer = $iTipMessage->sender;
  820. $sequence = $iTipMessage->sequence;
  821. $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
  822. $vevent->{'RECURRENCE-ID'}->serialize() : null;
  823. $uid = $vevent->{'UID'};
  824. $query = $this->db->getQueryBuilder();
  825. $query->insert('calendar_invitations')
  826. ->values([
  827. 'token' => $query->createNamedParameter($token),
  828. 'attendee' => $query->createNamedParameter($attendee),
  829. 'organizer' => $query->createNamedParameter($organizer),
  830. 'sequence' => $query->createNamedParameter($sequence),
  831. 'recurrenceid' => $query->createNamedParameter($recurrenceId),
  832. 'expiration' => $query->createNamedParameter($lastOccurrence),
  833. 'uid' => $query->createNamedParameter($uid)
  834. ])
  835. ->executeStatement();
  836. return $token;
  837. }
  838. /**
  839. * @param IEMailTemplate $template
  840. * @param $token
  841. */
  842. public function addResponseButtons(IEMailTemplate $template, $token) {
  843. $template->addBodyButtonGroup(
  844. $this->l10n->t('Accept'),
  845. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
  846. 'token' => $token,
  847. ]),
  848. $this->l10n->t('Decline'),
  849. $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
  850. 'token' => $token,
  851. ])
  852. );
  853. }
  854. public function addMoreOptionsButton(IEMailTemplate $template, $token) {
  855. $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
  856. 'token' => $token,
  857. ]);
  858. $html = vsprintf('<small><a href="%s">%s</a></small>', [
  859. $moreOptionsURL, $this->l10n->t('More options …')
  860. ]);
  861. $text = $this->l10n->t('More options at %s', [$moreOptionsURL]);
  862. $template->addBodyText($html, $text);
  863. }
  864. public function getReplyingAttendee(Message $iTipMessage): ?Property {
  865. /** @var VEvent $vevent */
  866. $vevent = $iTipMessage->message->VEVENT;
  867. $attendees = $vevent->select('ATTENDEE');
  868. foreach ($attendees as $attendee) {
  869. /** @var Property $attendee */
  870. if (strcasecmp($attendee->getValue(), $iTipMessage->sender) === 0) {
  871. return $attendee;
  872. }
  873. }
  874. return null;
  875. }
  876. public function isRoomOrResource(Property $attendee): bool {
  877. $cuType = $attendee->offsetGet('CUTYPE');
  878. if (!$cuType instanceof Parameter) {
  879. return false;
  880. }
  881. $type = $cuType->getValue() ?? 'INDIVIDUAL';
  882. if (\in_array(strtoupper($type), ['RESOURCE', 'ROOM', 'UNKNOWN'], true)) {
  883. // Don't send emails to things
  884. return true;
  885. }
  886. return false;
  887. }
  888. public function minimizeInterval(\DateInterval $dateInterval): array {
  889. // evaluate if time interval is in the past
  890. if ($dateInterval->invert == 1) {
  891. return [1, 'the past'];
  892. }
  893. // evaluate interval parts and return smallest time period
  894. if ($dateInterval->y > 0) {
  895. $interval = $dateInterval->y;
  896. $scale = ($dateInterval->y > 1) ? 'years' : 'year';
  897. } elseif ($dateInterval->m > 0) {
  898. $interval = $dateInterval->m;
  899. $scale = ($dateInterval->m > 1) ? 'months' : 'month';
  900. } elseif ($dateInterval->d >= 7) {
  901. $interval = (int)($dateInterval->d / 7);
  902. $scale = ((int)($dateInterval->d / 7) > 1) ? 'weeks' : 'week';
  903. } elseif ($dateInterval->d > 0) {
  904. $interval = $dateInterval->d;
  905. $scale = ($dateInterval->d > 1) ? 'days' : 'day';
  906. } elseif ($dateInterval->h > 0) {
  907. $interval = $dateInterval->h;
  908. $scale = ($dateInterval->h > 1) ? 'hours' : 'hour';
  909. } else {
  910. $interval = $dateInterval->i;
  911. $scale = 'minutes';
  912. }
  913. return [$interval, $scale];
  914. }
  915. /**
  916. * Localizes week day names to another language
  917. *
  918. * @param string $value
  919. *
  920. * @return string
  921. */
  922. public function localizeDayName(string $value): string {
  923. return match ($value) {
  924. 'Monday' => $this->l10n->t('Monday'),
  925. 'Tuesday' => $this->l10n->t('Tuesday'),
  926. 'Wednesday' => $this->l10n->t('Wednesday'),
  927. 'Thursday' => $this->l10n->t('Thursday'),
  928. 'Friday' => $this->l10n->t('Friday'),
  929. 'Saturday' => $this->l10n->t('Saturday'),
  930. 'Sunday' => $this->l10n->t('Sunday'),
  931. };
  932. }
  933. /**
  934. * Localizes month names to another language
  935. *
  936. * @param string $value
  937. *
  938. * @return string
  939. */
  940. public function localizeMonthName(string $value): string {
  941. return match ($value) {
  942. 'January' => $this->l10n->t('January'),
  943. 'February' => $this->l10n->t('February'),
  944. 'March' => $this->l10n->t('March'),
  945. 'April' => $this->l10n->t('April'),
  946. 'May' => $this->l10n->t('May'),
  947. 'June' => $this->l10n->t('June'),
  948. 'July' => $this->l10n->t('July'),
  949. 'August' => $this->l10n->t('August'),
  950. 'September' => $this->l10n->t('September'),
  951. 'October' => $this->l10n->t('October'),
  952. 'November' => $this->l10n->t('November'),
  953. 'December' => $this->l10n->t('December'),
  954. };
  955. }
  956. /**
  957. * Localizes relative position names to another language
  958. *
  959. * @param string $value
  960. *
  961. * @return string
  962. */
  963. public function localizeRelativePositionName(string $value): string {
  964. return match ($value) {
  965. 'First' => $this->l10n->t('First'),
  966. 'Second' => $this->l10n->t('Second'),
  967. 'Third' => $this->l10n->t('Third'),
  968. 'Fourth' => $this->l10n->t('Fourth'),
  969. 'Fifty' => $this->l10n->t('Fifty'),
  970. 'Last' => $this->l10n->t('Last'),
  971. 'Second Last' => $this->l10n->t('Second Last'),
  972. 'Third Last' => $this->l10n->t('Third Last'),
  973. 'Fourth Last' => $this->l10n->t('Fourth Last'),
  974. 'Fifty Last' => $this->l10n->t('Fifty Last'),
  975. };
  976. }
  977. }