You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SyncFederationAddressBooks.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation;
  28. use OC\OCS\DiscoveryService;
  29. use OCA\DAV\CardDAV\SyncService;
  30. use OCP\AppFramework\Http;
  31. use OCP\OCS\IDiscoveryService;
  32. class SyncFederationAddressBooks {
  33. /** @var DbHandler */
  34. protected $dbHandler;
  35. /** @var SyncService */
  36. private $syncService;
  37. /** @var DiscoveryService */
  38. private $ocsDiscoveryService;
  39. /**
  40. * @param DbHandler $dbHandler
  41. * @param SyncService $syncService
  42. * @param IDiscoveryService $ocsDiscoveryService
  43. */
  44. public function __construct(DbHandler $dbHandler,
  45. SyncService $syncService,
  46. IDiscoveryService $ocsDiscoveryService
  47. ) {
  48. $this->syncService = $syncService;
  49. $this->dbHandler = $dbHandler;
  50. $this->ocsDiscoveryService = $ocsDiscoveryService;
  51. }
  52. /**
  53. * @param \Closure $callback
  54. */
  55. public function syncThemAll(\Closure $callback) {
  56. $trustedServers = $this->dbHandler->getAllServer();
  57. foreach ($trustedServers as $trustedServer) {
  58. $url = $trustedServer['url'];
  59. $callback($url, null);
  60. $sharedSecret = $trustedServer['shared_secret'];
  61. $syncToken = $trustedServer['sync_token'];
  62. $endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
  63. $cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system';
  64. $addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
  65. if (is_null($sharedSecret)) {
  66. continue;
  67. }
  68. $targetBookId = $trustedServer['url_hash'];
  69. $targetPrincipal = "principals/system/system";
  70. $targetBookProperties = [
  71. '{DAV:}displayname' => $url
  72. ];
  73. try {
  74. $newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  75. if ($newToken !== $syncToken) {
  76. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  77. }
  78. } catch (\Exception $ex) {
  79. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  80. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  81. }
  82. $callback($url, $ex);
  83. }
  84. }
  85. }
  86. }