1
robstockley
Converting email addresses as images

I manage a site based on XOOPS and one of my users was concerned about the potential for email addresses to be harvested. I've since introduced a hack to convert email addresses into images on the fly. This works both for tagged emails in articles and for the emails displayed in the user's profile. For an example check this link.

Basically there are four elements to this hack.
1. a php script to generate the image
2. an update to module.textsanitiser.php
3. an update to userinfo.php
4. an update to the userinfo template

The image generation script is pretty rough. I'm not a professional and I make no promises as to how well this might work for you. I called it swirl.php and put it in the include folder.
/* script adapted by Rob Stockley from example published at
* http://nz2.php.net/manual/en/function.imagestring.php
* by brooks dot boyd at gmail dot com
*/
if (($_GET['l1'] != "")&&($_GET['l2'] != "")) {
    
$msg $_GET['l1'].'@'.$_GET['l2'];
    
// you need to make sure this font file exists
    
$font '/usr/share/fonts/truetype/ttf-bitstream-vera/VeraMono.ttf';

    if (
$_GET['l3'] != "") {
        
$size $_GET['l3'];
        if (
$size 16$size 16;
        if (
$size 8$size 8;
    } else {
        
$size 8;
    }
    
// top, right, bottom, left
    
$padding = array($size/22$size/22);

    
$textbox imageTTFBbox($size0$font$msg);
    
$textwidth abs($textbox[4] - $textbox[0]);
    
$totalwidth $textwidth $padding[1] + $padding[3];
    
$textheight abs($textbox[5] - $textbox[1]);
    
$totalheight $textheight $padding[0] + $padding[2];
    if (
$textbox[1] > $textbox[3]) {
        
$textorigin = array($padding[3], ($totalheight $padding[2] - $textbox[1]));
    } else {
        
$textorigin = array($padding[3], ($totalheight $padding[2] - $textbox[3]));
    }
    
$im imagecreatetruecolor($totalwidth,$totalheight);
    
imagealphablending($imFALSE);
    
imagesavealpha($imTRUE);
    
// Create some colors
    
$white imagecolorallocatealpha($im255255255100);
    
$grey imagecolorallocatealpha($im128128128100);
    
$black imagecolorallocatealpha($im0000);

    
// Add the text with a shadow
    
imagefilledrectangle ($im00$totalwidth$totalheight$white); # Make transparent
    
imagettftext($im$size0$textorigin[0]+1$textorigin[1]+1$grey$font$msg);
    
imagettftext($im$size0$textorigin[0], $textorigin[1], $black$font$msg);

    
// create a blank image to copy into
    
$dest imagecreatetruecolor($totalwidth,$totalheight);
    
imagealphablending($destFALSE);
    
imagesavealpha($destTRUE);
    
imagefilledrectangle ($dest00$totalwidth$totalheight$white); # Make transparent

    // make it wavy
    
$yperiod $size*3;
    
$yamp min($padding[0], $padding[2])-1;
    for (
$i 1$i $totalwidth$i++) {
        
imagecopy($dest$im$i$off$i01$totalheight);
        
$off sin($i/$yperiod)*$yamp;
    }
} else {
    
$im imagecreatetruecolor(1,1);
    
imagealphablending($imFALSE);
    
imagesavealpha($imTRUE);
    
$bg imagecolorallocatealpha($im25500125);
    
imagefilledrectangle ($im0011$bg); # Make transparent
}
header('Content-type: image/jpg');
imagepng($dest);
imagedestroy($im);
imagedestroy($dest);
?>


Next I modified module.textsanitiser.php to tie in the email tags. I haven't modified the textbox to include font size though. Users can add that manually if required. The default size 8 works well on my site.
// The following changed by Rob Stockley to include image emails
//$patterns[] = "/[email]([^;<>*()"']*)[/email]/sU";
//$replacements[] = '\1';
$patterns[] = "/[email]([^;<>*()"'@]*)@([^;<>*()"'@]*)[/email]/sU";
$replacements[] = '/include/swirl.php?l1=\1&l2=\2">';
$patterns[] = "/[email=(['"]?)([0-9]*)\1]([^;<>*()"'@]*)@([^;<>*()"'@]*)[/email]/sU";
$replacements[] = '.XOOPS_URL.'/include/swirl.php?l1=\3&l2=\4&l3=\2">';


Next I had to modify userinfo.php. I chose to add a new smarty tag user_email_img to the template rather than delete teh existing code for emails. This way I can roll back the change (if required) simply by updating the template.

// added by Rob 25 May 07 to incorporate image emails
list($e_name$e_domain) = explode("@"$thisUser->getVar('email'));
$e_img '.XOOPS_URL.'/include/swirl.php?l1='.$e_name.'&l2='.$e_domain.'">';

if (
$thisUser->getVar('user_viewemail') == 1) {
    
$xoopsTpl->assign('user_email'$thisUser->getVar('email''E'));
    
$xoopsTpl->assign('user_email_img'$e_img); //added by Rob
} else {
    if (
is_object($xoopsUser)) {
        
// All admins will be allowed to see emails, even those that are not allowed to edit users (I think it's ok like this)
        
if ($xoopsUserIsAdmin || ($xoopsUser->getVar("uid") == $thisUser->getVar("uid"))) {
            
$xoopsTpl->assign('user_email'$thisUser->getVar('email''E'));
            
$xoopsTpl->assign('user_email_img'$e_img); // added by Rob
        
} else {
            
$xoopsTpl->assign('user_email'' ');
            
$xoopsTpl->assign('user_email_img'' '); //added by Rob
        
}
    }
}


The last change was done through the XOOPS site admin pages. Under the system templates I editted system_userinfo.html and switched to the new user_email_img tag about half way down.

I know this approach is not bullet proof but it has satisfied my users for the moment. I hope someone else finds it of use.

Rob

2
robstockley
Re: Converting email addresses as images

Doh!!!!

Just found this excellent post.

Not sure how I can sort the email tags yet but this approach appears superior to the one I've taken.

Rob

3
kerkyra
Re: Converting email addresses as images
  • 2007/5/25 6:17

  • kerkyra

  • Just can't stay away

  • Posts: 553

  • Since: 2005/2/14


I tried that way and it gave me a white picture! Didn't work for me. I'm using XOOPS 2.0.16. :(
whttp://www.guidemap.gr - Beta is out...

4
robstockley
Re: Converting email addresses as images

Most likely the font file doesn't exist on your server or there is no GD support.

5
kerkyra
Re: Converting email addresses as images
  • 2007/5/25 16:47

  • kerkyra

  • Just can't stay away

  • Posts: 553

  • Since: 2005/2/14


i did upload the font file in the include folder as described in the readme txt file. And i'm using gd2 in the server for xcgal as well! Perhaps the problem lies somewhere else??? Have you tried it in 2.0.16? Any hints are welcome, i'd like to use this hack!Thanks
whttp://www.guidemap.gr - Beta is out...

6
robstockley
Re: Converting email addresses as images

Ah, I see you are using KuBaZ's solution and not mine. I have not installed that version so am not sure what is wrong. My solution works with 2.0.15 using plain GD.

I've modified my approach a bit now. At first I liked KuBaZ's solution but I couldn't see an easy way to implement it for the email xoopscodes. Once I've done some more testing of my final solution I'll upload the results.

7
robstockley
Re: Converting email addresses as images

I've had a closer look and notice that you also require the FreeType library to use true type fonts and imagettftext().

Try commenting out this line
imagettftext($image$fontSize0015$fontColor$font$emailAddress);


And uncommenting this line
// ImageString($image,3,2,2,$emailAddress,$fontColor);


And see if that helps. The font won't be as pretty but it might help narrow down your problem.

8
kerkyra
Re: Converting email addresses as images
  • 2007/5/25 21:22

  • kerkyra

  • Just can't stay away

  • Posts: 553

  • Since: 2005/2/14


you are the man!!! that was it! It works now and the font looks just fiinee! Thanks mate!

PS: Anyway to work for msnm and yim in the user info perhaps?
whttp://www.guidemap.gr - Beta is out...

9
robstockley
Re: Converting email addresses as images

You could add another field to emailprotection.php but the code might start to look a bit messy.

My new solution extends the xoopsObject class by adding a new output format to the getVar function. I haven't tested fully but in theory you'll be able to configure any textbox to be displayed as an image. I use TT fonts so I'm just figuring out how to check for the presence of the FreeType library before adding a fallback option to use the GD default fonts.

10
skenow
Re: Converting email addresses as images
  • 2007/5/26 0:10

  • skenow

  • Home away from home

  • Posts: 993

  • Since: 2004/11/17


Quote:

robstockley wrote:
... I'm just figuring out how to check for the presence of the FreeType library before adding a fallback option to use the GD default fonts.


Use function_exists() and check for imageftbbox () - http://us.php.net/imageftbbox

Login

Who's Online

387 user(s) are online (281 user(s) are browsing Support Forums)


Members: 0


Guests: 387


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits