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.

312 lines
8.2 KiB

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