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

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