diff options
| author | Jonas Kohl | 2024-09-17 13:39:12 +0200 | 
|---|---|---|
| committer | Jonas Kohl | 2024-09-17 13:39:12 +0200 | 
| commit | f6dd78734f86f8daed7c5d472e7a199301095ff8 (patch) | |
| tree | ef173ed502e189b25c6b5af08db982943adca718 /src/index.php | |
| parent | e8ab5cbddd018f311e2d942c048203b8b3221e4e (diff) | |
More theme and i18n stuff
Diffstat (limited to 'src/index.php')
| -rw-r--r-- | src/index.php | 82 | 
1 files changed, 65 insertions, 17 deletions
| 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 "<!--{" . htmlentities($name) . "}-->\n"; -    include __DIR__ . "/application/views/" . $___NAME . ".php"; -    echo "<!--{/" . htmlentities($name) . "}-->\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 "<!--!ERROR Failed to include {" . htmlentities($___NAME) . "}-->\n"; +        echo "<br><p><strong style=\"color:red !important\">Failed to include <em>" . htmlentities($___NAME) . "</em></strong></p><br>\n"; +        echo "<!--/ERROR-->\n"; +    } else { +        extract($___PARAMS); +        echo "<!--{" . htmlentities($___NAME) . "}-->\n"; +        include $___PATH; +        echo "<!--{/" . htmlentities($___NAME) . "}-->\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")); |