AddressBookImpl.php 6.2 KB

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