From f6dd78734f86f8daed7c5d472e7a199301095ff8 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Tue, 17 Sep 2024 13:39:12 +0200 Subject: More theme and i18n stuff --- src/index.php | 82 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 17 deletions(-) (limited to 'src/index.php') diff --git a/src/index.php b/src/index.php index 1807608..1b13e97 100644 --- a/src/index.php +++ b/src/index.php @@ -49,7 +49,7 @@ function msg_error(string $err, bool $skipLoginCheck = false): void { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($GLOBALS["db"])]); _view("template_navigation_end"); _view("alert_error", ["message" => $err]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } function msg_info(string $msg, bool $skipLoginCheck = false): void { @@ -59,7 +59,7 @@ function msg_info(string $msg, bool $skipLoginCheck = false): void { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($GLOBALS["db"])]); _view("template_navigation_end"); _view("alert_info", ["message" => $msg]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } function generateCaptchaText(): string { @@ -69,13 +69,47 @@ function generateCaptchaText(): string { return $phrase; } -function _view(string $name, array $params = []): void { - $___NAME = $name; - $___PARAMS = &$params; - extract($params); - echo "\n"; - include __DIR__ . "/application/views/" . $___NAME . ".php"; - echo "\n"; +function getThemeAndLangInfo(): array { + $availableThemes = []; + $currentTheme = $_GET["theme"] ?? $_COOKIE["theme"] ?? env("MYSTIC_FORUM_THEME") ?? "default"; + foreach (scandir($dir = __DIR__ . '/themes/') as $ent) { + if ($ent[0] === "." || !is_dir($dir . "/" . $ent) || !is_file($theme_file = $dir . "/" . $ent . "/theme.json")) + continue; + $theme_info = json_decode(file_get_contents($theme_file)); + $availableThemes[$ent] = $theme_info; + } + + $availableLangs = [ + "en" => "English", + ]; + foreach (i18n_get_available_locales() as $loc) { + if (isset($availableLangs[$loc])) + continue; + $metadata = i18n_metadata($loc); + $availableLangs[$loc] = $metadata["langName"] ?? $loc; + } + + return [ + "availableThemes" => $availableThemes, + "currentTheme" => $currentTheme, + + "availableLangs" => $availableLangs, + "currentLang" => i18n_get_current_locale(), + ]; +} + +function _view(string $___NAME, array $___PARAMS = []): void { + $___PATH = __DIR__ . "/application/views/" . $___NAME . ".php"; + if (!is_file($___PATH)) { + echo "\n"; + echo "

Failed to include " . htmlentities($___NAME) . "


\n"; + echo "\n"; + } else { + extract($___PARAMS); + echo "\n"; + include $___PATH; + echo "\n"; + } } function isTrue(string $str): bool { @@ -242,7 +276,7 @@ if ($_action === "auth") { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]); _view("template_navigation_end"); _view("form_login"); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "register") { if ($currentUser) { @@ -350,7 +384,7 @@ if ($_action === "auth") { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]); _view("template_navigation_end"); _view("form_register"); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "verifyemail") { RequestUtils::ensureRequestMethod("GET"); @@ -503,9 +537,11 @@ if ($_action === "auth") { if ($currentUser) { _view("form_addpost"); + } else { + _view("view_logintoreply"); } - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "newtopic") { @@ -581,7 +617,7 @@ if ($_action === "auth") { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]); _view("template_navigation_end"); _view("form_newtopic"); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "lookupuser") { RequestUtils::ensureRequestMethod("GET"); @@ -700,7 +736,7 @@ if ($_action === "auth") { "attachments" => $attachments, "lastNameChangeTooRecent" => $lastNameChangeTooRecent, ]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "attachment") { if (!$currentUser) { @@ -946,7 +982,7 @@ if ($_action === "auth") { "topicAuthor" => null, "attachments" => $attachments, ]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "updatepost") { RequestUtils::ensureRequestMethod("POST"); @@ -1056,7 +1092,7 @@ if ($_action === "auth") { "topic" => $topic, "topicAuthor" => $topicAuthor, ]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } } elseif ($_action === "updatetopic") { RequestUtils::ensureRequestMethod("POST"); @@ -1123,6 +1159,18 @@ if ($_action === "auth") { $theme_info = json_decode(file_get_contents($theme_file)); echo $ent . "\t" . $theme_info->name . "\n"; } +} elseif ($_action === "settheme") { + RequestUtils::ensureRequestMethod("POST"); + $theme = $_POST["theme"] ?? exit(msg_error("Missing required field 'theme'") ?? 1); + $next = $_POST["next"] ?? "."; + setcookie("theme", $theme, time()+60*60*24*30); + header("Location: $next"); +} elseif ($_action === "setlang") { + RequestUtils::ensureRequestMethod("POST"); + $lang = $_POST["lang"] ?? exit(msg_error("Missing required field 'lang'") ?? 1); + $next = $_POST["next"] ?? "."; + setcookie("lang", $lang, time()+60*60*24*30); + header("Location: $next"); } elseif ($_action === "ctheme") { // options $enableLogging = true; @@ -1204,7 +1252,7 @@ if ($_action === "auth") { _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]); _view("template_navigation_end"); _view("view_topics", ["topics" => $db->fetchCustom(Topic::class, "ORDER BY creation_date DESC")]); - _view("template_end"); + _view("template_end", [...getThemeAndLangInfo()]); } else { http_response_code(404); msg_error(__("Invalid or unknown action $_action")); -- cgit v1.2.3