1
0

SyncFederationAddressBooks.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation;
  8. use OC\OCS\DiscoveryService;
  9. use OCA\DAV\CardDAV\SyncService;
  10. use OCP\AppFramework\Http;
  11. use OCP\OCS\IDiscoveryService;
  12. use Psr\Log\LoggerInterface;
  13. class SyncFederationAddressBooks {
  14. protected DbHandler $dbHandler;
  15. private SyncService $syncService;
  16. private DiscoveryService $ocsDiscoveryService;
  17. private LoggerInterface $logger;
  18. public function __construct(DbHandler $dbHandler,
  19. SyncService $syncService,
  20. IDiscoveryService $ocsDiscoveryService,
  21. LoggerInterface $logger
  22. ) {
  23. $this->syncService = $syncService;
  24. $this->dbHandler = $dbHandler;
  25. $this->ocsDiscoveryService = $ocsDiscoveryService;
  26. $this->logger = $logger;
  27. }
  28. /**
  29. * @param \Closure $callback
  30. */
  31. public function syncThemAll(\Closure $callback) {
  32. $trustedServers = $this->dbHandler->getAllServer();
  33. foreach ($trustedServers as $trustedServer) {
  34. $url = $trustedServer['url'];
  35. $callback($url, null);
  36. $sharedSecret = $trustedServer['shared_secret'];
  37. $syncToken = $trustedServer['sync_token'];
  38. $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
  39. $cardDavUser = $endPoints['carddav-user'] ?? 'system';
  40. $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
  41. if (is_null($sharedSecret)) {
  42. $this->logger->debug("Shared secret for $url is null");
  43. continue;
  44. }
  45. $targetBookId = $trustedServer['url_hash'];
  46. $targetPrincipal = "principals/system/system";
  47. $targetBookProperties = [
  48. '{DAV:}displayname' => $url
  49. ];
  50. try {
  51. $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  52. if ($newToken !== $syncToken) {
  53. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  54. } else {
  55. $this->logger->debug("Sync Token for $url unchanged from previous sync");
  56. }
  57. } catch (\Exception $ex) {
  58. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  59. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  60. $this->logger->error("Server sync for $url failed because of revoked access.", [
  61. 'exception' => $ex,
  62. ]);
  63. } else {
  64. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
  65. $this->logger->error("Server sync for $url failed.", [
  66. 'exception' => $ex,
  67. ]);
  68. }
  69. $callback($url, $ex);
  70. }
  71. }
  72. }
  73. }