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

Moodle new Kekule.js Loader Filter problem #269

Open
registerme1963 opened this issue Feb 19, 2022 · 10 comments
Open

Moodle new Kekule.js Loader Filter problem #269

registerme1963 opened this issue Feb 19, 2022 · 10 comments

Comments

@registerme1963
Copy link

As moodle admin I enabled the filter, and disabled the Kekule.js injector block. The problem comes when I tried to add structure in to forum posts. When I save a forum post after adding the structure from chemeditor, it strips <img tag and kekule-widget text. this seems to happen only in forum post. This did not change even after adding hidden injector block in the course.

As exams are scheduled from today evening onwards, I need some immediate solution.
Thanks for your cooperation
regards

@registerme1963 registerme1963 changed the title Moodle new Kekule.js Loader problem Moodle new Kekule.js Loader Filter problem Feb 19, 2022
@partridgejiang
Copy link
Owner

Hi @registerme1963, the forum mod in Moodle applies HTML purifier for the post by default, which erases all img tags inserted with Kekule plugin. For a quick (and dirty) workaround, you may modify the a piece of PHP code of Moodle to disable to purifier.

In /lib/weblib.php, about line 1709, you may find function clean_text. Inside it, modify the following code:

if (is_purify_html_necessary($text)) {
   $text = purify_html($text, $options);
}

to:

if (is_purify_html_necessary($text)) {
   // $text = purify_html($text, $options);
}

So that the purifier will be bypassed. Do not forget to change back the code after all chem objects being inserted to forum.

@registerme1963
Copy link
Author

Thanks it works, but if I change back, all chem objects disappear, it seems that this change needs to be permanent. I will go with this work around till I complete my examination, but this is dangerous for moodle. Need an alternative way.
Thanks for fast response, really enjoying your plugin in moodle

@partridgejiang
Copy link
Owner

partridgejiang commented Feb 19, 2022

After checking the Moodle documents for a while, the better and official solution is to turn on the trusted Content permission for certain user account, afterwards, the message posted by this account will not be purified anymore. Please refer to: https://docs.moodle.org/27/en/Capabilities/moodle/site:trustcontent .

@registerme1963
Copy link
Author

I saw that, but it is not possible for moodle admin to enable one by one on individual requests. Why not have some regex pattern like "src="(data:image\/[^;]+;base64[^"]+)" along with kekule related tags and add them to /lib/weblib.php for the time being?

@registerme1963
Copy link
Author

registerme1963 commented Feb 19, 2022

something like /src="(data:image\/[^;]+;base64[^*]+Kekule[^*]+K-Transparent-Background"\>)/i and not to filter this pattern in /lib/weblib.php

The image from kekule editor gives the following in moodle:
<img src="data:image/png;base64, ......" style="width .... data-kekule-widget="Kekule.ChemWidget.Viewer" data-render-type="2" data-chem-obj="{&quot;id .....class=" KekuleChemObjViewer K-Transparent-Background">

@partridgejiang
Copy link
Owner

The trusted Content permission seems to be appliable to user groups, not need to be set to user one by one. And the /lib/weblib.php is within the core modules of Moodle, it is better not to change it from a third-party plugin, :).

@registerme1963
Copy link
Author

I understand, I'm the admin and also a course teacher, but kekule structure gets stripped for me. Presently I commented out the purify_html line to complete the examination.
Please suggest some workaround with kekulejs filter settings
Thanks for your efforts

@registerme1963
Copy link
Author

registerme1963 commented Feb 21, 2022

I tried to add student/authenticated roles for trusted content, but still it strips off the kekule structure. So went back and commented out the purify_html line. If this is case, then students cannot use this atto plugin in moodle freely. Do you have any alternate solution without changing the moodle core code

@partridgejiang
Copy link
Owner

After checking the HTML purifier code in Moodle for a while, I myself found it was hard coded and unable to be customizable (I'll be very happy if my conclusion is wrong). So you may really have to modify lib/weblib.php directly, :(. In function ````purify_html`` (about line 1776 of weblib.php), two places need to be changed. At about line 1832, $config->set('URI.AllowedSchemes'...) is used to set the allowed URI protocals. In the end of the last array parameter, add a line to enable to the ```data``` protocal:

$config->set('URI.AllowedSchemes', array(
  'http' => true,
  'https' => true,
  // ...
  'mailto' => true,
  'data' => true
));

Down to about line 1862, inside the block of if ($def = $config->maybeGetRawHTMLDefinition()), add the following lines to allow extra attributes of img element exported by Kekule:

$def->addAttribute('img', 'data-chem-obj', 'Text');
$def->addAttribute('img', 'data-kekule-widget', 'Text');
$def->addAttribute('img', 'data-render-type', 'Text');
$def->addAttribute('img', 'data-draw-options', 'Text');
$def->addAttribute('img', 'data-predefined-setting', 'Text');
$def->addAttribute('img', 'data-auto-size', 'Text');
$def->addAttribute('img', 'data-autofit', 'Text');
$def->addAttribute('img', 'data-background-color', 'Text');
$def->addAttribute('img', 'style', 'Text');
$def->addAttribute('img', 'class', 'Text');
$def->addAttribute('img', 'width', 'Text');
$def->addAttribute('img', 'height', 'Text');

With all those modifications, do not forget to purge the cache of Moodle. After that, the plugin can work in forum properly with HTML purifier enabled.

@registerme1963
Copy link
Author

This works throughout the site, thank you. I also submitted your suggestion to Moodle forum to enable custom attributes setting to disable html purifier without editing the core code. I will submit the response here when I get something
Thanks for your kind help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants