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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 call-me-matt <nextcloud@matthiasheinisch.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Georg Ehrke <oc.list@georgehrke.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  13. * @author Julius Härtl <jus@bitgrid.net>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\CardDAV;
  32. use OCP\Constants;
  33. use OCP\IAddressBook;
  34. use OCP\IURLGenerator;
  35. use Sabre\VObject\Component\VCard;
  36. use Sabre\VObject\Property;
  37. use Sabre\VObject\Reader;
  38. use Sabre\VObject\UUIDUtil;
  39. class AddressBookImpl implements IAddressBook {
  40. /** @var CardDavBackend */
  41. private $backend;
  42. /** @var array */
  43. private $addressBookInfo;
  44. /** @var AddressBook */
  45. private $addressBook;
  46. /** @var IURLGenerator */
  47. private $urlGenerator;
  48. /**
  49. * AddressBookImpl constructor.
  50. *
  51. * @param AddressBook $addressBook
  52. * @param array $addressBookInfo
  53. * @param CardDavBackend $backend
  54. * @param IUrlGenerator $urlGenerator
  55. */
  56. public function __construct(
  57. AddressBook $addressBook,
  58. array $addressBookInfo,
  59. CardDavBackend $backend,
  60. IURLGenerator $urlGenerator) {
  61. $this->addressBook = $addressBook;
  62. $this->addressBookInfo = $addressBookInfo;
  63. $this->backend = $backend;
  64. $this->urlGenerator = $urlGenerator;
  65. }
  66. /**
  67. * @return string defining the technical unique key
  68. * @since 5.0.0
  69. */
  70. public function getKey() {
  71. return $this->addressBookInfo['id'];
  72. }
  73. /**
  74. * @return string defining the unique uri
  75. * @since 16.0.0
  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. if (is_array($value)) {
  134. $vCard->remove($key);
  135. foreach ($value as $entry) {
  136. if (($key === "ADR" || $key === "PHOTO") && is_string($entry["value"])) {
  137. $entry["value"] = stripslashes($entry["value"]);
  138. $entry["value"] = explode(';', $entry["value"]);
  139. }
  140. $property = $vCard->createProperty($key, $entry["value"]);
  141. if (isset($entry["type"])) {
  142. $property->add('TYPE', $entry["type"]);
  143. }
  144. $vCard->add($property);
  145. }
  146. } elseif ($key !== 'URI') {
  147. $vCard->$key = $vCard->createProperty($key, $value);
  148. }
  149. }
  150. if ($update) {
  151. $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize());
  152. } else {
  153. $this->backend->createCard($this->getKey(), $uri, $vCard->serialize());
  154. }
  155. return $this->vCard2Array($uri, $vCard);
  156. }
  157. /**
  158. * @return mixed
  159. * @since 5.0.0
  160. */
  161. public function getPermissions() {
  162. $permissions = $this->addressBook->getACL();
  163. $result = 0;
  164. foreach ($permissions as $permission) {
  165. switch ($permission['privilege']) {
  166. case '{DAV:}read':
  167. $result |= Constants::PERMISSION_READ;
  168. break;
  169. case '{DAV:}write':
  170. $result |= Constants::PERMISSION_CREATE;
  171. $result |= Constants::PERMISSION_UPDATE;
  172. break;
  173. case '{DAV:}all':
  174. $result |= Constants::PERMISSION_ALL;
  175. break;
  176. }
  177. }
  178. return $result;
  179. }
  180. /**
  181. * @param object $id the unique identifier to a contact
  182. * @return bool successful or not
  183. * @since 5.0.0
  184. */
  185. public function delete($id) {
  186. $uri = $this->backend->getCardUri($id);
  187. return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
  188. }
  189. /**
  190. * read vCard data into a vCard object
  191. *
  192. * @param string $cardData
  193. * @return VCard
  194. */
  195. protected function readCard($cardData) {
  196. return Reader::read($cardData);
  197. }
  198. /**
  199. * create UID for contact
  200. *
  201. * @return string
  202. */
  203. protected function createUid() {
  204. do {
  205. $uid = $this->getUid();
  206. $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
  207. } while (!empty($contact));
  208. return $uid;
  209. }
  210. /**
  211. * getUid is only there for testing, use createUid instead
  212. */
  213. protected function getUid() {
  214. return UUIDUtil::getUUID();
  215. }
  216. /**
  217. * create empty vcard
  218. *
  219. * @param string $uid
  220. * @return VCard
  221. */
  222. protected function createEmptyVCard($uid) {
  223. $vCard = new VCard();
  224. $vCard->UID = $uid;
  225. return $vCard;
  226. }
  227. /**
  228. * create array with all vCard properties
  229. *
  230. * @param string $uri
  231. * @param VCard $vCard
  232. * @param boolean $withTypes (optional) return the values as arrays of value/type pairs
  233. * @return array
  234. */
  235. protected function vCard2Array($uri, VCard $vCard, $withTypes = false) {
  236. $result = [
  237. 'URI' => $uri,
  238. ];
  239. foreach ($vCard->children() as $property) {
  240. if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') {
  241. $url = $this->urlGenerator->getAbsoluteURL(
  242. $this->urlGenerator->linkTo('', 'remote.php') . '/dav/');
  243. $url .= implode('/', [
  244. 'addressbooks',
  245. substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/'
  246. $this->addressBookInfo['uri'],
  247. $uri
  248. ]) . '?photo';
  249. $result['PHOTO'] = 'VALUE=uri:' . $url;
  250. } elseif (in_array($property->name, ['URL', 'GEO', 'CLOUD', 'ADR', 'EMAIL', 'IMPP', 'TEL', 'X-SOCIALPROFILE', 'RELATED', 'LANG', 'X-ADDRESSBOOKSERVER-MEMBER'])) {
  251. if (!isset($result[$property->name])) {
  252. $result[$property->name] = [];
  253. }
  254. $type = $this->getTypeFromProperty($property);
  255. if ($withTypes) {
  256. $result[$property->name][] = [
  257. 'type' => $type,
  258. 'value' => $property->getValue()
  259. ];
  260. } else {
  261. $result[$property->name][] = $property->getValue();
  262. }
  263. } else {
  264. $result[$property->name] = $property->getValue();
  265. }
  266. }
  267. if ($this->isSystemAddressBook()) {
  268. $result['isLocalSystemBook'] = true;
  269. }
  270. return $result;
  271. }
  272. /**
  273. * Get the type of the current property
  274. *
  275. * @param Property $property
  276. * @return null|string
  277. */
  278. protected function getTypeFromProperty(Property $property) {
  279. $parameters = $property->parameters();
  280. // Type is the social network, when it's empty we don't need this.
  281. if (isset($parameters['TYPE'])) {
  282. /** @var \Sabre\VObject\Parameter $type */
  283. $type = $parameters['TYPE'];
  284. return $type->getValue();
  285. }
  286. return null;
  287. }
  288. /**
  289. * @inheritDoc
  290. */
  291. public function isShared(): bool {
  292. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  293. return false;
  294. }
  295. return $this->addressBookInfo['principaluri']
  296. !== $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  297. }
  298. /**
  299. * @inheritDoc
  300. */
  301. public function isSystemAddressBook(): bool {
  302. return $this->addressBookInfo['principaluri'] === 'principals/system/system' && (
  303. $this->addressBookInfo['uri'] === 'system' ||
  304. $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl()
  305. );
  306. }
  307. }