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

5 Posts   2687 Views

Avatar
ravid

2 February 2010 at 5:09am (Last edited: 2 February 2010 5:09am), Community Member, 6 Posts

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

8 July 2010 at 1:13am Community Member, 82 Posts

This works great! Thank you very much!

Avatar
Graphicator

1 October 2011 at 2:52pm Community Member, 62 Posts

Is there any update to the second conditional statement?

// replace mailadresses within the links themselfes

Avatar
BenWu

31 October 2012 at 12:11am (Last edited: 31 October 2012 12:11am), Community Member, 90 Posts

Lovely. The posted javascript should be

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

}

Avatar
purplespider

17 September 2013 at 9:33am Community Member, 82 Posts

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?