Page 1 of 1

Why is UDT not working?

Posted: Mon Jul 04, 2022 5:48 pm
by rooon
Hi all,

I'm trying to remove mailto links from the {content}, but keep the emailaddress.
Second, the @domain.tld part of each emailaddress must be a base64 string.

Example, replace
this: <a href="mailto:info@example.com">....</a>
into: info<script>eml('QGV4YW1wbGUuY29t')</__script>

The php below does work in a separate test.php file.
I'm trying to do this in an UDT but it doesn't work.

Someone can help me out here?

UDT "email_base64"

Code: Select all

$inputText = get_parameter_value($params,'text');

// get "name" part and "@domain.tld" part from email
$regex = '/<a[^(href)]+href=\"mailto:([^@]+)([^\"]+)\">[^(<\/a>)]+<\/a>/i';

// preg_replace all matches
$newText = preg_replace_callback ($regex, function ($matches) {
   return $matches[1] . "<__script__>eml('" . base64_encode($matches[2]) . "')</__script>";
}, $inputText);

echo $newText;
TEMPLATE

Code: Select all

{process_pagedata}<!doctype html>
<__html>
<head>
<title>Test</title>
<__script__>{literal}
var eml = function (s) {
   document.write(window.atob(s));
}
</__script>{/literal}
</head>
</__body>
{email_base64 text="{content}"}
<__body>
</__html>
PAGE

Code: Select all

Replace a mailto link <a href="mailto:info@example.com">this link</a> and show part of email in base64

Re: Why is UDT not working?

Posted: Mon Jul 04, 2022 8:05 pm
by Jo Morg
A couple of things:
  • Although what you aim to do that way is possible, it is quite expensive in terms of server time; parsing any content (potentially large content) of a page via a UDT on every request is not a good practice. A better way to go about this would be via events, where you could use the UDT to handle a Core ContentPreCompile attaching the UDT to it. This would run the UDT once before Smarty caching, not every time the page is requested.
  • get_parameter_value($params,'text'); is not meant to be used that way, is a specific utility function for the backend admin pages. Inside the UDT's the approach would be $params['text']. The same mechanism is used with the Events handling and all of the Core events are documented. In any case to grab the contents passed by the event you'll have to use $inputText = &$params['content'];
HTH

Re: Why is UDT not working?

Posted: Mon Jul 04, 2022 8:19 pm
by rooon
Thank you for your helpful answer :)
I'll try the event with &$params['content'];

Re: Why is UDT not working?

Posted: Mon Jul 04, 2022 8:27 pm
by Jo Morg
Oh and BTW,
the UDT shouldn't echo in this case: either return the resulting value or just act on the content itself since the parameter is passed by reference (hence my & in the example above although you'd have to keep the result on the referenced variable for it to be effective)

Re: Why is UDT not working?

Posted: Mon Jul 04, 2022 8:42 pm
by rooon
Thanks. I noticed the & by reference. I'll use return instead of echo :)

Re: Why is UDT not working?

Posted: Tue Jul 05, 2022 12:05 pm
by rooon
Great, I'm almost therre.

Event Core ContentPreCompile runs UDT "email_base64" with this code in the UDT

Code: Select all

$inputText = &$params['content'];

// get "name" part and "@domain.tld" part from email
$regex = '/<a[^(href)]+href=\"mailto:([^@]+)([^\"]+)\">[^(<\/a>)]+<\/a>/i';

$inputText = preg_replace_callback ($regex, function ($matches) {
  return $matches[1] . "<__script__>eml('" . base64_encode($matches[2]) . "')</__script>";
}, $inputText);

return $inputText;
For example, email addresses like support@example.com and phone@example.nl work fine.

But <a href="mailto:info@example.nl">Contact Us</a> doesn't work.
Possibly a bug in the regex?

Do you have an idea?


edit 1 *** in regexr.com
<a href="mailto:info@exampe.nl">Cont</a> does work
<a href="mailto:info@exampe.nl">Conta</a> does not work

edit 2 *** for now, this works at the end of regex : [^(<\/)]+<\/a>

Code: Select all

$regex = '/<a[^(href)]+href=\"mailto:([^@]+)([^\"]+)\">[^(<\/)]+<\/a>/i';