162 lines
4.7 KiB
PHP
162 lines
4.7 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 $err = array();
|
|
public $msg = array();
|
|
public $data = array();
|
|
|
|
|
|
public function __construct(){
|
|
$t = new Translator();
|
|
|
|
$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 {
|
|
if(isset($_POST) && !empty($_POST)){
|
|
$this->sanitizePost();
|
|
$this->doPost();
|
|
}
|
|
|
|
$this->prepareNavbar();
|
|
$this->load();
|
|
|
|
if(!$this->template){
|
|
$this->template = str_replace('.php', '.html', $_SERVER['SCRIPT_NAME']);
|
|
}
|
|
|
|
if($this->httpStatus == 200 || $this instanceof ErrorPage){
|
|
global $twig;
|
|
|
|
if(DEBUG){
|
|
header("X-Template: ".$this->template);
|
|
header("X-Script-name: ".$_SERVER['SCRIPT_NAME']);
|
|
}
|
|
|
|
http_response_code($this->httpStatus);
|
|
|
|
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){
|
|
echo "<pre>";
|
|
print_r($e->getMessage());
|
|
echo "</pre>";
|
|
}
|
|
else {
|
|
echo $e;
|
|
}
|
|
}
|
|
catch(\Twig\Error\RuntimeError | \Twig\Error\SyntaxError | Exception $e) {
|
|
http_response_code(500);
|
|
|
|
if(!$this instanceof ErrorPage){
|
|
$e = new ErrorPage(500, $e, print_r( $this->vars(), true ));
|
|
}
|
|
else {
|
|
echo "<h1>A critical error occurred!</h1>";
|
|
echo "<pre>".$e."</pre>";
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
function load(){ throw new Exception("Incomplete implementation"); }
|
|
|
|
function doPost(){ throw new Exception("Incomplete implementation"); }
|
|
|
|
function vars(): array {
|
|
$this->debug = DEBUG;
|
|
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"=>__('verb', 'Plan'), "active"=>$this->activePage=="/plan/" ? 'active' : '' ],
|
|
["href"=>"/review/", "name"=>__('Review'), "active"=>$this->activePage=="/review/" ? 'active' : '' ]
|
|
], $this->navbar ?? []);
|
|
}
|
|
|
|
protected function sanitizePost(){
|
|
$data = [];
|
|
|
|
foreach($_POST as $key => $value){
|
|
if(($data[$key] = Utils::filter($value)) === false){
|
|
$err[] = __("Failed to sanitize: `%s`: %s \t-\t type: %s\n", $key, $value, gettype($value));
|
|
}
|
|
}
|
|
|
|
if(!empty($err)){ return $err; }
|
|
$this->data = $data;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class WebPageAuth extends WebPage {
|
|
public function __construct(){
|
|
if(Auth::checklogin()){
|
|
parent::__construct();
|
|
}
|
|
}
|
|
}
|
|
|
|
class ErrorPage extends WebPage {
|
|
|
|
public $errCode = 500;
|
|
public $errormessage = "An unexpected error occurred";
|
|
public $errorDescription = "";
|
|
public $template = "errors/500.html";
|
|
|
|
public function load(){
|
|
if($this->errCode != 500){
|
|
$this->template = "errors/$this->errCode.html";
|
|
}
|
|
}
|
|
|
|
public function __construct($errCode = 500, $e = "", $extra = ""){
|
|
$this->errCode = $errCode;
|
|
if($e instanceof Exception){
|
|
$this->errormessage = $e->getMessage();
|
|
}
|
|
$this->errorDescription = $e;
|
|
$this->errorDescription .= "\n".$extra;
|
|
|
|
parent::__construct();
|
|
}
|
|
}
|
|
|
|
function isExecutingPage($file) : bool {
|
|
$file1 = explode("/", $file);
|
|
$request = str_replace('/','', $_SERVER['REQUEST_URI']);
|
|
if(strstr($request, '?')){
|
|
$request = explode('?', $request)[0];
|
|
}
|
|
return $request == $file1[ array_key_last($file1) ];
|
|
} |