PaperBag/application/WebPage.php

92 lines
2.6 KiB
PHP

<?php
interface RequireAuth {}
class WebPage {
protected $httpStatus = 200;
protected $activePage;
public $status;
public $template;
public $title = "PaperBag";
public $lang = "EN";
public $navbar;
public $loggedIn = false;
public $pr = ""; // project root
public function __construct(){
$this->pr = Config::get('system', 'projectroot', '');
$this->loggedIn = Auth::checkLogin();
if($this instanceof RequireAuth && !$this->loggedIn) {
$returnTo = str_ireplace('index.php', '', $_SERVER['PHP_SELF']);
$_SESSION['pre-auth'] = $returnTo;
header("Location: ".$this->pr."/login.php");
return;
}
try {
$this->prepareNavbar();
$this->load();
if(!$this->template){
$this->template = str_replace('.php', '.html', $_SERVER['SCRIPT_NAME']);
}
if($this->httpStatus == 200){
global $twig;
if(DEBUG){
header("X-Template: ".$this->template);
header("X-Script-name: ".$_SERVER['SCRIPT_NAME']);
}
http_response_code(200);
echo $twig->render($this->template, $this->vars());
}
else {
http_response_code($this->httpStatus);
print_r( $this->vars() );
}
}
catch (\Twig\Error\LoaderError $e){
http_response_code(500);
if( $e->getCode() == 0){
print_r($e->getMessage());
}
else {
echo $e;
}
}
catch(\Twig\Error\RuntimeError | \Twig\Error\SyntaxError | Exception $e) {
http_response_code(500);
echo $e;
print_r( $this->vars() );
}
}
function load(){ throw new Exception("Incomplete implementation"); }
function vars(): array {
return (array) $this;
}
private function prepareNavbar(){
$this->activePage = str_replace('index.php', '', $_SERVER['REQUEST_URI']);
$this->navbar['links'] = array_merge([
["href"=>"/", "name"=>"Home", "active"=>$this->activePage=="/"?'active':''],
["href"=>"/plan/", "name"=>"Plan", "active"=>$this->activePage=="/plan/"?'active':''],
["href"=>"/review/", "name"=>"Review", "active"=>$this->activePage=="/review/"?'active':'']
], $this->navbar ?? []);
}
}
class WebPageAuth extends WebPage {
public function __construct(){
if(Auth::checklogin()){
parent::__construct();
}
}
}