<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Chris Morrell &#187; code</title>
	<atom:link href="http://cmorrell.com/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://cmorrell.com</link>
	<description>The personal home page of Chris Morrell</description>
	<lastBuildDate>Thu, 02 Sep 2010 01:05:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>E-Mail Address Obfuscator Class</title>
		<link>http://cmorrell.com/webdev/email-address-obfuscator-87</link>
		<comments>http://cmorrell.com/webdev/email-address-obfuscator-87#comments</comments>
		<pubDate>Thu, 02 Oct 2008 21:05:40 +0000</pubDate>
		<dc:creator>inxilpro</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://cmorrell.com/?p=87</guid>
		<description><![CDATA[A project I&#8217;m working on needs to output text that may have e-mail addresses in it to the Web. Because we don&#8217;t want people&#8217;s e-mail addresses exposed to spammers, I had to write an obfuscation class to make sure those &#8230; <a href="http://cmorrell.com/webdev/email-address-obfuscator-87">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A project I&#8217;m working on needs to output text that may have e-mail addresses in it to the Web.  Because we don&#8217;t want people&#8217;s e-mail addresses exposed to spammers, I had to write an obfuscation class to make sure those e-mail addresses are hidden somehow.  I haven&#8217;t decided what method to hide those addresses with yet (I&#8217;m leaning towards <a href="http://mailhide.recaptcha.net/">Mailhide</a>), but I thought I&#8217;d post the base code now.  The class is written so that you can extend it to hide the addresses however you like.  Here&#8217;s the class (PHP 5 only, but would be easy to convert to PHP 4).<br />
<span id="more-87"></span></p>
<pre class="brush: php;">
/**
 * Simple Obfuscation Class
 *
 * The beginning of an obfuscation class, made primarily to obfuscate
 * email addresses within a block of text or HTML.  Used in conjunction
 * with reCAPTCHA's Mailhide service  or
 * a database-driven e-mail address hider.
 *
 * LICENSE
 *
 * 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 3 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 .
 *
 * @author     Chris Morrell
 * @copyright  Copyright 2008 Chris Morrell
 * @version    0.1
 * @license    GPL
 *
 */
class Galahad_Obfuscator
{
	const EMAIL_ANCHOR = 101;
	const EMAIL_URL = 102;
	const EMAIL_DISPLAY = 103;
	protected $mailtoRegexp = '/((.*?)&lt;\/a&gt;/is';
	protected $emailRegexp = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
	/**
	 * Public method to obfuscate all e-mail addresses in a string
	 *
	 * @param string $string
	 * @return string
	 */
	public function emailAddresses($string)
	{
		$string = $this-&gt;replaceMailto($string);
		$string = $this-&gt;replaceEmail($string);
		return $string;
	}
	/**
	 * Private function to replace all e-mail addresses with
	 * obfuscated string (most often links)
	 *
	 * @param string $string
	 * @return string
	 */
	private function replaceEmail($string)
	{
		$string = preg_replace_callback($this-&gt;emailRegexp, array($this, 'processEmail'), $string);
		return $string;
	}
	/**
	 * Private function to handle all found e-mail addresses in string
	 *
	 * @param array $matches
	 * @return string
	 */
	private function processEmail($matches)
	{
		return $this-&gt;obfuscateEmail($matches[0], self::EMAIL_ANCHOR);
	}
	/**
	 * Private function to replace all mailto: anchors with
	 * obfuscated anchors
	 *
	 * @param string $string
	 * @return string
	 */
	private function replaceMailto($string)
	{
		$string = preg_replace_callback($this-&gt;mailtoRegexp, array($this, 'processMailto'), $string);
		return $string;
	}
	/**
	 * Private function to handle all found mailto: anchors
	 *
	 * @param array $matches
	 * @return string
	 */
	private function processMailto($matches)
	{
		$newUrl = $this-&gt;obfuscateEmail($matches[2], self::EMAIL_URL);
		$inner = $matches[4];
		if (preg_match($this-&gt;emailRegexp, $inner)) {
			$inner = $this-&gt;obfuscateEmail($inner, self::EMAIL_DISPLAY);
		}
		return &quot;{$matches[1]}{$newUrl}{$matches[3]}&gt;{$inner}&quot;;
	}
	/**
	 * Subclass this method to obfuscate the address however you see
	 * fit (whether with Mailhide or some other system).  Right now
	 * this function isn't very helpful, as it obfuscates all e-mail
	 * addresses and links them to #
	 *
	 * @param string $email
	 * @param int $type
	 * @return string
	 */
	protected function obfuscateEmail($email, $type = self::EMAIL_ANCHOR)
	{
		$email = substr($email, 0, strpos($email, '@') + 1) . '...';
		if ($type == self::EMAIL_ANCHOR) {
			return &quot;&lt;a href=&quot;\&quot;&gt;{$email}&lt;/a&gt;&quot;;
		} else if ($type == self::EMAIL_URL) {
			return &quot;#&quot;;
		}
		return $email;
	}
}
</pre>
<p>Here&#8217;s some sample usage:</p>
<pre class="brush: php;">
class My_Obfuscator extends Galahad_Obfuscator
{
	protected function obfuscateEmail($email, $type = self::EMAIL_ANCHOR)
	{
		$id = $this-&gt;emailToId($email);
		$email = substr($email, 0, strpos($email, '@') + 1) . '...';
		if ($type == self::EMAIL_ANCHOR) {
			return &quot;&lt;a href=&quot;\&quot;&gt;{$email}&lt;/a&gt;&quot;;
		} else if ($type == self::EMAIL_URL) {
			return &quot;http://yoursite.com/get-email.php?id={$id}&quot;;
		}
		return $email;
	}
	private function emailToId($email)
	{
		// Implement this by adding the email to a database
		// and returning the row's ID, for example
		return rand(1,500);
	}
}
$html = 'some string with e-mail addresses in it.';
$obfuscator = new My_Obfuscator();
echo $obfuscator-&gt;emailAddresses($html);
</pre>
<p>So far the class has worked well with limited testing.  Obviously it handles both e-mail addresses in plain text and mailto: links.  Please let me know if you find any bugs in it, or have a suggestion about how to make it better.</p>
<div class="su-linkbox" id="post-87-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://cmorrell.com/webdev/email-address-obfuscator-87&quot;&gt;E-Mail Address Obfuscator Class&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://cmorrell.com/webdev/email-address-obfuscator-87/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
