Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

mail obfuscation / hide mailto and address


Reply


7 Posts   2825 Views

Avatar
ravid

Community Member, 6 Posts

2 February 2010 at 5:09am

Edited: 02/02/2010 5:09am

Here is a solution for mail obfuscation (obfuscation occures while the page is saved, not while is it showed)

the page.php (mysite/code/page.php) or any instance of it:

class Page extends SiteTree {
   
   ...
   
   public function onBeforeWrite() {
      // obfuscate mailto-links
         // replace mailto-mailadresses within href attributes of links
         if (preg_match_all('/href="(mailto:.*?)"/', $this->Content, $matches)) {
            $searches = array();
            $replaces = array();
            for($i=0; $i<count($matches[0]); $i++) {
               $link = $matches[1][$i];
               $obfuscatedLink = "javascript:defuscateMailTo([";
               $first = true;
               for ($j=0; $j<strlen($link); $j++) {
                  $obfuscatedLink .= ($first ? '' : ', ') . (ord($link[$j]) + 111);
                  $first = false;
               }
               $obfuscatedLink .= ']);';
               array_push($searches, $link);
               array_push($replaces, $obfuscatedLink);
            }
            $this->Content = str_replace($searches, $replaces, $this->Content);
         }
         // replace mailadresses within the links themselfes
         if (preg_match_all('/<a [^>]+>([^@]+@[^<]+)<\/a>/', $this->Content, $matches)) {
            $searches = array();
            $replaces = array();
            for($i=0; $i<count($matches[0]); $i++) {
               $link = $matches[1][$i];
               // this would convert mailadresses into text
                  /*$obfuscatedLink = "Mail";*/
               // this would replace the "@" with " at " and remove the domain (for example ".com")
                  /*$obfuscatedLink = substr($link, 0, strpos($link, "."));
                  $obfuscatedLink = str_replace("@", " at ", $obfuscatedLink);*/
               // this would replace the "@" with " at " and the "." with " . "
                  $obfuscatedLink = str_replace("@", " at ", $link);
                  $obfuscatedLink = str_replace(".", " . ", $obfuscatedLink);
               array_push($searches, $link);
               array_push($replaces, $obfuscatedLink);
            }
            $this->Content = str_replace($searches, $replaces, $this->Content);
         }
      parent::onBeforeWrite();
   }
   
}

class Page_Controller extends ContentController {
   
   public function init() {
      parent::init();
      Requirements::javascript("mysite/javascript/rosa.js");
   }

}

the javascript:

// defuscates obfuscated mailto adresses
function defuscateMailTo (mailArr) {
   var mail = "";
   for (var i=0; i<mailArr.length; i++) mail += String.fromCharCode(mailArr - 111);
   window.location.href = mail;
}

Attached Files
Avatar
purplespider

Community Member, 83 Posts

8 July 2010 at 1:13am

This works great! Thank you very much!

Avatar
Graphicator

Community Member, 62 Posts

1 October 2011 at 2:52pm

Is there any update to the second conditional statement?

// replace mailadresses within the links themselfes

Avatar
BenWu

Community Member, 92 Posts

31 October 2012 at 12:11am

Edited: 31/10/2012 12:11am

Lovely. The posted javascript should be

for (var i=0; i<mailArr.length; i++){
mail += String.fromCharCode(mailArr [ i ] - 111);

}

Avatar
purplespider

Community Member, 83 Posts

17 September 2013 at 9:33am

I've been using this script on all my sites for a few years now, but I've been getting reports from clients using SilverStripe 3.0, that sometimes when they make a change to a page including an e-mail address, chunks of text are being wiped out upon save. Any idea why this could be? Does it need updating for SilverStripe 3.0?

Avatar
stumck

Community Member, 5 Posts

15 December 2014 at 10:58am

Hi there, to get this working. You paste the code into the mysite/page.php. Which is linked to the Javascript file.
Is there anything else to do? (with the templates side of things.)
I have had trouble getting things to work.
Cheers Stuart

Avatar
purplespider

Community Member, 83 Posts

15 December 2014 at 10:26pm

I've since started using this module: https://github.com/axllent/silverstripe-email-obfuscator which I've found to work much more reliably.

James