Merge remote-tracking branch 'origin/main'

This commit is contained in:
Bruno Rybársky 2024-02-05 21:42:23 +01:00
commit c8e2f75b7e
11 changed files with 223 additions and 26 deletions

@ -20,6 +20,39 @@ function doAccountAction(requestData, successMessage, failureMessage, silent=fal
});
}
function handlePageResponse(data){
if(data.Navigation){
document.getElementById("navbar").innerHTML = data.Navigation;
}
if(data.Page){
document.getElementById("pagearea").innerHTML = data.Page;
if(data.PageLocation){
history.pushState({}, "", data.PageLocation);
}
}
}
function doPageAction(requestData){
return fetch('/page', {
method: 'POST',
body: requestData,
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
handlePageResponse(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
function displayList(data, element_id, delete_function=null) {
const tableContainer = document.getElementById(element_id);
tableContainer.innerHTML = ""; // Clear previous content
@ -86,13 +119,49 @@ function handleResponse(data, SuccessMessage, failureMessage) {
}, 3000);
}
function navigateTo(site, page){
const data = new URLSearchParams();
data.append("action", "getPage");
data.append("site", site);
data.append("page", page);
doPageAction(data);
}
function refreshNavbar(){
const data = new URLSearchParams();
data.append("action", "getNavigation");
doPageAction(data);
}
function logout() {
const data = new URLSearchParams();
data.append("action", "logout");
doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => {
location.reload();
refreshNavbar();
navigateTo("", pageData.defaultPage);
// Expected output: "Success!"
});
}
function initAjax() {
let links = document.querySelectorAll('.navsite_item, .navpage_item');
// Add click event listener to each link
links.forEach(function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
// Get page and site information
let site = this.dataset.site;
let page = this.dataset.page;
if (site && page) {
navigateTo(site, page);
}
// You can use this information to update the URL or perform other actions
});
});
}
document.addEventListener('DOMContentLoaded', initAjax);

@ -1,4 +1,5 @@
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap');
:root {
--primary-bg: rgb(27, 21, 41);
@ -240,3 +241,17 @@ table.list-table > tbody, table.list-table > tbody > th, table.list-table > tbod
}
/* </DASHBOARD STYLING> */
#loginForm input {
border-radius: 50px;
background: none;
border: 2px solid var(--primary);
width: 175px;
}
#register_Form input {
border-radius: 50px;
background: none;
border: 2px solid var(--primary);
width: 175px;
}

13
endpoints/global/page.php Normal file

@ -0,0 +1,13 @@
<?php
require_once "lib/page.php";
require_once "lib/navigation.php";
function endpoint($endpoint_data): array
{
return match ($endpoint_data["action"]) {
"getNavigation" => getNavigationEndpoint(),
"getPage" => getPageEndpoint($endpoint_data["page"], $endpoint_data["site"]),
default => ["Status" => "Fail", "message" => "Invalid action"],
};
}

@ -16,7 +16,7 @@ $canRender = initRouter();
if ($canRender) {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true);
; session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true);
session_start();
if (!isLoggedIn()) {

@ -174,7 +174,7 @@ function doRegister($firstname, $lastname, $email, $password, $activation_token)
$privilege_level = $routerConfig["logged_in_default_permission_level"];
/** @noinspection SpellCheckingInspection */
$stmt->bind_param("ssssiss", $firstname, $lastname, $email, $passwordHash, $privilege_level, $activation_token, $activation_token);
$stmt->bind_param("ssssis", $firstname, $lastname, $email, $passwordHash, $privilege_level, $activation_token);
$stmt->execute();

14
lib/dynamic_style.php Normal file

@ -0,0 +1,14 @@
<?php
function doDynamicStyling() :string
{
$dynamic_style = "";
if(isLoggedIn() && !empty($_SESSION["favorite_color"]) && is_int($_SESSION["favorite_color"]) && $_SESSION["favorite_color"] <= 4294967295){
$dynamic_style = "<style>";
$color = dechex($_SESSION["favorite_color"]);
$dynamic_style .= "--root{ --favorite-color: #$color;";
$dynamic_style .= "</style>";
}
return $dynamic_style;
}

@ -83,15 +83,21 @@ function generateNavigation(): string
}
if($page_required_permission <= $_SESSION["privilege_level"]) {
$navpages .= "<li class='navpage_item' data-page='$page_basename'><a href='$page_location' $page_class>$page_name</a></li>";
$navpages .= "<li class='navpage_item' data-site='$site_dir' data-page='$page_basename'><a href='$page_location' $page_class>$page_name</a></li>";
}
}
if(!empty($navpages)){
$nav_out .= "<li class='navsite_item' data-site='$site_dir'><a href='$site_location' $site_class>$site_name</a><ul class='navpage_list'>$navpages</ul></li>";
$default_page = $routerConfig["default_page"];
$nav_out .= "<li class='navsite_item' data-page='$default_page' data-site='$site_dir'><a href='$site_location' $site_class>$site_name</a><ul class='navpage_list'>$navpages</ul></li>";
}
}
return $nav_out;
}
function getNavigationEndpoint() :array{
return [
"Status" => "Success",
"Navigation" => generateNavigation(),
];
}

@ -1,5 +1,7 @@
<?php
require_once "lib/inliner.php";
require_once "lib/dynamic_style.php";
require_once "lib/script_data.php";
function renderDynamicPage($page_file): array
{
return require $page_file;
@ -30,20 +32,21 @@ function parsePageTag($input): array
return ['parameters' => [], 'output' => $input];
}
function getPage($page_name = null): array|false|string
function renderPage($page_name = null, $site_name = null): array
{
global $routerConfig;
global $routerRequest;
if(!$site_name) {
$site_name = $routerRequest["subdomain"];
}
if(!$page_name){
$page_name = $routerRequest["page_name"];
}
$dynamic_page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".php";
$page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".html";
$skeleton = file_get_contents($routerConfig["template_dir"] . "skeleton.html");
$nav = file_get_contents($routerConfig["template_dir"] . "nav.html");
$dynamic_page_file = $routerConfig["page_dir"] . $site_name . "/" . $page_name . ".php";
$page_file = $routerConfig["page_dir"] . $site_name . "/" . $page_name . ".html";
if (file_exists($dynamic_page_file)){
$pageMetadata = renderDynamicPage($dynamic_page_file);
@ -106,20 +109,38 @@ function getPage($page_name = null): array|false|string
$page = "";
}
return [
"PageContent" => $page,
"PageName" => $page_name,
"SiteName" => $site_name,
];
}
function getPage($page_name_in = null, $site_name_in = null): string
{
$page_tmp = renderPage($page_name_in, $site_name_in);
$page = $page_tmp["PageContent"];
$page_name = $page_tmp["PageName"];
$site_name = $page_tmp["SiteName"];
global $routerConfig;
$skeleton = file_get_contents($routerConfig["template_dir"] . "skeleton.html");
$nav = file_get_contents($routerConfig["template_dir"] . "nav.html");
if(!empty($pageMetadata["parameters"]["page_title"])){
$page_title = $pageMetadata["parameters"]["page_title"];
}
else{
$page_title = $page_name;
}
$dynamic_style = "<style>";
if(isLoggedIn() && !empty($_SESSION["favorite_color"]) && is_int($_SESSION["favorite_color"]) && $_SESSION["favorite_color"] <= 4294967295){
$color = dechex($_SESSION["favorite_color"]);
$dynamic_style .= "--root{ --favorite-color: #$color;";
}
$dynamic_style .= "</style>";
$dynamic_style = doDynamicStyling();
$dynamic_script = generateScriptData([
"currentPage" => $page_name,
"currentSite" => $site_name,
"currentTitle" => $page_title,
"defaultPage" => $routerConfig["default_page"],
]);
$navpages = generateNavigation();
$nav = str_replace("__NAV_PAGES__", $navpages, $nav);
@ -127,8 +148,21 @@ function getPage($page_name = null): array|false|string
$out = $skeleton;
$out = str_replace("__TEMPLATE__NAV__", $nav, $out);
$out = str_replace("__TEMPLATE__PAGE__", $page, $out);
$out = str_replace("__TEMPLATE__DYNASCRIPT__", $dynamic_script, $out);
$out = str_replace("__TEMPLATE__DYNASTYLE__", $dynamic_style, $out);
$out = inlineLocalStylesFromHref($out);
$out = inlineScriptFromSrc($out);
return str_replace("__TEMPLATE_PAGE_TITLE__", $page_title, $out);
}
function getPageEndpoint($page_name, $site_name) :array
{
global $routerRequest, $routerConfig;
$page_location = $routerConfig["protocol"] . $site_name . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $page_name;
$page_tmp = renderPage($page_name, $site_name);
return [
"Status" => "Success",
"Page" => $page_tmp["PageContent"],
"PageLocation" => $page_location,
];
}

9
lib/script_data.php Normal file

@ -0,0 +1,9 @@
<?php
function generateScriptData($input) :string
{
// Convert PHP array to JSON string
$jsonString = json_encode($input);
// Output JavaScript code with the JSON string
return "<script>let pageData = JSON.parse('$jsonString');</script>";
}

@ -41,7 +41,7 @@
<!-- Centralized Status Message -->
<p id="StatusMessage"></p>
<div class="form-container" id="loginForm">
<!-- <div class="form-container" id="loginForm">
<h1>Login</h1>
<form>
<label for="login_email">Email:</label>
@ -51,7 +51,7 @@
<input type="password" id="login_password" name="password" required><br>
<button type="button" onclick="login()">Login</button>
</form><hr>
</form>
</div>
<div class="form-container" id="register_Form">
@ -74,4 +74,36 @@
<button type="button" onclick="register()">Register</button>
</form>
</div>
</div> -->
<main class="login-file">
<div class="container" id="container">
<div class="form-container sign-up">
<form>
<h1>Create Account</h1>
<input type="text" name="firstName" id="register_firstName" required placeholder="First name">
<input type="text" name="lastName" id="register_lastName" required placeholder="Last name">
<input type="text" name="email" id="register_email" required placeholder="Email">
<input type="text" name="password" id="register_password" required placeholder="Password">
<input type="text" name="activationToken" id="register_activationToken" required
placeholder="Activation Token">
<button type="button" onclick="register()">Register</button>
</form>
</div>
<div class="form-container sign-in">
<h1>Login</h1>
<form>
<input type="text" name="email" id="login_email" required placeholder="Email">
<input type="text" name="password" id="login_password" required placeholder="Password">
<button type="button" onclick="login()">Login</button>
</form>
</div>
<!-- <div class="toggle-container">
<div class="toggle">
<div class="toggle-panel toggle-left">
<h1>Glad to have you back!</h1>
<p>Enter your login information to continue to adlerka.top</p>
</div>
</div>
</div> -->
</div>
</main>

@ -9,9 +9,14 @@
<script async src="https://umami.brn.systems/script.js" data-website-id="95e93885-5c19-4cab-ba9b-2f746a316a2a"></script>
<title>Adlerka __TEMPLATE_PAGE_TITLE__</title>
__TEMPLATE__DYNASTYLE__
__TEMPLATE__DYNASCRIPT__
</head>
<body>
<div id="navbar">
__TEMPLATE__NAV__
</div>
<div id="pagearea">
__TEMPLATE__PAGE__
</div>
</body>
</html>