ContactsMigrator.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\UserMigration;
  25. use function Safe\sort;
  26. use function Safe\substr;
  27. use OCA\DAV\AppInfo\Application;
  28. use OCA\DAV\CardDAV\CardDavBackend;
  29. use OCA\DAV\CardDAV\Plugin as CardDAVPlugin;
  30. use OCA\DAV\Connector\Sabre\CachingTree;
  31. use OCA\DAV\Connector\Sabre\Server as SabreDavServer;
  32. use OCA\DAV\RootCollection;
  33. use OCP\IL10N;
  34. use OCP\IUser;
  35. use OCP\UserMigration\IExportDestination;
  36. use OCP\UserMigration\IImportSource;
  37. use OCP\UserMigration\IMigrator;
  38. use OCP\UserMigration\ISizeEstimationMigrator;
  39. use OCP\UserMigration\TMigratorBasicVersionHandling;
  40. use Sabre\VObject\Component\VCard;
  41. use Sabre\VObject\Parser\Parser as VObjectParser;
  42. use Sabre\VObject\Reader as VObjectReader;
  43. use Sabre\VObject\Splitter\VCard as VCardSplitter;
  44. use Sabre\VObject\UUIDUtil;
  45. use Safe\Exceptions\ArrayException;
  46. use Safe\Exceptions\StringsException;
  47. use Symfony\Component\Console\Output\NullOutput;
  48. use Symfony\Component\Console\Output\OutputInterface;
  49. use Throwable;
  50. class ContactsMigrator implements IMigrator, ISizeEstimationMigrator {
  51. use TMigratorBasicVersionHandling;
  52. private CardDavBackend $cardDavBackend;
  53. private IL10N $l10n;
  54. private SabreDavServer $sabreDavServer;
  55. private const USERS_URI_ROOT = 'principals/users/';
  56. private const FILENAME_EXT = 'vcf';
  57. private const METADATA_EXT = 'json';
  58. private const MIGRATED_URI_PREFIX = 'migrated-';
  59. private const PATH_ROOT = Application::APP_ID . '/address_books/';
  60. public function __construct(
  61. CardDavBackend $cardDavBackend,
  62. IL10N $l10n
  63. ) {
  64. $this->cardDavBackend = $cardDavBackend;
  65. $this->l10n = $l10n;
  66. $root = new RootCollection();
  67. $this->sabreDavServer = new SabreDavServer(new CachingTree($root));
  68. $this->sabreDavServer->addPlugin(new CardDAVPlugin());
  69. }
  70. private function getPrincipalUri(IUser $user): string {
  71. return ContactsMigrator::USERS_URI_ROOT . $user->getUID();
  72. }
  73. /**
  74. * @return array{name: string, displayName: string, description: ?string, vCards: VCard[]}
  75. *
  76. * @throws InvalidAddressBookException
  77. */
  78. private function getAddressBookExportData(IUser $user, array $addressBookInfo, OutputInterface $output): array {
  79. $userId = $user->getUID();
  80. if (!isset($addressBookInfo['uri'])) {
  81. throw new InvalidAddressBookException();
  82. }
  83. $uri = $addressBookInfo['uri'];
  84. $path = CardDAVPlugin::ADDRESSBOOK_ROOT . "/users/$userId/$uri";
  85. /**
  86. * @see \Sabre\CardDAV\VCFExportPlugin::httpGet() implementation reference
  87. */
  88. $addressBookDataProp = '{' . CardDAVPlugin::NS_CARDDAV . '}address-data';
  89. $addressBookNode = $this->sabreDavServer->tree->getNodeForPath($path);
  90. $nodes = $this->sabreDavServer->getPropertiesIteratorForPath($path, [$addressBookDataProp], 1);
  91. /**
  92. * @see \Sabre\CardDAV\VCFExportPlugin::generateVCF() implementation reference
  93. */
  94. /** @var VCard[] $vCards */
  95. $vCards = [];
  96. foreach ($nodes as $node) {
  97. if (isset($node[200][$addressBookDataProp])) {
  98. $vCard = VObjectReader::read($node[200][$addressBookDataProp]);
  99. $problems = $vCard->validate();
  100. if (!empty($problems)) {
  101. $output->writeln('Skipping contact "' . ($vCard->FN ?? 'null') . '" containing invalid contact data');
  102. continue;
  103. }
  104. $vCards[] = $vCard;
  105. }
  106. }
  107. if (count($vCards) === 0) {
  108. throw new InvalidAddressBookException();
  109. }
  110. return [
  111. 'name' => $addressBookNode->getName(),
  112. 'displayName' => $addressBookInfo['{DAV:}displayname'],
  113. 'description' => $addressBookInfo['{' . CardDAVPlugin::NS_CARDDAV . '}addressbook-description'],
  114. 'vCards' => $vCards,
  115. ];
  116. }
  117. /**
  118. * @return array<int, array{name: string, displayName: string, description: ?string, vCards: VCard[]}>
  119. */
  120. private function getAddressBookExports(IUser $user, OutputInterface $output): array {
  121. $principalUri = $this->getPrincipalUri($user);
  122. return array_values(array_filter(array_map(
  123. function (array $addressBookInfo) use ($user, $output) {
  124. try {
  125. return $this->getAddressBookExportData($user, $addressBookInfo, $output);
  126. } catch (InvalidAddressBookException $e) {
  127. // Allow this exception as invalid address books are not to be exported
  128. return null;
  129. }
  130. },
  131. $this->cardDavBackend->getAddressBooksForUser($principalUri),
  132. )));
  133. }
  134. private function getUniqueAddressBookUri(IUser $user, string $initialAddressBookUri): string {
  135. $principalUri = $this->getPrincipalUri($user);
  136. try {
  137. $initialAddressBookUri = substr($initialAddressBookUri, 0, strlen(ContactsMigrator::MIGRATED_URI_PREFIX)) === ContactsMigrator::MIGRATED_URI_PREFIX
  138. ? $initialAddressBookUri
  139. : ContactsMigrator::MIGRATED_URI_PREFIX . $initialAddressBookUri;
  140. } catch (StringsException $e) {
  141. throw new ContactsMigratorException('Failed to get unique address book URI', 0, $e);
  142. }
  143. $existingAddressBookUris = array_map(
  144. fn (array $addressBookInfo): string => $addressBookInfo['uri'],
  145. $this->cardDavBackend->getAddressBooksForUser($principalUri),
  146. );
  147. $addressBookUri = $initialAddressBookUri;
  148. $acc = 1;
  149. while (in_array($addressBookUri, $existingAddressBookUris, true)) {
  150. $addressBookUri = $initialAddressBookUri . "-$acc";
  151. ++$acc;
  152. }
  153. return $addressBookUri;
  154. }
  155. /**
  156. * @param VCard[] $vCards
  157. */
  158. private function serializeCards(array $vCards): string {
  159. return array_reduce(
  160. $vCards,
  161. fn (string $addressBookBlob, VCard $vCard) => $addressBookBlob . $vCard->serialize(),
  162. '',
  163. );
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public function getEstimatedExportSize(IUser $user): int {
  169. $addressBookExports = $this->getAddressBookExports($user, new NullOutput());
  170. $addressBookCount = count($addressBookExports);
  171. // 50B for each metadata JSON
  172. $size = ($addressBookCount * 50) / 1024;
  173. $contactsCount = array_sum(array_map(
  174. fn (array $data): int => count($data['vCards']),
  175. $addressBookExports,
  176. ));
  177. // 350B for each contact
  178. $size += ($contactsCount * 350) / 1024;
  179. return (int)ceil($size);
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
  185. $output->writeln('Exporting contacts into ' . ContactsMigrator::PATH_ROOT . '…');
  186. $addressBookExports = $this->getAddressBookExports($user, $output);
  187. if (empty($addressBookExports)) {
  188. $output->writeln('No contacts to export…');
  189. }
  190. try {
  191. /**
  192. * @var string $name
  193. * @var string $displayName
  194. * @var ?string $description
  195. * @var VCard[] $vCards
  196. */
  197. foreach ($addressBookExports as ['name' => $name, 'displayName' => $displayName, 'description' => $description, 'vCards' => $vCards]) {
  198. // Set filename to sanitized address book name
  199. $basename = preg_replace('/[^a-z0-9-_]/iu', '', $name);
  200. $exportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::FILENAME_EXT;
  201. $metadataExportPath = ContactsMigrator::PATH_ROOT . $basename . '.' . ContactsMigrator::METADATA_EXT;
  202. $exportDestination->addFileContents($exportPath, $this->serializeCards($vCards));
  203. $metadata = array_filter(['displayName' => $displayName, 'description' => $description]);
  204. $exportDestination->addFileContents($metadataExportPath, json_encode($metadata));
  205. }
  206. } catch (Throwable $e) {
  207. throw new CalendarMigratorException('Could not export address book', 0, $e);
  208. }
  209. }
  210. private function importContact(int $addressBookId, VCard $vCard, string $filename, OutputInterface $output): void {
  211. // Operate on clone to prevent mutation of the original
  212. $vCard = clone $vCard;
  213. $vCard->PRODID = '-//IDN nextcloud.com//Migrated contact//EN';
  214. try {
  215. $this->cardDavBackend->createCard(
  216. $addressBookId,
  217. UUIDUtil::getUUID() . '.' . ContactsMigrator::FILENAME_EXT,
  218. $vCard->serialize(),
  219. );
  220. } catch (Throwable $e) {
  221. $output->writeln("Error creating contact \"" . ($vCard->FN ?? 'null') . "\" from \"$filename\", skipping…");
  222. }
  223. }
  224. /**
  225. * @param array{displayName: string, description?: string} $metadata
  226. * @param VCard[] $vCards
  227. */
  228. private function importAddressBook(IUser $user, string $filename, string $initialAddressBookUri, array $metadata, array $vCards, OutputInterface $output): void {
  229. $principalUri = $this->getPrincipalUri($user);
  230. $addressBookUri = $this->getUniqueAddressBookUri($user, $initialAddressBookUri);
  231. $addressBookId = $this->cardDavBackend->createAddressBook($principalUri, $addressBookUri, array_filter([
  232. '{DAV:}displayname' => $metadata['displayName'],
  233. '{' . CardDAVPlugin::NS_CARDDAV . '}addressbook-description' => $metadata['description'] ?? null,
  234. ]));
  235. foreach ($vCards as $vCard) {
  236. $this->importContact($addressBookId, $vCard, $filename, $output);
  237. }
  238. }
  239. /**
  240. * @return array<int, array{addressBook: string, metadata: string}>
  241. */
  242. private function getAddressBookImports(array $importFiles): array {
  243. $addressBookImports = array_filter(
  244. $importFiles,
  245. fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === ContactsMigrator::FILENAME_EXT,
  246. );
  247. $metadataImports = array_filter(
  248. $importFiles,
  249. fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === ContactsMigrator::METADATA_EXT,
  250. );
  251. try {
  252. sort($addressBookImports);
  253. sort($metadataImports);
  254. } catch (ArrayException $e) {
  255. throw new ContactsMigratorException('Failed to sort address book files in ' . ContactsMigrator::PATH_ROOT, 0, $e);
  256. }
  257. if (count($addressBookImports) !== count($metadataImports)) {
  258. throw new ContactsMigratorException('Each ' . ContactsMigrator::FILENAME_EXT . ' file must have a corresponding ' . ContactsMigrator::METADATA_EXT . ' file');
  259. }
  260. for ($i = 0; $i < count($addressBookImports); ++$i) {
  261. if (pathinfo($addressBookImports[$i], PATHINFO_FILENAME) !== pathinfo($metadataImports[$i], PATHINFO_FILENAME)) {
  262. throw new ContactsMigratorException('Each ' . ContactsMigrator::FILENAME_EXT . ' file must have a corresponding ' . ContactsMigrator::METADATA_EXT . ' file');
  263. }
  264. }
  265. return array_map(
  266. fn (string $addressBookFilename, string $metadataFilename) => ['addressBook' => $addressBookFilename, 'metadata' => $metadataFilename],
  267. $addressBookImports,
  268. $metadataImports,
  269. );
  270. }
  271. /**
  272. * {@inheritDoc}
  273. *
  274. * @throws ContactsMigratorException
  275. */
  276. public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
  277. if ($importSource->getMigratorVersion($this->getId()) === null) {
  278. $output->writeln('No version for ' . static::class . ', skipping import…');
  279. return;
  280. }
  281. $output->writeln('Importing contacts from ' . ContactsMigrator::PATH_ROOT . '…');
  282. $importFiles = $importSource->getFolderListing(ContactsMigrator::PATH_ROOT);
  283. if (empty($importFiles)) {
  284. $output->writeln('No contacts to import…');
  285. }
  286. foreach ($this->getAddressBookImports($importFiles) as ['addressBook' => $addressBookFilename, 'metadata' => $metadataFilename]) {
  287. $addressBookImportPath = ContactsMigrator::PATH_ROOT . $addressBookFilename;
  288. $metadataImportPath = ContactsMigrator::PATH_ROOT . $metadataFilename;
  289. $vCardSplitter = new VCardSplitter(
  290. $importSource->getFileAsStream($addressBookImportPath),
  291. VObjectParser::OPTION_FORGIVING,
  292. );
  293. /** @var VCard[] $vCards */
  294. $vCards = [];
  295. /** @var ?VCard $vCard */
  296. while ($vCard = $vCardSplitter->getNext()) {
  297. $problems = $vCard->validate();
  298. if (!empty($problems)) {
  299. $output->writeln('Skipping contact "' . ($vCard->FN ?? 'null') . '" containing invalid contact data');
  300. continue;
  301. }
  302. $vCards[] = $vCard;
  303. }
  304. $splitFilename = explode('.', $addressBookFilename, 2);
  305. if (count($splitFilename) !== 2) {
  306. throw new ContactsMigratorException("Invalid filename \"$addressBookFilename\", expected filename of the format \"<address_book_name>." . ContactsMigrator::FILENAME_EXT . '"');
  307. }
  308. [$initialAddressBookUri, $ext] = $splitFilename;
  309. /** @var array{displayName: string, description?: string} $metadata */
  310. $metadata = json_decode($importSource->getFileContents($metadataImportPath), true, 512, JSON_THROW_ON_ERROR);
  311. $this->importAddressBook(
  312. $user,
  313. $addressBookFilename,
  314. $initialAddressBookUri,
  315. $metadata,
  316. $vCards,
  317. $output,
  318. );
  319. foreach ($vCards as $vCard) {
  320. $vCard->destroy();
  321. }
  322. }
  323. }
  324. /**
  325. * {@inheritDoc}
  326. */
  327. public function getId(): string {
  328. return 'contacts';
  329. }
  330. /**
  331. * {@inheritDoc}
  332. */
  333. public function getDisplayName(): string {
  334. return $this->l10n->t('Contacts');
  335. }
  336. /**
  337. * {@inheritDoc}
  338. */
  339. public function getDescription(): string {
  340. return $this->l10n->t('Contacts and groups');
  341. }
  342. }