2015-11-25 17:24:50 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arne Hamann <kontakt+github@arne.email>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-11-25 17:24:50 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-11-25 17:24:50 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
|
|
|
use OCP\Constants;
|
|
|
|
use OCP\IAddressBook;
|
2016-06-21 16:25:44 +03:00
|
|
|
use OCP\IURLGenerator;
|
2015-11-25 17:24:50 +03:00
|
|
|
use Sabre\VObject\Component\VCard;
|
2016-08-30 13:08:39 +03:00
|
|
|
use Sabre\VObject\Property;
|
2015-11-25 17:24:50 +03:00
|
|
|
use Sabre\VObject\Reader;
|
|
|
|
use Sabre\VObject\UUIDUtil;
|
|
|
|
|
|
|
|
class AddressBookImpl implements IAddressBook {
|
|
|
|
|
|
|
|
/** @var CardDavBackend */
|
|
|
|
private $backend;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $addressBookInfo;
|
|
|
|
|
|
|
|
/** @var AddressBook */
|
|
|
|
private $addressBook;
|
|
|
|
|
2016-06-21 16:25:44 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2015-11-25 17:24:50 +03:00
|
|
|
/**
|
|
|
|
* AddressBookImpl constructor.
|
|
|
|
*
|
|
|
|
* @param AddressBook $addressBook
|
|
|
|
* @param array $addressBookInfo
|
|
|
|
* @param CardDavBackend $backend
|
2016-06-21 16:25:44 +03:00
|
|
|
* @param IUrlGenerator $urlGenerator
|
2015-11-25 17:24:50 +03:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AddressBook $addressBook,
|
|
|
|
array $addressBookInfo,
|
2016-06-21 16:25:44 +03:00
|
|
|
CardDavBackend $backend,
|
|
|
|
IURLGenerator $urlGenerator) {
|
2015-11-25 17:24:50 +03:00
|
|
|
$this->addressBook = $addressBook;
|
|
|
|
$this->addressBookInfo = $addressBookInfo;
|
|
|
|
$this->backend = $backend;
|
2016-06-21 16:25:44 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-11-25 17:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string defining the technical unique key
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function getKey() {
|
|
|
|
return $this->addressBookInfo['id'];
|
|
|
|
}
|
|
|
|
|
2019-01-17 13:13:45 +03:00
|
|
|
/**
|
|
|
|
* @return string defining the unique uri
|
|
|
|
* @since 16.0.0
|
|
|
|
*/
|
|
|
|
public function getUri(): string {
|
|
|
|
return $this->addressBookInfo['uri'];
|
|
|
|
}
|
|
|
|
|
2015-11-25 17:24:50 +03:00
|
|
|
/**
|
|
|
|
* In comparison to getKey() this function returns a human readable (maybe translated) name
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function getDisplayName() {
|
|
|
|
return $this->addressBookInfo['{DAV:}displayname'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $pattern which should match within the $searchProperties
|
|
|
|
* @param array $searchProperties defines the properties within the query pattern should match
|
2018-01-18 12:38:24 +03:00
|
|
|
* @param array $options Options to define the output format and search behavior
|
|
|
|
* - 'types' boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array
|
2018-10-30 16:43:08 +03:00
|
|
|
* example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => 'g@h.i']]
|
2018-01-18 12:38:24 +03:00
|
|
|
* - 'escape_like_param' - If set to false wildcards _ and % are not escaped
|
2020-01-10 18:06:44 +03:00
|
|
|
* - 'limit' - Set a numeric limit for the search results
|
|
|
|
* - 'offset' - Set the offset for the limited search results
|
2018-10-30 16:43:08 +03:00
|
|
|
* @return array an array of contacts which are arrays of key-value-pairs
|
|
|
|
* example result:
|
|
|
|
* [
|
|
|
|
* ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => 'a@b.c', 'GEO' => '37.386013;-122.082932'],
|
|
|
|
* ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['d@e.f', 'g@h.i']]
|
|
|
|
* ]
|
2015-11-25 17:24:50 +03:00
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function search($pattern, $searchProperties, $options) {
|
2019-07-22 19:48:47 +03:00
|
|
|
$results = $this->backend->search($this->getKey(), $pattern, $searchProperties, $options);
|
2015-11-25 17:24:50 +03:00
|
|
|
|
2018-10-29 19:00:09 +03:00
|
|
|
$withTypes = \array_key_exists('types', $options) && $options['types'] === true;
|
|
|
|
|
2015-11-25 17:24:50 +03:00
|
|
|
$vCards = [];
|
2016-06-21 16:25:44 +03:00
|
|
|
foreach ($results as $result) {
|
2018-10-29 19:00:09 +03:00
|
|
|
$vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes);
|
2015-11-25 17:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $vCards;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $properties this array if key-value-pairs defines a contact
|
|
|
|
* @return array an array representing the contact just created or updated
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function createOrUpdate($properties) {
|
|
|
|
$update = false;
|
2016-06-21 16:25:44 +03:00
|
|
|
if (!isset($properties['URI'])) { // create a new contact
|
2015-11-25 17:24:50 +03:00
|
|
|
$uid = $this->createUid();
|
|
|
|
$uri = $uid . '.vcf';
|
|
|
|
$vCard = $this->createEmptyVCard($uid);
|
|
|
|
} else { // update existing contact
|
2016-06-21 16:25:44 +03:00
|
|
|
$uri = $properties['URI'];
|
2015-11-25 17:24:50 +03:00
|
|
|
$vCardData = $this->backend->getCard($this->getKey(), $uri);
|
|
|
|
$vCard = $this->readCard($vCardData['carddata']);
|
|
|
|
$update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($properties as $key => $value) {
|
|
|
|
$vCard->$key = $vCard->createProperty($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($update) {
|
|
|
|
$this->backend->updateCard($this->getKey(), $uri, $vCard->serialize());
|
|
|
|
} else {
|
|
|
|
$this->backend->createCard($this->getKey(), $uri, $vCard->serialize());
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:25:44 +03:00
|
|
|
return $this->vCard2Array($uri, $vCard);
|
2015-11-25 17:24:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function getPermissions() {
|
|
|
|
$permissions = $this->addressBook->getACL();
|
|
|
|
$result = 0;
|
|
|
|
foreach ($permissions as $permission) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($permission['privilege']) {
|
2015-11-25 17:24:50 +03:00
|
|
|
case '{DAV:}read':
|
|
|
|
$result |= Constants::PERMISSION_READ;
|
|
|
|
break;
|
|
|
|
case '{DAV:}write':
|
|
|
|
$result |= Constants::PERMISSION_CREATE;
|
|
|
|
$result |= Constants::PERMISSION_UPDATE;
|
|
|
|
break;
|
|
|
|
case '{DAV:}all':
|
|
|
|
$result |= Constants::PERMISSION_ALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $id the unique identifier to a contact
|
|
|
|
* @return bool successful or not
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
public function delete($id) {
|
|
|
|
$uri = $this->backend->getCardUri($id);
|
|
|
|
return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read vCard data into a vCard object
|
|
|
|
*
|
|
|
|
* @param string $cardData
|
|
|
|
* @return VCard
|
|
|
|
*/
|
|
|
|
protected function readCard($cardData) {
|
|
|
|
return Reader::read($cardData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create UID for contact
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function createUid() {
|
|
|
|
do {
|
|
|
|
$uid = $this->getUid();
|
2016-02-15 16:13:04 +03:00
|
|
|
$contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
|
2016-01-28 18:03:19 +03:00
|
|
|
} while (!empty($contact));
|
2015-11-25 17:24:50 +03:00
|
|
|
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getUid is only there for testing, use createUid instead
|
|
|
|
*/
|
|
|
|
protected function getUid() {
|
|
|
|
return UUIDUtil::getUUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create empty vcard
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @return VCard
|
|
|
|
*/
|
|
|
|
protected function createEmptyVCard($uid) {
|
|
|
|
$vCard = new VCard();
|
2016-10-13 13:15:10 +03:00
|
|
|
$vCard->UID = $uid;
|
2015-11-25 17:24:50 +03:00
|
|
|
return $vCard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create array with all vCard properties
|
|
|
|
*
|
2016-06-21 16:25:44 +03:00
|
|
|
* @param string $uri
|
2015-11-25 17:24:50 +03:00
|
|
|
* @param VCard $vCard
|
2020-04-17 10:12:06 +03:00
|
|
|
* @param boolean $withTypes (optional) return the values as arrays of value/type pairs
|
2015-11-25 17:24:50 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-10-29 19:00:09 +03:00
|
|
|
protected function vCard2Array($uri, VCard $vCard, $withTypes = false) {
|
2016-06-21 16:25:44 +03:00
|
|
|
$result = [
|
|
|
|
'URI' => $uri,
|
|
|
|
];
|
|
|
|
|
2016-10-13 13:15:10 +03:00
|
|
|
foreach ($vCard->children() as $property) {
|
2016-06-21 16:25:44 +03:00
|
|
|
if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') {
|
|
|
|
$url = $this->urlGenerator->getAbsoluteURL(
|
|
|
|
$this->urlGenerator->linkTo('', 'remote.php') . '/dav/');
|
|
|
|
$url .= implode('/', [
|
|
|
|
'addressbooks',
|
|
|
|
substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/'
|
|
|
|
$this->addressBookInfo['uri'],
|
|
|
|
$uri
|
|
|
|
]) . '?photo';
|
|
|
|
|
|
|
|
$result['PHOTO'] = 'VALUE=uri:' . $url;
|
2020-04-17 10:12:06 +03:00
|
|
|
} elseif (in_array($property->name, ['URL', 'GEO', 'CLOUD', 'ADR', 'EMAIL', 'IMPP', 'TEL', 'X-SOCIALPROFILE', 'RELATED', 'LANG', 'X-ADDRESSBOOKSERVER-MEMBER'])) {
|
2016-08-30 13:08:39 +03:00
|
|
|
if (!isset($result[$property->name])) {
|
|
|
|
$result[$property->name] = [];
|
|
|
|
}
|
|
|
|
|
2018-10-18 13:31:57 +03:00
|
|
|
$type = $this->getTypeFromProperty($property);
|
2018-10-29 19:00:09 +03:00
|
|
|
if ($withTypes) {
|
|
|
|
$result[$property->name][] = [
|
|
|
|
'type' => $type,
|
|
|
|
'value' => $property->getValue()
|
2020-04-09 10:22:29 +03:00
|
|
|
];
|
2018-10-18 13:31:57 +03:00
|
|
|
} else {
|
|
|
|
$result[$property->name][] = $property->getValue();
|
|
|
|
}
|
2016-06-21 16:25:44 +03:00
|
|
|
} else {
|
|
|
|
$result[$property->name] = $property->getValue();
|
|
|
|
}
|
2015-11-25 17:24:50 +03:00
|
|
|
}
|
2016-08-30 13:08:39 +03:00
|
|
|
|
2020-08-04 08:57:07 +03:00
|
|
|
if ($this->isSystemAddressBook()) {
|
2016-02-02 13:24:26 +03:00
|
|
|
$result['isLocalSystemBook'] = true;
|
|
|
|
}
|
2015-11-25 17:24:50 +03:00
|
|
|
return $result;
|
|
|
|
}
|
2016-08-30 13:08:39 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of the current property
|
|
|
|
*
|
|
|
|
* @param Property $property
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
protected function getTypeFromProperty(Property $property) {
|
|
|
|
$parameters = $property->parameters();
|
|
|
|
// Type is the social network, when it's empty we don't need this.
|
|
|
|
if (isset($parameters['TYPE'])) {
|
|
|
|
/** @var \Sabre\VObject\Parameter $type */
|
|
|
|
$type = $parameters['TYPE'];
|
|
|
|
return $type->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-08-04 08:57:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isShared(): bool {
|
|
|
|
if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->addressBookInfo['principaluri']
|
|
|
|
!== $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isSystemAddressBook(): bool {
|
|
|
|
return $this->addressBookInfo['principaluri'] === 'principals/system/system' && (
|
|
|
|
$this->addressBookInfo['uri'] === 'system' ||
|
|
|
|
$this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl()
|
|
|
|
);
|
|
|
|
}
|
2015-11-25 17:24:50 +03:00
|
|
|
}
|