PHP and JS Email Obfuscator
I have been using some great email obfuscation code created by Ross Killen ,Tim Williams and Andrew Moulden. And like Andrew’s generator that allows for “link text”, I needed a PHP solution for sites I’m developing. So here’s my twist:
function munge($address,$link_text) {
$address = strtolower($address);
$coded = "";
$unmixedkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.@-_";
$inprogresskey = $unmixedkey;
$mixedkey="";
$unshuffled = strlen($unmixedkey);
for ($i = 1; $i <= strlen($unmixedkey); $i++) {
// set i = 1 because i = 0 was throwing an offset error in PHP
$ranpos = rand(0,$unshuffled-1);
$nextchar = $inprogresskey{$ranpos};
$mixedkey .= $nextchar;
$before = substr($inprogresskey,0,$ranpos);
$after = substr($inprogresskey,$ranpos+1,$unshuffled-($ranpos+1));
$inprogresskey = $before.''.$after;
$unshuffled -= 1;
}
$cipher = $mixedkey;
$shift = strlen($address);
$txt = "<script type=\"text/javascript\" language=\"javascript\">\n" .
"<!-"."-\n" .
"// Email obfuscator script 2.1 by Tim Williams, University of Arizona\n".
"// Random encryption key feature by Andrew Moulden, Site Engineering Ltd\n".
"// PHP version coded by Ross Killen, Celtic Productions Ltd\n".
"// This code is freeware provided these six comment lines remain intact\n".
"// A wizard to generate this code is at http://www.jottings.com/obfuscator/\n".
"// The PHP code may be obtained from http://www.celticproductions.net/\n\n";
for ($j=0; $j<strlen($address); $j++) {
if (strpos($cipher,$address{$j}) == -1 ) {
$chr = $address{$j};
$coded .= $address{$j};
} else {
$chr = (strpos($cipher,$address{$j}) + $shift) %
strlen($cipher);
$coded .= $cipher{$chr};
}
}
if($link_text):
$link_text_block = "document.write(\"<a href='mailto:\"+link+\"'>" . $link_text . "</a>\")\n";
else:
$link_text_block = "document.write(\"<a href='mailto:\"+link+\"'>\"+link+\"</a>\")\n";
endif;
$txt .= "\ncoded = \"" . $coded . "\"\n" .
" key = \"".$cipher."\"\n".
" shift=coded.length\n".
" link=\"\"\n".
" for (i=0; i<coded.length; i++) {\n" .
" if (key.indexOf(coded.charAt(i))==-1) {\n" .
" ltr = coded.charAt(i)\n" .
" link += (ltr)\n" .
" }\n" .
" else { \n".
" ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length\n".
" link += (key.charAt(ltr))\n".
" }\n".
" }\n".
// Original link markup
//"document.write(\"<a href='mailto:\"+link+\"'>\"+link+\"</a>\")\n" .
// Link markup from above
$link_text_block .
"\n".
"//-"."->\n" .
"<" . "/script><noscript>Please turn on javascript to email me." .
"<"."/noscript>";
return $txt;
}
Published on October 16, 2013