AddressBookImpl.php 8.9 KB

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