/** * @param string $url * @param string $userName * @param string $sharedSecret * @param string $syncToken * @param int $targetBookId * @param string $targetPrincipal * @param array $targetProperties * @return string */ 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']; }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $principalBackend = new Principal($this->config, $this->userManager); $this->backend = new CardDavBackend($this->dbConnection, $principalBackend); // ensure system addressbook exists $systemAddressBook = $this->ensureSystemAddressBookExists(); $converter = new Converter(); $output->writeln('Syncing users ...'); $progress = new ProgressBar($output); $progress->start(); $this->userManager->callForAllUsers(function ($user) use($systemAddressBook, $converter, $progress) { /** @var IUser $user */ $name = $user->getBackendClassName(); $userId = $user->getUID(); $cardId = "{$name}:{$userId}.vcf"; $card = $this->backend->getCard($systemAddressBook['id'], $cardId); if ($card === false) { $vCard = $converter->createCardFromUser($user); $this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize()); } else { $vCard = Reader::read($card['carddata']); if ($converter->updateCard($vCard, $user)) { $this->backend->updateCard($systemAddressBook['id'], $cardId, $vCard->serialize()); } } $progress->advance(); }); $progress->finish(); $output->writeln(''); }
/** * @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; if (!isset($properties['UID'])) { // create a new contact $uid = $this->createUid(); $uri = $uid . '.vcf'; $vCard = $this->createEmptyVCard($uid); } else { // update existing contact $uid = $properties['UID']; $uri = $uid . '.vcf'; $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()); } return $this->vCard2Array($vCard); }
/** * @param int $addressBookId * @param int $newAddressBookId */ private function migrateBook($addressBookId, $newAddressBookId) { $this->adapter->foreachCard($addressBookId, function ($card) use($newAddressBookId) { try { $this->backend->createCard($newAddressBookId, $card['uri'], $card['carddata']); } catch (\Exception $ex) { $eventId = $card['id']; $addressBookId = $card['addressbookid']; $msg = "One event could not be migrated. (id: {$eventId}, addressbookid: {$addressBookId})"; $this->logger->logException($ex, ['app' => 'dav', 'message' => $msg]); if (!is_null($this->consoleOutput)) { $this->consoleOutput->writeln($msg); } } }); }
public function insertCard($addressBookId, $uid) { $user = $this->userManager->get($uid); $userId = $user->getUID(); $cardId = md5($userId) . ".vcf"; $vCard = $this->converterUser->createCardFromUser($user); $this->cardDavBackend->createCard($addressBookId, $cardId, $vCard->serialize()); }
public function testSyncSupport() { // create a new address book $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []); $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER); $this->assertEquals(1, count($books)); $bookId = $books[0]['id']; // fist call without synctoken $changes = $this->backend->getChangesForAddressBook($bookId, '', 1); $syncToken = $changes['syncToken']; // add a change $uri0 = $this->getUniqueID('card'); $this->backend->createCard($bookId, $uri0, ''); // look for changes $changes = $this->backend->getChangesForAddressBook($bookId, $syncToken, 1); $this->assertEquals($uri0, $changes['added'][0]); }
public function testSyncSupport() { $this->backend = $this->getMockBuilder('OCA\\DAV\\CardDAV\\CardDavBackend')->setConstructorArgs([$this->db, $this->principal, null])->setMethods(['updateProperties'])->getMock(); // create a new address book $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []); $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER); $this->assertEquals(1, count($books)); $bookId = $books[0]['id']; // fist call without synctoken $changes = $this->backend->getChangesForAddressBook($bookId, '', 1); $syncToken = $changes['syncToken']; // add a change $uri0 = $this->getUniqueID('card'); $this->backend->createCard($bookId, $uri0, ''); // look for changes $changes = $this->backend->getChangesForAddressBook($bookId, $syncToken, 1); $this->assertEquals($uri0, $changes['added'][0]); }
/** * @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 int $addressBookId * @param int $newAddressBookId */ private function migrateBook($addressBookId, $newAddressBookId) { $this->adapter->foreachCard($addressBookId, function ($card) use($newAddressBookId) { $this->backend->createCard($newAddressBookId, $card['uri'], $card['carddata']); }); }