@@ -24,7 +24,7 @@ |
||
| 24 | 24 | |
| 25 | 25 | class CalendarRoot extends \Sabre\CalDAV\CalendarRoot { |
| 26 | 26 | |
| 27 | - function getChildForPrincipal(array $principal) { |
|
| 28 | - return new CalendarHome($this->caldavBackend, $principal); |
|
| 29 | - } |
|
| 27 | + function getChildForPrincipal(array $principal) { |
|
| 28 | + return new CalendarHome($this->caldavBackend, $principal); |
|
| 29 | + } |
|
| 30 | 30 | } |
| 31 | 31 | \ No newline at end of file |
@@ -34,255 +34,255 @@ |
||
| 34 | 34 | |
| 35 | 35 | class AddressBookImpl implements IAddressBook { |
| 36 | 36 | |
| 37 | - /** @var CardDavBackend */ |
|
| 38 | - private $backend; |
|
| 39 | - |
|
| 40 | - /** @var array */ |
|
| 41 | - private $addressBookInfo; |
|
| 42 | - |
|
| 43 | - /** @var AddressBook */ |
|
| 44 | - private $addressBook; |
|
| 45 | - |
|
| 46 | - /** @var IURLGenerator */ |
|
| 47 | - private $urlGenerator; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * AddressBookImpl constructor. |
|
| 51 | - * |
|
| 52 | - * @param AddressBook $addressBook |
|
| 53 | - * @param array $addressBookInfo |
|
| 54 | - * @param CardDavBackend $backend |
|
| 55 | - * @param IUrlGenerator $urlGenerator |
|
| 56 | - */ |
|
| 57 | - public function __construct( |
|
| 58 | - AddressBook $addressBook, |
|
| 59 | - array $addressBookInfo, |
|
| 60 | - CardDavBackend $backend, |
|
| 61 | - IURLGenerator $urlGenerator) { |
|
| 62 | - |
|
| 63 | - $this->addressBook = $addressBook; |
|
| 64 | - $this->addressBookInfo = $addressBookInfo; |
|
| 65 | - $this->backend = $backend; |
|
| 66 | - $this->urlGenerator = $urlGenerator; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return string defining the technical unique key |
|
| 71 | - * @since 5.0.0 |
|
| 72 | - */ |
|
| 73 | - public function getKey() { |
|
| 74 | - return $this->addressBookInfo['id']; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 79 | - * |
|
| 80 | - * @return mixed |
|
| 81 | - * @since 5.0.0 |
|
| 82 | - */ |
|
| 83 | - public function getDisplayName() { |
|
| 84 | - return $this->addressBookInfo['{DAV:}displayname']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param string $pattern which should match within the $searchProperties |
|
| 89 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 90 | - * @param array $options - for future use. One should always have options! |
|
| 91 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 92 | - * @since 5.0.0 |
|
| 93 | - */ |
|
| 94 | - public function search($pattern, $searchProperties, $options) { |
|
| 95 | - $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 96 | - |
|
| 97 | - $vCards = []; |
|
| 98 | - foreach ($results as $result) { |
|
| 99 | - $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata'])); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return $vCards; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param array $properties this array if key-value-pairs defines a contact |
|
| 107 | - * @return array an array representing the contact just created or updated |
|
| 108 | - * @since 5.0.0 |
|
| 109 | - */ |
|
| 110 | - public function createOrUpdate($properties) { |
|
| 111 | - $update = false; |
|
| 112 | - if (!isset($properties['URI'])) { // create a new contact |
|
| 113 | - $uid = $this->createUid(); |
|
| 114 | - $uri = $uid . '.vcf'; |
|
| 115 | - $vCard = $this->createEmptyVCard($uid); |
|
| 116 | - } else { // update existing contact |
|
| 117 | - $uri = $properties['URI']; |
|
| 118 | - $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 119 | - $vCard = $this->readCard($vCardData['carddata']); |
|
| 120 | - $update = true; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - foreach ($properties as $key => $value) { |
|
| 124 | - $vCard->$key = $vCard->createProperty($key, $value); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ($update) { |
|
| 128 | - $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 129 | - } else { |
|
| 130 | - $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this->vCard2Array($uri, $vCard); |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @return mixed |
|
| 139 | - * @since 5.0.0 |
|
| 140 | - */ |
|
| 141 | - public function getPermissions() { |
|
| 142 | - $permissions = $this->addressBook->getACL(); |
|
| 143 | - $result = 0; |
|
| 144 | - foreach ($permissions as $permission) { |
|
| 145 | - switch($permission['privilege']) { |
|
| 146 | - case '{DAV:}read': |
|
| 147 | - $result |= Constants::PERMISSION_READ; |
|
| 148 | - break; |
|
| 149 | - case '{DAV:}write': |
|
| 150 | - $result |= Constants::PERMISSION_CREATE; |
|
| 151 | - $result |= Constants::PERMISSION_UPDATE; |
|
| 152 | - break; |
|
| 153 | - case '{DAV:}all': |
|
| 154 | - $result |= Constants::PERMISSION_ALL; |
|
| 155 | - break; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $result; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param object $id the unique identifier to a contact |
|
| 164 | - * @return bool successful or not |
|
| 165 | - * @since 5.0.0 |
|
| 166 | - */ |
|
| 167 | - public function delete($id) { |
|
| 168 | - $uri = $this->backend->getCardUri($id); |
|
| 169 | - return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * read vCard data into a vCard object |
|
| 174 | - * |
|
| 175 | - * @param string $cardData |
|
| 176 | - * @return VCard |
|
| 177 | - */ |
|
| 178 | - protected function readCard($cardData) { |
|
| 179 | - return Reader::read($cardData); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * create UID for contact |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - protected function createUid() { |
|
| 188 | - do { |
|
| 189 | - $uid = $this->getUid(); |
|
| 190 | - $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 191 | - } while (!empty($contact)); |
|
| 192 | - |
|
| 193 | - return $uid; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * getUid is only there for testing, use createUid instead |
|
| 198 | - */ |
|
| 199 | - protected function getUid() { |
|
| 200 | - return UUIDUtil::getUUID(); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * create empty vcard |
|
| 205 | - * |
|
| 206 | - * @param string $uid |
|
| 207 | - * @return VCard |
|
| 208 | - */ |
|
| 209 | - protected function createEmptyVCard($uid) { |
|
| 210 | - $vCard = new VCard(); |
|
| 211 | - $vCard->UID = $uid; |
|
| 212 | - return $vCard; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * create array with all vCard properties |
|
| 217 | - * |
|
| 218 | - * @param string $uri |
|
| 219 | - * @param VCard $vCard |
|
| 220 | - * @return array |
|
| 221 | - */ |
|
| 222 | - protected function vCard2Array($uri, VCard $vCard) { |
|
| 223 | - $result = [ |
|
| 224 | - 'URI' => $uri, |
|
| 225 | - ]; |
|
| 226 | - |
|
| 227 | - foreach ($vCard->children() as $property) { |
|
| 228 | - if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 229 | - $url = $this->urlGenerator->getAbsoluteURL( |
|
| 230 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 231 | - $url .= implode('/', [ |
|
| 232 | - 'addressbooks', |
|
| 233 | - substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 234 | - $this->addressBookInfo['uri'], |
|
| 235 | - $uri |
|
| 236 | - ]) . '?photo'; |
|
| 237 | - |
|
| 238 | - $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 239 | - |
|
| 240 | - } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 241 | - $type = $this->getTypeFromProperty($property); |
|
| 242 | - |
|
| 243 | - // Type is the social network, when it's empty we don't need this. |
|
| 244 | - if ($type !== null) { |
|
| 245 | - if (!isset($result[$property->name])) { |
|
| 246 | - $result[$property->name] = []; |
|
| 247 | - } |
|
| 248 | - $result[$property->name][$type] = $property->getValue(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // The following properties can be set multiple times |
|
| 252 | - } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 253 | - if (!isset($result[$property->name])) { |
|
| 254 | - $result[$property->name] = []; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - $result[$property->name][] = $property->getValue(); |
|
| 258 | - |
|
| 259 | - } else { |
|
| 260 | - $result[$property->name] = $property->getValue(); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if ($this->addressBookInfo['principaluri'] === 'principals/system/system' && |
|
| 265 | - $this->addressBookInfo['uri'] === 'system') { |
|
| 266 | - $result['isLocalSystemBook'] = true; |
|
| 267 | - } |
|
| 268 | - return $result; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Get the type of the current property |
|
| 273 | - * |
|
| 274 | - * @param Property $property |
|
| 275 | - * @return null|string |
|
| 276 | - */ |
|
| 277 | - protected function getTypeFromProperty(Property $property) { |
|
| 278 | - $parameters = $property->parameters(); |
|
| 279 | - // Type is the social network, when it's empty we don't need this. |
|
| 280 | - if (isset($parameters['TYPE'])) { |
|
| 281 | - /** @var \Sabre\VObject\Parameter $type */ |
|
| 282 | - $type = $parameters['TYPE']; |
|
| 283 | - return $type->getValue(); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return null; |
|
| 287 | - } |
|
| 37 | + /** @var CardDavBackend */ |
|
| 38 | + private $backend; |
|
| 39 | + |
|
| 40 | + /** @var array */ |
|
| 41 | + private $addressBookInfo; |
|
| 42 | + |
|
| 43 | + /** @var AddressBook */ |
|
| 44 | + private $addressBook; |
|
| 45 | + |
|
| 46 | + /** @var IURLGenerator */ |
|
| 47 | + private $urlGenerator; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * AddressBookImpl constructor. |
|
| 51 | + * |
|
| 52 | + * @param AddressBook $addressBook |
|
| 53 | + * @param array $addressBookInfo |
|
| 54 | + * @param CardDavBackend $backend |
|
| 55 | + * @param IUrlGenerator $urlGenerator |
|
| 56 | + */ |
|
| 57 | + public function __construct( |
|
| 58 | + AddressBook $addressBook, |
|
| 59 | + array $addressBookInfo, |
|
| 60 | + CardDavBackend $backend, |
|
| 61 | + IURLGenerator $urlGenerator) { |
|
| 62 | + |
|
| 63 | + $this->addressBook = $addressBook; |
|
| 64 | + $this->addressBookInfo = $addressBookInfo; |
|
| 65 | + $this->backend = $backend; |
|
| 66 | + $this->urlGenerator = $urlGenerator; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return string defining the technical unique key |
|
| 71 | + * @since 5.0.0 |
|
| 72 | + */ |
|
| 73 | + public function getKey() { |
|
| 74 | + return $this->addressBookInfo['id']; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 79 | + * |
|
| 80 | + * @return mixed |
|
| 81 | + * @since 5.0.0 |
|
| 82 | + */ |
|
| 83 | + public function getDisplayName() { |
|
| 84 | + return $this->addressBookInfo['{DAV:}displayname']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param string $pattern which should match within the $searchProperties |
|
| 89 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 90 | + * @param array $options - for future use. One should always have options! |
|
| 91 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 92 | + * @since 5.0.0 |
|
| 93 | + */ |
|
| 94 | + public function search($pattern, $searchProperties, $options) { |
|
| 95 | + $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 96 | + |
|
| 97 | + $vCards = []; |
|
| 98 | + foreach ($results as $result) { |
|
| 99 | + $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata'])); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return $vCards; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param array $properties this array if key-value-pairs defines a contact |
|
| 107 | + * @return array an array representing the contact just created or updated |
|
| 108 | + * @since 5.0.0 |
|
| 109 | + */ |
|
| 110 | + public function createOrUpdate($properties) { |
|
| 111 | + $update = false; |
|
| 112 | + if (!isset($properties['URI'])) { // create a new contact |
|
| 113 | + $uid = $this->createUid(); |
|
| 114 | + $uri = $uid . '.vcf'; |
|
| 115 | + $vCard = $this->createEmptyVCard($uid); |
|
| 116 | + } else { // update existing contact |
|
| 117 | + $uri = $properties['URI']; |
|
| 118 | + $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 119 | + $vCard = $this->readCard($vCardData['carddata']); |
|
| 120 | + $update = true; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + foreach ($properties as $key => $value) { |
|
| 124 | + $vCard->$key = $vCard->createProperty($key, $value); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ($update) { |
|
| 128 | + $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 129 | + } else { |
|
| 130 | + $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this->vCard2Array($uri, $vCard); |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @return mixed |
|
| 139 | + * @since 5.0.0 |
|
| 140 | + */ |
|
| 141 | + public function getPermissions() { |
|
| 142 | + $permissions = $this->addressBook->getACL(); |
|
| 143 | + $result = 0; |
|
| 144 | + foreach ($permissions as $permission) { |
|
| 145 | + switch($permission['privilege']) { |
|
| 146 | + case '{DAV:}read': |
|
| 147 | + $result |= Constants::PERMISSION_READ; |
|
| 148 | + break; |
|
| 149 | + case '{DAV:}write': |
|
| 150 | + $result |= Constants::PERMISSION_CREATE; |
|
| 151 | + $result |= Constants::PERMISSION_UPDATE; |
|
| 152 | + break; |
|
| 153 | + case '{DAV:}all': |
|
| 154 | + $result |= Constants::PERMISSION_ALL; |
|
| 155 | + break; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $result; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param object $id the unique identifier to a contact |
|
| 164 | + * @return bool successful or not |
|
| 165 | + * @since 5.0.0 |
|
| 166 | + */ |
|
| 167 | + public function delete($id) { |
|
| 168 | + $uri = $this->backend->getCardUri($id); |
|
| 169 | + return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * read vCard data into a vCard object |
|
| 174 | + * |
|
| 175 | + * @param string $cardData |
|
| 176 | + * @return VCard |
|
| 177 | + */ |
|
| 178 | + protected function readCard($cardData) { |
|
| 179 | + return Reader::read($cardData); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * create UID for contact |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + protected function createUid() { |
|
| 188 | + do { |
|
| 189 | + $uid = $this->getUid(); |
|
| 190 | + $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 191 | + } while (!empty($contact)); |
|
| 192 | + |
|
| 193 | + return $uid; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * getUid is only there for testing, use createUid instead |
|
| 198 | + */ |
|
| 199 | + protected function getUid() { |
|
| 200 | + return UUIDUtil::getUUID(); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * create empty vcard |
|
| 205 | + * |
|
| 206 | + * @param string $uid |
|
| 207 | + * @return VCard |
|
| 208 | + */ |
|
| 209 | + protected function createEmptyVCard($uid) { |
|
| 210 | + $vCard = new VCard(); |
|
| 211 | + $vCard->UID = $uid; |
|
| 212 | + return $vCard; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * create array with all vCard properties |
|
| 217 | + * |
|
| 218 | + * @param string $uri |
|
| 219 | + * @param VCard $vCard |
|
| 220 | + * @return array |
|
| 221 | + */ |
|
| 222 | + protected function vCard2Array($uri, VCard $vCard) { |
|
| 223 | + $result = [ |
|
| 224 | + 'URI' => $uri, |
|
| 225 | + ]; |
|
| 226 | + |
|
| 227 | + foreach ($vCard->children() as $property) { |
|
| 228 | + if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 229 | + $url = $this->urlGenerator->getAbsoluteURL( |
|
| 230 | + $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 231 | + $url .= implode('/', [ |
|
| 232 | + 'addressbooks', |
|
| 233 | + substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 234 | + $this->addressBookInfo['uri'], |
|
| 235 | + $uri |
|
| 236 | + ]) . '?photo'; |
|
| 237 | + |
|
| 238 | + $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 239 | + |
|
| 240 | + } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 241 | + $type = $this->getTypeFromProperty($property); |
|
| 242 | + |
|
| 243 | + // Type is the social network, when it's empty we don't need this. |
|
| 244 | + if ($type !== null) { |
|
| 245 | + if (!isset($result[$property->name])) { |
|
| 246 | + $result[$property->name] = []; |
|
| 247 | + } |
|
| 248 | + $result[$property->name][$type] = $property->getValue(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // The following properties can be set multiple times |
|
| 252 | + } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 253 | + if (!isset($result[$property->name])) { |
|
| 254 | + $result[$property->name] = []; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + $result[$property->name][] = $property->getValue(); |
|
| 258 | + |
|
| 259 | + } else { |
|
| 260 | + $result[$property->name] = $property->getValue(); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if ($this->addressBookInfo['principaluri'] === 'principals/system/system' && |
|
| 265 | + $this->addressBookInfo['uri'] === 'system') { |
|
| 266 | + $result['isLocalSystemBook'] = true; |
|
| 267 | + } |
|
| 268 | + return $result; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Get the type of the current property |
|
| 273 | + * |
|
| 274 | + * @param Property $property |
|
| 275 | + * @return null|string |
|
| 276 | + */ |
|
| 277 | + protected function getTypeFromProperty(Property $property) { |
|
| 278 | + $parameters = $property->parameters(); |
|
| 279 | + // Type is the social network, when it's empty we don't need this. |
|
| 280 | + if (isset($parameters['TYPE'])) { |
|
| 281 | + /** @var \Sabre\VObject\Parameter $type */ |
|
| 282 | + $type = $parameters['TYPE']; |
|
| 283 | + return $type->getValue(); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return null; |
|
| 287 | + } |
|
| 288 | 288 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $update = false; |
| 112 | 112 | if (!isset($properties['URI'])) { // create a new contact |
| 113 | 113 | $uid = $this->createUid(); |
| 114 | - $uri = $uid . '.vcf'; |
|
| 114 | + $uri = $uid.'.vcf'; |
|
| 115 | 115 | $vCard = $this->createEmptyVCard($uid); |
| 116 | 116 | } else { // update existing contact |
| 117 | 117 | $uri = $properties['URI']; |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | $permissions = $this->addressBook->getACL(); |
| 143 | 143 | $result = 0; |
| 144 | 144 | foreach ($permissions as $permission) { |
| 145 | - switch($permission['privilege']) { |
|
| 145 | + switch ($permission['privilege']) { |
|
| 146 | 146 | case '{DAV:}read': |
| 147 | 147 | $result |= Constants::PERMISSION_READ; |
| 148 | 148 | break; |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | protected function createUid() { |
| 188 | 188 | do { |
| 189 | 189 | $uid = $this->getUid(); |
| 190 | - $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 190 | + $contact = $this->backend->getContact($this->getKey(), $uid.'.vcf'); |
|
| 191 | 191 | } while (!empty($contact)); |
| 192 | 192 | |
| 193 | 193 | return $uid; |
@@ -227,15 +227,15 @@ discard block |
||
| 227 | 227 | foreach ($vCard->children() as $property) { |
| 228 | 228 | if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
| 229 | 229 | $url = $this->urlGenerator->getAbsoluteURL( |
| 230 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 230 | + $this->urlGenerator->linkTo('', 'remote.php').'/dav/'); |
|
| 231 | 231 | $url .= implode('/', [ |
| 232 | 232 | 'addressbooks', |
| 233 | 233 | substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
| 234 | 234 | $this->addressBookInfo['uri'], |
| 235 | 235 | $uri |
| 236 | - ]) . '?photo'; |
|
| 236 | + ]).'?photo'; |
|
| 237 | 237 | |
| 238 | - $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 238 | + $result['PHOTO'] = 'VALUE=uri:'.$url; |
|
| 239 | 239 | |
| 240 | 240 | } else if ($property->name === 'X-SOCIALPROFILE') { |
| 241 | 241 | $type = $this->getTypeFromProperty($property); |
@@ -26,21 +26,21 @@ |
||
| 26 | 26 | use Sabre\Xml\Writer; |
| 27 | 27 | |
| 28 | 28 | class Groups implements XmlSerializable { |
| 29 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 29 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 30 | 30 | |
| 31 | - /** @var string[] of TYPE:CHECKSUM */ |
|
| 32 | - private $groups; |
|
| 31 | + /** @var string[] of TYPE:CHECKSUM */ |
|
| 32 | + private $groups; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param string $groups |
|
| 36 | - */ |
|
| 37 | - public function __construct($groups) { |
|
| 38 | - $this->groups = $groups; |
|
| 39 | - } |
|
| 34 | + /** |
|
| 35 | + * @param string $groups |
|
| 36 | + */ |
|
| 37 | + public function __construct($groups) { |
|
| 38 | + $this->groups = $groups; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - function xmlSerialize(Writer $writer) { |
|
| 42 | - foreach ($this->groups as $group) { |
|
| 43 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 44 | - } |
|
| 45 | - } |
|
| 41 | + function xmlSerialize(Writer $writer) { |
|
| 42 | + foreach ($this->groups as $group) { |
|
| 43 | + $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | |
| 41 | 41 | function xmlSerialize(Writer $writer) { |
| 42 | 42 | foreach ($this->groups as $group) { |
| 43 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 43 | + $writer->writeElement('{'.self::NS_OWNCLOUD.'}group', $group); |
|
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -745,7 +745,9 @@ |
||
| 745 | 745 | $stmt->execute([ $addressBookId ]); |
| 746 | 746 | $currentToken = $stmt->fetchColumn(0); |
| 747 | 747 | |
| 748 | - if (is_null($currentToken)) return null; |
|
| 748 | + if (is_null($currentToken)) { |
|
| 749 | + return null; |
|
| 750 | + } |
|
| 749 | 751 | |
| 750 | 752 | $result = [ |
| 751 | 753 | 'syncToken' => $currentToken, |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | /** |
| 105 | 105 | * Return the number of address books for a principal |
| 106 | 106 | * |
| 107 | - * @param $principalUri |
|
| 107 | + * @param string $principalUri |
|
| 108 | 108 | * @return int |
| 109 | 109 | */ |
| 110 | 110 | public function getAddressBooksForUserCount($principalUri) { |
@@ -219,6 +219,9 @@ discard block |
||
| 219 | 219 | return array_values($addressBooks); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | + /** |
|
| 223 | + * @param string $principalUri |
|
| 224 | + */ |
|
| 222 | 225 | public function getUsersOwnAddressBooks($principalUri) { |
| 223 | 226 | $principalUri = $this->convertPrincipal($principalUri, true); |
| 224 | 227 | $query = $this->db->getQueryBuilder(); |
@@ -293,7 +296,8 @@ discard block |
||
| 293 | 296 | } |
| 294 | 297 | |
| 295 | 298 | /** |
| 296 | - * @param $addressBookUri |
|
| 299 | + * @param string $addressBookUri |
|
| 300 | + * @param string $principal |
|
| 297 | 301 | * @return array|null |
| 298 | 302 | */ |
| 299 | 303 | public function getAddressBooksByUri($principal, $addressBookUri) { |
@@ -982,6 +986,7 @@ discard block |
||
| 982 | 986 | * * readOnly - boolean |
| 983 | 987 | * * summary - Optional, a description for the share |
| 984 | 988 | * |
| 989 | + * @param integer $addressBookId |
|
| 985 | 990 | * @return array |
| 986 | 991 | */ |
| 987 | 992 | public function getShares($addressBookId) { |
@@ -1081,7 +1086,7 @@ discard block |
||
| 1081 | 1086 | |
| 1082 | 1087 | /** |
| 1083 | 1088 | * For shared address books the sharee is set in the ACL of the address book |
| 1084 | - * @param $addressBookId |
|
| 1089 | + * @param integer $addressBookId |
|
| 1085 | 1090 | * @param $acl |
| 1086 | 1091 | * @return array |
| 1087 | 1092 | */ |
@@ -1089,6 +1094,9 @@ discard block |
||
| 1089 | 1094 | return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
| 1090 | 1095 | } |
| 1091 | 1096 | |
| 1097 | + /** |
|
| 1098 | + * @param boolean $toV2 |
|
| 1099 | + */ |
|
| 1092 | 1100 | private function convertPrincipal($principalUri, $toV2) { |
| 1093 | 1101 | if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
| 1094 | 1102 | list(, $name) = \Sabre\Uri\split($principalUri); |
@@ -40,7 +40,6 @@ |
||
| 40 | 40 | use Sabre\CardDAV\Backend\SyncSupport; |
| 41 | 41 | use Sabre\CardDAV\Plugin; |
| 42 | 42 | use Sabre\DAV\Exception\BadRequest; |
| 43 | -use Sabre\HTTP\URLUtil; |
|
| 44 | 43 | use Sabre\VObject\Component\VCard; |
| 45 | 44 | use Sabre\VObject\Reader; |
| 46 | 45 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | ->from('addressbooks') |
| 115 | 115 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
| 116 | 116 | |
| 117 | - return (int)$query->execute()->fetchColumn(); |
|
| 117 | + return (int) $query->execute()->fetchColumn(); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -145,15 +145,15 @@ discard block |
||
| 145 | 145 | $addressBooks = []; |
| 146 | 146 | |
| 147 | 147 | $result = $query->execute(); |
| 148 | - while($row = $result->fetch()) { |
|
| 148 | + while ($row = $result->fetch()) { |
|
| 149 | 149 | $addressBooks[$row['id']] = [ |
| 150 | 150 | 'id' => $row['id'], |
| 151 | 151 | 'uri' => $row['uri'], |
| 152 | 152 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 153 | 153 | '{DAV:}displayname' => $row['displayname'], |
| 154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 154 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 155 | 155 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 157 | 157 | ]; |
| 158 | 158 | |
| 159 | 159 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | $principals = array_map(function($principal) { |
| 166 | 166 | return urldecode($principal); |
| 167 | 167 | }, $principals); |
| 168 | - $principals[]= $principalUri; |
|
| 168 | + $principals[] = $principalUri; |
|
| 169 | 169 | |
| 170 | 170 | $query = $this->db->getQueryBuilder(); |
| 171 | 171 | $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
@@ -177,8 +177,8 @@ discard block |
||
| 177 | 177 | ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
| 178 | 178 | ->execute(); |
| 179 | 179 | |
| 180 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 181 | - while($row = $result->fetch()) { |
|
| 180 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
| 181 | + while ($row = $result->fetch()) { |
|
| 182 | 182 | if ($row['principaluri'] === $principalUri) { |
| 183 | 183 | continue; |
| 184 | 184 | } |
@@ -197,18 +197,18 @@ discard block |
||
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | list(, $name) = \Sabre\Uri\split($row['principaluri']); |
| 200 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 201 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 200 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
| 201 | + $displayName = $row['displayname'].' ('.$this->getUserDisplayName($name).')'; |
|
| 202 | 202 | |
| 203 | 203 | $addressBooks[$row['id']] = [ |
| 204 | 204 | 'id' => $row['id'], |
| 205 | 205 | 'uri' => $uri, |
| 206 | 206 | 'principaluri' => $principalUriOriginal, |
| 207 | 207 | '{DAV:}displayname' => $displayName, |
| 208 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 208 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 209 | 209 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 210 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 211 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 210 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 211 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $row['principaluri'], |
|
| 212 | 212 | $readOnlyPropertyName => $readOnly, |
| 213 | 213 | ]; |
| 214 | 214 | |
@@ -229,15 +229,15 @@ discard block |
||
| 229 | 229 | $addressBooks = []; |
| 230 | 230 | |
| 231 | 231 | $result = $query->execute(); |
| 232 | - while($row = $result->fetch()) { |
|
| 232 | + while ($row = $result->fetch()) { |
|
| 233 | 233 | $addressBooks[$row['id']] = [ |
| 234 | 234 | 'id' => $row['id'], |
| 235 | 235 | 'uri' => $row['uri'], |
| 236 | 236 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 237 | 237 | '{DAV:}displayname' => $row['displayname'], |
| 238 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 238 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 239 | 239 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 240 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 240 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 241 | 241 | ]; |
| 242 | 242 | |
| 243 | 243 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | 'uri' => $row['uri'], |
| 283 | 283 | 'principaluri' => $row['principaluri'], |
| 284 | 284 | '{DAV:}displayname' => $row['displayname'], |
| 285 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 285 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 286 | 286 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 287 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 287 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 288 | 288 | ]; |
| 289 | 289 | |
| 290 | 290 | $this->addOwnerPrincipal($addressBook); |
@@ -316,9 +316,9 @@ discard block |
||
| 316 | 316 | 'uri' => $row['uri'], |
| 317 | 317 | 'principaluri' => $row['principaluri'], |
| 318 | 318 | '{DAV:}displayname' => $row['displayname'], |
| 319 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 319 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 320 | 320 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 321 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 321 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 322 | 322 | ]; |
| 323 | 323 | |
| 324 | 324 | $this->addOwnerPrincipal($addressBook); |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
| 346 | 346 | $supportedProperties = [ |
| 347 | 347 | '{DAV:}displayname', |
| 348 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 348 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description', |
|
| 349 | 349 | ]; |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -354,13 +354,13 @@ discard block |
||
| 354 | 354 | $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
| 355 | 355 | |
| 356 | 356 | $updates = []; |
| 357 | - foreach($mutations as $property=>$newValue) { |
|
| 357 | + foreach ($mutations as $property=>$newValue) { |
|
| 358 | 358 | |
| 359 | - switch($property) { |
|
| 359 | + switch ($property) { |
|
| 360 | 360 | case '{DAV:}displayname' : |
| 361 | 361 | $updates['displayname'] = $newValue; |
| 362 | 362 | break; |
| 363 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 363 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 364 | 364 | $updates['description'] = $newValue; |
| 365 | 365 | break; |
| 366 | 366 | } |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | $query = $this->db->getQueryBuilder(); |
| 369 | 369 | $query->update('addressbooks'); |
| 370 | 370 | |
| 371 | - foreach($updates as $key=>$value) { |
|
| 371 | + foreach ($updates as $key=>$value) { |
|
| 372 | 372 | $query->set($key, $query->createNamedParameter($value)); |
| 373 | 373 | } |
| 374 | 374 | $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
@@ -399,24 +399,24 @@ discard block |
||
| 399 | 399 | 'synctoken' => 1 |
| 400 | 400 | ]; |
| 401 | 401 | |
| 402 | - foreach($properties as $property=>$newValue) { |
|
| 402 | + foreach ($properties as $property=>$newValue) { |
|
| 403 | 403 | |
| 404 | - switch($property) { |
|
| 404 | + switch ($property) { |
|
| 405 | 405 | case '{DAV:}displayname' : |
| 406 | 406 | $values['displayname'] = $newValue; |
| 407 | 407 | break; |
| 408 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 408 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 409 | 409 | $values['description'] = $newValue; |
| 410 | 410 | break; |
| 411 | 411 | default : |
| 412 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 412 | + throw new BadRequest('Unknown property: '.$property); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | // Fallback to make sure the displayname is set. Some clients may refuse |
| 418 | 418 | // to work with addressbooks not having a displayname. |
| 419 | - if(is_null($values['displayname'])) { |
|
| 419 | + if (is_null($values['displayname'])) { |
|
| 420 | 420 | $values['displayname'] = $url; |
| 421 | 421 | } |
| 422 | 422 | |
@@ -494,8 +494,8 @@ discard block |
||
| 494 | 494 | $cards = []; |
| 495 | 495 | |
| 496 | 496 | $result = $query->execute(); |
| 497 | - while($row = $result->fetch()) { |
|
| 498 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 497 | + while ($row = $result->fetch()) { |
|
| 498 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 499 | 499 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 500 | 500 | $cards[] = $row; |
| 501 | 501 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | if (!$row) { |
| 530 | 530 | return false; |
| 531 | 531 | } |
| 532 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 532 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 533 | 533 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 534 | 534 | |
| 535 | 535 | return $row; |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | $result = $query->execute(); |
| 567 | 567 | |
| 568 | 568 | while ($row = $result->fetch()) { |
| 569 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 569 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 570 | 570 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 571 | 571 | $cards[] = $row; |
| 572 | 572 | } |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | 'cardUri' => $cardUri, |
| 625 | 625 | 'cardData' => $cardData])); |
| 626 | 626 | |
| 627 | - return '"' . $etag . '"'; |
|
| 627 | + return '"'.$etag.'"'; |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -674,7 +674,7 @@ discard block |
||
| 674 | 674 | 'cardUri' => $cardUri, |
| 675 | 675 | 'cardData' => $cardData])); |
| 676 | 676 | |
| 677 | - return '"' . $etag . '"'; |
|
| 677 | + return '"'.$etag.'"'; |
|
| 678 | 678 | } |
| 679 | 679 | |
| 680 | 680 | /** |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
| 773 | 773 | // Current synctoken |
| 774 | 774 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
| 775 | - $stmt->execute([ $addressBookId ]); |
|
| 775 | + $stmt->execute([$addressBookId]); |
|
| 776 | 776 | $currentToken = $stmt->fetchColumn(0); |
| 777 | 777 | |
| 778 | 778 | if (is_null($currentToken)) return null; |
@@ -787,8 +787,8 @@ discard block |
||
| 787 | 787 | if ($syncToken) { |
| 788 | 788 | |
| 789 | 789 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
| 790 | - if ($limit>0) { |
|
| 791 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 790 | + if ($limit > 0) { |
|
| 791 | + $query .= " `LIMIT` ".(int) $limit; |
|
| 792 | 792 | } |
| 793 | 793 | |
| 794 | 794 | // Fetching all changes |
@@ -799,15 +799,15 @@ discard block |
||
| 799 | 799 | |
| 800 | 800 | // This loop ensures that any duplicates are overwritten, only the |
| 801 | 801 | // last change on a node is relevant. |
| 802 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 802 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 803 | 803 | |
| 804 | 804 | $changes[$row['uri']] = $row['operation']; |
| 805 | 805 | |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | - foreach($changes as $uri => $operation) { |
|
| 808 | + foreach ($changes as $uri => $operation) { |
|
| 809 | 809 | |
| 810 | - switch($operation) { |
|
| 810 | + switch ($operation) { |
|
| 811 | 811 | case 1: |
| 812 | 812 | $result['added'][] = $uri; |
| 813 | 813 | break; |
@@ -893,7 +893,7 @@ discard block |
||
| 893 | 893 | |
| 894 | 894 | // No need for like when the pattern is empty |
| 895 | 895 | if ('' !== $pattern) { |
| 896 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 896 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%'))); |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
@@ -946,7 +946,7 @@ discard block |
||
| 946 | 946 | $result->closeCursor(); |
| 947 | 947 | |
| 948 | 948 | if (!isset($uri['uri'])) { |
| 949 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 949 | + throw new \InvalidArgumentException('Card does not exists: '.$id); |
|
| 950 | 950 | } |
| 951 | 951 | |
| 952 | 952 | return $uri['uri']; |
@@ -1018,11 +1018,11 @@ discard block |
||
| 1018 | 1018 | ); |
| 1019 | 1019 | |
| 1020 | 1020 | foreach ($vCard->children() as $property) { |
| 1021 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1021 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1022 | 1022 | continue; |
| 1023 | 1023 | } |
| 1024 | 1024 | $preferred = 0; |
| 1025 | - foreach($property->parameters as $parameter) { |
|
| 1025 | + foreach ($property->parameters as $parameter) { |
|
| 1026 | 1026 | if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
| 1027 | 1027 | $preferred = 1; |
| 1028 | 1028 | break; |
@@ -1077,10 +1077,10 @@ discard block |
||
| 1077 | 1077 | $result->closeCursor(); |
| 1078 | 1078 | |
| 1079 | 1079 | if (!isset($cardIds['id'])) { |
| 1080 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1080 | + throw new \InvalidArgumentException('Card does not exists: '.$uri); |
|
| 1081 | 1081 | } |
| 1082 | 1082 | |
| 1083 | - return (int)$cardIds['id']; |
|
| 1083 | + return (int) $cardIds['id']; |
|
| 1084 | 1084 | } |
| 1085 | 1085 | |
| 1086 | 1086 | /** |
@@ -1105,8 +1105,8 @@ discard block |
||
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | private function addOwnerPrincipal(&$addressbookInfo) { |
| 1108 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1109 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1108 | + $ownerPrincipalKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal'; |
|
| 1109 | + $displaynameKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}owner-displayname'; |
|
| 1110 | 1110 | if (isset($addressbookInfo[$ownerPrincipalKey])) { |
| 1111 | 1111 | $uri = $addressbookInfo[$ownerPrincipalKey]; |
| 1112 | 1112 | } else { |
@@ -49,1076 +49,1076 @@ |
||
| 49 | 49 | |
| 50 | 50 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 51 | 51 | |
| 52 | - const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 53 | - const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 54 | - |
|
| 55 | - /** @var Principal */ |
|
| 56 | - private $principalBackend; |
|
| 57 | - |
|
| 58 | - /** @var string */ |
|
| 59 | - private $dbCardsTable = 'cards'; |
|
| 60 | - |
|
| 61 | - /** @var string */ |
|
| 62 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 63 | - |
|
| 64 | - /** @var IDBConnection */ |
|
| 65 | - private $db; |
|
| 66 | - |
|
| 67 | - /** @var Backend */ |
|
| 68 | - private $sharingBackend; |
|
| 69 | - |
|
| 70 | - /** @var array properties to index */ |
|
| 71 | - public static $indexProperties = array( |
|
| 72 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 73 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var string[] Map of uid => display name |
|
| 77 | - */ |
|
| 78 | - protected $userDisplayNames; |
|
| 79 | - |
|
| 80 | - /** @var IUserManager */ |
|
| 81 | - private $userManager; |
|
| 82 | - |
|
| 83 | - /** @var EventDispatcherInterface */ |
|
| 84 | - private $dispatcher; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * CardDavBackend constructor. |
|
| 88 | - * |
|
| 89 | - * @param IDBConnection $db |
|
| 90 | - * @param Principal $principalBackend |
|
| 91 | - * @param IUserManager $userManager |
|
| 92 | - * @param IGroupManager $groupManager |
|
| 93 | - * @param EventDispatcherInterface $dispatcher |
|
| 94 | - */ |
|
| 95 | - public function __construct(IDBConnection $db, |
|
| 96 | - Principal $principalBackend, |
|
| 97 | - IUserManager $userManager, |
|
| 98 | - IGroupManager $groupManager, |
|
| 99 | - EventDispatcherInterface $dispatcher) { |
|
| 100 | - $this->db = $db; |
|
| 101 | - $this->principalBackend = $principalBackend; |
|
| 102 | - $this->userManager = $userManager; |
|
| 103 | - $this->dispatcher = $dispatcher; |
|
| 104 | - $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Return the number of address books for a principal |
|
| 109 | - * |
|
| 110 | - * @param $principalUri |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 114 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 115 | - $query = $this->db->getQueryBuilder(); |
|
| 116 | - $query->select($query->createFunction('COUNT(*)')) |
|
| 117 | - ->from('addressbooks') |
|
| 118 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 119 | - |
|
| 120 | - return (int)$query->execute()->fetchColumn(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Returns the list of address books for a specific user. |
|
| 125 | - * |
|
| 126 | - * Every addressbook should have the following properties: |
|
| 127 | - * id - an arbitrary unique id |
|
| 128 | - * uri - the 'basename' part of the url |
|
| 129 | - * principaluri - Same as the passed parameter |
|
| 130 | - * |
|
| 131 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 132 | - * common ones are : |
|
| 133 | - * {DAV:}displayname |
|
| 134 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 135 | - * {http://calendarserver.org/ns/}getctag |
|
| 136 | - * |
|
| 137 | - * @param string $principalUri |
|
| 138 | - * @return array |
|
| 139 | - */ |
|
| 140 | - function getAddressBooksForUser($principalUri) { |
|
| 141 | - $principalUriOriginal = $principalUri; |
|
| 142 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 143 | - $query = $this->db->getQueryBuilder(); |
|
| 144 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 145 | - ->from('addressbooks') |
|
| 146 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 147 | - |
|
| 148 | - $addressBooks = []; |
|
| 149 | - |
|
| 150 | - $result = $query->execute(); |
|
| 151 | - while($row = $result->fetch()) { |
|
| 152 | - $addressBooks[$row['id']] = [ |
|
| 153 | - 'id' => $row['id'], |
|
| 154 | - 'uri' => $row['uri'], |
|
| 155 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 156 | - '{DAV:}displayname' => $row['displayname'], |
|
| 157 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 158 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 159 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 160 | - ]; |
|
| 161 | - |
|
| 162 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 163 | - } |
|
| 164 | - $result->closeCursor(); |
|
| 165 | - |
|
| 166 | - // query for shared calendars |
|
| 167 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 168 | - $principals = array_map(function($principal) { |
|
| 169 | - return urldecode($principal); |
|
| 170 | - }, $principals); |
|
| 171 | - $principals[]= $principalUri; |
|
| 172 | - |
|
| 173 | - $query = $this->db->getQueryBuilder(); |
|
| 174 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 175 | - ->from('dav_shares', 's') |
|
| 176 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 177 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 178 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 179 | - ->setParameter('type', 'addressbook') |
|
| 180 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 181 | - ->execute(); |
|
| 182 | - |
|
| 183 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 184 | - while($row = $result->fetch()) { |
|
| 185 | - if ($row['principaluri'] === $principalUri) { |
|
| 186 | - continue; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 190 | - if (isset($addressBooks[$row['id']])) { |
|
| 191 | - if ($readOnly) { |
|
| 192 | - // New share can not have more permissions then the old one. |
|
| 193 | - continue; |
|
| 194 | - } |
|
| 195 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 196 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 197 | - // Old share is already read-write, no more permissions can be gained |
|
| 198 | - continue; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - list(, $name) = \Sabre\Uri\split($row['principaluri']); |
|
| 203 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 204 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 205 | - |
|
| 206 | - $addressBooks[$row['id']] = [ |
|
| 207 | - 'id' => $row['id'], |
|
| 208 | - 'uri' => $uri, |
|
| 209 | - 'principaluri' => $principalUriOriginal, |
|
| 210 | - '{DAV:}displayname' => $displayName, |
|
| 211 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 212 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 213 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 214 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 215 | - $readOnlyPropertyName => $readOnly, |
|
| 216 | - ]; |
|
| 217 | - |
|
| 218 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 219 | - } |
|
| 220 | - $result->closeCursor(); |
|
| 221 | - |
|
| 222 | - return array_values($addressBooks); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 226 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 227 | - $query = $this->db->getQueryBuilder(); |
|
| 228 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 229 | - ->from('addressbooks') |
|
| 230 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 231 | - |
|
| 232 | - $addressBooks = []; |
|
| 233 | - |
|
| 234 | - $result = $query->execute(); |
|
| 235 | - while($row = $result->fetch()) { |
|
| 236 | - $addressBooks[$row['id']] = [ |
|
| 237 | - 'id' => $row['id'], |
|
| 238 | - 'uri' => $row['uri'], |
|
| 239 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 240 | - '{DAV:}displayname' => $row['displayname'], |
|
| 241 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 242 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 243 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 244 | - ]; |
|
| 245 | - |
|
| 246 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 247 | - } |
|
| 248 | - $result->closeCursor(); |
|
| 249 | - |
|
| 250 | - return array_values($addressBooks); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - private function getUserDisplayName($uid) { |
|
| 254 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 255 | - $user = $this->userManager->get($uid); |
|
| 256 | - |
|
| 257 | - if ($user instanceof IUser) { |
|
| 258 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 259 | - } else { |
|
| 260 | - $this->userDisplayNames[$uid] = $uid; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return $this->userDisplayNames[$uid]; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @param int $addressBookId |
|
| 269 | - */ |
|
| 270 | - public function getAddressBookById($addressBookId) { |
|
| 271 | - $query = $this->db->getQueryBuilder(); |
|
| 272 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 273 | - ->from('addressbooks') |
|
| 274 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 275 | - ->execute(); |
|
| 276 | - |
|
| 277 | - $row = $result->fetch(); |
|
| 278 | - $result->closeCursor(); |
|
| 279 | - if ($row === false) { |
|
| 280 | - return null; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $addressBook = [ |
|
| 284 | - 'id' => $row['id'], |
|
| 285 | - 'uri' => $row['uri'], |
|
| 286 | - 'principaluri' => $row['principaluri'], |
|
| 287 | - '{DAV:}displayname' => $row['displayname'], |
|
| 288 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 289 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 290 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 291 | - ]; |
|
| 292 | - |
|
| 293 | - $this->addOwnerPrincipal($addressBook); |
|
| 294 | - |
|
| 295 | - return $addressBook; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * @param $addressBookUri |
|
| 300 | - * @return array|null |
|
| 301 | - */ |
|
| 302 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 303 | - $query = $this->db->getQueryBuilder(); |
|
| 304 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 305 | - ->from('addressbooks') |
|
| 306 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 307 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 308 | - ->setMaxResults(1) |
|
| 309 | - ->execute(); |
|
| 310 | - |
|
| 311 | - $row = $result->fetch(); |
|
| 312 | - $result->closeCursor(); |
|
| 313 | - if ($row === false) { |
|
| 314 | - return null; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $addressBook = [ |
|
| 318 | - 'id' => $row['id'], |
|
| 319 | - 'uri' => $row['uri'], |
|
| 320 | - 'principaluri' => $row['principaluri'], |
|
| 321 | - '{DAV:}displayname' => $row['displayname'], |
|
| 322 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 323 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 324 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 325 | - ]; |
|
| 326 | - |
|
| 327 | - $this->addOwnerPrincipal($addressBook); |
|
| 328 | - |
|
| 329 | - return $addressBook; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Updates properties for an address book. |
|
| 334 | - * |
|
| 335 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 336 | - * To do the actual updates, you must tell this object which properties |
|
| 337 | - * you're going to process with the handle() method. |
|
| 338 | - * |
|
| 339 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 340 | - * promise I can handle updating this property". |
|
| 341 | - * |
|
| 342 | - * Read the PropPatch documentation for more info and examples. |
|
| 343 | - * |
|
| 344 | - * @param string $addressBookId |
|
| 345 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 346 | - * @return void |
|
| 347 | - */ |
|
| 348 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 349 | - $supportedProperties = [ |
|
| 350 | - '{DAV:}displayname', |
|
| 351 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 352 | - ]; |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * @suppress SqlInjectionChecker |
|
| 356 | - */ |
|
| 357 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 358 | - |
|
| 359 | - $updates = []; |
|
| 360 | - foreach($mutations as $property=>$newValue) { |
|
| 361 | - |
|
| 362 | - switch($property) { |
|
| 363 | - case '{DAV:}displayname' : |
|
| 364 | - $updates['displayname'] = $newValue; |
|
| 365 | - break; |
|
| 366 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 367 | - $updates['description'] = $newValue; |
|
| 368 | - break; |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - $query = $this->db->getQueryBuilder(); |
|
| 372 | - $query->update('addressbooks'); |
|
| 373 | - |
|
| 374 | - foreach($updates as $key=>$value) { |
|
| 375 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 376 | - } |
|
| 377 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 378 | - ->execute(); |
|
| 379 | - |
|
| 380 | - $this->addChange($addressBookId, "", 2); |
|
| 381 | - |
|
| 382 | - return true; |
|
| 383 | - |
|
| 384 | - }); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Creates a new address book |
|
| 389 | - * |
|
| 390 | - * @param string $principalUri |
|
| 391 | - * @param string $url Just the 'basename' of the url. |
|
| 392 | - * @param array $properties |
|
| 393 | - * @return int |
|
| 394 | - * @throws BadRequest |
|
| 395 | - */ |
|
| 396 | - function createAddressBook($principalUri, $url, array $properties) { |
|
| 397 | - $values = [ |
|
| 398 | - 'displayname' => null, |
|
| 399 | - 'description' => null, |
|
| 400 | - 'principaluri' => $principalUri, |
|
| 401 | - 'uri' => $url, |
|
| 402 | - 'synctoken' => 1 |
|
| 403 | - ]; |
|
| 404 | - |
|
| 405 | - foreach($properties as $property=>$newValue) { |
|
| 406 | - |
|
| 407 | - switch($property) { |
|
| 408 | - case '{DAV:}displayname' : |
|
| 409 | - $values['displayname'] = $newValue; |
|
| 410 | - break; |
|
| 411 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 412 | - $values['description'] = $newValue; |
|
| 413 | - break; |
|
| 414 | - default : |
|
| 415 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 421 | - // to work with addressbooks not having a displayname. |
|
| 422 | - if(is_null($values['displayname'])) { |
|
| 423 | - $values['displayname'] = $url; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - $query = $this->db->getQueryBuilder(); |
|
| 427 | - $query->insert('addressbooks') |
|
| 428 | - ->values([ |
|
| 429 | - 'uri' => $query->createParameter('uri'), |
|
| 430 | - 'displayname' => $query->createParameter('displayname'), |
|
| 431 | - 'description' => $query->createParameter('description'), |
|
| 432 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 433 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 434 | - ]) |
|
| 435 | - ->setParameters($values) |
|
| 436 | - ->execute(); |
|
| 437 | - |
|
| 438 | - return $query->getLastInsertId(); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Deletes an entire addressbook and all its contents |
|
| 443 | - * |
|
| 444 | - * @param mixed $addressBookId |
|
| 445 | - * @return void |
|
| 446 | - */ |
|
| 447 | - function deleteAddressBook($addressBookId) { |
|
| 448 | - $query = $this->db->getQueryBuilder(); |
|
| 449 | - $query->delete('cards') |
|
| 450 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 451 | - ->setParameter('addressbookid', $addressBookId) |
|
| 452 | - ->execute(); |
|
| 453 | - |
|
| 454 | - $query->delete('addressbookchanges') |
|
| 455 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 456 | - ->setParameter('addressbookid', $addressBookId) |
|
| 457 | - ->execute(); |
|
| 458 | - |
|
| 459 | - $query->delete('addressbooks') |
|
| 460 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 461 | - ->setParameter('id', $addressBookId) |
|
| 462 | - ->execute(); |
|
| 463 | - |
|
| 464 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 465 | - |
|
| 466 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 467 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 468 | - ->execute(); |
|
| 469 | - |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Returns all cards for a specific addressbook id. |
|
| 474 | - * |
|
| 475 | - * This method should return the following properties for each card: |
|
| 476 | - * * carddata - raw vcard data |
|
| 477 | - * * uri - Some unique url |
|
| 478 | - * * lastmodified - A unix timestamp |
|
| 479 | - * |
|
| 480 | - * It's recommended to also return the following properties: |
|
| 481 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 482 | - * * size - The size of the card in bytes. |
|
| 483 | - * |
|
| 484 | - * If these last two properties are provided, less time will be spent |
|
| 485 | - * calculating them. If they are specified, you can also ommit carddata. |
|
| 486 | - * This may speed up certain requests, especially with large cards. |
|
| 487 | - * |
|
| 488 | - * @param mixed $addressBookId |
|
| 489 | - * @return array |
|
| 490 | - */ |
|
| 491 | - function getCards($addressBookId) { |
|
| 492 | - $query = $this->db->getQueryBuilder(); |
|
| 493 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 494 | - ->from('cards') |
|
| 495 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 496 | - |
|
| 497 | - $cards = []; |
|
| 498 | - |
|
| 499 | - $result = $query->execute(); |
|
| 500 | - while($row = $result->fetch()) { |
|
| 501 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 502 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 503 | - $cards[] = $row; |
|
| 504 | - } |
|
| 505 | - $result->closeCursor(); |
|
| 506 | - |
|
| 507 | - return $cards; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Returns a specific card. |
|
| 512 | - * |
|
| 513 | - * The same set of properties must be returned as with getCards. The only |
|
| 514 | - * exception is that 'carddata' is absolutely required. |
|
| 515 | - * |
|
| 516 | - * If the card does not exist, you must return false. |
|
| 517 | - * |
|
| 518 | - * @param mixed $addressBookId |
|
| 519 | - * @param string $cardUri |
|
| 520 | - * @return array |
|
| 521 | - */ |
|
| 522 | - function getCard($addressBookId, $cardUri) { |
|
| 523 | - $query = $this->db->getQueryBuilder(); |
|
| 524 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 525 | - ->from('cards') |
|
| 526 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 527 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 528 | - ->setMaxResults(1); |
|
| 529 | - |
|
| 530 | - $result = $query->execute(); |
|
| 531 | - $row = $result->fetch(); |
|
| 532 | - if (!$row) { |
|
| 533 | - return false; |
|
| 534 | - } |
|
| 535 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 536 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 537 | - |
|
| 538 | - return $row; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * Returns a list of cards. |
|
| 543 | - * |
|
| 544 | - * This method should work identical to getCard, but instead return all the |
|
| 545 | - * cards in the list as an array. |
|
| 546 | - * |
|
| 547 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 548 | - * |
|
| 549 | - * @param mixed $addressBookId |
|
| 550 | - * @param string[] $uris |
|
| 551 | - * @return array |
|
| 552 | - */ |
|
| 553 | - function getMultipleCards($addressBookId, array $uris) { |
|
| 554 | - if (empty($uris)) { |
|
| 555 | - return []; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $chunks = array_chunk($uris, 100); |
|
| 559 | - $cards = []; |
|
| 560 | - |
|
| 561 | - $query = $this->db->getQueryBuilder(); |
|
| 562 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 563 | - ->from('cards') |
|
| 564 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 565 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 566 | - |
|
| 567 | - foreach ($chunks as $uris) { |
|
| 568 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 569 | - $result = $query->execute(); |
|
| 570 | - |
|
| 571 | - while ($row = $result->fetch()) { |
|
| 572 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 573 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 574 | - $cards[] = $row; |
|
| 575 | - } |
|
| 576 | - $result->closeCursor(); |
|
| 577 | - } |
|
| 578 | - return $cards; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Creates a new card. |
|
| 583 | - * |
|
| 584 | - * The addressbook id will be passed as the first argument. This is the |
|
| 585 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 586 | - * |
|
| 587 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 588 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 589 | - * |
|
| 590 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 591 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 592 | - * is, the string itself must contain the double quotes). |
|
| 593 | - * |
|
| 594 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 595 | - * subsequent GET request on the same card does not have the same body, |
|
| 596 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 597 | - * confused. |
|
| 598 | - * |
|
| 599 | - * If you don't return an ETag, you can just return null. |
|
| 600 | - * |
|
| 601 | - * @param mixed $addressBookId |
|
| 602 | - * @param string $cardUri |
|
| 603 | - * @param string $cardData |
|
| 604 | - * @return string |
|
| 605 | - */ |
|
| 606 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
| 607 | - $etag = md5($cardData); |
|
| 608 | - |
|
| 609 | - $query = $this->db->getQueryBuilder(); |
|
| 610 | - $query->insert('cards') |
|
| 611 | - ->values([ |
|
| 612 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 613 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 614 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 615 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 616 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 617 | - 'etag' => $query->createNamedParameter($etag), |
|
| 618 | - ]) |
|
| 619 | - ->execute(); |
|
| 620 | - |
|
| 621 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 622 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 623 | - |
|
| 624 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 625 | - new GenericEvent(null, [ |
|
| 626 | - 'addressBookId' => $addressBookId, |
|
| 627 | - 'cardUri' => $cardUri, |
|
| 628 | - 'cardData' => $cardData])); |
|
| 629 | - |
|
| 630 | - return '"' . $etag . '"'; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Updates a card. |
|
| 635 | - * |
|
| 636 | - * The addressbook id will be passed as the first argument. This is the |
|
| 637 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 638 | - * |
|
| 639 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 640 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 641 | - * |
|
| 642 | - * It is possible to return an ETag from this method. This ETag should |
|
| 643 | - * match that of the updated resource, and must be enclosed with double |
|
| 644 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 645 | - * |
|
| 646 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 647 | - * subsequent GET request on the same card does not have the same body, |
|
| 648 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 649 | - * confused. |
|
| 650 | - * |
|
| 651 | - * If you don't return an ETag, you can just return null. |
|
| 652 | - * |
|
| 653 | - * @param mixed $addressBookId |
|
| 654 | - * @param string $cardUri |
|
| 655 | - * @param string $cardData |
|
| 656 | - * @return string |
|
| 657 | - */ |
|
| 658 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 659 | - |
|
| 660 | - $etag = md5($cardData); |
|
| 661 | - $query = $this->db->getQueryBuilder(); |
|
| 662 | - $query->update('cards') |
|
| 663 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 664 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 665 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 666 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 667 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 668 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 669 | - ->execute(); |
|
| 670 | - |
|
| 671 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 672 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 673 | - |
|
| 674 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 675 | - new GenericEvent(null, [ |
|
| 676 | - 'addressBookId' => $addressBookId, |
|
| 677 | - 'cardUri' => $cardUri, |
|
| 678 | - 'cardData' => $cardData])); |
|
| 679 | - |
|
| 680 | - return '"' . $etag . '"'; |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Deletes a card |
|
| 685 | - * |
|
| 686 | - * @param mixed $addressBookId |
|
| 687 | - * @param string $cardUri |
|
| 688 | - * @return bool |
|
| 689 | - */ |
|
| 690 | - function deleteCard($addressBookId, $cardUri) { |
|
| 691 | - try { |
|
| 692 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 693 | - } catch (\InvalidArgumentException $e) { |
|
| 694 | - $cardId = null; |
|
| 695 | - } |
|
| 696 | - $query = $this->db->getQueryBuilder(); |
|
| 697 | - $ret = $query->delete('cards') |
|
| 698 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 699 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 700 | - ->execute(); |
|
| 701 | - |
|
| 702 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 703 | - |
|
| 704 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 705 | - new GenericEvent(null, [ |
|
| 706 | - 'addressBookId' => $addressBookId, |
|
| 707 | - 'cardUri' => $cardUri])); |
|
| 708 | - |
|
| 709 | - if ($ret === 1) { |
|
| 710 | - if ($cardId !== null) { |
|
| 711 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 712 | - } |
|
| 713 | - return true; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - return false; |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - /** |
|
| 720 | - * The getChanges method returns all the changes that have happened, since |
|
| 721 | - * the specified syncToken in the specified address book. |
|
| 722 | - * |
|
| 723 | - * This function should return an array, such as the following: |
|
| 724 | - * |
|
| 725 | - * [ |
|
| 726 | - * 'syncToken' => 'The current synctoken', |
|
| 727 | - * 'added' => [ |
|
| 728 | - * 'new.txt', |
|
| 729 | - * ], |
|
| 730 | - * 'modified' => [ |
|
| 731 | - * 'modified.txt', |
|
| 732 | - * ], |
|
| 733 | - * 'deleted' => [ |
|
| 734 | - * 'foo.php.bak', |
|
| 735 | - * 'old.txt' |
|
| 736 | - * ] |
|
| 737 | - * ]; |
|
| 738 | - * |
|
| 739 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 740 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 741 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 742 | - * |
|
| 743 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 744 | - * sync, and all members should be reported. |
|
| 745 | - * |
|
| 746 | - * The modified property is an array of nodenames that have changed since |
|
| 747 | - * the last token. |
|
| 748 | - * |
|
| 749 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 750 | - * from collection. |
|
| 751 | - * |
|
| 752 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 753 | - * 1, you only have to report changes that happened only directly in |
|
| 754 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 755 | - * the nodes below the child collections. (grandchildren) |
|
| 756 | - * |
|
| 757 | - * The $limit argument allows a client to specify how many results should |
|
| 758 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 759 | - * as infinite. |
|
| 760 | - * |
|
| 761 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 762 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 763 | - * |
|
| 764 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 765 | - * return null. |
|
| 766 | - * |
|
| 767 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 768 | - * |
|
| 769 | - * @param string $addressBookId |
|
| 770 | - * @param string $syncToken |
|
| 771 | - * @param int $syncLevel |
|
| 772 | - * @param int $limit |
|
| 773 | - * @return array |
|
| 774 | - */ |
|
| 775 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 776 | - // Current synctoken |
|
| 777 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 778 | - $stmt->execute([ $addressBookId ]); |
|
| 779 | - $currentToken = $stmt->fetchColumn(0); |
|
| 780 | - |
|
| 781 | - if (is_null($currentToken)) return null; |
|
| 782 | - |
|
| 783 | - $result = [ |
|
| 784 | - 'syncToken' => $currentToken, |
|
| 785 | - 'added' => [], |
|
| 786 | - 'modified' => [], |
|
| 787 | - 'deleted' => [], |
|
| 788 | - ]; |
|
| 789 | - |
|
| 790 | - if ($syncToken) { |
|
| 791 | - |
|
| 792 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 793 | - if ($limit>0) { |
|
| 794 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - // Fetching all changes |
|
| 798 | - $stmt = $this->db->prepare($query); |
|
| 799 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 800 | - |
|
| 801 | - $changes = []; |
|
| 802 | - |
|
| 803 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 804 | - // last change on a node is relevant. |
|
| 805 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 806 | - |
|
| 807 | - $changes[$row['uri']] = $row['operation']; |
|
| 808 | - |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - foreach($changes as $uri => $operation) { |
|
| 812 | - |
|
| 813 | - switch($operation) { |
|
| 814 | - case 1: |
|
| 815 | - $result['added'][] = $uri; |
|
| 816 | - break; |
|
| 817 | - case 2: |
|
| 818 | - $result['modified'][] = $uri; |
|
| 819 | - break; |
|
| 820 | - case 3: |
|
| 821 | - $result['deleted'][] = $uri; |
|
| 822 | - break; |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - } |
|
| 826 | - } else { |
|
| 827 | - // No synctoken supplied, this is the initial sync. |
|
| 828 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 829 | - $stmt = $this->db->prepare($query); |
|
| 830 | - $stmt->execute([$addressBookId]); |
|
| 831 | - |
|
| 832 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 833 | - } |
|
| 834 | - return $result; |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - /** |
|
| 838 | - * Adds a change record to the addressbookchanges table. |
|
| 839 | - * |
|
| 840 | - * @param mixed $addressBookId |
|
| 841 | - * @param string $objectUri |
|
| 842 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 843 | - * @return void |
|
| 844 | - */ |
|
| 845 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 846 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 847 | - $stmt = $this->db->prepare($sql); |
|
| 848 | - $stmt->execute([ |
|
| 849 | - $objectUri, |
|
| 850 | - $addressBookId, |
|
| 851 | - $operation, |
|
| 852 | - $addressBookId |
|
| 853 | - ]); |
|
| 854 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 855 | - $stmt->execute([ |
|
| 856 | - $addressBookId |
|
| 857 | - ]); |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - private function readBlob($cardData) { |
|
| 861 | - if (is_resource($cardData)) { |
|
| 862 | - return stream_get_contents($cardData); |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - return $cardData; |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * @param IShareable $shareable |
|
| 870 | - * @param string[] $add |
|
| 871 | - * @param string[] $remove |
|
| 872 | - */ |
|
| 873 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 874 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 875 | - } |
|
| 876 | - |
|
| 877 | - /** |
|
| 878 | - * search contact |
|
| 879 | - * |
|
| 880 | - * @param int $addressBookId |
|
| 881 | - * @param string $pattern which should match within the $searchProperties |
|
| 882 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 883 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 884 | - */ |
|
| 885 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
| 886 | - $query = $this->db->getQueryBuilder(); |
|
| 887 | - $query2 = $this->db->getQueryBuilder(); |
|
| 888 | - |
|
| 889 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 890 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 891 | - $or = $query2->expr()->orX(); |
|
| 892 | - foreach ($searchProperties as $property) { |
|
| 893 | - $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 894 | - } |
|
| 895 | - $query2->andWhere($or); |
|
| 896 | - |
|
| 897 | - // No need for like when the pattern is empty |
|
| 898 | - if ('' !== $pattern) { |
|
| 899 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 903 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 904 | - |
|
| 905 | - $result = $query->execute(); |
|
| 906 | - $cards = $result->fetchAll(); |
|
| 907 | - |
|
| 908 | - $result->closeCursor(); |
|
| 909 | - |
|
| 910 | - return array_map(function($array) { |
|
| 911 | - $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 912 | - return $array; |
|
| 913 | - }, $cards); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * @param int $bookId |
|
| 918 | - * @param string $name |
|
| 919 | - * @return array |
|
| 920 | - */ |
|
| 921 | - public function collectCardProperties($bookId, $name) { |
|
| 922 | - $query = $this->db->getQueryBuilder(); |
|
| 923 | - $result = $query->selectDistinct('value') |
|
| 924 | - ->from($this->dbCardsPropertiesTable) |
|
| 925 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 926 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 927 | - ->execute(); |
|
| 928 | - |
|
| 929 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 930 | - $result->closeCursor(); |
|
| 931 | - |
|
| 932 | - return $all; |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - /** |
|
| 936 | - * get URI from a given contact |
|
| 937 | - * |
|
| 938 | - * @param int $id |
|
| 939 | - * @return string |
|
| 940 | - */ |
|
| 941 | - public function getCardUri($id) { |
|
| 942 | - $query = $this->db->getQueryBuilder(); |
|
| 943 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 944 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 945 | - ->setParameter('id', $id); |
|
| 946 | - |
|
| 947 | - $result = $query->execute(); |
|
| 948 | - $uri = $result->fetch(); |
|
| 949 | - $result->closeCursor(); |
|
| 950 | - |
|
| 951 | - if (!isset($uri['uri'])) { |
|
| 952 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - return $uri['uri']; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - /** |
|
| 959 | - * return contact with the given URI |
|
| 960 | - * |
|
| 961 | - * @param int $addressBookId |
|
| 962 | - * @param string $uri |
|
| 963 | - * @returns array |
|
| 964 | - */ |
|
| 965 | - public function getContact($addressBookId, $uri) { |
|
| 966 | - $result = []; |
|
| 967 | - $query = $this->db->getQueryBuilder(); |
|
| 968 | - $query->select('*')->from($this->dbCardsTable) |
|
| 969 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 970 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 971 | - $queryResult = $query->execute(); |
|
| 972 | - $contact = $queryResult->fetch(); |
|
| 973 | - $queryResult->closeCursor(); |
|
| 974 | - |
|
| 975 | - if (is_array($contact)) { |
|
| 976 | - $result = $contact; |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - return $result; |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * Returns the list of people whom this address book is shared with. |
|
| 984 | - * |
|
| 985 | - * Every element in this array should have the following properties: |
|
| 986 | - * * href - Often a mailto: address |
|
| 987 | - * * commonName - Optional, for example a first + last name |
|
| 988 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 989 | - * * readOnly - boolean |
|
| 990 | - * * summary - Optional, a description for the share |
|
| 991 | - * |
|
| 992 | - * @return array |
|
| 993 | - */ |
|
| 994 | - public function getShares($addressBookId) { |
|
| 995 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 996 | - } |
|
| 997 | - |
|
| 998 | - /** |
|
| 999 | - * update properties table |
|
| 1000 | - * |
|
| 1001 | - * @param int $addressBookId |
|
| 1002 | - * @param string $cardUri |
|
| 1003 | - * @param string $vCardSerialized |
|
| 1004 | - */ |
|
| 1005 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1006 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1007 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1008 | - |
|
| 1009 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1010 | - |
|
| 1011 | - $query = $this->db->getQueryBuilder(); |
|
| 1012 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1013 | - ->values( |
|
| 1014 | - [ |
|
| 1015 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1016 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1017 | - 'name' => $query->createParameter('name'), |
|
| 1018 | - 'value' => $query->createParameter('value'), |
|
| 1019 | - 'preferred' => $query->createParameter('preferred') |
|
| 1020 | - ] |
|
| 1021 | - ); |
|
| 1022 | - |
|
| 1023 | - foreach ($vCard->children() as $property) { |
|
| 1024 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1025 | - continue; |
|
| 1026 | - } |
|
| 1027 | - $preferred = 0; |
|
| 1028 | - foreach($property->parameters as $parameter) { |
|
| 1029 | - if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1030 | - $preferred = 1; |
|
| 1031 | - break; |
|
| 1032 | - } |
|
| 1033 | - } |
|
| 1034 | - $query->setParameter('name', $property->name); |
|
| 1035 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1036 | - $query->setParameter('preferred', $preferred); |
|
| 1037 | - $query->execute(); |
|
| 1038 | - } |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - /** |
|
| 1042 | - * read vCard data into a vCard object |
|
| 1043 | - * |
|
| 1044 | - * @param string $cardData |
|
| 1045 | - * @return VCard |
|
| 1046 | - */ |
|
| 1047 | - protected function readCard($cardData) { |
|
| 1048 | - return Reader::read($cardData); |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * delete all properties from a given card |
|
| 1053 | - * |
|
| 1054 | - * @param int $addressBookId |
|
| 1055 | - * @param int $cardId |
|
| 1056 | - */ |
|
| 1057 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1058 | - $query = $this->db->getQueryBuilder(); |
|
| 1059 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1060 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1061 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1062 | - $query->execute(); |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - /** |
|
| 1066 | - * get ID from a given contact |
|
| 1067 | - * |
|
| 1068 | - * @param int $addressBookId |
|
| 1069 | - * @param string $uri |
|
| 1070 | - * @return int |
|
| 1071 | - */ |
|
| 1072 | - protected function getCardId($addressBookId, $uri) { |
|
| 1073 | - $query = $this->db->getQueryBuilder(); |
|
| 1074 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1075 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1076 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1077 | - |
|
| 1078 | - $result = $query->execute(); |
|
| 1079 | - $cardIds = $result->fetch(); |
|
| 1080 | - $result->closeCursor(); |
|
| 1081 | - |
|
| 1082 | - if (!isset($cardIds['id'])) { |
|
| 1083 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - return (int)$cardIds['id']; |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - /** |
|
| 1090 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1091 | - * @param $addressBookId |
|
| 1092 | - * @param $acl |
|
| 1093 | - * @return array |
|
| 1094 | - */ |
|
| 1095 | - public function applyShareAcl($addressBookId, $acl) { |
|
| 1096 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 1100 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1101 | - list(, $name) = \Sabre\Uri\split($principalUri); |
|
| 1102 | - if ($toV2 === true) { |
|
| 1103 | - return "principals/users/$name"; |
|
| 1104 | - } |
|
| 1105 | - return "principals/$name"; |
|
| 1106 | - } |
|
| 1107 | - return $principalUri; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1111 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1112 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1113 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1114 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1115 | - } else { |
|
| 1116 | - $uri = $addressbookInfo['principaluri']; |
|
| 1117 | - } |
|
| 1118 | - |
|
| 1119 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1120 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1121 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1122 | - } |
|
| 1123 | - } |
|
| 52 | + const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 53 | + const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 54 | + |
|
| 55 | + /** @var Principal */ |
|
| 56 | + private $principalBackend; |
|
| 57 | + |
|
| 58 | + /** @var string */ |
|
| 59 | + private $dbCardsTable = 'cards'; |
|
| 60 | + |
|
| 61 | + /** @var string */ |
|
| 62 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 63 | + |
|
| 64 | + /** @var IDBConnection */ |
|
| 65 | + private $db; |
|
| 66 | + |
|
| 67 | + /** @var Backend */ |
|
| 68 | + private $sharingBackend; |
|
| 69 | + |
|
| 70 | + /** @var array properties to index */ |
|
| 71 | + public static $indexProperties = array( |
|
| 72 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 73 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var string[] Map of uid => display name |
|
| 77 | + */ |
|
| 78 | + protected $userDisplayNames; |
|
| 79 | + |
|
| 80 | + /** @var IUserManager */ |
|
| 81 | + private $userManager; |
|
| 82 | + |
|
| 83 | + /** @var EventDispatcherInterface */ |
|
| 84 | + private $dispatcher; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * CardDavBackend constructor. |
|
| 88 | + * |
|
| 89 | + * @param IDBConnection $db |
|
| 90 | + * @param Principal $principalBackend |
|
| 91 | + * @param IUserManager $userManager |
|
| 92 | + * @param IGroupManager $groupManager |
|
| 93 | + * @param EventDispatcherInterface $dispatcher |
|
| 94 | + */ |
|
| 95 | + public function __construct(IDBConnection $db, |
|
| 96 | + Principal $principalBackend, |
|
| 97 | + IUserManager $userManager, |
|
| 98 | + IGroupManager $groupManager, |
|
| 99 | + EventDispatcherInterface $dispatcher) { |
|
| 100 | + $this->db = $db; |
|
| 101 | + $this->principalBackend = $principalBackend; |
|
| 102 | + $this->userManager = $userManager; |
|
| 103 | + $this->dispatcher = $dispatcher; |
|
| 104 | + $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Return the number of address books for a principal |
|
| 109 | + * |
|
| 110 | + * @param $principalUri |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 114 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 115 | + $query = $this->db->getQueryBuilder(); |
|
| 116 | + $query->select($query->createFunction('COUNT(*)')) |
|
| 117 | + ->from('addressbooks') |
|
| 118 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 119 | + |
|
| 120 | + return (int)$query->execute()->fetchColumn(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Returns the list of address books for a specific user. |
|
| 125 | + * |
|
| 126 | + * Every addressbook should have the following properties: |
|
| 127 | + * id - an arbitrary unique id |
|
| 128 | + * uri - the 'basename' part of the url |
|
| 129 | + * principaluri - Same as the passed parameter |
|
| 130 | + * |
|
| 131 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 132 | + * common ones are : |
|
| 133 | + * {DAV:}displayname |
|
| 134 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 135 | + * {http://calendarserver.org/ns/}getctag |
|
| 136 | + * |
|
| 137 | + * @param string $principalUri |
|
| 138 | + * @return array |
|
| 139 | + */ |
|
| 140 | + function getAddressBooksForUser($principalUri) { |
|
| 141 | + $principalUriOriginal = $principalUri; |
|
| 142 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 143 | + $query = $this->db->getQueryBuilder(); |
|
| 144 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 145 | + ->from('addressbooks') |
|
| 146 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 147 | + |
|
| 148 | + $addressBooks = []; |
|
| 149 | + |
|
| 150 | + $result = $query->execute(); |
|
| 151 | + while($row = $result->fetch()) { |
|
| 152 | + $addressBooks[$row['id']] = [ |
|
| 153 | + 'id' => $row['id'], |
|
| 154 | + 'uri' => $row['uri'], |
|
| 155 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 156 | + '{DAV:}displayname' => $row['displayname'], |
|
| 157 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 158 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 159 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 160 | + ]; |
|
| 161 | + |
|
| 162 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 163 | + } |
|
| 164 | + $result->closeCursor(); |
|
| 165 | + |
|
| 166 | + // query for shared calendars |
|
| 167 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 168 | + $principals = array_map(function($principal) { |
|
| 169 | + return urldecode($principal); |
|
| 170 | + }, $principals); |
|
| 171 | + $principals[]= $principalUri; |
|
| 172 | + |
|
| 173 | + $query = $this->db->getQueryBuilder(); |
|
| 174 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 175 | + ->from('dav_shares', 's') |
|
| 176 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 177 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 178 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 179 | + ->setParameter('type', 'addressbook') |
|
| 180 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 181 | + ->execute(); |
|
| 182 | + |
|
| 183 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 184 | + while($row = $result->fetch()) { |
|
| 185 | + if ($row['principaluri'] === $principalUri) { |
|
| 186 | + continue; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 190 | + if (isset($addressBooks[$row['id']])) { |
|
| 191 | + if ($readOnly) { |
|
| 192 | + // New share can not have more permissions then the old one. |
|
| 193 | + continue; |
|
| 194 | + } |
|
| 195 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 196 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 197 | + // Old share is already read-write, no more permissions can be gained |
|
| 198 | + continue; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + list(, $name) = \Sabre\Uri\split($row['principaluri']); |
|
| 203 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 204 | + $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 205 | + |
|
| 206 | + $addressBooks[$row['id']] = [ |
|
| 207 | + 'id' => $row['id'], |
|
| 208 | + 'uri' => $uri, |
|
| 209 | + 'principaluri' => $principalUriOriginal, |
|
| 210 | + '{DAV:}displayname' => $displayName, |
|
| 211 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 212 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 213 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 214 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 215 | + $readOnlyPropertyName => $readOnly, |
|
| 216 | + ]; |
|
| 217 | + |
|
| 218 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 219 | + } |
|
| 220 | + $result->closeCursor(); |
|
| 221 | + |
|
| 222 | + return array_values($addressBooks); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 226 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 227 | + $query = $this->db->getQueryBuilder(); |
|
| 228 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 229 | + ->from('addressbooks') |
|
| 230 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 231 | + |
|
| 232 | + $addressBooks = []; |
|
| 233 | + |
|
| 234 | + $result = $query->execute(); |
|
| 235 | + while($row = $result->fetch()) { |
|
| 236 | + $addressBooks[$row['id']] = [ |
|
| 237 | + 'id' => $row['id'], |
|
| 238 | + 'uri' => $row['uri'], |
|
| 239 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 240 | + '{DAV:}displayname' => $row['displayname'], |
|
| 241 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 242 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 243 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 244 | + ]; |
|
| 245 | + |
|
| 246 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 247 | + } |
|
| 248 | + $result->closeCursor(); |
|
| 249 | + |
|
| 250 | + return array_values($addressBooks); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + private function getUserDisplayName($uid) { |
|
| 254 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 255 | + $user = $this->userManager->get($uid); |
|
| 256 | + |
|
| 257 | + if ($user instanceof IUser) { |
|
| 258 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 259 | + } else { |
|
| 260 | + $this->userDisplayNames[$uid] = $uid; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return $this->userDisplayNames[$uid]; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @param int $addressBookId |
|
| 269 | + */ |
|
| 270 | + public function getAddressBookById($addressBookId) { |
|
| 271 | + $query = $this->db->getQueryBuilder(); |
|
| 272 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 273 | + ->from('addressbooks') |
|
| 274 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 275 | + ->execute(); |
|
| 276 | + |
|
| 277 | + $row = $result->fetch(); |
|
| 278 | + $result->closeCursor(); |
|
| 279 | + if ($row === false) { |
|
| 280 | + return null; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $addressBook = [ |
|
| 284 | + 'id' => $row['id'], |
|
| 285 | + 'uri' => $row['uri'], |
|
| 286 | + 'principaluri' => $row['principaluri'], |
|
| 287 | + '{DAV:}displayname' => $row['displayname'], |
|
| 288 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 289 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 290 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 291 | + ]; |
|
| 292 | + |
|
| 293 | + $this->addOwnerPrincipal($addressBook); |
|
| 294 | + |
|
| 295 | + return $addressBook; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * @param $addressBookUri |
|
| 300 | + * @return array|null |
|
| 301 | + */ |
|
| 302 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 303 | + $query = $this->db->getQueryBuilder(); |
|
| 304 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 305 | + ->from('addressbooks') |
|
| 306 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 307 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 308 | + ->setMaxResults(1) |
|
| 309 | + ->execute(); |
|
| 310 | + |
|
| 311 | + $row = $result->fetch(); |
|
| 312 | + $result->closeCursor(); |
|
| 313 | + if ($row === false) { |
|
| 314 | + return null; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $addressBook = [ |
|
| 318 | + 'id' => $row['id'], |
|
| 319 | + 'uri' => $row['uri'], |
|
| 320 | + 'principaluri' => $row['principaluri'], |
|
| 321 | + '{DAV:}displayname' => $row['displayname'], |
|
| 322 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 323 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 324 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 325 | + ]; |
|
| 326 | + |
|
| 327 | + $this->addOwnerPrincipal($addressBook); |
|
| 328 | + |
|
| 329 | + return $addressBook; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Updates properties for an address book. |
|
| 334 | + * |
|
| 335 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 336 | + * To do the actual updates, you must tell this object which properties |
|
| 337 | + * you're going to process with the handle() method. |
|
| 338 | + * |
|
| 339 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 340 | + * promise I can handle updating this property". |
|
| 341 | + * |
|
| 342 | + * Read the PropPatch documentation for more info and examples. |
|
| 343 | + * |
|
| 344 | + * @param string $addressBookId |
|
| 345 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 346 | + * @return void |
|
| 347 | + */ |
|
| 348 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 349 | + $supportedProperties = [ |
|
| 350 | + '{DAV:}displayname', |
|
| 351 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 352 | + ]; |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * @suppress SqlInjectionChecker |
|
| 356 | + */ |
|
| 357 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 358 | + |
|
| 359 | + $updates = []; |
|
| 360 | + foreach($mutations as $property=>$newValue) { |
|
| 361 | + |
|
| 362 | + switch($property) { |
|
| 363 | + case '{DAV:}displayname' : |
|
| 364 | + $updates['displayname'] = $newValue; |
|
| 365 | + break; |
|
| 366 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 367 | + $updates['description'] = $newValue; |
|
| 368 | + break; |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + $query = $this->db->getQueryBuilder(); |
|
| 372 | + $query->update('addressbooks'); |
|
| 373 | + |
|
| 374 | + foreach($updates as $key=>$value) { |
|
| 375 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 376 | + } |
|
| 377 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 378 | + ->execute(); |
|
| 379 | + |
|
| 380 | + $this->addChange($addressBookId, "", 2); |
|
| 381 | + |
|
| 382 | + return true; |
|
| 383 | + |
|
| 384 | + }); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Creates a new address book |
|
| 389 | + * |
|
| 390 | + * @param string $principalUri |
|
| 391 | + * @param string $url Just the 'basename' of the url. |
|
| 392 | + * @param array $properties |
|
| 393 | + * @return int |
|
| 394 | + * @throws BadRequest |
|
| 395 | + */ |
|
| 396 | + function createAddressBook($principalUri, $url, array $properties) { |
|
| 397 | + $values = [ |
|
| 398 | + 'displayname' => null, |
|
| 399 | + 'description' => null, |
|
| 400 | + 'principaluri' => $principalUri, |
|
| 401 | + 'uri' => $url, |
|
| 402 | + 'synctoken' => 1 |
|
| 403 | + ]; |
|
| 404 | + |
|
| 405 | + foreach($properties as $property=>$newValue) { |
|
| 406 | + |
|
| 407 | + switch($property) { |
|
| 408 | + case '{DAV:}displayname' : |
|
| 409 | + $values['displayname'] = $newValue; |
|
| 410 | + break; |
|
| 411 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 412 | + $values['description'] = $newValue; |
|
| 413 | + break; |
|
| 414 | + default : |
|
| 415 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 421 | + // to work with addressbooks not having a displayname. |
|
| 422 | + if(is_null($values['displayname'])) { |
|
| 423 | + $values['displayname'] = $url; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + $query = $this->db->getQueryBuilder(); |
|
| 427 | + $query->insert('addressbooks') |
|
| 428 | + ->values([ |
|
| 429 | + 'uri' => $query->createParameter('uri'), |
|
| 430 | + 'displayname' => $query->createParameter('displayname'), |
|
| 431 | + 'description' => $query->createParameter('description'), |
|
| 432 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 433 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 434 | + ]) |
|
| 435 | + ->setParameters($values) |
|
| 436 | + ->execute(); |
|
| 437 | + |
|
| 438 | + return $query->getLastInsertId(); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Deletes an entire addressbook and all its contents |
|
| 443 | + * |
|
| 444 | + * @param mixed $addressBookId |
|
| 445 | + * @return void |
|
| 446 | + */ |
|
| 447 | + function deleteAddressBook($addressBookId) { |
|
| 448 | + $query = $this->db->getQueryBuilder(); |
|
| 449 | + $query->delete('cards') |
|
| 450 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 451 | + ->setParameter('addressbookid', $addressBookId) |
|
| 452 | + ->execute(); |
|
| 453 | + |
|
| 454 | + $query->delete('addressbookchanges') |
|
| 455 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 456 | + ->setParameter('addressbookid', $addressBookId) |
|
| 457 | + ->execute(); |
|
| 458 | + |
|
| 459 | + $query->delete('addressbooks') |
|
| 460 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 461 | + ->setParameter('id', $addressBookId) |
|
| 462 | + ->execute(); |
|
| 463 | + |
|
| 464 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 465 | + |
|
| 466 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 467 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 468 | + ->execute(); |
|
| 469 | + |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Returns all cards for a specific addressbook id. |
|
| 474 | + * |
|
| 475 | + * This method should return the following properties for each card: |
|
| 476 | + * * carddata - raw vcard data |
|
| 477 | + * * uri - Some unique url |
|
| 478 | + * * lastmodified - A unix timestamp |
|
| 479 | + * |
|
| 480 | + * It's recommended to also return the following properties: |
|
| 481 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 482 | + * * size - The size of the card in bytes. |
|
| 483 | + * |
|
| 484 | + * If these last two properties are provided, less time will be spent |
|
| 485 | + * calculating them. If they are specified, you can also ommit carddata. |
|
| 486 | + * This may speed up certain requests, especially with large cards. |
|
| 487 | + * |
|
| 488 | + * @param mixed $addressBookId |
|
| 489 | + * @return array |
|
| 490 | + */ |
|
| 491 | + function getCards($addressBookId) { |
|
| 492 | + $query = $this->db->getQueryBuilder(); |
|
| 493 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 494 | + ->from('cards') |
|
| 495 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 496 | + |
|
| 497 | + $cards = []; |
|
| 498 | + |
|
| 499 | + $result = $query->execute(); |
|
| 500 | + while($row = $result->fetch()) { |
|
| 501 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 502 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 503 | + $cards[] = $row; |
|
| 504 | + } |
|
| 505 | + $result->closeCursor(); |
|
| 506 | + |
|
| 507 | + return $cards; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Returns a specific card. |
|
| 512 | + * |
|
| 513 | + * The same set of properties must be returned as with getCards. The only |
|
| 514 | + * exception is that 'carddata' is absolutely required. |
|
| 515 | + * |
|
| 516 | + * If the card does not exist, you must return false. |
|
| 517 | + * |
|
| 518 | + * @param mixed $addressBookId |
|
| 519 | + * @param string $cardUri |
|
| 520 | + * @return array |
|
| 521 | + */ |
|
| 522 | + function getCard($addressBookId, $cardUri) { |
|
| 523 | + $query = $this->db->getQueryBuilder(); |
|
| 524 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 525 | + ->from('cards') |
|
| 526 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 527 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 528 | + ->setMaxResults(1); |
|
| 529 | + |
|
| 530 | + $result = $query->execute(); |
|
| 531 | + $row = $result->fetch(); |
|
| 532 | + if (!$row) { |
|
| 533 | + return false; |
|
| 534 | + } |
|
| 535 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 536 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 537 | + |
|
| 538 | + return $row; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * Returns a list of cards. |
|
| 543 | + * |
|
| 544 | + * This method should work identical to getCard, but instead return all the |
|
| 545 | + * cards in the list as an array. |
|
| 546 | + * |
|
| 547 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 548 | + * |
|
| 549 | + * @param mixed $addressBookId |
|
| 550 | + * @param string[] $uris |
|
| 551 | + * @return array |
|
| 552 | + */ |
|
| 553 | + function getMultipleCards($addressBookId, array $uris) { |
|
| 554 | + if (empty($uris)) { |
|
| 555 | + return []; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $chunks = array_chunk($uris, 100); |
|
| 559 | + $cards = []; |
|
| 560 | + |
|
| 561 | + $query = $this->db->getQueryBuilder(); |
|
| 562 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 563 | + ->from('cards') |
|
| 564 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 565 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 566 | + |
|
| 567 | + foreach ($chunks as $uris) { |
|
| 568 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 569 | + $result = $query->execute(); |
|
| 570 | + |
|
| 571 | + while ($row = $result->fetch()) { |
|
| 572 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 573 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 574 | + $cards[] = $row; |
|
| 575 | + } |
|
| 576 | + $result->closeCursor(); |
|
| 577 | + } |
|
| 578 | + return $cards; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Creates a new card. |
|
| 583 | + * |
|
| 584 | + * The addressbook id will be passed as the first argument. This is the |
|
| 585 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 586 | + * |
|
| 587 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 588 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 589 | + * |
|
| 590 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 591 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 592 | + * is, the string itself must contain the double quotes). |
|
| 593 | + * |
|
| 594 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 595 | + * subsequent GET request on the same card does not have the same body, |
|
| 596 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 597 | + * confused. |
|
| 598 | + * |
|
| 599 | + * If you don't return an ETag, you can just return null. |
|
| 600 | + * |
|
| 601 | + * @param mixed $addressBookId |
|
| 602 | + * @param string $cardUri |
|
| 603 | + * @param string $cardData |
|
| 604 | + * @return string |
|
| 605 | + */ |
|
| 606 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
| 607 | + $etag = md5($cardData); |
|
| 608 | + |
|
| 609 | + $query = $this->db->getQueryBuilder(); |
|
| 610 | + $query->insert('cards') |
|
| 611 | + ->values([ |
|
| 612 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 613 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 614 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 615 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 616 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 617 | + 'etag' => $query->createNamedParameter($etag), |
|
| 618 | + ]) |
|
| 619 | + ->execute(); |
|
| 620 | + |
|
| 621 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 622 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 623 | + |
|
| 624 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 625 | + new GenericEvent(null, [ |
|
| 626 | + 'addressBookId' => $addressBookId, |
|
| 627 | + 'cardUri' => $cardUri, |
|
| 628 | + 'cardData' => $cardData])); |
|
| 629 | + |
|
| 630 | + return '"' . $etag . '"'; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Updates a card. |
|
| 635 | + * |
|
| 636 | + * The addressbook id will be passed as the first argument. This is the |
|
| 637 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 638 | + * |
|
| 639 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 640 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 641 | + * |
|
| 642 | + * It is possible to return an ETag from this method. This ETag should |
|
| 643 | + * match that of the updated resource, and must be enclosed with double |
|
| 644 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 645 | + * |
|
| 646 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 647 | + * subsequent GET request on the same card does not have the same body, |
|
| 648 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 649 | + * confused. |
|
| 650 | + * |
|
| 651 | + * If you don't return an ETag, you can just return null. |
|
| 652 | + * |
|
| 653 | + * @param mixed $addressBookId |
|
| 654 | + * @param string $cardUri |
|
| 655 | + * @param string $cardData |
|
| 656 | + * @return string |
|
| 657 | + */ |
|
| 658 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 659 | + |
|
| 660 | + $etag = md5($cardData); |
|
| 661 | + $query = $this->db->getQueryBuilder(); |
|
| 662 | + $query->update('cards') |
|
| 663 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 664 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 665 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 666 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 667 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 668 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 669 | + ->execute(); |
|
| 670 | + |
|
| 671 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 672 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 673 | + |
|
| 674 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 675 | + new GenericEvent(null, [ |
|
| 676 | + 'addressBookId' => $addressBookId, |
|
| 677 | + 'cardUri' => $cardUri, |
|
| 678 | + 'cardData' => $cardData])); |
|
| 679 | + |
|
| 680 | + return '"' . $etag . '"'; |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Deletes a card |
|
| 685 | + * |
|
| 686 | + * @param mixed $addressBookId |
|
| 687 | + * @param string $cardUri |
|
| 688 | + * @return bool |
|
| 689 | + */ |
|
| 690 | + function deleteCard($addressBookId, $cardUri) { |
|
| 691 | + try { |
|
| 692 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 693 | + } catch (\InvalidArgumentException $e) { |
|
| 694 | + $cardId = null; |
|
| 695 | + } |
|
| 696 | + $query = $this->db->getQueryBuilder(); |
|
| 697 | + $ret = $query->delete('cards') |
|
| 698 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 699 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 700 | + ->execute(); |
|
| 701 | + |
|
| 702 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 703 | + |
|
| 704 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 705 | + new GenericEvent(null, [ |
|
| 706 | + 'addressBookId' => $addressBookId, |
|
| 707 | + 'cardUri' => $cardUri])); |
|
| 708 | + |
|
| 709 | + if ($ret === 1) { |
|
| 710 | + if ($cardId !== null) { |
|
| 711 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 712 | + } |
|
| 713 | + return true; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + return false; |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + /** |
|
| 720 | + * The getChanges method returns all the changes that have happened, since |
|
| 721 | + * the specified syncToken in the specified address book. |
|
| 722 | + * |
|
| 723 | + * This function should return an array, such as the following: |
|
| 724 | + * |
|
| 725 | + * [ |
|
| 726 | + * 'syncToken' => 'The current synctoken', |
|
| 727 | + * 'added' => [ |
|
| 728 | + * 'new.txt', |
|
| 729 | + * ], |
|
| 730 | + * 'modified' => [ |
|
| 731 | + * 'modified.txt', |
|
| 732 | + * ], |
|
| 733 | + * 'deleted' => [ |
|
| 734 | + * 'foo.php.bak', |
|
| 735 | + * 'old.txt' |
|
| 736 | + * ] |
|
| 737 | + * ]; |
|
| 738 | + * |
|
| 739 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 740 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 741 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 742 | + * |
|
| 743 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 744 | + * sync, and all members should be reported. |
|
| 745 | + * |
|
| 746 | + * The modified property is an array of nodenames that have changed since |
|
| 747 | + * the last token. |
|
| 748 | + * |
|
| 749 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 750 | + * from collection. |
|
| 751 | + * |
|
| 752 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 753 | + * 1, you only have to report changes that happened only directly in |
|
| 754 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 755 | + * the nodes below the child collections. (grandchildren) |
|
| 756 | + * |
|
| 757 | + * The $limit argument allows a client to specify how many results should |
|
| 758 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 759 | + * as infinite. |
|
| 760 | + * |
|
| 761 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 762 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 763 | + * |
|
| 764 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 765 | + * return null. |
|
| 766 | + * |
|
| 767 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 768 | + * |
|
| 769 | + * @param string $addressBookId |
|
| 770 | + * @param string $syncToken |
|
| 771 | + * @param int $syncLevel |
|
| 772 | + * @param int $limit |
|
| 773 | + * @return array |
|
| 774 | + */ |
|
| 775 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 776 | + // Current synctoken |
|
| 777 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 778 | + $stmt->execute([ $addressBookId ]); |
|
| 779 | + $currentToken = $stmt->fetchColumn(0); |
|
| 780 | + |
|
| 781 | + if (is_null($currentToken)) return null; |
|
| 782 | + |
|
| 783 | + $result = [ |
|
| 784 | + 'syncToken' => $currentToken, |
|
| 785 | + 'added' => [], |
|
| 786 | + 'modified' => [], |
|
| 787 | + 'deleted' => [], |
|
| 788 | + ]; |
|
| 789 | + |
|
| 790 | + if ($syncToken) { |
|
| 791 | + |
|
| 792 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 793 | + if ($limit>0) { |
|
| 794 | + $query .= " `LIMIT` " . (int)$limit; |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + // Fetching all changes |
|
| 798 | + $stmt = $this->db->prepare($query); |
|
| 799 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 800 | + |
|
| 801 | + $changes = []; |
|
| 802 | + |
|
| 803 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 804 | + // last change on a node is relevant. |
|
| 805 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 806 | + |
|
| 807 | + $changes[$row['uri']] = $row['operation']; |
|
| 808 | + |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + foreach($changes as $uri => $operation) { |
|
| 812 | + |
|
| 813 | + switch($operation) { |
|
| 814 | + case 1: |
|
| 815 | + $result['added'][] = $uri; |
|
| 816 | + break; |
|
| 817 | + case 2: |
|
| 818 | + $result['modified'][] = $uri; |
|
| 819 | + break; |
|
| 820 | + case 3: |
|
| 821 | + $result['deleted'][] = $uri; |
|
| 822 | + break; |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + } |
|
| 826 | + } else { |
|
| 827 | + // No synctoken supplied, this is the initial sync. |
|
| 828 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 829 | + $stmt = $this->db->prepare($query); |
|
| 830 | + $stmt->execute([$addressBookId]); |
|
| 831 | + |
|
| 832 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 833 | + } |
|
| 834 | + return $result; |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + /** |
|
| 838 | + * Adds a change record to the addressbookchanges table. |
|
| 839 | + * |
|
| 840 | + * @param mixed $addressBookId |
|
| 841 | + * @param string $objectUri |
|
| 842 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 843 | + * @return void |
|
| 844 | + */ |
|
| 845 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 846 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 847 | + $stmt = $this->db->prepare($sql); |
|
| 848 | + $stmt->execute([ |
|
| 849 | + $objectUri, |
|
| 850 | + $addressBookId, |
|
| 851 | + $operation, |
|
| 852 | + $addressBookId |
|
| 853 | + ]); |
|
| 854 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 855 | + $stmt->execute([ |
|
| 856 | + $addressBookId |
|
| 857 | + ]); |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + private function readBlob($cardData) { |
|
| 861 | + if (is_resource($cardData)) { |
|
| 862 | + return stream_get_contents($cardData); |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + return $cardData; |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * @param IShareable $shareable |
|
| 870 | + * @param string[] $add |
|
| 871 | + * @param string[] $remove |
|
| 872 | + */ |
|
| 873 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 874 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 875 | + } |
|
| 876 | + |
|
| 877 | + /** |
|
| 878 | + * search contact |
|
| 879 | + * |
|
| 880 | + * @param int $addressBookId |
|
| 881 | + * @param string $pattern which should match within the $searchProperties |
|
| 882 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 883 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 884 | + */ |
|
| 885 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
| 886 | + $query = $this->db->getQueryBuilder(); |
|
| 887 | + $query2 = $this->db->getQueryBuilder(); |
|
| 888 | + |
|
| 889 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 890 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 891 | + $or = $query2->expr()->orX(); |
|
| 892 | + foreach ($searchProperties as $property) { |
|
| 893 | + $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 894 | + } |
|
| 895 | + $query2->andWhere($or); |
|
| 896 | + |
|
| 897 | + // No need for like when the pattern is empty |
|
| 898 | + if ('' !== $pattern) { |
|
| 899 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 903 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 904 | + |
|
| 905 | + $result = $query->execute(); |
|
| 906 | + $cards = $result->fetchAll(); |
|
| 907 | + |
|
| 908 | + $result->closeCursor(); |
|
| 909 | + |
|
| 910 | + return array_map(function($array) { |
|
| 911 | + $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 912 | + return $array; |
|
| 913 | + }, $cards); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * @param int $bookId |
|
| 918 | + * @param string $name |
|
| 919 | + * @return array |
|
| 920 | + */ |
|
| 921 | + public function collectCardProperties($bookId, $name) { |
|
| 922 | + $query = $this->db->getQueryBuilder(); |
|
| 923 | + $result = $query->selectDistinct('value') |
|
| 924 | + ->from($this->dbCardsPropertiesTable) |
|
| 925 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 926 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 927 | + ->execute(); |
|
| 928 | + |
|
| 929 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 930 | + $result->closeCursor(); |
|
| 931 | + |
|
| 932 | + return $all; |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + /** |
|
| 936 | + * get URI from a given contact |
|
| 937 | + * |
|
| 938 | + * @param int $id |
|
| 939 | + * @return string |
|
| 940 | + */ |
|
| 941 | + public function getCardUri($id) { |
|
| 942 | + $query = $this->db->getQueryBuilder(); |
|
| 943 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 944 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 945 | + ->setParameter('id', $id); |
|
| 946 | + |
|
| 947 | + $result = $query->execute(); |
|
| 948 | + $uri = $result->fetch(); |
|
| 949 | + $result->closeCursor(); |
|
| 950 | + |
|
| 951 | + if (!isset($uri['uri'])) { |
|
| 952 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + return $uri['uri']; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + /** |
|
| 959 | + * return contact with the given URI |
|
| 960 | + * |
|
| 961 | + * @param int $addressBookId |
|
| 962 | + * @param string $uri |
|
| 963 | + * @returns array |
|
| 964 | + */ |
|
| 965 | + public function getContact($addressBookId, $uri) { |
|
| 966 | + $result = []; |
|
| 967 | + $query = $this->db->getQueryBuilder(); |
|
| 968 | + $query->select('*')->from($this->dbCardsTable) |
|
| 969 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 970 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 971 | + $queryResult = $query->execute(); |
|
| 972 | + $contact = $queryResult->fetch(); |
|
| 973 | + $queryResult->closeCursor(); |
|
| 974 | + |
|
| 975 | + if (is_array($contact)) { |
|
| 976 | + $result = $contact; |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + return $result; |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * Returns the list of people whom this address book is shared with. |
|
| 984 | + * |
|
| 985 | + * Every element in this array should have the following properties: |
|
| 986 | + * * href - Often a mailto: address |
|
| 987 | + * * commonName - Optional, for example a first + last name |
|
| 988 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 989 | + * * readOnly - boolean |
|
| 990 | + * * summary - Optional, a description for the share |
|
| 991 | + * |
|
| 992 | + * @return array |
|
| 993 | + */ |
|
| 994 | + public function getShares($addressBookId) { |
|
| 995 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 996 | + } |
|
| 997 | + |
|
| 998 | + /** |
|
| 999 | + * update properties table |
|
| 1000 | + * |
|
| 1001 | + * @param int $addressBookId |
|
| 1002 | + * @param string $cardUri |
|
| 1003 | + * @param string $vCardSerialized |
|
| 1004 | + */ |
|
| 1005 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1006 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1007 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1008 | + |
|
| 1009 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1010 | + |
|
| 1011 | + $query = $this->db->getQueryBuilder(); |
|
| 1012 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1013 | + ->values( |
|
| 1014 | + [ |
|
| 1015 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1016 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1017 | + 'name' => $query->createParameter('name'), |
|
| 1018 | + 'value' => $query->createParameter('value'), |
|
| 1019 | + 'preferred' => $query->createParameter('preferred') |
|
| 1020 | + ] |
|
| 1021 | + ); |
|
| 1022 | + |
|
| 1023 | + foreach ($vCard->children() as $property) { |
|
| 1024 | + if(!in_array($property->name, self::$indexProperties)) { |
|
| 1025 | + continue; |
|
| 1026 | + } |
|
| 1027 | + $preferred = 0; |
|
| 1028 | + foreach($property->parameters as $parameter) { |
|
| 1029 | + if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1030 | + $preferred = 1; |
|
| 1031 | + break; |
|
| 1032 | + } |
|
| 1033 | + } |
|
| 1034 | + $query->setParameter('name', $property->name); |
|
| 1035 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1036 | + $query->setParameter('preferred', $preferred); |
|
| 1037 | + $query->execute(); |
|
| 1038 | + } |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + /** |
|
| 1042 | + * read vCard data into a vCard object |
|
| 1043 | + * |
|
| 1044 | + * @param string $cardData |
|
| 1045 | + * @return VCard |
|
| 1046 | + */ |
|
| 1047 | + protected function readCard($cardData) { |
|
| 1048 | + return Reader::read($cardData); |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * delete all properties from a given card |
|
| 1053 | + * |
|
| 1054 | + * @param int $addressBookId |
|
| 1055 | + * @param int $cardId |
|
| 1056 | + */ |
|
| 1057 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1058 | + $query = $this->db->getQueryBuilder(); |
|
| 1059 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1060 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1061 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1062 | + $query->execute(); |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + /** |
|
| 1066 | + * get ID from a given contact |
|
| 1067 | + * |
|
| 1068 | + * @param int $addressBookId |
|
| 1069 | + * @param string $uri |
|
| 1070 | + * @return int |
|
| 1071 | + */ |
|
| 1072 | + protected function getCardId($addressBookId, $uri) { |
|
| 1073 | + $query = $this->db->getQueryBuilder(); |
|
| 1074 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1075 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1076 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1077 | + |
|
| 1078 | + $result = $query->execute(); |
|
| 1079 | + $cardIds = $result->fetch(); |
|
| 1080 | + $result->closeCursor(); |
|
| 1081 | + |
|
| 1082 | + if (!isset($cardIds['id'])) { |
|
| 1083 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + return (int)$cardIds['id']; |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + /** |
|
| 1090 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1091 | + * @param $addressBookId |
|
| 1092 | + * @param $acl |
|
| 1093 | + * @return array |
|
| 1094 | + */ |
|
| 1095 | + public function applyShareAcl($addressBookId, $acl) { |
|
| 1096 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 1100 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1101 | + list(, $name) = \Sabre\Uri\split($principalUri); |
|
| 1102 | + if ($toV2 === true) { |
|
| 1103 | + return "principals/users/$name"; |
|
| 1104 | + } |
|
| 1105 | + return "principals/$name"; |
|
| 1106 | + } |
|
| 1107 | + return $principalUri; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1111 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1112 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1113 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1114 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1115 | + } else { |
|
| 1116 | + $uri = $addressbookInfo['principaluri']; |
|
| 1117 | + } |
|
| 1118 | + |
|
| 1119 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1120 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1121 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1122 | + } |
|
| 1123 | + } |
|
| 1124 | 1124 | } |
@@ -25,52 +25,52 @@ |
||
| 25 | 25 | |
| 26 | 26 | class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome { |
| 27 | 27 | |
| 28 | - /** @var IL10N */ |
|
| 29 | - protected $l10n; |
|
| 28 | + /** @var IL10N */ |
|
| 29 | + protected $l10n; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Returns a list of addressbooks |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - function getChildren() { |
|
| 37 | - if ($this->l10n === null) { |
|
| 38 | - $this->l10n = \OC::$server->getL10N('dav'); |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * Returns a list of addressbooks |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + function getChildren() { |
|
| 37 | + if ($this->l10n === null) { |
|
| 38 | + $this->l10n = \OC::$server->getL10N('dav'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri); |
|
| 42 | - $objects = []; |
|
| 43 | - foreach($addressBooks as $addressBook) { |
|
| 44 | - $objects[] = new AddressBook($this->carddavBackend, $addressBook, $this->l10n); |
|
| 45 | - } |
|
| 46 | - return $objects; |
|
| 41 | + $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri); |
|
| 42 | + $objects = []; |
|
| 43 | + foreach($addressBooks as $addressBook) { |
|
| 44 | + $objects[] = new AddressBook($this->carddavBackend, $addressBook, $this->l10n); |
|
| 45 | + } |
|
| 46 | + return $objects; |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Returns a list of ACE's for this node. |
|
| 52 | - * |
|
| 53 | - * Each ACE has the following properties: |
|
| 54 | - * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
| 55 | - * currently the only supported privileges |
|
| 56 | - * * 'principal', a url to the principal who owns the node |
|
| 57 | - * * 'protected' (optional), indicating that this ACE is not allowed to |
|
| 58 | - * be updated. |
|
| 59 | - * |
|
| 60 | - * @return array |
|
| 61 | - */ |
|
| 62 | - function getACL() { |
|
| 50 | + /** |
|
| 51 | + * Returns a list of ACE's for this node. |
|
| 52 | + * |
|
| 53 | + * Each ACE has the following properties: |
|
| 54 | + * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are |
|
| 55 | + * currently the only supported privileges |
|
| 56 | + * * 'principal', a url to the principal who owns the node |
|
| 57 | + * * 'protected' (optional), indicating that this ACE is not allowed to |
|
| 58 | + * be updated. |
|
| 59 | + * |
|
| 60 | + * @return array |
|
| 61 | + */ |
|
| 62 | + function getACL() { |
|
| 63 | 63 | |
| 64 | - $acl = parent::getACL(); |
|
| 65 | - if ($this->principalUri === 'principals/system/system') { |
|
| 66 | - $acl[] = [ |
|
| 67 | - 'privilege' => '{DAV:}read', |
|
| 68 | - 'principal' => '{DAV:}authenticated', |
|
| 69 | - 'protected' => true, |
|
| 70 | - ]; |
|
| 71 | - } |
|
| 64 | + $acl = parent::getACL(); |
|
| 65 | + if ($this->principalUri === 'principals/system/system') { |
|
| 66 | + $acl[] = [ |
|
| 67 | + 'privilege' => '{DAV:}read', |
|
| 68 | + 'principal' => '{DAV:}authenticated', |
|
| 69 | + 'protected' => true, |
|
| 70 | + ]; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return $acl; |
|
| 74 | - } |
|
| 73 | + return $acl; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | |
| 41 | 41 | $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri); |
| 42 | 42 | $objects = []; |
| 43 | - foreach($addressBooks as $addressBook) { |
|
| 43 | + foreach ($addressBooks as $addressBook) { |
|
| 44 | 44 | $objects[] = new AddressBook($this->carddavBackend, $addressBook, $this->l10n); |
| 45 | 45 | } |
| 46 | 46 | return $objects; |
@@ -28,15 +28,15 @@ |
||
| 28 | 28 | |
| 29 | 29 | class SyncJob extends TimedJob { |
| 30 | 30 | |
| 31 | - public function __construct() { |
|
| 32 | - // Run once a day |
|
| 33 | - $this->setInterval(24 * 60 * 60); |
|
| 34 | - } |
|
| 31 | + public function __construct() { |
|
| 32 | + // Run once a day |
|
| 33 | + $this->setInterval(24 * 60 * 60); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - protected function run($argument) { |
|
| 37 | - $app = new Application(); |
|
| 38 | - /** @var SyncService $ss */ |
|
| 39 | - $ss = $app->getSyncService(); |
|
| 40 | - $ss->syncInstance(); |
|
| 41 | - } |
|
| 36 | + protected function run($argument) { |
|
| 37 | + $app = new Application(); |
|
| 38 | + /** @var SyncService $ss */ |
|
| 39 | + $ss = $app->getSyncService(); |
|
| 40 | + $ss->syncInstance(); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -32,115 +32,115 @@ |
||
| 32 | 32 | |
| 33 | 33 | class HookManager { |
| 34 | 34 | |
| 35 | - /** @var IUserManager */ |
|
| 36 | - private $userManager; |
|
| 37 | - |
|
| 38 | - /** @var SyncService */ |
|
| 39 | - private $syncService; |
|
| 40 | - |
|
| 41 | - /** @var IUser[] */ |
|
| 42 | - private $usersToDelete; |
|
| 43 | - |
|
| 44 | - /** @var CalDavBackend */ |
|
| 45 | - private $calDav; |
|
| 46 | - |
|
| 47 | - /** @var CardDavBackend */ |
|
| 48 | - private $cardDav; |
|
| 49 | - |
|
| 50 | - /** @var array */ |
|
| 51 | - private $calendarsToDelete; |
|
| 52 | - |
|
| 53 | - /** @var array */ |
|
| 54 | - private $addressBooksToDelete; |
|
| 55 | - |
|
| 56 | - /** @var EventDispatcher */ |
|
| 57 | - private $eventDispatcher; |
|
| 58 | - |
|
| 59 | - public function __construct(IUserManager $userManager, |
|
| 60 | - SyncService $syncService, |
|
| 61 | - CalDavBackend $calDav, |
|
| 62 | - CardDavBackend $cardDav, |
|
| 63 | - EventDispatcher $eventDispatcher) { |
|
| 64 | - $this->userManager = $userManager; |
|
| 65 | - $this->syncService = $syncService; |
|
| 66 | - $this->calDav = $calDav; |
|
| 67 | - $this->cardDav = $cardDav; |
|
| 68 | - $this->eventDispatcher = $eventDispatcher; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function setup() { |
|
| 72 | - Util::connectHook('OC_User', |
|
| 73 | - 'post_createUser', |
|
| 74 | - $this, |
|
| 75 | - 'postCreateUser'); |
|
| 76 | - Util::connectHook('OC_User', |
|
| 77 | - 'pre_deleteUser', |
|
| 78 | - $this, |
|
| 79 | - 'preDeleteUser'); |
|
| 80 | - Util::connectHook('OC_User', |
|
| 81 | - 'post_deleteUser', |
|
| 82 | - $this, |
|
| 83 | - 'postDeleteUser'); |
|
| 84 | - Util::connectHook('OC_User', |
|
| 85 | - 'changeUser', |
|
| 86 | - $this, |
|
| 87 | - 'changeUser'); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function postCreateUser($params) { |
|
| 91 | - $user = $this->userManager->get($params['uid']); |
|
| 92 | - $this->syncService->updateUser($user); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function preDeleteUser($params) { |
|
| 96 | - $uid = $params['uid']; |
|
| 97 | - $this->usersToDelete[$uid] = $this->userManager->get($uid); |
|
| 98 | - $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | - $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function postDeleteUser($params) { |
|
| 103 | - $uid = $params['uid']; |
|
| 104 | - if (isset($this->usersToDelete[$uid])){ |
|
| 105 | - $this->syncService->deleteUser($this->usersToDelete[$uid]); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - foreach ($this->calendarsToDelete as $calendar) { |
|
| 109 | - $this->calDav->deleteCalendar($calendar['id']); |
|
| 110 | - } |
|
| 111 | - $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 112 | - |
|
| 113 | - foreach ($this->addressBooksToDelete as $addressBook) { |
|
| 114 | - $this->cardDav->deleteAddressBook($addressBook['id']); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function changeUser($params) { |
|
| 119 | - $user = $params['user']; |
|
| 120 | - $this->syncService->updateUser($user); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - public function firstLogin(IUser $user = null) { |
|
| 124 | - if (!is_null($user)) { |
|
| 125 | - $principal = 'principals/users/' . $user->getUID(); |
|
| 126 | - if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
|
| 127 | - try { |
|
| 128 | - $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |
|
| 129 | - '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME, |
|
| 130 | - ]); |
|
| 131 | - } catch (\Exception $ex) { |
|
| 132 | - \OC::$server->getLogger()->logException($ex); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) { |
|
| 136 | - try { |
|
| 137 | - $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [ |
|
| 138 | - '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME, |
|
| 139 | - ]); |
|
| 140 | - } catch (\Exception $ex) { |
|
| 141 | - \OC::$server->getLogger()->logException($ex); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 35 | + /** @var IUserManager */ |
|
| 36 | + private $userManager; |
|
| 37 | + |
|
| 38 | + /** @var SyncService */ |
|
| 39 | + private $syncService; |
|
| 40 | + |
|
| 41 | + /** @var IUser[] */ |
|
| 42 | + private $usersToDelete; |
|
| 43 | + |
|
| 44 | + /** @var CalDavBackend */ |
|
| 45 | + private $calDav; |
|
| 46 | + |
|
| 47 | + /** @var CardDavBackend */ |
|
| 48 | + private $cardDav; |
|
| 49 | + |
|
| 50 | + /** @var array */ |
|
| 51 | + private $calendarsToDelete; |
|
| 52 | + |
|
| 53 | + /** @var array */ |
|
| 54 | + private $addressBooksToDelete; |
|
| 55 | + |
|
| 56 | + /** @var EventDispatcher */ |
|
| 57 | + private $eventDispatcher; |
|
| 58 | + |
|
| 59 | + public function __construct(IUserManager $userManager, |
|
| 60 | + SyncService $syncService, |
|
| 61 | + CalDavBackend $calDav, |
|
| 62 | + CardDavBackend $cardDav, |
|
| 63 | + EventDispatcher $eventDispatcher) { |
|
| 64 | + $this->userManager = $userManager; |
|
| 65 | + $this->syncService = $syncService; |
|
| 66 | + $this->calDav = $calDav; |
|
| 67 | + $this->cardDav = $cardDav; |
|
| 68 | + $this->eventDispatcher = $eventDispatcher; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function setup() { |
|
| 72 | + Util::connectHook('OC_User', |
|
| 73 | + 'post_createUser', |
|
| 74 | + $this, |
|
| 75 | + 'postCreateUser'); |
|
| 76 | + Util::connectHook('OC_User', |
|
| 77 | + 'pre_deleteUser', |
|
| 78 | + $this, |
|
| 79 | + 'preDeleteUser'); |
|
| 80 | + Util::connectHook('OC_User', |
|
| 81 | + 'post_deleteUser', |
|
| 82 | + $this, |
|
| 83 | + 'postDeleteUser'); |
|
| 84 | + Util::connectHook('OC_User', |
|
| 85 | + 'changeUser', |
|
| 86 | + $this, |
|
| 87 | + 'changeUser'); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function postCreateUser($params) { |
|
| 91 | + $user = $this->userManager->get($params['uid']); |
|
| 92 | + $this->syncService->updateUser($user); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function preDeleteUser($params) { |
|
| 96 | + $uid = $params['uid']; |
|
| 97 | + $this->usersToDelete[$uid] = $this->userManager->get($uid); |
|
| 98 | + $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | + $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function postDeleteUser($params) { |
|
| 103 | + $uid = $params['uid']; |
|
| 104 | + if (isset($this->usersToDelete[$uid])){ |
|
| 105 | + $this->syncService->deleteUser($this->usersToDelete[$uid]); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + foreach ($this->calendarsToDelete as $calendar) { |
|
| 109 | + $this->calDav->deleteCalendar($calendar['id']); |
|
| 110 | + } |
|
| 111 | + $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 112 | + |
|
| 113 | + foreach ($this->addressBooksToDelete as $addressBook) { |
|
| 114 | + $this->cardDav->deleteAddressBook($addressBook['id']); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function changeUser($params) { |
|
| 119 | + $user = $params['user']; |
|
| 120 | + $this->syncService->updateUser($user); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + public function firstLogin(IUser $user = null) { |
|
| 124 | + if (!is_null($user)) { |
|
| 125 | + $principal = 'principals/users/' . $user->getUID(); |
|
| 126 | + if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
|
| 127 | + try { |
|
| 128 | + $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |
|
| 129 | + '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME, |
|
| 130 | + ]); |
|
| 131 | + } catch (\Exception $ex) { |
|
| 132 | + \OC::$server->getLogger()->logException($ex); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) { |
|
| 136 | + try { |
|
| 137 | + $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [ |
|
| 138 | + '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME, |
|
| 139 | + ]); |
|
| 140 | + } catch (\Exception $ex) { |
|
| 141 | + \OC::$server->getLogger()->logException($ex); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -95,20 +95,20 @@ discard block |
||
| 95 | 95 | public function preDeleteUser($params) { |
| 96 | 96 | $uid = $params['uid']; |
| 97 | 97 | $this->usersToDelete[$uid] = $this->userManager->get($uid); |
| 98 | - $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid); |
|
| 99 | - $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid); |
|
| 98 | + $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/'.$uid); |
|
| 99 | + $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/'.$uid); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | public function postDeleteUser($params) { |
| 103 | 103 | $uid = $params['uid']; |
| 104 | - if (isset($this->usersToDelete[$uid])){ |
|
| 104 | + if (isset($this->usersToDelete[$uid])) { |
|
| 105 | 105 | $this->syncService->deleteUser($this->usersToDelete[$uid]); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | foreach ($this->calendarsToDelete as $calendar) { |
| 109 | 109 | $this->calDav->deleteCalendar($calendar['id']); |
| 110 | 110 | } |
| 111 | - $this->calDav->deleteAllSharesByUser('principals/users/' . $uid); |
|
| 111 | + $this->calDav->deleteAllSharesByUser('principals/users/'.$uid); |
|
| 112 | 112 | |
| 113 | 113 | foreach ($this->addressBooksToDelete as $addressBook) { |
| 114 | 114 | $this->cardDav->deleteAddressBook($addressBook['id']); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | public function firstLogin(IUser $user = null) { |
| 124 | 124 | if (!is_null($user)) { |
| 125 | - $principal = 'principals/users/' . $user->getUID(); |
|
| 125 | + $principal = 'principals/users/'.$user->getUID(); |
|
| 126 | 126 | if ($this->calDav->getCalendarsForUserCount($principal) === 0) { |
| 127 | 127 | try { |
| 128 | 128 | $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [ |
@@ -39,136 +39,136 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class SystemTagNode implements \Sabre\DAV\INode { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @var ISystemTag |
|
| 44 | - */ |
|
| 45 | - protected $tag; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var ISystemTagManager |
|
| 49 | - */ |
|
| 50 | - protected $tagManager; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * User |
|
| 54 | - * |
|
| 55 | - * @var IUser |
|
| 56 | - */ |
|
| 57 | - protected $user; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Whether to allow permissions for admins |
|
| 61 | - * |
|
| 62 | - * @var bool |
|
| 63 | - */ |
|
| 64 | - protected $isAdmin; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Sets up the node, expects a full path name |
|
| 68 | - * |
|
| 69 | - * @param ISystemTag $tag system tag |
|
| 70 | - * @param IUser $user user |
|
| 71 | - * @param bool $isAdmin whether to allow operations for admins |
|
| 72 | - * @param ISystemTagManager $tagManager tag manager |
|
| 73 | - */ |
|
| 74 | - public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) { |
|
| 75 | - $this->tag = $tag; |
|
| 76 | - $this->user = $user; |
|
| 77 | - $this->isAdmin = $isAdmin; |
|
| 78 | - $this->tagManager = $tagManager; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Returns the id of the tag |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function getName() { |
|
| 87 | - return $this->tag->getId(); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Returns the system tag represented by this node |
|
| 92 | - * |
|
| 93 | - * @return ISystemTag system tag |
|
| 94 | - */ |
|
| 95 | - public function getSystemTag() { |
|
| 96 | - return $this->tag; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Renames the node |
|
| 101 | - * |
|
| 102 | - * @param string $name The new name |
|
| 103 | - * |
|
| 104 | - * @throws MethodNotAllowed not allowed to rename node |
|
| 105 | - */ |
|
| 106 | - public function setName($name) { |
|
| 107 | - throw new MethodNotAllowed(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Update tag |
|
| 112 | - * |
|
| 113 | - * @param string $name new tag name |
|
| 114 | - * @param bool $userVisible user visible |
|
| 115 | - * @param bool $userAssignable user assignable |
|
| 116 | - * @throws NotFound whenever the given tag id does not exist |
|
| 117 | - * @throws Forbidden whenever there is no permission to update said tag |
|
| 118 | - * @throws Conflict whenever a tag already exists with the given attributes |
|
| 119 | - */ |
|
| 120 | - public function update($name, $userVisible, $userAssignable) { |
|
| 121 | - try { |
|
| 122 | - if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 123 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 124 | - } |
|
| 125 | - if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
|
| 126 | - throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // only admin is able to change permissions, regular users can only rename |
|
| 130 | - if (!$this->isAdmin) { |
|
| 131 | - // only renaming is allowed for regular users |
|
| 132 | - if ($userVisible !== $this->tag->isUserVisible() |
|
| 133 | - || $userAssignable !== $this->tag->isUserAssignable() |
|
| 134 | - ) { |
|
| 135 | - throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
|
| 140 | - } catch (TagNotFoundException $e) { |
|
| 141 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 142 | - } catch (TagAlreadyExistsException $e) { |
|
| 143 | - throw new Conflict( |
|
| 144 | - 'Tag with the properties "' . $name . '", ' . |
|
| 145 | - $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 146 | - ); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Returns null, not supported |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - public function getLastModified() { |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function delete() { |
|
| 159 | - try { |
|
| 160 | - if (!$this->isAdmin) { |
|
| 161 | - throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 165 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $this->tagManager->deleteTags($this->tag->getId()); |
|
| 169 | - } catch (TagNotFoundException $e) { |
|
| 170 | - // can happen if concurrent deletion occurred |
|
| 171 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 172 | - } |
|
| 173 | - } |
|
| 42 | + /** |
|
| 43 | + * @var ISystemTag |
|
| 44 | + */ |
|
| 45 | + protected $tag; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var ISystemTagManager |
|
| 49 | + */ |
|
| 50 | + protected $tagManager; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * User |
|
| 54 | + * |
|
| 55 | + * @var IUser |
|
| 56 | + */ |
|
| 57 | + protected $user; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Whether to allow permissions for admins |
|
| 61 | + * |
|
| 62 | + * @var bool |
|
| 63 | + */ |
|
| 64 | + protected $isAdmin; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Sets up the node, expects a full path name |
|
| 68 | + * |
|
| 69 | + * @param ISystemTag $tag system tag |
|
| 70 | + * @param IUser $user user |
|
| 71 | + * @param bool $isAdmin whether to allow operations for admins |
|
| 72 | + * @param ISystemTagManager $tagManager tag manager |
|
| 73 | + */ |
|
| 74 | + public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) { |
|
| 75 | + $this->tag = $tag; |
|
| 76 | + $this->user = $user; |
|
| 77 | + $this->isAdmin = $isAdmin; |
|
| 78 | + $this->tagManager = $tagManager; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Returns the id of the tag |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function getName() { |
|
| 87 | + return $this->tag->getId(); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Returns the system tag represented by this node |
|
| 92 | + * |
|
| 93 | + * @return ISystemTag system tag |
|
| 94 | + */ |
|
| 95 | + public function getSystemTag() { |
|
| 96 | + return $this->tag; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Renames the node |
|
| 101 | + * |
|
| 102 | + * @param string $name The new name |
|
| 103 | + * |
|
| 104 | + * @throws MethodNotAllowed not allowed to rename node |
|
| 105 | + */ |
|
| 106 | + public function setName($name) { |
|
| 107 | + throw new MethodNotAllowed(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Update tag |
|
| 112 | + * |
|
| 113 | + * @param string $name new tag name |
|
| 114 | + * @param bool $userVisible user visible |
|
| 115 | + * @param bool $userAssignable user assignable |
|
| 116 | + * @throws NotFound whenever the given tag id does not exist |
|
| 117 | + * @throws Forbidden whenever there is no permission to update said tag |
|
| 118 | + * @throws Conflict whenever a tag already exists with the given attributes |
|
| 119 | + */ |
|
| 120 | + public function update($name, $userVisible, $userAssignable) { |
|
| 121 | + try { |
|
| 122 | + if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 123 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 124 | + } |
|
| 125 | + if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
|
| 126 | + throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // only admin is able to change permissions, regular users can only rename |
|
| 130 | + if (!$this->isAdmin) { |
|
| 131 | + // only renaming is allowed for regular users |
|
| 132 | + if ($userVisible !== $this->tag->isUserVisible() |
|
| 133 | + || $userAssignable !== $this->tag->isUserAssignable() |
|
| 134 | + ) { |
|
| 135 | + throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
|
| 140 | + } catch (TagNotFoundException $e) { |
|
| 141 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 142 | + } catch (TagAlreadyExistsException $e) { |
|
| 143 | + throw new Conflict( |
|
| 144 | + 'Tag with the properties "' . $name . '", ' . |
|
| 145 | + $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 146 | + ); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Returns null, not supported |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + public function getLastModified() { |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function delete() { |
|
| 159 | + try { |
|
| 160 | + if (!$this->isAdmin) { |
|
| 161 | + throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 165 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $this->tagManager->deleteTags($this->tag->getId()); |
|
| 169 | + } catch (TagNotFoundException $e) { |
|
| 170 | + // can happen if concurrent deletion occurred |
|
| 171 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -120,10 +120,10 @@ discard block |
||
| 120 | 120 | public function update($name, $userVisible, $userAssignable) { |
| 121 | 121 | try { |
| 122 | 122 | if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
| 123 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 123 | + throw new NotFound('Tag with id '.$this->tag->getId().' does not exist'); |
|
| 124 | 124 | } |
| 125 | 125 | if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
| 126 | - throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 126 | + throw new Forbidden('No permission to update tag '.$this->tag->getId()); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // only admin is able to change permissions, regular users can only rename |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | if ($userVisible !== $this->tag->isUserVisible() |
| 133 | 133 | || $userAssignable !== $this->tag->isUserAssignable() |
| 134 | 134 | ) { |
| 135 | - throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 135 | + throw new Forbidden('No permission to update permissions for tag '.$this->tag->getId()); |
|
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
| 140 | 140 | } catch (TagNotFoundException $e) { |
| 141 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 141 | + throw new NotFound('Tag with id '.$this->tag->getId().' does not exist'); |
|
| 142 | 142 | } catch (TagAlreadyExistsException $e) { |
| 143 | 143 | throw new Conflict( |
| 144 | - 'Tag with the properties "' . $name . '", ' . |
|
| 145 | - $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 144 | + 'Tag with the properties "'.$name.'", '. |
|
| 145 | + $userVisible.', '.$userAssignable.' already exists' |
|
| 146 | 146 | ); |
| 147 | 147 | } |
| 148 | 148 | } |
@@ -158,17 +158,17 @@ discard block |
||
| 158 | 158 | public function delete() { |
| 159 | 159 | try { |
| 160 | 160 | if (!$this->isAdmin) { |
| 161 | - throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 161 | + throw new Forbidden('No permission to delete tag '.$this->tag->getId()); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
| 165 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 165 | + throw new NotFound('Tag with id '.$this->tag->getId().' not found'); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | $this->tagManager->deleteTags($this->tag->getId()); |
| 169 | 169 | } catch (TagNotFoundException $e) { |
| 170 | 170 | // can happen if concurrent deletion occurred |
| 171 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 171 | + throw new NotFound('Tag with id '.$this->tag->getId().' not found', 0, $e); |
|
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -37,144 +37,144 @@ |
||
| 37 | 37 | |
| 38 | 38 | class SystemTagsByIdCollection implements ICollection { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @var ISystemTagManager |
|
| 42 | - */ |
|
| 43 | - private $tagManager; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var IGroupManager |
|
| 47 | - */ |
|
| 48 | - private $groupManager; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var IUserSession |
|
| 52 | - */ |
|
| 53 | - private $userSession; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * SystemTagsByIdCollection constructor. |
|
| 57 | - * |
|
| 58 | - * @param ISystemTagManager $tagManager |
|
| 59 | - * @param IUserSession $userSession |
|
| 60 | - * @param IGroupManager $groupManager |
|
| 61 | - */ |
|
| 62 | - public function __construct( |
|
| 63 | - ISystemTagManager $tagManager, |
|
| 64 | - IUserSession $userSession, |
|
| 65 | - IGroupManager $groupManager |
|
| 66 | - ) { |
|
| 67 | - $this->tagManager = $tagManager; |
|
| 68 | - $this->userSession = $userSession; |
|
| 69 | - $this->groupManager = $groupManager; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Returns whether the currently logged in user is an administrator |
|
| 74 | - * |
|
| 75 | - * @return bool true if the user is an admin |
|
| 76 | - */ |
|
| 77 | - private function isAdmin() { |
|
| 78 | - $user = $this->userSession->getUser(); |
|
| 79 | - if ($user !== null) { |
|
| 80 | - return $this->groupManager->isAdmin($user->getUID()); |
|
| 81 | - } |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $name |
|
| 87 | - * @param resource|string $data Initial payload |
|
| 88 | - * @throws Forbidden |
|
| 89 | - */ |
|
| 90 | - function createFile($name, $data = null) { |
|
| 91 | - throw new Forbidden('Cannot create tags by id'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $name |
|
| 96 | - */ |
|
| 97 | - function createDirectory($name) { |
|
| 98 | - throw new Forbidden('Permission denied to create collections'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param string $name |
|
| 103 | - */ |
|
| 104 | - function getChild($name) { |
|
| 105 | - try { |
|
| 106 | - $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 107 | - $tag = current($tag); |
|
| 108 | - if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 109 | - throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 110 | - } |
|
| 111 | - return $this->makeNode($tag); |
|
| 112 | - } catch (\InvalidArgumentException $e) { |
|
| 113 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 114 | - } catch (TagNotFoundException $e) { |
|
| 115 | - throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - function getChildren() { |
|
| 120 | - $visibilityFilter = true; |
|
| 121 | - if ($this->isAdmin()) { |
|
| 122 | - $visibilityFilter = null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $tags = $this->tagManager->getAllTags($visibilityFilter); |
|
| 126 | - return array_map(function($tag) { |
|
| 127 | - return $this->makeNode($tag); |
|
| 128 | - }, $tags); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param string $name |
|
| 133 | - */ |
|
| 134 | - function childExists($name) { |
|
| 135 | - try { |
|
| 136 | - $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 137 | - $tag = current($tag); |
|
| 138 | - if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - return true; |
|
| 142 | - } catch (\InvalidArgumentException $e) { |
|
| 143 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 144 | - } catch (TagNotFoundException $e) { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - function delete() { |
|
| 150 | - throw new Forbidden('Permission denied to delete this collection'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - function getName() { |
|
| 154 | - return 'systemtags'; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - function setName($name) { |
|
| 158 | - throw new Forbidden('Permission denied to rename this collection'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the last modification time, as a unix timestamp |
|
| 163 | - * |
|
| 164 | - * @return int |
|
| 165 | - */ |
|
| 166 | - function getLastModified() { |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Create a sabre node for the given system tag |
|
| 172 | - * |
|
| 173 | - * @param ISystemTag $tag |
|
| 174 | - * |
|
| 175 | - * @return SystemTagNode |
|
| 176 | - */ |
|
| 177 | - private function makeNode(ISystemTag $tag) { |
|
| 178 | - return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager); |
|
| 179 | - } |
|
| 40 | + /** |
|
| 41 | + * @var ISystemTagManager |
|
| 42 | + */ |
|
| 43 | + private $tagManager; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var IGroupManager |
|
| 47 | + */ |
|
| 48 | + private $groupManager; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var IUserSession |
|
| 52 | + */ |
|
| 53 | + private $userSession; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * SystemTagsByIdCollection constructor. |
|
| 57 | + * |
|
| 58 | + * @param ISystemTagManager $tagManager |
|
| 59 | + * @param IUserSession $userSession |
|
| 60 | + * @param IGroupManager $groupManager |
|
| 61 | + */ |
|
| 62 | + public function __construct( |
|
| 63 | + ISystemTagManager $tagManager, |
|
| 64 | + IUserSession $userSession, |
|
| 65 | + IGroupManager $groupManager |
|
| 66 | + ) { |
|
| 67 | + $this->tagManager = $tagManager; |
|
| 68 | + $this->userSession = $userSession; |
|
| 69 | + $this->groupManager = $groupManager; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Returns whether the currently logged in user is an administrator |
|
| 74 | + * |
|
| 75 | + * @return bool true if the user is an admin |
|
| 76 | + */ |
|
| 77 | + private function isAdmin() { |
|
| 78 | + $user = $this->userSession->getUser(); |
|
| 79 | + if ($user !== null) { |
|
| 80 | + return $this->groupManager->isAdmin($user->getUID()); |
|
| 81 | + } |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $name |
|
| 87 | + * @param resource|string $data Initial payload |
|
| 88 | + * @throws Forbidden |
|
| 89 | + */ |
|
| 90 | + function createFile($name, $data = null) { |
|
| 91 | + throw new Forbidden('Cannot create tags by id'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $name |
|
| 96 | + */ |
|
| 97 | + function createDirectory($name) { |
|
| 98 | + throw new Forbidden('Permission denied to create collections'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param string $name |
|
| 103 | + */ |
|
| 104 | + function getChild($name) { |
|
| 105 | + try { |
|
| 106 | + $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 107 | + $tag = current($tag); |
|
| 108 | + if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 109 | + throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 110 | + } |
|
| 111 | + return $this->makeNode($tag); |
|
| 112 | + } catch (\InvalidArgumentException $e) { |
|
| 113 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 114 | + } catch (TagNotFoundException $e) { |
|
| 115 | + throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + function getChildren() { |
|
| 120 | + $visibilityFilter = true; |
|
| 121 | + if ($this->isAdmin()) { |
|
| 122 | + $visibilityFilter = null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $tags = $this->tagManager->getAllTags($visibilityFilter); |
|
| 126 | + return array_map(function($tag) { |
|
| 127 | + return $this->makeNode($tag); |
|
| 128 | + }, $tags); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param string $name |
|
| 133 | + */ |
|
| 134 | + function childExists($name) { |
|
| 135 | + try { |
|
| 136 | + $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 137 | + $tag = current($tag); |
|
| 138 | + if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + return true; |
|
| 142 | + } catch (\InvalidArgumentException $e) { |
|
| 143 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 144 | + } catch (TagNotFoundException $e) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + function delete() { |
|
| 150 | + throw new Forbidden('Permission denied to delete this collection'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + function getName() { |
|
| 154 | + return 'systemtags'; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + function setName($name) { |
|
| 158 | + throw new Forbidden('Permission denied to rename this collection'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the last modification time, as a unix timestamp |
|
| 163 | + * |
|
| 164 | + * @return int |
|
| 165 | + */ |
|
| 166 | + function getLastModified() { |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Create a sabre node for the given system tag |
|
| 172 | + * |
|
| 173 | + * @param ISystemTag $tag |
|
| 174 | + * |
|
| 175 | + * @return SystemTagNode |
|
| 176 | + */ |
|
| 177 | + private function makeNode(ISystemTag $tag) { |
|
| 178 | + return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager); |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -106,13 +106,13 @@ |
||
| 106 | 106 | $tag = $this->tagManager->getTagsByIds([$name]); |
| 107 | 107 | $tag = current($tag); |
| 108 | 108 | if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
| 109 | - throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 109 | + throw new NotFound('Tag with id '.$name.' not found'); |
|
| 110 | 110 | } |
| 111 | 111 | return $this->makeNode($tag); |
| 112 | 112 | } catch (\InvalidArgumentException $e) { |
| 113 | 113 | throw new BadRequest('Invalid tag id', 0, $e); |
| 114 | 114 | } catch (TagNotFoundException $e) { |
| 115 | - throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 115 | + throw new NotFound('Tag with id '.$name.' not found', 0, $e); |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |