You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BirthdayService.php 14KB

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