SyncFederationAddressBooks.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // The server status might have been changed to a failure status in previous runs.
  57. if ($this->dbHandler->getServerStatus($url) !== TrustedServers::STATUS_OK) {
  58. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK);
  59. }
  60. }
  61. } catch (\Exception $ex) {
  62. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  63. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  64. $this->logger->error("Server sync for $url failed because of revoked access.", [
  65. 'exception' => $ex,
  66. ]);
  67. } else {
  68. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
  69. $this->logger->error("Server sync for $url failed.", [
  70. 'exception' => $ex,
  71. ]);
  72. }
  73. $callback($url, $ex);
  74. }
  75. }
  76. }
  77. }