You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddressBookImpl.php 8.9KB

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