|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Public metadata service for the REST API bootstrap endpoint. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | + * @copyright 2026 phpMyFAQ Team |
| 13 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | + * @link https://www.phpmyfaq.de |
| 15 | + * @since 2026-04-11 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Api; |
| 21 | + |
| 22 | +use phpMyFAQ\Configuration; |
| 23 | +use phpMyFAQ\Helper\LanguageHelper; |
| 24 | + |
| 25 | +final readonly class MetaService |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private Configuration $configuration, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return array{ |
| 34 | + * version: string, |
| 35 | + * title: string, |
| 36 | + * language: string, |
| 37 | + * availableLanguages: array<string, string>, |
| 38 | + * enabledFeatures: array<string, bool>, |
| 39 | + * publicLogoUrl: string, |
| 40 | + * oauthDiscovery: array<string, bool|string|string[]> |
| 41 | + * } |
| 42 | + */ |
| 43 | + public function getPublicMetadata(): array |
| 44 | + { |
| 45 | + return [ |
| 46 | + 'version' => $this->configuration->getVersion(), |
| 47 | + 'title' => $this->configuration->getTitle(), |
| 48 | + 'language' => $this->configuration->getLanguage()->getLanguage(), |
| 49 | + 'availableLanguages' => LanguageHelper::getAvailableLanguages(), |
| 50 | + 'enabledFeatures' => $this->buildEnabledFeatures(), |
| 51 | + 'publicLogoUrl' => $this->buildPublicLogoUrl(), |
| 52 | + 'oauthDiscovery' => $this->buildOAuthDiscovery(), |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return array<string, bool> |
| 58 | + */ |
| 59 | + private function buildEnabledFeatures(): array |
| 60 | + { |
| 61 | + return [ |
| 62 | + 'api' => true, |
| 63 | + 'oauth2' => $this->toBool($this->configuration->get('oauth2.enable')), |
| 64 | + 'captcha' => $this->toBool($this->configuration->get('spam.enableCaptchaCode')), |
| 65 | + 'ldap' => $this->configuration->isLdapActive(), |
| 66 | + 'elasticsearch' => $this->toBool($this->configuration->get('search.enableElasticsearch')), |
| 67 | + 'opensearch' => $this->toBool($this->configuration->get('search.enableOpenSearch')), |
| 68 | + 'sso' => $this->toBool($this->configuration->get('security.ssoSupport')), |
| 69 | + 'signInWithMicrosoft' => $this->configuration->isSignInWithMicrosoftActive(), |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return array<string, bool|string|string[]> |
| 75 | + */ |
| 76 | + private function buildOAuthDiscovery(): array |
| 77 | + { |
| 78 | + $apiBaseUrl = rtrim($this->configuration->getDefaultUrl(), characters: '/') . '/api'; |
| 79 | + |
| 80 | + return [ |
| 81 | + 'enabled' => $this->toBool($this->configuration->get('oauth2.enable')), |
| 82 | + 'issuer' => $apiBaseUrl, |
| 83 | + 'authorizationEndpoint' => $apiBaseUrl . '/oauth/authorize', |
| 84 | + 'tokenEndpoint' => $apiBaseUrl . '/oauth/token', |
| 85 | + 'grantTypesSupported' => ['authorization_code', 'client_credentials', 'refresh_token'], |
| 86 | + 'responseTypesSupported' => ['code'], |
| 87 | + 'tokenEndpointAuthMethodsSupported' => ['client_secret_basic', 'client_secret_post', 'none'], |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + private function buildPublicLogoUrl(): string |
| 92 | + { |
| 93 | + return rtrim($this->configuration->getDefaultUrl(), characters: '/') . '/assets/images/logo-transparent.svg'; |
| 94 | + } |
| 95 | + |
| 96 | + private function toBool(mixed $value): bool |
| 97 | + { |
| 98 | + return $value === true || $value === 1 || $value === '1' || $value === 'true'; |
| 99 | + } |
| 100 | +} |
0 commit comments