AddressBookImpl.php 7.3 KB

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