diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php index 0911b12f..d6396122 100644 --- a/lib/Controller/NavigationController.php +++ b/lib/Controller/NavigationController.php @@ -40,9 +40,11 @@ use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Service\ActivityPub\DocumentService; use OCA\Social\Service\ActivityPub\PersonService; use OCA\Social\Service\ActorService; +use OCA\Social\Service\CheckService; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\FileDisplayResponse; @@ -52,6 +54,7 @@ use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\Http\Client\IClientService; class NavigationController extends Controller { @@ -86,6 +89,9 @@ class NavigationController extends Controller { /** @var PersonService */ private $personService; + /** @var CheckService */ + private $checkService; + /** * NavigationController constructor. * @@ -97,13 +103,14 @@ class NavigationController extends Controller { * @param DocumentService $documentService * @param ConfigService $configService * @param PersonService $personService + * @param CheckService $checkService * @param MiscService $miscService * @param IL10N $l10n */ public function __construct( IRequest $request, $userId, IConfig $config, IURLGenerator $urlGenerator, ActorService $actorService, DocumentService $documentService, ConfigService $configService, - PersonService $personService, + PersonService $personService, CheckService $checkService, MiscService $miscService, IL10N $l10n ) { parent::__construct(Application::APP_NAME, $request); @@ -111,6 +118,7 @@ class NavigationController extends Controller { $this->userId = $userId; $this->config = $config; $this->urlGenerator = $urlGenerator; + $this->checkService = $checkService; $this->actorService = $actorService; $this->documentService = $documentService; @@ -138,42 +146,37 @@ class NavigationController extends Controller { 'public' => false, 'firstrun' => false, 'setup' => false, + 'isAdmin' => \OC::$server->getGroupManager()->isAdmin($this->userId), + 'cliUrl' => $this->getCliUrl() ] ]; + $checks = $this->checkService->checkDefault(); + $data['serverData']['checks'] = $checks; + try { $data['serverData']['cloudAddress'] = $this->configService->getCloudAddress(); } catch (SocialAppConfigException $e) { - $cloudAddress = rtrim( - $this->config->getSystemValue('overwrite.cli.url', ''), '/' - ); - $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); + $cloudAddress = $this->setupCloudAddress(); if ($cloudAddress !== ''){ - if (!$frontControllerActive){ - $cloudAddress .= '/index.php'; - } - $this->configService->setCloudAddress($cloudAddress); $data['serverData']['cloudAddress'] = $cloudAddress; } else { $data['serverData']['setup'] = true; - $data['serverData']['isAdmin'] = \OC::$server->getGroupManager() - ->isAdmin($this->userId); + if ($data['serverData']['isAdmin']) { $cloudAddress = $this->request->getParam('cloudAddress'); if ($cloudAddress !== null) { $this->configService->setCloudAddress($cloudAddress); } else { - $data['serverData']['cliUrl'] = $this->config->getSystemValue( - 'overwrite.cli.url', \OC::$server->getURLGenerator() - ->getBaseUrl() - ); - return new TemplateResponse(Application::APP_NAME, 'main', $data); } } } } + /* + * Create social user account if it doesn't exist yet + */ try { $this->actorService->createActor($this->userId, $this->userId); $data['serverData']['firstrun'] = true; @@ -185,13 +188,31 @@ class NavigationController extends Controller { // neither. } - $csp = new ContentSecurityPolicy(); - $csp->addAllowedImageDomain('*'); - $response = new TemplateResponse(Application::APP_NAME, 'main', $data); - $response->setContentSecurityPolicy($csp); - return $response; + return new TemplateResponse(Application::APP_NAME, 'main', $data); } + private function setupCloudAddress(): string { + $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); + + $cloudAddress = rtrim($this->config->getSystemValue('overwrite.cli.url', ''), '/'); + if ($cloudAddress !== '') { + if (!$frontControllerActive) { + $cloudAddress .= '/index.php'; + } + $this->configService->setCloudAddress($cloudAddress); + return $cloudAddress; + } + return ''; + } + + private function getCliUrl() { + $url = rtrim($this->urlGenerator->getBaseUrl(), '/'); + $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); + if (!$frontControllerActive) { + $url .= '/index.php'; + } + return $url; + } /** * Display the navigation page of the Social app. @@ -301,4 +322,3 @@ class NavigationController extends Controller { } } - diff --git a/lib/Service/CheckService.php b/lib/Service/CheckService.php new file mode 100644 index 00000000..dea98418 --- /dev/null +++ b/lib/Service/CheckService.php @@ -0,0 +1,105 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +namespace OCA\Social\Service; + + +use OCP\AppFramework\Http; +use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; + +class CheckService { + + private $cache; + private $config; + private $clientService; + private $request; + private $urlGenerator; + + const CACHE_PREFIX = 'social_check_'; + + + public function __construct(ICache $cache, IConfig $config, IClientService $clientService, IRequest $request, IURLGenerator $urlGenerator) { + $this->cache = $cache; + $this->config = $config; + $this->clientService = $clientService; + $this->request = $request; + $this->urlGenerator = $urlGenerator; + } + + public function checkDefault(): array { + $checks = []; + $checks['wellknown'] = $this->checkWellKnown(); + + $success = true; + foreach ($checks as $check) { + if (!$check) { + $success = false; + } + } + return [ + 'success' => $success, + 'checks' => $checks + ]; + } + public function checkWellKnown(): bool { + $state = (bool) ($this->cache->get(self::CACHE_PREFIX . 'wellknown') === 'true'); + if ($state === true) { + return true; + } + + $address = $this->config->getAppValue('social', 'address', ''); + + if ($address !== '' && $this->requestWellKnown($address)) { + return true; + } + + if ($this->requestWellKnown($this->request->getServerProtocol() . '://' . $this->request->getServerHost())) { + return true; + } + + if ($this->requestWellKnown($this->urlGenerator->getBaseUrl())) { + return true; + } + + return false; + } + + private function requestWellKnown($base) { + try { + $url = $base . '/.well-known/webfinger'; + $response = $this->clientService->newClient()->get($url); + if ($response->getStatusCode() === Http::STATUS_OK) { + $this->cache->set(self::CACHE_PREFIX . 'wellknown', 'true', 3600); + return true; + } + } catch (\GuzzleHttp\Exception\ClientException $e) { + } catch (\Exception $e) { + } + return false; + } + +} diff --git a/src/App.vue b/src/App.vue index c7c9355b..5108225b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,16 +4,19 @@
-
@@ -57,6 +65,9 @@ #social-spacer a:focus { border: none !important; } + a.external_link { + text-decoration: underline; + }