Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » mail obfuscation / hide mailto and address

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2593 Views
  • ravid
    Avatar
    Community Member
    6 Posts

    mail obfuscation / hide mailto and address Link to this post

    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
  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: mail obfuscation / hide mailto and address Link to this post

    This works great! Thank you very much!

  • Graphicator
    Avatar
    Community Member
    62 Posts

    Re: mail obfuscation / hide mailto and address Link to this post

    Is there any update to the second conditional statement?

    // replace mailadresses within the links themselfes

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: mail obfuscation / hide mailto and address Link to this post

    Lovely. The posted javascript should be

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

    }

  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: mail obfuscation / hide mailto and address Link to this post

    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?

    2593 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.