1
0

index request works almost

This commit is contained in:
Mike Schwörer 2020-01-15 01:46:31 +01:00
parent 114e93d09e
commit f9a692e635
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
7 changed files with 112 additions and 147 deletions

View File

@ -15,7 +15,15 @@ require_once (__DIR__ . '/../internals/website.php');
<title><?php echo $FRAME_OPTIONS->title; ?></title> <title><?php echo $FRAME_OPTIONS->title; ?></title>
<meta name="google-site-verification" content="pZOhmjeJcQbRMNa8xRLam4dwJ2oYwMwISY1lRKreSSs"/> <meta name="google-site-verification" content="pZOhmjeJcQbRMNa8xRLam4dwJ2oYwMwISY1lRKreSSs"/>
<link rel="icon" type="image/png" href="/data/images/favicon.png"/> <link rel="icon" type="image/png" href="/data/images/favicon.png"/>
<link rel="canonical" href="<?php echo $FRAME_OPTIONS->canonical_url; ?>"/> <?php
if ($FRAME_OPTIONS->canonical_url !== null) echo '<link rel="canonical" href="'.$FRAME_OPTIONS->canonical_url.'"/>';
foreach ($FRAME_OPTIONS->stylesheets as $cssfile) echo '<link rel="stylesheet" href="' . $cssfile . '"/>';
foreach ($FRAME_OPTIONS->scripts as $scriptfile)
{
if ($scriptfile[1]) echo '<script src="' . $scriptfile[0] . '" defer/>';
else echo '<script src="' . $scriptfile[0] . '" type="text/javascript" ></script>';
}
?>
</head> </head>
<body> <body>
<div id="mastercontainer"> <div id="mastercontainer">
@ -40,7 +48,9 @@ require_once (__DIR__ . '/../internals/website.php');
</div> </div>
<?php echo $FRAME_OPTIONS->raw; ?> <div id="content" class="<?php echo join(' ', $FRAME_OPTIONS->contentCSSClasses); ?>">
<?php echo $FRAME_OPTIONS->raw; ?>
</div>
<div id="footerdiv" class="content-responsive"> <div id="footerdiv" class="content-responsive">
<hr /> <hr />

View File

@ -7,7 +7,7 @@ class PageFrameOptions
public $raw; public $raw;
/** @var string */ /** @var string */
public $title = ''; public $title = 'Mikescher.com';
/** @var int */ /** @var int */
public $statuscode = 200; public $statuscode = 200;
@ -23,4 +23,39 @@ class PageFrameOptions
/** @var string */ /** @var string */
public $contentType = null; public $contentType = null;
/** @var string */
public $activeHeader = null;
/** @var string */
public $canonical_url = null;
/** @var string[] */
public $contentCSSClasses = [ 'content-responsive' ];
/** @var array */
public $stylesheets = [];
/** @var array */
public $scripts = [];
public function addStylesheet(string $url)
{
foreach ($this->stylesheets as $css) if ($css === $url) return;
$this->stylesheets []= $url;
}
public function addScript(string $url, bool $defer = false)
{
foreach ($this->scripts as &$script)
{
if ($script[0] === $url)
{
if (!$defer && $script[1]) $script[1] = false; // upgrade from defered to immediate script
return;
}
}
$this->scripts []= [ $url, $defer ];
}
} }

View File

@ -5,13 +5,13 @@ require_once "website.php";
class RuleEngine class RuleEngine
{ {
/** /**
* @param Website $app * @param Website $site
* @param array $urlConfig * @param array $urlConfig
* @return URLRoute * @return URLRoute
*/ */
public static function findRoute(Website $app, array $urlConfig): URLRoute public static function findRoute(Website $site, array $urlConfig): URLRoute
{ {
if ($app->isProd()) if ($site->isProd())
$requri = $_SERVER['REQUEST_URI']; $requri = $_SERVER['REQUEST_URI'];
else else
$requri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'localhost:80/'; $requri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'localhost:80/';
@ -24,16 +24,18 @@ class RuleEngine
foreach ($urlConfig as $rule) foreach ($urlConfig as $rule)
{ {
$route = self::testRule($app, $rule, $requri, $pathparts, $partcount); $route = self::testRule($site, $rule, $requri, $pathparts, $partcount);
if ($route === null) continue; if ($route === null) continue;
if ($route->needsAdminLogin && !$app->isLoggedIn()) return URLRoute::getLoginRoute($route, $requri); if ($route->needsAdminLogin && !$site->isLoggedInByCookie()) return URLRoute::getLoginRoute($route, $requri);
return $route;
} }
return URLRoute::getNotFoundRoute($requri); return URLRoute::getNotFoundRoute($requri);
} }
private static function testRule(Website $app, array $rule, string $requri, array $pathparts, int $partcount) private static function testRule(Website $site, array $rule, string $requri, array $pathparts, int $partcount)
{ {
if ($partcount !== count($rule['url'])) return null; if ($partcount !== count($rule['url'])) return null;
@ -95,7 +97,7 @@ class RuleEngine
$route->needsAdminLogin = isset($ctrlOpt['password']); $route->needsAdminLogin = isset($ctrlOpt['password']);
if ($app->isProd() && isHTTPRequest() && !in_array('http', $ctrlOpt)) if ($site->isProd() && isHTTPRequest() && !in_array('http', $ctrlOpt))
{ {
// enforce https // enforce https
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

View File

@ -16,52 +16,43 @@ class URLRoute
/** @var int */ /** @var int */
public $needsAdminLogin; public $needsAdminLogin;
/** @var int */
public $isAPI;
public function __construct(string $target, string $url) public function __construct(string $target, string $url)
{ {
$this->targetpath = __DIR__ . '/../pages/' . $target; $this->targetpath = __DIR__ . '/../pages/' . $target;
$this->full_url = $url; $this->full_url = $url;
$this->parameter = []; $this->parameter = [];
$this->needsAdminLogin = false; $this->needsAdminLogin = false;
$this->isAPI = false;
} }
/** /**
* @param Website $app * @param Website $site
* @return PageFrameOptions * @return PageFrameOptions
*/ */
public function get(Website $app): PageFrameOptions public function get(Website $site): PageFrameOptions
{ {
$pfo = new PageFrameOptions(); $pfo = new PageFrameOptions();
$pfo->title = 'Mikescher.com'; // default title $pfo->addStylesheet($site->isProd() ? ('/data/css/styles.min.css') : ('/data/css/styles.css'));
if ($this->isAPI)
{
$pfo->frame = 'no_frame.php';
$pfo->contentType = 'application/json';
}
return $this->getDirect($app, $pfo); return $this->getDirect($site, $pfo);
} }
/** /**
* @param Website $app * @param Website $site
* @param PageFrameOptions $pfo * @param PageFrameOptions $pfo
* @return PageFrameOptions * @return PageFrameOptions
*/ */
public function getDirect(Website $app, PageFrameOptions $pfo): PageFrameOptions public function getDirect(Website $site, PageFrameOptions $pfo): PageFrameOptions
{ {
@ob_end_clean(); @ob_end_clean();
ob_start(); ob_start();
global $ROUTE; global $ROUTE;
global $FRAME_OPTIONS; global $FRAME_OPTIONS;
global $APP; global $SITE;
$ROUTE = $this; $ROUTE = $this;
$FRAME_OPTIONS = $pfo; $FRAME_OPTIONS = $pfo;
$APP = $app; $SITE = $site;
/** @noinspection PhpIncludeInspection */ /** @noinspection PhpIncludeInspection */
require $this->targetpath; require $this->targetpath;
@ -79,7 +70,7 @@ class URLRoute
public static function getLoginRoute(URLRoute $route, string $requri): URLRoute public static function getLoginRoute(URLRoute $route, string $requri): URLRoute
{ {
$r = new URLRoute('login.php', $requri); $r = new URLRoute('login.php', $requri);
$r->parameter = [ 'redirect' => $route->full_url ]; $r->parameter = [ 'login_target' => $route->full_url ];
return $r; return $r;
} }
@ -89,7 +80,7 @@ class URLRoute
*/ */
public static function getNotFoundRoute(string $requri): URLRoute public static function getNotFoundRoute(string $requri): URLRoute
{ {
$r = new URLRoute('errors/not_found.php', $requri); $r = new URLRoute('error_notfound.php', $requri);
$r->parameter = []; $r->parameter = [];
return $r; return $r;
} }
@ -100,7 +91,7 @@ class URLRoute
*/ */
public static function getServerErrorRoute(string $requri): URLRoute public static function getServerErrorRoute(string $requri): URLRoute
{ {
$r = new URLRoute('errors/server_error.php', $requri); $r = new URLRoute('error_servererror.php', $requri);
$r->parameter = []; $r->parameter = [];
return $r; return $r;
} }

View File

@ -82,61 +82,6 @@ function formatMilliseconds($millis)
} }
} }
function includeAdditionalScript($script, $attr='', $printImmediately = false) {
global $ADDITIONAL_SCRIPTS;
if (in_array($script, $ADDITIONAL_SCRIPTS)) return false;
if ($printImmediately) {
$ADDITIONAL_SCRIPTS[$script] = ['src' => $script, 'attr' => $attr, 'consumed' => true];
echo '<script src="'.$script.'" type="text/javascript" '.$attr.'></script>';
return true;
} else {
$ADDITIONAL_SCRIPTS[$script] = ['src' => $script, 'attr' => $attr, 'consumed' => false];
return true;
}
}
function includeAdditionalStylesheet($sheet, $attr='', $printImmediately = false) {
global $ADDITIONAL_STYLESHEETS;
if (in_array($sheet, $ADDITIONAL_STYLESHEETS)) return false;
if ($printImmediately) {
$ADDITIONAL_STYLESHEETS[$sheet] = ['src' => $sheet, 'attr' => $attr, 'consumed' => true];
echo '<link rel="stylesheet" href="' . $sheet . '" '.$attr.'/>';
return true;
} else {
$ADDITIONAL_STYLESHEETS[$sheet] = ['src' => $sheet, 'attr' => $attr, 'consumed' => false];
return true;
}
}
function printHeaderCSS() {
global $CSS_BASE;
includeAdditionalStylesheet($CSS_BASE, '', true);
}
function printAdditionalScripts() {
global $ADDITIONAL_SCRIPTS;
foreach ($ADDITIONAL_SCRIPTS as $d) {
if ($d['consumed']) continue;
echo '<script src="' . $d['src'] . '" type="text/javascript" ' . $d['attr'] . '></script>';
$d['consumed'] = true;
}
}
function printAdditionalStylesheets() {
global $ADDITIONAL_STYLESHEETS;
foreach ($ADDITIONAL_STYLESHEETS as $d) {
if ($d['consumed']) continue;
echo '<link rel="stylesheet" href="' . $d['src'] . '" ' . $d['attr'] . '/>';
$d['consumed'] = true;
}
}
function isProd() { function isProd() {
global $CONFIG; global $CONFIG;
return $CONFIG['prod']; return $CONFIG['prod'];
@ -180,34 +125,6 @@ function convertLanguageToFlag($lang) {
return null; return null;
} }
function setLoginCookie($user, $pass)
{
$expires = time() + (24*60*60); // 24h
$hash = hash('sha256', $user . ';' . $pass . ';' . gmdate('Y-m-d'));
setcookie('mikescher_auth', $hash, $expires);
}
function isLoggedInByCookie()
{
static $_loginCache = null;
if ($_loginCache !== null) return $_loginCache;
global $CONFIG;
if (key_exists('mikescher_auth', $_COOKIE))
{
if (strlen($_COOKIE['mikescher_auth']) !== 64) return $_loginCache = false;
$auth = hash('sha256', $CONFIG['admin_username'] . ';' . $CONFIG['admin_password'] . ';' . gmdate('Y-m-d'));
if ($auth === $_COOKIE['mikescher_auth']) return $_loginCache = true;
}
return $_loginCache = false;
}
function clearLoginCookie()
{
setcookie("mikescher_auth", "", time()+30);
}
/** /**
* easy image resize function * easy image resize function
* @author http://www.nimrodstech.com/php-image-resize/ * @author http://www.nimrodstech.com/php-image-resize/

View File

@ -14,6 +14,9 @@ class Website
/** @var array */ /** @var array */
public $config; public $config;
/** @var bool|null */
public $isLoggedIn = null;
/** @var Database|null */ private $database = null; /** @var Database|null */ private $database = null;
/** @var AdventOfCode|null */ private $adventOfCode = null; /** @var AdventOfCode|null */ private $adventOfCode = null;
/** @var Blog|null */ private $blog = null; /** @var Blog|null */ private $blog = null;
@ -145,13 +148,13 @@ class Website
global $ROUTE; global $ROUTE;
global $FRAME_OPTIONS; global $FRAME_OPTIONS;
global $APP; global $SITE;
$ROUTE = $route; $ROUTE = $route;
$FRAME_OPTIONS = $pfo; $FRAME_OPTIONS = $pfo;
$APP = $this; $SITE = $this;
/** @noinspection PhpIncludeInspection */ /** @noinspection PhpIncludeInspection */
require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; require __DIR__ . '/../frames/' . $FRAME_OPTIONS->frame;
} }
public function Database() public function Database()
@ -222,6 +225,33 @@ class Website
if ($this->config == null) return true; if ($this->config == null) return true;
return $this->config['prod']; return $this->config['prod'];
} }
public function isLoggedInByCookie()
{
if ($this->isLoggedIn !== null) return $this->isLoggedIn;
if (key_exists('mikescher_auth', $_COOKIE))
{
if (strlen($_COOKIE['mikescher_auth']) !== 64) return ($this->isLoggedIn = false);
$auth = hash('sha256', $this->config['admin_username'] . ';' . $this->config['admin_password'] . ';' . gmdate('Y-m-d'));
if ($auth === $_COOKIE['mikescher_auth']) return ($this->isLoggedIn = true);
}
return ($this->isLoggedIn = false);
}
function setLoginCookie($user, $pass)
{
$expires = time() + (24*60*60); // 24h
$hash = hash('sha256', $user . ';' . $pass . ';' . gmdate('Y-m-d'));
setcookie('mikescher_auth', $hash, $expires);
}
function clearLoginCookie()
{
setcookie("mikescher_auth", "", time()+30);
}
} }
/** /**

View File

@ -1,37 +1,17 @@
<!DOCTYPE html> <?php
<html lang="en"> require_once (__DIR__ . '/../internals/website.php');
<?php require_once (__DIR__ . '/../internals/base.php'); ?>
<head>
<meta charset="utf-8">
<title>Mikescher.com</title>
<meta name="google-site-verification" content="pZOhmjeJcQbRMNa8xRLam4dwJ2oYwMwISY1lRKreSSs"/>
<link rel="icon" type="image/png" href="/data/images/favicon.png"/>
<link rel="canonical" href="https://www.mikescher.com/"/>
<?php printHeaderCSS(); ?>
</head>
<body>
<div id="mastercontainer">
<?php $HEADER_ACTIVE='home'; include (__DIR__ . '/../fragments/header.php'); ?> /** @var PageFrameOptions $FRAME_OPTIONS */ global $FRAME_OPTIONS;
/** @var URLRoute $ROUTE */ global $ROUTE;
/** @var Website $SITE */ global $SITE;
?>
<div id="content" class="content-responsive"> <?php include (__DIR__ . '/../fragments/panel_euler.php'); ?>
<?php include (__DIR__ . '/../fragments/panel_euler.php'); ?> <?php include (__DIR__ . '/../fragments/panel_programs.php'); ?>
<?php include (__DIR__ . '/../fragments/panel_programs.php'); ?> <?php include (__DIR__ . '/../fragments/panel_blog.php'); ?>
<?php include (__DIR__ . '/../fragments/panel_blog.php'); ?> <?php include (__DIR__ . '/../fragments/panel_books.php'); ?>
<?php include (__DIR__ . '/../fragments/panel_books.php'); ?> <?php include (__DIR__ . '/../fragments/panel_aoc.php'); ?>
<?php include (__DIR__ . '/../fragments/panel_aoc.php'); ?>
</div>
<?php include (__DIR__ . '/../fragments/footer.php'); ?>
</div>
<?php printAdditionalScripts(); ?>
<?php printAdditionalStylesheets(); ?>
</body>
</html>