1
0

SyncFederationAddressBooks.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  76. } catch (\Exception $ex) {
  77. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  78. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  79. $this->logger->error("Server sync for $url failed because of revoked access.", [
  80. 'exception' => $ex,
  81. ]);
  82. } else {
  83. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
  84. $this->logger->error("Server sync for $url failed.", [
  85. 'exception' => $ex,
  86. ]);
  87. }
  88. $callback($url, $ex);
  89. }
  90. }
  91. }
  92. }