nextcloud/apps/federation/lib/SyncFederationAddressBooks.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.9 KiB
PHP
Raw Normal View History

2016-01-22 08:58:49 -05:00
<?php
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
2016-01-22 08:58:49 -05:00
namespace OCA\Federation;
use OC\OCS\DiscoveryService;
2016-01-22 08:58:49 -05:00
use OCA\DAV\CardDAV\SyncService;
use OCP\AppFramework\Http;
use OCP\OCS\IDiscoveryService;
use Psr\Log\LoggerInterface;
2016-01-22 08:58:49 -05:00
class SyncFederationAddressBooks {
private DiscoveryService $ocsDiscoveryService;
2016-01-22 08:58:49 -05:00
public function __construct(
protected DbHandler $dbHandler,
private SyncService $syncService,
IDiscoveryService $ocsDiscoveryService,
private LoggerInterface $logger,
) {
$this->ocsDiscoveryService = $ocsDiscoveryService;
2016-01-22 08:58:49 -05:00
}
2016-01-26 11:27:58 -05:00
/**
* @param \Closure $callback
*/
2016-01-22 08:58:49 -05:00
public function syncThemAll(\Closure $callback) {
$trustedServers = $this->dbHandler->getAllServer();
foreach ($trustedServers as $trustedServer) {
$url = $trustedServer['url'];
2016-01-25 05:39:57 -05:00
$callback($url, null);
2016-01-22 08:58:49 -05:00
$sharedSecret = $trustedServer['shared_secret'];
$oldSyncToken = $trustedServer['sync_token'];
2016-01-22 08:58:49 -05:00
$endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
$cardDavUser = $endPoints['carddav-user'] ?? 'system';
$addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
2016-01-22 08:58:49 -05:00
if (is_null($sharedSecret)) {
$this->logger->debug("Shared secret for $url is null");
2016-01-22 08:58:49 -05:00
continue;
}
$targetBookId = $trustedServer['url_hash'];
2016-01-22 08:58:49 -05:00
$targetPrincipal = 'principals/system/system';
$targetBookProperties = [
'{DAV:}displayname' => $url
2016-01-22 08:58:49 -05:00
];
2016-01-22 08:58:49 -05:00
try {
$syncToken = $oldSyncToken;
do {
[$syncToken, $truncated] = $this->syncService->syncRemoteAddressBook(
$url,
$cardDavUser,
$addressBookUrl,
$sharedSecret,
$syncToken,
$targetBookId,
$targetPrincipal,
$targetBookProperties
);
} while ($truncated);
if ($syncToken !== $oldSyncToken) {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $syncToken);
} else {
$this->logger->debug("Sync Token for $url unchanged from previous sync");
// The server status might have been changed to a failure status in previous runs.
if ($this->dbHandler->getServerStatus($url) !== TrustedServers::STATUS_OK) {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK);
}
2016-01-22 08:58:49 -05:00
}
} catch (\Exception $ex) {
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED);
$this->logger->error("Server sync for $url failed because of revoked access.", [
'exception' => $ex,
]);
} else {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_FAILURE);
$this->logger->error("Server sync for $url failed.", [
'exception' => $ex,
]);
}
2016-01-22 08:58:49 -05:00
$callback($url, $ex);
}
}
}
}