Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 => '',
Expand Down
140 changes: 135 additions & 5 deletions lib/Service/InterfaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,7 @@ class InterfaceService {

use TStringTools;
use TArrayTools;
use TNC22Logger;


/** @var IURLGenerator */
Expand Down Expand Up @@ -101,6 +104,8 @@ public function __construct(
$this->urlGenerator = $urlGenerator;
$this->remoteRequest = $remoteRequest;
$this->configService = $configService;

$this->setup('app', Application::APP_ID);
}


Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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:
Expand All @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

}
19 changes: 16 additions & 3 deletions lib/Service/ShareTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}


Expand All @@ -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();
}

Expand Down Expand Up @@ -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()]
);
Expand Down