<?php 
/*
    Copyright (C) 2008 Aymeric AUGUSTIN

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


/*
    EMAIL ADDRESS ENCODER
    
    This script encodes emails using &#XXX; entities.
    As of the writing of this script, this is a sufficient protection againt
    email address crawlers.
    
    It requires PHP >= 5.0.0
*/


////// UTILITY FUNCTIONS

function entity($char) {
    return 
'&#' ord($char) . ';';
}

function 
mail_link($email$text '') {
    if (
$text == '')
        
$text $email;
    
$email_crypt implode(''array_map('entity'str_split("mailto:$email")));
    
$text_crypt implode(''array_map('entity'str_split($text)));
    return 
"<a href=\"$email_crypt\">$text_crypt</a>";
}

function 
valid_email($email) {
    
/* see http://www.regular-expressions.info/email.html */
    /* this is a "theoretical regex", not a good one */
    
$rfc_2822 = <<< REGEX
(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
REGEX
;
    return (
preg_match('/^' $rfc_2822 '$/i'strtolower($email)) == 1);
}

//// SCRIPT

// process POST arguments
if (get_magic_quotes_gpc()) {
    
$email stripslashes($_POST['email']);
    
$text stripslashes($_POST['text']);
} else {
    
$email $_POST['email'];
    
$text $_POST['text'];
}

// todo add email validation regexp

// a result is generated only if the email is not empty
$encoded $email mail_link($email$text) : '';

//// PAGE OUTPUT

if (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE') === false) {
    
header("Content-Type: application/xhtml+xml; charset=us-ascii");
    echo 
'<?xml version="1.0" encoding="us-ascii" ?>'"\n";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>Email address encoder</title>
<link rel="stylesheet" type="text/css" media="screen" href="/css/clear.css" />
<link rel="icon" type="image/png" href="/images/favicon.png" />
<link rel="index" href="/index.php" />
</head>

<body>

<div id="global">

<div id="content">

<h2>Email address encoder</h2>

<h3>Overview</h3>

<p>Email address displayed on web pages are harvested by spammers using automatic web crawlers, and get quickly a lot of spam. There are a lot a workarounds for this problem. The following encryption method provides reasonnably good protection and will never prevent a human from using the email adress.</p>

<p>Just enter the email address and the text of the link if it's different from the email, and use the generated code in your web page.</p>

<h3>Enter email address</h3>

<form accept-charset="US-ASCII" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="post">

<table style="margin: auto;" summary="email and text input form">

<tr>
<td style="text-align: right;"><label for="email">Email address:</label></td>
<td style="text-align: left;"><input id="email" name="email" size="16" type="text" 
value="<?php echo htmlentities($email); ?>" /></td>
</tr>

<?php if (!$email && $_POST['generate']) { ?>
<tr>
<td colspan="2" style="text-align: center;">
<em style="color: red;">Empty email!</em></td>
</tr>
<?php ?>


<tr>
<td style="text-align: right;"><label for="text">Link text <i>(optional)</i>:</label></td>
<td style="text-align: left;"><input id="text" name="text" size="16" type="text" 
value="<?php echo htmlentities($text); ?>" /></td>
</tr>

<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="Generate encoded email link" name="generate"/>
</td>
</tr>

</table>

</form>

<?php if ($encoded) { ?>
<h3>Result</h3>

<?php if (!valid_email($email)) { ?>
<p style="text-align: center;"><em style="color: red;">Warning: this email address does no appear to be valid.</em></p>
<?php ?>

<p style="font-size: 130%; text-align: center; color: black; overflow: scroll;">
<?php echo str_replace(' ''&nbsp;'htmlentities($encoded)) . "\n"?>
</p>

<p>The previous code will show up in a web page as follows:</p>

<p style="font-size: 130%; text-align: center; color: black;">
<?php echo $encoded "\n"?>
</p>

<?php ?>

<h3>Remarks</h3>

<p>
You may wonder if it is secure to enter your email address in this form. Indeed, 
I could use this page to harvest email addresses and sell them to spammers. I do 
not log entries, and if you don't trust me, don't use this tool. You can also 
check <a href="/utilities/email_encode.phps">the source</a>.
</p>

</div>

<div id="footer">
<span id="copyright">&copy; 2007 Aymeric Augustin</span>
<a href="http://validator.w3.org/check/referer">
<img src="/images/xhtml11.png" alt="Valid XHTML 1.1!" height="15" width="80"/>
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="/images/css.png" alt="Valid CSS!" height="15" width="80"/>
</a>
</div>

</div>

</body>

</html>