First day playing with MODx and very impressed.
Here's a little plugin to obfuscate email links with Javascript.
It basically scours the page for mailto: email links, and if it finds one it obfuscates it before sending the result to the browser. Yes, this is kind of a brute force method, but for our clients we find it's MUCH easier to just let them enter email addresses as normal and to obfuscate them on-the-fly than making them remember to enter one via some sort of snippet code. I suppose this could easily be re-written as a Snippet to let you choose which bits of content you wanted to scour for obfuscation, but as a plug-in it's set it and forget it for the entire site.
To install:
1. Create a new Plugin named whatever you want (i.e. Email Obfuscator).
2. Under System Events, check the OnWebPagePrerender event.
3. Save and enjoy.
Assuming everything works right, all the regular email links on every page on your site should be obfuscated with Javascript. No guarantees as to the spambot-proofness of the actual obfuscation, and please note that it does NOT obfuscate email addresses that are written outside a mailto: link.
And with all that said, here's the plugin:
function replaceEntities($str) {
$str=html_entity_decode($str);
for ($i = 0 ; $i < strlen($str) ; $i++) {
$strreplaced = $strreplaced . "&#" . ord($str{$i}) . ";";
}
return $strreplaced;
}
function emailaddress($matches) {
$strNewAddress = replaceEntities($matches[1]);
$strText = replaceEntities($matches[2]);
$arrEmail = explode("@",$strNewAddress);
$strTag = "<script language='Javascript' type='text/javascript'>" . "\r";
$strTag = $strTag . "<!--" . "\r";
$strTag = $strTag . "document.write('<a href=\"mai');" . "\r";
$strTag = $strTag . "document.write('lto');" . "\r";
$strTag = $strTag . "document.write(':" . $arrEmail[0] . "');" . "\r";
$strTag = $strTag . "document.write('@');" . "\r";
$strTag = $strTag . "document.write('" . $arrEmail[1] . "\">');" . "\r";
$strTag = $strTag . "document.write('" . $strText . "<\/a>');" . "\r";
$strTag = $strTag . "// -->" . "\r";
$strTag = $strTag . "</script><noscript>" . $arrEmail[0] . " at " . "\r";
$strTag = $strTag . str_replace("."," dot ",$arrEmail[1]) . "</noscript>";
return $strTag;
}
$modx->documentOutput=preg_replace_callback("#<a[^>]*mailto:([^'\" ]*)['\" ]>([^<]*)</a>#i","emailaddress",$modx->documentOutput);