@@ -39,303 +39,303 @@ |
||
39 | 39 | |
40 | 40 | class SyncService { |
41 | 41 | |
42 | - /** @var CardDavBackend */ |
|
43 | - private $backend; |
|
44 | - |
|
45 | - /** @var IUserManager */ |
|
46 | - private $userManager; |
|
47 | - |
|
48 | - /** @var ILogger */ |
|
49 | - private $logger; |
|
50 | - |
|
51 | - /** @var array */ |
|
52 | - private $localSystemAddressBook; |
|
53 | - |
|
54 | - /** @var AccountManager */ |
|
55 | - private $accountManager; |
|
56 | - |
|
57 | - /** @var string */ |
|
58 | - protected $certPath; |
|
59 | - |
|
60 | - /** |
|
61 | - * SyncService constructor. |
|
62 | - * |
|
63 | - * @param CardDavBackend $backend |
|
64 | - * @param IUserManager $userManager |
|
65 | - * @param ILogger $logger |
|
66 | - * @param AccountManager $accountManager |
|
67 | - */ |
|
68 | - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) { |
|
69 | - $this->backend = $backend; |
|
70 | - $this->userManager = $userManager; |
|
71 | - $this->logger = $logger; |
|
72 | - $this->accountManager = $accountManager; |
|
73 | - $this->certPath = ''; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param string $url |
|
78 | - * @param string $userName |
|
79 | - * @param string $addressBookUrl |
|
80 | - * @param string $sharedSecret |
|
81 | - * @param string $syncToken |
|
82 | - * @param int $targetBookId |
|
83 | - * @param string $targetPrincipal |
|
84 | - * @param array $targetProperties |
|
85 | - * @return string |
|
86 | - * @throws \Exception |
|
87 | - */ |
|
88 | - public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
89 | - // 1. create addressbook |
|
90 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
91 | - $addressBookId = $book['id']; |
|
92 | - |
|
93 | - // 2. query changes |
|
94 | - try { |
|
95 | - $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
96 | - } catch (ClientHttpException $ex) { |
|
97 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
98 | - // remote server revoked access to the address book, remove it |
|
99 | - $this->backend->deleteAddressBook($addressBookId); |
|
100 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
101 | - throw $ex; |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - // 3. apply changes |
|
106 | - // TODO: use multi-get for download |
|
107 | - foreach ($response['response'] as $resource => $status) { |
|
108 | - $cardUri = basename($resource); |
|
109 | - if (isset($status[200])) { |
|
110 | - $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
111 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
112 | - if ($existingCard === false) { |
|
113 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
114 | - } else { |
|
115 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
116 | - } |
|
117 | - } else { |
|
118 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return $response['token']; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $principal |
|
127 | - * @param string $id |
|
128 | - * @param array $properties |
|
129 | - * @return array|null |
|
130 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
131 | - */ |
|
132 | - public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
133 | - $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
134 | - if (!is_null($book)) { |
|
135 | - return $book; |
|
136 | - } |
|
137 | - $this->backend->createAddressBook($principal, $id, $properties); |
|
138 | - |
|
139 | - return $this->backend->getAddressBooksByUri($principal, $id); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Check if there is a valid certPath we should use |
|
144 | - * |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - protected function getCertPath() { |
|
148 | - |
|
149 | - // we already have a valid certPath |
|
150 | - if ($this->certPath !== '') { |
|
151 | - return $this->certPath; |
|
152 | - } |
|
153 | - |
|
154 | - /** @var ICertificateManager $certManager */ |
|
155 | - $certManager = \OC::$server->getCertificateManager(null); |
|
156 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
157 | - if (file_exists($certPath)) { |
|
158 | - $this->certPath = $certPath; |
|
159 | - } |
|
160 | - |
|
161 | - return $this->certPath; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $url |
|
166 | - * @param string $userName |
|
167 | - * @param string $addressBookUrl |
|
168 | - * @param string $sharedSecret |
|
169 | - * @return Client |
|
170 | - */ |
|
171 | - protected function getClient($url, $userName, $sharedSecret) { |
|
172 | - $settings = [ |
|
173 | - 'baseUri' => $url . '/', |
|
174 | - 'userName' => $userName, |
|
175 | - 'password' => $sharedSecret, |
|
176 | - ]; |
|
177 | - $client = new Client($settings); |
|
178 | - $certPath = $this->getCertPath(); |
|
179 | - $client->setThrowExceptions(true); |
|
180 | - |
|
181 | - if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
182 | - $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
183 | - } |
|
184 | - |
|
185 | - return $client; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param string $url |
|
190 | - * @param string $userName |
|
191 | - * @param string $addressBookUrl |
|
192 | - * @param string $sharedSecret |
|
193 | - * @param string $syncToken |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
197 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
198 | - |
|
199 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
200 | - |
|
201 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
202 | - 'Content-Type' => 'application/xml' |
|
203 | - ]); |
|
204 | - |
|
205 | - return $this->parseMultiStatus($response['body']); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @param string $url |
|
210 | - * @param string $userName |
|
211 | - * @param string $sharedSecret |
|
212 | - * @param string $resourcePath |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
216 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
217 | - return $client->request('GET', $resourcePath); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param string|null $syncToken |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - private function buildSyncCollectionRequestBody($syncToken) { |
|
225 | - |
|
226 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
227 | - $dom->formatOutput = true; |
|
228 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
229 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
230 | - $prop = $dom->createElement('d:prop'); |
|
231 | - $cont = $dom->createElement('d:getcontenttype'); |
|
232 | - $etag = $dom->createElement('d:getetag'); |
|
233 | - |
|
234 | - $prop->appendChild($cont); |
|
235 | - $prop->appendChild($etag); |
|
236 | - $root->appendChild($sync); |
|
237 | - $root->appendChild($prop); |
|
238 | - $dom->appendChild($root); |
|
239 | - return $dom->saveXML(); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @param string $body |
|
244 | - * @return array |
|
245 | - * @throws \Sabre\Xml\ParseException |
|
246 | - */ |
|
247 | - private function parseMultiStatus($body) { |
|
248 | - $xml = new Service(); |
|
249 | - |
|
250 | - /** @var MultiStatus $multiStatus */ |
|
251 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
252 | - |
|
253 | - $result = []; |
|
254 | - foreach ($multiStatus->getResponses() as $response) { |
|
255 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
256 | - } |
|
257 | - |
|
258 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * @param IUser $user |
|
263 | - */ |
|
264 | - public function updateUser($user) { |
|
265 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
266 | - $addressBookId = $systemAddressBook['id']; |
|
267 | - $converter = new Converter($this->accountManager); |
|
268 | - if(!$user instanceof IUser) { |
|
269 | - \OC::$server->getLogger()->logException(new \Exception('IUser expected, got ' . getType($user))); |
|
270 | - } |
|
271 | - $name = $user->getBackendClassName(); |
|
272 | - $userId = $user->getUID(); |
|
273 | - |
|
274 | - $cardId = "$name:$userId.vcf"; |
|
275 | - $card = $this->backend->getCard($addressBookId, $cardId); |
|
276 | - if ($card === false) { |
|
277 | - $vCard = $converter->createCardFromUser($user); |
|
278 | - if ($vCard !== null) { |
|
279 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
280 | - } |
|
281 | - } else { |
|
282 | - $vCard = $converter->createCardFromUser($user); |
|
283 | - if (is_null($vCard)) { |
|
284 | - $this->backend->deleteCard($addressBookId, $cardId); |
|
285 | - } else { |
|
286 | - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
287 | - } |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @param IUser|string $userOrCardId |
|
293 | - */ |
|
294 | - public function deleteUser($userOrCardId) { |
|
295 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
296 | - if ($userOrCardId instanceof IUser){ |
|
297 | - $name = $userOrCardId->getBackendClassName(); |
|
298 | - $userId = $userOrCardId->getUID(); |
|
299 | - |
|
300 | - $userOrCardId = "$name:$userId.vcf"; |
|
301 | - } |
|
302 | - $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @return array|null |
|
307 | - */ |
|
308 | - public function getLocalSystemAddressBook() { |
|
309 | - if (is_null($this->localSystemAddressBook)) { |
|
310 | - $systemPrincipal = "principals/system/system"; |
|
311 | - $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
312 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
313 | - ]); |
|
314 | - } |
|
315 | - |
|
316 | - return $this->localSystemAddressBook; |
|
317 | - } |
|
318 | - |
|
319 | - public function syncInstance(\Closure $progressCallback = null) { |
|
320 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
321 | - $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
322 | - $this->updateUser($user); |
|
323 | - if (!is_null($progressCallback)) { |
|
324 | - $progressCallback(); |
|
325 | - } |
|
326 | - }); |
|
327 | - |
|
328 | - // remove no longer existing |
|
329 | - $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
330 | - foreach($allCards as $card) { |
|
331 | - $vCard = Reader::read($card['carddata']); |
|
332 | - $uid = $vCard->UID->getValue(); |
|
333 | - // load backend and see if user exists |
|
334 | - if (!$this->userManager->userExists($uid)) { |
|
335 | - $this->deleteUser($card['uri']); |
|
336 | - } |
|
337 | - } |
|
338 | - } |
|
42 | + /** @var CardDavBackend */ |
|
43 | + private $backend; |
|
44 | + |
|
45 | + /** @var IUserManager */ |
|
46 | + private $userManager; |
|
47 | + |
|
48 | + /** @var ILogger */ |
|
49 | + private $logger; |
|
50 | + |
|
51 | + /** @var array */ |
|
52 | + private $localSystemAddressBook; |
|
53 | + |
|
54 | + /** @var AccountManager */ |
|
55 | + private $accountManager; |
|
56 | + |
|
57 | + /** @var string */ |
|
58 | + protected $certPath; |
|
59 | + |
|
60 | + /** |
|
61 | + * SyncService constructor. |
|
62 | + * |
|
63 | + * @param CardDavBackend $backend |
|
64 | + * @param IUserManager $userManager |
|
65 | + * @param ILogger $logger |
|
66 | + * @param AccountManager $accountManager |
|
67 | + */ |
|
68 | + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) { |
|
69 | + $this->backend = $backend; |
|
70 | + $this->userManager = $userManager; |
|
71 | + $this->logger = $logger; |
|
72 | + $this->accountManager = $accountManager; |
|
73 | + $this->certPath = ''; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param string $url |
|
78 | + * @param string $userName |
|
79 | + * @param string $addressBookUrl |
|
80 | + * @param string $sharedSecret |
|
81 | + * @param string $syncToken |
|
82 | + * @param int $targetBookId |
|
83 | + * @param string $targetPrincipal |
|
84 | + * @param array $targetProperties |
|
85 | + * @return string |
|
86 | + * @throws \Exception |
|
87 | + */ |
|
88 | + public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
89 | + // 1. create addressbook |
|
90 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
91 | + $addressBookId = $book['id']; |
|
92 | + |
|
93 | + // 2. query changes |
|
94 | + try { |
|
95 | + $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
96 | + } catch (ClientHttpException $ex) { |
|
97 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
98 | + // remote server revoked access to the address book, remove it |
|
99 | + $this->backend->deleteAddressBook($addressBookId); |
|
100 | + $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
101 | + throw $ex; |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + // 3. apply changes |
|
106 | + // TODO: use multi-get for download |
|
107 | + foreach ($response['response'] as $resource => $status) { |
|
108 | + $cardUri = basename($resource); |
|
109 | + if (isset($status[200])) { |
|
110 | + $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
111 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
112 | + if ($existingCard === false) { |
|
113 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
114 | + } else { |
|
115 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
116 | + } |
|
117 | + } else { |
|
118 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return $response['token']; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $principal |
|
127 | + * @param string $id |
|
128 | + * @param array $properties |
|
129 | + * @return array|null |
|
130 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
131 | + */ |
|
132 | + public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
133 | + $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
134 | + if (!is_null($book)) { |
|
135 | + return $book; |
|
136 | + } |
|
137 | + $this->backend->createAddressBook($principal, $id, $properties); |
|
138 | + |
|
139 | + return $this->backend->getAddressBooksByUri($principal, $id); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Check if there is a valid certPath we should use |
|
144 | + * |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + protected function getCertPath() { |
|
148 | + |
|
149 | + // we already have a valid certPath |
|
150 | + if ($this->certPath !== '') { |
|
151 | + return $this->certPath; |
|
152 | + } |
|
153 | + |
|
154 | + /** @var ICertificateManager $certManager */ |
|
155 | + $certManager = \OC::$server->getCertificateManager(null); |
|
156 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
157 | + if (file_exists($certPath)) { |
|
158 | + $this->certPath = $certPath; |
|
159 | + } |
|
160 | + |
|
161 | + return $this->certPath; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $url |
|
166 | + * @param string $userName |
|
167 | + * @param string $addressBookUrl |
|
168 | + * @param string $sharedSecret |
|
169 | + * @return Client |
|
170 | + */ |
|
171 | + protected function getClient($url, $userName, $sharedSecret) { |
|
172 | + $settings = [ |
|
173 | + 'baseUri' => $url . '/', |
|
174 | + 'userName' => $userName, |
|
175 | + 'password' => $sharedSecret, |
|
176 | + ]; |
|
177 | + $client = new Client($settings); |
|
178 | + $certPath = $this->getCertPath(); |
|
179 | + $client->setThrowExceptions(true); |
|
180 | + |
|
181 | + if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
182 | + $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
183 | + } |
|
184 | + |
|
185 | + return $client; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param string $url |
|
190 | + * @param string $userName |
|
191 | + * @param string $addressBookUrl |
|
192 | + * @param string $sharedSecret |
|
193 | + * @param string $syncToken |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
197 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
198 | + |
|
199 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
200 | + |
|
201 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
202 | + 'Content-Type' => 'application/xml' |
|
203 | + ]); |
|
204 | + |
|
205 | + return $this->parseMultiStatus($response['body']); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @param string $url |
|
210 | + * @param string $userName |
|
211 | + * @param string $sharedSecret |
|
212 | + * @param string $resourcePath |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
216 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
217 | + return $client->request('GET', $resourcePath); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param string|null $syncToken |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + private function buildSyncCollectionRequestBody($syncToken) { |
|
225 | + |
|
226 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
227 | + $dom->formatOutput = true; |
|
228 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
229 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
230 | + $prop = $dom->createElement('d:prop'); |
|
231 | + $cont = $dom->createElement('d:getcontenttype'); |
|
232 | + $etag = $dom->createElement('d:getetag'); |
|
233 | + |
|
234 | + $prop->appendChild($cont); |
|
235 | + $prop->appendChild($etag); |
|
236 | + $root->appendChild($sync); |
|
237 | + $root->appendChild($prop); |
|
238 | + $dom->appendChild($root); |
|
239 | + return $dom->saveXML(); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @param string $body |
|
244 | + * @return array |
|
245 | + * @throws \Sabre\Xml\ParseException |
|
246 | + */ |
|
247 | + private function parseMultiStatus($body) { |
|
248 | + $xml = new Service(); |
|
249 | + |
|
250 | + /** @var MultiStatus $multiStatus */ |
|
251 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
252 | + |
|
253 | + $result = []; |
|
254 | + foreach ($multiStatus->getResponses() as $response) { |
|
255 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
256 | + } |
|
257 | + |
|
258 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * @param IUser $user |
|
263 | + */ |
|
264 | + public function updateUser($user) { |
|
265 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
266 | + $addressBookId = $systemAddressBook['id']; |
|
267 | + $converter = new Converter($this->accountManager); |
|
268 | + if(!$user instanceof IUser) { |
|
269 | + \OC::$server->getLogger()->logException(new \Exception('IUser expected, got ' . getType($user))); |
|
270 | + } |
|
271 | + $name = $user->getBackendClassName(); |
|
272 | + $userId = $user->getUID(); |
|
273 | + |
|
274 | + $cardId = "$name:$userId.vcf"; |
|
275 | + $card = $this->backend->getCard($addressBookId, $cardId); |
|
276 | + if ($card === false) { |
|
277 | + $vCard = $converter->createCardFromUser($user); |
|
278 | + if ($vCard !== null) { |
|
279 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
280 | + } |
|
281 | + } else { |
|
282 | + $vCard = $converter->createCardFromUser($user); |
|
283 | + if (is_null($vCard)) { |
|
284 | + $this->backend->deleteCard($addressBookId, $cardId); |
|
285 | + } else { |
|
286 | + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
287 | + } |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @param IUser|string $userOrCardId |
|
293 | + */ |
|
294 | + public function deleteUser($userOrCardId) { |
|
295 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
296 | + if ($userOrCardId instanceof IUser){ |
|
297 | + $name = $userOrCardId->getBackendClassName(); |
|
298 | + $userId = $userOrCardId->getUID(); |
|
299 | + |
|
300 | + $userOrCardId = "$name:$userId.vcf"; |
|
301 | + } |
|
302 | + $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @return array|null |
|
307 | + */ |
|
308 | + public function getLocalSystemAddressBook() { |
|
309 | + if (is_null($this->localSystemAddressBook)) { |
|
310 | + $systemPrincipal = "principals/system/system"; |
|
311 | + $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
312 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
313 | + ]); |
|
314 | + } |
|
315 | + |
|
316 | + return $this->localSystemAddressBook; |
|
317 | + } |
|
318 | + |
|
319 | + public function syncInstance(\Closure $progressCallback = null) { |
|
320 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
321 | + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
322 | + $this->updateUser($user); |
|
323 | + if (!is_null($progressCallback)) { |
|
324 | + $progressCallback(); |
|
325 | + } |
|
326 | + }); |
|
327 | + |
|
328 | + // remove no longer existing |
|
329 | + $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
330 | + foreach($allCards as $card) { |
|
331 | + $vCard = Reader::read($card['carddata']); |
|
332 | + $uid = $vCard->UID->getValue(); |
|
333 | + // load backend and see if user exists |
|
334 | + if (!$this->userManager->userExists($uid)) { |
|
335 | + $this->deleteUser($card['uri']); |
|
336 | + } |
|
337 | + } |
|
338 | + } |
|
339 | 339 | |
340 | 340 | |
341 | 341 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
98 | 98 | // remote server revoked access to the address book, remove it |
99 | 99 | $this->backend->deleteAddressBook($addressBookId); |
100 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
100 | + $this->logger->info('Authorization failed, remove address book: '.$url, ['app' => 'dav']); |
|
101 | 101 | throw $ex; |
102 | 102 | } |
103 | 103 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | */ |
171 | 171 | protected function getClient($url, $userName, $sharedSecret) { |
172 | 172 | $settings = [ |
173 | - 'baseUri' => $url . '/', |
|
173 | + 'baseUri' => $url.'/', |
|
174 | 174 | 'userName' => $userName, |
175 | 175 | 'password' => $sharedSecret, |
176 | 176 | ]; |
@@ -265,8 +265,8 @@ discard block |
||
265 | 265 | $systemAddressBook = $this->getLocalSystemAddressBook(); |
266 | 266 | $addressBookId = $systemAddressBook['id']; |
267 | 267 | $converter = new Converter($this->accountManager); |
268 | - if(!$user instanceof IUser) { |
|
269 | - \OC::$server->getLogger()->logException(new \Exception('IUser expected, got ' . getType($user))); |
|
268 | + if (!$user instanceof IUser) { |
|
269 | + \OC::$server->getLogger()->logException(new \Exception('IUser expected, got '.getType($user))); |
|
270 | 270 | } |
271 | 271 | $name = $user->getBackendClassName(); |
272 | 272 | $userId = $user->getUID(); |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | */ |
294 | 294 | public function deleteUser($userOrCardId) { |
295 | 295 | $systemAddressBook = $this->getLocalSystemAddressBook(); |
296 | - if ($userOrCardId instanceof IUser){ |
|
296 | + if ($userOrCardId instanceof IUser) { |
|
297 | 297 | $name = $userOrCardId->getBackendClassName(); |
298 | 298 | $userId = $userOrCardId->getUID(); |
299 | 299 | |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | if (is_null($this->localSystemAddressBook)) { |
310 | 310 | $systemPrincipal = "principals/system/system"; |
311 | 311 | $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
312 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
312 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
313 | 313 | ]); |
314 | 314 | } |
315 | 315 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | |
328 | 328 | // remove no longer existing |
329 | 329 | $allCards = $this->backend->getCards($systemAddressBook['id']); |
330 | - foreach($allCards as $card) { |
|
330 | + foreach ($allCards as $card) { |
|
331 | 331 | $vCard = Reader::read($card['carddata']); |
332 | 332 | $uid = $vCard->UID->getValue(); |
333 | 333 | // load backend and see if user exists |