Initial config support + force HTTPs

This commit is contained in:
Belle Aerni 2023-01-06 14:17:33 -08:00
parent d6e4338689
commit ba585f7616
6 changed files with 108 additions and 26 deletions

View file

@ -7,11 +7,6 @@
"michelf/php-markdown": "^2.0",
"symfony/yaml": "^6.0"
},
"autoload": {
"psr-4": {
"Belle\\AntCms\\": "src/"
}
},
"authors": [
{
"name": "Belle Aerni",

View file

@ -2,36 +2,50 @@
namespace AntCMS;
use AntCMS\AntConfig;
class AntCache
{
public function setCache($key, $content)
{
$cachePath = AntCache . "/$key.cache";
try {
$cache = fopen($cachePath, "w");
fwrite($cache, (string)$content);
fclose($cache);
return true;
} catch (\Exception $e) {
return false;
$cachePath = AntCachePath . "/$key.cache";
$config = AntConfig::currentConfig();
if ($config['enableCache']) {
try {
$cache = fopen($cachePath, "w");
fwrite($cache, (string)$content);
fclose($cache);
return true;
} catch (\Exception $e) {
return false;
}
}
}
public function getCache($key)
{
$cachePath = AntCache . "/$key.cache";
try {
$contents = file_get_contents($cachePath);
return $contents;
} catch (\Exception $e) {
$cachePath = AntCachePath . "/$key.cache";
$config = AntConfig::currentConfig();
if ($config['enableCache']) {
try {
$contents = file_get_contents($cachePath);
return $contents;
} catch (\Exception $e) {
return false;
}
} else {
return false;
}
}
public function isCached($key)
{
$cachePath = AntCache . "/$key.cache";
return file_exists($cachePath);
$config = AntConfig::currentConfig();
if ($config['enableCache']) {
$cachePath = AntCachePath . "/$key.cache";
return file_exists($cachePath);
} else {
return false;
}
}
}

35
src/AntCMS/AntConfig.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace AntCMS;
use AntCMS\AntYaml;
class AntConfig
{
public static function generateConfig()
{
$defaultOptions = array(
'forceHTTPS' => true,
'activeTheme' => 'default',
'generateKeywords' => true,
'enableCache' => true,
'admin' => array(
'password' => '',
'username' => '',
),
'debug' => true,
);
AntYaml::saveFile(antConfigFile, $defaultOptions);
}
public static function currentConfig()
{
return AntYaml::parseFile(antConfigFile);
}
public static function saveConfig($config)
{
AntYaml::saveFile(antConfigFile, $config);
}
}

View file

@ -1,9 +1,19 @@
<?php
namespace AntCMS;
use Symfony\Component\Yaml\Yaml;
class AntYaml{
class AntYaml
{
public static function parseFile($file)
{
return Yaml::parseFile($file);
}
public static function saveFile($file, $data)
{
$yaml = Yaml::dump($data);
file_put_contents($file, $yaml);
}
}
?>

View file

@ -1,6 +1,6 @@
<?php
spl_autoload_register(function($class) {
spl_autoload_register(function ($class) {
$class = str_replace('\\', '/', $class);
$path = __DIR__ . '/' . $class . '.php';
if (is_readable($path)) {

View file

@ -4,14 +4,42 @@ error_reporting(E_ALL);
ini_set('display_errors', 1);
const AntDir = __DIR__;
const AntCache = __DIR__ . '/Cache';
const antConfig = __DIR__ . '/config.ymal';
const AntCachePath = __DIR__ . '/Cache';
const antConfigFile = __DIR__ . '/config.ymal';
require_once __DIR__ . '/Vendor/autoload.php';
require_once __DIR__ . '/Autoload.php';
use AntCMS\AntCMS;
use AntCMS\AntConfig;
$antCms = new AntCMS();
if(!file_exists(antConfigFile)){
AntConfig::generateConfig();
}
$currentConfg = AntConfig::currentConfig();
if ($currentConfg['forceHTTPS'] && 'cli' !== PHP_SAPI){
$isHTTPS = false;
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
$isHTTPS = true;
}
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') {
$isHTTPS = true;
}
if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) !== 'off') {
$isHTTPS = true;
}
if(!$isHTTPS){
$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('Location: ' . $url);
exit;
}
}
$requestedPage = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$indexes = ['/', '/index.php', '/index.html'];
if (in_array($requestedPage, $indexes)) {