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

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