Why is UDT not working? Topic is solved

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
rooon
Forum Members
Forum Members
Posts: 113
Joined: Mon Dec 05, 2011 4:40 pm
Location: Nederland

Why is UDT not working?

Post 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
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1921
Joined: Mon Jan 29, 2007 4:47 pm

Re: Why is UDT not working?

Post 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
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
rooon
Forum Members
Forum Members
Posts: 113
Joined: Mon Dec 05, 2011 4:40 pm
Location: Nederland

Re: Why is UDT not working?

Post by rooon »

Thank you for your helpful answer :)
I'll try the event with &$params['content'];
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1921
Joined: Mon Jan 29, 2007 4:47 pm

Re: Why is UDT not working?

Post 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)
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
rooon
Forum Members
Forum Members
Posts: 113
Joined: Mon Dec 05, 2011 4:40 pm
Location: Nederland

Re: Why is UDT not working?

Post by rooon »

Thanks. I noticed the & by reference. I'll use return instead of echo :)
rooon
Forum Members
Forum Members
Posts: 113
Joined: Mon Dec 05, 2011 4:40 pm
Location: Nederland

Re: Why is UDT not working?

Post 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';
Post Reply

Return to “CMSMS Core”