BirthdayService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\CalDAV;
  9. use Exception;
  10. use OCA\DAV\CardDAV\CardDavBackend;
  11. use OCA\DAV\DAV\GroupPrincipalBackend;
  12. use OCP\IConfig;
  13. use OCP\IDBConnection;
  14. use OCP\IL10N;
  15. use Sabre\VObject\Component\VCalendar;
  16. use Sabre\VObject\Component\VCard;
  17. use Sabre\VObject\DateTimeParser;
  18. use Sabre\VObject\Document;
  19. use Sabre\VObject\InvalidDataException;
  20. use Sabre\VObject\Property\VCard\DateAndOrTime;
  21. use Sabre\VObject\Reader;
  22. /**
  23. * Class BirthdayService
  24. *
  25. * @package OCA\DAV\CalDAV
  26. */
  27. class BirthdayService {
  28. public const BIRTHDAY_CALENDAR_URI = 'contact_birthdays';
  29. public const EXCLUDE_FROM_BIRTHDAY_CALENDAR_PROPERTY_NAME = 'X-NC-EXCLUDE-FROM-BIRTHDAY-CALENDAR';
  30. private GroupPrincipalBackend $principalBackend;
  31. private CalDavBackend $calDavBackEnd;
  32. private CardDavBackend $cardDavBackEnd;
  33. private IConfig $config;
  34. private IDBConnection $dbConnection;
  35. private IL10N $l10n;
  36. /**
  37. * BirthdayService constructor.
  38. */
  39. public function __construct(CalDavBackend $calDavBackEnd,
  40. CardDavBackend $cardDavBackEnd,
  41. GroupPrincipalBackend $principalBackend,
  42. IConfig $config,
  43. IDBConnection $dbConnection,
  44. IL10N $l10n) {
  45. $this->calDavBackEnd = $calDavBackEnd;
  46. $this->cardDavBackEnd = $cardDavBackEnd;
  47. $this->principalBackend = $principalBackend;
  48. $this->config = $config;
  49. $this->dbConnection = $dbConnection;
  50. $this->l10n = $l10n;
  51. }
  52. public function onCardChanged(int $addressBookId,
  53. string $cardUri,
  54. string $cardData): void {
  55. if (!$this->isGloballyEnabled()) {
  56. return;
  57. }
  58. $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
  59. $book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
  60. if ($book === null) {
  61. return;
  62. }
  63. $targetPrincipals[] = $book['principaluri'];
  64. $datesToSync = [
  65. ['postfix' => '', 'field' => 'BDAY'],
  66. ['postfix' => '-death', 'field' => 'DEATHDATE'],
  67. ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY'],
  68. ];
  69. foreach ($targetPrincipals as $principalUri) {
  70. if (!$this->isUserEnabled($principalUri)) {
  71. continue;
  72. }
  73. $reminderOffset = $this->getReminderOffsetForUser($principalUri);
  74. $calendar = $this->ensureCalendarExists($principalUri);
  75. if ($calendar === null) {
  76. return;
  77. }
  78. foreach ($datesToSync as $type) {
  79. $this->updateCalendar($cardUri, $cardData, $book, (int)$calendar['id'], $type, $reminderOffset);
  80. }
  81. }
  82. }
  83. public function onCardDeleted(int $addressBookId,
  84. string $cardUri): void {
  85. if (!$this->isGloballyEnabled()) {
  86. return;
  87. }
  88. $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
  89. $book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
  90. $targetPrincipals[] = $book['principaluri'];
  91. foreach ($targetPrincipals as $principalUri) {
  92. if (!$this->isUserEnabled($principalUri)) {
  93. continue;
  94. }
  95. $calendar = $this->ensureCalendarExists($principalUri);
  96. foreach (['', '-death', '-anniversary'] as $tag) {
  97. $objectUri = $book['uri'] . '-' . $cardUri . $tag . '.ics';
  98. $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri, CalDavBackend::CALENDAR_TYPE_CALENDAR, true);
  99. }
  100. }
  101. }
  102. /**
  103. * @throws \Sabre\DAV\Exception\BadRequest
  104. */
  105. public function ensureCalendarExists(string $principal): ?array {
  106. $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
  107. if (!is_null($calendar)) {
  108. return $calendar;
  109. }
  110. $this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [
  111. '{DAV:}displayname' => $this->l10n->t('Contact birthdays'),
  112. '{http://apple.com/ns/ical/}calendar-color' => '#E9D859',
  113. 'components' => 'VEVENT',
  114. ]);
  115. return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
  116. }
  117. /**
  118. * @param $cardData
  119. * @param $dateField
  120. * @param $postfix
  121. * @param $reminderOffset
  122. * @return VCalendar|null
  123. * @throws InvalidDataException
  124. */
  125. public function buildDateFromContact(string $cardData,
  126. string $dateField,
  127. string $postfix,
  128. ?string $reminderOffset):?VCalendar {
  129. if (empty($cardData)) {
  130. return null;
  131. }
  132. try {
  133. $doc = Reader::read($cardData);
  134. // We're always converting to vCard 4.0 so we can rely on the
  135. // VCardConverter handling the X-APPLE-OMIT-YEAR property for us.
  136. if (!$doc instanceof VCard) {
  137. return null;
  138. }
  139. $doc = $doc->convert(Document::VCARD40);
  140. } catch (Exception $e) {
  141. return null;
  142. }
  143. if (isset($doc->{self::EXCLUDE_FROM_BIRTHDAY_CALENDAR_PROPERTY_NAME})) {
  144. return null;
  145. }
  146. if (!isset($doc->{$dateField})) {
  147. return null;
  148. }
  149. if (!isset($doc->FN)) {
  150. return null;
  151. }
  152. $birthday = $doc->{$dateField};
  153. if (!(string)$birthday) {
  154. return null;
  155. }
  156. // Skip if the BDAY property is not of the right type.
  157. if (!$birthday instanceof DateAndOrTime) {
  158. return null;
  159. }
  160. // Skip if we can't parse the BDAY value.
  161. try {
  162. $dateParts = DateTimeParser::parseVCardDateTime($birthday->getValue());
  163. } catch (InvalidDataException $e) {
  164. return null;
  165. }
  166. if ($dateParts['year'] !== null) {
  167. $parameters = $birthday->parameters();
  168. $omitYear = (isset($parameters['X-APPLE-OMIT-YEAR'])
  169. && $parameters['X-APPLE-OMIT-YEAR'] === $dateParts['year']);
  170. // 'X-APPLE-OMIT-YEAR' is not always present, at least iOS 12.4 uses the hard coded date of 1604 (the start of the gregorian calendar) when the year is unknown
  171. if ($omitYear || (int)$dateParts['year'] === 1604) {
  172. $dateParts['year'] = null;
  173. }
  174. }
  175. $originalYear = null;
  176. if ($dateParts['year'] !== null) {
  177. $originalYear = (int)$dateParts['year'];
  178. }
  179. $leapDay = ((int)$dateParts['month'] === 2
  180. && (int)$dateParts['date'] === 29);
  181. if ($dateParts['year'] === null || $originalYear < 1970) {
  182. $birthday = ($leapDay ? '1972-' : '1970-')
  183. . $dateParts['month'] . '-' . $dateParts['date'];
  184. }
  185. try {
  186. if ($birthday instanceof DateAndOrTime) {
  187. $date = $birthday->getDateTime();
  188. } else {
  189. $date = new \DateTimeImmutable($birthday);
  190. }
  191. } catch (Exception $e) {
  192. return null;
  193. }
  194. $summary = $this->formatTitle($dateField, $doc->FN->getValue(), $originalYear, $this->dbConnection->supports4ByteText());
  195. $vCal = new VCalendar();
  196. $vCal->VERSION = '2.0';
  197. $vCal->PRODID = '-//IDN nextcloud.com//Birthday calendar//EN';
  198. $vEvent = $vCal->createComponent('VEVENT');
  199. $vEvent->add('DTSTART');
  200. $vEvent->DTSTART->setDateTime(
  201. $date
  202. );
  203. $vEvent->DTSTART['VALUE'] = 'DATE';
  204. $vEvent->add('DTEND');
  205. $dtEndDate = (new \DateTime())->setTimestamp($date->getTimeStamp());
  206. $dtEndDate->add(new \DateInterval('P1D'));
  207. $vEvent->DTEND->setDateTime(
  208. $dtEndDate
  209. );
  210. $vEvent->DTEND['VALUE'] = 'DATE';
  211. $vEvent->{'UID'} = $doc->UID . $postfix;
  212. $vEvent->{'RRULE'} = 'FREQ=YEARLY';
  213. if ($leapDay) {
  214. /* Sabre\VObject supports BYMONTHDAY only if BYMONTH
  215. * is also set */
  216. $vEvent->{'RRULE'} = 'FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=-1';
  217. }
  218. $vEvent->{'SUMMARY'} = $summary;
  219. $vEvent->{'TRANSP'} = 'TRANSPARENT';
  220. $vEvent->{'X-NEXTCLOUD-BC-FIELD-TYPE'} = $dateField;
  221. $vEvent->{'X-NEXTCLOUD-BC-UNKNOWN-YEAR'} = $dateParts['year'] === null ? '1' : '0';
  222. if ($originalYear !== null) {
  223. $vEvent->{'X-NEXTCLOUD-BC-YEAR'} = (string)$originalYear;
  224. }
  225. if ($reminderOffset) {
  226. $alarm = $vCal->createComponent('VALARM');
  227. $alarm->add($vCal->createProperty('TRIGGER', $reminderOffset, ['VALUE' => 'DURATION']));
  228. $alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));
  229. $alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'}));
  230. $vEvent->add($alarm);
  231. }
  232. $vCal->add($vEvent);
  233. return $vCal;
  234. }
  235. /**
  236. * @param string $user
  237. */
  238. public function resetForUser(string $user):void {
  239. $principal = 'principals/users/' . $user;
  240. $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
  241. if (!$calendar) {
  242. return; // The user's birthday calendar doesn't exist, no need to purge it
  243. }
  244. $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR);
  245. foreach ($calendarObjects as $calendarObject) {
  246. $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR, true);
  247. }
  248. }
  249. /**
  250. * @param string $user
  251. * @throws \Sabre\DAV\Exception\BadRequest
  252. */
  253. public function syncUser(string $user):void {
  254. $principal = 'principals/users/' . $user;
  255. $this->ensureCalendarExists($principal);
  256. $books = $this->cardDavBackEnd->getAddressBooksForUser($principal);
  257. foreach ($books as $book) {
  258. $cards = $this->cardDavBackEnd->getCards($book['id']);
  259. foreach ($cards as $card) {
  260. $this->onCardChanged((int)$book['id'], $card['uri'], $card['carddata']);
  261. }
  262. }
  263. }
  264. /**
  265. * @param string $existingCalendarData
  266. * @param VCalendar $newCalendarData
  267. * @return bool
  268. */
  269. public function birthdayEvenChanged(string $existingCalendarData,
  270. VCalendar $newCalendarData):bool {
  271. try {
  272. $existingBirthday = Reader::read($existingCalendarData);
  273. } catch (Exception $ex) {
  274. return true;
  275. }
  276. return (
  277. $newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() ||
  278. $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue()
  279. );
  280. }
  281. /**
  282. * @param integer $addressBookId
  283. * @return mixed
  284. */
  285. protected function getAllAffectedPrincipals(int $addressBookId) {
  286. $targetPrincipals = [];
  287. $shares = $this->cardDavBackEnd->getShares($addressBookId);
  288. foreach ($shares as $share) {
  289. if ($share['{http://owncloud.org/ns}group-share']) {
  290. $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']);
  291. foreach ($users as $user) {
  292. $targetPrincipals[] = $user['uri'];
  293. }
  294. } else {
  295. $targetPrincipals[] = $share['{http://owncloud.org/ns}principal'];
  296. }
  297. }
  298. return array_values(array_unique($targetPrincipals, SORT_STRING));
  299. }
  300. /**
  301. * @param string $cardUri
  302. * @param string $cardData
  303. * @param array $book
  304. * @param int $calendarId
  305. * @param array $type
  306. * @param string $reminderOffset
  307. * @throws InvalidDataException
  308. * @throws \Sabre\DAV\Exception\BadRequest
  309. */
  310. private function updateCalendar(string $cardUri,
  311. string $cardData,
  312. array $book,
  313. int $calendarId,
  314. array $type,
  315. ?string $reminderOffset):void {
  316. $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
  317. $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $reminderOffset);
  318. $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
  319. if ($calendarData === null) {
  320. if ($existing !== null) {
  321. $this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri, CalDavBackend::CALENDAR_TYPE_CALENDAR, true);
  322. }
  323. } else {
  324. if ($existing === null) {
  325. // not found by URI, but maybe by UID
  326. // happens when a contact with birthday is moved to a different address book
  327. $calendarInfo = $this->calDavBackEnd->getCalendarById($calendarId);
  328. $extraData = $this->calDavBackEnd->getDenormalizedData($calendarData->serialize());
  329. if ($calendarInfo && array_key_exists('principaluri', $calendarInfo)) {
  330. $existing2path = $this->calDavBackEnd->getCalendarObjectByUID($calendarInfo['principaluri'], $extraData['uid']);
  331. if ($existing2path !== null && array_key_exists('uri', $calendarInfo)) {
  332. // delete the old birthday entry first so that we do not get duplicate UIDs
  333. $existing2objectUri = substr($existing2path, strlen($calendarInfo['uri']) + 1);
  334. $this->calDavBackEnd->deleteCalendarObject($calendarId, $existing2objectUri, CalDavBackend::CALENDAR_TYPE_CALENDAR, true);
  335. }
  336. }
  337. $this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize());
  338. } else {
  339. if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
  340. $this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize());
  341. }
  342. }
  343. }
  344. }
  345. /**
  346. * checks if the admin opted-out of birthday calendars
  347. *
  348. * @return bool
  349. */
  350. private function isGloballyEnabled():bool {
  351. return $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes') === 'yes';
  352. }
  353. /**
  354. * Extracts the userId part of a principal
  355. *
  356. * @param string $userPrincipal
  357. * @return string|null
  358. */
  359. private function principalToUserId(string $userPrincipal):?string {
  360. if (str_starts_with($userPrincipal, 'principals/users/')) {
  361. return substr($userPrincipal, 17);
  362. }
  363. return null;
  364. }
  365. /**
  366. * Checks if the user opted-out of birthday calendars
  367. *
  368. * @param string $userPrincipal The user principal to check for
  369. * @return bool
  370. */
  371. private function isUserEnabled(string $userPrincipal):bool {
  372. $userId = $this->principalToUserId($userPrincipal);
  373. if ($userId !== null) {
  374. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  375. return $isEnabled === 'yes';
  376. }
  377. // not sure how we got here, just be on the safe side and return true
  378. return true;
  379. }
  380. /**
  381. * Get the reminder offset value for a user. This is a duration string (e.g.
  382. * PT9H) or null if no reminder is wanted.
  383. *
  384. * @param string $userPrincipal
  385. * @return string|null
  386. */
  387. private function getReminderOffsetForUser(string $userPrincipal):?string {
  388. $userId = $this->principalToUserId($userPrincipal);
  389. if ($userId !== null) {
  390. return $this->config->getUserValue($userId, 'dav', 'birthdayCalendarReminderOffset', 'PT9H') ?: null;
  391. }
  392. // not sure how we got here, just be on the safe side and return the default value
  393. return 'PT9H';
  394. }
  395. /**
  396. * Formats title of Birthday event
  397. *
  398. * @param string $field Field name like BDAY, ANNIVERSARY, ...
  399. * @param string $name Name of contact
  400. * @param int|null $year Year of birth, anniversary, ...
  401. * @param bool $supports4Byte Whether or not the database supports 4 byte chars
  402. * @return string The formatted title
  403. */
  404. private function formatTitle(string $field,
  405. string $name,
  406. ?int $year = null,
  407. bool $supports4Byte = true):string {
  408. if ($supports4Byte) {
  409. switch ($field) {
  410. case 'BDAY':
  411. return implode('', [
  412. '🎂 ',
  413. $name,
  414. $year ? (' (' . $year . ')') : '',
  415. ]);
  416. case 'DEATHDATE':
  417. return implode('', [
  418. $this->l10n->t('Death of %s', [$name]),
  419. $year ? (' (' . $year . ')') : '',
  420. ]);
  421. case 'ANNIVERSARY':
  422. return implode('', [
  423. '💍 ',
  424. $name,
  425. $year ? (' (' . $year . ')') : '',
  426. ]);
  427. default:
  428. return '';
  429. }
  430. } else {
  431. switch ($field) {
  432. case 'BDAY':
  433. return implode('', [
  434. $name,
  435. ' ',
  436. $year ? ('(*' . $year . ')') : '*',
  437. ]);
  438. case 'DEATHDATE':
  439. return implode('', [
  440. $this->l10n->t('Death of %s', [$name]),
  441. $year ? (' (' . $year . ')') : '',
  442. ]);
  443. case 'ANNIVERSARY':
  444. return implode('', [
  445. $name,
  446. ' ',
  447. $year ? ('(⚭' . $year . ')') : '⚭',
  448. ]);
  449. default:
  450. return '';
  451. }
  452. }
  453. }
  454. }