@@ -35,278 +35,278 @@ |
||
| 35 | 35 | |
| 36 | 36 | class AddressBookImpl implements IAddressBook { |
| 37 | 37 | |
| 38 | - /** @var CardDavBackend */ |
|
| 39 | - private $backend; |
|
| 40 | - |
|
| 41 | - /** @var array */ |
|
| 42 | - private $addressBookInfo; |
|
| 43 | - |
|
| 44 | - /** @var AddressBook */ |
|
| 45 | - private $addressBook; |
|
| 46 | - |
|
| 47 | - /** @var IURLGenerator */ |
|
| 48 | - private $urlGenerator; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * AddressBookImpl constructor. |
|
| 52 | - * |
|
| 53 | - * @param AddressBook $addressBook |
|
| 54 | - * @param array $addressBookInfo |
|
| 55 | - * @param CardDavBackend $backend |
|
| 56 | - * @param IUrlGenerator $urlGenerator |
|
| 57 | - */ |
|
| 58 | - public function __construct( |
|
| 59 | - AddressBook $addressBook, |
|
| 60 | - array $addressBookInfo, |
|
| 61 | - CardDavBackend $backend, |
|
| 62 | - IURLGenerator $urlGenerator) { |
|
| 63 | - |
|
| 64 | - $this->addressBook = $addressBook; |
|
| 65 | - $this->addressBookInfo = $addressBookInfo; |
|
| 66 | - $this->backend = $backend; |
|
| 67 | - $this->urlGenerator = $urlGenerator; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return string defining the technical unique key |
|
| 72 | - * @since 5.0.0 |
|
| 73 | - */ |
|
| 74 | - public function getKey() { |
|
| 75 | - return $this->addressBookInfo['id']; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 80 | - * |
|
| 81 | - * @return mixed |
|
| 82 | - * @since 5.0.0 |
|
| 83 | - */ |
|
| 84 | - public function getDisplayName() { |
|
| 85 | - return $this->addressBookInfo['{DAV:}displayname']; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string $pattern which should match within the $searchProperties |
|
| 90 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 91 | - * @param array $options Options to define the output format |
|
| 92 | - * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 93 | - * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 94 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 95 | - * example result: |
|
| 96 | - * [ |
|
| 97 | - * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 98 | - * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 99 | - * ] |
|
| 100 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 101 | - * @since 5.0.0 |
|
| 102 | - */ |
|
| 103 | - public function search($pattern, $searchProperties, $options) { |
|
| 104 | - $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 105 | - |
|
| 106 | - $withTypes = \array_key_exists('types', $options) && $options['types'] === true; |
|
| 107 | - |
|
| 108 | - $vCards = []; |
|
| 109 | - foreach ($results as $result) { |
|
| 110 | - $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $vCards; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param array $properties this array if key-value-pairs defines a contact |
|
| 118 | - * @return array an array representing the contact just created or updated |
|
| 119 | - * @since 5.0.0 |
|
| 120 | - */ |
|
| 121 | - public function createOrUpdate($properties) { |
|
| 122 | - $update = false; |
|
| 123 | - if (!isset($properties['URI'])) { // create a new contact |
|
| 124 | - $uid = $this->createUid(); |
|
| 125 | - $uri = $uid . '.vcf'; |
|
| 126 | - $vCard = $this->createEmptyVCard($uid); |
|
| 127 | - } else { // update existing contact |
|
| 128 | - $uri = $properties['URI']; |
|
| 129 | - $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 130 | - $vCard = $this->readCard($vCardData['carddata']); |
|
| 131 | - $update = true; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - foreach ($properties as $key => $value) { |
|
| 135 | - $vCard->$key = $vCard->createProperty($key, $value); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ($update) { |
|
| 139 | - $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 140 | - } else { |
|
| 141 | - $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $this->vCard2Array($uri, $vCard); |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @return mixed |
|
| 150 | - * @since 5.0.0 |
|
| 151 | - */ |
|
| 152 | - public function getPermissions() { |
|
| 153 | - $permissions = $this->addressBook->getACL(); |
|
| 154 | - $result = 0; |
|
| 155 | - foreach ($permissions as $permission) { |
|
| 156 | - switch($permission['privilege']) { |
|
| 157 | - case '{DAV:}read': |
|
| 158 | - $result |= Constants::PERMISSION_READ; |
|
| 159 | - break; |
|
| 160 | - case '{DAV:}write': |
|
| 161 | - $result |= Constants::PERMISSION_CREATE; |
|
| 162 | - $result |= Constants::PERMISSION_UPDATE; |
|
| 163 | - break; |
|
| 164 | - case '{DAV:}all': |
|
| 165 | - $result |= Constants::PERMISSION_ALL; |
|
| 166 | - break; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - return $result; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param object $id the unique identifier to a contact |
|
| 175 | - * @return bool successful or not |
|
| 176 | - * @since 5.0.0 |
|
| 177 | - */ |
|
| 178 | - public function delete($id) { |
|
| 179 | - $uri = $this->backend->getCardUri($id); |
|
| 180 | - return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * read vCard data into a vCard object |
|
| 185 | - * |
|
| 186 | - * @param string $cardData |
|
| 187 | - * @return VCard |
|
| 188 | - */ |
|
| 189 | - protected function readCard($cardData) { |
|
| 190 | - return Reader::read($cardData); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * create UID for contact |
|
| 195 | - * |
|
| 196 | - * @return string |
|
| 197 | - */ |
|
| 198 | - protected function createUid() { |
|
| 199 | - do { |
|
| 200 | - $uid = $this->getUid(); |
|
| 201 | - $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 202 | - } while (!empty($contact)); |
|
| 203 | - |
|
| 204 | - return $uid; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * getUid is only there for testing, use createUid instead |
|
| 209 | - */ |
|
| 210 | - protected function getUid() { |
|
| 211 | - return UUIDUtil::getUUID(); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * create empty vcard |
|
| 216 | - * |
|
| 217 | - * @param string $uid |
|
| 218 | - * @return VCard |
|
| 219 | - */ |
|
| 220 | - protected function createEmptyVCard($uid) { |
|
| 221 | - $vCard = new VCard(); |
|
| 222 | - $vCard->UID = $uid; |
|
| 223 | - return $vCard; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * create array with all vCard properties |
|
| 228 | - * |
|
| 229 | - * @param string $uri |
|
| 230 | - * @param VCard $vCard |
|
| 231 | - * @return array |
|
| 232 | - */ |
|
| 233 | - protected function vCard2Array($uri, VCard $vCard, $withTypes = false) { |
|
| 234 | - $result = [ |
|
| 235 | - 'URI' => $uri, |
|
| 236 | - ]; |
|
| 237 | - |
|
| 238 | - foreach ($vCard->children() as $property) { |
|
| 239 | - if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 240 | - $url = $this->urlGenerator->getAbsoluteURL( |
|
| 241 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 242 | - $url .= implode('/', [ |
|
| 243 | - 'addressbooks', |
|
| 244 | - substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 245 | - $this->addressBookInfo['uri'], |
|
| 246 | - $uri |
|
| 247 | - ]) . '?photo'; |
|
| 248 | - |
|
| 249 | - $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 250 | - |
|
| 251 | - } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 252 | - $type = $this->getTypeFromProperty($property); |
|
| 253 | - |
|
| 254 | - // Type is the social network, when it's empty we don't need this. |
|
| 255 | - if ($type !== null) { |
|
| 256 | - if (!isset($result[$property->name])) { |
|
| 257 | - $result[$property->name] = []; |
|
| 258 | - } |
|
| 259 | - $result[$property->name][$type] = $property->getValue(); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // The following properties can be set multiple times |
|
| 263 | - } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 264 | - if (!isset($result[$property->name])) { |
|
| 265 | - $result[$property->name] = []; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $type = $this->getTypeFromProperty($property); |
|
| 269 | - if ($withTypes) { |
|
| 270 | - $result[$property->name][] = [ |
|
| 271 | - 'type' => $type, |
|
| 272 | - 'value' => $property->getValue() |
|
| 273 | - ]; |
|
| 274 | - } else { |
|
| 275 | - $result[$property->name][] = $property->getValue(); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - |
|
| 279 | - } else { |
|
| 280 | - $result[$property->name] = $property->getValue(); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - if ( |
|
| 285 | - $this->addressBookInfo['principaluri'] === 'principals/system/system' && ( |
|
| 286 | - $this->addressBookInfo['uri'] === 'system' || |
|
| 287 | - $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl() |
|
| 288 | - ) |
|
| 289 | - ) { |
|
| 290 | - $result['isLocalSystemBook'] = true; |
|
| 291 | - } |
|
| 292 | - return $result; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Get the type of the current property |
|
| 297 | - * |
|
| 298 | - * @param Property $property |
|
| 299 | - * @return null|string |
|
| 300 | - */ |
|
| 301 | - protected function getTypeFromProperty(Property $property) { |
|
| 302 | - $parameters = $property->parameters(); |
|
| 303 | - // Type is the social network, when it's empty we don't need this. |
|
| 304 | - if (isset($parameters['TYPE'])) { |
|
| 305 | - /** @var \Sabre\VObject\Parameter $type */ |
|
| 306 | - $type = $parameters['TYPE']; |
|
| 307 | - return $type->getValue(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return null; |
|
| 311 | - } |
|
| 38 | + /** @var CardDavBackend */ |
|
| 39 | + private $backend; |
|
| 40 | + |
|
| 41 | + /** @var array */ |
|
| 42 | + private $addressBookInfo; |
|
| 43 | + |
|
| 44 | + /** @var AddressBook */ |
|
| 45 | + private $addressBook; |
|
| 46 | + |
|
| 47 | + /** @var IURLGenerator */ |
|
| 48 | + private $urlGenerator; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * AddressBookImpl constructor. |
|
| 52 | + * |
|
| 53 | + * @param AddressBook $addressBook |
|
| 54 | + * @param array $addressBookInfo |
|
| 55 | + * @param CardDavBackend $backend |
|
| 56 | + * @param IUrlGenerator $urlGenerator |
|
| 57 | + */ |
|
| 58 | + public function __construct( |
|
| 59 | + AddressBook $addressBook, |
|
| 60 | + array $addressBookInfo, |
|
| 61 | + CardDavBackend $backend, |
|
| 62 | + IURLGenerator $urlGenerator) { |
|
| 63 | + |
|
| 64 | + $this->addressBook = $addressBook; |
|
| 65 | + $this->addressBookInfo = $addressBookInfo; |
|
| 66 | + $this->backend = $backend; |
|
| 67 | + $this->urlGenerator = $urlGenerator; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return string defining the technical unique key |
|
| 72 | + * @since 5.0.0 |
|
| 73 | + */ |
|
| 74 | + public function getKey() { |
|
| 75 | + return $this->addressBookInfo['id']; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 80 | + * |
|
| 81 | + * @return mixed |
|
| 82 | + * @since 5.0.0 |
|
| 83 | + */ |
|
| 84 | + public function getDisplayName() { |
|
| 85 | + return $this->addressBookInfo['{DAV:}displayname']; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string $pattern which should match within the $searchProperties |
|
| 90 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 91 | + * @param array $options Options to define the output format |
|
| 92 | + * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 93 | + * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 94 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 95 | + * example result: |
|
| 96 | + * [ |
|
| 97 | + * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 98 | + * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 99 | + * ] |
|
| 100 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 101 | + * @since 5.0.0 |
|
| 102 | + */ |
|
| 103 | + public function search($pattern, $searchProperties, $options) { |
|
| 104 | + $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 105 | + |
|
| 106 | + $withTypes = \array_key_exists('types', $options) && $options['types'] === true; |
|
| 107 | + |
|
| 108 | + $vCards = []; |
|
| 109 | + foreach ($results as $result) { |
|
| 110 | + $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $vCards; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param array $properties this array if key-value-pairs defines a contact |
|
| 118 | + * @return array an array representing the contact just created or updated |
|
| 119 | + * @since 5.0.0 |
|
| 120 | + */ |
|
| 121 | + public function createOrUpdate($properties) { |
|
| 122 | + $update = false; |
|
| 123 | + if (!isset($properties['URI'])) { // create a new contact |
|
| 124 | + $uid = $this->createUid(); |
|
| 125 | + $uri = $uid . '.vcf'; |
|
| 126 | + $vCard = $this->createEmptyVCard($uid); |
|
| 127 | + } else { // update existing contact |
|
| 128 | + $uri = $properties['URI']; |
|
| 129 | + $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 130 | + $vCard = $this->readCard($vCardData['carddata']); |
|
| 131 | + $update = true; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + foreach ($properties as $key => $value) { |
|
| 135 | + $vCard->$key = $vCard->createProperty($key, $value); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ($update) { |
|
| 139 | + $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 140 | + } else { |
|
| 141 | + $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $this->vCard2Array($uri, $vCard); |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @return mixed |
|
| 150 | + * @since 5.0.0 |
|
| 151 | + */ |
|
| 152 | + public function getPermissions() { |
|
| 153 | + $permissions = $this->addressBook->getACL(); |
|
| 154 | + $result = 0; |
|
| 155 | + foreach ($permissions as $permission) { |
|
| 156 | + switch($permission['privilege']) { |
|
| 157 | + case '{DAV:}read': |
|
| 158 | + $result |= Constants::PERMISSION_READ; |
|
| 159 | + break; |
|
| 160 | + case '{DAV:}write': |
|
| 161 | + $result |= Constants::PERMISSION_CREATE; |
|
| 162 | + $result |= Constants::PERMISSION_UPDATE; |
|
| 163 | + break; |
|
| 164 | + case '{DAV:}all': |
|
| 165 | + $result |= Constants::PERMISSION_ALL; |
|
| 166 | + break; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + return $result; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param object $id the unique identifier to a contact |
|
| 175 | + * @return bool successful or not |
|
| 176 | + * @since 5.0.0 |
|
| 177 | + */ |
|
| 178 | + public function delete($id) { |
|
| 179 | + $uri = $this->backend->getCardUri($id); |
|
| 180 | + return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * read vCard data into a vCard object |
|
| 185 | + * |
|
| 186 | + * @param string $cardData |
|
| 187 | + * @return VCard |
|
| 188 | + */ |
|
| 189 | + protected function readCard($cardData) { |
|
| 190 | + return Reader::read($cardData); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * create UID for contact |
|
| 195 | + * |
|
| 196 | + * @return string |
|
| 197 | + */ |
|
| 198 | + protected function createUid() { |
|
| 199 | + do { |
|
| 200 | + $uid = $this->getUid(); |
|
| 201 | + $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 202 | + } while (!empty($contact)); |
|
| 203 | + |
|
| 204 | + return $uid; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * getUid is only there for testing, use createUid instead |
|
| 209 | + */ |
|
| 210 | + protected function getUid() { |
|
| 211 | + return UUIDUtil::getUUID(); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * create empty vcard |
|
| 216 | + * |
|
| 217 | + * @param string $uid |
|
| 218 | + * @return VCard |
|
| 219 | + */ |
|
| 220 | + protected function createEmptyVCard($uid) { |
|
| 221 | + $vCard = new VCard(); |
|
| 222 | + $vCard->UID = $uid; |
|
| 223 | + return $vCard; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * create array with all vCard properties |
|
| 228 | + * |
|
| 229 | + * @param string $uri |
|
| 230 | + * @param VCard $vCard |
|
| 231 | + * @return array |
|
| 232 | + */ |
|
| 233 | + protected function vCard2Array($uri, VCard $vCard, $withTypes = false) { |
|
| 234 | + $result = [ |
|
| 235 | + 'URI' => $uri, |
|
| 236 | + ]; |
|
| 237 | + |
|
| 238 | + foreach ($vCard->children() as $property) { |
|
| 239 | + if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 240 | + $url = $this->urlGenerator->getAbsoluteURL( |
|
| 241 | + $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 242 | + $url .= implode('/', [ |
|
| 243 | + 'addressbooks', |
|
| 244 | + substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 245 | + $this->addressBookInfo['uri'], |
|
| 246 | + $uri |
|
| 247 | + ]) . '?photo'; |
|
| 248 | + |
|
| 249 | + $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 250 | + |
|
| 251 | + } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 252 | + $type = $this->getTypeFromProperty($property); |
|
| 253 | + |
|
| 254 | + // Type is the social network, when it's empty we don't need this. |
|
| 255 | + if ($type !== null) { |
|
| 256 | + if (!isset($result[$property->name])) { |
|
| 257 | + $result[$property->name] = []; |
|
| 258 | + } |
|
| 259 | + $result[$property->name][$type] = $property->getValue(); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // The following properties can be set multiple times |
|
| 263 | + } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 264 | + if (!isset($result[$property->name])) { |
|
| 265 | + $result[$property->name] = []; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $type = $this->getTypeFromProperty($property); |
|
| 269 | + if ($withTypes) { |
|
| 270 | + $result[$property->name][] = [ |
|
| 271 | + 'type' => $type, |
|
| 272 | + 'value' => $property->getValue() |
|
| 273 | + ]; |
|
| 274 | + } else { |
|
| 275 | + $result[$property->name][] = $property->getValue(); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + |
|
| 279 | + } else { |
|
| 280 | + $result[$property->name] = $property->getValue(); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + if ( |
|
| 285 | + $this->addressBookInfo['principaluri'] === 'principals/system/system' && ( |
|
| 286 | + $this->addressBookInfo['uri'] === 'system' || |
|
| 287 | + $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl() |
|
| 288 | + ) |
|
| 289 | + ) { |
|
| 290 | + $result['isLocalSystemBook'] = true; |
|
| 291 | + } |
|
| 292 | + return $result; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Get the type of the current property |
|
| 297 | + * |
|
| 298 | + * @param Property $property |
|
| 299 | + * @return null|string |
|
| 300 | + */ |
|
| 301 | + protected function getTypeFromProperty(Property $property) { |
|
| 302 | + $parameters = $property->parameters(); |
|
| 303 | + // Type is the social network, when it's empty we don't need this. |
|
| 304 | + if (isset($parameters['TYPE'])) { |
|
| 305 | + /** @var \Sabre\VObject\Parameter $type */ |
|
| 306 | + $type = $parameters['TYPE']; |
|
| 307 | + return $type->getValue(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return null; |
|
| 311 | + } |
|
| 312 | 312 | } |
@@ -31,66 +31,66 @@ |
||
| 31 | 31 | // use OCP namespace for all classes that are considered public. |
| 32 | 32 | // This means that they should be used by apps instead of the internal ownCloud classes |
| 33 | 33 | namespace OCP { |
| 34 | - /** |
|
| 35 | - * Interface IAddressBook |
|
| 36 | - * |
|
| 37 | - * @package OCP |
|
| 38 | - * @since 5.0.0 |
|
| 39 | - */ |
|
| 40 | - interface IAddressBook { |
|
| 34 | + /** |
|
| 35 | + * Interface IAddressBook |
|
| 36 | + * |
|
| 37 | + * @package OCP |
|
| 38 | + * @since 5.0.0 |
|
| 39 | + */ |
|
| 40 | + interface IAddressBook { |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return string defining the technical unique key |
|
| 44 | - * @since 5.0.0 |
|
| 45 | - */ |
|
| 46 | - public function getKey(); |
|
| 42 | + /** |
|
| 43 | + * @return string defining the technical unique key |
|
| 44 | + * @since 5.0.0 |
|
| 45 | + */ |
|
| 46 | + public function getKey(); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 50 | - * @return mixed |
|
| 51 | - * @since 5.0.0 |
|
| 52 | - */ |
|
| 53 | - public function getDisplayName(); |
|
| 48 | + /** |
|
| 49 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 50 | + * @return mixed |
|
| 51 | + * @since 5.0.0 |
|
| 52 | + */ |
|
| 53 | + public function getDisplayName(); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @param string $pattern which should match within the $searchProperties |
|
| 57 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 58 | - * @param array $options Options to define the output format |
|
| 59 | - * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 60 | - * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 61 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 62 | - * example result: |
|
| 63 | - * [ |
|
| 64 | - * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 65 | - * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 66 | - * ] |
|
| 67 | - * @since 5.0.0 |
|
| 68 | - */ |
|
| 69 | - public function search($pattern, $searchProperties, $options); |
|
| 55 | + /** |
|
| 56 | + * @param string $pattern which should match within the $searchProperties |
|
| 57 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 58 | + * @param array $options Options to define the output format |
|
| 59 | + * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 60 | + * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 61 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 62 | + * example result: |
|
| 63 | + * [ |
|
| 64 | + * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 65 | + * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 66 | + * ] |
|
| 67 | + * @since 5.0.0 |
|
| 68 | + */ |
|
| 69 | + public function search($pattern, $searchProperties, $options); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param array $properties this array if key-value-pairs defines a contact |
|
| 73 | - * @return array an array representing the contact just created or updated |
|
| 74 | - * @since 5.0.0 |
|
| 75 | - */ |
|
| 76 | - public function createOrUpdate($properties); |
|
| 77 | - // // dummy |
|
| 78 | - // return array('id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', |
|
| 79 | - // 'PHOTO' => 'VALUE=uri:http://www.abc.com/pub/photos/jqpublic.gif', |
|
| 80 | - // 'ADR' => ';;123 Main Street;Any Town;CA;91921-1234' |
|
| 81 | - // ); |
|
| 71 | + /** |
|
| 72 | + * @param array $properties this array if key-value-pairs defines a contact |
|
| 73 | + * @return array an array representing the contact just created or updated |
|
| 74 | + * @since 5.0.0 |
|
| 75 | + */ |
|
| 76 | + public function createOrUpdate($properties); |
|
| 77 | + // // dummy |
|
| 78 | + // return array('id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', |
|
| 79 | + // 'PHOTO' => 'VALUE=uri:http://www.abc.com/pub/photos/jqpublic.gif', |
|
| 80 | + // 'ADR' => ';;123 Main Street;Any Town;CA;91921-1234' |
|
| 81 | + // ); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @return mixed |
|
| 85 | - * @since 5.0.0 |
|
| 86 | - */ |
|
| 87 | - public function getPermissions(); |
|
| 83 | + /** |
|
| 84 | + * @return mixed |
|
| 85 | + * @since 5.0.0 |
|
| 86 | + */ |
|
| 87 | + public function getPermissions(); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param object $id the unique identifier to a contact |
|
| 91 | - * @return bool successful or not |
|
| 92 | - * @since 5.0.0 |
|
| 93 | - */ |
|
| 94 | - public function delete($id); |
|
| 95 | - } |
|
| 89 | + /** |
|
| 90 | + * @param object $id the unique identifier to a contact |
|
| 91 | + * @return bool successful or not |
|
| 92 | + * @since 5.0.0 |
|
| 93 | + */ |
|
| 94 | + public function delete($id); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -35,152 +35,152 @@ |
||
| 35 | 35 | use OCP\Share; |
| 36 | 36 | |
| 37 | 37 | class RemotePlugin implements ISearchPlugin { |
| 38 | - protected $shareeEnumeration; |
|
| 38 | + protected $shareeEnumeration; |
|
| 39 | 39 | |
| 40 | - /** @var IManager */ |
|
| 41 | - private $contactsManager; |
|
| 42 | - /** @var ICloudIdManager */ |
|
| 43 | - private $cloudIdManager; |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - private $config; |
|
| 46 | - /** @var IUserManager */ |
|
| 47 | - private $userManager; |
|
| 48 | - /** @var string */ |
|
| 49 | - private $userId = ''; |
|
| 40 | + /** @var IManager */ |
|
| 41 | + private $contactsManager; |
|
| 42 | + /** @var ICloudIdManager */ |
|
| 43 | + private $cloudIdManager; |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + private $config; |
|
| 46 | + /** @var IUserManager */ |
|
| 47 | + private $userManager; |
|
| 48 | + /** @var string */ |
|
| 49 | + private $userId = ''; |
|
| 50 | 50 | |
| 51 | - public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IUserManager $userManager, IUserSession $userSession) { |
|
| 52 | - $this->contactsManager = $contactsManager; |
|
| 53 | - $this->cloudIdManager = $cloudIdManager; |
|
| 54 | - $this->config = $config; |
|
| 55 | - $this->userManager = $userManager; |
|
| 56 | - $user = $userSession->getUser(); |
|
| 57 | - if ($user !== null) { |
|
| 58 | - $this->userId = $user->getUID(); |
|
| 59 | - } |
|
| 60 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 61 | - } |
|
| 51 | + public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IUserManager $userManager, IUserSession $userSession) { |
|
| 52 | + $this->contactsManager = $contactsManager; |
|
| 53 | + $this->cloudIdManager = $cloudIdManager; |
|
| 54 | + $this->config = $config; |
|
| 55 | + $this->userManager = $userManager; |
|
| 56 | + $user = $userSession->getUser(); |
|
| 57 | + if ($user !== null) { |
|
| 58 | + $this->userId = $user->getUID(); |
|
| 59 | + } |
|
| 60 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 64 | - $result = ['wide' => [], 'exact' => []]; |
|
| 65 | - $resultType = new SearchResultType('remotes'); |
|
| 63 | + public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 64 | + $result = ['wide' => [], 'exact' => []]; |
|
| 65 | + $resultType = new SearchResultType('remotes'); |
|
| 66 | 66 | |
| 67 | - // Search in contacts |
|
| 68 | - //@todo Pagination missing |
|
| 69 | - $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']); |
|
| 70 | - foreach ($addressBookContacts as $contact) { |
|
| 71 | - if (isset($contact['isLocalSystemBook'])) { |
|
| 72 | - continue; |
|
| 73 | - } |
|
| 74 | - if (isset($contact['CLOUD'])) { |
|
| 75 | - $cloudIds = $contact['CLOUD']; |
|
| 76 | - if (is_string($cloudIds)) { |
|
| 77 | - $cloudIds = [$cloudIds]; |
|
| 78 | - } |
|
| 79 | - $lowerSearch = strtolower($search); |
|
| 80 | - foreach ($cloudIds as $cloudId) { |
|
| 81 | - $cloudIdType = ''; |
|
| 82 | - if (\is_array($cloudId)) { |
|
| 83 | - $cloudIdData = $cloudId; |
|
| 84 | - $cloudId = $cloudIdData['value']; |
|
| 85 | - $cloudIdType = $cloudIdData['type']; |
|
| 86 | - } |
|
| 87 | - try { |
|
| 88 | - list($remoteUser, $serverUrl) = $this->splitUserRemote($cloudId); |
|
| 89 | - } catch (\InvalidArgumentException $e) { |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 67 | + // Search in contacts |
|
| 68 | + //@todo Pagination missing |
|
| 69 | + $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']); |
|
| 70 | + foreach ($addressBookContacts as $contact) { |
|
| 71 | + if (isset($contact['isLocalSystemBook'])) { |
|
| 72 | + continue; |
|
| 73 | + } |
|
| 74 | + if (isset($contact['CLOUD'])) { |
|
| 75 | + $cloudIds = $contact['CLOUD']; |
|
| 76 | + if (is_string($cloudIds)) { |
|
| 77 | + $cloudIds = [$cloudIds]; |
|
| 78 | + } |
|
| 79 | + $lowerSearch = strtolower($search); |
|
| 80 | + foreach ($cloudIds as $cloudId) { |
|
| 81 | + $cloudIdType = ''; |
|
| 82 | + if (\is_array($cloudId)) { |
|
| 83 | + $cloudIdData = $cloudId; |
|
| 84 | + $cloudId = $cloudIdData['value']; |
|
| 85 | + $cloudIdType = $cloudIdData['type']; |
|
| 86 | + } |
|
| 87 | + try { |
|
| 88 | + list($remoteUser, $serverUrl) = $this->splitUserRemote($cloudId); |
|
| 89 | + } catch (\InvalidArgumentException $e) { |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - $localUser = $this->userManager->get($remoteUser); |
|
| 94 | - /** |
|
| 95 | - * Add local share if remote cloud id matches a local user ones |
|
| 96 | - */ |
|
| 97 | - if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId() ) { |
|
| 98 | - $result['wide'][] = [ |
|
| 99 | - 'label' => $contact['FN'], |
|
| 100 | - 'uuid' => $contact['UID'], |
|
| 101 | - 'value' => [ |
|
| 102 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 103 | - 'shareWith' => $remoteUser |
|
| 104 | - ] |
|
| 105 | - ]; |
|
| 106 | - } |
|
| 93 | + $localUser = $this->userManager->get($remoteUser); |
|
| 94 | + /** |
|
| 95 | + * Add local share if remote cloud id matches a local user ones |
|
| 96 | + */ |
|
| 97 | + if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId() ) { |
|
| 98 | + $result['wide'][] = [ |
|
| 99 | + 'label' => $contact['FN'], |
|
| 100 | + 'uuid' => $contact['UID'], |
|
| 101 | + 'value' => [ |
|
| 102 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 103 | + 'shareWith' => $remoteUser |
|
| 104 | + ] |
|
| 105 | + ]; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { |
|
| 109 | - if (strtolower($cloudId) === $lowerSearch) { |
|
| 110 | - $searchResult->markExactIdMatch($resultType); |
|
| 111 | - } |
|
| 112 | - $result['exact'][] = [ |
|
| 113 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
| 114 | - 'uuid' => $contact['UID'], |
|
| 115 | - 'name' => $contact['FN'], |
|
| 116 | - 'type' => $cloudIdType, |
|
| 117 | - 'value' => [ |
|
| 118 | - 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 119 | - 'shareWith' => $cloudId, |
|
| 120 | - 'server' => $serverUrl, |
|
| 121 | - ], |
|
| 122 | - ]; |
|
| 123 | - } else { |
|
| 124 | - $result['wide'][] = [ |
|
| 125 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
| 126 | - 'uuid' => $contact['UID'], |
|
| 127 | - 'name' => $contact['FN'], |
|
| 128 | - 'type' => $cloudIdType, |
|
| 129 | - 'value' => [ |
|
| 130 | - 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 131 | - 'shareWith' => $cloudId, |
|
| 132 | - 'server' => $serverUrl, |
|
| 133 | - ], |
|
| 134 | - ]; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 108 | + if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { |
|
| 109 | + if (strtolower($cloudId) === $lowerSearch) { |
|
| 110 | + $searchResult->markExactIdMatch($resultType); |
|
| 111 | + } |
|
| 112 | + $result['exact'][] = [ |
|
| 113 | + 'label' => $contact['FN'] . " ($cloudId)", |
|
| 114 | + 'uuid' => $contact['UID'], |
|
| 115 | + 'name' => $contact['FN'], |
|
| 116 | + 'type' => $cloudIdType, |
|
| 117 | + 'value' => [ |
|
| 118 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 119 | + 'shareWith' => $cloudId, |
|
| 120 | + 'server' => $serverUrl, |
|
| 121 | + ], |
|
| 122 | + ]; |
|
| 123 | + } else { |
|
| 124 | + $result['wide'][] = [ |
|
| 125 | + 'label' => $contact['FN'] . " ($cloudId)", |
|
| 126 | + 'uuid' => $contact['UID'], |
|
| 127 | + 'name' => $contact['FN'], |
|
| 128 | + 'type' => $cloudIdType, |
|
| 129 | + 'value' => [ |
|
| 130 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 131 | + 'shareWith' => $cloudId, |
|
| 132 | + 'server' => $serverUrl, |
|
| 133 | + ], |
|
| 134 | + ]; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - if (!$this->shareeEnumeration) { |
|
| 141 | - $result['wide'] = []; |
|
| 142 | - } else { |
|
| 143 | - $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
| 144 | - } |
|
| 140 | + if (!$this->shareeEnumeration) { |
|
| 141 | + $result['wide'] = []; |
|
| 142 | + } else { |
|
| 143 | + $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Add generic share with remote item for valid cloud ids that are not users of the local instance |
|
| 148 | - */ |
|
| 149 | - if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { |
|
| 150 | - try { |
|
| 151 | - list($remoteUser, $serverUrl) = $this->splitUserRemote($search); |
|
| 152 | - $localUser = $this->userManager->get($remoteUser); |
|
| 153 | - if ($localUser === null || $search !== $localUser->getCloudId()) { |
|
| 154 | - $result['exact'][] = [ |
|
| 155 | - 'label' => $search, |
|
| 156 | - 'value' => [ |
|
| 157 | - 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 158 | - 'shareWith' => $search, |
|
| 159 | - ], |
|
| 160 | - ]; |
|
| 161 | - } |
|
| 162 | - } catch (\InvalidArgumentException $e) { |
|
| 163 | - } |
|
| 164 | - } |
|
| 146 | + /** |
|
| 147 | + * Add generic share with remote item for valid cloud ids that are not users of the local instance |
|
| 148 | + */ |
|
| 149 | + if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { |
|
| 150 | + try { |
|
| 151 | + list($remoteUser, $serverUrl) = $this->splitUserRemote($search); |
|
| 152 | + $localUser = $this->userManager->get($remoteUser); |
|
| 153 | + if ($localUser === null || $search !== $localUser->getCloudId()) { |
|
| 154 | + $result['exact'][] = [ |
|
| 155 | + 'label' => $search, |
|
| 156 | + 'value' => [ |
|
| 157 | + 'shareType' => Share::SHARE_TYPE_REMOTE, |
|
| 158 | + 'shareWith' => $search, |
|
| 159 | + ], |
|
| 160 | + ]; |
|
| 161 | + } |
|
| 162 | + } catch (\InvalidArgumentException $e) { |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); |
|
| 166 | + $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); |
|
| 167 | 167 | |
| 168 | - return true; |
|
| 169 | - } |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * split user and remote from federated cloud id |
|
| 173 | - * |
|
| 174 | - * @param string $address federated share address |
|
| 175 | - * @return array [user, remoteURL] |
|
| 176 | - * @throws \InvalidArgumentException |
|
| 177 | - */ |
|
| 178 | - public function splitUserRemote($address) { |
|
| 179 | - try { |
|
| 180 | - $cloudId = $this->cloudIdManager->resolveCloudId($address); |
|
| 181 | - return [$cloudId->getUser(), $cloudId->getRemote()]; |
|
| 182 | - } catch (\InvalidArgumentException $e) { |
|
| 183 | - throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e); |
|
| 184 | - } |
|
| 185 | - } |
|
| 171 | + /** |
|
| 172 | + * split user and remote from federated cloud id |
|
| 173 | + * |
|
| 174 | + * @param string $address federated share address |
|
| 175 | + * @return array [user, remoteURL] |
|
| 176 | + * @throws \InvalidArgumentException |
|
| 177 | + */ |
|
| 178 | + public function splitUserRemote($address) { |
|
| 179 | + try { |
|
| 180 | + $cloudId = $this->cloudIdManager->resolveCloudId($address); |
|
| 181 | + return [$cloudId->getUser(), $cloudId->getRemote()]; |
|
| 182 | + } catch (\InvalidArgumentException $e) { |
|
| 183 | + throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | /** |
| 95 | 95 | * Add local share if remote cloud id matches a local user ones |
| 96 | 96 | */ |
| 97 | - if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId() ) { |
|
| 97 | + if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId()) { |
|
| 98 | 98 | $result['wide'][] = [ |
| 99 | 99 | 'label' => $contact['FN'], |
| 100 | 100 | 'uuid' => $contact['UID'], |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $searchResult->markExactIdMatch($resultType); |
| 111 | 111 | } |
| 112 | 112 | $result['exact'][] = [ |
| 113 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
| 113 | + 'label' => $contact['FN']." ($cloudId)", |
|
| 114 | 114 | 'uuid' => $contact['UID'], |
| 115 | 115 | 'name' => $contact['FN'], |
| 116 | 116 | 'type' => $cloudIdType, |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | ]; |
| 123 | 123 | } else { |
| 124 | 124 | $result['wide'][] = [ |
| 125 | - 'label' => $contact['FN'] . " ($cloudId)", |
|
| 125 | + 'label' => $contact['FN']." ($cloudId)", |
|
| 126 | 126 | 'uuid' => $contact['UID'], |
| 127 | 127 | 'name' => $contact['FN'], |
| 128 | 128 | 'type' => $cloudIdType, |
@@ -37,197 +37,197 @@ |
||
| 37 | 37 | use OCP\Share; |
| 38 | 38 | |
| 39 | 39 | class MailPlugin implements ISearchPlugin { |
| 40 | - protected $shareeEnumeration; |
|
| 41 | - protected $shareWithGroupOnly; |
|
| 42 | - |
|
| 43 | - /** @var IManager */ |
|
| 44 | - private $contactsManager; |
|
| 45 | - /** @var ICloudIdManager */ |
|
| 46 | - private $cloudIdManager; |
|
| 47 | - /** @var IConfig */ |
|
| 48 | - private $config; |
|
| 49 | - |
|
| 50 | - /** @var IGroupManager */ |
|
| 51 | - private $groupManager; |
|
| 52 | - |
|
| 53 | - /** @var IUserSession */ |
|
| 54 | - private $userSession; |
|
| 55 | - |
|
| 56 | - public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 57 | - $this->contactsManager = $contactsManager; |
|
| 58 | - $this->cloudIdManager = $cloudIdManager; |
|
| 59 | - $this->config = $config; |
|
| 60 | - $this->groupManager = $groupManager; |
|
| 61 | - $this->userSession = $userSession; |
|
| 62 | - |
|
| 63 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 64 | - $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param $search |
|
| 69 | - * @param $limit |
|
| 70 | - * @param $offset |
|
| 71 | - * @param ISearchResult $searchResult |
|
| 72 | - * @return bool |
|
| 73 | - * @since 13.0.0 |
|
| 74 | - */ |
|
| 75 | - public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 76 | - $result = $userResults = ['wide' => [], 'exact' => []]; |
|
| 77 | - $userType = new SearchResultType('users'); |
|
| 78 | - $emailType = new SearchResultType('emails'); |
|
| 79 | - |
|
| 80 | - // Search in contacts |
|
| 81 | - //@todo Pagination missing |
|
| 82 | - $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); |
|
| 83 | - $lowerSearch = strtolower($search); |
|
| 84 | - foreach ($addressBookContacts as $contact) { |
|
| 85 | - if (isset($contact['EMAIL'])) { |
|
| 86 | - $emailAddresses = $contact['EMAIL']; |
|
| 87 | - if (\is_string($emailAddresses)) { |
|
| 88 | - $emailAddresses = [$emailAddresses]; |
|
| 89 | - } |
|
| 90 | - foreach ($emailAddresses as $type => $emailAddress) { |
|
| 91 | - $displayName = $emailAddress; |
|
| 92 | - $emailAddressType = null; |
|
| 93 | - if (\is_array($emailAddress)) { |
|
| 94 | - $emailAddressData = $emailAddress; |
|
| 95 | - $emailAddress = $emailAddressData['value']; |
|
| 96 | - $emailAddressType = $emailAddressData['type']; |
|
| 97 | - } |
|
| 98 | - if (isset($contact['FN'])) { |
|
| 99 | - $displayName = $contact['FN'] . ' (' . $emailAddress . ')'; |
|
| 100 | - } |
|
| 101 | - $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
|
| 102 | - |
|
| 103 | - if (isset($contact['isLocalSystemBook'])) { |
|
| 104 | - if ($this->shareWithGroupOnly) { |
|
| 105 | - /* |
|
| 40 | + protected $shareeEnumeration; |
|
| 41 | + protected $shareWithGroupOnly; |
|
| 42 | + |
|
| 43 | + /** @var IManager */ |
|
| 44 | + private $contactsManager; |
|
| 45 | + /** @var ICloudIdManager */ |
|
| 46 | + private $cloudIdManager; |
|
| 47 | + /** @var IConfig */ |
|
| 48 | + private $config; |
|
| 49 | + |
|
| 50 | + /** @var IGroupManager */ |
|
| 51 | + private $groupManager; |
|
| 52 | + |
|
| 53 | + /** @var IUserSession */ |
|
| 54 | + private $userSession; |
|
| 55 | + |
|
| 56 | + public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 57 | + $this->contactsManager = $contactsManager; |
|
| 58 | + $this->cloudIdManager = $cloudIdManager; |
|
| 59 | + $this->config = $config; |
|
| 60 | + $this->groupManager = $groupManager; |
|
| 61 | + $this->userSession = $userSession; |
|
| 62 | + |
|
| 63 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 64 | + $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param $search |
|
| 69 | + * @param $limit |
|
| 70 | + * @param $offset |
|
| 71 | + * @param ISearchResult $searchResult |
|
| 72 | + * @return bool |
|
| 73 | + * @since 13.0.0 |
|
| 74 | + */ |
|
| 75 | + public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 76 | + $result = $userResults = ['wide' => [], 'exact' => []]; |
|
| 77 | + $userType = new SearchResultType('users'); |
|
| 78 | + $emailType = new SearchResultType('emails'); |
|
| 79 | + |
|
| 80 | + // Search in contacts |
|
| 81 | + //@todo Pagination missing |
|
| 82 | + $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); |
|
| 83 | + $lowerSearch = strtolower($search); |
|
| 84 | + foreach ($addressBookContacts as $contact) { |
|
| 85 | + if (isset($contact['EMAIL'])) { |
|
| 86 | + $emailAddresses = $contact['EMAIL']; |
|
| 87 | + if (\is_string($emailAddresses)) { |
|
| 88 | + $emailAddresses = [$emailAddresses]; |
|
| 89 | + } |
|
| 90 | + foreach ($emailAddresses as $type => $emailAddress) { |
|
| 91 | + $displayName = $emailAddress; |
|
| 92 | + $emailAddressType = null; |
|
| 93 | + if (\is_array($emailAddress)) { |
|
| 94 | + $emailAddressData = $emailAddress; |
|
| 95 | + $emailAddress = $emailAddressData['value']; |
|
| 96 | + $emailAddressType = $emailAddressData['type']; |
|
| 97 | + } |
|
| 98 | + if (isset($contact['FN'])) { |
|
| 99 | + $displayName = $contact['FN'] . ' (' . $emailAddress . ')'; |
|
| 100 | + } |
|
| 101 | + $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
|
| 102 | + |
|
| 103 | + if (isset($contact['isLocalSystemBook'])) { |
|
| 104 | + if ($this->shareWithGroupOnly) { |
|
| 105 | + /* |
|
| 106 | 106 | * Check if the user may share with the user associated with the e-mail of the just found contact |
| 107 | 107 | */ |
| 108 | - $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
|
| 109 | - $found = false; |
|
| 110 | - foreach ($userGroups as $userGroup) { |
|
| 111 | - if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) { |
|
| 112 | - $found = true; |
|
| 113 | - break; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - if (!$found) { |
|
| 117 | - continue; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - if ($exactEmailMatch) { |
|
| 121 | - try { |
|
| 122 | - $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
|
| 123 | - } catch (\InvalidArgumentException $e) { |
|
| 124 | - continue; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 128 | - $singleResult = [[ |
|
| 129 | - 'label' => $displayName, |
|
| 130 | - 'uuid' => $contact['UID'], |
|
| 131 | - 'name' => $contact['FN'], |
|
| 132 | - 'value' => [ |
|
| 133 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 134 | - 'shareWith' => $cloud->getUser(), |
|
| 135 | - ], |
|
| 136 | - ]]; |
|
| 137 | - $searchResult->addResultSet($userType, [], $singleResult); |
|
| 138 | - $searchResult->markExactIdMatch($emailType); |
|
| 139 | - } |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if ($this->shareeEnumeration) { |
|
| 144 | - try { |
|
| 145 | - $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
|
| 146 | - } catch (\InvalidArgumentException $e) { |
|
| 147 | - continue; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 151 | - $userResults['wide'][] = [ |
|
| 152 | - 'label' => $displayName, |
|
| 153 | - 'uuid' => $contact['UID'], |
|
| 154 | - 'name' => $contact['FN'], |
|
| 155 | - 'value' => [ |
|
| 156 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 157 | - 'shareWith' => $cloud->getUser(), |
|
| 158 | - ], |
|
| 159 | - ]; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - continue; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ($exactEmailMatch |
|
| 166 | - || isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch) |
|
| 167 | - { |
|
| 168 | - if ($exactEmailMatch) { |
|
| 169 | - $searchResult->markExactIdMatch($emailType); |
|
| 170 | - } |
|
| 171 | - $result['exact'][] = [ |
|
| 172 | - 'label' => $displayName, |
|
| 173 | - 'uuid' => $contact['UID'], |
|
| 174 | - 'name' => $contact['FN'], |
|
| 175 | - 'type' => $emailAddressType ?? '', |
|
| 176 | - 'value' => [ |
|
| 177 | - 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 178 | - 'shareWith' => $emailAddress, |
|
| 179 | - ], |
|
| 180 | - ]; |
|
| 181 | - } else { |
|
| 182 | - $result['wide'][] = [ |
|
| 183 | - 'label' => $displayName, |
|
| 184 | - 'uuid' => $contact['UID'], |
|
| 185 | - 'name' => $contact['FN'], |
|
| 186 | - 'type' => $emailAddressType ?? '', |
|
| 187 | - 'value' => [ |
|
| 188 | - 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 189 | - 'shareWith' => $emailAddress, |
|
| 190 | - ], |
|
| 191 | - ]; |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $reachedEnd = true; |
|
| 198 | - if (!$this->shareeEnumeration) { |
|
| 199 | - $result['wide'] = []; |
|
| 200 | - $userResults['wide'] = []; |
|
| 201 | - } else { |
|
| 202 | - $reachedEnd = (count($result['wide']) < $offset + $limit) && |
|
| 203 | - (count($userResults['wide']) < $offset + $limit); |
|
| 204 | - |
|
| 205 | - $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
| 206 | - $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
|
| 211 | - $result['exact'][] = [ |
|
| 212 | - 'label' => $search, |
|
| 213 | - 'uuid' => $search, |
|
| 214 | - 'value' => [ |
|
| 215 | - 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 216 | - 'shareWith' => $search, |
|
| 217 | - ], |
|
| 218 | - ]; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if (!empty($userResults['wide'])) { |
|
| 222 | - $searchResult->addResultSet($userType, $userResults['wide'], []); |
|
| 223 | - } |
|
| 224 | - $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); |
|
| 225 | - |
|
| 226 | - return !$reachedEnd; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - public function isCurrentUser(ICloudId $cloud): bool { |
|
| 230 | - $currentUser = $this->userSession->getUser(); |
|
| 231 | - return $currentUser instanceof IUser ? $currentUser->getUID() === $cloud->getUser() : false; |
|
| 232 | - } |
|
| 108 | + $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
|
| 109 | + $found = false; |
|
| 110 | + foreach ($userGroups as $userGroup) { |
|
| 111 | + if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) { |
|
| 112 | + $found = true; |
|
| 113 | + break; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + if (!$found) { |
|
| 117 | + continue; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + if ($exactEmailMatch) { |
|
| 121 | + try { |
|
| 122 | + $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
|
| 123 | + } catch (\InvalidArgumentException $e) { |
|
| 124 | + continue; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 128 | + $singleResult = [[ |
|
| 129 | + 'label' => $displayName, |
|
| 130 | + 'uuid' => $contact['UID'], |
|
| 131 | + 'name' => $contact['FN'], |
|
| 132 | + 'value' => [ |
|
| 133 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 134 | + 'shareWith' => $cloud->getUser(), |
|
| 135 | + ], |
|
| 136 | + ]]; |
|
| 137 | + $searchResult->addResultSet($userType, [], $singleResult); |
|
| 138 | + $searchResult->markExactIdMatch($emailType); |
|
| 139 | + } |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if ($this->shareeEnumeration) { |
|
| 144 | + try { |
|
| 145 | + $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
|
| 146 | + } catch (\InvalidArgumentException $e) { |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
|
| 151 | + $userResults['wide'][] = [ |
|
| 152 | + 'label' => $displayName, |
|
| 153 | + 'uuid' => $contact['UID'], |
|
| 154 | + 'name' => $contact['FN'], |
|
| 155 | + 'value' => [ |
|
| 156 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 157 | + 'shareWith' => $cloud->getUser(), |
|
| 158 | + ], |
|
| 159 | + ]; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + continue; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ($exactEmailMatch |
|
| 166 | + || isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch) |
|
| 167 | + { |
|
| 168 | + if ($exactEmailMatch) { |
|
| 169 | + $searchResult->markExactIdMatch($emailType); |
|
| 170 | + } |
|
| 171 | + $result['exact'][] = [ |
|
| 172 | + 'label' => $displayName, |
|
| 173 | + 'uuid' => $contact['UID'], |
|
| 174 | + 'name' => $contact['FN'], |
|
| 175 | + 'type' => $emailAddressType ?? '', |
|
| 176 | + 'value' => [ |
|
| 177 | + 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 178 | + 'shareWith' => $emailAddress, |
|
| 179 | + ], |
|
| 180 | + ]; |
|
| 181 | + } else { |
|
| 182 | + $result['wide'][] = [ |
|
| 183 | + 'label' => $displayName, |
|
| 184 | + 'uuid' => $contact['UID'], |
|
| 185 | + 'name' => $contact['FN'], |
|
| 186 | + 'type' => $emailAddressType ?? '', |
|
| 187 | + 'value' => [ |
|
| 188 | + 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 189 | + 'shareWith' => $emailAddress, |
|
| 190 | + ], |
|
| 191 | + ]; |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $reachedEnd = true; |
|
| 198 | + if (!$this->shareeEnumeration) { |
|
| 199 | + $result['wide'] = []; |
|
| 200 | + $userResults['wide'] = []; |
|
| 201 | + } else { |
|
| 202 | + $reachedEnd = (count($result['wide']) < $offset + $limit) && |
|
| 203 | + (count($userResults['wide']) < $offset + $limit); |
|
| 204 | + |
|
| 205 | + $result['wide'] = array_slice($result['wide'], $offset, $limit); |
|
| 206 | + $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
|
| 211 | + $result['exact'][] = [ |
|
| 212 | + 'label' => $search, |
|
| 213 | + 'uuid' => $search, |
|
| 214 | + 'value' => [ |
|
| 215 | + 'shareType' => Share::SHARE_TYPE_EMAIL, |
|
| 216 | + 'shareWith' => $search, |
|
| 217 | + ], |
|
| 218 | + ]; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if (!empty($userResults['wide'])) { |
|
| 222 | + $searchResult->addResultSet($userType, $userResults['wide'], []); |
|
| 223 | + } |
|
| 224 | + $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); |
|
| 225 | + |
|
| 226 | + return !$reachedEnd; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + public function isCurrentUser(ICloudId $cloud): bool { |
|
| 230 | + $currentUser = $this->userSession->getUser(); |
|
| 231 | + return $currentUser instanceof IUser ? $currentUser->getUID() === $cloud->getUser() : false; |
|
| 232 | + } |
|
| 233 | 233 | } |
@@ -95,8 +95,8 @@ |
||
| 95 | 95 | $emailAddress = $emailAddressData['value']; |
| 96 | 96 | $emailAddressType = $emailAddressData['type']; |
| 97 | 97 | } |
| 98 | - if (isset($contact['FN'])) { |
|
| 99 | - $displayName = $contact['FN'] . ' (' . $emailAddress . ')'; |
|
| 98 | + if (isset($contact['FN'])) { |
|
| 99 | + $displayName = $contact['FN'].' ('.$emailAddress.')'; |
|
| 100 | 100 | } |
| 101 | 101 | $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
| 102 | 102 | |