2016-02-03 14:43:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-11-16 21:06:36 +00:00
|
|
|
* @copyright Copyright (c) 2016, Georg Ehrke
|
2016-07-21 14:49:16 +00:00
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Achim Königs <garfonso@tratschtante.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-02-03 14:43:45 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-11-16 21:06:36 +00:00
|
|
|
* @author Georg Ehrke <georg@nextcloud.com>
|
2016-02-03 14:43:45 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CalDAV;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use OCA\DAV\CardDAV\CardDavBackend;
|
2016-03-23 13:12:50 +00:00
|
|
|
use OCA\DAV\DAV\GroupPrincipalBackend;
|
2016-02-03 14:43:45 +00:00
|
|
|
use Sabre\VObject\Component\VCalendar;
|
|
|
|
use Sabre\VObject\Reader;
|
|
|
|
|
|
|
|
class BirthdayService {
|
|
|
|
|
|
|
|
const BIRTHDAY_CALENDAR_URI = 'contact_birthdays';
|
|
|
|
|
2016-03-23 13:12:50 +00:00
|
|
|
/** @var GroupPrincipalBackend */
|
|
|
|
private $principalBackend;
|
|
|
|
|
2016-07-05 20:45:05 +00:00
|
|
|
/** @var CalDavBackend */
|
|
|
|
private $calDavBackEnd;
|
|
|
|
|
|
|
|
/** @var CardDavBackend */
|
|
|
|
private $cardDavBackEnd;
|
|
|
|
|
2016-02-03 14:43:45 +00:00
|
|
|
/**
|
|
|
|
* BirthdayService constructor.
|
|
|
|
*
|
|
|
|
* @param CalDavBackend $calDavBackEnd
|
|
|
|
* @param CardDavBackend $cardDavBackEnd
|
2016-03-23 13:12:50 +00:00
|
|
|
* @param GroupPrincipalBackend $principalBackend
|
2016-02-03 14:43:45 +00:00
|
|
|
*/
|
2016-09-19 23:15:24 +00:00
|
|
|
public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend) {
|
2016-02-03 14:43:45 +00:00
|
|
|
$this->calDavBackEnd = $calDavBackEnd;
|
|
|
|
$this->cardDavBackEnd = $cardDavBackEnd;
|
2016-03-23 13:12:50 +00:00
|
|
|
$this->principalBackend = $principalBackend;
|
2016-02-03 14:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $addressBookId
|
|
|
|
* @param string $cardUri
|
|
|
|
* @param string $cardData
|
|
|
|
*/
|
|
|
|
public function onCardChanged($addressBookId, $cardUri, $cardData) {
|
2016-03-23 13:12:50 +00:00
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
|
2016-03-23 11:28:54 +00:00
|
|
|
|
2016-02-03 14:43:45 +00:00
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
|
2016-03-23 11:28:54 +00:00
|
|
|
$targetPrincipals[] = $book['principaluri'];
|
2016-09-30 08:29:27 +00:00
|
|
|
$datesToSync = [
|
|
|
|
['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'],
|
|
|
|
['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†"],
|
|
|
|
['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"],
|
|
|
|
];
|
2016-03-23 11:28:54 +00:00
|
|
|
foreach ($targetPrincipals as $principalUri) {
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri);
|
2016-09-30 08:29:27 +00:00
|
|
|
foreach ($datesToSync as $type) {
|
|
|
|
$this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type);
|
2016-02-03 14:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $addressBookId
|
|
|
|
* @param string $cardUri
|
|
|
|
*/
|
|
|
|
public function onCardDeleted($addressBookId, $cardUri) {
|
2016-03-23 13:12:50 +00:00
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
|
2016-02-03 14:43:45 +00:00
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
|
2016-03-23 11:28:54 +00:00
|
|
|
$targetPrincipals[] = $book['principaluri'];
|
|
|
|
foreach ($targetPrincipals as $principalUri) {
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri);
|
2016-09-30 08:29:27 +00:00
|
|
|
foreach (['', '-death', '-anniversary'] as $tag) {
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics';
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
|
|
|
|
}
|
2016-03-23 11:28:54 +00:00
|
|
|
}
|
2016-02-03 14:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $principal
|
|
|
|
* @return array|null
|
|
|
|
* @throws \Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
2016-03-16 16:19:14 +00:00
|
|
|
public function ensureCalendarExists($principal) {
|
|
|
|
$book = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
|
2016-02-03 14:43:45 +00:00
|
|
|
if (!is_null($book)) {
|
|
|
|
return $book;
|
|
|
|
}
|
2016-03-16 16:19:14 +00:00
|
|
|
$this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [
|
|
|
|
'{DAV:}displayname' => 'Contact birthdays',
|
|
|
|
'{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA',
|
2016-07-28 12:19:39 +00:00
|
|
|
'components' => 'VEVENT',
|
2016-03-16 16:19:14 +00:00
|
|
|
]);
|
2016-02-03 14:43:45 +00:00
|
|
|
|
2016-03-16 16:19:14 +00:00
|
|
|
return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
|
2016-02-03 14:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cardData
|
2016-09-30 08:29:27 +00:00
|
|
|
* @param string $dateField
|
|
|
|
* @param string $summarySymbol
|
2016-02-03 14:43:45 +00:00
|
|
|
* @return null|VCalendar
|
|
|
|
*/
|
2016-09-30 08:29:27 +00:00
|
|
|
public function buildDateFromContact($cardData, $dateField, $summarySymbol) {
|
2016-02-03 14:43:45 +00:00
|
|
|
if (empty($cardData)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$doc = Reader::read($cardData);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-09-30 08:29:27 +00:00
|
|
|
if (!isset($doc->{$dateField})) {
|
2016-02-03 14:43:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-30 08:29:27 +00:00
|
|
|
$birthday = $doc->{$dateField};
|
2016-02-03 14:43:45 +00:00
|
|
|
if (!(string)$birthday) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$title = str_replace('{name}',
|
|
|
|
strtr((string)$doc->FN, array('\,' => ',', '\;' => ';')),
|
2016-03-10 11:41:37 +00:00
|
|
|
'{name}'
|
2016-02-03 14:43:45 +00:00
|
|
|
);
|
|
|
|
try {
|
|
|
|
$date = new \DateTime($birthday);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-16 21:06:36 +00:00
|
|
|
|
|
|
|
$summary = $title . ' (' . $summarySymbol . $date->format('Y') . ')';
|
2016-02-03 14:43:45 +00:00
|
|
|
$vCal = new VCalendar();
|
|
|
|
$vCal->VERSION = '2.0';
|
|
|
|
$vEvent = $vCal->createComponent('VEVENT');
|
|
|
|
$vEvent->add('DTSTART');
|
|
|
|
$vEvent->DTSTART->setDateTime(
|
|
|
|
$date
|
|
|
|
);
|
|
|
|
$vEvent->DTSTART['VALUE'] = 'DATE';
|
|
|
|
$vEvent->add('DTEND');
|
|
|
|
$date->add(new \DateInterval('P1D'));
|
|
|
|
$vEvent->DTEND->setDateTime(
|
|
|
|
$date
|
|
|
|
);
|
|
|
|
$vEvent->DTEND['VALUE'] = 'DATE';
|
|
|
|
$vEvent->{'UID'} = $doc->UID;
|
|
|
|
$vEvent->{'RRULE'} = 'FREQ=YEARLY';
|
2016-11-16 21:06:36 +00:00
|
|
|
$vEvent->{'SUMMARY'} = $summary;
|
2016-02-03 14:43:45 +00:00
|
|
|
$vEvent->{'TRANSP'} = 'TRANSPARENT';
|
2016-03-22 22:47:34 +00:00
|
|
|
$alarm = $vCal->createComponent('VALARM');
|
|
|
|
$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION']));
|
|
|
|
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));
|
|
|
|
$alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'}));
|
|
|
|
$vEvent->add($alarm);
|
2016-02-03 14:43:45 +00:00
|
|
|
$vCal->add($vEvent);
|
|
|
|
return $vCal;
|
|
|
|
}
|
|
|
|
|
2016-02-18 13:49:45 +00:00
|
|
|
/**
|
|
|
|
* @param string $user
|
|
|
|
*/
|
|
|
|
public function syncUser($user) {
|
2016-03-16 16:19:14 +00:00
|
|
|
$principal = 'principals/users/'.$user;
|
|
|
|
$this->ensureCalendarExists($principal);
|
|
|
|
$books = $this->cardDavBackEnd->getAddressBooksForUser($principal);
|
2016-02-18 13:49:45 +00:00
|
|
|
foreach($books as $book) {
|
|
|
|
$cards = $this->cardDavBackEnd->getCards($book['id']);
|
|
|
|
foreach($cards as $card) {
|
|
|
|
$this->onCardChanged($book['id'], $card['uri'], $card['carddata']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $existingCalendarData
|
|
|
|
* @param VCalendar $newCalendarData
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function birthdayEvenChanged($existingCalendarData, $newCalendarData) {
|
|
|
|
try {
|
|
|
|
$existingBirthday = Reader::read($existingCalendarData);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() ||
|
|
|
|
$newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue()
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-23 13:12:50 +00:00
|
|
|
/**
|
2016-04-08 15:11:37 +00:00
|
|
|
* @param integer $addressBookId
|
2016-03-23 13:12:50 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getAllAffectedPrincipals($addressBookId) {
|
|
|
|
$targetPrincipals = [];
|
|
|
|
$shares = $this->cardDavBackEnd->getShares($addressBookId);
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
if ($share['{http://owncloud.org/ns}group-share']) {
|
|
|
|
$users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']);
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$targetPrincipals[] = $user['uri'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$targetPrincipals[] = $share['{http://owncloud.org/ns}principal'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values(array_unique($targetPrincipals, SORT_STRING));
|
|
|
|
}
|
|
|
|
|
2016-09-30 08:29:27 +00:00
|
|
|
/**
|
|
|
|
* @param string $cardUri
|
|
|
|
* @param string $cardData
|
|
|
|
* @param array $book
|
|
|
|
* @param int $calendarId
|
|
|
|
* @param string $type
|
|
|
|
*/
|
|
|
|
private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) {
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
|
|
|
|
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['symbol']);
|
|
|
|
$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
|
|
|
|
if (is_null($calendarData)) {
|
|
|
|
if (!is_null($existing)) {
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_null($existing)) {
|
|
|
|
$this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize());
|
|
|
|
} else {
|
|
|
|
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
|
|
|
|
$this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 14:43:45 +00:00
|
|
|
}
|