commit 7b5413cbe1e254d9b22a053b42b76dbc38445fb5
parent 9547029ddbc1d727463a85275d8e00f6a6bb9704
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 22 Oct 2020 13:49:13 +0200
Refactoring: Generating the root HTML with a clean template.
Diffstat:
6 files changed, 238 insertions(+), 137 deletions(-)
diff --git a/modules/cms/ui/UI.class.php b/modules/cms/ui/UI.class.php
@@ -6,6 +6,7 @@ use BadMethodCallException;
use cms\action\RequestParams;
use cms\Dispatcher;
use Exception;
+use template_engine\engine\TemplateRunner;
use util\Http;
use logger\Logger;
use LogicException;
@@ -104,7 +105,7 @@ class UI
if ( DEVELOPMENT )
header('X-OR-Template: '.$templateFile);
- $engine = new TemplateEngine();
+ $engine = new TemplateRunner();
$engine->request = $request;
$engine->executeTemplate( $templateFile, $outputData );
}
@@ -116,25 +117,10 @@ class UI
private static function setContentSecurityPolicy()
{
// config is not loaded yet.
- $contentSecurityPolicyEntries = array(
- 'default-src \'none\'',
- // no eval, no inline.
- 'script-src \'self\'',
- // No <object>, <embed> or <applet>.
- 'object-src \'none\'',
- // no external CSS
- 'style-src \'self\'',
- // no external images.
- 'img-src \'self\'',
- // No <audio>, <video> elements
- 'media-src \'none\'',
- // For preview of urls we need to show every url in an iframe.
- 'frame-src *',
- 'worker-src \'self\'',
- 'form-action \'self\'',
- 'font-src \'self\'',
- // Ajax-Calls
- 'connect-src \'self\'');
+ $contentSecurityPolicyEntries = [
+ 'default-src \'self\'', // Default for all is 'self'
+ 'frame-src *' // For preview of urls we need to show every url in an iframe.
+ ];
header('Content-Security-Policy: ' . implode(';', $contentSecurityPolicyEntries));
}
diff --git a/modules/cms/ui/action/IndexAction.class.php b/modules/cms/ui/action/IndexAction.class.php
@@ -132,6 +132,7 @@ class IndexAction extends Action
$this->setTemplateVar('styleLink' , $this->getStyleLink() );
$this->setTemplateVar('themeStyleLink', Html::url('index','themestyle') );
+ $this->setTemplateVar('manifestLink' , Html::url('index','manifest' ) );
$styleConfig = C::config('style-default'); // default style config
$userStyleConfig = C::config('style', $style); // user style config
diff --git a/modules/cms/ui/themes/default/html/views/index/show.php b/modules/cms/ui/themes/default/html/views/index/show.php
@@ -1,96 +1,77 @@
-<?php
- defined('APP_STARTED') || die('Forbidden');
- use cms\base\Configuration as C;
- use cms\base\Language as L;
- use cms\base\Startup;
- use util\Html;
- if (!headers_sent()) header('Content-Type: text/html; charset=UTF-8')
-?><!DOCTYPE html>
-<html class="or-theme-<?php echo strtolower($style) ?> nojs" lang="<?php $language ?>">
-<head>
- <title data-default="<?php echo htmlentities($defaultTitle,ENT_QUOTES|ENT_HTML5) ?>"><?php echo htmlentities($defaultTitle,ENT_COMPAT|ENT_HTML5) ?></title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta charset="<?php echo $charset ?>">
- <meta name="robots" content="noindex,nofollow" >
- <script src="<?php echo $scriptLink ?>" defer></script>
- <link rel="stylesheet" type="text/css" href="<?php echo $styleLink ?>" />
- <link rel="stylesheet" type="text/css" href="<?php echo $themeStyleLink ?>" />
- <meta id="theme-color" name="theme-color" content="<?php echo $themeColor ?>" />
- <link rel="manifest" href="<?php echo Html::url('index','manifest',0) ?>" />
- <link rel="shortcut icon" type="image/x-icon" href="<?php echo $favicon_url ?>">
-</head>
-
-<body>
-
-
-<div id="workbench" class="or-workbench or--initial-hidden">
-
- <header id="title" class="or-workbench-title or-view or-act-view-static" data-action="title" data-method="show">
- </header>
-
-
- <div class="or-main-area">
-
- <nav class="or-navigation">
- <div class="or-view or-act-view-static" data-action="tree" data-method="show">
- </div>
-
+<?php defined('APP_STARTED') || die('Forbidden'); use \template_engine\Output as O; ?><!DOCTYPE html><html class="<?php echo O::escapeHtml('or-theme-'.@$style.' nojs') ?>" lang="<?php echo O::escapeHtml(''.@$language.'') ?>"><?php echo '' ?>
+ <head><?php echo '' ?>
+ <title data-default="<?php echo O::escapeHtml(''.@$defaultTitle.'') ?>"><?php echo '' ?>
+ <?php echo O::escapeHtml(''.@$defaultTitle.'') ?>
+ </title>
+ <meta name="<?php echo O::escapeHtml('viewport') ?>" content="<?php echo O::escapeHtml('width=device-width, initial-scale=1.0') ?>" /><?php echo '' ?>
+ <meta charset="<?php echo O::escapeHtml(''.@$charset.'') ?>" /><?php echo '' ?>
+ <meta name="<?php echo O::escapeHtml('robots') ?>" content="<?php echo O::escapeHtml('noindex,nofollow') ?>" /><?php echo '' ?>
+ <script src="<?php echo O::escapeHtml(''.@$scriptLink.'') ?>" defer="<?php echo O::escapeHtml('defer') ?>"><?php echo '' ?>
+ </script>
+ <link rel="<?php echo O::escapeHtml('stylesheet') ?>" type="<?php echo O::escapeHtml('text/css') ?>" href="<?php echo O::escapeHtml(''.@$styleLink.'') ?>" /><?php echo '' ?>
+ <link rel="<?php echo O::escapeHtml('stylesheet') ?>" type="<?php echo O::escapeHtml('text/css') ?>" href="<?php echo O::escapeHtml(''.@$themeStyleLink.'') ?>" /><?php echo '' ?>
+ <meta id="<?php echo O::escapeHtml('theme-color') ?>" name="<?php echo O::escapeHtml('theme-color') ?>" content="<?php echo O::escapeHtml(''.@$themeColor.'') ?>" /><?php echo '' ?>
+ <link rel="<?php echo O::escapeHtml('manifest') ?>" href="<?php echo O::escapeHtml(''.@$manifestLink.'') ?>" /><?php echo '' ?>
+ <link rel="<?php echo O::escapeHtml('shortcut icon') ?>" type="<?php echo O::escapeHtml('image/x-icon') ?>" href="<?php echo O::escapeHtml(''.@$favicon_url.'') ?>" /><?php echo '' ?>
+ </head>
+ <body><?php echo '' ?>
+ <div id="<?php echo O::escapeHtml('workbench') ?>" class="<?php echo O::escapeHtml('or-workbench or--initial-hidden') ?>"><?php echo '' ?>
+ <header id="<?php echo O::escapeHtml('title') ?>" class="<?php echo O::escapeHtml('or-workbench-title or-view or-act-view-static') ?>" data-action="<?php echo O::escapeHtml('title') ?>" data-method="<?php echo O::escapeHtml('show') ?>"><?php echo '' ?>
+ </header>
+ <div class="<?php echo O::escapeHtml('or-main-area') ?>"><?php echo '' ?>
+ <nav class="<?php echo O::escapeHtml('or-navigation') ?>"><?php echo '' ?>
+ <div class="<?php echo O::escapeHtml('or-view or-act-view-static') ?>" data-action="<?php echo O::escapeHtml('tree') ?>" data-method="<?php echo O::escapeHtml('show') ?>"><?php echo '' ?>
+ </div>
</nav>
-
- <div class="or-workplace">
-
- <main id="editor">
- <header>
- <div class="or-breadcrumb"></div>
+ <div class="<?php echo O::escapeHtml('or-workplace') ?>"><?php echo '' ?>
+ <main id="<?php echo O::escapeHtml('editor') ?>"><?php echo '' ?>
+ <header><?php echo '' ?>
+ <div class="<?php echo O::escapeHtml('or-breadcrumb') ?>"><?php echo '' ?>
+ </div>
+ </header>
+ <?php foreach((array)$methodList as $list_key=>$method) { ?>
+ <section class="<?php echo O::escapeHtml('or-collapsible or-collapsible--is-open') ?>"><?php echo '' ?>
+ <header class="<?php echo O::escapeHtml('or-view-header or-collapsible-act-switch or-collapsible-title') ?>"><?php echo '' ?>
+ <span class="<?php echo O::escapeHtml('or-view-icon or-image-icon or-image-icon--method-'.@$method['name'].'') ?>"><?php echo '' ?>
+ </span>
+ <span><?php echo O::escapeHtml(''.@O::lang('METHOD_'.@$method['name'].'').'') ?>
+ </span>
+ <i class="<?php echo O::escapeHtml('or-collapsible--on-open or-image-icon or-image-icon--node-open') ?>"><?php echo '' ?>
+ </i>
+ <i class="<?php echo O::escapeHtml('or-collapsible--on-closed or-image-icon or-image-icon--node-closed') ?>"><?php echo '' ?>
+ </i>
</header>
-
- <?php foreach( $methodList as $method ) { ?>
- <?php if (DEVELOPMENT) echo "<!-- Section for: ".$method['name']." -->"; ?>
- <section class="or-collapsible or-collapsible--is-<?php echo $method ['open']?'open':'closed' ?>">
-
- <header class="or-view-header or-collapsible-act-switch or-collapsible-title">
- <span class="or-view-icon or-image-icon or-image-icon--method-<?php echo $method['name'] ?>" ></span>
- <?php echo L::lang('METHOD_'.$method['name'] ) ?>
- <i class="or-collapsible--on-open or-image-icon or-image-icon--node-open"></i>
- <i class="or-collapsible--on-closed or-image-icon or-image-icon--node-closed"></i>
- </header>
-
- <div class="or-collapsible-value or-view or-act-view-loader or-closable" data-method="<?php echo $method['name'] ?>"></div>
- </section>
- <?php } ?>
-
- </main>
+ <div class="<?php echo O::escapeHtml('or-collapsible-value or-view or-act-view-loader or-closable') ?>" data-method="<?php echo O::escapeHtml(''.@$method['name'].'') ?>"><?php echo '' ?>
+ </div>
+ </section>
+ <?php } ?>
+ </main>
</div>
-
+ </div>
</div>
-
-</div>
-
-
-<?php /* Modal dialog */ ?>
-<div id="dialog" class="or-dialog <?php echo empty($dialogAction)?'':'or-dialog--is-open' ?>" data-action="<?php echo (!empty($dialogAction)?$dialogAction:'') ?>" data-method="<?php echo (!empty($dialogMethod)?$dialogMethod:'') ?>">
- <div class="or-view or-round-corners">
+ <div id="<?php echo O::escapeHtml('dialog') ?>" class="<?php echo O::escapeHtml('or-dialog or-dialog--is-closed') ?>" data-action="<?php echo O::escapeHtml(''.@$dialogAction.'') ?>" data-method="<?php echo O::escapeHtml(''.@$dialogMethod.'') ?>"><?php echo '' ?>
+ <div class="<?php echo O::escapeHtml('or-view or-round-corners') ?>"><?php echo '' ?>
+ </div>
+ <div class="<?php echo O::escapeHtml('or-dialog-filler') ?>"><?php echo '' ?>
+ <span class="<?php echo O::escapeHtml('or-dialog-filler-icon or-btn or-image-icon or-image-icon--menu-close') ?>"><?php echo '' ?>
+ </span>
+ </div>
</div>
-
- <div class="or-dialog-filler"><?php /* empty element, this is only for styling the background. */ ?>
- <span class="or-dialog-filler-icon or-btn or-image-icon or-image-icon--menu-close"></span>
+ <div id="<?php echo O::escapeHtml('noticebar') ?>" class="<?php echo O::escapeHtml('or-notices') ?>"><?php echo '' ?>
</div>
-</div>
-
-
-<div id="noticebar" class="or-notices">
-</div>
-
-<?php /* Inline Notices */ foreach( $notices as $notice ) { ?>
-<div class="or--invisible or-act-initial-notice"><?php echo $notice['text'] ?></div>
-<?php } ?>
-
-
-<footer class="or--initial-hidden" id="footer">
-
-</footer>
-
-<noscript><div class="noscript"><em>Javascript is required to view this site</em></div></noscript>
-
-</body>
+ <?php foreach((array)$notices as $list_key=>$notice) { ?>
+ <div class="<?php echo O::escapeHtml('or--invisible or-act-initial-notice') ?>"><?php echo '' ?>
+ <span><?php echo O::escapeHtml(''.@$notice['text'].'') ?>
+ </span>
+ </div>
+ <?php } ?>
+ <footer class="<?php echo O::escapeHtml('or--initial-hidden') ?>" id="<?php echo O::escapeHtml('footer') ?>"><?php echo '' ?>
+ </footer>
+ <noscript><?php echo '' ?>
+ <div class="<?php echo O::escapeHtml('noscript') ?>"><?php echo '' ?>
+ <em><?php echo '' ?>
+ </em>
+ </div>
+ </noscript>
+ </body>
</html>
\ No newline at end of file
diff --git a/modules/cms/ui/themes/default/html/views/index/show.tpl.src.xml b/modules/cms/ui/themes/default/html/views/index/show.tpl.src.xml
@@ -0,0 +1,102 @@
+<!DOCTYPE html>
+<html
+ xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:or="http://www.openrat.de/template"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.openrat.de/template ../../../../../../../template_engine/components/template.xsd"
+ class="or-theme-${style} nojs"
+ lang="${language}">
+ <head>
+ <title data-default="${defaultTitle}">
+ <or:text type="none" value="${defaultTitle}"/>
+ </title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+ <meta charset="${charset}"/>
+ <meta name="robots" content="noindex,nofollow"/>
+ <script src="${scriptLink}" defer="defer"/>
+ <link rel="stylesheet" type="text/css" href="${styleLink}"/>
+ <link rel="stylesheet" type="text/css" href="${themeStyleLink}"/>
+ <meta id="theme-color" name="theme-color" content="${themeColor}"/>
+ <link rel="manifest" href="${manifestLink}"/>
+ <link rel="shortcut icon" type="image/x-icon" href="${favicon_url}"/>
+ </head>
+
+ <body>
+
+ <div id="workbench" class="or-workbench or--initial-hidden">
+
+ <header id="title" class="or-workbench-title or-view or-act-view-static" data-action="title"
+ data-method="show">
+ </header>
+
+
+ <div class="or-main-area">
+
+ <nav class="or-navigation">
+ <div class="or-view or-act-view-static" data-action="tree" data-method="show">
+ </div>
+
+ </nav>
+
+ <div class="or-workplace">
+
+ <main id="editor">
+ <header>
+ <div class="or-breadcrumb"></div>
+ </header>
+
+ <or:list list="${methodList}" value="method">
+ <section class="or-collapsible or-collapsible--is-open">
+
+ <header class="or-view-header or-collapsible-act-switch or-collapsible-title">
+ <span
+ class="or-view-icon or-image-icon or-image-icon--method-${method.name}"></span>
+ <or:text value="${message:METHOD_${method.name}}"/>
+ <i class="or-collapsible--on-open or-image-icon or-image-icon--node-open"></i>
+ <i class="or-collapsible--on-closed or-image-icon or-image-icon--node-closed"></i>
+ </header>
+
+ <div class="or-collapsible-value or-view or-act-view-loader or-closable"
+ data-method="${method.name}"/>
+ </section>
+ </or:list>
+ </main>
+ </div>
+
+ </div>
+
+ </div>
+
+
+ <!-- Modal dialog -->
+ <div id="dialog" class="or-dialog or-dialog--is-closed" data-action="${dialogAction}"
+ data-method="${dialogMethod}">
+ <div class="or-view or-round-corners">
+ </div>
+
+ <div class="or-dialog-filler"><!-- empty element, this is only for styling the background -->
+ <span class="or-dialog-filler-icon or-btn or-image-icon or-image-icon--menu-close"></span>
+ </div>
+ </div>
+
+
+ <div id="noticebar" class="or-notices">
+ </div>
+ <or:list list="${notices}" value="notice">
+ <div class="or--invisible or-act-initial-notice">
+ <or:text value="${notice.text}"/>
+ </div>
+ </or:list>
+
+ <footer class="or--initial-hidden" id="footer">
+
+ </footer>
+
+ <noscript>
+ <div class="noscript">
+ <em>Javascript is required to view this site</em>
+ </div>
+ </noscript>
+
+ </body>
+</html>+
\ No newline at end of file
diff --git a/modules/template_engine/engine/TemplateEngine.class.php b/modules/template_engine/engine/TemplateEngine.class.php
@@ -13,9 +13,7 @@ use template_engine\components\html\Component;
use template_engine\components\html\NativeHtmlComponent;
/**
- * Wandelt eine Vorlage in ein PHP-Skript um.
- *
- * Die Vorlage wird gesparst, Elemente werden geladen und in die Zieldatei kopiert.
+ * Converting an XML-Template to a PHP file.
*
* @author Jan Dankert
* @package openrat.services
@@ -82,13 +80,13 @@ class TemplateEngine
// creating a tree of components
$rootComponent = $this->processElement( $document->documentElement );
+ if ( $document->doctype ) {
+ file_put_contents( $filename, '<!DOCTYPE '.$document->doctype->name.'>',FILE_APPEND );
+ }
+
// converting the component tree to a element tree
$rootElement = $rootComponent->getElement();
-
- $writtenBytes = file_put_contents( $filename, $rootElement->render( new XMLFormatter(' ')),FILE_APPEND );
-
- if ( $writtenBytes === FALSE )
- throw new LogicException("Unable writing to output file: '$filename'");
+ file_put_contents( $filename, $rootElement->render( new XMLFormatter(' ')),FILE_APPEND );
// CHMOD ausfuehren.
if ( @$confCompiler['chmod'] )
@@ -185,9 +183,15 @@ class TemplateEngine
$component = new NativeHtmlComponent();
$component->tag = $element->localName;
+
+ $element->removeAttribute('xsi:schemaLocation');
$component->attributes = $element->attributes;
$component->init();
+ foreach( $element->childNodes as $child ) {
+ $component->addChildComponent( $this->processElement( $child,$depth ) );
+ }
+
return $component;
}
@@ -214,25 +218,5 @@ class TemplateEngine
$document->load( $filename );
return $document;
}
-
-
- /**
- * Executes the required template and writes the output to standard-out..
- *
- * @param $templateFile string filename of template
- * @param $outputData array output data
- */
- public function executeTemplate($templateFile, $outputData)
- {
- if ( ! is_file($templateFile) )
- throw new LogicException("Template file '$templateFile' was not found.");
-
- // Extracting all output data into the actual context
- extract($outputData);
-
- // Include the template
- require_once($templateFile);
- }
-
}
diff --git a/modules/template_engine/engine/TemplateRunner.class.php b/modules/template_engine/engine/TemplateRunner.class.php
@@ -0,0 +1,46 @@
+<?php
+
+
+namespace template_engine\engine;
+use DomainException;
+use DOMDocument;
+use DOMElement;
+use Exception;
+use LogicException;
+use template_engine\element\XMLFormatter;
+use template_engine\element\PHPBlockElement;
+use template_engine\components\html\Component;
+use template_engine\components\html\NativeHtmlComponent;
+
+/**
+ * Executes a template.
+ *
+ * @author Jan Dankert
+ */
+class TemplateRunner
+{
+ /**
+ * @var \cms\action\RequestParams
+ */
+ public $request;
+
+ /**
+ * Executes the required template and writes the output to standard-out..
+ *
+ * @param $templateFile string filename of template
+ * @param $outputData array output data
+ */
+ public function executeTemplate($templateFile, $outputData)
+ {
+ if ( ! is_file($templateFile) )
+ throw new LogicException("Template file '$templateFile' was not found.");
+
+ // Extracting all output data into the actual context
+ extract($outputData);
+
+ // Include the template
+ require_once($templateFile);
+ }
+
+}
+