1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* @copyright Copyright (c) 2016, Georg Ehrke |
5
|
|
|
* |
6
|
|
|
* @author Achim Königs <[email protected]> |
7
|
|
|
* @author Georg Ehrke <[email protected]> |
8
|
|
|
* @author Joas Schilling <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* @author Roeland Jago Douma <[email protected]> |
11
|
|
|
* @author Thomas Müller <[email protected]> |
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
|
|
|
|
29
|
|
|
namespace OCA\DAV\CalDAV; |
30
|
|
|
|
31
|
|
|
use Exception; |
32
|
|
|
use OCA\DAV\CardDAV\CardDavBackend; |
33
|
|
|
use OCA\DAV\DAV\GroupPrincipalBackend; |
34
|
|
|
use OCP\IConfig; |
35
|
|
|
use OCP\IDBConnection; |
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
|
|
|
const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; |
47
|
|
|
|
48
|
|
|
/** @var GroupPrincipalBackend */ |
49
|
|
|
private $principalBackend; |
50
|
|
|
|
51
|
|
|
/** @var CalDavBackend */ |
52
|
|
|
private $calDavBackEnd; |
53
|
|
|
|
54
|
|
|
/** @var CardDavBackend */ |
55
|
|
|
private $cardDavBackEnd; |
56
|
|
|
|
57
|
|
|
/** @var IConfig */ |
58
|
|
|
private $config; |
59
|
|
|
|
60
|
|
|
/** @var IDBConnection */ |
61
|
|
|
private $dbConnection; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* BirthdayService constructor. |
65
|
|
|
* |
66
|
|
|
* @param CalDavBackend $calDavBackEnd |
67
|
|
|
* @param CardDavBackend $cardDavBackEnd |
68
|
|
|
* @param GroupPrincipalBackend $principalBackend |
69
|
|
|
* @param IConfig $config; |
70
|
|
|
*/ |
71
|
|
|
public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config, IDBConnection $dbConnection) { |
72
|
|
|
$this->calDavBackEnd = $calDavBackEnd; |
73
|
|
|
$this->cardDavBackEnd = $cardDavBackEnd; |
74
|
|
|
$this->principalBackend = $principalBackend; |
75
|
|
|
$this->config = $config; |
76
|
|
|
$this->dbConnection = $dbConnection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $addressBookId |
81
|
|
|
* @param string $cardUri |
82
|
|
|
* @param string $cardData |
83
|
|
|
*/ |
84
|
|
|
public function onCardChanged($addressBookId, $cardUri, $cardData) { |
85
|
|
|
if (!$this->isGloballyEnabled()) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
90
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
|
|
|
|
91
|
|
|
$targetPrincipals[] = $book['principaluri']; |
92
|
|
|
$datesToSync = [ |
93
|
|
|
['postfix' => '', 'field' => 'BDAY', 'symbol' => '*', 'utfSymbol' => '🎂'], |
94
|
|
|
['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†", 'utfSymbol' => '⚰️'], |
95
|
|
|
['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭", 'utfSymbol' => '💍'], |
96
|
|
|
]; |
97
|
|
|
foreach ($targetPrincipals as $principalUri) { |
98
|
|
|
if (!$this->isUserEnabled($principalUri)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
103
|
|
|
foreach ($datesToSync as $type) { |
104
|
|
|
$this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $addressBookId |
111
|
|
|
* @param string $cardUri |
112
|
|
|
*/ |
113
|
|
|
public function onCardDeleted($addressBookId, $cardUri) { |
114
|
|
|
if (!$this->isGloballyEnabled()) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
119
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
|
|
|
|
120
|
|
|
$targetPrincipals[] = $book['principaluri']; |
121
|
|
|
foreach ($targetPrincipals as $principalUri) { |
122
|
|
|
if (!$this->isUserEnabled($principalUri)) { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
127
|
|
|
foreach (['', '-death', '-anniversary'] as $tag) { |
128
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics'; |
129
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $principal |
136
|
|
|
* @return array|null |
137
|
|
|
* @throws \Sabre\DAV\Exception\BadRequest |
138
|
|
|
*/ |
139
|
|
|
public function ensureCalendarExists($principal) { |
140
|
|
|
$calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
|
|
|
|
141
|
|
|
if (!is_null($calendar)) { |
|
|
|
|
142
|
|
|
return $calendar; |
143
|
|
|
} |
144
|
|
|
$this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [ |
145
|
|
|
'{DAV:}displayname' => 'Contact birthdays', |
146
|
|
|
'{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA', |
147
|
|
|
'components' => 'VEVENT', |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $cardData |
155
|
|
|
* @param string $dateField |
156
|
|
|
* @param string $postfix |
157
|
|
|
* @param string $summarySymbol |
158
|
|
|
* @param string $utfSummarySymbol |
159
|
|
|
* @return null|VCalendar |
160
|
|
|
*/ |
161
|
|
|
public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol, $utfSummarySymbol) { |
162
|
|
|
if (empty($cardData)) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
try { |
166
|
|
|
$doc = Reader::read($cardData); |
167
|
|
|
// We're always converting to vCard 4.0 so we can rely on the |
168
|
|
|
// VCardConverter handling the X-APPLE-OMIT-YEAR property for us. |
169
|
|
|
if (!$doc instanceof VCard) { |
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
$doc = $doc->convert(Document::VCARD40); |
173
|
|
|
} catch (Exception $e) { |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!isset($doc->{$dateField})) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
if (!isset($doc->FN)) { |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
$birthday = $doc->{$dateField}; |
184
|
|
|
if (!(string)$birthday) { |
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
// Skip if the BDAY property is not of the right type. |
188
|
|
|
if (!$birthday instanceof DateAndOrTime) { |
189
|
|
|
return null; |
190
|
|
|
} |
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
|
|
|
|
199
|
|
|
$unknownYear = false; |
200
|
|
|
$originalYear = null; |
201
|
|
|
if (!$dateParts['year']) { |
202
|
|
|
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; |
203
|
|
|
|
204
|
|
|
$unknownYear = true; |
205
|
|
|
} else { |
206
|
|
|
$parameters = $birthday->parameters(); |
207
|
|
|
if (isset($parameters['X-APPLE-OMIT-YEAR'])) { |
208
|
|
|
$omitYear = $parameters['X-APPLE-OMIT-YEAR']; |
209
|
|
|
if ($dateParts['year'] === $omitYear) { |
210
|
|
|
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; |
211
|
|
|
$unknownYear = true; |
212
|
|
|
} |
213
|
|
|
} else { |
214
|
|
|
$originalYear = (int)$dateParts['year']; |
215
|
|
|
|
216
|
|
|
if ($originalYear < 1970) { |
217
|
|
|
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
try { |
223
|
|
|
$date = new \DateTime($birthday); |
224
|
|
|
} catch (Exception $e) { |
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
if ($this->dbConnection->supports4ByteText()) { |
228
|
|
|
if ($unknownYear) { |
229
|
|
|
$summary = $utfSummarySymbol . ' ' . $doc->FN->getValue(); |
230
|
|
|
} else { |
231
|
|
|
$summary = $utfSummarySymbol . ' ' . $doc->FN->getValue() . " ($originalYear)"; |
232
|
|
|
} |
233
|
|
|
} else { |
234
|
|
|
if ($unknownYear) { |
235
|
|
|
$summary = $doc->FN->getValue() . ' ' . $summarySymbol; |
236
|
|
|
} else { |
237
|
|
|
$summary = $doc->FN->getValue() . " ($summarySymbol$originalYear)"; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$vCal = new VCalendar(); |
242
|
|
|
$vCal->VERSION = '2.0'; |
243
|
|
|
$vEvent = $vCal->createComponent('VEVENT'); |
244
|
|
|
$vEvent->add('DTSTART'); |
245
|
|
|
$vEvent->DTSTART->setDateTime( |
|
|
|
|
246
|
|
|
$date |
247
|
|
|
); |
248
|
|
|
$vEvent->DTSTART['VALUE'] = 'DATE'; |
249
|
|
|
$vEvent->add('DTEND'); |
250
|
|
|
$date->add(new \DateInterval('P1D')); |
251
|
|
|
$vEvent->DTEND->setDateTime( |
252
|
|
|
$date |
253
|
|
|
); |
254
|
|
|
$vEvent->DTEND['VALUE'] = 'DATE'; |
255
|
|
|
$vEvent->{'UID'} = $doc->UID . $postfix; |
256
|
|
|
$vEvent->{'RRULE'} = 'FREQ=YEARLY'; |
257
|
|
|
$vEvent->{'SUMMARY'} = $summary; |
258
|
|
|
$vEvent->{'TRANSP'} = 'TRANSPARENT'; |
259
|
|
|
$vEvent->{'X-NEXTCLOUD-BC-FIELD-TYPE'} = $dateField; |
260
|
|
|
$vEvent->{'X-NEXTCLOUD-BC-UNKNOWN-YEAR'} = $unknownYear ? '1' : '0'; |
261
|
|
|
if ($originalYear !== null) { |
262
|
|
|
$vEvent->{'X-NEXTCLOUD-BC-YEAR'} = (string) $originalYear; |
263
|
|
|
} |
264
|
|
|
$alarm = $vCal->createComponent('VALARM'); |
265
|
|
|
$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION'])); |
266
|
|
|
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY')); |
267
|
|
|
$alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'})); |
268
|
|
|
$vEvent->add($alarm); |
269
|
|
|
$vCal->add($vEvent); |
270
|
|
|
return $vCal; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $user |
275
|
|
|
*/ |
276
|
|
|
public function resetForUser($user) { |
277
|
|
|
$principal = 'principals/users/'.$user; |
278
|
|
|
$calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
|
|
|
|
279
|
|
|
$calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
280
|
|
|
|
281
|
|
|
foreach($calendarObjects as $calendarObject) { |
282
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $user |
288
|
|
|
*/ |
289
|
|
|
public function syncUser($user) { |
290
|
|
|
$principal = 'principals/users/'.$user; |
291
|
|
|
$this->ensureCalendarExists($principal); |
292
|
|
|
$books = $this->cardDavBackEnd->getAddressBooksForUser($principal); |
293
|
|
|
foreach($books as $book) { |
294
|
|
|
$cards = $this->cardDavBackEnd->getCards($book['id']); |
295
|
|
|
foreach($cards as $card) { |
296
|
|
|
$this->onCardChanged($book['id'], $card['uri'], $card['carddata']); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $existingCalendarData |
303
|
|
|
* @param VCalendar $newCalendarData |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
public function birthdayEvenChanged($existingCalendarData, $newCalendarData) { |
307
|
|
|
try { |
308
|
|
|
$existingBirthday = Reader::read($existingCalendarData); |
309
|
|
|
} catch (Exception $ex) { |
310
|
|
|
return true; |
311
|
|
|
} |
312
|
|
|
if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || |
|
|
|
|
313
|
|
|
$newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue() |
|
|
|
|
314
|
|
|
) { |
315
|
|
|
return true; |
316
|
|
|
} |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param integer $addressBookId |
322
|
|
|
* @return mixed |
323
|
|
|
*/ |
324
|
|
|
protected function getAllAffectedPrincipals($addressBookId) { |
325
|
|
|
$targetPrincipals = []; |
326
|
|
|
$shares = $this->cardDavBackEnd->getShares($addressBookId); |
327
|
|
|
foreach ($shares as $share) { |
328
|
|
|
if ($share['{http://owncloud.org/ns}group-share']) { |
329
|
|
|
$users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); |
330
|
|
|
foreach ($users as $user) { |
331
|
|
|
$targetPrincipals[] = $user['uri']; |
332
|
|
|
} |
333
|
|
|
} else { |
334
|
|
|
$targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
return array_values(array_unique($targetPrincipals, SORT_STRING)); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param string $cardUri |
342
|
|
|
* @param string $cardData |
343
|
|
|
* @param array $book |
344
|
|
|
* @param int $calendarId |
345
|
|
|
* @param string[] $type |
346
|
|
|
*/ |
347
|
|
|
private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) { |
348
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; |
349
|
|
|
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol'], $type['utfSymbol']); |
350
|
|
|
$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); |
351
|
|
|
if (is_null($calendarData)) { |
352
|
|
|
if (!is_null($existing)) { |
353
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri); |
354
|
|
|
} |
355
|
|
|
} else { |
356
|
|
|
if (is_null($existing)) { |
357
|
|
|
$this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
358
|
|
|
} else { |
359
|
|
|
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
360
|
|
|
$this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* checks if the admin opted-out of birthday calendars |
368
|
|
|
* |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
private function isGloballyEnabled() { |
372
|
|
|
$isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); |
373
|
|
|
return $isGloballyEnabled === 'yes'; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* checks if the user opted-out of birthday calendars |
378
|
|
|
* |
379
|
|
|
* @param $userPrincipal |
380
|
|
|
* @return bool |
381
|
|
|
*/ |
382
|
|
|
private function isUserEnabled($userPrincipal) { |
383
|
|
|
if (strpos($userPrincipal, 'principals/users/') === 0) { |
384
|
|
|
$userId = substr($userPrincipal, 17); |
385
|
|
|
$isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); |
386
|
|
|
return $isEnabled === 'yes'; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
// not sure how we got here, just be on the safe side and return true |
390
|
|
|
return true; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.