From ed68f289ccbb2f0026debd25423f2f8175ac56f3 Mon Sep 17 00:00:00 2001 From: Clinton Graham Date: Tue, 2 May 2023 16:27:42 -0400 Subject: [PATCH 1/2] pkp#28: provide backend header support Conflicts: CustomHeaderSettingsForm.php --- CustomHeaderPlugin.php | 1 + CustomHeaderSettingsForm.php | 21 ++++++++++++++++++++- locale/en/locale.po | 6 ++++++ templates/settingsForm.tpl | 19 ++++++++----------- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CustomHeaderPlugin.php b/CustomHeaderPlugin.php index 45d493f..91a859c 100644 --- a/CustomHeaderPlugin.php +++ b/CustomHeaderPlugin.php @@ -115,6 +115,7 @@ function displayTemplateHook($hookName, $params) { $request = Application::get()->getRequest(); $context = $request->getContext(); $templateMgr->addHeader('custom', $this->getSetting($context?$context->getId():CONTEXT_ID_NONE, 'content')); + $templateMgr->addHeader('custombackend', $this->getSetting($context?$context->getId():CONTEXT_ID_NONE, 'backendContent'), ['contexts' => ['backend']]); } return false; } diff --git a/CustomHeaderSettingsForm.php b/CustomHeaderSettingsForm.php index 198bc37..48fa244 100644 --- a/CustomHeaderSettingsForm.php +++ b/CustomHeaderSettingsForm.php @@ -36,6 +36,7 @@ function __construct($plugin, $contextId) { parent::__construct($plugin->getTemplateResource('settingsForm.tpl')); + $this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'backendContent', FORM_VALIDATOR_OPTIONAL_VALUE, 'plugins.generic.customHeader.backendContent.error', function ($backendContent) { return $this->validateWellFormed($backendContent); })); $this->addCheck(new \PKP\form\validation\FormValidatorPost($this)); $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); } @@ -46,6 +47,7 @@ function __construct($plugin, $contextId) { function initData() { $this->_data = array( 'content' => $this->_plugin->getSetting($this->_contextId, 'content'), + 'backendContent' => $this->_plugin->getSetting($this->_contextId, 'backendContent'), 'footerContent' => $this->_plugin->getSetting($this->_contextId, 'footerContent') ); } @@ -54,7 +56,7 @@ function initData() { * Assign form data to user-submitted data. */ function readInputData() { - $this->readUserVars(array('content', 'footerContent')); + $this->readUserVars(array('content', 'backendContent', 'footerContent')); } /** @@ -75,8 +77,25 @@ function execute(...$functionArgs) { $request = Application::get()->getRequest(); $this->_plugin->updateSetting($this->_contextId, 'content', $this->getData('content'), 'string'); + $this->_plugin->updateSetting($this->_contextId, 'backendContent', $this->getData('backendContent'), 'string'); $this->_plugin->updateSetting($this->_contextId, 'footerContent', $this->getData('footerContent'), 'string'); $notificationManager = new NotificationManager(); $notificationManager->createTrivialNotification($request->getUser()->getId(), NOTIFICATION_TYPE_SUCCESS); } + + /** + * Validate that the input is well-formed XML + * We want to avoid breaking the whole HTML page with an unclosed HTML attribute quote or tag + * @param $input string + * @return boolean + */ + function validateWellFormed($input) { + $libxml_errors_setting = libxml_use_internal_errors(); + libxml_use_internal_errors(true); + libxml_clear_errors(); + $xml = simplexml_load_string($input); + $isWellFormed = count(libxml_get_errors())==0; + libxml_use_internal_errors($libxml_errors_setting); + return $isWellFormed; + } } diff --git a/locale/en/locale.po b/locale/en/locale.po index d3f3cbd..c762243 100644 --- a/locale/en/locale.po +++ b/locale/en/locale.po @@ -25,3 +25,9 @@ msgstr "Header Content" msgid "plugins.generic.customHeader.footerContent" msgstr "Footer Content" + +msgid "plugins.generic.customHeader.backendContent" +msgstr "Editorial Backend Header Content" + +msgid "plugins.generic.customHeader.backendContent.error" +msgstr "Editorial Backend Header Content must be valid XHTML." diff --git a/templates/settingsForm.tpl b/templates/settingsForm.tpl index f74f477..572bb62 100644 --- a/templates/settingsForm.tpl +++ b/templates/settingsForm.tpl @@ -8,12 +8,6 @@ * Plugin settings * *} -
-
{translate key="plugins.generic.customHeader.manager.settings.description"}
- -
- -
{csrf} +

{translate key="plugins.generic.customHeader.manager.settings.description"}

+ {include file="controllers/notification/inPlaceNotification.tpl" notificationId="customHeaderFormNotification"} {fbvFormArea id="customHeaderSettingsFormArea"} {fbvFormSection for="headerContent" title="plugins.generic.customHeader.content"} {fbvElement type="textarea" name="content" id="headerContent" value=$content height=$fbvStyles.height.TALL} {/fbvFormSection} - {/fbvFormArea} - - {fbvFormArea id="customHeaderSettingsFormArea"} {fbvFormSection for="footerContent" title="plugins.generic.customHeader.footerContent"} {fbvElement type="textarea" name="footerContent" id="footerContent" value=$footerContent height=$fbvStyles.height.TALL} {/fbvFormSection} {/fbvFormArea} + {fbvFormArea id="customHeaderBackendSettingsFormArea"} + {fbvFormSection for="backendContent" title="plugins.generic.customHeader.backendContent"} + {fbvElement type="textarea" name="backendContent" id="backendContent" value=$backendContent height=$fbvStyles.height.TALL} + {/fbvFormSection} + {/fbvFormArea} + {fbvFormButtons}
-

{translate key="common.requiredField"}

-
From e1cc5f323c9f6599852b3008bf39ec130abbfe62 Mon Sep 17 00:00:00 2001 From: Clinton Graham Date: Tue, 19 Sep 2023 15:56:03 -0400 Subject: [PATCH 2/2] #28: Change from XML to HTML well-formedness --- CustomHeaderSettingsForm.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CustomHeaderSettingsForm.php b/CustomHeaderSettingsForm.php index 48fa244..1a0bba1 100644 --- a/CustomHeaderSettingsForm.php +++ b/CustomHeaderSettingsForm.php @@ -93,7 +93,8 @@ function validateWellFormed($input) { $libxml_errors_setting = libxml_use_internal_errors(); libxml_use_internal_errors(true); libxml_clear_errors(); - $xml = simplexml_load_string($input); + $dom = new DOMDocument(); + $dom->loadHTML($input); $isWellFormed = count(libxml_get_errors())==0; libxml_use_internal_errors($libxml_errors_setting); return $isWellFormed;