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