5099 Posts in 1519 Topics by 1116 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2035 Views |
-
mail obfuscation / hide mailto and address

2 February 2010 at 5:09am Last edited: 2 February 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;
} -
Re: mail obfuscation / hide mailto and address

8 July 2010 at 1:13am
This works great! Thank you very much!
-
Re: mail obfuscation / hide mailto and address

1 October 2011 at 2:52pm
Is there any update to the second conditional statement?
// replace mailadresses within the links themselfes
-
Re: mail obfuscation / hide mailto and address

31 October 2012 at 12:11am Last edited: 31 October 2012 12:11am
Lovely. The posted javascript should be
for (var i=0; i<mailArr.length; i++){
mail += String.fromCharCode(mailArr [ i ] - 111);}
| 2035 Views | ||
|
Page:
1
|
Go to Top |



