From f9a692e6353161119b4d3c90196d2ceb526681cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 15 Jan 2020 01:46:31 +0100 Subject: [PATCH] index request works almost --- www/frames/default_frame.php | 14 ++++- www/internals/pageframeoptions.php | 37 ++++++++++++- www/internals/ruleengine.php | 16 +++--- www/internals/urlroute.php | 31 ++++------- www/internals/utils.php | 83 ------------------------------ www/internals/website.php | 36 +++++++++++-- www/pages/main.php | 42 ++++----------- 7 files changed, 112 insertions(+), 147 deletions(-) diff --git a/www/frames/default_frame.php b/www/frames/default_frame.php index 36045c4..713a2a0 100644 --- a/www/frames/default_frame.php +++ b/www/frames/default_frame.php @@ -15,7 +15,15 @@ require_once (__DIR__ . '/../internals/website.php'); <?php echo $FRAME_OPTIONS->title; ?> - + canonical_url !== null) echo ''; + foreach ($FRAME_OPTIONS->stylesheets as $cssfile) echo ''; + foreach ($FRAME_OPTIONS->scripts as $scriptfile) + { + if ($scriptfile[1]) echo ''; + } + ?>
@@ -40,7 +48,9 @@ require_once (__DIR__ . '/../internals/website.php');
- raw; ?> +
+ raw; ?> +

diff --git a/www/internals/pageframeoptions.php b/www/internals/pageframeoptions.php index e01ac02..726d00e 100644 --- a/www/internals/pageframeoptions.php +++ b/www/internals/pageframeoptions.php @@ -7,7 +7,7 @@ class PageFrameOptions public $raw; /** @var string */ - public $title = ''; + public $title = 'Mikescher.com'; /** @var int */ public $statuscode = 200; @@ -23,4 +23,39 @@ class PageFrameOptions /** @var string */ 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 ]; + } } \ No newline at end of file diff --git a/www/internals/ruleengine.php b/www/internals/ruleengine.php index 3db339d..1f96f5a 100644 --- a/www/internals/ruleengine.php +++ b/www/internals/ruleengine.php @@ -5,13 +5,13 @@ require_once "website.php"; class RuleEngine { /** - * @param Website $app + * @param Website $site * @param array $urlConfig * @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']; else $requri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'localhost:80/'; @@ -24,16 +24,18 @@ class RuleEngine 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->needsAdminLogin && !$app->isLoggedIn()) return URLRoute::getLoginRoute($route, $requri); + if ($route->needsAdminLogin && !$site->isLoggedInByCookie()) return URLRoute::getLoginRoute($route, $requri); + + return $route; } 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; @@ -95,7 +97,7 @@ class RuleEngine $route->needsAdminLogin = isset($ctrlOpt['password']); - if ($app->isProd() && isHTTPRequest() && !in_array('http', $ctrlOpt)) + if ($site->isProd() && isHTTPRequest() && !in_array('http', $ctrlOpt)) { // enforce https $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; diff --git a/www/internals/urlroute.php b/www/internals/urlroute.php index 8c65a8a..0ba3f79 100644 --- a/www/internals/urlroute.php +++ b/www/internals/urlroute.php @@ -16,52 +16,43 @@ class URLRoute /** @var int */ public $needsAdminLogin; - /** @var int */ - public $isAPI; - public function __construct(string $target, string $url) { $this->targetpath = __DIR__ . '/../pages/' . $target; $this->full_url = $url; $this->parameter = []; $this->needsAdminLogin = false; - $this->isAPI = false; } /** - * @param Website $app + * @param Website $site * @return PageFrameOptions */ - public function get(Website $app): PageFrameOptions + public function get(Website $site): PageFrameOptions { $pfo = new PageFrameOptions(); - $pfo->title = 'Mikescher.com'; // default title - if ($this->isAPI) - { - $pfo->frame = 'no_frame.php'; - $pfo->contentType = 'application/json'; - } + $pfo->addStylesheet($site->isProd() ? ('/data/css/styles.min.css') : ('/data/css/styles.css')); - return $this->getDirect($app, $pfo); + return $this->getDirect($site, $pfo); } /** - * @param Website $app + * @param Website $site * @param PageFrameOptions $pfo * @return PageFrameOptions */ - public function getDirect(Website $app, PageFrameOptions $pfo): PageFrameOptions + public function getDirect(Website $site, PageFrameOptions $pfo): PageFrameOptions { @ob_end_clean(); ob_start(); global $ROUTE; global $FRAME_OPTIONS; - global $APP; + global $SITE; $ROUTE = $this; $FRAME_OPTIONS = $pfo; - $APP = $app; + $SITE = $site; /** @noinspection PhpIncludeInspection */ require $this->targetpath; @@ -79,7 +70,7 @@ class URLRoute public static function getLoginRoute(URLRoute $route, string $requri): URLRoute { $r = new URLRoute('login.php', $requri); - $r->parameter = [ 'redirect' => $route->full_url ]; + $r->parameter = [ 'login_target' => $route->full_url ]; return $r; } @@ -89,7 +80,7 @@ class 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 = []; return $r; } @@ -100,7 +91,7 @@ class 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 = []; return $r; } diff --git a/www/internals/utils.php b/www/internals/utils.php index 13cf5ae..431262a 100644 --- a/www/internals/utils.php +++ b/www/internals/utils.php @@ -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 ''; - 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 ''; - 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 ''; - $d['consumed'] = true; - } -} - -function printAdditionalStylesheets() { - global $ADDITIONAL_STYLESHEETS; - - foreach ($ADDITIONAL_STYLESHEETS as $d) { - if ($d['consumed']) continue; - echo ''; - $d['consumed'] = true; - } -} - function isProd() { global $CONFIG; return $CONFIG['prod']; @@ -180,34 +125,6 @@ function convertLanguageToFlag($lang) { 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 * @author http://www.nimrodstech.com/php-image-resize/ diff --git a/www/internals/website.php b/www/internals/website.php index 11a1b58..3c18f5c 100644 --- a/www/internals/website.php +++ b/www/internals/website.php @@ -14,6 +14,9 @@ class Website /** @var array */ public $config; + /** @var bool|null */ + public $isLoggedIn = null; + /** @var Database|null */ private $database = null; /** @var AdventOfCode|null */ private $adventOfCode = null; /** @var Blog|null */ private $blog = null; @@ -145,13 +148,13 @@ class Website global $ROUTE; global $FRAME_OPTIONS; - global $APP; + global $SITE; $ROUTE = $route; $FRAME_OPTIONS = $pfo; - $APP = $this; + $SITE = $this; /** @noinspection PhpIncludeInspection */ - require __DIR__ . '/../pages/frame/' . $FRAME_OPTIONS->frame; + require __DIR__ . '/../frames/' . $FRAME_OPTIONS->frame; } public function Database() @@ -222,6 +225,33 @@ class Website if ($this->config == null) return true; 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); + } + } /** diff --git a/www/pages/main.php b/www/pages/main.php index 1cd4b33..ea37c14 100644 --- a/www/pages/main.php +++ b/www/pages/main.php @@ -1,37 +1,17 @@ - - - - - - Mikescher.com - - - - - - -
+ +/** @var PageFrameOptions $FRAME_OPTIONS */ global $FRAME_OPTIONS; +/** @var URLRoute $ROUTE */ global $ROUTE; +/** @var Website $SITE */ global $SITE; +?> -
+ - + - + - + - - - - -
- - - -
- - - - \ No newline at end of file + \ No newline at end of file