@@ -36,98 +36,98 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Converter { |
| 38 | 38 | |
| 39 | - /** @var IAccountManager */ |
|
| 40 | - private $accountManager; |
|
| 41 | - |
|
| 42 | - public function __construct(IAccountManager $accountManager) { |
|
| 43 | - $this->accountManager = $accountManager; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function createCardFromUser(IUser $user): ?VCard { |
|
| 47 | - $userProperties = $this->accountManager->getAccount($user)->getProperties(); |
|
| 48 | - |
|
| 49 | - $uid = $user->getUID(); |
|
| 50 | - $cloudId = $user->getCloudId(); |
|
| 51 | - $image = $this->getAvatarImage($user); |
|
| 52 | - |
|
| 53 | - $vCard = new VCard(); |
|
| 54 | - $vCard->VERSION = '3.0'; |
|
| 55 | - $vCard->UID = $uid; |
|
| 56 | - |
|
| 57 | - $publish = false; |
|
| 58 | - |
|
| 59 | - foreach ($userProperties as $property) { |
|
| 60 | - $shareWithTrustedServers = |
|
| 61 | - $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
|
| 62 | - $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
|
| 63 | - |
|
| 64 | - $emptyValue = $property->getValue() === ''; |
|
| 65 | - |
|
| 66 | - if ($shareWithTrustedServers && !$emptyValue) { |
|
| 67 | - $publish = true; |
|
| 68 | - switch ($property->getName()) { |
|
| 69 | - case IAccountManager::PROPERTY_DISPLAYNAME: |
|
| 70 | - $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
|
| 71 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
|
| 72 | - break; |
|
| 73 | - case IAccountManager::PROPERTY_AVATAR: |
|
| 74 | - if ($image !== null) { |
|
| 75 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
| 76 | - } |
|
| 77 | - break; |
|
| 78 | - case IAccountManager::PROPERTY_EMAIL: |
|
| 79 | - $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 80 | - break; |
|
| 81 | - case IAccountManager::PROPERTY_WEBSITE: |
|
| 82 | - $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
|
| 83 | - break; |
|
| 84 | - case IAccountManager::PROPERTY_PHONE: |
|
| 85 | - $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 86 | - break; |
|
| 87 | - case IAccountManager::PROPERTY_ADDRESS: |
|
| 88 | - $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 89 | - break; |
|
| 90 | - case IAccountManager::PROPERTY_TWITTER: |
|
| 91 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
|
| 92 | - break; |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if ($publish && !empty($cloudId)) { |
|
| 98 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
| 99 | - $vCard->validate(); |
|
| 100 | - return $vCard; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return null; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function splitFullName(string $fullName): array { |
|
| 107 | - // Very basic western style parsing. I'm not gonna implement |
|
| 108 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
| 109 | - |
|
| 110 | - $elements = explode(' ', $fullName); |
|
| 111 | - $result = ['', '', '', '', '']; |
|
| 112 | - if (count($elements) > 2) { |
|
| 113 | - $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
| 114 | - $result[1] = $elements[0]; |
|
| 115 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
| 116 | - } elseif (count($elements) === 2) { |
|
| 117 | - $result[0] = $elements[1]; |
|
| 118 | - $result[1] = $elements[0]; |
|
| 119 | - } else { |
|
| 120 | - $result[0] = $elements[0]; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return $result; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - private function getAvatarImage(IUser $user): ?IImage { |
|
| 127 | - try { |
|
| 128 | - return $user->getAvatarImage(-1); |
|
| 129 | - } catch (Exception $ex) { |
|
| 130 | - return null; |
|
| 131 | - } |
|
| 132 | - } |
|
| 39 | + /** @var IAccountManager */ |
|
| 40 | + private $accountManager; |
|
| 41 | + |
|
| 42 | + public function __construct(IAccountManager $accountManager) { |
|
| 43 | + $this->accountManager = $accountManager; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function createCardFromUser(IUser $user): ?VCard { |
|
| 47 | + $userProperties = $this->accountManager->getAccount($user)->getProperties(); |
|
| 48 | + |
|
| 49 | + $uid = $user->getUID(); |
|
| 50 | + $cloudId = $user->getCloudId(); |
|
| 51 | + $image = $this->getAvatarImage($user); |
|
| 52 | + |
|
| 53 | + $vCard = new VCard(); |
|
| 54 | + $vCard->VERSION = '3.0'; |
|
| 55 | + $vCard->UID = $uid; |
|
| 56 | + |
|
| 57 | + $publish = false; |
|
| 58 | + |
|
| 59 | + foreach ($userProperties as $property) { |
|
| 60 | + $shareWithTrustedServers = |
|
| 61 | + $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
|
| 62 | + $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
|
| 63 | + |
|
| 64 | + $emptyValue = $property->getValue() === ''; |
|
| 65 | + |
|
| 66 | + if ($shareWithTrustedServers && !$emptyValue) { |
|
| 67 | + $publish = true; |
|
| 68 | + switch ($property->getName()) { |
|
| 69 | + case IAccountManager::PROPERTY_DISPLAYNAME: |
|
| 70 | + $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
|
| 71 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
|
| 72 | + break; |
|
| 73 | + case IAccountManager::PROPERTY_AVATAR: |
|
| 74 | + if ($image !== null) { |
|
| 75 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
| 76 | + } |
|
| 77 | + break; |
|
| 78 | + case IAccountManager::PROPERTY_EMAIL: |
|
| 79 | + $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 80 | + break; |
|
| 81 | + case IAccountManager::PROPERTY_WEBSITE: |
|
| 82 | + $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
|
| 83 | + break; |
|
| 84 | + case IAccountManager::PROPERTY_PHONE: |
|
| 85 | + $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 86 | + break; |
|
| 87 | + case IAccountManager::PROPERTY_ADDRESS: |
|
| 88 | + $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
| 89 | + break; |
|
| 90 | + case IAccountManager::PROPERTY_TWITTER: |
|
| 91 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
|
| 92 | + break; |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if ($publish && !empty($cloudId)) { |
|
| 98 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
| 99 | + $vCard->validate(); |
|
| 100 | + return $vCard; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return null; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function splitFullName(string $fullName): array { |
|
| 107 | + // Very basic western style parsing. I'm not gonna implement |
|
| 108 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
| 109 | + |
|
| 110 | + $elements = explode(' ', $fullName); |
|
| 111 | + $result = ['', '', '', '', '']; |
|
| 112 | + if (count($elements) > 2) { |
|
| 113 | + $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
| 114 | + $result[1] = $elements[0]; |
|
| 115 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
| 116 | + } elseif (count($elements) === 2) { |
|
| 117 | + $result[0] = $elements[1]; |
|
| 118 | + $result[1] = $elements[0]; |
|
| 119 | + } else { |
|
| 120 | + $result[0] = $elements[0]; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return $result; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + private function getAvatarImage(IUser $user): ?IImage { |
|
| 127 | + try { |
|
| 128 | + return $user->getAvatarImage(-1); |
|
| 129 | + } catch (Exception $ex) { |
|
| 130 | + return null; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | 133 | } |
@@ -40,245 +40,245 @@ |
||
| 40 | 40 | use Sabre\VObject\Reader; |
| 41 | 41 | |
| 42 | 42 | class SyncService { |
| 43 | - private CardDavBackend $backend; |
|
| 44 | - private IUserManager $userManager; |
|
| 45 | - private LoggerInterface $logger; |
|
| 46 | - private ?array $localSystemAddressBook = null; |
|
| 47 | - private Converter $converter; |
|
| 48 | - protected string $certPath; |
|
| 49 | - |
|
| 50 | - public function __construct(CardDavBackend $backend, |
|
| 51 | - IUserManager $userManager, |
|
| 52 | - LoggerInterface $logger, |
|
| 53 | - Converter $converter) { |
|
| 54 | - $this->backend = $backend; |
|
| 55 | - $this->userManager = $userManager; |
|
| 56 | - $this->logger = $logger; |
|
| 57 | - $this->converter = $converter; |
|
| 58 | - $this->certPath = ''; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @throws \Exception |
|
| 63 | - */ |
|
| 64 | - public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken, string $targetBookHash, string $targetPrincipal, array $targetProperties): string { |
|
| 65 | - // 1. create addressbook |
|
| 66 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookHash, $targetProperties); |
|
| 67 | - $addressBookId = $book['id']; |
|
| 68 | - |
|
| 69 | - // 2. query changes |
|
| 70 | - try { |
|
| 71 | - $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
| 72 | - } catch (ClientHttpException $ex) { |
|
| 73 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 74 | - // remote server revoked access to the address book, remove it |
|
| 75 | - $this->backend->deleteAddressBook($addressBookId); |
|
| 76 | - $this->logger->error('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 77 | - throw $ex; |
|
| 78 | - } |
|
| 79 | - $this->logger->error('Client exception:', ['app' => 'dav', 'exception' => $ex]); |
|
| 80 | - throw $ex; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // 3. apply changes |
|
| 84 | - // TODO: use multi-get for download |
|
| 85 | - foreach ($response['response'] as $resource => $status) { |
|
| 86 | - $cardUri = basename($resource); |
|
| 87 | - if (isset($status[200])) { |
|
| 88 | - $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
| 89 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 90 | - if ($existingCard === false) { |
|
| 91 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 92 | - } else { |
|
| 93 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 94 | - } |
|
| 95 | - } else { |
|
| 96 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $response['token']; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 105 | - */ |
|
| 106 | - public function ensureSystemAddressBookExists(string $principal, string $uri, array $properties): ?array { |
|
| 107 | - $book = $this->backend->getAddressBooksByUri($principal, $uri); |
|
| 108 | - if (!is_null($book)) { |
|
| 109 | - return $book; |
|
| 110 | - } |
|
| 111 | - // FIXME This might break in clustered DB setup |
|
| 112 | - $this->backend->createAddressBook($principal, $uri, $properties); |
|
| 113 | - |
|
| 114 | - return $this->backend->getAddressBooksByUri($principal, $uri); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Check if there is a valid certPath we should use |
|
| 119 | - */ |
|
| 120 | - protected function getCertPath(): string { |
|
| 121 | - |
|
| 122 | - // we already have a valid certPath |
|
| 123 | - if ($this->certPath !== '') { |
|
| 124 | - return $this->certPath; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $certManager = \OC::$server->getCertificateManager(); |
|
| 128 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
| 129 | - if (file_exists($certPath)) { |
|
| 130 | - $this->certPath = $certPath; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this->certPath; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - protected function getClient(string $url, string $userName, string $sharedSecret): Client { |
|
| 137 | - $settings = [ |
|
| 138 | - 'baseUri' => $url . '/', |
|
| 139 | - 'userName' => $userName, |
|
| 140 | - 'password' => $sharedSecret, |
|
| 141 | - ]; |
|
| 142 | - $client = new Client($settings); |
|
| 143 | - $certPath = $this->getCertPath(); |
|
| 144 | - $client->setThrowExceptions(true); |
|
| 145 | - |
|
| 146 | - if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
| 147 | - $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $client; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - protected function requestSyncReport(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken): array { |
|
| 154 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 155 | - |
|
| 156 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 157 | - |
|
| 158 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 159 | - 'Content-Type' => 'application/xml' |
|
| 160 | - ]); |
|
| 161 | - |
|
| 162 | - return $this->parseMultiStatus($response['body']); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - protected function download(string $url, string $userName, string $sharedSecret, string $resourcePath): array { |
|
| 166 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 167 | - return $client->request('GET', $resourcePath); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - private function buildSyncCollectionRequestBody(?string $syncToken): string { |
|
| 171 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 172 | - $dom->formatOutput = true; |
|
| 173 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 174 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 175 | - $prop = $dom->createElement('d:prop'); |
|
| 176 | - $cont = $dom->createElement('d:getcontenttype'); |
|
| 177 | - $etag = $dom->createElement('d:getetag'); |
|
| 178 | - |
|
| 179 | - $prop->appendChild($cont); |
|
| 180 | - $prop->appendChild($etag); |
|
| 181 | - $root->appendChild($sync); |
|
| 182 | - $root->appendChild($prop); |
|
| 183 | - $dom->appendChild($root); |
|
| 184 | - return $dom->saveXML(); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @param string $body |
|
| 189 | - * @return array |
|
| 190 | - * @throws \Sabre\Xml\ParseException |
|
| 191 | - */ |
|
| 192 | - private function parseMultiStatus($body) { |
|
| 193 | - $xml = new Service(); |
|
| 194 | - |
|
| 195 | - /** @var MultiStatus $multiStatus */ |
|
| 196 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 197 | - |
|
| 198 | - $result = []; |
|
| 199 | - foreach ($multiStatus->getResponses() as $response) { |
|
| 200 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param IUser $user |
|
| 208 | - */ |
|
| 209 | - public function updateUser(IUser $user) { |
|
| 210 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 211 | - $addressBookId = $systemAddressBook['id']; |
|
| 212 | - $name = $user->getBackendClassName(); |
|
| 213 | - $userId = $user->getUID(); |
|
| 214 | - |
|
| 215 | - $cardId = "$name:$userId.vcf"; |
|
| 216 | - if ($user->isEnabled()) { |
|
| 217 | - $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 218 | - if ($card === false) { |
|
| 219 | - $vCard = $this->converter->createCardFromUser($user); |
|
| 220 | - if ($vCard !== null) { |
|
| 221 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false); |
|
| 222 | - } |
|
| 223 | - } else { |
|
| 224 | - $vCard = $this->converter->createCardFromUser($user); |
|
| 225 | - if (is_null($vCard)) { |
|
| 226 | - $this->backend->deleteCard($addressBookId, $cardId); |
|
| 227 | - } else { |
|
| 228 | - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } else { |
|
| 232 | - $this->backend->deleteCard($addressBookId, $cardId); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @param IUser|string $userOrCardId |
|
| 238 | - */ |
|
| 239 | - public function deleteUser($userOrCardId) { |
|
| 240 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 241 | - if ($userOrCardId instanceof IUser) { |
|
| 242 | - $name = $userOrCardId->getBackendClassName(); |
|
| 243 | - $userId = $userOrCardId->getUID(); |
|
| 244 | - |
|
| 245 | - $userOrCardId = "$name:$userId.vcf"; |
|
| 246 | - } |
|
| 247 | - $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @return array|null |
|
| 252 | - */ |
|
| 253 | - public function getLocalSystemAddressBook() { |
|
| 254 | - if (is_null($this->localSystemAddressBook)) { |
|
| 255 | - $systemPrincipal = "principals/system/system"; |
|
| 256 | - $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 257 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 258 | - ]); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - return $this->localSystemAddressBook; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - public function syncInstance(\Closure $progressCallback = null) { |
|
| 265 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 266 | - $this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback) { |
|
| 267 | - $this->updateUser($user); |
|
| 268 | - if (!is_null($progressCallback)) { |
|
| 269 | - $progressCallback(); |
|
| 270 | - } |
|
| 271 | - }); |
|
| 272 | - |
|
| 273 | - // remove no longer existing |
|
| 274 | - $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 275 | - foreach ($allCards as $card) { |
|
| 276 | - $vCard = Reader::read($card['carddata']); |
|
| 277 | - $uid = $vCard->UID->getValue(); |
|
| 278 | - // load backend and see if user exists |
|
| 279 | - if (!$this->userManager->userExists($uid)) { |
|
| 280 | - $this->deleteUser($card['uri']); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - } |
|
| 43 | + private CardDavBackend $backend; |
|
| 44 | + private IUserManager $userManager; |
|
| 45 | + private LoggerInterface $logger; |
|
| 46 | + private ?array $localSystemAddressBook = null; |
|
| 47 | + private Converter $converter; |
|
| 48 | + protected string $certPath; |
|
| 49 | + |
|
| 50 | + public function __construct(CardDavBackend $backend, |
|
| 51 | + IUserManager $userManager, |
|
| 52 | + LoggerInterface $logger, |
|
| 53 | + Converter $converter) { |
|
| 54 | + $this->backend = $backend; |
|
| 55 | + $this->userManager = $userManager; |
|
| 56 | + $this->logger = $logger; |
|
| 57 | + $this->converter = $converter; |
|
| 58 | + $this->certPath = ''; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @throws \Exception |
|
| 63 | + */ |
|
| 64 | + public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken, string $targetBookHash, string $targetPrincipal, array $targetProperties): string { |
|
| 65 | + // 1. create addressbook |
|
| 66 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookHash, $targetProperties); |
|
| 67 | + $addressBookId = $book['id']; |
|
| 68 | + |
|
| 69 | + // 2. query changes |
|
| 70 | + try { |
|
| 71 | + $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
| 72 | + } catch (ClientHttpException $ex) { |
|
| 73 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 74 | + // remote server revoked access to the address book, remove it |
|
| 75 | + $this->backend->deleteAddressBook($addressBookId); |
|
| 76 | + $this->logger->error('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 77 | + throw $ex; |
|
| 78 | + } |
|
| 79 | + $this->logger->error('Client exception:', ['app' => 'dav', 'exception' => $ex]); |
|
| 80 | + throw $ex; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // 3. apply changes |
|
| 84 | + // TODO: use multi-get for download |
|
| 85 | + foreach ($response['response'] as $resource => $status) { |
|
| 86 | + $cardUri = basename($resource); |
|
| 87 | + if (isset($status[200])) { |
|
| 88 | + $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
| 89 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 90 | + if ($existingCard === false) { |
|
| 91 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 92 | + } else { |
|
| 93 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 94 | + } |
|
| 95 | + } else { |
|
| 96 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $response['token']; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 105 | + */ |
|
| 106 | + public function ensureSystemAddressBookExists(string $principal, string $uri, array $properties): ?array { |
|
| 107 | + $book = $this->backend->getAddressBooksByUri($principal, $uri); |
|
| 108 | + if (!is_null($book)) { |
|
| 109 | + return $book; |
|
| 110 | + } |
|
| 111 | + // FIXME This might break in clustered DB setup |
|
| 112 | + $this->backend->createAddressBook($principal, $uri, $properties); |
|
| 113 | + |
|
| 114 | + return $this->backend->getAddressBooksByUri($principal, $uri); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Check if there is a valid certPath we should use |
|
| 119 | + */ |
|
| 120 | + protected function getCertPath(): string { |
|
| 121 | + |
|
| 122 | + // we already have a valid certPath |
|
| 123 | + if ($this->certPath !== '') { |
|
| 124 | + return $this->certPath; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $certManager = \OC::$server->getCertificateManager(); |
|
| 128 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
| 129 | + if (file_exists($certPath)) { |
|
| 130 | + $this->certPath = $certPath; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this->certPath; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + protected function getClient(string $url, string $userName, string $sharedSecret): Client { |
|
| 137 | + $settings = [ |
|
| 138 | + 'baseUri' => $url . '/', |
|
| 139 | + 'userName' => $userName, |
|
| 140 | + 'password' => $sharedSecret, |
|
| 141 | + ]; |
|
| 142 | + $client = new Client($settings); |
|
| 143 | + $certPath = $this->getCertPath(); |
|
| 144 | + $client->setThrowExceptions(true); |
|
| 145 | + |
|
| 146 | + if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
| 147 | + $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $client; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + protected function requestSyncReport(string $url, string $userName, string $addressBookUrl, string $sharedSecret, ?string $syncToken): array { |
|
| 154 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 155 | + |
|
| 156 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 157 | + |
|
| 158 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 159 | + 'Content-Type' => 'application/xml' |
|
| 160 | + ]); |
|
| 161 | + |
|
| 162 | + return $this->parseMultiStatus($response['body']); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + protected function download(string $url, string $userName, string $sharedSecret, string $resourcePath): array { |
|
| 166 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 167 | + return $client->request('GET', $resourcePath); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + private function buildSyncCollectionRequestBody(?string $syncToken): string { |
|
| 171 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 172 | + $dom->formatOutput = true; |
|
| 173 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 174 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 175 | + $prop = $dom->createElement('d:prop'); |
|
| 176 | + $cont = $dom->createElement('d:getcontenttype'); |
|
| 177 | + $etag = $dom->createElement('d:getetag'); |
|
| 178 | + |
|
| 179 | + $prop->appendChild($cont); |
|
| 180 | + $prop->appendChild($etag); |
|
| 181 | + $root->appendChild($sync); |
|
| 182 | + $root->appendChild($prop); |
|
| 183 | + $dom->appendChild($root); |
|
| 184 | + return $dom->saveXML(); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @param string $body |
|
| 189 | + * @return array |
|
| 190 | + * @throws \Sabre\Xml\ParseException |
|
| 191 | + */ |
|
| 192 | + private function parseMultiStatus($body) { |
|
| 193 | + $xml = new Service(); |
|
| 194 | + |
|
| 195 | + /** @var MultiStatus $multiStatus */ |
|
| 196 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 197 | + |
|
| 198 | + $result = []; |
|
| 199 | + foreach ($multiStatus->getResponses() as $response) { |
|
| 200 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param IUser $user |
|
| 208 | + */ |
|
| 209 | + public function updateUser(IUser $user) { |
|
| 210 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 211 | + $addressBookId = $systemAddressBook['id']; |
|
| 212 | + $name = $user->getBackendClassName(); |
|
| 213 | + $userId = $user->getUID(); |
|
| 214 | + |
|
| 215 | + $cardId = "$name:$userId.vcf"; |
|
| 216 | + if ($user->isEnabled()) { |
|
| 217 | + $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 218 | + if ($card === false) { |
|
| 219 | + $vCard = $this->converter->createCardFromUser($user); |
|
| 220 | + if ($vCard !== null) { |
|
| 221 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false); |
|
| 222 | + } |
|
| 223 | + } else { |
|
| 224 | + $vCard = $this->converter->createCardFromUser($user); |
|
| 225 | + if (is_null($vCard)) { |
|
| 226 | + $this->backend->deleteCard($addressBookId, $cardId); |
|
| 227 | + } else { |
|
| 228 | + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } else { |
|
| 232 | + $this->backend->deleteCard($addressBookId, $cardId); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @param IUser|string $userOrCardId |
|
| 238 | + */ |
|
| 239 | + public function deleteUser($userOrCardId) { |
|
| 240 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 241 | + if ($userOrCardId instanceof IUser) { |
|
| 242 | + $name = $userOrCardId->getBackendClassName(); |
|
| 243 | + $userId = $userOrCardId->getUID(); |
|
| 244 | + |
|
| 245 | + $userOrCardId = "$name:$userId.vcf"; |
|
| 246 | + } |
|
| 247 | + $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @return array|null |
|
| 252 | + */ |
|
| 253 | + public function getLocalSystemAddressBook() { |
|
| 254 | + if (is_null($this->localSystemAddressBook)) { |
|
| 255 | + $systemPrincipal = "principals/system/system"; |
|
| 256 | + $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 257 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 258 | + ]); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + return $this->localSystemAddressBook; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + public function syncInstance(\Closure $progressCallback = null) { |
|
| 265 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 266 | + $this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback) { |
|
| 267 | + $this->updateUser($user); |
|
| 268 | + if (!is_null($progressCallback)) { |
|
| 269 | + $progressCallback(); |
|
| 270 | + } |
|
| 271 | + }); |
|
| 272 | + |
|
| 273 | + // remove no longer existing |
|
| 274 | + $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 275 | + foreach ($allCards as $card) { |
|
| 276 | + $vCard = Reader::read($card['carddata']); |
|
| 277 | + $uid = $vCard->UID->getValue(); |
|
| 278 | + // load backend and see if user exists |
|
| 279 | + if (!$this->userManager->userExists($uid)) { |
|
| 280 | + $this->deleteUser($card['uri']); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | 284 | } |