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 9.8KB

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