refactor config and add allowed_hosts

This commit is contained in:
Freya Murphy 2024-10-20 16:49:11 -04:00
parent b4bb02cee3
commit cec4fb7ede
Signed by: freya
GPG key ID: 744AB800E383AE52
12 changed files with 77 additions and 56 deletions

View file

@ -48,9 +48,11 @@ class _meta_controller extends Controller {
public function manifest(): void public function manifest(): void
{ {
$domain = CONFIG['domain'];
$theme_color = CONFIG['theme_color'];
$json = array( $json = array(
'short_name' => lang('domain'), 'short_name' => $domain,
'name' => lang('domain'), 'name' => $domain,
'icons' => [ 'icons' => [
array( array(
'src' => $this->get_url('public/icons/logo512.png'), 'src' => $this->get_url('public/icons/logo512.png'),
@ -61,10 +63,10 @@ class _meta_controller extends Controller {
], ],
'id' => $this->get_url('home'), 'id' => $this->get_url('home'),
'start_url' => $this->get_url('home'), 'start_url' => $this->get_url('home'),
'background_color' => lang('theme_color'), 'background_color' => $theme_color,
'display' => 'standalone', 'display' => 'standalone',
'scope' => lang('base_path'), 'scope' => CONFIG['base_path'],
'theme_color' => lang('theme_color'), 'theme_color' => $theme_color,
'shortcuts' => [], 'shortcuts' => [],
'description' => lang('default_short_desc'), 'description' => lang('default_short_desc'),
'screenshots' => [] 'screenshots' => []

View file

@ -2,7 +2,7 @@
<rss version="2.0"> <rss version="2.0">
<channel> <channel>
<title><?=lang('title')?></title> <title><?=lang('title')?></title>
<link><?=lang('root_url') . '/blog'?></link> <link><?=$this->get_url('blog')?></link>
<description><?=lang('blog_short_desc')?></description> <description><?=lang('blog_short_desc')?></description>
<language><?=lang('lang_short')?></language> <language><?=lang('lang_short')?></language>
<?php <?php
@ -12,8 +12,8 @@
echo '<title>' . $post['meta']['name'] . '</title>'; echo '<title>' . $post['meta']['name'] . '</title>';
echo '<description>' . $post['meta']['desc'] . '</description>'; echo '<description>' . $post['meta']['desc'] . '</description>';
echo '<pubDate>' . $post['meta']['date'] . '</pubDate>'; echo '<pubDate>' . $post['meta']['date'] . '</pubDate>';
echo '<link>' . lang('root_url') . 'blog/post/' . $name . '</link>'; echo '<link>' . $this->get_url('blog/post/' . $name) . '</link>';
echo '<guid>' . lang('root_url') . 'blog/post/' . $name . '</guid>'; echo '<guid>' . $this->get_url('blog/post/' . $name) . '</guid>';
echo '</item>'; echo '</item>';
} }
?> ?>

View file

@ -17,7 +17,7 @@
?> ?>
<div class="new"> <div class="new">
<h3><?=lang('new_comment_title')?></h3> <h3><?=lang('new_comment_title')?></h3>
<form id="new_comment" method="get" action="<?=lang('base_path') . '_comments/post'?>"> <form id="new_comment" method="get" action="<?=$this->get_url('_comments/post')?>">
<div><input <div><input
type="text" type="text"
name="author" name="author"

View file

@ -6,12 +6,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="freya"> <meta name="author" content="freya">
<meta name="description" content="<?=$desc?>"> <meta name="description" content="<?=$desc?>">
<meta name="theme-color" content="<?=lang('theme_color')?>"> <meta name="theme-color" content="<?=CONFIG['theme_color']?>">
<meta name="referrer" content="origin"> <meta name="referrer" content="origin">
<meta name="color-scheme" content="none"> <meta name="color-scheme" content="none">
<meta property="og:description" content="<?=$desc?>"> <meta property="og:description" content="<?=$desc?>">
<meta property="og:title" content="<?=$title?>"> <meta property="og:title" content="<?=$title?>">
<meta property="og:site_name" content="<?=lang('domain')?>"> <meta property="og:site_name" content="<?=CONFIG['domain']?>">
<meta property="og:image" content="<?=$this->get_url('public/icons/logo640.png', TRUE)?>"> <meta property="og:image" content="<?=$this->get_url('public/icons/logo640.png', TRUE)?>">
<title><?=$title?></title> <title><?=$title?></title>
<link rel="icon" type="image/png" sizes="16x16" href="<?=$this->get_url("public/icons/logo16.png", TRUE)?>"> <link rel="icon" type="image/png" sizes="16x16" href="<?=$this->get_url("public/icons/logo16.png", TRUE)?>">

42
src/web/config.php Normal file
View file

@ -0,0 +1,42 @@
<?php /* Copyright (c) 2024 Freya Murphy */
// ENVIRONMENT
//
// devlopment - do not cache any assets
// - use http host provided by user
//
// production - use generated timestamps for each file
// - hard code http host to 'domain' lang string
//
define('ENVIRONMENT', 'devlopment');
// CONFIG
//
// config values needed across the website
define('CONFIG', array(
/* core config settings */
'domain' => 'freya.cat',
'allowed_hosts' => ['freya.cat', 'www.freya.cat'],
'base_path' => '/',
'theme_color' => '#181818',
'git_url' => 'https://g.freya.cat/freya',
/* route overides */
'routes' => array(
'' => 'home',
'robots.txt' => '_meta/robots',
'sitemap.xml' => '_meta/sitemap',
'manifest.json' => '_meta/manifest',
'rss.xml' => 'blog/rss',
),
/* css to load on each route */
'style' => array(
'home' => 'css/home.css',
'blog' => ['css/blog.css', 'css/prism.css'],
'error' => 'css/error.css',
),
/* js to load on each route */
'js' => array(
'blog' => 'js/prism.js',
),
));

View file

@ -1,7 +0,0 @@
<?php /* Copyright (c) 2024 Freya Murphy */
$routes = array();
$routes[''] = 'home';
$routes['robots.txt'] = '_meta/robots';
$routes['sitemap.xml'] = '_meta/sitemap';
$routes['manifest.json'] = '_meta/manifest';
$routes['rss.xml'] = 'blog/rss';

View file

@ -1,9 +0,0 @@
<?php /* Copyright (c) 2024 Freya Murphy */
$style = array();
$style['home'] = 'css/home.css';
$style['blog'] = ['css/blog.css', 'css/prism.css'];
$style['error'] = 'css/error.css';
$style['prism'] = 'css/prism.css';
$js = array();
$js['blog'] = 'js/prism.js';

View file

@ -43,10 +43,16 @@ abstract class Core {
$host = $_SERVER['HTTP_HOST']; $host = $_SERVER['HTTP_HOST'];
if (ENVIRONMENT == 'production') if (ENVIRONMENT == 'production') {
$host = lang('domain'); $default = CONFIG['domain'];
$allowed = CONFIG['allowed_hosts'];
if (!is_array($allowed))
$allowed = [$allowed];
if (!in_array($host, $allowed))
$host = $default;
}
$base = lang('base_path'); $base = CONFIG['base_path'];
$url = "{$scheme}://{$host}{$base}{$path}"; $url = "{$scheme}://{$host}{$base}{$path}";
if ($timestamp) { if ($timestamp) {
$time = Core::asset_stamp($path); $time = Core::asset_stamp($path);

View file

@ -2,7 +2,10 @@
abstract class Model extends Component { abstract class Model extends Component {
public static function get_base_data(?string $app = NULL): array /**
* @return array<string,mixed>
*/
public static function get_base_data(?string $app = NULL): array
{ {
$data = array(); $data = array();
$data['title'] = lang('first_name'); $data['title'] = lang('first_name');
@ -10,8 +13,8 @@ abstract class Model extends Component {
$data['css'] = array(); $data['css'] = array();
$data['js'] = array(); $data['js'] = array();
$style = $GLOBALS['style']; $style = CONFIG['style'];
$js = $GLOBALS['js']; $js = CONFIG['js'];
if (!$app) if (!$app)
$app = CONTEXT['app']; $app = CONTEXT['app'];

View file

@ -30,7 +30,8 @@ class Router extends Component {
$path = substr($path, 1); $path = substr($path, 1);
// get modified route // get modified route
foreach ($GLOBALS['routes'] as $key => $value) { $routes = CONFIG['routes'];
foreach ($routes as $key => $value) {
$key = "/^{$key}$/"; $key = "/^{$key}$/";
if (!preg_match($key, $path, $matches)) if (!preg_match($key, $path, $matches))
continue; continue;
@ -83,7 +84,6 @@ class Router extends Component {
'args' => array_slice($parts, 2), 'args' => array_slice($parts, 2),
); );
$routes = $GLOBALS['routes'];
if (isset($routes[$path])) { if (isset($routes[$path])) {
$parts = explode('/', $routes[$path]); $parts = explode('/', $routes[$path]);
if (count($parts) == 1) { if (count($parts) == 1) {

View file

@ -6,16 +6,6 @@ ini_set('html_errors', '1');
ini_set('browscap', 'browscap.ini'); ini_set('browscap', 'browscap.ini');
date_default_timezone_set('America/New_York'); date_default_timezone_set('America/New_York');
// ENVIRONMENT
//
// devlopment - do not cache any assets
// - use http host provided by user
//
// production - use generated timestamps for each file
// - hard code http host to 'domain' lang string
//
define('ENVIRONMENT', 'devlopment');
// FOLDER_ROOT // FOLDER_ROOT
// //
// define folder directiroy paths based on this file // define folder directiroy paths based on this file
@ -26,14 +16,13 @@ define('PUBLIC_ROOT', PHP_ROOT . '/public');
// ========================== BOOTSTRAP == // ========================== BOOTSTRAP ==
// load the config
require(WEB_ROOT . '/config.php');
// load all third party // load all third party
require(WEB_ROOT . '/third_party/parsedown.php'); require(WEB_ROOT . '/third_party/parsedown.php');
require(WEB_ROOT . '/third_party/parsedown_extra.php'); require(WEB_ROOT . '/third_party/parsedown_extra.php');
// load all the config files
require(WEB_ROOT . '/config/routes.php');
require(WEB_ROOT . '/config/style.php');
// load all the helpers // load all the helpers
require(WEB_ROOT . '/helpers/ie.php'); require(WEB_ROOT . '/helpers/ie.php');
require(WEB_ROOT . '/helpers/lang.php'); require(WEB_ROOT . '/helpers/lang.php');
@ -50,6 +39,7 @@ require(WEB_ROOT . '/core/controller.php');
require(WEB_ROOT . '/core/model.php'); require(WEB_ROOT . '/core/model.php');
require(WEB_ROOT . '/core/router.php'); require(WEB_ROOT . '/core/router.php');
// load file stamps // load file stamps
require(WEB_ROOT . '/stamp.php'); require(WEB_ROOT . '/stamp.php');

View file

@ -1,15 +1,9 @@
<?php /* Copyright (c) 2024 Freya Murphy */ <?php /* Copyright (c) 2024 Freya Murphy */
# things # lang
$lang['lang_short'] = 'en'; $lang['lang_short'] = 'en';
$lang['lang_full'] = 'en_US'; $lang['lang_full'] = 'en_US';
$lang['domain'] = 'freya.cat';
$lang['base_path'] = '/';
$lang['root_url'] = sprintf("https://%s%s", $lang['domain'], $lang['base_path']);
$lang['git_url'] = 'https://g.freya.cat/freya';
$lang['theme_color'] = '#181818';
# names # names
$lang['first_name'] = 'Freya'; $lang['first_name'] = 'Freya';
$lang['last_name'] = 'Murphy'; $lang['last_name'] = 'Murphy';