diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 4aa0ea09f..59cc3668f 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -53,6 +53,7 @@ class ConfigService { use TArrayTools; + public const FRONTAL_CLOUD_BASE = 'frontal_cloud_base'; public const FRONTAL_CLOUD_ID = 'frontal_cloud_id'; public const FRONTAL_CLOUD_SCHEME = 'frontal_cloud_scheme'; public const INTERNAL_CLOUD_ID = 'internal_cloud_id'; @@ -115,6 +116,7 @@ class ConfigService { private $defaults = [ + self::FRONTAL_CLOUD_BASE => '', self::FRONTAL_CLOUD_ID => '', self::FRONTAL_CLOUD_SCHEME => 'https', self::INTERNAL_CLOUD_ID => '', diff --git a/lib/Service/InterfaceService.php b/lib/Service/InterfaceService.php index 1cdab371e..6df08b960 100644 --- a/lib/Service/InterfaceService.php +++ b/lib/Service/InterfaceService.php @@ -31,8 +31,10 @@ namespace OCA\Circles\Service; +use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger; use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools; use ArtificialOwl\MySmallPhpTools\Traits\TStringTools; +use OCA\Circles\AppInfo\Application; use OCA\Circles\Db\RemoteRequest; use OCA\Circles\Exceptions\RemoteNotFoundException; use OCA\Circles\Exceptions\UnknownInterfaceException; @@ -67,6 +69,7 @@ class InterfaceService { use TStringTools; use TArrayTools; + use TNC22Logger; /** @var IURLGenerator */ @@ -101,6 +104,8 @@ public function __construct( $this->urlGenerator = $urlGenerator; $this->remoteRequest = $remoteRequest; $this->configService = $configService; + + $this->setup('app', Application::APP_ID); } @@ -224,6 +229,50 @@ public function setCurrentInterfaceFromInstance(string $instance): void { } + /** + * @param int $interface + * + * @return bool + */ + public function isInterfaceConfigured(int $interface): bool { + try { + $config = $this->getCloudIdConfigKey($interface); + } catch (UnknownInterfaceException $e) { + return false; + } + + return ($this->configService->getAppValue($config) !== ''); + } + + + /** + * @param int $interface + * + * @return string + * @throws UnknownInterfaceException + */ + private function getCloudIdConfigKey(int $interface): string { + switch ($interface) { + case self::IFACE_INTERNAL: + return ConfigService::INTERNAL_CLOUD_ID; + case self::IFACE_FRONTAL: + return ConfigService::FRONTAL_CLOUD_ID; + case self::IFACE0: + return ConfigService::IFACE0_CLOUD_ID; + case self::IFACE1: + return ConfigService::IFACE1_CLOUD_ID; + case self::IFACE2: + return ConfigService::IFACE2_CLOUD_ID; + case self::IFACE3: + return ConfigService::IFACE3_CLOUD_ID; + case self::IFACE4: + return ConfigService::IFACE4_CLOUD_ID; + } + + throw new UnknownInterfaceException('unknown interface'); + } + + /** * @param bool $useString * @@ -288,8 +337,12 @@ public function getInternalInterfaces(bool $useString = false): array { * * @throws UnknownInterfaceException */ - public function getCloudInstance(): string { - switch ($this->getCurrentInterface()) { + public function getCloudInstance(int $interface = 0): string { + if ($interface === 0) { + $interface = $this->getCurrentInterface(); + } + + switch ($interface) { case self::IFACE_INTERNAL: return $this->configService->getInternalInstance(); case self::IFACE_FRONTAL: @@ -299,7 +352,7 @@ public function getCloudInstance(): string { case self::IFACE2: case self::IFACE3: case self::IFACE4: - return $this->configService->getIfaceInstance($this->getCurrentInterface()); + return $this->configService->getIfaceInstance($interface); case self::IFACE_TEST: return $this->getTestingInstance(); } @@ -311,9 +364,13 @@ public function getCloudInstance(): string { /** * @throws UnknownInterfaceException */ - public function getCloudPath(string $route = '', array $args = []): string { + public function getCloudPath(string $route = '', array $args = [], int $interface = 0): string { + if ($interface === 0) { + $interface = $this->getCurrentInterface(); + } + $scheme = ''; - switch ($this->getCurrentInterface()) { + switch ($interface) { case self::IFACE_INTERNAL: $scheme = $this->configService->getAppValue(ConfigService::INTERNAL_CLOUD_SCHEME); break; @@ -371,10 +428,83 @@ public function getLocalInstance(): string { } + /** + * @param string $route + * @param array $args + * + * @return string + */ + public function getLocalPath(string $route, array $args): string { + $base = $this->configService->getAppValue(ConfigService::FRONTAL_CLOUD_BASE); + if ($base === '') { + return $this->configService->getLoopbackPath($route, $args); + } + + return rtrim($base, '/') . $this->urlGenerator->linkToRoute($route, $args); + } + + + /** + * should be used when trying to generate an address + * + * @param string $route + * @param array $args + * + * @return string + */ + public function getFrontalPath(string $route, array $args): string { + $frontalBase = $this->configService->getAppValue(ConfigService::FRONTAL_CLOUD_BASE); + if ($frontalBase !== '') { + return $this->getLocalPath($route, $args); + } + + if ($this->isInterfaceConfigured(self::IFACE_FRONTAL)) { + try { + return $this->getCloudPath($route, $args, self::IFACE_FRONTAL); + } catch (UnknownInterfaceException $e) { + } + } + + $ifaces = [self::IFACE0, self::IFACE1, self::IFACE2, self::IFACE3, self::IFACE4]; + foreach ($ifaces as $iface) { + if ($this->isInterfaceConfigured($iface) && !$this->isInterfaceInternal($iface)) { + try { + return $this->getCloudPath($route, $args, $iface); + } catch (UnknownInterfaceException $e) { + } + } + } + + if ($this->isInterfaceConfigured(self::IFACE_INTERNAL)) { + try { + return $this->getCloudPath($route, $args, self::IFACE_INTERNAL); + } catch (UnknownInterfaceException $e) { + } + } + + foreach ($ifaces as $iface) { + if ($this->isInterfaceConfigured($iface) && $this->isInterfaceInternal($iface)) { + try { + return $this->getCloudPath($route, $args, $iface); + } catch (UnknownInterfaceException $e) { + } + } + } + + try { + return $this->getCloudPath($route, $args); + } catch (UnknownInterfaceException $e) { + } + + return $this->getLocalPath($route, $args); + } + + /** * @return string */ private function getTestingInstance(): string { return $this->configService->getAppValue(ConfigService::IFACE_TEST_ID); } + } diff --git a/lib/Service/ShareTokenService.php b/lib/Service/ShareTokenService.php index 399c19b66..2c36f0dc4 100644 --- a/lib/Service/ShareTokenService.php +++ b/lib/Service/ShareTokenService.php @@ -35,6 +35,7 @@ use OCA\Circles\Db\ShareTokenRequest; use OCA\Circles\Exceptions\ShareTokenAlreadyExistException; use OCA\Circles\Exceptions\ShareTokenNotFoundException; +use OCA\Circles\Exceptions\UnknownInterfaceException; use OCA\Circles\Model\Member; use OCA\Circles\Model\ShareToken; use OCA\Circles\Model\ShareWrapper; @@ -56,19 +57,31 @@ class ShareTokenService { /** @var ShareTokenRequest */ private $shareTokenRequest; + /** @var ConfigService */ + private $configService; + + /** @var InterfaceService */ + private $interfaceService; + /** * ShareTokenService constructor. * * @param IURLGenerator $urlGenerator * @param ShareTokenRequest $shareTokenRequest + * @param InterfaceService $interfaceService + * @param ConfigService $configService */ public function __construct( IURLGenerator $urlGenerator, - ShareTokenRequest $shareTokenRequest + ShareTokenRequest $shareTokenRequest, + InterfaceService $interfaceService, + ConfigService $configService ) { $this->urlGenerator = $urlGenerator; $this->shareTokenRequest = $shareTokenRequest; + $this->interfaceService = $interfaceService; + $this->configService = $configService; } @@ -87,7 +100,7 @@ public function generateShareToken( string $password = '' ): ShareToken { if ($member->getUserType() !== Member::TYPE_MAIL - && $member->getUserType() !== Member::TYPE_CONTACT) { + && $member->getUserType() !== Member::TYPE_CONTACT) { throw new ShareTokenNotFoundException(); } @@ -119,7 +132,7 @@ public function generateShareToken( * @param ShareToken $shareToken */ public function setShareTokenLink(ShareToken $shareToken): void { - $link = $this->urlGenerator->linkToRouteAbsolute( + $link = $this->interfaceService->getFrontalPath( 'files_sharing.sharecontroller.showShare', ['token' => $shareToken->getToken()] );