1
0

SyncFederationAddressBooks.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Federation;
  27. use OC\OCS\DiscoveryService;
  28. use OCA\DAV\CardDAV\SyncService;
  29. use OCP\AppFramework\Http;
  30. use OCP\OCS\IDiscoveryService;
  31. use Psr\Log\LoggerInterface;
  32. class SyncFederationAddressBooks {
  33. protected DbHandler $dbHandler;
  34. private SyncService $syncService;
  35. private DiscoveryService $ocsDiscoveryService;
  36. private LoggerInterface $logger;
  37. public function __construct(DbHandler $dbHandler,
  38. SyncService $syncService,
  39. IDiscoveryService $ocsDiscoveryService,
  40. LoggerInterface $logger
  41. ) {
  42. $this->syncService = $syncService;
  43. $this->dbHandler = $dbHandler;
  44. $this->ocsDiscoveryService = $ocsDiscoveryService;
  45. $this->logger = $logger;
  46. }
  47. /**
  48. * @param \Closure $callback
  49. */
  50. public function syncThemAll(\Closure $callback) {
  51. $trustedServers = $this->dbHandler->getAllServer();
  52. foreach ($trustedServers as $trustedServer) {
  53. $url = $trustedServer['url'];
  54. $callback($url, null);
  55. $sharedSecret = $trustedServer['shared_secret'];
  56. $syncToken = $trustedServer['sync_token'];
  57. $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
  58. $cardDavUser = $endPoints['carddav-user'] ?? 'system';
  59. $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
  60. if (is_null($sharedSecret)) {
  61. $this->logger->debug("Shared secret for $url is null");
  62. continue;
  63. }
  64. $targetBookId = $trustedServer['url_hash'];
  65. $targetPrincipal = "principals/system/system";
  66. $targetBookProperties = [
  67. '{DAV:}displayname' => $url
  68. ];
  69. try {
  70. $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  71. if ($newToken !== $syncToken) {
  72. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  73. } else {
  74. $this->logger->debug("Sync Token for $url unchanged from previous sync");
  75. // The server status might have been changed to a failure status in previous runs.
  76. if ($this->dbHandler->getServerStatus($url) !== TrustedServers::STATUS_OK) {
  77. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK);
  78. }
  79. }
  80. } catch (\Exception $ex) {
  81. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  82. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  83. $this->logger->error("Server sync for $url failed because of revoked access.", [
  84. 'exception' => $ex,
  85. ]);
  86. } else {
  87. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
  88. $this->logger->error("Server sync for $url failed.", [
  89. 'exception' => $ex,
  90. ]);
  91. }
  92. $callback($url, $ex);
  93. }
  94. }
  95. }
  96. }