protectemail

I wanted a way to encode emails so spam bots couldn’t steal emails on my Conrete5 site. At the same time I didn’t want the client to have to do anything different than he or she normally would to add an email through the WYSIWYG editor. I searched the forums and found a few solutions, however, they all had bugs. Worst of all, no one ever told anyone how or where to place the code! So here is how you can fight spam in C5:

First, go into and open concrete/blocks/content/controller.php

Then find this block of code:

function getContent() {
	$content = $this->translateFrom($this->content);
	return $content;				
}

Replace that code with the code below:

function modifyLinks($groups) {
	$mid  = round(strlen($groups[2])/2);
	$mid2  = round(strlen($groups[4])/2);
	return $ret  = '<a '.$groups[1].' href="" onclick="location.href=\'mailto:' . substr($groups[2],0,$mid) . '\'+\'' . substr($groups[2],$mid) . '\'" '.$groups[3].'>' . substr($groups[4],0,$mid) . '<span style="display:none;">a</span>' . substr($groups[4],$mid) . '</a>';     
}
 
function modifyAddress($groups) {
	$mid = floor(strlen($groups[0])/2);
	return substr($groups[0],0,$mid) . '<span style="display:none;">antibot</span>' . substr($groups[0],$mid);     
} 
 
 
function getContent() {
	$content = $this->translateFrom($this->content);
	$content = preg_replace_callback('#\<a(.*)href="mailto:(.*)"(.*)>(.*)</a>#Ui',array($this,'modifyLinks'),$content);
	$content = preg_replace_callback('(\w+@[a-zA-Z_]+?\.[a-zA-Z]{})',array($this,'modifyAddress'),$content);
	return $content;                
}

Now go ahead and add an email using the normal content block and add an email with the WYSIWYG editor. Save it and save the page. Then view the source. You should see something sort of like this in the code now:

<a   href="" onclick="location.href='mailto:myemail@'+'mydomain.com'" >My Em<span style="display:none;">a</span>il</a>

Let me know if any of you are having any issues and what version of C5 you are using if so.

[Post to Twitter]   [Post to Delicious]   [Post to Digg]   [Post to Reddit]   [Post to StumbleUpon]