migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,63 @@
<?php
/**
* @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationNotification;
use OCP\Federation\ICloudFederationShare;
class CloudFederationFactory implements ICloudFederationFactory {
/**
* get a CloudFederationShare Object to prepare a share you want to send
*
* @param string $shareWith
* @param string $name resource name (e.g. document.odt)
* @param string $description share description (optional)
* @param string $providerId resource UID on the provider side
* @param string $owner provider specific UID of the user who owns the resource
* @param string $ownerDisplayName display name of the user who shared the item
* @param string $sharedBy provider specific UID of the user who shared the resource
* @param string $sharedByDisplayName display name of the user who shared the resource
* @param string $sharedSecret used to authenticate requests across servers
* @param string $shareType ('group' or 'user' share)
* @param $resourceType ('file', 'calendar',...)
* @return ICloudFederationShare
*
* @since 14.0.0
*/
public function getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $sharedSecret, $shareType, $resourceType) {
return new CloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $shareType, $resourceType, $sharedSecret);
}
/**
* get a Cloud FederationNotification object to prepare a notification you
* want to send
*
* @return ICloudFederationNotification
*
* @since 14.0.0
*/
public function getCloudFederationNotification() {
return new CloudFederationNotification();
}
}
@@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OCP\Federation\ICloudFederationNotification;
/**
* Class CloudFederationNotification
*
* @package OC\Federation
*
* @since 14.0.0
*/
class CloudFederationNotification implements ICloudFederationNotification {
private $message = [];
/**
* add a message to the notification
*
* @param string $notificationType (e.g. SHARE_ACCEPTED)
* @param string $resourceType (e.g. file, calendar, contact,...)
* @param string $providerId id of the share
* @param array $notification payload of the notification
*
* @since 14.0.0
*/
public function setMessage($notificationType, $resourceType, $providerId, array $notification) {
$this->message = [
'notificationType' => $notificationType,
'resourceType' => $resourceType,
'providerId' => $providerId,
'notification' => $notification,
];
}
/**
* get message, ready to send out
*
* @return array
*
* @since 14.0.0
*/
public function getMessage() {
return $this->message;
}
}
@@ -0,0 +1,191 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OC\AppFramework\Http;
use OCP\App\IAppManager;
use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
use OCP\Federation\ICloudFederationNotification;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudFederationShare;
use OCP\Federation\ICloudIdManager;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\OCM\Exceptions\OCMProviderException;
use OCP\OCM\IOCMDiscoveryService;
use Psr\Log\LoggerInterface;
/**
* Class Manager
*
* Manage Cloud Federation Providers
*
* @package OC\Federation
*/
class CloudFederationProviderManager implements ICloudFederationProviderManager {
/** @var array list of available cloud federation providers */
private array $cloudFederationProvider = [];
public function __construct(
private IConfig $config,
private IAppManager $appManager,
private IClientService $httpClientService,
private ICloudIdManager $cloudIdManager,
private IOCMDiscoveryService $discoveryService,
private LoggerInterface $logger
) {
}
/**
* Registers an callback function which must return an cloud federation provider
*
* @param string $resourceType which resource type does the provider handles
* @param string $displayName user facing name of the federated share provider
* @param callable $callback
*/
public function addCloudFederationProvider($resourceType, $displayName, callable $callback) {
$this->cloudFederationProvider[$resourceType] = [
'resourceType' => $resourceType,
'displayName' => $displayName,
'callback' => $callback,
];
}
/**
* remove cloud federation provider
*
* @param string $providerId
*/
public function removeCloudFederationProvider($providerId) {
unset($this->cloudFederationProvider[$providerId]);
}
/**
* get a list of all cloudFederationProviders
*
* @return array [resourceType => ['resourceType' => $resourceType, 'displayName' => $displayName, 'callback' => callback]]
*/
public function getAllCloudFederationProviders() {
return $this->cloudFederationProvider;
}
/**
* get a specific cloud federation provider
*
* @param string $resourceType
* @return ICloudFederationProvider
* @throws ProviderDoesNotExistsException
*/
public function getCloudFederationProvider($resourceType) {
if (isset($this->cloudFederationProvider[$resourceType])) {
return call_user_func($this->cloudFederationProvider[$resourceType]['callback']);
} else {
throw new ProviderDoesNotExistsException($resourceType);
}
}
public function sendShare(ICloudFederationShare $share) {
$cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith());
try {
$ocmProvider = $this->discoveryService->discover($cloudID->getRemote());
} catch (OCMProviderException $e) {
return false;
}
$client = $this->httpClientService->newClient();
try {
$response = $client->post($ocmProvider->getEndPoint() . '/shares', [
'body' => json_encode($share->getShare()),
'headers' => ['content-type' => 'application/json'],
'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
'timeout' => 10,
'connect_timeout' => 10,
]);
if ($response->getStatusCode() === Http::STATUS_CREATED) {
$result = json_decode($response->getBody(), true);
return (is_array($result)) ? $result : [];
}
} catch (\Exception $e) {
$this->logger->debug($e->getMessage(), ['exception' => $e]);
// if flat re-sharing is not supported by the remote server
// we re-throw the exception and fall back to the old behaviour.
// (flat re-shares has been introduced in Nextcloud 9.1)
if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
throw $e;
}
}
return false;
}
/**
* @param string $url
* @param ICloudFederationNotification $notification
* @return array|false
*/
public function sendNotification($url, ICloudFederationNotification $notification) {
try {
$ocmProvider = $this->discoveryService->discover($url);
} catch (OCMProviderException $e) {
return false;
}
$client = $this->httpClientService->newClient();
try {
$response = $client->post($ocmProvider->getEndPoint() . '/notifications', [
'body' => json_encode($notification->getMessage()),
'headers' => ['content-type' => 'application/json'],
'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
'timeout' => 10,
'connect_timeout' => 10,
]);
if ($response->getStatusCode() === Http::STATUS_CREATED) {
$result = json_decode($response->getBody(), true);
return (is_array($result)) ? $result : [];
}
} catch (\Exception $e) {
// log the error and return false
$this->logger->error('error while sending notification for federated share: ' . $e->getMessage(), ['exception' => $e]);
}
return false;
}
/**
* check if the new cloud federation API is ready to be used
*
* @return bool
*/
public function isReady() {
return $this->appManager->isEnabledForUser('cloud_federation_api');
}
}
@@ -0,0 +1,357 @@
<?php
/**
* @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OCP\Federation\ICloudFederationShare;
use OCP\Share\IShare;
class CloudFederationShare implements ICloudFederationShare {
private $share = [
'shareWith' => '',
'shareType' => '',
'name' => '',
'resourceType' => '',
'description' => '',
'providerId' => '',
'owner' => '',
'ownerDisplayName' => '',
'sharedBy' => '',
'sharedByDisplayName' => '',
'protocol' => []
];
/**
* get a CloudFederationShare Object to prepare a share you want to send
*
* @param string $shareWith
* @param string $name resource name (e.g. document.odt)
* @param string $description share description (optional)
* @param string $providerId resource UID on the provider side
* @param string $owner provider specific UID of the user who owns the resource
* @param string $ownerDisplayName display name of the user who shared the item
* @param string $sharedBy provider specific UID of the user who shared the resource
* @param string $sharedByDisplayName display name of the user who shared the resource
* @param string $shareType ('group' or 'user' share)
* @param string $resourceType ('file', 'calendar',...)
* @param string $sharedSecret
*/
public function __construct($shareWith = '',
$name = '',
$description = '',
$providerId = '',
$owner = '',
$ownerDisplayName = '',
$sharedBy = '',
$sharedByDisplayName = '',
$shareType = '',
$resourceType = '',
$sharedSecret = ''
) {
$this->setShareWith($shareWith);
$this->setResourceName($name);
$this->setDescription($description);
$this->setProviderId($providerId);
$this->setOwner($owner);
$this->setOwnerDisplayName($ownerDisplayName);
$this->setSharedBy($sharedBy);
$this->setSharedByDisplayName($sharedByDisplayName);
$this->setProtocol([
'name' => 'webdav',
'options' => [
'sharedSecret' => $sharedSecret,
'permissions' => '{http://open-cloud-mesh.org/ns}share-permissions'
]
]);
$this->setShareType($shareType);
$this->setResourceType($resourceType);
}
/**
* set uid of the recipient
*
* @param string $user
*
* @since 14.0.0
*/
public function setShareWith($user) {
$this->share['shareWith'] = $user;
}
/**
* set resource name (e.g. document.odt)
*
* @param string $name
*
* @since 14.0.0
*/
public function setResourceName($name) {
$this->share['name'] = $name;
}
/**
* set resource type (e.g. file, calendar, contact,...)
*
* @param string $resourceType
*
* @since 14.0.0
*/
public function setResourceType($resourceType) {
$this->share['resourceType'] = $resourceType;
}
/**
* set resource description (optional)
*
* @param string $description
*
* @since 14.0.0
*/
public function setDescription($description) {
$this->share['description'] = $description;
}
/**
* set provider ID (e.g. file ID)
*
* @param string $providerId
*
* @since 14.0.0
*/
public function setProviderId($providerId) {
$this->share['providerId'] = $providerId;
}
/**
* set owner UID
*
* @param string $owner
*
* @since 14.0.0
*/
public function setOwner($owner) {
$this->share['owner'] = $owner;
}
/**
* set owner display name
*
* @param string $ownerDisplayName
*
* @since 14.0.0
*/
public function setOwnerDisplayName($ownerDisplayName) {
$this->share['ownerDisplayName'] = $ownerDisplayName;
}
/**
* set UID of the user who sends the share
*
* @param string $sharedBy
*
* @since 14.0.0
*/
public function setSharedBy($sharedBy) {
$this->share['sharedBy'] = $sharedBy;
}
/**
* set display name of the user who sends the share
*
* @param $sharedByDisplayName
*
* @since 14.0.0
*/
public function setSharedByDisplayName($sharedByDisplayName) {
$this->share['sharedByDisplayName'] = $sharedByDisplayName;
}
/**
* set protocol specification
*
* @param array $protocol
*
* @since 14.0.0
*/
public function setProtocol(array $protocol) {
$this->share['protocol'] = $protocol;
}
/**
* share type (group or user)
*
* @param string $shareType
*
* @since 14.0.0
*/
public function setShareType($shareType) {
if ($shareType === 'group' || $shareType === IShare::TYPE_REMOTE_GROUP) {
$this->share['shareType'] = 'group';
} else {
$this->share['shareType'] = 'user';
}
}
/**
* get the whole share, ready to send out
*
* @return array
*
* @since 14.0.0
*/
public function getShare() {
return $this->share;
}
/**
* get uid of the recipient
*
* @return string
*
* @since 14.0.0
*/
public function getShareWith() {
return $this->share['shareWith'];
}
/**
* get resource name (e.g. file, calendar, contact,...)
*
* @return string
*
* @since 14.0.0
*/
public function getResourceName() {
return $this->share['name'];
}
/**
* get resource type (e.g. file, calendar, contact,...)
*
* @return string
*
* @since 14.0.0
*/
public function getResourceType() {
return $this->share['resourceType'];
}
/**
* get resource description (optional)
*
* @return string
*
* @since 14.0.0
*/
public function getDescription() {
return $this->share['description'];
}
/**
* get provider ID (e.g. file ID)
*
* @return string
*
* @since 14.0.0
*/
public function getProviderId() {
return $this->share['providerId'];
}
/**
* get owner UID
*
* @return string
*
* @since 14.0.0
*/
public function getOwner() {
return $this->share['owner'];
}
/**
* get owner display name
*
* @return string
*
* @since 14.0.0
*/
public function getOwnerDisplayName() {
return $this->share['ownerDisplayName'];
}
/**
* get UID of the user who sends the share
*
* @return string
*
* @since 14.0.0
*/
public function getSharedBy() {
return $this->share['sharedBy'];
}
/**
* get display name of the user who sends the share
*
* @return string
*
* @since 14.0.0
*/
public function getSharedByDisplayName() {
return $this->share['sharedByDisplayName'];
}
/**
* get share type (group or user)
*
* @return string
*
* @since 14.0.0
*/
public function getShareType() {
return $this->share['shareType'];
}
/**
* get share Secret
*
* @return string
*
* @since 14.0.0
*/
public function getShareSecret() {
return $this->share['protocol']['options']['sharedSecret'];
}
/**
* get protocol specification
*
* @return array
*
* @since 14.0.0
*/
public function getProtocol() {
return $this->share['protocol'];
}
}
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OCP\Federation\ICloudId;
class CloudId implements ICloudId {
/** @var string */
private $id;
/** @var string */
private $user;
/** @var string */
private $remote;
/** @var string|null */
private $displayName;
/**
* CloudId constructor.
*
* @param string $id
* @param string $user
* @param string $remote
*/
public function __construct(string $id, string $user, string $remote, ?string $displayName = null) {
$this->id = $id;
$this->user = $user;
$this->remote = $remote;
$this->displayName = $displayName;
}
/**
* The full remote cloud id
*
* @return string
*/
public function getId(): string {
return $this->id;
}
public function getDisplayId(): string {
if ($this->displayName) {
$atPos = strrpos($this->getId(), '@');
$atHost = substr($this->getId(), $atPos);
return $this->displayName . $atHost;
}
return str_replace('https://', '', str_replace('http://', '', $this->getId()));
}
/**
* The username on the remote server
*
* @return string
*/
public function getUser(): string {
return $this->user;
}
/**
* The base address of the remote server
*
* @return string
*/
public function getRemote(): string {
return $this->remote;
}
}
@@ -0,0 +1,252 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Guillaume Virlet <github@virlet.org>
* @author Joas Schilling <coding@schilljs.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Federation;
use OCA\DAV\Events\CardUpdatedEvent;
use OCP\Contacts\IManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\User\Events\UserChangedEvent;
class CloudIdManager implements ICloudIdManager {
/** @var IManager */
private $contactsManager;
/** @var IURLGenerator */
private $urlGenerator;
/** @var IUserManager */
private $userManager;
private ICache $memCache;
/** @var array[] */
private array $cache = [];
public function __construct(
IManager $contactsManager,
IURLGenerator $urlGenerator,
IUserManager $userManager,
ICacheFactory $cacheFactory,
IEventDispatcher $eventDispatcher
) {
$this->contactsManager = $contactsManager;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->memCache = $cacheFactory->createDistributed('cloud_id_');
$eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']);
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
}
public function handleUserEvent(Event $event): void {
if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
$userId = $event->getUser()->getUID();
$key = $userId . '@local';
unset($this->cache[$key]);
$this->memCache->remove($key);
}
}
public function handleCardEvent(Event $event): void {
if ($event instanceof CardUpdatedEvent) {
$data = $event->getCardData()['carddata'];
foreach (explode("\r\n", $data) as $line) {
if (str_starts_with($line, "CLOUD;")) {
$parts = explode(':', $line, 2);
if (isset($parts[1])) {
$key = $parts[1];
unset($this->cache[$key]);
$this->memCache->remove($key);
}
}
}
}
}
/**
* @param string $cloudId
* @return ICloudId
* @throws \InvalidArgumentException
*/
public function resolveCloudId(string $cloudId): ICloudId {
// TODO magic here to get the url and user instead of just splitting on @
if (!$this->isValidCloudId($cloudId)) {
throw new \InvalidArgumentException('Invalid cloud id');
}
// Find the first character that is not allowed in user names
$id = $this->fixRemoteURL($cloudId);
$posSlash = strpos($id, '/');
$posColon = strpos($id, ':');
if ($posSlash === false && $posColon === false) {
$invalidPos = \strlen($id);
} elseif ($posSlash === false) {
$invalidPos = $posColon;
} elseif ($posColon === false) {
$invalidPos = $posSlash;
} else {
$invalidPos = min($posSlash, $posColon);
}
$lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id));
if ($lastValidAtPos !== false) {
$user = substr($id, 0, $lastValidAtPos);
$remote = substr($id, $lastValidAtPos + 1);
$this->userManager->validateUserId($user);
if (!empty($user) && !empty($remote)) {
return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
}
}
throw new \InvalidArgumentException('Invalid cloud id');
}
protected function getDisplayNameFromContact(string $cloudId): ?string {
$addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD'], [
'limit' => 1,
'enumeration' => false,
'fullmatch' => false,
'strict_search' => true,
]);
foreach ($addressBookEntries as $entry) {
if (isset($entry['CLOUD'])) {
foreach ($entry['CLOUD'] as $cloudID) {
if ($cloudID === $cloudId) {
// Warning, if user decides to make his full name local only,
// no FN is found on federated servers
if (isset($entry['FN'])) {
return $entry['FN'];
} else {
return $cloudID;
}
}
}
}
}
return null;
}
/**
* @param string $user
* @param string|null $remote
* @return CloudId
*/
public function getCloudId(string $user, ?string $remote): ICloudId {
$isLocal = $remote === null;
if ($isLocal) {
$remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/');
$fixedRemote = $this->fixRemoteURL($remote);
$host = $fixedRemote;
} else {
// note that for remote id's we don't strip the protocol for the remote we use to construct the CloudId
// this way if a user has an explicit non-https cloud id this will be preserved
// we do still use the version without protocol for looking up the display name
$fixedRemote = $this->fixRemoteURL($remote);
$host = $this->removeProtocolFromUrl($fixedRemote);
}
$key = $user . '@' . ($isLocal ? 'local' : $host);
$cached = $this->cache[$key] ?? $this->memCache->get($key);
if ($cached) {
$this->cache[$key] = $cached; // put items from memcache into local cache
return new CloudId($cached['id'], $cached['user'], $cached['remote'], $cached['displayName']);
}
if ($isLocal) {
$localUser = $this->userManager->get($user);
$displayName = $localUser ? $localUser->getDisplayName() : '';
} else {
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
}
$id = $user . '@' . $remote;
$data = [
'id' => $id,
'user' => $user,
'remote' => $fixedRemote,
'displayName' => $displayName,
];
$this->cache[$key] = $data;
$this->memCache->set($key, $data, 15 * 60);
return new CloudId($id, $user, $fixedRemote, $displayName);
}
/**
* @param string $url
* @return string
*/
public function removeProtocolFromUrl(string $url): string {
if (str_starts_with($url, 'https://')) {
return substr($url, 8);
}
if (str_starts_with($url, 'http://')) {
return substr($url, 7);
}
return $url;
}
/**
* Strips away a potential file names and trailing slashes:
* - http://localhost
* - http://localhost/
* - http://localhost/index.php
* - http://localhost/index.php/s/{shareToken}
*
* all return: http://localhost
*
* @param string $remote
* @return string
*/
protected function fixRemoteURL(string $remote): string {
$remote = str_replace('\\', '/', $remote);
if ($fileNamePosition = strpos($remote, '/index.php')) {
$remote = substr($remote, 0, $fileNamePosition);
}
$remote = rtrim($remote, '/');
return $remote;
}
/**
* @param string $cloudId
* @return bool
*/
public function isValidCloudId(string $cloudId): bool {
return str_contains($cloudId, '@');
}
}