web-base/core/Elements/Document.class.php

81 lines
2.0 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
namespace Elements;
abstract class Document {
protected $head;
protected $body;
protected $user;
2020-04-02 21:39:02 +02:00
protected $databaseRequired;
2020-02-09 23:02:19 +01:00
public function __construct($user, $headClass, $bodyClass) {
$this->head = new $headClass($this);
$this->body = new $bodyClass($this);
$this->user = $user;
2020-04-02 21:39:02 +02:00
$this->databaseRequired = true;
2020-02-09 23:02:19 +01:00
}
public function getHead() { return $this->head; }
public function getBody() { return $this->body; }
public function getSQL() { return $this->user->getSQL(); }
public function getUser() { return $this->user; }
protected function sendHeaders() {
header("X-Frame-Options: DENY");
}
public static function createSearchableDocument($documentClass, $user) {
return new $documentClass($user);
}
public static function createDocument($class) {
// TODO: check instance, configuration, ..
require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/sql.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/conf/config.php';
// require_once realpath($_SERVER['DOCUMENT_ROOT']) . "/php/pages/$file.php";
require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/php/api/objects/User.php';
$connectionData = getSqlData($database);
$sql = connectSQL($connectionData);
if(!$sql->isConnected()) {
http_response_code(500);
die('Internal Database error');
}
$user = new CUser($sql);
$document = new $class($user);
$code = $document->getCode();
$document->sendHeaders();
$user->sendCookies();
die($code);
}
function getCode() {
2020-04-02 21:39:02 +02:00
if ($this->databaseRequired) {
2020-02-09 23:02:19 +01:00
$sql = $this->user->getSQL();
if (is_null($sql)) {
die("Database is not configured yet.");
2020-02-10 12:32:53 +01:00
} else if(!$sql->isConnected()) {
2020-02-09 23:02:19 +01:00
die("Database is not connected: " . $sql->getLastError());
}
}
$body = $this->body->getCode();
$head = $this->head->getCode();
$html = "<!DOCTYPE html>";
$html .= "<html>";
$html .= $head;
$html .= $body;
$html .= "</html>";
return $html;
}
};
?>