Extension:EmailObfuscator

From MediaWiki.org

Jump to: navigation, search

       

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Email Obfuscator

Release status: stable

Implementation  Tag
Description This extension obfuscates email addresses within <mail></mail> tags.
Last Version  1.0
MediaWiki  1.9.3
License No license specified
Download Source code

check usage (experimental)

Contents

[edit] What can this extension do?

This extension obfuscates email addresses by replacing every character with its equivalent ASCII code for HTML so that spiders 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.

[edit] Usage

The email address must be specified in this tag format: <mail address='admin@example.com' description='some description'>Some Text</mail>.

[edit] Installation

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

[edit] Parameters

[edit] Changes to LocalSettings.php

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

[edit] 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' );
}
 
# 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;
}

[edit] Download

The script is available for download at the following location: http://www.esnips.com/doc/9fa9116c-0c00-42f7-aaf4-629fa7e02be3/obfuscate_email

A screenshot of 'before' and 'after' the extension was enabled is available for download at the following location: http://www.esnips.com/doc/4d2f3e22-27e6-4986-a4d3-17bbf5cd5bdc/obfuscate_email_screenshot

[edit] See also