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;
}