Jump to content

Extension:EmailObfuscator

From mediawiki.org
MediaWiki extensions manual
Email Obfuscator
Release status: unstable
Implementation Tag
Description This extension obfuscates email addresses within <mail></mail> tags.
Latest version 1.0
MediaWiki 1.9.3
License No license specified
Download Source code

Email Obfuscator obfuscates email addresses by replacing every character with its equivalent ASCII code for HTML so that spamspiders ignore them. The email address must be within <mail> and </mail>. It parses these tags and converts them to <a href=''></a> tags with the email address along with the 'mailto:' obfuscated to keep spiders from spamming.

Usage

The email address must be specified in this tag format: <mail address='[email protected]' description='some description'>Some Text</mail>.

Installation

To install / test the extension kindly add it to the end of your 'LocalSettings.php' file in your MediaWiki installation folder.

Parameters

Changes to LocalSettings.php

require_once("/extensions/EmailObfuscator/obfuscate_email.php");

Code

<?php

# Defines the main function to be executed for this extension.
$wgExtensionFunctions[] = 'obfuscateEmailAddress';

# Sets the hook to be executed once the parser has stripped HTML tags.
$wgHooks['ParserAfterStrip'][] = 'obfuscateEmailAddress';

# This function initiates the hook for the parser to convert <mail></mail>
# tags to obfuscated email addresses.
function obfuscateEmailAddress() {
	// Declaring the global parser..
	global $wgParser;

	// Setting the hook to parse <mail></mail> tags from the parser output..
	$wgParser->setHook( 'mail', 'startObfuscate' );
        return true;
}

# This function extracts the parameters from the <mail></mail> tags and
# the text between the <mail> and </mail> tags and formats them as "<a href=''>"
# tags and writes them in the document.
function startObfuscate( $input, $argv ) {
	// Obfuscating the 'mailto:' text..
	$email_prefix = textObfuscator('mailto:');

	// Fetching the 'address' parameter from the <mail> tag..
	$email = $argv['address'];

	// Fetching the description parameter from the <mail> tag..
	$desc = $argv['description'];

	// Checking for a valid email address..
	$email = preg_replace("/([\\w\\d\\.\\_]+@[\\w\\d\\.\\_]+)/e", "textObfuscator('\$1')", $email);

	// If a description parameter exists.. then obfuscate that too..
	if ($desc != '') {
		// Obfuscate the description since it exists..
		$desc = textObfuscator($desc);

		// If a description parameter exists then print it in the link..
		return "<a href=\"" . htmlspecialchars($email_prefix . $email) . "\" description=\"" . htmlspecialchars($desc) . "\">" . htmlspecialchars($input) . "</a>";
	} else {
		// If a description parameter does not exist then just print the link with the 'mailto' URL..
		return "<a href=\"" . htmlspecialchars($email_prefix . $email) . "\">" . htmlspecialchars($input) . "</a>";
	}
}

# This function does the actual obfuscation
function textObfuscator( $text ) {
	$output = "";
	for( $i=0 ; $i<strlen($text) ; $i++ ){
		// Convert each character in the string to its equivalent ASCII code..
		$output .= "&#". ord(substr($text, $i, 1)) .";";
	}
	// Return the obfuscated string..
	return $output;
}

See also