too manu changes (will list by files)

.gitignore
ignores some launch files

changelog.md, readme.md
added end file line

public.php
moved
started the work on it

router.php
added require to functions & moved the function in the file
set an ini value to show errors
defied some constants
moved the public page management to public/public.php

exampleModule -> headGenerator.php
started working on module management

*.json
reworked files

function.php
added this file to manages cores functions

website.php
the Website Object for theme & modules

page.php
created a test theme
This commit is contained in:
Florian Bouillon 2019-02-17 00:12:50 +01:00
parent 0399e5043b
commit bc8fba19d4
15 changed files with 208 additions and 100 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode/
cache/

View File

@ -1,3 +0,0 @@
function testModule($modulesVars, $elementVars) {
return new DO
}

View File

@ -0,0 +1,5 @@
<?php
function headGenerator() {
return "<title>Title</title>";
}

View File

@ -1,4 +1,5 @@
{ {
"theme": "default", "theme": "default",
"cache": true,
"modules": ["default"] "modules": ["default"]
} }

View File

@ -1,6 +1 @@
{ {"Page":{"URI":"templates\/page.php","function":"page","static":true}}
"default": {
"URI": "pouet/pouet.php",
"static": true
}
}

View File

@ -0,0 +1,55 @@
<?php
function loadJSON($file) {
return json_decode(file_get_contents($file), true);
}
function getWebPage($useCache) {
$fileCache = ROOT . "/cache" . URL . ".html";
if($useCache && file_exists($fileCache)) {
return file_get_contents($fileCache);
}
// var_dump(URL);
$fileURI = ROOT . "/pages" . URL . ".json";
$json = loadJSON($fileURI);
$template = $json["template"];
$json["template"] = null;
define("PAGE", $json);
$templates = loadJSON(ROOT . "/admin/settings/templates.json");
require_once ROOT . "/admin/themes/" . SETTINGS["theme"] . "/" . $templates[$template]["URI"];
$function = $templates[$template]["function"];
if(function_exists($function)) {
$content = $function();
if($useCache && $templates[$template]["static"]) file_put_contents($fileCache, $content);
return $content;
return null;
}
}
function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
/*
function generateWebPage()
load pages/page.json
load variables for theme
generate the basic web page
*/
?>

40
admin/system/website.php Normal file
View File

@ -0,0 +1,40 @@
<?php
class Website {
private $root;
private const TEMPLATEJSON = "templates.json";
public function __construct(String $root) {
$this->root = $root;
}
private function getTemplateFileURI() {
return $this->root . "/admin/settings/" . self::TEMPLATEJSON;
}
public function addTemplate(String $name, String $path, String $func, $bool) {
var_dump($_SERVER);
$val = array(
$name => array(
"URI" => $path,
"function" => $func,
"static" => $bool
)
);
if(! file_exists($this->getTemplateFileURI())) {
file_put_contents($this->getTemplateFileURI(), json_encode($val));
} else {
$json = json_decode(file_get_contents($this->getTemplateFileURI()), true);
$json = array_merge($json, $val);
file_put_contents($this->getTemplateFileURI(), json_encode($json));
}
}
public function addJS() {
}
}

View File

@ -1,3 +1,13 @@
<?php <?php
function page() {
// require_once "includes/header.php";
// echo PAGE["content"];
// require_once "includes/footer.php";
return "<header>Header</header>" . PAGE["content"] . "<footer>Footer</footer>";
}
?> ?>

View File

@ -1,8 +1,8 @@
{ {
"title": "Home", "title": "Home",
"template": "page", "template": "Page",
"access": "public", "access": "public",
"content": "<h1>hello world</h1>", "content": "<main>Hello World</main>",
"modules": { "modules": {
"?": "?" "?": "?"
} }

View File

@ -1,21 +0,0 @@
<?php
/*
if cache is active
if active is active load cache (scripts.js styles.css)
load templates.json
if template has cache for webpage and cache exist
load the file
else
launch generateWebPage()
if modules are in the page
load the modules
function generateWebPage()
load pages/page.json
load variables for theme
generate the basic web page
*/
?>

40
public/public.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
* The process to load a public page (located in /pages)
*/
$isCacheActive = SETTINGS["cache"];
$webPage = "<html><head><placeholder type=\"head\"/></head><body>" . getWebPage($isCacheActive) . "</body>";
//load .css & .js
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($webPage);
libxml_clear_errors();
//handle modules here
echo $doc->saveHTML();
/*
if cache is active
if active is active load cache (scripts.js styles.css)
load templates.json
if template has cache for webpage and cache exist
load the file
else
launch generateWebPage()
load theme css & js
if modules are in the page
load the modules
*/
?>

View File

@ -1,20 +1,33 @@
<?php <?php
/**
* this file initialize some variables and do the first part of the routing (between public pages & admin pages)
*/
require_once "admin/system/functions.php";
ini_set('display_errors', 'On');
$url = strtolower(endsWith($_SERVER["REQUEST_URI"], "/") ? $_SERVER["REQUEST_URI"] . "index" : $_SERVER["REQUEST_URI"]); $url = strtolower(endsWith($_SERVER["REQUEST_URI"], "/") ? $_SERVER["REQUEST_URI"] . "index" : $_SERVER["REQUEST_URI"]);
define("URL", $url);
define("ROOT", __DIR__);
define("SETTINGS", loadJSON(ROOT . "/admin/settings/admin.json"));
// $website = new Website();
// var_dump(__DIR__);
$_SERVER = null;
if($url == "/test") { if($url == "/test") {
require_once "test.php"; require_once "test.php";
die; die;
} }
$_SERVER = null;
$fileURI = "./pages" . $url . ".json"; $fileURI = "./pages" . $url . ".json";
if(in_array(explode("/", $url)[1], ["login", "admin"])) echo ("this is the login/admin page"); if(in_array(explode("/", $url)[1], ["login", "admin"])) echo ("this is the login/admin page");
elseif (file_exists($fileURI)) { elseif (file_exists($fileURI)) {
$json = json_decode(file_get_contents($fileURI)); require_once "public/public.php";
echo ($json->title);
} else echo "404"; } else echo "404";
/* /*
@ -30,39 +43,4 @@ verify if user is logged in
*/ */
function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
?> ?>

View File

@ -1,31 +1,37 @@
<?php <?php
ini_set('display_errors', 'On');
$doc = new DOMDocument(); // $doc = new DOMDocument();
$test = "<html><body><p>pouet</p></body></html>"; // $test = "<html><body><p>pouet</p></body></html>";
$doc->loadHTML($test); // $doc->loadHTML($test);
appendHTML($doc->getElementsByTagName("body")->item(0), "<img src=\"./\">"); // appendHTML($doc->getElementsByTagName("body")->item(0), "<img src=\"./\">");
echo $doc->saveHTML(); // echo $doc->saveHTML();
// echo gettype(true);
function appendHTML(DOMNode $parent, $source) { // include_once "admin/system/website.php";
$tmpDoc = new DOMDocument(); // $website = new Website(__DIR__);
$html = "<html><body>"; // $website->addTemplate("Blog Page", "templates/blog.php", "page", true);
$html .= $source;
$html .= "</body></html>";
$tmpDoc->loadHTML('<?xml encoding="UTF-8">'.$html);
foreach ($tmpDoc->childNodes as $item) // phpinfo();
if ($item->nodeType == XML_PI_NODE)
$tmpDoc->removeChild($item);
$tmpDoc->encoding = 'UTF-8';
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) { // function appendHTML(DOMNode $parent, $source) {
$importedNode = $parent->ownerDocument->importNode($node, true); // $tmpDoc = new DOMDocument();
$parent->appendChild($importedNode); // $html = "<html><body>";
} // $html .= $source;
} // $html .= "</body></html>";
// $tmpDoc->loadHTML('<?xml encoding="UTF-8">'.$html);
// foreach ($tmpDoc->childNodes as $item)
// if ($item->nodeType == XML_PI_NODE)
// $tmpDoc->removeChild($item);
// $tmpDoc->encoding = 'UTF-8';
// foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
// $importedNode = $parent->ownerDocument->importNode($node, true);
// $parent->appendChild($importedNode);
// }
// }
?> ?>