syncfederationaddressbooks.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace OCA\Federation;
  3. use OCA\DAV\CardDAV\SyncService;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Helper\ProgressBar;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class SyncFederationAddressBooks {
  9. /** @var DbHandler */
  10. protected $dbHandler;
  11. /** @var SyncService */
  12. private $syncService;
  13. /**
  14. * @param DbHandler $dbHandler
  15. * @param SyncService $syncService
  16. */
  17. function __construct(DbHandler $dbHandler, SyncService $syncService) {
  18. $this->syncService = $syncService;
  19. $this->dbHandler = $dbHandler;
  20. }
  21. /**
  22. * @param \Closure $callback
  23. */
  24. public function syncThemAll(\Closure $callback) {
  25. $trustedServers = $this->dbHandler->getAllServer();
  26. foreach ($trustedServers as $trustedServer) {
  27. $url = $trustedServer['url'];
  28. $callback($url, null);
  29. $sharedSecret = $trustedServer['shared_secret'];
  30. $syncToken = $trustedServer['sync_token'];
  31. if (is_null($sharedSecret)) {
  32. continue;
  33. }
  34. $targetBookId = sha1($url);
  35. $targetPrincipal = "principals/system/system";
  36. $targetBookProperties = [
  37. '{DAV:}displayname' => $url
  38. ];
  39. try {
  40. $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
  41. if ($newToken !== $syncToken) {
  42. $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
  43. }
  44. } catch (\Exception $ex) {
  45. $callback($url, $ex);
  46. }
  47. }
  48. }
  49. }