AddressBookImpl.php 8.5 KB

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