Page 1 of 1

Convert mailto links to images

Posted: Fri Sep 05, 2008 10:59 am
by Phydeaux
I am trying to convert mailto: links to png images for thwarting spambots. I do have a solution working, but it doesn't appear that it would thwart any bot as the email is still there. My users use a custom built CMS system and design their own webpages online. I tell them not to include email addresses, but you know how people listen. Here is some sample code on the display page, after pulling $content from the database:

Code: Select all

$emailreg = "/<a href=\"mailto:([[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+)\">[[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+<\/a>/";
$emailrep = "<img src=\"/includes/texttoimage.php?text=$1\">";
$content = preg_replace($emailreg, $emailrep, $content);
This works great, the texttoimage.php file uses imagettftext and other things to generate the png. However, the source code still has the email address as an argument. A couple of thoughts were to use the texttoimage as a function so it never leaves the code, or to hash/obfuscate the email address, then reverse it before it is written, a la Facebook. The latter might be the easiest, but I'm not sure how to do it and reverse it in the texttoimage. Any thoughts? Thanks.

Re: Convert mailto links to images

Posted: Fri Sep 05, 2008 11:24 am
by andyhoneycutt
You could base64_encode it, pass that as an argument, then base64_decode it on the other end (in your text_to_image function, before you use the email address).

-Andy

Re: Convert mailto links to images

Posted: Fri Sep 05, 2008 11:45 am
by Phydeaux
How do I separate it so only the $1 is encoded? Is this going to be in a separate statement, using preg_grep or preg_match first?

Re: Convert mailto links to images

Posted: Fri Sep 05, 2008 11:46 am
by andyhoneycutt
do whatever processing you would want done to $1 before you pass it to the function. just before you pass it to your function, encode it.

Re: Convert mailto links to images

Posted: Fri Sep 05, 2008 12:37 pm
by Phydeaux
I'm assuming I would use preg_replace_callback, but I'm not finding some useful examples on this. Thank you for your help, you're getting me on the right track, I'm just not getting how to extract the sub expression to use.

Re: Convert mailto links to images

Posted: Fri Sep 05, 2008 1:45 pm
by Phydeaux
Ooo! I figured it out. For those who want to know:

Code: Select all

$emailreg = "/<a href=\"mailto:([[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+)\">[[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+<\/a>/";
    function obfuscate_email($string) {
        $emailrep = "<img src=\"/includes/texttoimage.php?text=";
        $myreturn = $emailrep . base64_encode($string[1]) . "\">";
        return $myreturn;
    }
    $content = preg_replace_callback($emailreg, 'obfuscate_email', $content);
Then my texttoimage file base_64decodes it.