1
0

AddressBookImpl.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CardDAV;
  8. use OCP\Constants;
  9. use OCP\IAddressBook;
  10. use OCP\IURLGenerator;
  11. use Sabre\VObject\Component\VCard;
  12. use Sabre\VObject\Property;
  13. use Sabre\VObject\Reader;
  14. use Sabre\VObject\UUIDUtil;
  15. class AddressBookImpl implements IAddressBook {
  16. /**
  17. * AddressBookImpl constructor.
  18. *
  19. * @param AddressBook $addressBook
  20. * @param array $addressBookInfo
  21. * @param CardDavBackend $backend
  22. * @param IUrlGenerator $urlGenerator
  23. */
  24. public function __construct(
  25. private AddressBook $addressBook,
  26. private array $addressBookInfo,
  27. private CardDavBackend $backend,
  28. private IURLGenerator $urlGenerator,
  29. ) {
  30. }
  31. /**
  32. * @return string defining the technical unique key
  33. * @since 5.0.0
  34. */
  35. public function getKey() {
  36. return $this->addressBookInfo['id'];
  37. }
  38. /**
  39. * @return string defining the unique uri
  40. * @since 16.0.0
  41. */
  42. public function getUri(): string {
  43. return $this->addressBookInfo['uri'];
  44. }
  45. /**
  46. * In comparison to getKey() this function returns a human readable (maybe translated) name
  47. *
  48. * @return mixed
  49. * @since 5.0.0
  50. */
  51. public function getDisplayName() {
  52. return $this->addressBookInfo['{DAV:}displayname'];
  53. }
  54. /**
  55. * @param string $pattern which should match within the $searchProperties
  56. * @param array $searchProperties defines the properties within the query pattern should match
  57. * @param array $options Options to define the output format and search behavior
  58. * - 'types' boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array
  59. * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => 'g@h.i']]
  60. * - 'escape_like_param' - If set to false wildcards _ and % are not escaped
  61. * - 'limit' - Set a numeric limit for the search results
  62. * - 'offset' - Set the offset for the limited search results
  63. * - 'wildcard' - Whether the search should use wildcards
  64. * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options
  65. * @return array an array of contacts which are arrays of key-value-pairs
  66. * example result:
  67. * [
  68. * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => 'a@b.c', 'GEO' => '37.386013;-122.082932'],
  69. * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['d@e.f', 'g@h.i']]
  70. * ]
  71. * @since 5.0.0
  72. */
  73. public function search($pattern, $searchProperties, $options) {
  74. $results = $this->backend->search($this->getKey(), $pattern, $searchProperties, $options);
  75. $withTypes = \array_key_exists('types', $options) && $options['types'] === true;
  76. $vCards = [];
  77. foreach ($results as $result) {
  78. $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes);
  79. }
  80. return $vCards;
  81. }
  82. /**
  83. * @param array $properties this array if key-value-pairs defines a contact
  84. * @return array an array representing the contact just created or updated
  85. * @since 5.0.0
  86. */
  87. public function createOrUpdate($properties) {
  88. $update = false;
  89. if (!isset($properties['URI'])) { // create a new contact
  90. $uid = $this->createUid();
  91. $uri = $uid . '.vcf';
  92. $vCard = $this->createEmptyVCard($uid);
  93. } else { // update existing contact
  94. $uri = $properties['URI'];
  95. $vCardData = $this->backend->getCard($this->getKey(), $uri);
  96. $vCard = $this->readCard($vCardData['carddata']);
  97. $update = true;
  98. }
  99. foreach ($properties as $key => $value) {
  100. if (is_array($value)) {
  101. $vCard->remove($key);
  102. foreach ($value as $entry) {
  103. if (is_string($entry)) {
  104. $property = $vCard->createProperty($key, $entry);
  105. } else {
  106. if (($key === 'ADR' || $key === 'PHOTO') && is_string($entry['value'])) {
  107. $entry['value'] = stripslashes($entry['value']);
  108. $entry['value'] = explode(';', $entry['value']);
  109. }
  110. $property = $vCard->createProperty($key, $entry['value']);
  111. if (isset($entry['type'])) {
  112. $property->add('TYPE', $entry['type']);
  113. }
  114. }
  115. $vCard->add($property);
  116. }
  117. } elseif ($key !== 'URI') {
  118. $vCard->$key = $vCard->createProperty($key, $value);
  119. }
  120. }
  121. if ($update) {
  122. $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize());
  123. } else {
  124. $this->backend->createCard($this->getKey(), $uri, $vCard->serialize());
  125. }
  126. return $this->vCard2Array($uri, $vCard);
  127. }
  128. /**
  129. * @return mixed
  130. * @since 5.0.0
  131. */
  132. public function getPermissions() {
  133. $permissions = $this->addressBook->getACL();
  134. $result = 0;
  135. foreach ($permissions as $permission) {
  136. switch ($permission['privilege']) {
  137. case '{DAV:}read':
  138. $result |= Constants::PERMISSION_READ;
  139. break;
  140. case '{DAV:}write':
  141. $result |= Constants::PERMISSION_CREATE;
  142. $result |= Constants::PERMISSION_UPDATE;
  143. break;
  144. case '{DAV:}all':
  145. $result |= Constants::PERMISSION_ALL;
  146. break;
  147. }
  148. }
  149. return $result;
  150. }
  151. /**
  152. * @param int $id the unique identifier to a contact
  153. * @return bool successful or not
  154. * @since 5.0.0
  155. */
  156. public function delete($id) {
  157. $uri = $this->backend->getCardUri($id);
  158. return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
  159. }
  160. /**
  161. * read vCard data into a vCard object
  162. *
  163. * @param string $cardData
  164. * @return VCard
  165. */
  166. protected function readCard($cardData) {
  167. return Reader::read($cardData);
  168. }
  169. /**
  170. * create UID for contact
  171. *
  172. * @return string
  173. */
  174. protected function createUid() {
  175. do {
  176. $uid = $this->getUid();
  177. $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
  178. } while (!empty($contact));
  179. return $uid;
  180. }
  181. /**
  182. * getUid is only there for testing, use createUid instead
  183. */
  184. protected function getUid() {
  185. return UUIDUtil::getUUID();
  186. }
  187. /**
  188. * create empty vcard
  189. *
  190. * @param string $uid
  191. * @return VCard
  192. */
  193. protected function createEmptyVCard($uid) {
  194. $vCard = new VCard();
  195. $vCard->UID = $uid;
  196. return $vCard;
  197. }
  198. /**
  199. * create array with all vCard properties
  200. *
  201. * @param string $uri
  202. * @param VCard $vCard
  203. * @param boolean $withTypes (optional) return the values as arrays of value/type pairs
  204. * @return array
  205. */
  206. protected function vCard2Array($uri, VCard $vCard, $withTypes = false) {
  207. $result = [
  208. 'URI' => $uri,
  209. ];
  210. foreach ($vCard->children() as $property) {
  211. if ($property->name === 'PHOTO' && in_array($property->getValueType(), ['BINARY', 'URI'])) {
  212. $url = $this->urlGenerator->getAbsoluteURL(
  213. $this->urlGenerator->linkTo('', 'remote.php') . '/dav/');
  214. $url .= implode('/', [
  215. 'addressbooks',
  216. substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/'
  217. $this->addressBookInfo['uri'],
  218. $uri
  219. ]) . '?photo';
  220. $result['PHOTO'] = 'VALUE=uri:' . $url;
  221. } elseif (in_array($property->name, ['URL', 'GEO', 'CLOUD', 'ADR', 'EMAIL', 'IMPP', 'TEL', 'X-SOCIALPROFILE', 'RELATED', 'LANG', 'X-ADDRESSBOOKSERVER-MEMBER'])) {
  222. if (!isset($result[$property->name])) {
  223. $result[$property->name] = [];
  224. }
  225. $type = $this->getTypeFromProperty($property);
  226. if ($withTypes) {
  227. $result[$property->name][] = [
  228. 'type' => $type,
  229. 'value' => $property->getValue()
  230. ];
  231. } else {
  232. $result[$property->name][] = $property->getValue();
  233. }
  234. } else {
  235. $result[$property->name] = $property->getValue();
  236. }
  237. }
  238. if ($this->isSystemAddressBook()) {
  239. $result['isLocalSystemBook'] = true;
  240. }
  241. return $result;
  242. }
  243. /**
  244. * Get the type of the current property
  245. *
  246. * @param Property $property
  247. * @return null|string
  248. */
  249. protected function getTypeFromProperty(Property $property) {
  250. $parameters = $property->parameters();
  251. // Type is the social network, when it's empty we don't need this.
  252. if (isset($parameters['TYPE'])) {
  253. /** @var \Sabre\VObject\Parameter $type */
  254. $type = $parameters['TYPE'];
  255. return $type->getValue();
  256. }
  257. return null;
  258. }
  259. /**
  260. * @inheritDoc
  261. */
  262. public function isShared(): bool {
  263. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  264. return false;
  265. }
  266. return $this->addressBookInfo['principaluri']
  267. !== $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  268. }
  269. /**
  270. * @inheritDoc
  271. */
  272. public function isSystemAddressBook(): bool {
  273. return $this->addressBookInfo['principaluri'] === 'principals/system/system' && (
  274. $this->addressBookInfo['uri'] === 'system' ||
  275. $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl()
  276. );
  277. }
  278. }