2015-12-04 15:38:32 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $localSystemAddressBook;
|
|
|
|
|
|
|
|
public function __construct(CardDavBackend $backend, IUserManager $userManager) {
|
2015-12-04 15:38:32 +03:00
|
|
|
$this->backend = $backend;
|
2016-01-13 22:56:25 +03:00
|
|
|
$this->userManager = $userManager;
|
2015-12-04 15:38:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $userName
|
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $syncToken
|
|
|
|
* @param int $targetBookId
|
|
|
|
* @param string $targetPrincipal
|
|
|
|
* @param array $targetProperties
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-04 15:38:32 +03:00
|
|
|
public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
|
|
|
|
// 1. create addressbook
|
|
|
|
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
|
|
|
|
$addressBookId = $book['id'];
|
|
|
|
|
|
|
|
// 2. query changes
|
|
|
|
$response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken);
|
|
|
|
|
|
|
|
// 3. apply changes
|
|
|
|
// TODO: use multi-get for download
|
|
|
|
foreach ($response['response'] as $resource => $status) {
|
|
|
|
$cardUri = basename($resource);
|
|
|
|
if (isset($status[200])) {
|
|
|
|
$vCard = $this->download($url, $sharedSecret, $resource);
|
|
|
|
$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) {
|
2015-12-04 15:38:32 +03:00
|
|
|
$book = $this->backend->getAddressBooksByUri($id);
|
|
|
|
if (!is_null($book)) {
|
|
|
|
return $book;
|
|
|
|
}
|
|
|
|
$this->backend->createAddressBook($principal, $id, $properties);
|
|
|
|
|
|
|
|
return $this->backend->getAddressBooksByUri($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $userName
|
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $syncToken
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-12-10 17:53:44 +03:00
|
|
|
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
|
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);
|
|
|
|
$client->setThrowExceptions(true);
|
|
|
|
|
2016-01-11 18:28:54 +03:00
|
|
|
$addressBookUrl = "remote.php/dav/addressbooks/system/system/system";
|
2015-12-04 15:38:32 +03:00
|
|
|
$body = $this->buildSyncCollectionRequestBody($syncToken);
|
|
|
|
|
|
|
|
$response = $client->request('REPORT', $addressBookUrl, $body, [
|
|
|
|
'Content-Type' => 'application/xml'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$result = $this->parseMultiStatus($response['body']);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $sharedSecret
|
|
|
|
* @param string $resourcePath
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-13 13:30:22 +03:00
|
|
|
protected function download($url, $sharedSecret, $resourcePath) {
|
2015-12-04 15:38:32 +03:00
|
|
|
$settings = [
|
|
|
|
'baseUri' => $url,
|
|
|
|
'userName' => 'system',
|
|
|
|
'password' => $sharedSecret,
|
|
|
|
];
|
|
|
|
$client = new Client($settings);
|
|
|
|
$client->setThrowExceptions(true);
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
$response = $client->request('GET', $resourcePath);
|
2015-12-04 15:38:32 +03:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$body = $dom->saveXML();
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function updateUser($user) {
|
|
|
|
$systemAddressBook = $this->getLocalSystemAddressBook();
|
|
|
|
$addressBookId = $systemAddressBook['id'];
|
|
|
|
$converter = new Converter();
|
|
|
|
$name = $user->getBackendClassName();
|
|
|
|
$userId = $user->getUID();
|
|
|
|
|
|
|
|
$cardId = "$name:$userId.vcf";
|
|
|
|
$card = $this->backend->getCard($addressBookId, $cardId);
|
|
|
|
if ($card === false) {
|
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
$this->backend->createCard($addressBookId, $cardId, $vCard->serialize());
|
|
|
|
} else {
|
|
|
|
$vCard = Reader::read($card['carddata']);
|
|
|
|
if ($converter->updateCard($vCard, $user)) {
|
|
|
|
$this->backend->updateCard($addressBookId, $cardId, $vCard->serialize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function syncInstance(\Closure $progressCallback) {
|
|
|
|
$systemAddressBook = $this->getLocalSystemAddressBook();
|
|
|
|
$this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) {
|
|
|
|
$this->updateUser($user);
|
|
|
|
$progressCallback();
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
}
|