BirthdayService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Georg Ehrke
  5. *
  6. * @author Achim Königs <garfonso@tratschtante.de>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\CalDAV;
  29. use Exception;
  30. use OCA\DAV\CardDAV\CardDavBackend;
  31. use OCA\DAV\DAV\GroupPrincipalBackend;
  32. use OCP\IConfig;
  33. use Sabre\VObject\Component\VCalendar;
  34. use Sabre\VObject\Component\VCard;
  35. use Sabre\VObject\DateTimeParser;
  36. use Sabre\VObject\Document;
  37. use Sabre\VObject\InvalidDataException;
  38. use Sabre\VObject\Property\VCard\DateAndOrTime;
  39. use Sabre\VObject\Reader;
  40. class BirthdayService {
  41. const BIRTHDAY_CALENDAR_URI = 'contact_birthdays';
  42. /** @var GroupPrincipalBackend */
  43. private $principalBackend;
  44. /** @var CalDavBackend */
  45. private $calDavBackEnd;
  46. /** @var CardDavBackend */
  47. private $cardDavBackEnd;
  48. /** @var IConfig */
  49. private $config;
  50. /**
  51. * BirthdayService constructor.
  52. *
  53. * @param CalDavBackend $calDavBackEnd
  54. * @param CardDavBackend $cardDavBackEnd
  55. * @param GroupPrincipalBackend $principalBackend
  56. * @param IConfig $config;
  57. */
  58. public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config) {
  59. $this->calDavBackEnd = $calDavBackEnd;
  60. $this->cardDavBackEnd = $cardDavBackEnd;
  61. $this->principalBackend = $principalBackend;
  62. $this->config = $config;
  63. }
  64. /**
  65. * @param int $addressBookId
  66. * @param string $cardUri
  67. * @param string $cardData
  68. */
  69. public function onCardChanged($addressBookId, $cardUri, $cardData) {
  70. if (!$this->isGloballyEnabled()) {
  71. return;
  72. }
  73. $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
  74. $book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
  75. $targetPrincipals[] = $book['principaluri'];
  76. $datesToSync = [
  77. ['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'],
  78. ['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†"],
  79. ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"],
  80. ];
  81. foreach ($targetPrincipals as $principalUri) {
  82. if (!$this->isUserEnabled($principalUri)) {
  83. continue;
  84. }
  85. $calendar = $this->ensureCalendarExists($principalUri);
  86. foreach ($datesToSync as $type) {
  87. $this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type);
  88. }
  89. }
  90. }
  91. /**
  92. * @param int $addressBookId
  93. * @param string $cardUri
  94. */
  95. public function onCardDeleted($addressBookId, $cardUri) {
  96. if (!$this->isGloballyEnabled()) {
  97. return;
  98. }
  99. $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
  100. $book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
  101. $targetPrincipals[] = $book['principaluri'];
  102. foreach ($targetPrincipals as $principalUri) {
  103. if (!$this->isUserEnabled($principalUri)) {
  104. continue;
  105. }
  106. $calendar = $this->ensureCalendarExists($principalUri);
  107. foreach (['', '-death', '-anniversary'] as $tag) {
  108. $objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics';
  109. $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
  110. }
  111. }
  112. }
  113. /**
  114. * @param string $principal
  115. * @return array|null
  116. * @throws \Sabre\DAV\Exception\BadRequest
  117. */
  118. public function ensureCalendarExists($principal) {
  119. $book = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
  120. if (!is_null($book)) {
  121. return $book;
  122. }
  123. $this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [
  124. '{DAV:}displayname' => 'Contact birthdays',
  125. '{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA',
  126. 'components' => 'VEVENT',
  127. ]);
  128. return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
  129. }
  130. /**
  131. * @param string $cardData
  132. * @param string $dateField
  133. * @param string $postfix
  134. * @param string $summarySymbol
  135. * @return null|VCalendar
  136. */
  137. public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol) {
  138. if (empty($cardData)) {
  139. return null;
  140. }
  141. try {
  142. $doc = Reader::read($cardData);
  143. // We're always converting to vCard 4.0 so we can rely on the
  144. // VCardConverter handling the X-APPLE-OMIT-YEAR property for us.
  145. if (!$doc instanceof VCard) {
  146. return null;
  147. }
  148. $doc = $doc->convert(Document::VCARD40);
  149. } catch (Exception $e) {
  150. return null;
  151. }
  152. if (!isset($doc->{$dateField})) {
  153. return null;
  154. }
  155. if (!isset($doc->FN)) {
  156. return null;
  157. }
  158. $birthday = $doc->{$dateField};
  159. if (!(string)$birthday) {
  160. return null;
  161. }
  162. // Skip if the BDAY property is not of the right type.
  163. if (!$birthday instanceof DateAndOrTime) {
  164. return null;
  165. }
  166. // Skip if we can't parse the BDAY value.
  167. try {
  168. $dateParts = DateTimeParser::parseVCardDateTime($birthday->getValue());
  169. } catch (InvalidDataException $e) {
  170. return null;
  171. }
  172. $unknownYear = false;
  173. if (!$dateParts['year']) {
  174. $birthday = '1900-' . $dateParts['month'] . '-' . $dateParts['date'];
  175. $unknownYear = true;
  176. }
  177. try {
  178. $date = new \DateTime($birthday);
  179. } catch (Exception $e) {
  180. return null;
  181. }
  182. if ($unknownYear) {
  183. $summary = $doc->FN->getValue() . ' ' . $summarySymbol;
  184. } else {
  185. $year = (int)$date->format('Y');
  186. $summary = $doc->FN->getValue() . " ($summarySymbol$year)";
  187. }
  188. $vCal = new VCalendar();
  189. $vCal->VERSION = '2.0';
  190. $vEvent = $vCal->createComponent('VEVENT');
  191. $vEvent->add('DTSTART');
  192. $vEvent->DTSTART->setDateTime(
  193. $date
  194. );
  195. $vEvent->DTSTART['VALUE'] = 'DATE';
  196. $vEvent->add('DTEND');
  197. $date->add(new \DateInterval('P1D'));
  198. $vEvent->DTEND->setDateTime(
  199. $date
  200. );
  201. $vEvent->DTEND['VALUE'] = 'DATE';
  202. $vEvent->{'UID'} = $doc->UID . $postfix;
  203. $vEvent->{'RRULE'} = 'FREQ=YEARLY';
  204. $vEvent->{'SUMMARY'} = $summary;
  205. $vEvent->{'TRANSP'} = 'TRANSPARENT';
  206. $alarm = $vCal->createComponent('VALARM');
  207. $alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION']));
  208. $alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));
  209. $alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'}));
  210. $vEvent->add($alarm);
  211. $vCal->add($vEvent);
  212. return $vCal;
  213. }
  214. /**
  215. * @param string $user
  216. */
  217. public function syncUser($user) {
  218. $principal = 'principals/users/'.$user;
  219. $this->ensureCalendarExists($principal);
  220. $books = $this->cardDavBackEnd->getAddressBooksForUser($principal);
  221. foreach($books as $book) {
  222. $cards = $this->cardDavBackEnd->getCards($book['id']);
  223. foreach($cards as $card) {
  224. $this->onCardChanged($book['id'], $card['uri'], $card['carddata']);
  225. }
  226. }
  227. }
  228. /**
  229. * @param string $existingCalendarData
  230. * @param VCalendar $newCalendarData
  231. * @return bool
  232. */
  233. public function birthdayEvenChanged($existingCalendarData, $newCalendarData) {
  234. try {
  235. $existingBirthday = Reader::read($existingCalendarData);
  236. } catch (Exception $ex) {
  237. return true;
  238. }
  239. if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() ||
  240. $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue()
  241. ) {
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. * @param integer $addressBookId
  248. * @return mixed
  249. */
  250. protected function getAllAffectedPrincipals($addressBookId) {
  251. $targetPrincipals = [];
  252. $shares = $this->cardDavBackEnd->getShares($addressBookId);
  253. foreach ($shares as $share) {
  254. if ($share['{http://owncloud.org/ns}group-share']) {
  255. $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']);
  256. foreach ($users as $user) {
  257. $targetPrincipals[] = $user['uri'];
  258. }
  259. } else {
  260. $targetPrincipals[] = $share['{http://owncloud.org/ns}principal'];
  261. }
  262. }
  263. return array_values(array_unique($targetPrincipals, SORT_STRING));
  264. }
  265. /**
  266. * @param string $cardUri
  267. * @param string $cardData
  268. * @param array $book
  269. * @param int $calendarId
  270. * @param string[] $type
  271. */
  272. private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) {
  273. $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
  274. $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol']);
  275. $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
  276. if (is_null($calendarData)) {
  277. if (!is_null($existing)) {
  278. $this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri);
  279. }
  280. } else {
  281. if (is_null($existing)) {
  282. $this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize());
  283. } else {
  284. if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
  285. $this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize());
  286. }
  287. }
  288. }
  289. }
  290. /**
  291. * checks if the admin opted-out of birthday calendars
  292. *
  293. * @return bool
  294. */
  295. private function isGloballyEnabled() {
  296. $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
  297. return $isGloballyEnabled === 'yes';
  298. }
  299. /**
  300. * checks if the user opted-out of birthday calendars
  301. *
  302. * @param $userPrincipal
  303. * @return bool
  304. */
  305. private function isUserEnabled($userPrincipal) {
  306. if (strpos($userPrincipal, 'principals/users/') === 0) {
  307. $userId = substr($userPrincipal, 17);
  308. $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
  309. return $isEnabled === 'yes';
  310. }
  311. // not sure how we got here, just be on the safe side and return true
  312. return true;
  313. }
  314. }