|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * The abstract Front Controller |
| 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 <[email protected]> |
| 12 | + * @copyright 2024-2025 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 2024-06-16 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Controller; |
| 21 | + |
| 22 | +use phpMyFAQ\Core\Exception; |
| 23 | +use phpMyFAQ\Enums\PermissionType; |
| 24 | +use phpMyFAQ\Environment; |
| 25 | +use phpMyFAQ\Helper\LanguageHelper; |
| 26 | +use phpMyFAQ\System; |
| 27 | +use phpMyFAQ\Translation; |
| 28 | +use phpMyFAQ\Twig\TwigWrapper; |
| 29 | +use Symfony\Component\HttpFoundation\Request; |
| 30 | + |
| 31 | +abstract class AbstractFrontController extends AbstractController |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @return string[] |
| 35 | + * @throws Exception |
| 36 | + * @throws \Exception |
| 37 | + */ |
| 38 | + protected function getHeader(Request $request): array |
| 39 | + { |
| 40 | + $faqSystem = $this->container->get(id: 'phpmyfaq.system'); |
| 41 | + $seo = $this->container->get(id: 'phpmyfaq.seo'); |
| 42 | + $action = $request->query->get(key: 'action', default: 'index'); |
| 43 | + |
| 44 | + $isUserHasAdminRights = $this->currentUser->perm->hasPermission( |
| 45 | + $this->currentUser->getUserId(), |
| 46 | + PermissionType::VIEW_ADMIN_LINK->value, |
| 47 | + ); |
| 48 | + |
| 49 | + return [ |
| 50 | + 'isMaintenanceMode' => $this->configuration->get('main.maintenanceMode'), |
| 51 | + 'isCompletelySecured' => $this->configuration->get('security.enableLoginOnly'), |
| 52 | + 'isDebugEnabled' => Environment::isDebugMode(), |
| 53 | + 'richSnippetsEnabled' => $this->configuration->get('seo.enableRichSnippets'), |
| 54 | + 'tplSetName' => TwigWrapper::getTemplateSetName(), |
| 55 | + 'msgLoginUser' => $this->currentUser->isLoggedIn() |
| 56 | + ? $this->currentUser->getUserData('display_name') |
| 57 | + : Translation::get(key: 'msgLoginUser'), |
| 58 | + 'isUserLoggedIn' => $this->currentUser->isLoggedIn(), |
| 59 | + 'isUserHasAdminRights' => $isUserHasAdminRights || $this->currentUser->isSuperAdmin(), |
| 60 | + 'baseHref' => $faqSystem->getSystemUri($this->configuration), |
| 61 | + 'customCss' => $this->configuration->getCustomCss(), |
| 62 | + 'version' => $this->configuration->getVersion(), |
| 63 | + 'header' => str_replace('"', '', $this->configuration->getTitle()), |
| 64 | + 'metaDescription' => $metaDescription ?? $this->configuration->get('seo.description'), |
| 65 | + 'metaPublisher' => $this->configuration->get('main.metaPublisher'), |
| 66 | + 'metaLanguage' => Translation::get(key: 'metaLanguage'), |
| 67 | + 'metaRobots' => $seo->getMetaRobots($action), |
| 68 | + 'phpmyfaqVersion' => $this->configuration->getVersion(), |
| 69 | + 'stylesheet' => Translation::get(key: 'direction') == 'rtl' ? 'style.rtl' : 'style', |
| 70 | + 'currentPageUrl' => $request->getSchemeAndHttpHost() . $request->getRequestUri(), |
| 71 | + 'action' => $action, |
| 72 | + 'dir' => Translation::get(key: 'direction'), |
| 73 | + 'formActionUrl' => './search', |
| 74 | + 'searchBox' => Translation::get(key: 'msgSearch'), |
| 75 | + 'languageBox' => Translation::get(key: 'msgLanguageSubmit'), |
| 76 | + 'switchLanguages' => LanguageHelper::renderSelectLanguage( |
| 77 | + $this->configuration->getLanguage()->getLanguage(), |
| 78 | + true, |
| 79 | + ), |
| 80 | + 'copyright' => System::getPoweredByString(), |
| 81 | + 'isUserRegistrationEnabled' => $this->configuration->get('security.enableRegistration'), |
| 82 | + 'pluginStylesheets' => $this->configuration->getPluginManager()->getAllPluginStylesheets(), |
| 83 | + 'pluginScripts' => $this->configuration->getPluginManager()->getAllPluginScripts(), |
| 84 | + 'msgRegisterUser' => Translation::get(key: 'msgRegisterUser'), |
| 85 | + 'sendPassword' => |
| 86 | + '<a href="' |
| 87 | + . $faqSystem->getSystemUri($this->configuration) |
| 88 | + . 'forgot-password">' |
| 89 | + . Translation::get(key: 'lostPassword') |
| 90 | + . '</a>', |
| 91 | + 'msgFullName' => Translation::get(key: 'ad_user_loggedin') . $this->currentUser->getLogin(), |
| 92 | + 'msgLoginName' => $this->currentUser->getUserData('display_name'), |
| 93 | + 'loginHeader' => Translation::get(key: 'msgLoginUser'), |
| 94 | + 'msgAdvancedSearch' => Translation::get(key: 'msgAdvancedSearch'), |
| 95 | + 'currentYear' => date(format: 'Y', timestamp: time()), |
| 96 | + 'cookieConsentEnabled' => $this->configuration->get('layout.enableCookieConsent'), |
| 97 | + 'faqHome' => $this->configuration->getDefaultUrl(), |
| 98 | + 'topNavigation' => $this->getTopNavigation($request), |
| 99 | + 'isAskQuestionsEnabled' => $this->configuration->get('main.enableAskQuestions'), |
| 100 | + 'isOpenQuestionsEnabled' => $this->configuration->get('main.enableAskQuestions'), |
| 101 | + 'footerNavigation' => $this->getFooterNavigation($request), |
| 102 | + 'isPrivacyLinkEnabled' => $this->configuration->get('layout.enablePrivacyLink'), |
| 103 | + 'urlPrivacyLink' => $this->configuration->get('main.privacyURL'), |
| 104 | + 'msgPrivacyNote' => Translation::get(key: 'msgPrivacyNote'), |
| 105 | + 'isCookieConsentEnabled' => $this->configuration->get('layout.enableCookieConsent'), |
| 106 | + 'cookiePreferences' => Translation::get(key: 'cookiePreferences'), |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + private function getTopNavigation(Request $request): array |
| 111 | + { |
| 112 | + $action = $request->query->get(key: 'action', default: 'index'); |
| 113 | + |
| 114 | + return [ |
| 115 | + [ |
| 116 | + 'name' => Translation::get(key: 'msgShowAllCategories'), |
| 117 | + 'link' => './show-categories.html', |
| 118 | + 'active' => 'show' === $action ? 'active' : '', |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'name' => Translation::get(key: 'msgAddContent'), |
| 122 | + 'link' => './add-faq.html', |
| 123 | + 'active' => 'add' === $action ? 'active' : '', |
| 124 | + ], |
| 125 | + [ |
| 126 | + 'name' => Translation::get(key: 'msgQuestion'), |
| 127 | + 'link' => './add-question.html', |
| 128 | + 'active' => 'ask' == $action ? 'active' : '', |
| 129 | + ], |
| 130 | + [ |
| 131 | + 'name' => Translation::get(key: 'msgOpenQuestions'), |
| 132 | + 'link' => './open-questions.html', |
| 133 | + 'active' => 'open-questions' == $action ? 'active' : '', |
| 134 | + ], |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + private function getFooterNavigation(Request $request): array |
| 139 | + { |
| 140 | + $action = $request->query->get(key: 'action', default: 'index'); |
| 141 | + |
| 142 | + return [ |
| 143 | + [ |
| 144 | + 'name' => Translation::get(key: 'faqOverview'), |
| 145 | + 'link' => './overview.html', |
| 146 | + 'active' => 'faq-overview' == $action ? 'active' : '', |
| 147 | + ], |
| 148 | + [ |
| 149 | + 'name' => Translation::get(key: 'msgSitemap'), |
| 150 | + 'link' => './sitemap/A/' . $this->configuration->getLanguage()->getLanguage() . '.html', |
| 151 | + 'active' => 'sitemap' == $action ? 'active' : '', |
| 152 | + ], |
| 153 | + [ |
| 154 | + 'name' => Translation::get(key: 'ad_menu_glossary'), |
| 155 | + 'link' => './glossary.html', |
| 156 | + 'active' => 'glossary' == $action ? 'active' : '', |
| 157 | + ], |
| 158 | + [ |
| 159 | + 'name' => Translation::get(key: 'msgContact'), |
| 160 | + 'link' => './contact.html', |
| 161 | + 'active' => 'contact' == $action ? 'active' : '', |
| 162 | + ], |
| 163 | + ]; |
| 164 | + } |
| 165 | +} |
0 commit comments