openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8740b59946e7215671fcc4eb8adfed45b9e31806
parent 9bca2ea32f5b8fe61ed9e2dc0025934297faceb0
Author: dankert <openrat@jandankert.de>
Date:   Sun, 13 Feb 2022 19:39:49 +0100

Refactoring: Special output type "preview" for previewing pages and files.

Diffstat:
Mmodules/cms/action/RequestParams.class.php | 1+
Mmodules/cms/action/page/PagePreviewAction.class.php | 13++++++++++++-
Mmodules/cms/action/page/PageShowAction.class.php | 4++--
Mmodules/cms/generator/ValueGenerator.class.php | 9++-------
Mmodules/cms/generator/link/PreviewLink.class.php | 12+++++++-----
Dmodules/cms/output/HtmlOutput.class.php | 166-------------------------------------------------------------------------------
Mmodules/cms/output/OutputFactory.class.php | 19++++++++++++-------
Amodules/cms/output/PreviewOutput.class.php | 26++++++++++++++++++++++++++
Amodules/cms/output/UIOutput.class.php | 166+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmodules/util/Html.class.php | 15+++++++++++++++
10 files changed, 243 insertions(+), 188 deletions(-)

diff --git a/modules/cms/action/RequestParams.class.php b/modules/cms/action/RequestParams.class.php @@ -23,6 +23,7 @@ class RequestParams const PARAM_MODEL_ID = 'modelid' ; const PARAM_PROJECT_ID = 'projectid' ; const PARAM_DATABASE_ID = 'dbid' ; + const PARAM_OUTPUT = 'output' ; public $action; public $method; diff --git a/modules/cms/action/page/PagePreviewAction.class.php b/modules/cms/action/page/PagePreviewAction.class.php @@ -6,11 +6,22 @@ use cms\action\RequestParams; use util\Html; class PagePreviewAction extends PageAction implements Method { + + + /** + * Calculates the URL for the page preview + */ public function view() { + $this->setModelAndLanguage(); - $this->setTemplateVar('preview_url',Html::url('page','show',$this->page->objectid,array(RequestParams::PARAM_LANGUAGE_ID=>$this->page->getProject()->getDefaultLanguageId(),RequestParams::PARAM_MODEL_ID=>$this->page->getProject()->getDefaultModelId()) ) ); + $this->setTemplateVar('preview_url',Html::url('page','show',$this->page->objectid,array( + RequestParams::PARAM_OUTPUT => 'preview', + RequestParams::PARAM_LANGUAGE_ID => $this->page->getProject()->getDefaultLanguageId(), + RequestParams::PARAM_MODEL_ID => $this->page->getProject()->getDefaultModelId()) ) ); } + + public function post() { } } diff --git a/modules/cms/action/page/PageShowAction.class.php b/modules/cms/action/page/PageShowAction.class.php @@ -46,11 +46,11 @@ class PageShowAction extends PageAction implements Method { { ob_start(); require( $generator->getCache()->load()->getFilename() ); - $this->setTemplateVar('output',ob_get_contents() ); + $this->setTemplateVar('value',ob_get_contents() ); ob_end_clean(); } else - $this->setTemplateVar('output',$generator->getCache()->get()); + $this->setTemplateVar('value',$generator->getCache()->get()); } public function post() { diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php @@ -900,18 +900,13 @@ class ValueGenerator extends BaseGenerator break; case 'edit_url': $raw = true; - $db = \util\Session::getDatabase(); - $inhalt = Html::url('page',null,$page->objectid,array('dbid'=>$db->id)); + $inhalt = Html::locationUrl('page',$page->objectid ); break; case 'edit_fullurl': $raw = true; $inhalt = Http::getServer(); - // Der Link soll nicht auf die API, sondern auf das UI zeigen. - if ( substr($inhalt,-4) == 'api/' ) - $inhalt = substr($inhalt,0,-4); - - $inhalt .= '/#/page/'.$page->objectid; + $inhalt .= Html::locationUrl('page',$page->objectid ); break; case 'lastch_user_username': $user = $page->lastchangeUser; diff --git a/modules/cms/generator/link/PreviewLink.class.php b/modules/cms/generator/link/PreviewLink.class.php @@ -33,22 +33,24 @@ class PreviewLink implements LinkFormat /** - * @param $from \cms\model\BaseObject - * @param $to \cms\model\BaseObject + * Calculates the Preview Link to an object. + * + * @param $from \cms\model\BaseObject this is the source object from which the links points to $to. + * @param $to \cms\model\BaseObject the target where the link points to. */ public function linkToObject( BaseObject $from, BaseObject $to ) { $param = [ - 'oid' => '__OID__'.$to->objectid.'__' - ]; + 'oid' => '__OID__'.$to->objectid.'__', + RequestParams::PARAM_OUTPUT => 'preview', + ]; if ( $this->context instanceof PageContext ) { $param[ RequestParams::PARAM_MODEL_ID ] = $this->context->modelId; $param[ RequestParams::PARAM_LANGUAGE_ID ] = $this->context->languageId; } - // Interne Verlinkungen in der Seitenvorschau switch( $to->typeid ) { case BaseObject::TYPEID_FOLDER: diff --git a/modules/cms/output/HtmlOutput.class.php b/modules/cms/output/HtmlOutput.class.php @@ -1,166 +0,0 @@ -<?php - -namespace cms\output; - -use BadMethodCallException; -use cms\action\RequestParams; -use cms\base\Language as L; -use cms\base\Startup;use cms\Dispatcher; -use cms\output\BaseOutput; -use Exception; -use template_engine\engine\TemplateRunner; -use util\Http; -use logger\Logger; -use LogicException; -use \util\exception\ObjectNotFoundException; -use util\exception\UIException; -use util\exception\SecurityException; -use template_engine\engine\TemplateEngine; -use util\text\TextMessage; - - -/** - * The HTML output is calling a template for the user interface. - */ -class HtmlOutput extends BaseOutput -{ - /** - * Preparing the client... - */ - protected function beforeAction($request) - { - // Sending the Content-Security-Policy. - self::setContentSecurityPolicy(); - - if ( @$_REQUEST['scope']=='openid' ) { - $request->redirectActionAndMethod('login','oidc'); - } - elseif (empty($request->action)) { - $request->redirectActionAndMethod('index','show' ); - } - - if ( $request->isAction ) - throw new \RuntimeException('The HTML output driver does not accept POST requests'); - - } - - - - protected function outputData($request, $data) - { - // The action is able to change its method and action name. - $subaction = $request->method; - $action = $request->action; - - - $this::outputTemplate($request,$action, $subaction, $data['output'] ); - } - - - /** - * Executes and outputs a HTML template. - * - * @param $request RequestParams - * @param $action string action - * @param $subaction string method - * @param $outputData array Output data - */ - private static function outputTemplate($request, $action, $subaction, $outputData) - { - $templateFile = Startup::MODULE_DIR . 'cms/ui/themes/default/html/views/' . $action.'/'.$subaction . '.php'; - - if ( DEVELOPMENT ) - header('X-OR-Template: '.$templateFile); - - $engine = new TemplateRunner(); - //$engine->request = $request; - $engine->executeTemplate( $templateFile, $outputData ); - } - - - /** - * Content-Security-Policy. - */ - private static function setContentSecurityPolicy() - { - // config is not loaded yet. Allow nothing... - header('Content-Security-Policy: default-src \'none\'' ); - - // This will be overwritten by the index action - } - - - /** - * @param $text - * @param $cause Exception the cause, or <code>null</code> if not available. - */ - protected function setError($text, $cause) - { - if ( !headers_sent() ) - // The following HTML contains inline CSS code, so we have to allow inline CSS. - header('Content-Security-Policy: style-src: inline; default: self'); - -?><!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="utf-8"/> - <meta name="viewport" content="width=device-width, initial-scale=1"/> - <title><?php echo $text ?></title> - <style type="text/css"> - - header, main { - display: block - } - - body { - width: 100%; - height: 100%; - background-color: rgba(13,8,5,0.58); - color: white; - font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; - line-height: 1.4; - font-size: 1.5em; - text-align: center; - } - - pre { - margin:10%; - width: 80%; - overflow: visible; - height: 40%; - color: silver; - text-align: left; - font-size: 0.6rem; - } - - h1 { - font-size: 2em; - } -</style> -</head> -<body> - -<header> - <h1><?php echo $text ?></h1> -</header> - -<main> - <p>Something went terribly wrong &#x1F61E;</p> - - <pre><?php // Display exceptions only in development mode, because they may contain sensitive internal information like passwords. - if ($cause && defined('DEVELOPMENT') && DEVELOPMENT ) { - echo $cause->__toString(); - } - ?></pre> -</main> - -</body> -</html><?php - - } - - public function getContentType() - { - return 'text/html'; - } -} diff --git a/modules/cms/output/OutputFactory.class.php b/modules/cms/output/OutputFactory.class.php @@ -2,6 +2,7 @@ namespace cms\output; +use cms\action\RequestParams; use util\Http; class OutputFactory { @@ -11,9 +12,10 @@ class OutputFactory { const OUTPUT_JSON = 3; const OUTPUT_XML = 4; const OUTPUT_YAML = 5; - const OUTPUT_HTML = 6; + const OUTPUT_UI = 6; const OUTPUT_PLAIN = 7; const OUTPUT_CSS = 8; + const OUTPUT_PREVIEW = 9; /** @@ -26,8 +28,9 @@ class OutputFactory { 'xml' => self::OUTPUT_XML, 'yaml' => self::OUTPUT_YAML, 'plain' => self::OUTPUT_PLAIN, - 'html' => self::OUTPUT_HTML, + 'html' => self::OUTPUT_UI, 'css' => self::OUTPUT_CSS, + 'preview' => self::OUTPUT_PREVIEW, ]; /** @@ -41,8 +44,8 @@ class OutputFactory { 'text/xml' => self::OUTPUT_XML, 'application/xml' => self::OUTPUT_XML, 'application/yaml' => self::OUTPUT_YAML, - 'application/xhtml+xml' => self::OUTPUT_HTML, - 'text/html' => self::OUTPUT_HTML, + 'application/xhtml+xml' => self::OUTPUT_UI, + 'text/html' => self::OUTPUT_UI, 'text/css' => self::OUTPUT_CSS, //'*/*' => self::OUTPUT_HTML, ]; @@ -69,10 +72,12 @@ class OutputFactory { return new XmlOutput(); case self::OUTPUT_YAML: return new YamlOutput(); - case self::OUTPUT_HTML: - return new HtmlOutput(); + case self::OUTPUT_UI: + return new UIOutput(); case self::OUTPUT_CSS: return new CssOutput(); + case self::OUTPUT_PREVIEW: + return new PreviewOutput(); case self::OUTPUT_PLAIN: default: return new PlainOutput(); @@ -88,7 +93,7 @@ class OutputFactory { */ private static function discoverOutputType() { - $reqOutput = strtolower(@$_REQUEST['output']); + $reqOutput = strtolower(@$_REQUEST[ RequestParams::PARAM_OUTPUT ]); // Try 1: Checking the 'output' request parameter. if ( $reqOutput ) { diff --git a/modules/cms/output/PreviewOutput.class.php b/modules/cms/output/PreviewOutput.class.php @@ -0,0 +1,26 @@ +<?php + +namespace cms\output; + +use cms\output\APIOutput; +use util\json\JSON; +use util\YAML; + +/** + * Preview rendering. + */ +class PreviewOutput extends APIOutput +{ + /** + * Renders the output directy from the action output. + */ + protected function renderOutput( $data ) + { + return $data['output']['value']; + } + + public function getContentType() + { + return 'text/css'; + } +} diff --git a/modules/cms/output/UIOutput.class.php b/modules/cms/output/UIOutput.class.php @@ -0,0 +1,166 @@ +<?php + +namespace cms\output; + +use BadMethodCallException; +use cms\action\RequestParams; +use cms\base\Language as L; +use cms\base\Startup;use cms\Dispatcher; +use cms\output\BaseOutput; +use Exception; +use template_engine\engine\TemplateRunner; +use util\Http; +use logger\Logger; +use LogicException; +use \util\exception\ObjectNotFoundException; +use util\exception\UIException; +use util\exception\SecurityException; +use template_engine\engine\TemplateEngine; +use util\text\TextMessage; + + +/** + * The HTML output is calling a template for the user interface. + */ +class UIOutput extends BaseOutput +{ + /** + * Preparing the client... + */ + protected function beforeAction($request) + { + // Sending the Content-Security-Policy. + self::setContentSecurityPolicy(); + + if ( @$_REQUEST['scope']=='openid' ) { + $request->redirectActionAndMethod('login','oidc'); + } + elseif (empty($request->action)) { + $request->redirectActionAndMethod('index','show' ); + } + + if ( $request->isAction ) + throw new \RuntimeException('The HTML output driver does not accept POST requests'); + + } + + + + protected function outputData($request, $data) + { + // The action is able to change its method and action name. + $subaction = $request->method; + $action = $request->action; + + + $this::outputTemplate($request,$action, $subaction, $data['output'] ); + } + + + /** + * Executes and outputs a HTML template. + * + * @param $request RequestParams + * @param $action string action + * @param $subaction string method + * @param $outputData array Output data + */ + private static function outputTemplate($request, $action, $subaction, $outputData) + { + $templateFile = Startup::MODULE_DIR . 'cms/ui/themes/default/html/views/' . $action.'/'.$subaction . '.php'; + + if ( DEVELOPMENT ) + header('X-OR-Template: '.$templateFile); + + $engine = new TemplateRunner(); + //$engine->request = $request; + $engine->executeTemplate( $templateFile, $outputData ); + } + + + /** + * Content-Security-Policy. + */ + private static function setContentSecurityPolicy() + { + // config is not loaded yet. Allow nothing... + header('Content-Security-Policy: default-src \'none\'' ); + + // This will be overwritten by the index action + } + + + /** + * @param $text + * @param $cause Exception the cause, or <code>null</code> if not available. + */ + protected function setError($text, $cause) + { + if ( !headers_sent() ) + // The following HTML contains inline CSS code, so we have to allow inline CSS. + header('Content-Security-Policy: style-src: inline; default: self'); + +?><!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"/> + <meta name="viewport" content="width=device-width, initial-scale=1"/> + <title><?php echo $text ?></title> + <style type="text/css"> + + header, main { + display: block + } + + body { + width: 100%; + height: 100%; + background-color: rgba(13,8,5,0.58); + color: white; + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; + line-height: 1.4; + font-size: 1.5em; + text-align: center; + } + + pre { + margin:10%; + width: 80%; + overflow: visible; + height: 40%; + color: silver; + text-align: left; + font-size: 0.6rem; + } + + h1 { + font-size: 2em; + } +</style> +</head> +<body> + +<header> + <h1><?php echo $text ?></h1> +</header> + +<main> + <p>Something went terribly wrong &#x1F61E;</p> + + <pre><?php // Display exceptions only in development mode, because they may contain sensitive internal information like passwords. + if ($cause && defined('DEVELOPMENT') && DEVELOPMENT ) { + echo $cause->__toString(); + } + ?></pre> +</main> + +</body> +</html><?php + + } + + public function getContentType() + { + return 'text/html'; + } +} diff --git a/modules/util/Html.class.php b/modules/util/Html.class.php @@ -74,4 +74,19 @@ class Html // Maybe the escaping should be controlled by a parameter. return './?'.implode('&', $urlParameterList); } + + + /** + * creates a relative url to the UI. + * + * @param string $action Aktion, die aufgerufen werden soll + * @param $id string Unteraktion, die innerhalb der Aktion aufgerufen werden soll + */ + public static function locationUrl($action, $id = '') + { + if (intval($id) == 0) + $id = ''; + + return './#/'.$action.'/'.$id; + } }