E-mail Address Obfuscator (txp:dtj_obfuscated_email /)

E-mail Address Obfuscator (txp:dtj_obfuscated_email /) main page

A simple plugin that encodes ("obfuscates") an e-mail address link for you, helping to reduce the likelihood of your e-mail address getting harvested.

It's quite simple to use. The tag takes an email attribute (the address to be encoded) and an encoding mode (case insensitive).

Classification

The dtj_obfuscated_email tag is a Single_Tag. Textpattern will replace this tag with an encoded mailto: link, according to the attributes set.

Attributes

email
The e-mail address to be encoded.
mode
The encoding method to use:
basic
A really basic mode that just encodes the @ symbol and periods.
iso
Encodes the address with ISO character entities.
hex
Encodes the address with Hex character entities.
mixed (default)
Randomly encodes the address using a mix of ISO and Hex encoding methods.

This attribute is not case-sensitive.

output
The type of output required (new to version 0.3):
link (default)
A complete HTML encoded mailto: hyperlink is returned.
plain
Just the encoded e-mail address is returned.

This attribute is not case-sensitive.

Syntax:

<txp:dtj_obfuscated_email email="encoded address" />

Examples

Example 1: Encode with ISO character entities (pretty much as Textpattern's own email tag)

<txp:dtj_obfuscated_email email="noone@example.tld" mode="iso" />

Example 2: Just return a Hex-encoded e-mail address.

<txp:dtj_obfuscated_email email="noone@example.tld" mode="hex" output="plain" />

Plugin code


// dotjay obfuscated e-mail address plugin
function dtj_obfuscated_email($atts) {
	extract(lAtts(array(
		'email'    => '',
		'mode'     => 'mixed',
		'output'     => 'link'
	),$atts));

	$mode = strtolower($mode);
	$output = strtolower($output);

	// Normal mode
	if ($mode == 'basic') {
		list($username, $domain) = split("@", $email);
		if ($output == 'link') return '<a href="'.str_replace(".",'&#37;2e','m&#97;ilto:'.$username.'&#37;40'.$domain).'">'.str_replace(".",'&#x2E;',$username.'&#064;'.$domain).'</a>';
		else if ($output == 'plain') return str_replace(".",'&#x2E;',$username.'&#064;'.$domain);
		else return false;
	}

	// Hex/ISO/Mixed modes
	else {
		$encoded = '';
		$reencoded = '';

		for ($i=0;$i<strlen($email);++$i) {
			if ($mode == 'hex') $encoded .= '&#x'.sprintf("%X",ord($email{$i})).';';
			else if ($mode == 'iso') $encoded .= '&#'.ord($email{$i}).';';
			else if ($mode == 'mixed') $encoded .= rand(0,1) ? '&#x'.sprintf("%X",ord($email{$i})).';' : '&#'.ord($email{$i}).';';
		}

		if ($output == 'link') {
			$email = 'mailto:'.$email;

			for ($i=0;$i<strlen($email);++$i) {
				if ($mode == 'hex') $reencoded .= '&#x'.sprintf("%X",ord($email{$i})).';';
				else if ($mode == 'iso') $reencoded .= '&#'.ord($email{$i}).';';
				else if ($mode == 'mixed') $reencoded .= rand(0,1) ? '&#x'.sprintf("%X",ord($email{$i})).';' : '&#'.ord($email{$i}).';';
			}
		}

		// if all went well, we should have an encoded e-mail address
		if (!empty($encoded)) {
			if ($output == 'link') return '<a href="'.$reencoded.'">'.$encoded.'</a>';
			else if ($output == 'plain') return $encoded;
			else return false;
		}

		// unknown mode was set
		else return false;
	}
}

Installation

Textpattern plugins are base64 encoded. To install, copy and paste the plugin code into the textarea on the plugin tab in Textpattern admin.

Remember to activate the plugin before you use it... I always forget.