SyncFederationAddressBooks.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Federation;
  25. use OCA\DAV\CardDAV\SyncService;
  26. use OCP\AppFramework\Http;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Helper\ProgressBar;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class SyncFederationAddressBooks {
  32. /** @var DbHandler */
  33. protected $dbHandler;
  34. /** @var SyncService */
  35. private $syncService;
  36. /**
  37. * @param DbHandler $dbHandler
  38. * @param SyncService $syncService
  39. */
  40. function __construct(DbHandler $dbHandler, SyncService $syncService) {
  41. $this->syncService = $syncService;
  42. $this->dbHandler = $dbHandler;
  43. }
  44. /**
  45. * @param \Closure $callback
  46. */
  47. public function syncThemAll(\Closure $callback) {
  48. $trustedServers = $this->dbHandler->getAllServer();
  49. foreach ($trustedServers as $trustedServer) {
  50. $url = $trustedServer['url'];
  51. $callback($url, null);
  52. $sharedSecret = $trustedServer['shared_secret'];
  53. $syncToken = $trustedServer['sync_token'];
  54. if (is_null($sharedSecret)) {
  55. continue;
  56. }
  57. $targetBookId = $trustedServer['url_hash'];
  58. $targetPrincipal = "principals/system/system";
  59. $targetBookProperties = [
  60. '{DAV:}displayname' => $url
  61. ];
  62. try {
  63. $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  64. if ($newToken !== $syncToken) {
  65. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  66. }
  67. } catch (\Exception $ex) {
  68. if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
  69. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
  70. }
  71. $callback($url, $ex);
  72. }
  73. }
  74. }
  75. }