2015-12-04 15:38:32 +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 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2015-12-04 15:38:32 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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-12-04 15:38:32 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
use OC\Accounts\AccountManager;
|
2016-02-26 19:51:20 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2017-03-23 18:10:53 +03:00
|
|
|
use OCP\ICertificateManager;
|
2016-02-26 19:51:20 +03:00
|
|
|
use OCP\ILogger;
|
2016-01-13 22:56:25 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2015-12-04 15:38:32 +03:00
|
|
|
use Sabre\DAV\Client;
|
|
|
|
use Sabre\DAV\Xml\Response\MultiStatus;
|
|
|
|
use Sabre\DAV\Xml\Service;
|
2016-02-26 19:51:20 +03:00
|
|
|
use Sabre\HTTP\ClientHttpException;
|
2016-01-13 22:56:25 +03:00
|
|
|
use Sabre\VObject\Reader;
|
2015-12-04 15:38:32 +03:00
|
|
|
|
|
|
|
class SyncService {
|
|
|
|
|
|
|
|
/** @var CardDavBackend */
|
|
|
|
private $backend;
|
|
|
|
|
2016-01-13 22:56:25 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2016-02-26 19:51:20 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2016-01-13 22:56:25 +03:00
|
|
|
/** @var array */
|
|
|
|
private $localSystemAddressBook;
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
/** @var AccountManager */
|
|
|
|
private $accountManager;
|
|
|
|
|
2017-03-23 16:33:25 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $certPath;
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
/**
|
|
|
|
* SyncService constructor.
|
|
|
|
*
|
|
|
|
* @param CardDavBackend $backend
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param AccountManager $accountManager
|
|
|
|
*/
|
|
|
|
public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) {
|
2015-12-04 15:38:32 +03:00
|
|
|
$this->backend = $backend;
|
2016-01-13 22:56:25 +03:00
|
|
|
$this->userManager = $userManager;
|
2016-02-26 19:51:20 +03:00
|
|
|
$this->logger = $logger;
|
2016-11-11 16:36:17 +03:00
|
|
|
$this->accountManager = $accountManager;
|
2017-03-23 18:10:53 +03:00
|
|
|
$this->certPath = '';
|
2015-12-04 15:38:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $userName
|
2017-02-24 18:09:52 +03:00
|
|
|
* @param string $addressBookUrl
|
2015-12-07 17:28:06 +03:00
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $syncToken
|
|
|
|
* @param int $targetBookId
|
|
|
|
* @param string $targetPrincipal
|
|
|
|
* @param array $targetProperties
|
|
|
|
* @return string
|
2016-02-26 19:51:20 +03:00
|
|
|
* @throws \Exception
|
2015-12-07 17:28:06 +03:00
|
|
|
*/
|
2017-02-24 18:09:52 +03:00
|
|
|
public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
|
2015-12-04 15:38:32 +03:00
|
|
|
// 1. create addressbook
|
|
|
|
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
|
|
|
|
$addressBookId = $book['id'];
|
|
|
|
|
|
|
|
// 2. query changes
|
2016-02-26 19:51:20 +03:00
|
|
|
try {
|
2017-02-24 18:09:52 +03:00
|
|
|
$response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken);
|
2016-02-26 19:51:20 +03:00
|
|
|
} catch (ClientHttpException $ex) {
|
|
|
|
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
|
|
|
|
// remote server revoked access to the address book, remove it
|
|
|
|
$this->backend->deleteAddressBook($addressBookId);
|
|
|
|
$this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']);
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 15:38:32 +03:00
|
|
|
|
|
|
|
// 3. apply changes
|
|
|
|
// TODO: use multi-get for download
|
|
|
|
foreach ($response['response'] as $resource => $status) {
|
|
|
|
$cardUri = basename($resource);
|
|
|
|
if (isset($status[200])) {
|
2017-02-24 18:09:52 +03:00
|
|
|
$vCard = $this->download($url, $userName, $sharedSecret, $resource);
|
2015-12-04 15:38:32 +03:00
|
|
|
$existingCard = $this->backend->getCard($addressBookId, $cardUri);
|
|
|
|
if ($existingCard === false) {
|
|
|
|
$this->backend->createCard($addressBookId, $cardUri, $vCard['body']);
|
|
|
|
} else {
|
|
|
|
$this->backend->updateCard($addressBookId, $cardUri, $vCard['body']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->backend->deleteCard($addressBookId, $cardUri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response['token'];
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $principal
|
|
|
|
* @param string $id
|
|
|
|
* @param array $properties
|
|
|
|
* @return array|null
|
|
|
|
* @throws \Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
2016-01-13 22:56:25 +03:00
|
|
|
public function ensureSystemAddressBookExists($principal, $id, $properties) {
|
2016-01-13 13:55:50 +03:00
|
|
|
$book = $this->backend->getAddressBooksByUri($principal, $id);
|
2015-12-04 15:38:32 +03:00
|
|
|
if (!is_null($book)) {
|
|
|
|
return $book;
|
|
|
|
}
|
|
|
|
$this->backend->createAddressBook($principal, $id, $properties);
|
|
|
|
|
2016-01-13 13:55:50 +03:00
|
|
|
return $this->backend->getAddressBooksByUri($principal, $id);
|
2015-12-04 15:38:32 +03:00
|
|
|
}
|
|
|
|
|
2017-03-23 18:10:53 +03:00
|
|
|
/**
|
|
|
|
* Check if there is a valid certPath we should use
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getCertPath() {
|
|
|
|
|
|
|
|
// we already have a valid certPath
|
|
|
|
if ($this->certPath !== '') {
|
|
|
|
return $this->certPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var ICertificateManager $certManager */
|
|
|
|
$certManager = \OC::$server->getCertificateManager(null);
|
|
|
|
$certPath = $certManager->getAbsoluteBundlePath();
|
|
|
|
if (file_exists($certPath)) {
|
|
|
|
$this->certPath = $certPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->certPath;
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:38:32 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $userName
|
2017-02-24 18:09:52 +03:00
|
|
|
* @param string $addressBookUrl
|
2015-12-04 15:38:32 +03:00
|
|
|
* @param string $sharedSecret
|
2017-03-23 16:33:25 +03:00
|
|
|
* @return Client
|
2015-12-04 15:38:32 +03:00
|
|
|
*/
|
2017-03-23 16:33:25 +03:00
|
|
|
protected function getClient($url, $userName, $sharedSecret) {
|
2015-12-04 15:38:32 +03:00
|
|
|
$settings = [
|
2016-01-11 18:28:54 +03:00
|
|
|
'baseUri' => $url . '/',
|
2015-12-04 15:38:32 +03:00
|
|
|
'userName' => $userName,
|
|
|
|
'password' => $sharedSecret,
|
|
|
|
];
|
|
|
|
$client = new Client($settings);
|
2017-03-23 18:10:53 +03:00
|
|
|
$certPath = $this->getCertPath();
|
2015-12-04 15:38:32 +03:00
|
|
|
$client->setThrowExceptions(true);
|
|
|
|
|
2017-03-23 18:10:53 +03:00
|
|
|
if ($certPath !== '' && strpos($url, 'http://') !== 0) {
|
2017-03-23 16:33:25 +03:00
|
|
|
$client->addCurlSetting(CURLOPT_CAINFO, $this->certPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $userName
|
2017-02-24 18:09:52 +03:00
|
|
|
* @param string $addressBookUrl
|
2017-03-23 16:33:25 +03:00
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $syncToken
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-24 18:09:52 +03:00
|
|
|
protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) {
|
|
|
|
$client = $this->getClient($url, $userName, $sharedSecret);
|
2017-03-23 16:33:25 +03:00
|
|
|
|
2017-02-24 18:09:52 +03:00
|
|
|
$body = $this->buildSyncCollectionRequestBody($syncToken);
|
2015-12-04 15:38:32 +03:00
|
|
|
|
2017-02-24 18:09:52 +03:00
|
|
|
$response = $client->request('REPORT', $addressBookUrl, $body, [
|
|
|
|
'Content-Type' => 'application/xml'
|
|
|
|
]);
|
2015-12-04 15:38:32 +03:00
|
|
|
|
2017-02-24 18:09:52 +03:00
|
|
|
return $this->parseMultiStatus($response['body']);
|
|
|
|
}
|
2015-12-04 15:38:32 +03:00
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
2017-02-24 18:09:52 +03:00
|
|
|
* @param string $userName
|
2015-12-07 17:28:06 +03:00
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $resourcePath
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-02-24 18:09:52 +03:00
|
|
|
protected function download($url, $userName, $sharedSecret, $resourcePath) {
|
|
|
|
$client = $this->getClient($url, $userName, $sharedSecret);
|
2017-03-23 16:33:25 +03:00
|
|
|
return $client->request('GET', $resourcePath);
|
2015-12-04 15:38:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
2015-12-16 23:04:54 +03:00
|
|
|
* @param string|null $syncToken
|
2015-12-07 17:28:06 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-16 23:04:54 +03:00
|
|
|
private function buildSyncCollectionRequestBody($syncToken) {
|
2015-12-04 15:38:32 +03:00
|
|
|
|
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8');
|
|
|
|
$dom->formatOutput = true;
|
|
|
|
$root = $dom->createElementNS('DAV:', 'd:sync-collection');
|
2015-12-16 23:04:54 +03:00
|
|
|
$sync = $dom->createElement('d:sync-token', $syncToken);
|
2015-12-04 15:38:32 +03:00
|
|
|
$prop = $dom->createElement('d:prop');
|
|
|
|
$cont = $dom->createElement('d:getcontenttype');
|
|
|
|
$etag = $dom->createElement('d:getetag');
|
|
|
|
|
|
|
|
$prop->appendChild($cont);
|
|
|
|
$prop->appendChild($etag);
|
|
|
|
$root->appendChild($sync);
|
|
|
|
$root->appendChild($prop);
|
|
|
|
$dom->appendChild($root);
|
2018-02-13 23:55:24 +03:00
|
|
|
return $dom->saveXML();
|
2015-12-04 15:38:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $body
|
|
|
|
* @return array
|
|
|
|
* @throws \Sabre\Xml\ParseException
|
|
|
|
*/
|
2015-12-04 15:38:32 +03:00
|
|
|
private function parseMultiStatus($body) {
|
|
|
|
$xml = new Service();
|
|
|
|
|
|
|
|
/** @var MultiStatus $multiStatus */
|
|
|
|
$multiStatus = $xml->expect('{DAV:}multistatus', $body);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ($multiStatus->getResponses() as $response) {
|
|
|
|
$result[$response->getHref()] = $response->getResponseProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['response' => $result, 'token' => $multiStatus->getSyncToken()];
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:56:25 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
2018-11-27 19:09:55 +03:00
|
|
|
public function updateUser(IUser $user) {
|
2016-01-13 22:56:25 +03:00
|
|
|
$systemAddressBook = $this->getLocalSystemAddressBook();
|
|
|
|
$addressBookId = $systemAddressBook['id'];
|
2016-11-11 16:36:17 +03:00
|
|
|
$converter = new Converter($this->accountManager);
|
2016-01-13 22:56:25 +03:00
|
|
|
$name = $user->getBackendClassName();
|
|
|
|
$userId = $user->getUID();
|
|
|
|
|
|
|
|
$cardId = "$name:$userId.vcf";
|
|
|
|
$card = $this->backend->getCard($addressBookId, $cardId);
|
2018-08-05 22:27:15 +03:00
|
|
|
if ($user->isEnabled()) {
|
|
|
|
if ($card === false) {
|
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
if ($vCard !== null) {
|
|
|
|
$this->backend->createCard($addressBookId, $cardId, $vCard->serialize());
|
|
|
|
}
|
2016-11-11 16:36:17 +03:00
|
|
|
} else {
|
2018-08-05 22:27:15 +03:00
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
if (is_null($vCard)) {
|
|
|
|
$this->backend->deleteCard($addressBookId, $cardId);
|
|
|
|
} else {
|
|
|
|
$this->backend->updateCard($addressBookId, $cardId, $vCard->serialize());
|
|
|
|
}
|
2016-01-13 22:56:25 +03:00
|
|
|
}
|
2018-08-05 22:27:15 +03:00
|
|
|
} else {
|
|
|
|
$this->backend->deleteCard($addressBookId, $cardId);
|
2016-01-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser|string $userOrCardId
|
|
|
|
*/
|
|
|
|
public function deleteUser($userOrCardId) {
|
|
|
|
$systemAddressBook = $this->getLocalSystemAddressBook();
|
|
|
|
if ($userOrCardId instanceof IUser){
|
|
|
|
$name = $userOrCardId->getBackendClassName();
|
|
|
|
$userId = $userOrCardId->getUID();
|
|
|
|
|
|
|
|
$userOrCardId = "$name:$userId.vcf";
|
|
|
|
}
|
|
|
|
$this->backend->deleteCard($systemAddressBook['id'], $userOrCardId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getLocalSystemAddressBook() {
|
|
|
|
if (is_null($this->localSystemAddressBook)) {
|
|
|
|
$systemPrincipal = "principals/system/system";
|
|
|
|
$this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [
|
|
|
|
'{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->localSystemAddressBook;
|
|
|
|
}
|
|
|
|
|
2016-01-19 00:51:23 +03:00
|
|
|
public function syncInstance(\Closure $progressCallback = null) {
|
2016-01-13 22:56:25 +03:00
|
|
|
$systemAddressBook = $this->getLocalSystemAddressBook();
|
2019-04-17 14:40:48 +03:00
|
|
|
$this->userManager->callForSeenUsers(function($user) use ($systemAddressBook, $progressCallback) {
|
2016-01-13 22:56:25 +03:00
|
|
|
$this->updateUser($user);
|
2016-01-19 00:51:23 +03:00
|
|
|
if (!is_null($progressCallback)) {
|
|
|
|
$progressCallback();
|
|
|
|
}
|
2016-01-13 22:56:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// remove no longer existing
|
|
|
|
$allCards = $this->backend->getCards($systemAddressBook['id']);
|
|
|
|
foreach($allCards as $card) {
|
|
|
|
$vCard = Reader::read($card['carddata']);
|
|
|
|
$uid = $vCard->UID->getValue();
|
|
|
|
// load backend and see if user exists
|
|
|
|
if (!$this->userManager->userExists($uid)) {
|
|
|
|
$this->deleteUser($card['uri']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:38:32 +03:00
|
|
|
|
|
|
|
}
|