Skip to content

Instantly share code, notes, and snippets.

@rr-
Last active December 26, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rr-/7179962 to your computer and use it in GitHub Desktop.
Save rr-/7179962 to your computer and use it in GitHub Desktop.
E-mail anti-spam protection using Javascript/PHP obfuscation.
<?php
class EmailObfuscator
{
public static function embedJavascript($x, $justPlainText = false)
{
$seed = mt_rand(0, 10000);
$x = str_split($x);
//permutation
$a = 0;
$b = 1;
for ($i = 0; $i < count($x); $i ++)
{
$c = $a + $b;
$a = $b;
$b = $c;
$j = ($seed + $a) % count($x);
list($x[$i], $x[$j]) = [$x[$j], $x[$i]];
}
//substitution
for ($i = 0; $i < count($x); $i ++)
$x[$i] = ord($x[$i]) ^ (($seed + $i) & 0xff);
$x = base64_encode(join('', array_map('chr', $x)));
$id = 'm' . substr(md5(microtime(true)), 0, 8);
$html = '<span class="mail" id="' . $id . '"></span>';
$html .= '<script type="text/javascript">';
$html .= 'var seed=' . $seed . ',x=window.atob(\'' . $x . '\'),y=Array(l=x.length),s=Math.sqrt(5),i;';
$html .= 'for(i=l;i;)y[--i]=String.fromCharCode(x.charCodeAt(i)^(seed+i)&255);';
$html .= 'for(i=l;i;)';
$html .= 'x=Math.round(Math.pow((1+s)/2,i)/s+l+seed)%l,';
$html .= 'y[--i]=[y[x],y[x]=y[i]][0];';
if ($justPlainText)
$html .= 'document.getElementById(\'' . $id . '\').innerHTML=y.join(\'\');';
else
$html .= 'document.getElementById(\'' . $id . '\').innerHTML=\'<a href="mailto:\'+(y=y.join(\'\'))+\'">\'+y+\'</a>\';';
$html .= '</script>';
$html .= '<noscript class="mail">';
$html .= 'need JS to decode e-mail';
$html .= '</noscript>';
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment