BirthdayService.php 16 KB

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