SyncFederationAddressBooks.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. class SyncFederationAddressBooks {
  32. protected DbHandler $dbHandler;
  33. private SyncService $syncService;
  34. private DiscoveryService $ocsDiscoveryService;
  35. public function __construct(DbHandler $dbHandler,
  36. SyncService $syncService,
  37. IDiscoveryService $ocsDiscoveryService
  38. ) {
  39. $this->syncService = $syncService;
  40. $this->dbHandler = $dbHandler;
  41. $this->ocsDiscoveryService = $ocsDiscoveryService;
  42. }
  43. /**
  44. * @param \Closure $callback
  45. */
  46. public function syncThemAll(\Closure $callback) {
  47. $trustedServers = $this->dbHandler->getAllServer();
  48. foreach ($trustedServers as $trustedServer) {
  49. $url = $trustedServer['url'];
  50. $callback($url, null);
  51. $sharedSecret = $trustedServer['shared_secret'];
  52. $syncToken = $trustedServer['sync_token'];
  53. $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
  54. $cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system';
  55. $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
  56. if (is_null($sharedSecret)) {
  57. continue;
  58. }
  59. $targetBookId = $trustedServer['url_hash'];
  60. $targetPrincipal = "principals/system/system";
  61. $targetBookProperties = [
  62. '{DAV:}displayname' => $url
  63. ];
  64. try {
  65. $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  66. if ($newToken !== $syncToken) {
  67. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  68. }
  69. } catch (\Exception $ex) {
  70. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  71. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  72. }
  73. $callback($url, $ex);
  74. }
  75. }
  76. }
  77. }