<?php
/* Output mailto: links using nontrival javascript to thwart attempts by
* spammers to retrieve email address from web pages.
*
* Written by: Chris Studholme
* Copyright: GPL (http://www.fsf.org/copyleft/gpl.html)
* $Id: antispam.php,v 1.2 2003/12/21 00:26:13 cstudhol Exp $
*/
/* Returns the equivalent of '<a href="$url">$text</a>' but using javascript
* that the client must execute to recreate the link. If the client does not
* support javascript, they will only see '$text'.
*/
function antispam_link($url,$text) {
// encode entire tag
$tag = "<a href=\"".$url."\">";
// convert string to array of ordered numbers between 1 and 2 billion
$tag_a = array();
$max_inc = 1000000000 / 256 / strlen($tag);
$prefix = 1000000000;
for ($i=0; $i<strlen($tag); ++$i) {
$prefix += 256 * mt_rand(1,$max_inc);
$tag_a[] = $prefix + ord($tag[$i]);
}
// permute the array
for ($i=0; $i<count($tag_a)-1; ++$i) {
$j = mt_rand($i,count($tag_a)-1);
$x = $tag_a[$i]; $tag_a[$i] = $tag_a[$j]; $tag_a[$j] = $x;
}
// javascript to create array, sort, and output tag
$code = "a=Array(";
$code .= $tag_a[0];
for ($i=1; $i<count($tag_a); ++$i)
$code .= ",".$tag_a[$i];
$code .= "); a.sort(); for (i=0; i<a.length; ++i)".
" document.write(String.fromCharCode(a[i]%256));";
// complete javascript program
return
"<script type=\"text/javascript\">".
$code.
"</script>".
$text.
"<script type=\"text/javascript\">".
"document.write(\"</a>\");".
"</script>";
}
?>