Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkp#28: provide backend header support [3.3] #29

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CustomHeaderPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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;
}
Expand Down
22 changes: 21 additions & 1 deletion CustomHeaderSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function __construct($plugin, $contextId) {

parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));

$this->addCheck(new FormValidatorCustom($this, 'backendContent', FORM_VALIDATOR_OPTIONAL_VALUE, 'plugins.generic.customHeader.backendContent.error', array(&$this, 'validateWellFormed')));

$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
Expand All @@ -44,6 +46,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')
);
}
Expand All @@ -52,7 +55,7 @@ function initData() {
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars(array('content', 'footerContent'));
$this->readUserVars(array('content', 'backendContent', 'footerContent'));
}

/**
Expand All @@ -73,8 +76,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought -- is this going to be overly restrictive? It's requiring well-formed XML, but the field should be checking for well-formed HTML. I think the DOM tools might provide an alternative:

https://www.php.net/manual/en/domdocument.loadhtml.php

The idea would be:

  1. Use the same libxml_use_internal_errors approach and return value to determine whether a fatal parsing error occurred
  2. If it's flexible enough to allow parsing of documents that we don't want to permit, maybe re-serialize the HTML before storing in the setting to resolve that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. It's unlikely that someone would want to legitimately throw an unclosed li in the head, but I could see someone wanting to do an old-style meta tag perhaps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctgraham, unfortunately it looks like this won't work well. See:

#33

I'll follow up on that issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps and additional checkbox or a second confirmation "submit" which bypasses the validation? A "I solemnly swear I am up to no good" prompt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctgraham, that sounds OK to me (and I'm not sure we'll find a better solution). I did find an OK-looking work-around that gets the validator to pass without breaking the JS, which I described here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"I solemnly swear I am up to no good" 🧙‍♂️ totally yes! After all who doesn't know that "with great power comes great responsibility"?

$isWellFormed = count(libxml_get_errors())==0;
libxml_use_internal_errors($libxml_errors_setting);
return $isWellFormed;
}
}
6 changes: 6 additions & 0 deletions locale/en_US/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -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."
19 changes: 8 additions & 11 deletions templates/settingsForm.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
* Plugin settings
*
*}
<div id="customHeaderSettings">
<div id="description">{translate key="plugins.generic.customHeader.manager.settings.description"}</div>

<div class="separator"></div>

<br />

<script>
$(function() {ldelim}
Expand All @@ -23,21 +17,24 @@
</script>
<form class="pkp_form" id="customHeaderSettingsForm" method="post" action="{url router=$smarty.const.ROUTE_COMPONENT op="manage" category="generic" plugin=$pluginName verb="settings" save=true}">
{csrf}
<p id="description">{translate key="plugins.generic.customHeader.manager.settings.description"}</p>
{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}
</form>

<p><span class="formRequired">{translate key="common.requiredField"}</span></p>
</div>