From 3af4fc48ecfd7ded966e87ede38a99990971b5ef Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Wed, 22 Jan 2025 19:47:48 +0700 Subject: [PATCH] fixup! feat: add example contact on first login Signed-off-by: Hamza Mahjoubi --- .../dav/lib/Service/DefaultContactService.php | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/apps/dav/lib/Service/DefaultContactService.php b/apps/dav/lib/Service/DefaultContactService.php index 5e6f3b6db34d6..2aeff8072ace2 100644 --- a/apps/dav/lib/Service/DefaultContactService.php +++ b/apps/dav/lib/Service/DefaultContactService.php @@ -29,39 +29,31 @@ public function createDefaultContact(string $addressBookId): void { try { $folder = $appData->getFolder('defaultContact'); $defaultContactFile = $folder->getFile('defaultContact.vcf'); - $vcard = $defaultContactFile->getContent(); + $data = $defaultContactFile->getContent(); } catch (\Exception $e) { $this->logger->error('Couldn\'t get default contact file', ['exception' => $e]); return; } - // Make sure the UId is unique + // Make sure the UID is unique $newUid = Uuid::v4()->toRfc4122(); $newRev = date('Ymd\THis\Z'); - - - $vcard = (strpos($vcard, 'UID:') !== false) ? preg_replace( - '/UID:.*?(\r\n|\n)/', - "UID:$newUid\n", - $vcard - ) : str_replace( - 'END:VCARD', - "UID:$newUid\nEND:VCARD", - $vcard - ); - - // Add or update REV - $vcard = (strpos($vcard, 'REV:') !== false) ? preg_replace( - '/REV:.*?(\r\n|\n)/', - "REV:$newRev\n", - $vcard - ) : str_replace( - 'END:VCARD:', - "REV:$newRev\END:VCARD:", - $vcard - ); + $vcard = \Sabre\VObject\Reader::read($data, \Sabre\VObject\Reader::OPTION_FORGIVING); + $vcard->UID->setValue($newUid); + $vcard->REV->setValue($newRev); + + // Level 3 means that the document is invalid + // https://sabre.io/vobject/vcard/#validating-vcard + $level3Warnings = array_filter($vcard->validate(), function ($warning) { + return $warning['level'] === 3; + }); + + if (!empty($level3Warnings)) { + $this->logger->error('Default contact is invalid', ['warnings' => $level3Warnings]); + return; + } try { - $this->cardDav->createCard($addressBookId, 'default', $vcard, false); + $this->cardDav->createCard($addressBookId, 'default', $vcard->serialize(), false); } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); }