|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Circles\Controller; |
| 11 | + |
| 12 | +use OCA\Circles\ConfigLexicon; |
| 13 | +use OCA\Circles\Service\ConfigService; |
| 14 | +use OCP\AppFramework\Http; |
| 15 | +use OCP\AppFramework\Http\DataResponse; |
| 16 | +use OCP\AppFramework\OCSController; |
| 17 | +use OCP\AppFramework\Services\IAppConfig; |
| 18 | +use OCP\IRequest; |
| 19 | + |
| 20 | +class SettingsController extends OCSController { |
| 21 | + public function __construct( |
| 22 | + string $appName, |
| 23 | + IRequest $request, |
| 24 | + private readonly IAppConfig $appConfig, |
| 25 | + ) { |
| 26 | + parent::__construct($appName, $request); |
| 27 | + } |
| 28 | + |
| 29 | + public function setValue(string $key, string $value): DataResponse { |
| 30 | + if ($key === ConfigLexicon::FEDERATED_TEAMS_FRONTAL) { |
| 31 | + if ($this->setFrontalValue($value)) { |
| 32 | + return $this->getValues(); |
| 33 | + } |
| 34 | + |
| 35 | + return new DataResponse(['data' => ['message' => 'wrongly formated value']], Http::STATUS_BAD_REQUEST); |
| 36 | + } |
| 37 | + |
| 38 | + if ($key === ConfigLexicon::FEDERATED_TEAMS_ENABLED) { |
| 39 | + $this->appConfig->setAppValueBool(ConfigLexicon::FEDERATED_TEAMS_ENABLED, $value === 'yes'); |
| 40 | + return $this->getValues(); |
| 41 | + } |
| 42 | + |
| 43 | + return new DataResponse(['data' => ['message' => 'unsupported key']], Http::STATUS_BAD_REQUEST); |
| 44 | + } |
| 45 | + |
| 46 | + public function getValues(): DataResponse { |
| 47 | + return new DataResponse([ |
| 48 | + ConfigLexicon::FEDERATED_TEAMS_FRONTAL => $this->getFrontalValue() ?? '', |
| 49 | + ConfigLexicon::FEDERATED_TEAMS_ENABLED => $this->appConfig->getAppValueBool(ConfigLexicon::FEDERATED_TEAMS_ENABLED), |
| 50 | + ]); |
| 51 | + } |
| 52 | + |
| 53 | + private function setFrontalValue(string $url): bool { |
| 54 | + [$scheme, $cloudId, $path] = $this->parseFrontalAddress($url); |
| 55 | + if (is_null($scheme)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + $this->appConfig->setAppValueString(ConfigLexicon::FEDERATED_TEAMS_FRONTAL, $url); |
| 60 | + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_SCHEME, $scheme); |
| 61 | + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_ID, $cloudId); |
| 62 | + $this->appConfig->setAppValueString(ConfigService::FRONTAL_CLOUD_PATH, $path); |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + private function getFrontalValue(): ?string { |
| 68 | + if ($this->appConfig->hasAppKey(ConfigLexicon::FEDERATED_TEAMS_FRONTAL)) { |
| 69 | + return $this->appConfig->getAppValueString(ConfigLExicon::FEDERATED_TEAMS_FRONTAL); |
| 70 | + } |
| 71 | + |
| 72 | + if (!$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_SCHEME) |
| 73 | + || !$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_ID) |
| 74 | + || !$this->appConfig->hasAppKey(ConfigService::FRONTAL_CLOUD_PATH)) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + return $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_SCHEME) . '://' . |
| 79 | + $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_ID) . |
| 80 | + $this->appConfig->getAppValueString(ConfigService::FRONTAL_CLOUD_PATH) . '/'; |
| 81 | + } |
| 82 | + |
| 83 | + private function parseFrontalAddress(string $url): ?array { |
| 84 | + $scheme = parse_url($url, PHP_URL_SCHEME); |
| 85 | + $cloudId = parse_url($url, PHP_URL_HOST); |
| 86 | + $cloudIdPort = parse_url($url, PHP_URL_PORT); |
| 87 | + $path = parse_url($url, PHP_URL_PATH); |
| 88 | + |
| 89 | + if (is_bool($scheme) || is_bool($cloudId) || is_null($scheme) || is_null($cloudId)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if (is_null($path) || is_bool($path)) { |
| 94 | + $path = ''; |
| 95 | + } |
| 96 | + $path = rtrim($path, '/'); |
| 97 | + if (!is_null($cloudIdPort)) { |
| 98 | + $cloudId .= ':' . ((string)$cloudIdPort); |
| 99 | + } |
| 100 | + |
| 101 | + return [$scheme, $cloudId, $path]; |
| 102 | + } |
| 103 | +} |
0 commit comments