This commit is contained in:
count-null 2025-02-27 16:38:19 -05:00
parent a0cb5fb6b0
commit e435d32588
88 changed files with 1781 additions and 1383 deletions

View file

@ -1,12 +1,12 @@
<?php
//
// It all starts here..
// It all starts here..
//
use app\app;
use app\controllers\account;
use app\controllers\admin;
use app\controllers\category;
use app\controllers\cart;
use app\controllers\category;
use app\controllers\checkout;
use app\controllers\home;
use app\controllers\lnurlp;
@ -26,7 +26,7 @@ session_start();
session_regenerate_id(true); // prevent session fixation attacks
// prevent session hijack
if (!isset($_SESSION['fingerprint'])) {
if (! isset($_SESSION['fingerprint'])) {
$_SESSION['fingerprint'] = hash('sha256', $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
} else {
if ($_SESSION['fingerprint'] !== hash('sha256', $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'])) {
@ -38,21 +38,22 @@ if (!isset($_SESSION['fingerprint'])) {
// these will be available to use in all twig templates
$defaults = [
'copyright_year' => date('Y'),
'session' => $_SESSION,
'http_host' => $_SERVER['HTTP_HOST'],
'env' => $_ENV,
'is_admin' => isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1,
'session' => $_SESSION,
'http_host' => $_SERVER['HTTP_HOST'],
'env' => $_ENV,
'is_user' => isset($_SESSION['user_id']),
'is_admin' => isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1,
// uses cookie-js to get the client's preferred theme
// used to conditionally deliver image assets
// used to conditionally deliver image assets
// or styles based on theme
'theme' => isset($_COOKIE["theme"]) ? $_COOKIE["theme"] : 'light',
'theme' => isset($_COOKIE["theme"]) ? $_COOKIE["theme"] : 'light',
// set your tailwind colors here for app themeing
// the idea is to avoid using colors in your templates
'colors' => require dirname(__DIR__) . '/src/colors.php',
'colors' => require dirname(__DIR__) . '/src/colors.php',
];
// Setup a twig
$loader = new \Twig\Loader\FilesystemLoader(paths: dirname(__DIR__) . '/src/views');
$loader = new \Twig\Loader\FilesystemLoader(paths: dirname(__DIR__) . '/src/views');
$GLOBALS['twig'] = new \Twig\Environment($loader, [
//'cache' => dirname(__DIR__) . '/cache',
'cache' => false,
@ -66,33 +67,35 @@ if (str_starts_with(haystack: $route, needle: '/.well-known/lnurlp/')) {
// Combined regex to match multiple dynamic routes in one go
if (preg_match('/^\/(transaction|user|order|product)\/([\w-]+)$/', $route, $matches)) {
[$full, $type, $id] = $matches;
$controllers = [
'transaction' => fn($id) => transaction::view($defaults, $id),
'user' => fn($id) => users::view($id),
'order' => fn($id) => orders::view($id),
'quote' => fn($id) => quotes::view($id),
'product' => fn($id) => products::view($id),
$controllers = [
'transaction' => fn($id) => transaction::view($defaults, $id),
'user' => fn($id) => users::view($id),
'order' => fn($id) => orders::view($id),
'quote' => fn($id) => quotes::view($id),
'product' => fn($id) => products::view($id),
'subscription' => fn($id) => subscriptions::view($id),
'cart' => fn($id) => cart::index($id),
'cart' => fn($id) => cart::index($id),
];
if (isset($controllers[$type])) {
$controller = $controllers[$type]($id);
}
} else {
$controller = match ($route) {
'/' => home::index($defaults),
'/account' => account::index($defaults),
'/account/profile' => account::profile(),
'/account/login' => account::login($defaults),
'/account/email' => account::email(),
'/account/logout' => account::logout(),
'/account/returns' => account::returns($defaults),
'/account/signup' => account::signup($defaults),
'/account/billing' => account::billing($defaults),
'/account/orders' => account::orders($defaults),
'/account/shipping' => account::shipping($defaults),
'/account/verify' => account::verify($defaults),
'/account' => $defaults['is_user'] ? account::index($defaults) : header('Location: /account/login'),
'/account/profile' => $defaults['is_user'] ? account::profile() : header('Location: /account/login'),
'/account/email' => $defaults['is_user'] ? account::email() : header('Location: /account/login'),
'/account/logout' => $defaults['is_user'] ? account::logout() : header('Location: /account/login'),
'/account/returns' => $defaults['is_user'] ? account::returns($defaults) : header('Location: /account/login'),
'/account/billing' => $defaults['is_user'] ? account::billing($defaults) : header('Location: /account/login'),
'/account/orders' => $defaults['is_user'] ? account::orders($defaults) : header('Location: /account/login'),
'/account/shipping' => $defaults['is_user'] ? account::shipping($defaults) : header('Location: /account/login'),
'/account/address/edit' => $defaults['is_user'] ? account::address_edit($defaults) : header('Location: /account/login'),
'/account/address/confirm' => $defaults['is_user'] ? account::address_confirm($defaults) : header('Location: /account/login'),
'/admin' => $defaults['is_admin'] ? admin::index($defaults) : lost::index($defaults),
'/admin/users' => $defaults['is_admin'] ? admin::users($defaults) : lost::index($defaults),
'/admin/orders' => $defaults['is_admin'] ? admin::orders($defaults) : lost::index($defaults),
@ -113,7 +116,8 @@ if (preg_match('/^\/(transaction|user|order|product)\/([\w-]+)$/', $route, $matc
'/power-meters' => category::power_meters($defaults),
default => lost::index($defaults)
};
};
}
;
// Clear alerts after rendering
foreach (['error', 'warning', 'info', 'success'] as $alert) {