1
0

AddressBookImpl.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author labor4 <schreibtisch@labor4.ch>
  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. * @return array an array of contacts which are arrays of key-value-pairs
  97. * example result:
  98. * [
  99. * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => 'a@b.c', 'GEO' => '37.386013;-122.082932'],
  100. * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['d@e.f', 'g@h.i']]
  101. * ]
  102. * @since 5.0.0
  103. */
  104. public function search($pattern, $searchProperties, $options) {
  105. $results = $this->backend->search($this->getKey(), $pattern, $searchProperties, $options);
  106. $withTypes = \array_key_exists('types', $options) && $options['types'] === true;
  107. $vCards = [];
  108. foreach ($results as $result) {
  109. $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes);
  110. }
  111. return $vCards;
  112. }
  113. /**
  114. * @param array $properties this array if key-value-pairs defines a contact
  115. * @return array an array representing the contact just created or updated
  116. * @since 5.0.0
  117. */
  118. public function createOrUpdate($properties) {
  119. $update = false;
  120. if (!isset($properties['URI'])) { // create a new contact
  121. $uid = $this->createUid();
  122. $uri = $uid . '.vcf';
  123. $vCard = $this->createEmptyVCard($uid);
  124. } else { // update existing contact
  125. $uri = $properties['URI'];
  126. $vCardData = $this->backend->getCard($this->getKey(), $uri);
  127. $vCard = $this->readCard($vCardData['carddata']);
  128. $update = true;
  129. }
  130. foreach ($properties as $key => $value) {
  131. $vCard->$key = $vCard->createProperty($key, $value);
  132. }
  133. if ($update) {
  134. $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize());
  135. } else {
  136. $this->backend->createCard($this->getKey(), $uri, $vCard->serialize());
  137. }
  138. return $this->vCard2Array($uri, $vCard);
  139. }
  140. /**
  141. * @return mixed
  142. * @since 5.0.0
  143. */
  144. public function getPermissions() {
  145. $permissions = $this->addressBook->getACL();
  146. $result = 0;
  147. foreach ($permissions as $permission) {
  148. switch($permission['privilege']) {
  149. case '{DAV:}read':
  150. $result |= Constants::PERMISSION_READ;
  151. break;
  152. case '{DAV:}write':
  153. $result |= Constants::PERMISSION_CREATE;
  154. $result |= Constants::PERMISSION_UPDATE;
  155. break;
  156. case '{DAV:}all':
  157. $result |= Constants::PERMISSION_ALL;
  158. break;
  159. }
  160. }
  161. return $result;
  162. }
  163. /**
  164. * @param object $id the unique identifier to a contact
  165. * @return bool successful or not
  166. * @since 5.0.0
  167. */
  168. public function delete($id) {
  169. $uri = $this->backend->getCardUri($id);
  170. return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
  171. }
  172. /**
  173. * read vCard data into a vCard object
  174. *
  175. * @param string $cardData
  176. * @return VCard
  177. */
  178. protected function readCard($cardData) {
  179. return Reader::read($cardData);
  180. }
  181. /**
  182. * create UID for contact
  183. *
  184. * @return string
  185. */
  186. protected function createUid() {
  187. do {
  188. $uid = $this->getUid();
  189. $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
  190. } while (!empty($contact));
  191. return $uid;
  192. }
  193. /**
  194. * getUid is only there for testing, use createUid instead
  195. */
  196. protected function getUid() {
  197. return UUIDUtil::getUUID();
  198. }
  199. /**
  200. * create empty vcard
  201. *
  202. * @param string $uid
  203. * @return VCard
  204. */
  205. protected function createEmptyVCard($uid) {
  206. $vCard = new VCard();
  207. $vCard->UID = $uid;
  208. return $vCard;
  209. }
  210. /**
  211. * create array with all vCard properties
  212. *
  213. * @param string $uri
  214. * @param VCard $vCard
  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' && $property->getValueType() === 'BINARY') {
  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. } else if ($property->name === 'X-SOCIALPROFILE') {
  233. $type = $this->getTypeFromProperty($property);
  234. // Type is the social network, when it's empty we don't need this.
  235. if ($type !== null) {
  236. if (!isset($result[$property->name])) {
  237. $result[$property->name] = [];
  238. }
  239. $result[$property->name][$type] = $property->getValue();
  240. }
  241. // The following properties can be set multiple times
  242. } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL', 'X-ADDRESSBOOKSERVER-MEMBER'])) {
  243. if (!isset($result[$property->name])) {
  244. $result[$property->name] = [];
  245. }
  246. $type = $this->getTypeFromProperty($property);
  247. if ($withTypes) {
  248. $result[$property->name][] = [
  249. 'type' => $type,
  250. 'value' => $property->getValue()
  251. ];
  252. } else {
  253. $result[$property->name][] = $property->getValue();
  254. }
  255. } else {
  256. $result[$property->name] = $property->getValue();
  257. }
  258. }
  259. if (
  260. $this->addressBookInfo['principaluri'] === 'principals/system/system' && (
  261. $this->addressBookInfo['uri'] === 'system' ||
  262. $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl()
  263. )
  264. ) {
  265. $result['isLocalSystemBook'] = true;
  266. }
  267. return $result;
  268. }
  269. /**
  270. * Get the type of the current property
  271. *
  272. * @param Property $property
  273. * @return null|string
  274. */
  275. protected function getTypeFromProperty(Property $property) {
  276. $parameters = $property->parameters();
  277. // Type is the social network, when it's empty we don't need this.
  278. if (isset($parameters['TYPE'])) {
  279. /** @var \Sabre\VObject\Parameter $type */
  280. $type = $parameters['TYPE'];
  281. return $type->getValue();
  282. }
  283. return null;
  284. }
  285. }