public function testCardOperations() { // 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']; // create a card $uri = $this->getUniqueID('card'); $this->backend->createCard($bookId, $uri, ''); // get all the cards $cards = $this->backend->getCards($bookId); $this->assertEquals(1, count($cards)); $this->assertEquals('', $cards[0]['carddata']); // get the cards $card = $this->backend->getCard($bookId, $uri); $this->assertNotNull($card); $this->assertArrayHasKey('id', $card); $this->assertArrayHasKey('uri', $card); $this->assertArrayHasKey('lastmodified', $card); $this->assertArrayHasKey('etag', $card); $this->assertArrayHasKey('size', $card); $this->assertEquals('', $card['carddata']); // update the card $this->backend->updateCard($bookId, $uri, '***'); $card = $this->backend->getCard($bookId, $uri); $this->assertEquals('***', $card['carddata']); // delete the card $this->backend->deleteCard($bookId, $uri); $cards = $this->backend->getCards($bookId); $this->assertEquals(0, count($cards)); }
/** * @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); }
public function updateCard($addressBookId, $uid) { /** * @param \Sabre\VObject\Component\VCard $vCard * @param \OC\User\User $user */ $user = $this->userManager->get($uid); $userId = $user->getUID(); $cardId = md5($userId) . ".vcf"; $card = $this->cardDavBackend->getCard($addressBookId, $cardId); if (!$card) { $this->insertCard($addressBookId, $uid); } else { $vCard = Reader::read($card['carddata']); $needsUpdate = $this->converterUser->updateCard($vCard, $user); if ($needsUpdate) { $groups = \OC::$server->getGroupManager()->getUserGroups($user); if ($groups) { foreach ($groups as $groupName => $groupInfo) { $vCard->add(new Text($vCard, 'CATEGORIES', $groupName)); } } $this->cardDavBackend->updateCard($addressBookId, $cardId, $vCard->serialize()); // $this->cardDavBackend->deleteCard($addressBookId, $cardId); // $this->insertCard($addressBookId, $uid); } } }
/** * Retrieves a single contact from the ContactsManager and parse * them to a usable format * TODO Could be a problem if too many contacts. * @return array */ public function getContact($id) { $activeAddressbooks = $this->getAddressBooksIDsForUser(); foreach ($activeAddressbooks as $activeAddressbook) { $contact = $this->backend->getCard($activeAddressbook, $this->backend->getCardUri($id)); if ($contact != null) { return new Contact($this->fbController, $this->backend, $contact['id'], $contact['uri'], $contact['lastmodified'], $contact['etag'], $activeAddressbook, VObject\Reader::read($contact["carddata"])); } } return false; }
/** * @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()); } } }