Completed
Push — master ( 16b9c9...0923d2 )
by Lukas
70:38 queued 52:20
created

BirthdayService::syncUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Achim Königs <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\DAV\CalDAV;
26
27
use Exception;
28
use OCA\DAV\CardDAV\CardDavBackend;
29
use OCA\DAV\DAV\GroupPrincipalBackend;
30
use Sabre\VObject\Component\VCalendar;
31
use Sabre\VObject\Reader;
32
33
class BirthdayService {
34
35
	const BIRTHDAY_CALENDAR_URI = 'contact_birthdays';
36
37
	/** @var GroupPrincipalBackend */
38
	private $principalBackend;
39
40
	/** @var CalDavBackend  */
41
	private $calDavBackEnd;
42
43
	/** @var CardDavBackend  */
44
	private $cardDavBackEnd;
45
46
	/**
47
	 * BirthdayService constructor.
48
	 *
49
	 * @param CalDavBackend $calDavBackEnd
50
	 * @param CardDavBackend $cardDavBackEnd
51
	 * @param GroupPrincipalBackend $principalBackend
52
	 */
53
	public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend) {
54
		$this->calDavBackEnd = $calDavBackEnd;
55
		$this->cardDavBackEnd = $cardDavBackEnd;
56
		$this->principalBackend = $principalBackend;
57
	}
58
59
	/**
60
	 * @param int $addressBookId
61
	 * @param string $cardUri
62
	 * @param string $cardData
63
	 */
64
	public function onCardChanged($addressBookId, $cardUri, $cardData) {
65
		$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
66
		
67
		$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
68
		$targetPrincipals[] = $book['principaluri'];
69
		$datesToSync = [
70
			['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'],
71
			['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†"],
72
			['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"],
73
		];
74
		foreach ($targetPrincipals as $principalUri) {
75
			$calendar = $this->ensureCalendarExists($principalUri);
76
			foreach ($datesToSync as $type) {
77
				$this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type);
0 ignored issues
show
Documentation introduced by
$type is of type array<string,string,{"po...ng","symbol":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
			}
79
		}
80
	}
81
82
	/**
83
	 * @param int $addressBookId
84
	 * @param string $cardUri
85
	 */
86
	public function onCardDeleted($addressBookId, $cardUri) {
87
		$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId);
88
		$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
89
		$targetPrincipals[] = $book['principaluri'];
90
		foreach ($targetPrincipals as $principalUri) {
91
			$calendar = $this->ensureCalendarExists($principalUri);
92
			foreach (['', '-death', '-anniversary'] as $tag) {
93
				$objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics';
94
				$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri);
95
			}
96
		}
97
	}
98
99
	/**
100
	 * @param string $principal
101
	 * @return array|null
102
	 * @throws \Sabre\DAV\Exception\BadRequest
103
	 */
104
	public function ensureCalendarExists($principal) {
105
		$book = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
106
		if (!is_null($book)) {
107
			return $book;
108
		}
109
		$this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [
110
			'{DAV:}displayname' => 'Contact birthdays',
111
			'{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA',
112
			'components'   => 'VEVENT',
113
		]);
114
115
		return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
116
	}
117
118
	/**
119
	 * @param string $cardData
120
	 * @param string $dateField
121
	 * @param string $summarySymbol
122
	 * @return null|VCalendar
123
	 */
124
	public function buildDateFromContact($cardData, $dateField, $summarySymbol) {
125
		if (empty($cardData)) {
126
			return null;
127
		}
128
		try {
129
			$doc = Reader::read($cardData);
130
		} catch (Exception $e) {
131
			return null;
132
		}
133
134
		if (!isset($doc->{$dateField})) {
135
			return null;
136
		}
137
		$birthday = $doc->{$dateField};
138
		if (!(string)$birthday) {
139
			return null;
140
		}
141
		$title = str_replace('{name}',
142
			strtr((string)$doc->FN, array('\,' => ',', '\;' => ';')),
143
			'{name}'
144
		);
145
		try {
146
			$date = new \DateTime($birthday);
147
		} catch (Exception $e) {
148
			return null;
149
		}
150
		$vCal = new VCalendar();
151
		$vCal->VERSION = '2.0';
152
		$vEvent = $vCal->createComponent('VEVENT');
153
		$vEvent->add('DTSTART');
154
		$vEvent->DTSTART->setDateTime(
155
			$date
156
		);
157
		$vEvent->DTSTART['VALUE'] = 'DATE';
158
		$vEvent->add('DTEND');
159
		$date->add(new \DateInterval('P1D'));
160
		$vEvent->DTEND->setDateTime(
161
			$date
162
		);
163
		$vEvent->DTEND['VALUE'] = 'DATE';
164
		$vEvent->{'UID'} = $doc->UID;
165
		$vEvent->{'RRULE'} = 'FREQ=YEARLY';
166
		$vEvent->{'SUMMARY'} = $title . ' (' . $summarySymbol . $date->format('Y') . ')';
167
		$vEvent->{'TRANSP'} = 'TRANSPARENT';
168
		$alarm = $vCal->createComponent('VALARM');
169
		$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION']));
170
		$alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));
171
		$alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'}));
172
		$vEvent->add($alarm);
173
		$vCal->add($vEvent);
174
		return $vCal;
175
	}
176
177
	/**
178
	 * @param string $user
179
	 */
180
	public function syncUser($user) {
181
		$principal = 'principals/users/'.$user;
182
		$this->ensureCalendarExists($principal);
183
		$books = $this->cardDavBackEnd->getAddressBooksForUser($principal);
184
		foreach($books as $book) {
185
			$cards = $this->cardDavBackEnd->getCards($book['id']);
186
			foreach($cards as $card) {
187
				$this->onCardChanged($book['id'], $card['uri'], $card['carddata']);
188
			}
189
		}
190
	}
191
192
	/**
193
	 * @param string $existingCalendarData
194
	 * @param VCalendar $newCalendarData
195
	 * @return bool
196
	 */
197
	public function birthdayEvenChanged($existingCalendarData, $newCalendarData) {
198
		try {
199
			$existingBirthday = Reader::read($existingCalendarData);
200
		} catch (Exception $ex) {
201
			return true;
202
		}
203
		if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() ||
204
			$newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue()
205
		) {
206
			return true;
207
		}
208
		return false;
209
	}
210
211
	/**
212
	 * @param integer $addressBookId
213
	 * @return mixed
214
	 */
215
	protected function getAllAffectedPrincipals($addressBookId) {
216
		$targetPrincipals = [];
217
		$shares = $this->cardDavBackEnd->getShares($addressBookId);
218
		foreach ($shares as $share) {
219
			if ($share['{http://owncloud.org/ns}group-share']) {
220
				$users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']);
221
				foreach ($users as $user) {
222
					$targetPrincipals[] = $user['uri'];
223
				}
224
			} else {
225
				$targetPrincipals[] = $share['{http://owncloud.org/ns}principal'];
226
			}
227
		}
228
		return array_values(array_unique($targetPrincipals, SORT_STRING));
229
	}
230
231
	/**
232
	 * @param string $cardUri
233
	 * @param string  $cardData
234
	 * @param array $book
235
	 * @param int $calendarId
236
	 * @param string $type
237
	 */
238
	private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) {
239
		$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
240
		$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['symbol']);
241
		$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
242
		if (is_null($calendarData)) {
243
			if (!is_null($existing)) {
244
				$this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri);
245
			}
246
		} else {
247
			if (is_null($existing)) {
248
				$this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize());
249
			} else {
250
				if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) {
251
					$this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize());
252
				}
253
			}
254
		}
255
	}
256
257
}
258