User:Tdittmar/MailObfuscator

 
A proposal to move this page to MediaWiki.org was rejected.

Intro

edit

In many cases you need to publish your eMail address on your page, however, more and more Robots are sent through the Internet to collect eMail addresses later used to send Spam mail. This little extension is meant to provide a means to hide your eMail address from such robots. It does not guarantee 100% safety, but it's a step into the right direction :-)


How the extension works

edit

The extension consists of two files:

  • The extension itself (a PHP file)
  • A JavaScript file, which provides one function

The extension is invoked for the new <email> tag you can use to specify an eMail address. First of all, it parses the content of this tag for the address and the optional link label. It validates the eMail address against a regular expression and returns HTML-code which calls the function provided by the JavaScript file. The JavaScript function inserts a proper mailto: link into the page. In case JavaScript is disabled, the extension also outputs a <noscript> section that outputs a message to the user, telling him to activate JavaScript.


How to use the extension

edit

To enable the extension, copy both MailObfuscator.php and MailObfuscator.js to the extensions directory of your MediaWiki installation. Then, you must include the script in all templates that you provide for your page. I'm using the following code:

<script type="text/javascript" src="/extensions/MailObfuscator.js"></script>

NOTE: You may not need the leading slash before the extensions folder, eg:

<script type="text/javascript" src="extensions/MailObfuscator.js"></script>

Of course you must also register the extension by adding

require_once( "extensions/MailObfuscator.php" );

to your LocalSettings.php file.

From now on, you may use the <email> tag as follows:

<email>john@doe.test|John Doe</email>
<email>john@doe.test</email>

The first produces a mail link like John Doe, the second line would result in john@doe.test, formatted like a usual eMail link.

Customize it

edit

Of course, you will have to customize the extension by changing these variables

  • $mailObfuscate_ERR_Invalid_Address: The given eMail address is invalid
  • $mailObfuscate_MSG_Activate_JavaScript: Hint saying you should activate JavaScript


History

edit

Version 0.2

  • Code cleanup

Version 0.1

  • Initial version


To Do

edit
  • No not hardcode messages. Take them from the database instead. HOW?


The Code

edit

This is what goes into MailObfuscator.js:

 function writeMail($address,$domain,$name)
 {
   $sign = "@";
   
   document.write('<a href="mailto:');
   document.write($address+$sign+$domain);
   document.write('" class="external free" rel="nofollow">'+$name);
   document.write('</a>');
 }
 

Save the following as MailObfuscator.php:

<?php
## MailObfuscator.php
## version 0.2
## Written by Thorsten Dittmar <thorsten.dittmar@dithosoft.de>
## Extension for MediaWiki that hides eMail addresses from robots.


## The "main" function 
$wgExtensionFunctions[] = 'mailObfuscate_Install';


## Constants for message output
$mailObfuscate_ERR_Invalid_Address = "Invalid eMail address";
$mailObfuscate_MSG_Activate_JavaScript = "(Please activate JavaScript in order to see the eMail address)";


##
## Installs the extension
##
function mailObfuscate_Install() 
{
  global $wgParser, $cshLanguages;
  
  # register the extension with the WikiText parser
  $wgParser->setHook("email","mailObfuscate_Render");     
}


##
## Encodes the given text in HTML character entities
##
function mailObfuscate_Encode($text)
{
  $result = "";
  for ($i = 0; $i < strlen($text); $i++)
  {
    $c = substr($text,$i,1);
    $result .= "&#".ord($c).";";
  }
  return $result;
}


##
## Checks whether the given eMail address has a valid structure.
##
##
## The regular expression used here was found on: http://www.regexlib.com
##
## -- Author information from RegExLib.com --
## Author: Andy Smith
## Source: Todd Moon
##
function mailObfuscate_IsValidAddress($address)
{
  return preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',$address);
}

 
##
## Renders the <email> tag
##
function mailObfuscate_Render($source, $argv)
{
  // Split the content in to the mail address and the optional label
  list($address,$label,$dummy) = explode("|",$source);
  
  // If the address is empty, we do not include the link.
  // Otherwise, we split the address into name and domain
  if ($address === "") return "";
  list($name,$domain) = explode("@",$address);
  
  // If the label is empty, we assign the html-encoded address
  if (!isset($label) || $label === "") 
    $label = mailObfuscate_Encode($address);
  
  // If the eMail address is invalid, we output a message
  if (!mailObfuscate_IsValidAddress($address))
    return "<span style=\"color:red;\">$mailObfuscate_ERR_Invalid_Address</span>";

  // Now we can encode:
  $output  = "<script type=\"text/javascript\">writeMail(\"$name\",\"$domain\",\"$label\");</script>";
  $output .= "<noscript><span style=\"font-style:italic\">$mailObfuscate_MSG_Activate_JavaScript</span></noscript>";
  
  return $output;
}
?>