We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6208
    • 44 Posts
    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);
    
      • 5683
      • 96 Posts
      I had to replace the occurrences of "documentContent" with "documentOutput" huh

      After that, the plugin worked like a charm. Very cool grin
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        A plugin on the first day? Ooooh! Always very happy to see a professional join the community! (your site is really nice, too!)
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 6726
          • 7,075 Posts
          Yeah, nice one, and on your first day pretty amazing...
          Amazing we have two antibot plugins in the same week tongue

          And Susan, thanks for attracting our attention on cyberk’s website : indeed very very nice ! Waow... And also : I very much like your approach of webdesign, first class grin ...glad to have you there !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 6208
            • 44 Posts
            Thanks, all!

            Can someone explain the difference between documentContent and documentOutput? Both seem to work equally well for me here?
              • 22303 MODX Staff
              • 10,725 Posts
              $modx->documentContent is used internally by the parser to store the output of a document, and can be retrieved dynamically from the DB or from the cache, depending on certain settings. However, the final portion of the parsing process, which is responsible for parsing uncached snippet tags and replacing any remaining placeholders in the content, uses $modx->documentOutput to collect the final bits of uncached page data and merge it into the $modx->documentContent. Then it checks to see if the $modx->documentContent was generated from the database (vs. retrieved from cache), and if so, it caches the $modx->documentContent while still outputting the $modx->documentOutput.

              In other words, $modx->documentContent is the potentially cacheable content for a page, while $modx->documentOutput represents the final output to be delivered following final processing that occurs on all pages, regardless of the cacheability.
                • 6208
                • 44 Posts
                Thanks, Jason!

                I updated the code above.
                  • 15914
                  • 145 Posts
                  This plugin works a treat. Thanks a lot. This should be added to the REPOSITORY. This is much easier to use than my little snippet.
                    Kunal Kapoor
                    Technical Director
                    Limesharp Internet Limited - Effective, Functional, Standards Compliant

                    IRC Nick: KingKoopa
                    Skype ID: KingKoopa16
                    • 32241
                    • 1,495 Posts
                    Thanks for bringing this up. Link added. wink Thanks for the great plugin as well. I’m looking forward to all your new invention with MODx grin
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media
                      • 7131
                      • 83 Posts
                      The plugin does not allow rendering of special characters. For example, <a href="mailto:[email protected]">événement</a> will be shown as a link &eacute;v&eacute;nement.
                        at the edge of dawn,
                        you ask yourself if your shadow did follow you down
                        all night long